]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/dwarf2read.c
Automatic date update in version.in
[thirdparty/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 used to pass misc. parameters to read_die_and_children, et
878 al. which are used for both .debug_info and .debug_types dies.
879 All parameters here are unchanging for the life of the call. This
880 struct exists to abstract away the constant parameters of die reading. */
881
882 struct die_reader_specs
883 {
884 /* The bfd of die_section. */
885 bfd* abfd;
886
887 /* The CU of the DIE we are parsing. */
888 struct dwarf2_cu *cu;
889
890 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
891 struct dwo_file *dwo_file;
892
893 /* The section the die comes from.
894 This is either .debug_info or .debug_types, or the .dwo variants. */
895 struct dwarf2_section_info *die_section;
896
897 /* die_section->buffer. */
898 const gdb_byte *buffer;
899
900 /* The end of the buffer. */
901 const gdb_byte *buffer_end;
902
903 /* The value of the DW_AT_comp_dir attribute. */
904 const char *comp_dir;
905
906 /* The abbreviation table to use when reading the DIEs. */
907 struct abbrev_table *abbrev_table;
908 };
909
910 /* Type of function passed to init_cutu_and_read_dies, et.al. */
911 typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
912 const gdb_byte *info_ptr,
913 struct die_info *comp_unit_die,
914 int has_children,
915 void *data);
916
917 /* dir_index is 1-based in DWARF 4 and before, and is 0-based in DWARF 5 and
918 later. */
919 typedef int dir_index;
920
921 /* file_name_index is 1-based in DWARF 4 and before, and is 0-based in DWARF 5
922 and later. */
923 typedef int file_name_index;
924
925 struct file_entry
926 {
927 file_entry () = default;
928
929 file_entry (const char *name_, dir_index d_index_,
930 unsigned int mod_time_, unsigned int length_)
931 : name (name_),
932 d_index (d_index_),
933 mod_time (mod_time_),
934 length (length_)
935 {}
936
937 /* Return the include directory at D_INDEX stored in LH. Returns
938 NULL if D_INDEX is out of bounds. */
939 const char *include_dir (const line_header *lh) const;
940
941 /* The file name. Note this is an observing pointer. The memory is
942 owned by debug_line_buffer. */
943 const char *name {};
944
945 /* The directory index (1-based). */
946 dir_index d_index {};
947
948 unsigned int mod_time {};
949
950 unsigned int length {};
951
952 /* True if referenced by the Line Number Program. */
953 bool included_p {};
954
955 /* The associated symbol table, if any. */
956 struct symtab *symtab {};
957 };
958
959 /* The line number information for a compilation unit (found in the
960 .debug_line section) begins with a "statement program header",
961 which contains the following information. */
962 struct line_header
963 {
964 line_header ()
965 : offset_in_dwz {}
966 {}
967
968 /* Add an entry to the include directory table. */
969 void add_include_dir (const char *include_dir);
970
971 /* Add an entry to the file name table. */
972 void add_file_name (const char *name, dir_index d_index,
973 unsigned int mod_time, unsigned int length);
974
975 /* Return the include dir at INDEX (0-based in DWARF 5 and 1-based before).
976 Returns NULL if INDEX is out of bounds. */
977 const char *include_dir_at (dir_index index) const
978 {
979 int vec_index;
980 if (version >= 5)
981 vec_index = index;
982 else
983 vec_index = index - 1;
984 if (vec_index < 0 || vec_index >= m_include_dirs.size ())
985 return NULL;
986 return m_include_dirs[vec_index];
987 }
988
989 bool is_valid_file_index (int file_index)
990 {
991 if (version >= 5)
992 return 0 <= file_index && file_index < file_names_size ();
993 return 1 <= file_index && file_index <= file_names_size ();
994 }
995
996 /* Return the file name at INDEX (0-based in DWARF 5 and 1-based before).
997 Returns NULL if INDEX is out of bounds. */
998 file_entry *file_name_at (file_name_index index)
999 {
1000 int vec_index;
1001 if (version >= 5)
1002 vec_index = index;
1003 else
1004 vec_index = index - 1;
1005 if (vec_index < 0 || vec_index >= m_file_names.size ())
1006 return NULL;
1007 return &m_file_names[vec_index];
1008 }
1009
1010 /* The indexes are 0-based in DWARF 5 and 1-based in DWARF 4. Therefore,
1011 this method should only be used to iterate through all file entries in an
1012 index-agnostic manner. */
1013 std::vector<file_entry> &file_names ()
1014 { return m_file_names; }
1015
1016 /* Offset of line number information in .debug_line section. */
1017 sect_offset sect_off {};
1018
1019 /* OFFSET is for struct dwz_file associated with dwarf2_per_objfile. */
1020 unsigned offset_in_dwz : 1; /* Can't initialize bitfields in-class. */
1021
1022 unsigned int total_length {};
1023 unsigned short version {};
1024 unsigned int header_length {};
1025 unsigned char minimum_instruction_length {};
1026 unsigned char maximum_ops_per_instruction {};
1027 unsigned char default_is_stmt {};
1028 int line_base {};
1029 unsigned char line_range {};
1030 unsigned char opcode_base {};
1031
1032 /* standard_opcode_lengths[i] is the number of operands for the
1033 standard opcode whose value is i. This means that
1034 standard_opcode_lengths[0] is unused, and the last meaningful
1035 element is standard_opcode_lengths[opcode_base - 1]. */
1036 std::unique_ptr<unsigned char[]> standard_opcode_lengths;
1037
1038 int file_names_size ()
1039 { return m_file_names.size(); }
1040
1041 /* The start and end of the statement program following this
1042 header. These point into dwarf2_per_objfile->line_buffer. */
1043 const gdb_byte *statement_program_start {}, *statement_program_end {};
1044
1045 private:
1046 /* The include_directories table. Note these are observing
1047 pointers. The memory is owned by debug_line_buffer. */
1048 std::vector<const char *> m_include_dirs;
1049
1050 /* The file_names table. This is private because the meaning of indexes
1051 differs among DWARF versions (The first valid index is 1 in DWARF 4 and
1052 before, and is 0 in DWARF 5 and later). So the client should use
1053 file_name_at method for access. */
1054 std::vector<file_entry> m_file_names;
1055 };
1056
1057 typedef std::unique_ptr<line_header> line_header_up;
1058
1059 const char *
1060 file_entry::include_dir (const line_header *lh) const
1061 {
1062 return lh->include_dir_at (d_index);
1063 }
1064
1065 /* When we construct a partial symbol table entry we only
1066 need this much information. */
1067 struct partial_die_info : public allocate_on_obstack
1068 {
1069 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
1070
1071 /* Disable assign but still keep copy ctor, which is needed
1072 load_partial_dies. */
1073 partial_die_info& operator=(const partial_die_info& rhs) = delete;
1074
1075 /* Adjust the partial die before generating a symbol for it. This
1076 function may set the is_external flag or change the DIE's
1077 name. */
1078 void fixup (struct dwarf2_cu *cu);
1079
1080 /* Read a minimal amount of information into the minimal die
1081 structure. */
1082 const gdb_byte *read (const struct die_reader_specs *reader,
1083 const struct abbrev_info &abbrev,
1084 const gdb_byte *info_ptr);
1085
1086 /* Offset of this DIE. */
1087 const sect_offset sect_off;
1088
1089 /* DWARF-2 tag for this DIE. */
1090 const ENUM_BITFIELD(dwarf_tag) tag : 16;
1091
1092 /* Assorted flags describing the data found in this DIE. */
1093 const unsigned int has_children : 1;
1094
1095 unsigned int is_external : 1;
1096 unsigned int is_declaration : 1;
1097 unsigned int has_type : 1;
1098 unsigned int has_specification : 1;
1099 unsigned int has_pc_info : 1;
1100 unsigned int may_be_inlined : 1;
1101
1102 /* This DIE has been marked DW_AT_main_subprogram. */
1103 unsigned int main_subprogram : 1;
1104
1105 /* Flag set if the SCOPE field of this structure has been
1106 computed. */
1107 unsigned int scope_set : 1;
1108
1109 /* Flag set if the DIE has a byte_size attribute. */
1110 unsigned int has_byte_size : 1;
1111
1112 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1113 unsigned int has_const_value : 1;
1114
1115 /* Flag set if any of the DIE's children are template arguments. */
1116 unsigned int has_template_arguments : 1;
1117
1118 /* Flag set if fixup has been called on this die. */
1119 unsigned int fixup_called : 1;
1120
1121 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1122 unsigned int is_dwz : 1;
1123
1124 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1125 unsigned int spec_is_dwz : 1;
1126
1127 /* The name of this DIE. Normally the value of DW_AT_name, but
1128 sometimes a default name for unnamed DIEs. */
1129 const char *name = nullptr;
1130
1131 /* The linkage name, if present. */
1132 const char *linkage_name = nullptr;
1133
1134 /* The scope to prepend to our children. This is generally
1135 allocated on the comp_unit_obstack, so will disappear
1136 when this compilation unit leaves the cache. */
1137 const char *scope = nullptr;
1138
1139 /* Some data associated with the partial DIE. The tag determines
1140 which field is live. */
1141 union
1142 {
1143 /* The location description associated with this DIE, if any. */
1144 struct dwarf_block *locdesc;
1145 /* The offset of an import, for DW_TAG_imported_unit. */
1146 sect_offset sect_off;
1147 } d {};
1148
1149 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1150 CORE_ADDR lowpc = 0;
1151 CORE_ADDR highpc = 0;
1152
1153 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1154 DW_AT_sibling, if any. */
1155 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1156 could return DW_AT_sibling values to its caller load_partial_dies. */
1157 const gdb_byte *sibling = nullptr;
1158
1159 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1160 DW_AT_specification (or DW_AT_abstract_origin or
1161 DW_AT_extension). */
1162 sect_offset spec_offset {};
1163
1164 /* Pointers to this DIE's parent, first child, and next sibling,
1165 if any. */
1166 struct partial_die_info *die_parent = nullptr;
1167 struct partial_die_info *die_child = nullptr;
1168 struct partial_die_info *die_sibling = nullptr;
1169
1170 friend struct partial_die_info *
1171 dwarf2_cu::find_partial_die (sect_offset sect_off);
1172
1173 private:
1174 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1175 partial_die_info (sect_offset sect_off)
1176 : partial_die_info (sect_off, DW_TAG_padding, 0)
1177 {
1178 }
1179
1180 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1181 int has_children_)
1182 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1183 {
1184 is_external = 0;
1185 is_declaration = 0;
1186 has_type = 0;
1187 has_specification = 0;
1188 has_pc_info = 0;
1189 may_be_inlined = 0;
1190 main_subprogram = 0;
1191 scope_set = 0;
1192 has_byte_size = 0;
1193 has_const_value = 0;
1194 has_template_arguments = 0;
1195 fixup_called = 0;
1196 is_dwz = 0;
1197 spec_is_dwz = 0;
1198 }
1199 };
1200
1201 /* This data structure holds the information of an abbrev. */
1202 struct abbrev_info
1203 {
1204 unsigned int number; /* number identifying abbrev */
1205 enum dwarf_tag tag; /* dwarf tag */
1206 unsigned short has_children; /* boolean */
1207 unsigned short num_attrs; /* number of attributes */
1208 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1209 struct abbrev_info *next; /* next in chain */
1210 };
1211
1212 struct attr_abbrev
1213 {
1214 ENUM_BITFIELD(dwarf_attribute) name : 16;
1215 ENUM_BITFIELD(dwarf_form) form : 16;
1216
1217 /* It is valid only if FORM is DW_FORM_implicit_const. */
1218 LONGEST implicit_const;
1219 };
1220
1221 /* Size of abbrev_table.abbrev_hash_table. */
1222 #define ABBREV_HASH_SIZE 121
1223
1224 /* Top level data structure to contain an abbreviation table. */
1225
1226 struct abbrev_table
1227 {
1228 explicit abbrev_table (sect_offset off)
1229 : sect_off (off)
1230 {
1231 m_abbrevs =
1232 XOBNEWVEC (&abbrev_obstack, struct abbrev_info *, ABBREV_HASH_SIZE);
1233 memset (m_abbrevs, 0, ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
1234 }
1235
1236 DISABLE_COPY_AND_ASSIGN (abbrev_table);
1237
1238 /* Allocate space for a struct abbrev_info object in
1239 ABBREV_TABLE. */
1240 struct abbrev_info *alloc_abbrev ();
1241
1242 /* Add an abbreviation to the table. */
1243 void add_abbrev (unsigned int abbrev_number, struct abbrev_info *abbrev);
1244
1245 /* Look up an abbrev in the table.
1246 Returns NULL if the abbrev is not found. */
1247
1248 struct abbrev_info *lookup_abbrev (unsigned int abbrev_number);
1249
1250
1251 /* Where the abbrev table came from.
1252 This is used as a sanity check when the table is used. */
1253 const sect_offset sect_off;
1254
1255 /* Storage for the abbrev table. */
1256 auto_obstack abbrev_obstack;
1257
1258 private:
1259
1260 /* Hash table of abbrevs.
1261 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1262 It could be statically allocated, but the previous code didn't so we
1263 don't either. */
1264 struct abbrev_info **m_abbrevs;
1265 };
1266
1267 typedef std::unique_ptr<struct abbrev_table> abbrev_table_up;
1268
1269 /* Attributes have a name and a value. */
1270 struct attribute
1271 {
1272 ENUM_BITFIELD(dwarf_attribute) name : 16;
1273 ENUM_BITFIELD(dwarf_form) form : 15;
1274
1275 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1276 field should be in u.str (existing only for DW_STRING) but it is kept
1277 here for better struct attribute alignment. */
1278 unsigned int string_is_canonical : 1;
1279
1280 union
1281 {
1282 const char *str;
1283 struct dwarf_block *blk;
1284 ULONGEST unsnd;
1285 LONGEST snd;
1286 CORE_ADDR addr;
1287 ULONGEST signature;
1288 }
1289 u;
1290 };
1291
1292 /* This data structure holds a complete die structure. */
1293 struct die_info
1294 {
1295 /* DWARF-2 tag for this DIE. */
1296 ENUM_BITFIELD(dwarf_tag) tag : 16;
1297
1298 /* Number of attributes */
1299 unsigned char num_attrs;
1300
1301 /* True if we're presently building the full type name for the
1302 type derived from this DIE. */
1303 unsigned char building_fullname : 1;
1304
1305 /* True if this die is in process. PR 16581. */
1306 unsigned char in_process : 1;
1307
1308 /* Abbrev number */
1309 unsigned int abbrev;
1310
1311 /* Offset in .debug_info or .debug_types section. */
1312 sect_offset sect_off;
1313
1314 /* The dies in a compilation unit form an n-ary tree. PARENT
1315 points to this die's parent; CHILD points to the first child of
1316 this node; and all the children of a given node are chained
1317 together via their SIBLING fields. */
1318 struct die_info *child; /* Its first child, if any. */
1319 struct die_info *sibling; /* Its next sibling, if any. */
1320 struct die_info *parent; /* Its parent, if any. */
1321
1322 /* An array of attributes, with NUM_ATTRS elements. There may be
1323 zero, but it's not common and zero-sized arrays are not
1324 sufficiently portable C. */
1325 struct attribute attrs[1];
1326 };
1327
1328 /* Get at parts of an attribute structure. */
1329
1330 #define DW_STRING(attr) ((attr)->u.str)
1331 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
1332 #define DW_UNSND(attr) ((attr)->u.unsnd)
1333 #define DW_BLOCK(attr) ((attr)->u.blk)
1334 #define DW_SND(attr) ((attr)->u.snd)
1335 #define DW_ADDR(attr) ((attr)->u.addr)
1336 #define DW_SIGNATURE(attr) ((attr)->u.signature)
1337
1338 /* Blocks are a bunch of untyped bytes. */
1339 struct dwarf_block
1340 {
1341 size_t size;
1342
1343 /* Valid only if SIZE is not zero. */
1344 const gdb_byte *data;
1345 };
1346
1347 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1348 but this would require a corresponding change in unpack_field_as_long
1349 and friends. */
1350 static int bits_per_byte = 8;
1351
1352 /* When reading a variant or variant part, we track a bit more
1353 information about the field, and store it in an object of this
1354 type. */
1355
1356 struct variant_field
1357 {
1358 /* If we see a DW_TAG_variant, then this will be the discriminant
1359 value. */
1360 ULONGEST discriminant_value;
1361 /* If we see a DW_TAG_variant, then this will be set if this is the
1362 default branch. */
1363 bool default_branch;
1364 /* While reading a DW_TAG_variant_part, this will be set if this
1365 field is the discriminant. */
1366 bool is_discriminant;
1367 };
1368
1369 struct nextfield
1370 {
1371 int accessibility = 0;
1372 int virtuality = 0;
1373 /* Extra information to describe a variant or variant part. */
1374 struct variant_field variant {};
1375 struct field field {};
1376 };
1377
1378 struct fnfieldlist
1379 {
1380 const char *name = nullptr;
1381 std::vector<struct fn_field> fnfields;
1382 };
1383
1384 /* The routines that read and process dies for a C struct or C++ class
1385 pass lists of data member fields and lists of member function fields
1386 in an instance of a field_info structure, as defined below. */
1387 struct field_info
1388 {
1389 /* List of data member and baseclasses fields. */
1390 std::vector<struct nextfield> fields;
1391 std::vector<struct nextfield> baseclasses;
1392
1393 /* Number of fields (including baseclasses). */
1394 int nfields = 0;
1395
1396 /* Set if the accessibility of one of the fields is not public. */
1397 int non_public_fields = 0;
1398
1399 /* Member function fieldlist array, contains name of possibly overloaded
1400 member function, number of overloaded member functions and a pointer
1401 to the head of the member function field chain. */
1402 std::vector<struct fnfieldlist> fnfieldlists;
1403
1404 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1405 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1406 std::vector<struct decl_field> typedef_field_list;
1407
1408 /* Nested types defined by this class and the number of elements in this
1409 list. */
1410 std::vector<struct decl_field> nested_types_list;
1411 };
1412
1413 /* One item on the queue of compilation units to read in full symbols
1414 for. */
1415 struct dwarf2_queue_item
1416 {
1417 struct dwarf2_per_cu_data *per_cu;
1418 enum language pretend_language;
1419 struct dwarf2_queue_item *next;
1420 };
1421
1422 /* The current queue. */
1423 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1424
1425 /* Loaded secondary compilation units are kept in memory until they
1426 have not been referenced for the processing of this many
1427 compilation units. Set this to zero to disable caching. Cache
1428 sizes of up to at least twenty will improve startup time for
1429 typical inter-CU-reference binaries, at an obvious memory cost. */
1430 static int dwarf_max_cache_age = 5;
1431 static void
1432 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1433 struct cmd_list_element *c, const char *value)
1434 {
1435 fprintf_filtered (file, _("The upper bound on the age of cached "
1436 "DWARF compilation units is %s.\n"),
1437 value);
1438 }
1439 \f
1440 /* local function prototypes */
1441
1442 static const char *get_section_name (const struct dwarf2_section_info *);
1443
1444 static const char *get_section_file_name (const struct dwarf2_section_info *);
1445
1446 static void dwarf2_find_base_address (struct die_info *die,
1447 struct dwarf2_cu *cu);
1448
1449 static struct partial_symtab *create_partial_symtab
1450 (struct dwarf2_per_cu_data *per_cu, const char *name);
1451
1452 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1453 const gdb_byte *info_ptr,
1454 struct die_info *type_unit_die,
1455 int has_children, void *data);
1456
1457 static void dwarf2_build_psymtabs_hard
1458 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1459
1460 static void scan_partial_symbols (struct partial_die_info *,
1461 CORE_ADDR *, CORE_ADDR *,
1462 int, struct dwarf2_cu *);
1463
1464 static void add_partial_symbol (struct partial_die_info *,
1465 struct dwarf2_cu *);
1466
1467 static void add_partial_namespace (struct partial_die_info *pdi,
1468 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1469 int set_addrmap, struct dwarf2_cu *cu);
1470
1471 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1472 CORE_ADDR *highpc, int set_addrmap,
1473 struct dwarf2_cu *cu);
1474
1475 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1476 struct dwarf2_cu *cu);
1477
1478 static void add_partial_subprogram (struct partial_die_info *pdi,
1479 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1480 int need_pc, struct dwarf2_cu *cu);
1481
1482 static void dwarf2_read_symtab (struct partial_symtab *,
1483 struct objfile *);
1484
1485 static void psymtab_to_symtab_1 (struct partial_symtab *);
1486
1487 static abbrev_table_up abbrev_table_read_table
1488 (struct dwarf2_per_objfile *dwarf2_per_objfile, struct dwarf2_section_info *,
1489 sect_offset);
1490
1491 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1492
1493 static struct partial_die_info *load_partial_dies
1494 (const struct die_reader_specs *, const gdb_byte *, int);
1495
1496 /* A pair of partial_die_info and compilation unit. */
1497 struct cu_partial_die_info
1498 {
1499 /* The compilation unit of the partial_die_info. */
1500 struct dwarf2_cu *cu;
1501 /* A partial_die_info. */
1502 struct partial_die_info *pdi;
1503
1504 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1505 : cu (cu),
1506 pdi (pdi)
1507 { /* Nothing. */ }
1508
1509 private:
1510 cu_partial_die_info () = delete;
1511 };
1512
1513 static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1514 struct dwarf2_cu *);
1515
1516 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1517 struct attribute *, struct attr_abbrev *,
1518 const gdb_byte *, bool *need_reprocess);
1519
1520 static void read_attribute_reprocess (const struct die_reader_specs *reader,
1521 struct attribute *attr);
1522
1523 static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
1524
1525 static unsigned int read_1_byte (bfd *, const gdb_byte *);
1526
1527 static int read_1_signed_byte (bfd *, const gdb_byte *);
1528
1529 static unsigned int read_2_bytes (bfd *, const gdb_byte *);
1530
1531 /* Read the next three bytes (little-endian order) as an unsigned integer. */
1532 static unsigned int read_3_bytes (bfd *, const gdb_byte *);
1533
1534 static unsigned int read_4_bytes (bfd *, const gdb_byte *);
1535
1536 static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
1537
1538 static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
1539 unsigned int *);
1540
1541 static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
1542
1543 static LONGEST read_checked_initial_length_and_offset
1544 (bfd *, const gdb_byte *, const struct comp_unit_head *,
1545 unsigned int *, unsigned int *);
1546
1547 static LONGEST read_offset (bfd *, const gdb_byte *,
1548 const struct comp_unit_head *,
1549 unsigned int *);
1550
1551 static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
1552
1553 static sect_offset read_abbrev_offset
1554 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1555 struct dwarf2_section_info *, sect_offset);
1556
1557 static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
1558
1559 static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
1560
1561 static const char *read_indirect_string
1562 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1563 const struct comp_unit_head *, unsigned int *);
1564
1565 static const char *read_indirect_line_string
1566 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1567 const struct comp_unit_head *, unsigned int *);
1568
1569 static const char *read_indirect_string_at_offset
1570 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
1571 LONGEST str_offset);
1572
1573 static const char *read_indirect_string_from_dwz
1574 (struct objfile *objfile, struct dwz_file *, LONGEST);
1575
1576 static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
1577
1578 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1579 const gdb_byte *,
1580 unsigned int *);
1581
1582 static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1583 ULONGEST str_index);
1584
1585 static const char *read_stub_str_index (struct dwarf2_cu *cu,
1586 ULONGEST str_index);
1587
1588 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1589
1590 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1591 struct dwarf2_cu *);
1592
1593 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1594 unsigned int);
1595
1596 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1597 struct dwarf2_cu *cu);
1598
1599 static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1600
1601 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1602 struct dwarf2_cu *cu);
1603
1604 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1605
1606 static struct die_info *die_specification (struct die_info *die,
1607 struct dwarf2_cu **);
1608
1609 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1610 struct dwarf2_cu *cu);
1611
1612 static void dwarf_decode_lines (struct line_header *, const char *,
1613 struct dwarf2_cu *, struct partial_symtab *,
1614 CORE_ADDR, int decode_mapping);
1615
1616 static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1617 const char *);
1618
1619 static struct symbol *new_symbol (struct die_info *, struct type *,
1620 struct dwarf2_cu *, struct symbol * = NULL);
1621
1622 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1623 struct dwarf2_cu *);
1624
1625 static void dwarf2_const_value_attr (const struct attribute *attr,
1626 struct type *type,
1627 const char *name,
1628 struct obstack *obstack,
1629 struct dwarf2_cu *cu, LONGEST *value,
1630 const gdb_byte **bytes,
1631 struct dwarf2_locexpr_baton **baton);
1632
1633 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1634
1635 static int need_gnat_info (struct dwarf2_cu *);
1636
1637 static struct type *die_descriptive_type (struct die_info *,
1638 struct dwarf2_cu *);
1639
1640 static void set_descriptive_type (struct type *, struct die_info *,
1641 struct dwarf2_cu *);
1642
1643 static struct type *die_containing_type (struct die_info *,
1644 struct dwarf2_cu *);
1645
1646 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1647 struct dwarf2_cu *);
1648
1649 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1650
1651 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1652
1653 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1654
1655 static char *typename_concat (struct obstack *obs, const char *prefix,
1656 const char *suffix, int physname,
1657 struct dwarf2_cu *cu);
1658
1659 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1660
1661 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1662
1663 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1664
1665 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1666
1667 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1668
1669 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1670
1671 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1672 struct dwarf2_cu *, struct partial_symtab *);
1673
1674 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1675 values. Keep the items ordered with increasing constraints compliance. */
1676 enum pc_bounds_kind
1677 {
1678 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1679 PC_BOUNDS_NOT_PRESENT,
1680
1681 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1682 were present but they do not form a valid range of PC addresses. */
1683 PC_BOUNDS_INVALID,
1684
1685 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1686 PC_BOUNDS_RANGES,
1687
1688 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1689 PC_BOUNDS_HIGH_LOW,
1690 };
1691
1692 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1693 CORE_ADDR *, CORE_ADDR *,
1694 struct dwarf2_cu *,
1695 struct partial_symtab *);
1696
1697 static void get_scope_pc_bounds (struct die_info *,
1698 CORE_ADDR *, CORE_ADDR *,
1699 struct dwarf2_cu *);
1700
1701 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1702 CORE_ADDR, struct dwarf2_cu *);
1703
1704 static void dwarf2_add_field (struct field_info *, struct die_info *,
1705 struct dwarf2_cu *);
1706
1707 static void dwarf2_attach_fields_to_type (struct field_info *,
1708 struct type *, struct dwarf2_cu *);
1709
1710 static void dwarf2_add_member_fn (struct field_info *,
1711 struct die_info *, struct type *,
1712 struct dwarf2_cu *);
1713
1714 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1715 struct type *,
1716 struct dwarf2_cu *);
1717
1718 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1719
1720 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1721
1722 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1723
1724 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1725
1726 static struct using_direct **using_directives (struct dwarf2_cu *cu);
1727
1728 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1729
1730 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1731
1732 static struct type *read_module_type (struct die_info *die,
1733 struct dwarf2_cu *cu);
1734
1735 static const char *namespace_name (struct die_info *die,
1736 int *is_anonymous, struct dwarf2_cu *);
1737
1738 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1739
1740 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1741
1742 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1743 struct dwarf2_cu *);
1744
1745 static struct die_info *read_die_and_siblings_1
1746 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1747 struct die_info *);
1748
1749 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1750 const gdb_byte *info_ptr,
1751 const gdb_byte **new_info_ptr,
1752 struct die_info *parent);
1753
1754 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1755 struct die_info **, const gdb_byte *,
1756 int *, int);
1757
1758 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1759 struct die_info **, const gdb_byte *,
1760 int *);
1761
1762 static void process_die (struct die_info *, struct dwarf2_cu *);
1763
1764 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1765 struct obstack *);
1766
1767 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1768
1769 static const char *dwarf2_full_name (const char *name,
1770 struct die_info *die,
1771 struct dwarf2_cu *cu);
1772
1773 static const char *dwarf2_physname (const char *name, struct die_info *die,
1774 struct dwarf2_cu *cu);
1775
1776 static struct die_info *dwarf2_extension (struct die_info *die,
1777 struct dwarf2_cu **);
1778
1779 static const char *dwarf_tag_name (unsigned int);
1780
1781 static const char *dwarf_attr_name (unsigned int);
1782
1783 static const char *dwarf_unit_type_name (int unit_type);
1784
1785 static const char *dwarf_form_name (unsigned int);
1786
1787 static const char *dwarf_bool_name (unsigned int);
1788
1789 static const char *dwarf_type_encoding_name (unsigned int);
1790
1791 static struct die_info *sibling_die (struct die_info *);
1792
1793 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1794
1795 static void dump_die_for_error (struct die_info *);
1796
1797 static void dump_die_1 (struct ui_file *, int level, int max_level,
1798 struct die_info *);
1799
1800 /*static*/ void dump_die (struct die_info *, int max_level);
1801
1802 static void store_in_ref_table (struct die_info *,
1803 struct dwarf2_cu *);
1804
1805 static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
1806
1807 static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
1808
1809 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1810 const struct attribute *,
1811 struct dwarf2_cu **);
1812
1813 static struct die_info *follow_die_ref (struct die_info *,
1814 const struct attribute *,
1815 struct dwarf2_cu **);
1816
1817 static struct die_info *follow_die_sig (struct die_info *,
1818 const struct attribute *,
1819 struct dwarf2_cu **);
1820
1821 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1822 struct dwarf2_cu *);
1823
1824 static struct type *get_DW_AT_signature_type (struct die_info *,
1825 const struct attribute *,
1826 struct dwarf2_cu *);
1827
1828 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1829
1830 static void read_signatured_type (struct signatured_type *);
1831
1832 static int attr_to_dynamic_prop (const struct attribute *attr,
1833 struct die_info *die, struct dwarf2_cu *cu,
1834 struct dynamic_prop *prop, struct type *type);
1835
1836 /* memory allocation interface */
1837
1838 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1839
1840 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1841
1842 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1843
1844 static int attr_form_is_block (const struct attribute *);
1845
1846 static int attr_form_is_section_offset (const struct attribute *);
1847
1848 static int attr_form_is_constant (const struct attribute *);
1849
1850 static int attr_form_is_ref (const struct attribute *);
1851
1852 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1853 struct dwarf2_loclist_baton *baton,
1854 const struct attribute *attr);
1855
1856 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1857 struct symbol *sym,
1858 struct dwarf2_cu *cu,
1859 int is_block);
1860
1861 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1862 const gdb_byte *info_ptr,
1863 struct abbrev_info *abbrev);
1864
1865 static hashval_t partial_die_hash (const void *item);
1866
1867 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1868
1869 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1870 (sect_offset sect_off, unsigned int offset_in_dwz,
1871 struct dwarf2_per_objfile *dwarf2_per_objfile);
1872
1873 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1874 struct die_info *comp_unit_die,
1875 enum language pretend_language);
1876
1877 static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1878
1879 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1880
1881 static struct type *set_die_type (struct die_info *, struct type *,
1882 struct dwarf2_cu *);
1883
1884 static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1885
1886 static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1887
1888 static void load_full_comp_unit (struct dwarf2_per_cu_data *, bool,
1889 enum language);
1890
1891 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1892 enum language);
1893
1894 static void process_full_type_unit (struct dwarf2_per_cu_data *,
1895 enum language);
1896
1897 static void dwarf2_add_dependence (struct dwarf2_cu *,
1898 struct dwarf2_per_cu_data *);
1899
1900 static void dwarf2_mark (struct dwarf2_cu *);
1901
1902 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1903
1904 static struct type *get_die_type_at_offset (sect_offset,
1905 struct dwarf2_per_cu_data *);
1906
1907 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1908
1909 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1910 enum language pretend_language);
1911
1912 static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
1913
1914 static struct type *dwarf2_per_cu_addr_type (struct dwarf2_per_cu_data *per_cu);
1915 static struct type *dwarf2_per_cu_addr_sized_int_type
1916 (struct dwarf2_per_cu_data *per_cu, bool unsigned_p);
1917 static struct type *dwarf2_per_cu_int_type
1918 (struct dwarf2_per_cu_data *per_cu, int size_in_bytes,
1919 bool unsigned_p);
1920
1921 /* Class, the destructor of which frees all allocated queue entries. This
1922 will only have work to do if an error was thrown while processing the
1923 dwarf. If no error was thrown then the queue entries should have all
1924 been processed, and freed, as we went along. */
1925
1926 class dwarf2_queue_guard
1927 {
1928 public:
1929 dwarf2_queue_guard () = default;
1930
1931 /* Free any entries remaining on the queue. There should only be
1932 entries left if we hit an error while processing the dwarf. */
1933 ~dwarf2_queue_guard ()
1934 {
1935 struct dwarf2_queue_item *item, *last;
1936
1937 item = dwarf2_queue;
1938 while (item)
1939 {
1940 /* Anything still marked queued is likely to be in an
1941 inconsistent state, so discard it. */
1942 if (item->per_cu->queued)
1943 {
1944 if (item->per_cu->cu != NULL)
1945 free_one_cached_comp_unit (item->per_cu);
1946 item->per_cu->queued = 0;
1947 }
1948
1949 last = item;
1950 item = item->next;
1951 xfree (last);
1952 }
1953
1954 dwarf2_queue = dwarf2_queue_tail = NULL;
1955 }
1956 };
1957
1958 /* The return type of find_file_and_directory. Note, the enclosed
1959 string pointers are only valid while this object is valid. */
1960
1961 struct file_and_directory
1962 {
1963 /* The filename. This is never NULL. */
1964 const char *name;
1965
1966 /* The compilation directory. NULL if not known. If we needed to
1967 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1968 points directly to the DW_AT_comp_dir string attribute owned by
1969 the obstack that owns the DIE. */
1970 const char *comp_dir;
1971
1972 /* If we needed to build a new string for comp_dir, this is what
1973 owns the storage. */
1974 std::string comp_dir_storage;
1975 };
1976
1977 static file_and_directory find_file_and_directory (struct die_info *die,
1978 struct dwarf2_cu *cu);
1979
1980 static char *file_full_name (int file, struct line_header *lh,
1981 const char *comp_dir);
1982
1983 /* Expected enum dwarf_unit_type for read_comp_unit_head. */
1984 enum class rcuh_kind { COMPILE, TYPE };
1985
1986 static const gdb_byte *read_and_check_comp_unit_head
1987 (struct dwarf2_per_objfile* dwarf2_per_objfile,
1988 struct comp_unit_head *header,
1989 struct dwarf2_section_info *section,
1990 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
1991 rcuh_kind section_kind);
1992
1993 static void init_cutu_and_read_dies
1994 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
1995 int use_existing_cu, int keep, bool skip_partial,
1996 die_reader_func_ftype *die_reader_func, void *data);
1997
1998 static void init_cutu_and_read_dies_simple
1999 (struct dwarf2_per_cu_data *this_cu,
2000 die_reader_func_ftype *die_reader_func, void *data);
2001
2002 static htab_t allocate_signatured_type_table (struct objfile *objfile);
2003
2004 static htab_t allocate_dwo_unit_table (struct objfile *objfile);
2005
2006 static struct dwo_unit *lookup_dwo_unit_in_dwp
2007 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2008 struct dwp_file *dwp_file, const char *comp_dir,
2009 ULONGEST signature, int is_debug_types);
2010
2011 static struct dwp_file *get_dwp_file
2012 (struct dwarf2_per_objfile *dwarf2_per_objfile);
2013
2014 static struct dwo_unit *lookup_dwo_comp_unit
2015 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
2016
2017 static struct dwo_unit *lookup_dwo_type_unit
2018 (struct signatured_type *, const char *, const char *);
2019
2020 static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
2021
2022 /* A unique pointer to a dwo_file. */
2023
2024 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
2025
2026 static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
2027
2028 static void check_producer (struct dwarf2_cu *cu);
2029
2030 static void free_line_header_voidp (void *arg);
2031 \f
2032 /* Various complaints about symbol reading that don't abort the process. */
2033
2034 static void
2035 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2036 {
2037 complaint (_("statement list doesn't fit in .debug_line section"));
2038 }
2039
2040 static void
2041 dwarf2_debug_line_missing_file_complaint (void)
2042 {
2043 complaint (_(".debug_line section has line data without a file"));
2044 }
2045
2046 static void
2047 dwarf2_debug_line_missing_end_sequence_complaint (void)
2048 {
2049 complaint (_(".debug_line section has line "
2050 "program sequence without an end"));
2051 }
2052
2053 static void
2054 dwarf2_complex_location_expr_complaint (void)
2055 {
2056 complaint (_("location expression too complex"));
2057 }
2058
2059 static void
2060 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
2061 int arg3)
2062 {
2063 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
2064 arg1, arg2, arg3);
2065 }
2066
2067 static void
2068 dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2069 {
2070 complaint (_("debug info runs off end of %s section"
2071 " [in module %s]"),
2072 get_section_name (section),
2073 get_section_file_name (section));
2074 }
2075
2076 static void
2077 dwarf2_macro_malformed_definition_complaint (const char *arg1)
2078 {
2079 complaint (_("macro debug info contains a "
2080 "malformed macro definition:\n`%s'"),
2081 arg1);
2082 }
2083
2084 static void
2085 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
2086 {
2087 complaint (_("invalid attribute class or form for '%s' in '%s'"),
2088 arg1, arg2);
2089 }
2090
2091 /* Hash function for line_header_hash. */
2092
2093 static hashval_t
2094 line_header_hash (const struct line_header *ofs)
2095 {
2096 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
2097 }
2098
2099 /* Hash function for htab_create_alloc_ex for line_header_hash. */
2100
2101 static hashval_t
2102 line_header_hash_voidp (const void *item)
2103 {
2104 const struct line_header *ofs = (const struct line_header *) item;
2105
2106 return line_header_hash (ofs);
2107 }
2108
2109 /* Equality function for line_header_hash. */
2110
2111 static int
2112 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
2113 {
2114 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
2115 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
2116
2117 return (ofs_lhs->sect_off == ofs_rhs->sect_off
2118 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
2119 }
2120
2121 \f
2122
2123 /* Read the given attribute value as an address, taking the attribute's
2124 form into account. */
2125
2126 static CORE_ADDR
2127 attr_value_as_address (struct attribute *attr)
2128 {
2129 CORE_ADDR addr;
2130
2131 if (attr->form != DW_FORM_addr && attr->form != DW_FORM_addrx
2132 && attr->form != DW_FORM_GNU_addr_index)
2133 {
2134 /* Aside from a few clearly defined exceptions, attributes that
2135 contain an address must always be in DW_FORM_addr form.
2136 Unfortunately, some compilers happen to be violating this
2137 requirement by encoding addresses using other forms, such
2138 as DW_FORM_data4 for example. For those broken compilers,
2139 we try to do our best, without any guarantee of success,
2140 to interpret the address correctly. It would also be nice
2141 to generate a complaint, but that would require us to maintain
2142 a list of legitimate cases where a non-address form is allowed,
2143 as well as update callers to pass in at least the CU's DWARF
2144 version. This is more overhead than what we're willing to
2145 expand for a pretty rare case. */
2146 addr = DW_UNSND (attr);
2147 }
2148 else
2149 addr = DW_ADDR (attr);
2150
2151 return addr;
2152 }
2153
2154 /* See declaration. */
2155
2156 dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
2157 const dwarf2_debug_sections *names,
2158 bool can_copy_)
2159 : objfile (objfile_),
2160 can_copy (can_copy_)
2161 {
2162 if (names == NULL)
2163 names = &dwarf2_elf_names;
2164
2165 bfd *obfd = objfile->obfd;
2166
2167 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
2168 locate_sections (obfd, sec, *names);
2169 }
2170
2171 dwarf2_per_objfile::~dwarf2_per_objfile ()
2172 {
2173 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
2174 free_cached_comp_units ();
2175
2176 if (quick_file_names_table)
2177 htab_delete (quick_file_names_table);
2178
2179 if (line_header_hash)
2180 htab_delete (line_header_hash);
2181
2182 for (dwarf2_per_cu_data *per_cu : all_comp_units)
2183 per_cu->imported_symtabs_free ();
2184
2185 for (signatured_type *sig_type : all_type_units)
2186 sig_type->per_cu.imported_symtabs_free ();
2187
2188 /* Everything else should be on the objfile obstack. */
2189 }
2190
2191 /* See declaration. */
2192
2193 void
2194 dwarf2_per_objfile::free_cached_comp_units ()
2195 {
2196 dwarf2_per_cu_data *per_cu = read_in_chain;
2197 dwarf2_per_cu_data **last_chain = &read_in_chain;
2198 while (per_cu != NULL)
2199 {
2200 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
2201
2202 delete per_cu->cu;
2203 *last_chain = next_cu;
2204 per_cu = next_cu;
2205 }
2206 }
2207
2208 /* A helper class that calls free_cached_comp_units on
2209 destruction. */
2210
2211 class free_cached_comp_units
2212 {
2213 public:
2214
2215 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
2216 : m_per_objfile (per_objfile)
2217 {
2218 }
2219
2220 ~free_cached_comp_units ()
2221 {
2222 m_per_objfile->free_cached_comp_units ();
2223 }
2224
2225 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
2226
2227 private:
2228
2229 dwarf2_per_objfile *m_per_objfile;
2230 };
2231
2232 /* Try to locate the sections we need for DWARF 2 debugging
2233 information and return true if we have enough to do something.
2234 NAMES points to the dwarf2 section names, or is NULL if the standard
2235 ELF names are used. CAN_COPY is true for formats where symbol
2236 interposition is possible and so symbol values must follow copy
2237 relocation rules. */
2238
2239 int
2240 dwarf2_has_info (struct objfile *objfile,
2241 const struct dwarf2_debug_sections *names,
2242 bool can_copy)
2243 {
2244 if (objfile->flags & OBJF_READNEVER)
2245 return 0;
2246
2247 struct dwarf2_per_objfile *dwarf2_per_objfile
2248 = get_dwarf2_per_objfile (objfile);
2249
2250 if (dwarf2_per_objfile == NULL)
2251 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile,
2252 names,
2253 can_copy);
2254
2255 return (!dwarf2_per_objfile->info.is_virtual
2256 && dwarf2_per_objfile->info.s.section != NULL
2257 && !dwarf2_per_objfile->abbrev.is_virtual
2258 && dwarf2_per_objfile->abbrev.s.section != NULL);
2259 }
2260
2261 /* Return the containing section of virtual section SECTION. */
2262
2263 static struct dwarf2_section_info *
2264 get_containing_section (const struct dwarf2_section_info *section)
2265 {
2266 gdb_assert (section->is_virtual);
2267 return section->s.containing_section;
2268 }
2269
2270 /* Return the bfd owner of SECTION. */
2271
2272 static struct bfd *
2273 get_section_bfd_owner (const struct dwarf2_section_info *section)
2274 {
2275 if (section->is_virtual)
2276 {
2277 section = get_containing_section (section);
2278 gdb_assert (!section->is_virtual);
2279 }
2280 return section->s.section->owner;
2281 }
2282
2283 /* Return the bfd section of SECTION.
2284 Returns NULL if the section is not present. */
2285
2286 static asection *
2287 get_section_bfd_section (const struct dwarf2_section_info *section)
2288 {
2289 if (section->is_virtual)
2290 {
2291 section = get_containing_section (section);
2292 gdb_assert (!section->is_virtual);
2293 }
2294 return section->s.section;
2295 }
2296
2297 /* Return the name of SECTION. */
2298
2299 static const char *
2300 get_section_name (const struct dwarf2_section_info *section)
2301 {
2302 asection *sectp = get_section_bfd_section (section);
2303
2304 gdb_assert (sectp != NULL);
2305 return bfd_section_name (sectp);
2306 }
2307
2308 /* Return the name of the file SECTION is in. */
2309
2310 static const char *
2311 get_section_file_name (const struct dwarf2_section_info *section)
2312 {
2313 bfd *abfd = get_section_bfd_owner (section);
2314
2315 return bfd_get_filename (abfd);
2316 }
2317
2318 /* Return the id of SECTION.
2319 Returns 0 if SECTION doesn't exist. */
2320
2321 static int
2322 get_section_id (const struct dwarf2_section_info *section)
2323 {
2324 asection *sectp = get_section_bfd_section (section);
2325
2326 if (sectp == NULL)
2327 return 0;
2328 return sectp->id;
2329 }
2330
2331 /* Return the flags of SECTION.
2332 SECTION (or containing section if this is a virtual section) must exist. */
2333
2334 static int
2335 get_section_flags (const struct dwarf2_section_info *section)
2336 {
2337 asection *sectp = get_section_bfd_section (section);
2338
2339 gdb_assert (sectp != NULL);
2340 return bfd_section_flags (sectp);
2341 }
2342
2343 /* When loading sections, we look either for uncompressed section or for
2344 compressed section names. */
2345
2346 static int
2347 section_is_p (const char *section_name,
2348 const struct dwarf2_section_names *names)
2349 {
2350 if (names->normal != NULL
2351 && strcmp (section_name, names->normal) == 0)
2352 return 1;
2353 if (names->compressed != NULL
2354 && strcmp (section_name, names->compressed) == 0)
2355 return 1;
2356 return 0;
2357 }
2358
2359 /* See declaration. */
2360
2361 void
2362 dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
2363 const dwarf2_debug_sections &names)
2364 {
2365 flagword aflag = bfd_section_flags (sectp);
2366
2367 if ((aflag & SEC_HAS_CONTENTS) == 0)
2368 {
2369 }
2370 else if (elf_section_data (sectp)->this_hdr.sh_size
2371 > bfd_get_file_size (abfd))
2372 {
2373 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
2374 warning (_("Discarding section %s which has a section size (%s"
2375 ") larger than the file size [in module %s]"),
2376 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
2377 bfd_get_filename (abfd));
2378 }
2379 else if (section_is_p (sectp->name, &names.info))
2380 {
2381 this->info.s.section = sectp;
2382 this->info.size = bfd_section_size (sectp);
2383 }
2384 else if (section_is_p (sectp->name, &names.abbrev))
2385 {
2386 this->abbrev.s.section = sectp;
2387 this->abbrev.size = bfd_section_size (sectp);
2388 }
2389 else if (section_is_p (sectp->name, &names.line))
2390 {
2391 this->line.s.section = sectp;
2392 this->line.size = bfd_section_size (sectp);
2393 }
2394 else if (section_is_p (sectp->name, &names.loc))
2395 {
2396 this->loc.s.section = sectp;
2397 this->loc.size = bfd_section_size (sectp);
2398 }
2399 else if (section_is_p (sectp->name, &names.loclists))
2400 {
2401 this->loclists.s.section = sectp;
2402 this->loclists.size = bfd_section_size (sectp);
2403 }
2404 else if (section_is_p (sectp->name, &names.macinfo))
2405 {
2406 this->macinfo.s.section = sectp;
2407 this->macinfo.size = bfd_section_size (sectp);
2408 }
2409 else if (section_is_p (sectp->name, &names.macro))
2410 {
2411 this->macro.s.section = sectp;
2412 this->macro.size = bfd_section_size (sectp);
2413 }
2414 else if (section_is_p (sectp->name, &names.str))
2415 {
2416 this->str.s.section = sectp;
2417 this->str.size = bfd_section_size (sectp);
2418 }
2419 else if (section_is_p (sectp->name, &names.str_offsets))
2420 {
2421 this->str_offsets.s.section = sectp;
2422 this->str_offsets.size = bfd_section_size (sectp);
2423 }
2424 else if (section_is_p (sectp->name, &names.line_str))
2425 {
2426 this->line_str.s.section = sectp;
2427 this->line_str.size = bfd_section_size (sectp);
2428 }
2429 else if (section_is_p (sectp->name, &names.addr))
2430 {
2431 this->addr.s.section = sectp;
2432 this->addr.size = bfd_section_size (sectp);
2433 }
2434 else if (section_is_p (sectp->name, &names.frame))
2435 {
2436 this->frame.s.section = sectp;
2437 this->frame.size = bfd_section_size (sectp);
2438 }
2439 else if (section_is_p (sectp->name, &names.eh_frame))
2440 {
2441 this->eh_frame.s.section = sectp;
2442 this->eh_frame.size = bfd_section_size (sectp);
2443 }
2444 else if (section_is_p (sectp->name, &names.ranges))
2445 {
2446 this->ranges.s.section = sectp;
2447 this->ranges.size = bfd_section_size (sectp);
2448 }
2449 else if (section_is_p (sectp->name, &names.rnglists))
2450 {
2451 this->rnglists.s.section = sectp;
2452 this->rnglists.size = bfd_section_size (sectp);
2453 }
2454 else if (section_is_p (sectp->name, &names.types))
2455 {
2456 struct dwarf2_section_info type_section;
2457
2458 memset (&type_section, 0, sizeof (type_section));
2459 type_section.s.section = sectp;
2460 type_section.size = bfd_section_size (sectp);
2461
2462 this->types.push_back (type_section);
2463 }
2464 else if (section_is_p (sectp->name, &names.gdb_index))
2465 {
2466 this->gdb_index.s.section = sectp;
2467 this->gdb_index.size = bfd_section_size (sectp);
2468 }
2469 else if (section_is_p (sectp->name, &names.debug_names))
2470 {
2471 this->debug_names.s.section = sectp;
2472 this->debug_names.size = bfd_section_size (sectp);
2473 }
2474 else if (section_is_p (sectp->name, &names.debug_aranges))
2475 {
2476 this->debug_aranges.s.section = sectp;
2477 this->debug_aranges.size = bfd_section_size (sectp);
2478 }
2479
2480 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2481 && bfd_section_vma (sectp) == 0)
2482 this->has_section_at_zero = true;
2483 }
2484
2485 /* A helper function that decides whether a section is empty,
2486 or not present. */
2487
2488 static int
2489 dwarf2_section_empty_p (const struct dwarf2_section_info *section)
2490 {
2491 if (section->is_virtual)
2492 return section->size == 0;
2493 return section->s.section == NULL || section->size == 0;
2494 }
2495
2496 /* See dwarf2read.h. */
2497
2498 void
2499 dwarf2_read_section (struct objfile *objfile, dwarf2_section_info *info)
2500 {
2501 asection *sectp;
2502 bfd *abfd;
2503 gdb_byte *buf, *retbuf;
2504
2505 if (info->readin)
2506 return;
2507 info->buffer = NULL;
2508 info->readin = true;
2509
2510 if (dwarf2_section_empty_p (info))
2511 return;
2512
2513 sectp = get_section_bfd_section (info);
2514
2515 /* If this is a virtual section we need to read in the real one first. */
2516 if (info->is_virtual)
2517 {
2518 struct dwarf2_section_info *containing_section =
2519 get_containing_section (info);
2520
2521 gdb_assert (sectp != NULL);
2522 if ((sectp->flags & SEC_RELOC) != 0)
2523 {
2524 error (_("Dwarf Error: DWP format V2 with relocations is not"
2525 " supported in section %s [in module %s]"),
2526 get_section_name (info), get_section_file_name (info));
2527 }
2528 dwarf2_read_section (objfile, containing_section);
2529 /* Other code should have already caught virtual sections that don't
2530 fit. */
2531 gdb_assert (info->virtual_offset + info->size
2532 <= containing_section->size);
2533 /* If the real section is empty or there was a problem reading the
2534 section we shouldn't get here. */
2535 gdb_assert (containing_section->buffer != NULL);
2536 info->buffer = containing_section->buffer + info->virtual_offset;
2537 return;
2538 }
2539
2540 /* If the section has relocations, we must read it ourselves.
2541 Otherwise we attach it to the BFD. */
2542 if ((sectp->flags & SEC_RELOC) == 0)
2543 {
2544 info->buffer = gdb_bfd_map_section (sectp, &info->size);
2545 return;
2546 }
2547
2548 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, info->size);
2549 info->buffer = buf;
2550
2551 /* When debugging .o files, we may need to apply relocations; see
2552 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2553 We never compress sections in .o files, so we only need to
2554 try this when the section is not compressed. */
2555 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
2556 if (retbuf != NULL)
2557 {
2558 info->buffer = retbuf;
2559 return;
2560 }
2561
2562 abfd = get_section_bfd_owner (info);
2563 gdb_assert (abfd != NULL);
2564
2565 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2566 || bfd_bread (buf, info->size, abfd) != info->size)
2567 {
2568 error (_("Dwarf Error: Can't read DWARF data"
2569 " in section %s [in module %s]"),
2570 bfd_section_name (sectp), bfd_get_filename (abfd));
2571 }
2572 }
2573
2574 /* A helper function that returns the size of a section in a safe way.
2575 If you are positive that the section has been read before using the
2576 size, then it is safe to refer to the dwarf2_section_info object's
2577 "size" field directly. In other cases, you must call this
2578 function, because for compressed sections the size field is not set
2579 correctly until the section has been read. */
2580
2581 static bfd_size_type
2582 dwarf2_section_size (struct objfile *objfile,
2583 struct dwarf2_section_info *info)
2584 {
2585 if (!info->readin)
2586 dwarf2_read_section (objfile, info);
2587 return info->size;
2588 }
2589
2590 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2591 SECTION_NAME. */
2592
2593 void
2594 dwarf2_get_section_info (struct objfile *objfile,
2595 enum dwarf2_section_enum sect,
2596 asection **sectp, const gdb_byte **bufp,
2597 bfd_size_type *sizep)
2598 {
2599 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
2600 struct dwarf2_section_info *info;
2601
2602 /* We may see an objfile without any DWARF, in which case we just
2603 return nothing. */
2604 if (data == NULL)
2605 {
2606 *sectp = NULL;
2607 *bufp = NULL;
2608 *sizep = 0;
2609 return;
2610 }
2611 switch (sect)
2612 {
2613 case DWARF2_DEBUG_FRAME:
2614 info = &data->frame;
2615 break;
2616 case DWARF2_EH_FRAME:
2617 info = &data->eh_frame;
2618 break;
2619 default:
2620 gdb_assert_not_reached ("unexpected section");
2621 }
2622
2623 dwarf2_read_section (objfile, info);
2624
2625 *sectp = get_section_bfd_section (info);
2626 *bufp = info->buffer;
2627 *sizep = info->size;
2628 }
2629
2630 /* A helper function to find the sections for a .dwz file. */
2631
2632 static void
2633 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2634 {
2635 struct dwz_file *dwz_file = (struct dwz_file *) arg;
2636
2637 /* Note that we only support the standard ELF names, because .dwz
2638 is ELF-only (at the time of writing). */
2639 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2640 {
2641 dwz_file->abbrev.s.section = sectp;
2642 dwz_file->abbrev.size = bfd_section_size (sectp);
2643 }
2644 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2645 {
2646 dwz_file->info.s.section = sectp;
2647 dwz_file->info.size = bfd_section_size (sectp);
2648 }
2649 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2650 {
2651 dwz_file->str.s.section = sectp;
2652 dwz_file->str.size = bfd_section_size (sectp);
2653 }
2654 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2655 {
2656 dwz_file->line.s.section = sectp;
2657 dwz_file->line.size = bfd_section_size (sectp);
2658 }
2659 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2660 {
2661 dwz_file->macro.s.section = sectp;
2662 dwz_file->macro.size = bfd_section_size (sectp);
2663 }
2664 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2665 {
2666 dwz_file->gdb_index.s.section = sectp;
2667 dwz_file->gdb_index.size = bfd_section_size (sectp);
2668 }
2669 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2670 {
2671 dwz_file->debug_names.s.section = sectp;
2672 dwz_file->debug_names.size = bfd_section_size (sectp);
2673 }
2674 }
2675
2676 /* See dwarf2read.h. */
2677
2678 struct dwz_file *
2679 dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
2680 {
2681 const char *filename;
2682 bfd_size_type buildid_len_arg;
2683 size_t buildid_len;
2684 bfd_byte *buildid;
2685
2686 if (dwarf2_per_objfile->dwz_file != NULL)
2687 return dwarf2_per_objfile->dwz_file.get ();
2688
2689 bfd_set_error (bfd_error_no_error);
2690 gdb::unique_xmalloc_ptr<char> data
2691 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2692 &buildid_len_arg, &buildid));
2693 if (data == NULL)
2694 {
2695 if (bfd_get_error () == bfd_error_no_error)
2696 return NULL;
2697 error (_("could not read '.gnu_debugaltlink' section: %s"),
2698 bfd_errmsg (bfd_get_error ()));
2699 }
2700
2701 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
2702
2703 buildid_len = (size_t) buildid_len_arg;
2704
2705 filename = data.get ();
2706
2707 std::string abs_storage;
2708 if (!IS_ABSOLUTE_PATH (filename))
2709 {
2710 gdb::unique_xmalloc_ptr<char> abs
2711 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
2712
2713 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
2714 filename = abs_storage.c_str ();
2715 }
2716
2717 /* First try the file name given in the section. If that doesn't
2718 work, try to use the build-id instead. */
2719 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
2720 if (dwz_bfd != NULL)
2721 {
2722 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2723 dwz_bfd.reset (nullptr);
2724 }
2725
2726 if (dwz_bfd == NULL)
2727 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2728
2729 if (dwz_bfd == NULL)
2730 error (_("could not find '.gnu_debugaltlink' file for %s"),
2731 objfile_name (dwarf2_per_objfile->objfile));
2732
2733 std::unique_ptr<struct dwz_file> result
2734 (new struct dwz_file (std::move (dwz_bfd)));
2735
2736 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2737 result.get ());
2738
2739 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd,
2740 result->dwz_bfd.get ());
2741 dwarf2_per_objfile->dwz_file = std::move (result);
2742 return dwarf2_per_objfile->dwz_file.get ();
2743 }
2744 \f
2745 /* DWARF quick_symbols_functions support. */
2746
2747 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2748 unique line tables, so we maintain a separate table of all .debug_line
2749 derived entries to support the sharing.
2750 All the quick functions need is the list of file names. We discard the
2751 line_header when we're done and don't need to record it here. */
2752 struct quick_file_names
2753 {
2754 /* The data used to construct the hash key. */
2755 struct stmt_list_hash hash;
2756
2757 /* The number of entries in file_names, real_names. */
2758 unsigned int num_file_names;
2759
2760 /* The file names from the line table, after being run through
2761 file_full_name. */
2762 const char **file_names;
2763
2764 /* The file names from the line table after being run through
2765 gdb_realpath. These are computed lazily. */
2766 const char **real_names;
2767 };
2768
2769 /* When using the index (and thus not using psymtabs), each CU has an
2770 object of this type. This is used to hold information needed by
2771 the various "quick" methods. */
2772 struct dwarf2_per_cu_quick_data
2773 {
2774 /* The file table. This can be NULL if there was no file table
2775 or it's currently not read in.
2776 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2777 struct quick_file_names *file_names;
2778
2779 /* The corresponding symbol table. This is NULL if symbols for this
2780 CU have not yet been read. */
2781 struct compunit_symtab *compunit_symtab;
2782
2783 /* A temporary mark bit used when iterating over all CUs in
2784 expand_symtabs_matching. */
2785 unsigned int mark : 1;
2786
2787 /* True if we've tried to read the file table and found there isn't one.
2788 There will be no point in trying to read it again next time. */
2789 unsigned int no_file_data : 1;
2790 };
2791
2792 /* Utility hash function for a stmt_list_hash. */
2793
2794 static hashval_t
2795 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2796 {
2797 hashval_t v = 0;
2798
2799 if (stmt_list_hash->dwo_unit != NULL)
2800 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2801 v += to_underlying (stmt_list_hash->line_sect_off);
2802 return v;
2803 }
2804
2805 /* Utility equality function for a stmt_list_hash. */
2806
2807 static int
2808 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2809 const struct stmt_list_hash *rhs)
2810 {
2811 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2812 return 0;
2813 if (lhs->dwo_unit != NULL
2814 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2815 return 0;
2816
2817 return lhs->line_sect_off == rhs->line_sect_off;
2818 }
2819
2820 /* Hash function for a quick_file_names. */
2821
2822 static hashval_t
2823 hash_file_name_entry (const void *e)
2824 {
2825 const struct quick_file_names *file_data
2826 = (const struct quick_file_names *) e;
2827
2828 return hash_stmt_list_entry (&file_data->hash);
2829 }
2830
2831 /* Equality function for a quick_file_names. */
2832
2833 static int
2834 eq_file_name_entry (const void *a, const void *b)
2835 {
2836 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2837 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2838
2839 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2840 }
2841
2842 /* Delete function for a quick_file_names. */
2843
2844 static void
2845 delete_file_name_entry (void *e)
2846 {
2847 struct quick_file_names *file_data = (struct quick_file_names *) e;
2848 int i;
2849
2850 for (i = 0; i < file_data->num_file_names; ++i)
2851 {
2852 xfree ((void*) file_data->file_names[i]);
2853 if (file_data->real_names)
2854 xfree ((void*) file_data->real_names[i]);
2855 }
2856
2857 /* The space for the struct itself lives on objfile_obstack,
2858 so we don't free it here. */
2859 }
2860
2861 /* Create a quick_file_names hash table. */
2862
2863 static htab_t
2864 create_quick_file_names_table (unsigned int nr_initial_entries)
2865 {
2866 return htab_create_alloc (nr_initial_entries,
2867 hash_file_name_entry, eq_file_name_entry,
2868 delete_file_name_entry, xcalloc, xfree);
2869 }
2870
2871 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2872 have to be created afterwards. You should call age_cached_comp_units after
2873 processing PER_CU->CU. dw2_setup must have been already called. */
2874
2875 static void
2876 load_cu (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2877 {
2878 if (per_cu->is_debug_types)
2879 load_full_type_unit (per_cu);
2880 else
2881 load_full_comp_unit (per_cu, skip_partial, language_minimal);
2882
2883 if (per_cu->cu == NULL)
2884 return; /* Dummy CU. */
2885
2886 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2887 }
2888
2889 /* Read in the symbols for PER_CU. */
2890
2891 static void
2892 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2893 {
2894 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2895
2896 /* Skip type_unit_groups, reading the type units they contain
2897 is handled elsewhere. */
2898 if (IS_TYPE_UNIT_GROUP (per_cu))
2899 return;
2900
2901 /* The destructor of dwarf2_queue_guard frees any entries left on
2902 the queue. After this point we're guaranteed to leave this function
2903 with the dwarf queue empty. */
2904 dwarf2_queue_guard q_guard;
2905
2906 if (dwarf2_per_objfile->using_index
2907 ? per_cu->v.quick->compunit_symtab == NULL
2908 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2909 {
2910 queue_comp_unit (per_cu, language_minimal);
2911 load_cu (per_cu, skip_partial);
2912
2913 /* If we just loaded a CU from a DWO, and we're working with an index
2914 that may badly handle TUs, load all the TUs in that DWO as well.
2915 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2916 if (!per_cu->is_debug_types
2917 && per_cu->cu != NULL
2918 && per_cu->cu->dwo_unit != NULL
2919 && dwarf2_per_objfile->index_table != NULL
2920 && dwarf2_per_objfile->index_table->version <= 7
2921 /* DWP files aren't supported yet. */
2922 && get_dwp_file (dwarf2_per_objfile) == NULL)
2923 queue_and_load_all_dwo_tus (per_cu);
2924 }
2925
2926 process_queue (dwarf2_per_objfile);
2927
2928 /* Age the cache, releasing compilation units that have not
2929 been used recently. */
2930 age_cached_comp_units (dwarf2_per_objfile);
2931 }
2932
2933 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2934 the objfile from which this CU came. Returns the resulting symbol
2935 table. */
2936
2937 static struct compunit_symtab *
2938 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2939 {
2940 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2941
2942 gdb_assert (dwarf2_per_objfile->using_index);
2943 if (!per_cu->v.quick->compunit_symtab)
2944 {
2945 free_cached_comp_units freer (dwarf2_per_objfile);
2946 scoped_restore decrementer = increment_reading_symtab ();
2947 dw2_do_instantiate_symtab (per_cu, skip_partial);
2948 process_cu_includes (dwarf2_per_objfile);
2949 }
2950
2951 return per_cu->v.quick->compunit_symtab;
2952 }
2953
2954 /* See declaration. */
2955
2956 dwarf2_per_cu_data *
2957 dwarf2_per_objfile::get_cutu (int index)
2958 {
2959 if (index >= this->all_comp_units.size ())
2960 {
2961 index -= this->all_comp_units.size ();
2962 gdb_assert (index < this->all_type_units.size ());
2963 return &this->all_type_units[index]->per_cu;
2964 }
2965
2966 return this->all_comp_units[index];
2967 }
2968
2969 /* See declaration. */
2970
2971 dwarf2_per_cu_data *
2972 dwarf2_per_objfile::get_cu (int index)
2973 {
2974 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
2975
2976 return this->all_comp_units[index];
2977 }
2978
2979 /* See declaration. */
2980
2981 signatured_type *
2982 dwarf2_per_objfile::get_tu (int index)
2983 {
2984 gdb_assert (index >= 0 && index < this->all_type_units.size ());
2985
2986 return this->all_type_units[index];
2987 }
2988
2989 /* Return a new dwarf2_per_cu_data allocated on OBJFILE's
2990 objfile_obstack, and constructed with the specified field
2991 values. */
2992
2993 static dwarf2_per_cu_data *
2994 create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2995 struct dwarf2_section_info *section,
2996 int is_dwz,
2997 sect_offset sect_off, ULONGEST length)
2998 {
2999 struct objfile *objfile = dwarf2_per_objfile->objfile;
3000 dwarf2_per_cu_data *the_cu
3001 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3002 struct dwarf2_per_cu_data);
3003 the_cu->sect_off = sect_off;
3004 the_cu->length = length;
3005 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
3006 the_cu->section = section;
3007 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3008 struct dwarf2_per_cu_quick_data);
3009 the_cu->is_dwz = is_dwz;
3010 return the_cu;
3011 }
3012
3013 /* A helper for create_cus_from_index that handles a given list of
3014 CUs. */
3015
3016 static void
3017 create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
3018 const gdb_byte *cu_list, offset_type n_elements,
3019 struct dwarf2_section_info *section,
3020 int is_dwz)
3021 {
3022 for (offset_type i = 0; i < n_elements; i += 2)
3023 {
3024 gdb_static_assert (sizeof (ULONGEST) >= 8);
3025
3026 sect_offset sect_off
3027 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
3028 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
3029 cu_list += 2 * 8;
3030
3031 dwarf2_per_cu_data *per_cu
3032 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
3033 sect_off, length);
3034 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
3035 }
3036 }
3037
3038 /* Read the CU list from the mapped index, and use it to create all
3039 the CU objects for this objfile. */
3040
3041 static void
3042 create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
3043 const gdb_byte *cu_list, offset_type cu_list_elements,
3044 const gdb_byte *dwz_list, offset_type dwz_elements)
3045 {
3046 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
3047 dwarf2_per_objfile->all_comp_units.reserve
3048 ((cu_list_elements + dwz_elements) / 2);
3049
3050 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
3051 &dwarf2_per_objfile->info, 0);
3052
3053 if (dwz_elements == 0)
3054 return;
3055
3056 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3057 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
3058 &dwz->info, 1);
3059 }
3060
3061 /* Create the signatured type hash table from the index. */
3062
3063 static void
3064 create_signatured_type_table_from_index
3065 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3066 struct dwarf2_section_info *section,
3067 const gdb_byte *bytes,
3068 offset_type elements)
3069 {
3070 struct objfile *objfile = dwarf2_per_objfile->objfile;
3071
3072 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
3073 dwarf2_per_objfile->all_type_units.reserve (elements / 3);
3074
3075 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
3076
3077 for (offset_type i = 0; i < elements; i += 3)
3078 {
3079 struct signatured_type *sig_type;
3080 ULONGEST signature;
3081 void **slot;
3082 cu_offset type_offset_in_tu;
3083
3084 gdb_static_assert (sizeof (ULONGEST) >= 8);
3085 sect_offset sect_off
3086 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
3087 type_offset_in_tu
3088 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
3089 BFD_ENDIAN_LITTLE);
3090 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
3091 bytes += 3 * 8;
3092
3093 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3094 struct signatured_type);
3095 sig_type->signature = signature;
3096 sig_type->type_offset_in_tu = type_offset_in_tu;
3097 sig_type->per_cu.is_debug_types = 1;
3098 sig_type->per_cu.section = section;
3099 sig_type->per_cu.sect_off = sect_off;
3100 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3101 sig_type->per_cu.v.quick
3102 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3103 struct dwarf2_per_cu_quick_data);
3104
3105 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3106 *slot = sig_type;
3107
3108 dwarf2_per_objfile->all_type_units.push_back (sig_type);
3109 }
3110
3111 dwarf2_per_objfile->signatured_types = sig_types_hash;
3112 }
3113
3114 /* Create the signatured type hash table from .debug_names. */
3115
3116 static void
3117 create_signatured_type_table_from_debug_names
3118 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3119 const mapped_debug_names &map,
3120 struct dwarf2_section_info *section,
3121 struct dwarf2_section_info *abbrev_section)
3122 {
3123 struct objfile *objfile = dwarf2_per_objfile->objfile;
3124
3125 dwarf2_read_section (objfile, section);
3126 dwarf2_read_section (objfile, abbrev_section);
3127
3128 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
3129 dwarf2_per_objfile->all_type_units.reserve (map.tu_count);
3130
3131 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
3132
3133 for (uint32_t i = 0; i < map.tu_count; ++i)
3134 {
3135 struct signatured_type *sig_type;
3136 void **slot;
3137
3138 sect_offset sect_off
3139 = (sect_offset) (extract_unsigned_integer
3140 (map.tu_table_reordered + i * map.offset_size,
3141 map.offset_size,
3142 map.dwarf5_byte_order));
3143
3144 comp_unit_head cu_header;
3145 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
3146 abbrev_section,
3147 section->buffer + to_underlying (sect_off),
3148 rcuh_kind::TYPE);
3149
3150 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3151 struct signatured_type);
3152 sig_type->signature = cu_header.signature;
3153 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
3154 sig_type->per_cu.is_debug_types = 1;
3155 sig_type->per_cu.section = section;
3156 sig_type->per_cu.sect_off = sect_off;
3157 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3158 sig_type->per_cu.v.quick
3159 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3160 struct dwarf2_per_cu_quick_data);
3161
3162 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3163 *slot = sig_type;
3164
3165 dwarf2_per_objfile->all_type_units.push_back (sig_type);
3166 }
3167
3168 dwarf2_per_objfile->signatured_types = sig_types_hash;
3169 }
3170
3171 /* Read the address map data from the mapped index, and use it to
3172 populate the objfile's psymtabs_addrmap. */
3173
3174 static void
3175 create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
3176 struct mapped_index *index)
3177 {
3178 struct objfile *objfile = dwarf2_per_objfile->objfile;
3179 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3180 const gdb_byte *iter, *end;
3181 struct addrmap *mutable_map;
3182 CORE_ADDR baseaddr;
3183
3184 auto_obstack temp_obstack;
3185
3186 mutable_map = addrmap_create_mutable (&temp_obstack);
3187
3188 iter = index->address_table.data ();
3189 end = iter + index->address_table.size ();
3190
3191 baseaddr = objfile->section_offsets[SECT_OFF_TEXT (objfile)];
3192
3193 while (iter < end)
3194 {
3195 ULONGEST hi, lo, cu_index;
3196 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3197 iter += 8;
3198 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3199 iter += 8;
3200 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
3201 iter += 4;
3202
3203 if (lo > hi)
3204 {
3205 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
3206 hex_string (lo), hex_string (hi));
3207 continue;
3208 }
3209
3210 if (cu_index >= dwarf2_per_objfile->all_comp_units.size ())
3211 {
3212 complaint (_(".gdb_index address table has invalid CU number %u"),
3213 (unsigned) cu_index);
3214 continue;
3215 }
3216
3217 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
3218 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
3219 addrmap_set_empty (mutable_map, lo, hi - 1,
3220 dwarf2_per_objfile->get_cu (cu_index));
3221 }
3222
3223 objfile->partial_symtabs->psymtabs_addrmap
3224 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
3225 }
3226
3227 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
3228 populate the objfile's psymtabs_addrmap. */
3229
3230 static void
3231 create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
3232 struct dwarf2_section_info *section)
3233 {
3234 struct objfile *objfile = dwarf2_per_objfile->objfile;
3235 bfd *abfd = objfile->obfd;
3236 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3237 const CORE_ADDR baseaddr = objfile->section_offsets[SECT_OFF_TEXT (objfile)];
3238
3239 auto_obstack temp_obstack;
3240 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
3241
3242 std::unordered_map<sect_offset,
3243 dwarf2_per_cu_data *,
3244 gdb::hash_enum<sect_offset>>
3245 debug_info_offset_to_per_cu;
3246 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3247 {
3248 const auto insertpair
3249 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
3250 if (!insertpair.second)
3251 {
3252 warning (_("Section .debug_aranges in %s has duplicate "
3253 "debug_info_offset %s, ignoring .debug_aranges."),
3254 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
3255 return;
3256 }
3257 }
3258
3259 dwarf2_read_section (objfile, section);
3260
3261 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
3262
3263 const gdb_byte *addr = section->buffer;
3264
3265 while (addr < section->buffer + section->size)
3266 {
3267 const gdb_byte *const entry_addr = addr;
3268 unsigned int bytes_read;
3269
3270 const LONGEST entry_length = read_initial_length (abfd, addr,
3271 &bytes_read);
3272 addr += bytes_read;
3273
3274 const gdb_byte *const entry_end = addr + entry_length;
3275 const bool dwarf5_is_dwarf64 = bytes_read != 4;
3276 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
3277 if (addr + entry_length > section->buffer + section->size)
3278 {
3279 warning (_("Section .debug_aranges in %s entry at offset %s "
3280 "length %s exceeds section length %s, "
3281 "ignoring .debug_aranges."),
3282 objfile_name (objfile),
3283 plongest (entry_addr - section->buffer),
3284 plongest (bytes_read + entry_length),
3285 pulongest (section->size));
3286 return;
3287 }
3288
3289 /* The version number. */
3290 const uint16_t version = read_2_bytes (abfd, addr);
3291 addr += 2;
3292 if (version != 2)
3293 {
3294 warning (_("Section .debug_aranges in %s entry at offset %s "
3295 "has unsupported version %d, ignoring .debug_aranges."),
3296 objfile_name (objfile),
3297 plongest (entry_addr - section->buffer), version);
3298 return;
3299 }
3300
3301 const uint64_t debug_info_offset
3302 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
3303 addr += offset_size;
3304 const auto per_cu_it
3305 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
3306 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
3307 {
3308 warning (_("Section .debug_aranges in %s entry at offset %s "
3309 "debug_info_offset %s does not exists, "
3310 "ignoring .debug_aranges."),
3311 objfile_name (objfile),
3312 plongest (entry_addr - section->buffer),
3313 pulongest (debug_info_offset));
3314 return;
3315 }
3316 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
3317
3318 const uint8_t address_size = *addr++;
3319 if (address_size < 1 || address_size > 8)
3320 {
3321 warning (_("Section .debug_aranges in %s entry at offset %s "
3322 "address_size %u is invalid, ignoring .debug_aranges."),
3323 objfile_name (objfile),
3324 plongest (entry_addr - section->buffer), address_size);
3325 return;
3326 }
3327
3328 const uint8_t segment_selector_size = *addr++;
3329 if (segment_selector_size != 0)
3330 {
3331 warning (_("Section .debug_aranges in %s entry at offset %s "
3332 "segment_selector_size %u is not supported, "
3333 "ignoring .debug_aranges."),
3334 objfile_name (objfile),
3335 plongest (entry_addr - section->buffer),
3336 segment_selector_size);
3337 return;
3338 }
3339
3340 /* Must pad to an alignment boundary that is twice the address
3341 size. It is undocumented by the DWARF standard but GCC does
3342 use it. */
3343 for (size_t padding = ((-(addr - section->buffer))
3344 & (2 * address_size - 1));
3345 padding > 0; padding--)
3346 if (*addr++ != 0)
3347 {
3348 warning (_("Section .debug_aranges in %s entry at offset %s "
3349 "padding is not zero, ignoring .debug_aranges."),
3350 objfile_name (objfile),
3351 plongest (entry_addr - section->buffer));
3352 return;
3353 }
3354
3355 for (;;)
3356 {
3357 if (addr + 2 * address_size > entry_end)
3358 {
3359 warning (_("Section .debug_aranges in %s entry at offset %s "
3360 "address list is not properly terminated, "
3361 "ignoring .debug_aranges."),
3362 objfile_name (objfile),
3363 plongest (entry_addr - section->buffer));
3364 return;
3365 }
3366 ULONGEST start = extract_unsigned_integer (addr, address_size,
3367 dwarf5_byte_order);
3368 addr += address_size;
3369 ULONGEST length = extract_unsigned_integer (addr, address_size,
3370 dwarf5_byte_order);
3371 addr += address_size;
3372 if (start == 0 && length == 0)
3373 break;
3374 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
3375 {
3376 /* Symbol was eliminated due to a COMDAT group. */
3377 continue;
3378 }
3379 ULONGEST end = start + length;
3380 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
3381 - baseaddr);
3382 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
3383 - baseaddr);
3384 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
3385 }
3386 }
3387
3388 objfile->partial_symtabs->psymtabs_addrmap
3389 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
3390 }
3391
3392 /* Find a slot in the mapped index INDEX for the object named NAME.
3393 If NAME is found, set *VEC_OUT to point to the CU vector in the
3394 constant pool and return true. If NAME cannot be found, return
3395 false. */
3396
3397 static bool
3398 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3399 offset_type **vec_out)
3400 {
3401 offset_type hash;
3402 offset_type slot, step;
3403 int (*cmp) (const char *, const char *);
3404
3405 gdb::unique_xmalloc_ptr<char> without_params;
3406 if (current_language->la_language == language_cplus
3407 || current_language->la_language == language_fortran
3408 || current_language->la_language == language_d)
3409 {
3410 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3411 not contain any. */
3412
3413 if (strchr (name, '(') != NULL)
3414 {
3415 without_params = cp_remove_params (name);
3416
3417 if (without_params != NULL)
3418 name = without_params.get ();
3419 }
3420 }
3421
3422 /* Index version 4 did not support case insensitive searches. But the
3423 indices for case insensitive languages are built in lowercase, therefore
3424 simulate our NAME being searched is also lowercased. */
3425 hash = mapped_index_string_hash ((index->version == 4
3426 && case_sensitivity == case_sensitive_off
3427 ? 5 : index->version),
3428 name);
3429
3430 slot = hash & (index->symbol_table.size () - 1);
3431 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
3432 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
3433
3434 for (;;)
3435 {
3436 const char *str;
3437
3438 const auto &bucket = index->symbol_table[slot];
3439 if (bucket.name == 0 && bucket.vec == 0)
3440 return false;
3441
3442 str = index->constant_pool + MAYBE_SWAP (bucket.name);
3443 if (!cmp (name, str))
3444 {
3445 *vec_out = (offset_type *) (index->constant_pool
3446 + MAYBE_SWAP (bucket.vec));
3447 return true;
3448 }
3449
3450 slot = (slot + step) & (index->symbol_table.size () - 1);
3451 }
3452 }
3453
3454 /* A helper function that reads the .gdb_index from BUFFER and fills
3455 in MAP. FILENAME is the name of the file containing the data;
3456 it is used for error reporting. DEPRECATED_OK is true if it is
3457 ok to use deprecated sections.
3458
3459 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3460 out parameters that are filled in with information about the CU and
3461 TU lists in the section.
3462
3463 Returns true if all went well, false otherwise. */
3464
3465 static bool
3466 read_gdb_index_from_buffer (struct objfile *objfile,
3467 const char *filename,
3468 bool deprecated_ok,
3469 gdb::array_view<const gdb_byte> buffer,
3470 struct mapped_index *map,
3471 const gdb_byte **cu_list,
3472 offset_type *cu_list_elements,
3473 const gdb_byte **types_list,
3474 offset_type *types_list_elements)
3475 {
3476 const gdb_byte *addr = &buffer[0];
3477
3478 /* Version check. */
3479 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
3480 /* Versions earlier than 3 emitted every copy of a psymbol. This
3481 causes the index to behave very poorly for certain requests. Version 3
3482 contained incomplete addrmap. So, it seems better to just ignore such
3483 indices. */
3484 if (version < 4)
3485 {
3486 static int warning_printed = 0;
3487 if (!warning_printed)
3488 {
3489 warning (_("Skipping obsolete .gdb_index section in %s."),
3490 filename);
3491 warning_printed = 1;
3492 }
3493 return 0;
3494 }
3495 /* Index version 4 uses a different hash function than index version
3496 5 and later.
3497
3498 Versions earlier than 6 did not emit psymbols for inlined
3499 functions. Using these files will cause GDB not to be able to
3500 set breakpoints on inlined functions by name, so we ignore these
3501 indices unless the user has done
3502 "set use-deprecated-index-sections on". */
3503 if (version < 6 && !deprecated_ok)
3504 {
3505 static int warning_printed = 0;
3506 if (!warning_printed)
3507 {
3508 warning (_("\
3509 Skipping deprecated .gdb_index section in %s.\n\
3510 Do \"set use-deprecated-index-sections on\" before the file is read\n\
3511 to use the section anyway."),
3512 filename);
3513 warning_printed = 1;
3514 }
3515 return 0;
3516 }
3517 /* Version 7 indices generated by gold refer to the CU for a symbol instead
3518 of the TU (for symbols coming from TUs),
3519 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3520 Plus gold-generated indices can have duplicate entries for global symbols,
3521 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3522 These are just performance bugs, and we can't distinguish gdb-generated
3523 indices from gold-generated ones, so issue no warning here. */
3524
3525 /* Indexes with higher version than the one supported by GDB may be no
3526 longer backward compatible. */
3527 if (version > 8)
3528 return 0;
3529
3530 map->version = version;
3531
3532 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
3533
3534 int i = 0;
3535 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3536 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3537 / 8);
3538 ++i;
3539
3540 *types_list = addr + MAYBE_SWAP (metadata[i]);
3541 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3542 - MAYBE_SWAP (metadata[i]))
3543 / 8);
3544 ++i;
3545
3546 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3547 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3548 map->address_table
3549 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
3550 ++i;
3551
3552 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3553 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3554 map->symbol_table
3555 = gdb::array_view<mapped_index::symbol_table_slot>
3556 ((mapped_index::symbol_table_slot *) symbol_table,
3557 (mapped_index::symbol_table_slot *) symbol_table_end);
3558
3559 ++i;
3560 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
3561
3562 return 1;
3563 }
3564
3565 /* Callback types for dwarf2_read_gdb_index. */
3566
3567 typedef gdb::function_view
3568 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_objfile *)>
3569 get_gdb_index_contents_ftype;
3570 typedef gdb::function_view
3571 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3572 get_gdb_index_contents_dwz_ftype;
3573
3574 /* Read .gdb_index. If everything went ok, initialize the "quick"
3575 elements of all the CUs and return 1. Otherwise, return 0. */
3576
3577 static int
3578 dwarf2_read_gdb_index
3579 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3580 get_gdb_index_contents_ftype get_gdb_index_contents,
3581 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
3582 {
3583 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3584 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3585 struct dwz_file *dwz;
3586 struct objfile *objfile = dwarf2_per_objfile->objfile;
3587
3588 gdb::array_view<const gdb_byte> main_index_contents
3589 = get_gdb_index_contents (objfile, dwarf2_per_objfile);
3590
3591 if (main_index_contents.empty ())
3592 return 0;
3593
3594 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3595 if (!read_gdb_index_from_buffer (objfile, objfile_name (objfile),
3596 use_deprecated_index_sections,
3597 main_index_contents, map.get (), &cu_list,
3598 &cu_list_elements, &types_list,
3599 &types_list_elements))
3600 return 0;
3601
3602 /* Don't use the index if it's empty. */
3603 if (map->symbol_table.empty ())
3604 return 0;
3605
3606 /* If there is a .dwz file, read it so we can get its CU list as
3607 well. */
3608 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3609 if (dwz != NULL)
3610 {
3611 struct mapped_index dwz_map;
3612 const gdb_byte *dwz_types_ignore;
3613 offset_type dwz_types_elements_ignore;
3614
3615 gdb::array_view<const gdb_byte> dwz_index_content
3616 = get_gdb_index_contents_dwz (objfile, dwz);
3617
3618 if (dwz_index_content.empty ())
3619 return 0;
3620
3621 if (!read_gdb_index_from_buffer (objfile,
3622 bfd_get_filename (dwz->dwz_bfd.get ()),
3623 1, dwz_index_content, &dwz_map,
3624 &dwz_list, &dwz_list_elements,
3625 &dwz_types_ignore,
3626 &dwz_types_elements_ignore))
3627 {
3628 warning (_("could not read '.gdb_index' section from %s; skipping"),
3629 bfd_get_filename (dwz->dwz_bfd.get ()));
3630 return 0;
3631 }
3632 }
3633
3634 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3635 dwz_list, dwz_list_elements);
3636
3637 if (types_list_elements)
3638 {
3639 /* We can only handle a single .debug_types when we have an
3640 index. */
3641 if (dwarf2_per_objfile->types.size () != 1)
3642 return 0;
3643
3644 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
3645
3646 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3647 types_list, types_list_elements);
3648 }
3649
3650 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
3651
3652 dwarf2_per_objfile->index_table = std::move (map);
3653 dwarf2_per_objfile->using_index = 1;
3654 dwarf2_per_objfile->quick_file_names_table =
3655 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
3656
3657 return 1;
3658 }
3659
3660 /* die_reader_func for dw2_get_file_names. */
3661
3662 static void
3663 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3664 const gdb_byte *info_ptr,
3665 struct die_info *comp_unit_die,
3666 int has_children,
3667 void *data)
3668 {
3669 struct dwarf2_cu *cu = reader->cu;
3670 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3671 struct dwarf2_per_objfile *dwarf2_per_objfile
3672 = cu->per_cu->dwarf2_per_objfile;
3673 struct objfile *objfile = dwarf2_per_objfile->objfile;
3674 struct dwarf2_per_cu_data *lh_cu;
3675 struct attribute *attr;
3676 void **slot;
3677 struct quick_file_names *qfn;
3678
3679 gdb_assert (! this_cu->is_debug_types);
3680
3681 /* Our callers never want to match partial units -- instead they
3682 will match the enclosing full CU. */
3683 if (comp_unit_die->tag == DW_TAG_partial_unit)
3684 {
3685 this_cu->v.quick->no_file_data = 1;
3686 return;
3687 }
3688
3689 lh_cu = this_cu;
3690 slot = NULL;
3691
3692 line_header_up lh;
3693 sect_offset line_offset {};
3694
3695 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3696 if (attr != nullptr)
3697 {
3698 struct quick_file_names find_entry;
3699
3700 line_offset = (sect_offset) DW_UNSND (attr);
3701
3702 /* We may have already read in this line header (TU line header sharing).
3703 If we have we're done. */
3704 find_entry.hash.dwo_unit = cu->dwo_unit;
3705 find_entry.hash.line_sect_off = line_offset;
3706 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3707 &find_entry, INSERT);
3708 if (*slot != NULL)
3709 {
3710 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3711 return;
3712 }
3713
3714 lh = dwarf_decode_line_header (line_offset, cu);
3715 }
3716 if (lh == NULL)
3717 {
3718 lh_cu->v.quick->no_file_data = 1;
3719 return;
3720 }
3721
3722 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
3723 qfn->hash.dwo_unit = cu->dwo_unit;
3724 qfn->hash.line_sect_off = line_offset;
3725 gdb_assert (slot != NULL);
3726 *slot = qfn;
3727
3728 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3729
3730 int offset = 0;
3731 if (strcmp (fnd.name, "<unknown>") != 0)
3732 ++offset;
3733
3734 qfn->num_file_names = offset + lh->file_names_size ();
3735 qfn->file_names =
3736 XOBNEWVEC (&objfile->objfile_obstack, const char *, qfn->num_file_names);
3737 if (offset != 0)
3738 qfn->file_names[0] = xstrdup (fnd.name);
3739 for (int i = 0; i < lh->file_names_size (); ++i)
3740 qfn->file_names[i + offset] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
3741 qfn->real_names = NULL;
3742
3743 lh_cu->v.quick->file_names = qfn;
3744 }
3745
3746 /* A helper for the "quick" functions which attempts to read the line
3747 table for THIS_CU. */
3748
3749 static struct quick_file_names *
3750 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
3751 {
3752 /* This should never be called for TUs. */
3753 gdb_assert (! this_cu->is_debug_types);
3754 /* Nor type unit groups. */
3755 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
3756
3757 if (this_cu->v.quick->file_names != NULL)
3758 return this_cu->v.quick->file_names;
3759 /* If we know there is no line data, no point in looking again. */
3760 if (this_cu->v.quick->no_file_data)
3761 return NULL;
3762
3763 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
3764
3765 if (this_cu->v.quick->no_file_data)
3766 return NULL;
3767 return this_cu->v.quick->file_names;
3768 }
3769
3770 /* A helper for the "quick" functions which computes and caches the
3771 real path for a given file name from the line table. */
3772
3773 static const char *
3774 dw2_get_real_path (struct objfile *objfile,
3775 struct quick_file_names *qfn, int index)
3776 {
3777 if (qfn->real_names == NULL)
3778 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
3779 qfn->num_file_names, const char *);
3780
3781 if (qfn->real_names[index] == NULL)
3782 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3783
3784 return qfn->real_names[index];
3785 }
3786
3787 static struct symtab *
3788 dw2_find_last_source_symtab (struct objfile *objfile)
3789 {
3790 struct dwarf2_per_objfile *dwarf2_per_objfile
3791 = get_dwarf2_per_objfile (objfile);
3792 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->all_comp_units.back ();
3793 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, false);
3794
3795 if (cust == NULL)
3796 return NULL;
3797
3798 return compunit_primary_filetab (cust);
3799 }
3800
3801 /* Traversal function for dw2_forget_cached_source_info. */
3802
3803 static int
3804 dw2_free_cached_file_names (void **slot, void *info)
3805 {
3806 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3807
3808 if (file_data->real_names)
3809 {
3810 int i;
3811
3812 for (i = 0; i < file_data->num_file_names; ++i)
3813 {
3814 xfree ((void*) file_data->real_names[i]);
3815 file_data->real_names[i] = NULL;
3816 }
3817 }
3818
3819 return 1;
3820 }
3821
3822 static void
3823 dw2_forget_cached_source_info (struct objfile *objfile)
3824 {
3825 struct dwarf2_per_objfile *dwarf2_per_objfile
3826 = get_dwarf2_per_objfile (objfile);
3827
3828 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3829 dw2_free_cached_file_names, NULL);
3830 }
3831
3832 /* Helper function for dw2_map_symtabs_matching_filename that expands
3833 the symtabs and calls the iterator. */
3834
3835 static int
3836 dw2_map_expand_apply (struct objfile *objfile,
3837 struct dwarf2_per_cu_data *per_cu,
3838 const char *name, const char *real_path,
3839 gdb::function_view<bool (symtab *)> callback)
3840 {
3841 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3842
3843 /* Don't visit already-expanded CUs. */
3844 if (per_cu->v.quick->compunit_symtab)
3845 return 0;
3846
3847 /* This may expand more than one symtab, and we want to iterate over
3848 all of them. */
3849 dw2_instantiate_symtab (per_cu, false);
3850
3851 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3852 last_made, callback);
3853 }
3854
3855 /* Implementation of the map_symtabs_matching_filename method. */
3856
3857 static bool
3858 dw2_map_symtabs_matching_filename
3859 (struct objfile *objfile, const char *name, const char *real_path,
3860 gdb::function_view<bool (symtab *)> callback)
3861 {
3862 const char *name_basename = lbasename (name);
3863 struct dwarf2_per_objfile *dwarf2_per_objfile
3864 = get_dwarf2_per_objfile (objfile);
3865
3866 /* The rule is CUs specify all the files, including those used by
3867 any TU, so there's no need to scan TUs here. */
3868
3869 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3870 {
3871 /* We only need to look at symtabs not already expanded. */
3872 if (per_cu->v.quick->compunit_symtab)
3873 continue;
3874
3875 quick_file_names *file_data = dw2_get_file_names (per_cu);
3876 if (file_data == NULL)
3877 continue;
3878
3879 for (int j = 0; j < file_data->num_file_names; ++j)
3880 {
3881 const char *this_name = file_data->file_names[j];
3882 const char *this_real_name;
3883
3884 if (compare_filenames_for_search (this_name, name))
3885 {
3886 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3887 callback))
3888 return true;
3889 continue;
3890 }
3891
3892 /* Before we invoke realpath, which can get expensive when many
3893 files are involved, do a quick comparison of the basenames. */
3894 if (! basenames_may_differ
3895 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3896 continue;
3897
3898 this_real_name = dw2_get_real_path (objfile, file_data, j);
3899 if (compare_filenames_for_search (this_real_name, name))
3900 {
3901 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3902 callback))
3903 return true;
3904 continue;
3905 }
3906
3907 if (real_path != NULL)
3908 {
3909 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3910 gdb_assert (IS_ABSOLUTE_PATH (name));
3911 if (this_real_name != NULL
3912 && FILENAME_CMP (real_path, this_real_name) == 0)
3913 {
3914 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3915 callback))
3916 return true;
3917 continue;
3918 }
3919 }
3920 }
3921 }
3922
3923 return false;
3924 }
3925
3926 /* Struct used to manage iterating over all CUs looking for a symbol. */
3927
3928 struct dw2_symtab_iterator
3929 {
3930 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3931 struct dwarf2_per_objfile *dwarf2_per_objfile;
3932 /* If set, only look for symbols that match that block. Valid values are
3933 GLOBAL_BLOCK and STATIC_BLOCK. */
3934 gdb::optional<block_enum> block_index;
3935 /* The kind of symbol we're looking for. */
3936 domain_enum domain;
3937 /* The list of CUs from the index entry of the symbol,
3938 or NULL if not found. */
3939 offset_type *vec;
3940 /* The next element in VEC to look at. */
3941 int next;
3942 /* The number of elements in VEC, or zero if there is no match. */
3943 int length;
3944 /* Have we seen a global version of the symbol?
3945 If so we can ignore all further global instances.
3946 This is to work around gold/15646, inefficient gold-generated
3947 indices. */
3948 int global_seen;
3949 };
3950
3951 /* Initialize the index symtab iterator ITER. */
3952
3953 static void
3954 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3955 struct dwarf2_per_objfile *dwarf2_per_objfile,
3956 gdb::optional<block_enum> block_index,
3957 domain_enum domain,
3958 const char *name)
3959 {
3960 iter->dwarf2_per_objfile = dwarf2_per_objfile;
3961 iter->block_index = block_index;
3962 iter->domain = domain;
3963 iter->next = 0;
3964 iter->global_seen = 0;
3965
3966 mapped_index *index = dwarf2_per_objfile->index_table.get ();
3967
3968 /* index is NULL if OBJF_READNOW. */
3969 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
3970 iter->length = MAYBE_SWAP (*iter->vec);
3971 else
3972 {
3973 iter->vec = NULL;
3974 iter->length = 0;
3975 }
3976 }
3977
3978 /* Return the next matching CU or NULL if there are no more. */
3979
3980 static struct dwarf2_per_cu_data *
3981 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3982 {
3983 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3984
3985 for ( ; iter->next < iter->length; ++iter->next)
3986 {
3987 offset_type cu_index_and_attrs =
3988 MAYBE_SWAP (iter->vec[iter->next + 1]);
3989 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3990 gdb_index_symbol_kind symbol_kind =
3991 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3992 /* Only check the symbol attributes if they're present.
3993 Indices prior to version 7 don't record them,
3994 and indices >= 7 may elide them for certain symbols
3995 (gold does this). */
3996 int attrs_valid =
3997 (dwarf2_per_objfile->index_table->version >= 7
3998 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3999
4000 /* Don't crash on bad data. */
4001 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
4002 + dwarf2_per_objfile->all_type_units.size ()))
4003 {
4004 complaint (_(".gdb_index entry has bad CU index"
4005 " [in module %s]"),
4006 objfile_name (dwarf2_per_objfile->objfile));
4007 continue;
4008 }
4009
4010 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
4011
4012 /* Skip if already read in. */
4013 if (per_cu->v.quick->compunit_symtab)
4014 continue;
4015
4016 /* Check static vs global. */
4017 if (attrs_valid)
4018 {
4019 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4020
4021 if (iter->block_index.has_value ())
4022 {
4023 bool want_static = *iter->block_index == STATIC_BLOCK;
4024
4025 if (is_static != want_static)
4026 continue;
4027 }
4028
4029 /* Work around gold/15646. */
4030 if (!is_static && iter->global_seen)
4031 continue;
4032 if (!is_static)
4033 iter->global_seen = 1;
4034 }
4035
4036 /* Only check the symbol's kind if it has one. */
4037 if (attrs_valid)
4038 {
4039 switch (iter->domain)
4040 {
4041 case VAR_DOMAIN:
4042 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
4043 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
4044 /* Some types are also in VAR_DOMAIN. */
4045 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4046 continue;
4047 break;
4048 case STRUCT_DOMAIN:
4049 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4050 continue;
4051 break;
4052 case LABEL_DOMAIN:
4053 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4054 continue;
4055 break;
4056 case MODULE_DOMAIN:
4057 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4058 continue;
4059 break;
4060 default:
4061 break;
4062 }
4063 }
4064
4065 ++iter->next;
4066 return per_cu;
4067 }
4068
4069 return NULL;
4070 }
4071
4072 static struct compunit_symtab *
4073 dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
4074 const char *name, domain_enum domain)
4075 {
4076 struct compunit_symtab *stab_best = NULL;
4077 struct dwarf2_per_objfile *dwarf2_per_objfile
4078 = get_dwarf2_per_objfile (objfile);
4079
4080 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
4081
4082 struct dw2_symtab_iterator iter;
4083 struct dwarf2_per_cu_data *per_cu;
4084
4085 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
4086
4087 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4088 {
4089 struct symbol *sym, *with_opaque = NULL;
4090 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
4091 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
4092 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
4093
4094 sym = block_find_symbol (block, name, domain,
4095 block_find_non_opaque_type_preferred,
4096 &with_opaque);
4097
4098 /* Some caution must be observed with overloaded functions
4099 and methods, since the index will not contain any overload
4100 information (but NAME might contain it). */
4101
4102 if (sym != NULL
4103 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
4104 return stab;
4105 if (with_opaque != NULL
4106 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
4107 stab_best = stab;
4108
4109 /* Keep looking through other CUs. */
4110 }
4111
4112 return stab_best;
4113 }
4114
4115 static void
4116 dw2_print_stats (struct objfile *objfile)
4117 {
4118 struct dwarf2_per_objfile *dwarf2_per_objfile
4119 = get_dwarf2_per_objfile (objfile);
4120 int total = (dwarf2_per_objfile->all_comp_units.size ()
4121 + dwarf2_per_objfile->all_type_units.size ());
4122 int count = 0;
4123
4124 for (int i = 0; i < total; ++i)
4125 {
4126 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
4127
4128 if (!per_cu->v.quick->compunit_symtab)
4129 ++count;
4130 }
4131 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
4132 printf_filtered (_(" Number of unread CUs: %d\n"), count);
4133 }
4134
4135 /* This dumps minimal information about the index.
4136 It is called via "mt print objfiles".
4137 One use is to verify .gdb_index has been loaded by the
4138 gdb.dwarf2/gdb-index.exp testcase. */
4139
4140 static void
4141 dw2_dump (struct objfile *objfile)
4142 {
4143 struct dwarf2_per_objfile *dwarf2_per_objfile
4144 = get_dwarf2_per_objfile (objfile);
4145
4146 gdb_assert (dwarf2_per_objfile->using_index);
4147 printf_filtered (".gdb_index:");
4148 if (dwarf2_per_objfile->index_table != NULL)
4149 {
4150 printf_filtered (" version %d\n",
4151 dwarf2_per_objfile->index_table->version);
4152 }
4153 else
4154 printf_filtered (" faked for \"readnow\"\n");
4155 printf_filtered ("\n");
4156 }
4157
4158 static void
4159 dw2_expand_symtabs_for_function (struct objfile *objfile,
4160 const char *func_name)
4161 {
4162 struct dwarf2_per_objfile *dwarf2_per_objfile
4163 = get_dwarf2_per_objfile (objfile);
4164
4165 struct dw2_symtab_iterator iter;
4166 struct dwarf2_per_cu_data *per_cu;
4167
4168 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
4169
4170 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4171 dw2_instantiate_symtab (per_cu, false);
4172
4173 }
4174
4175 static void
4176 dw2_expand_all_symtabs (struct objfile *objfile)
4177 {
4178 struct dwarf2_per_objfile *dwarf2_per_objfile
4179 = get_dwarf2_per_objfile (objfile);
4180 int total_units = (dwarf2_per_objfile->all_comp_units.size ()
4181 + dwarf2_per_objfile->all_type_units.size ());
4182
4183 for (int i = 0; i < total_units; ++i)
4184 {
4185 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
4186
4187 /* We don't want to directly expand a partial CU, because if we
4188 read it with the wrong language, then assertion failures can
4189 be triggered later on. See PR symtab/23010. So, tell
4190 dw2_instantiate_symtab to skip partial CUs -- any important
4191 partial CU will be read via DW_TAG_imported_unit anyway. */
4192 dw2_instantiate_symtab (per_cu, true);
4193 }
4194 }
4195
4196 static void
4197 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
4198 const char *fullname)
4199 {
4200 struct dwarf2_per_objfile *dwarf2_per_objfile
4201 = get_dwarf2_per_objfile (objfile);
4202
4203 /* We don't need to consider type units here.
4204 This is only called for examining code, e.g. expand_line_sal.
4205 There can be an order of magnitude (or more) more type units
4206 than comp units, and we avoid them if we can. */
4207
4208 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4209 {
4210 /* We only need to look at symtabs not already expanded. */
4211 if (per_cu->v.quick->compunit_symtab)
4212 continue;
4213
4214 quick_file_names *file_data = dw2_get_file_names (per_cu);
4215 if (file_data == NULL)
4216 continue;
4217
4218 for (int j = 0; j < file_data->num_file_names; ++j)
4219 {
4220 const char *this_fullname = file_data->file_names[j];
4221
4222 if (filename_cmp (this_fullname, fullname) == 0)
4223 {
4224 dw2_instantiate_symtab (per_cu, false);
4225 break;
4226 }
4227 }
4228 }
4229 }
4230
4231 static void
4232 dw2_map_matching_symbols
4233 (struct objfile *objfile,
4234 const lookup_name_info &name, domain_enum domain,
4235 int global,
4236 gdb::function_view<symbol_found_callback_ftype> callback,
4237 symbol_compare_ftype *ordered_compare)
4238 {
4239 /* Currently unimplemented; used for Ada. The function can be called if the
4240 current language is Ada for a non-Ada objfile using GNU index. As Ada
4241 does not look for non-Ada symbols this function should just return. */
4242 }
4243
4244 /* Starting from a search name, return the string that finds the upper
4245 bound of all strings that start with SEARCH_NAME in a sorted name
4246 list. Returns the empty string to indicate that the upper bound is
4247 the end of the list. */
4248
4249 static std::string
4250 make_sort_after_prefix_name (const char *search_name)
4251 {
4252 /* When looking to complete "func", we find the upper bound of all
4253 symbols that start with "func" by looking for where we'd insert
4254 the closest string that would follow "func" in lexicographical
4255 order. Usually, that's "func"-with-last-character-incremented,
4256 i.e. "fund". Mind non-ASCII characters, though. Usually those
4257 will be UTF-8 multi-byte sequences, but we can't be certain.
4258 Especially mind the 0xff character, which is a valid character in
4259 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
4260 rule out compilers allowing it in identifiers. Note that
4261 conveniently, strcmp/strcasecmp are specified to compare
4262 characters interpreted as unsigned char. So what we do is treat
4263 the whole string as a base 256 number composed of a sequence of
4264 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
4265 to 0, and carries 1 to the following more-significant position.
4266 If the very first character in SEARCH_NAME ends up incremented
4267 and carries/overflows, then the upper bound is the end of the
4268 list. The string after the empty string is also the empty
4269 string.
4270
4271 Some examples of this operation:
4272
4273 SEARCH_NAME => "+1" RESULT
4274
4275 "abc" => "abd"
4276 "ab\xff" => "ac"
4277 "\xff" "a" "\xff" => "\xff" "b"
4278 "\xff" => ""
4279 "\xff\xff" => ""
4280 "" => ""
4281
4282 Then, with these symbols for example:
4283
4284 func
4285 func1
4286 fund
4287
4288 completing "func" looks for symbols between "func" and
4289 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
4290 which finds "func" and "func1", but not "fund".
4291
4292 And with:
4293
4294 funcÿ (Latin1 'ÿ' [0xff])
4295 funcÿ1
4296 fund
4297
4298 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
4299 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
4300
4301 And with:
4302
4303 ÿÿ (Latin1 'ÿ' [0xff])
4304 ÿÿ1
4305
4306 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
4307 the end of the list.
4308 */
4309 std::string after = search_name;
4310 while (!after.empty () && (unsigned char) after.back () == 0xff)
4311 after.pop_back ();
4312 if (!after.empty ())
4313 after.back () = (unsigned char) after.back () + 1;
4314 return after;
4315 }
4316
4317 /* See declaration. */
4318
4319 std::pair<std::vector<name_component>::const_iterator,
4320 std::vector<name_component>::const_iterator>
4321 mapped_index_base::find_name_components_bounds
4322 (const lookup_name_info &lookup_name_without_params, language lang) const
4323 {
4324 auto *name_cmp
4325 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4326
4327 const char *lang_name
4328 = lookup_name_without_params.language_lookup_name (lang).c_str ();
4329
4330 /* Comparison function object for lower_bound that matches against a
4331 given symbol name. */
4332 auto lookup_compare_lower = [&] (const name_component &elem,
4333 const char *name)
4334 {
4335 const char *elem_qualified = this->symbol_name_at (elem.idx);
4336 const char *elem_name = elem_qualified + elem.name_offset;
4337 return name_cmp (elem_name, name) < 0;
4338 };
4339
4340 /* Comparison function object for upper_bound that matches against a
4341 given symbol name. */
4342 auto lookup_compare_upper = [&] (const char *name,
4343 const name_component &elem)
4344 {
4345 const char *elem_qualified = this->symbol_name_at (elem.idx);
4346 const char *elem_name = elem_qualified + elem.name_offset;
4347 return name_cmp (name, elem_name) < 0;
4348 };
4349
4350 auto begin = this->name_components.begin ();
4351 auto end = this->name_components.end ();
4352
4353 /* Find the lower bound. */
4354 auto lower = [&] ()
4355 {
4356 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
4357 return begin;
4358 else
4359 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
4360 } ();
4361
4362 /* Find the upper bound. */
4363 auto upper = [&] ()
4364 {
4365 if (lookup_name_without_params.completion_mode ())
4366 {
4367 /* In completion mode, we want UPPER to point past all
4368 symbols names that have the same prefix. I.e., with
4369 these symbols, and completing "func":
4370
4371 function << lower bound
4372 function1
4373 other_function << upper bound
4374
4375 We find the upper bound by looking for the insertion
4376 point of "func"-with-last-character-incremented,
4377 i.e. "fund". */
4378 std::string after = make_sort_after_prefix_name (lang_name);
4379 if (after.empty ())
4380 return end;
4381 return std::lower_bound (lower, end, after.c_str (),
4382 lookup_compare_lower);
4383 }
4384 else
4385 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
4386 } ();
4387
4388 return {lower, upper};
4389 }
4390
4391 /* See declaration. */
4392
4393 void
4394 mapped_index_base::build_name_components ()
4395 {
4396 if (!this->name_components.empty ())
4397 return;
4398
4399 this->name_components_casing = case_sensitivity;
4400 auto *name_cmp
4401 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4402
4403 /* The code below only knows how to break apart components of C++
4404 symbol names (and other languages that use '::' as
4405 namespace/module separator) and Ada symbol names. */
4406 auto count = this->symbol_name_count ();
4407 for (offset_type idx = 0; idx < count; idx++)
4408 {
4409 if (this->symbol_name_slot_invalid (idx))
4410 continue;
4411
4412 const char *name = this->symbol_name_at (idx);
4413
4414 /* Add each name component to the name component table. */
4415 unsigned int previous_len = 0;
4416
4417 if (strstr (name, "::") != nullptr)
4418 {
4419 for (unsigned int current_len = cp_find_first_component (name);
4420 name[current_len] != '\0';
4421 current_len += cp_find_first_component (name + current_len))
4422 {
4423 gdb_assert (name[current_len] == ':');
4424 this->name_components.push_back ({previous_len, idx});
4425 /* Skip the '::'. */
4426 current_len += 2;
4427 previous_len = current_len;
4428 }
4429 }
4430 else
4431 {
4432 /* Handle the Ada encoded (aka mangled) form here. */
4433 for (const char *iter = strstr (name, "__");
4434 iter != nullptr;
4435 iter = strstr (iter, "__"))
4436 {
4437 this->name_components.push_back ({previous_len, idx});
4438 iter += 2;
4439 previous_len = iter - name;
4440 }
4441 }
4442
4443 this->name_components.push_back ({previous_len, idx});
4444 }
4445
4446 /* Sort name_components elements by name. */
4447 auto name_comp_compare = [&] (const name_component &left,
4448 const name_component &right)
4449 {
4450 const char *left_qualified = this->symbol_name_at (left.idx);
4451 const char *right_qualified = this->symbol_name_at (right.idx);
4452
4453 const char *left_name = left_qualified + left.name_offset;
4454 const char *right_name = right_qualified + right.name_offset;
4455
4456 return name_cmp (left_name, right_name) < 0;
4457 };
4458
4459 std::sort (this->name_components.begin (),
4460 this->name_components.end (),
4461 name_comp_compare);
4462 }
4463
4464 /* Helper for dw2_expand_symtabs_matching that works with a
4465 mapped_index_base instead of the containing objfile. This is split
4466 to a separate function in order to be able to unit test the
4467 name_components matching using a mock mapped_index_base. For each
4468 symbol name that matches, calls MATCH_CALLBACK, passing it the
4469 symbol's index in the mapped_index_base symbol table. */
4470
4471 static void
4472 dw2_expand_symtabs_matching_symbol
4473 (mapped_index_base &index,
4474 const lookup_name_info &lookup_name_in,
4475 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4476 enum search_domain kind,
4477 gdb::function_view<bool (offset_type)> match_callback)
4478 {
4479 lookup_name_info lookup_name_without_params
4480 = lookup_name_in.make_ignore_params ();
4481
4482 /* Build the symbol name component sorted vector, if we haven't
4483 yet. */
4484 index.build_name_components ();
4485
4486 /* The same symbol may appear more than once in the range though.
4487 E.g., if we're looking for symbols that complete "w", and we have
4488 a symbol named "w1::w2", we'll find the two name components for
4489 that same symbol in the range. To be sure we only call the
4490 callback once per symbol, we first collect the symbol name
4491 indexes that matched in a temporary vector and ignore
4492 duplicates. */
4493 std::vector<offset_type> matches;
4494
4495 struct name_and_matcher
4496 {
4497 symbol_name_matcher_ftype *matcher;
4498 const std::string &name;
4499
4500 bool operator== (const name_and_matcher &other) const
4501 {
4502 return matcher == other.matcher && name == other.name;
4503 }
4504 };
4505
4506 /* A vector holding all the different symbol name matchers, for all
4507 languages. */
4508 std::vector<name_and_matcher> matchers;
4509
4510 for (int i = 0; i < nr_languages; i++)
4511 {
4512 enum language lang_e = (enum language) i;
4513
4514 const language_defn *lang = language_def (lang_e);
4515 symbol_name_matcher_ftype *name_matcher
4516 = get_symbol_name_matcher (lang, lookup_name_without_params);
4517
4518 name_and_matcher key {
4519 name_matcher,
4520 lookup_name_without_params.language_lookup_name (lang_e)
4521 };
4522
4523 /* Don't insert the same comparison routine more than once.
4524 Note that we do this linear walk. This is not a problem in
4525 practice because the number of supported languages is
4526 low. */
4527 if (std::find (matchers.begin (), matchers.end (), key)
4528 != matchers.end ())
4529 continue;
4530 matchers.push_back (std::move (key));
4531
4532 auto bounds
4533 = index.find_name_components_bounds (lookup_name_without_params,
4534 lang_e);
4535
4536 /* Now for each symbol name in range, check to see if we have a name
4537 match, and if so, call the MATCH_CALLBACK callback. */
4538
4539 for (; bounds.first != bounds.second; ++bounds.first)
4540 {
4541 const char *qualified = index.symbol_name_at (bounds.first->idx);
4542
4543 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4544 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4545 continue;
4546
4547 matches.push_back (bounds.first->idx);
4548 }
4549 }
4550
4551 std::sort (matches.begin (), matches.end ());
4552
4553 /* Finally call the callback, once per match. */
4554 ULONGEST prev = -1;
4555 for (offset_type idx : matches)
4556 {
4557 if (prev != idx)
4558 {
4559 if (!match_callback (idx))
4560 break;
4561 prev = idx;
4562 }
4563 }
4564
4565 /* Above we use a type wider than idx's for 'prev', since 0 and
4566 (offset_type)-1 are both possible values. */
4567 static_assert (sizeof (prev) > sizeof (offset_type), "");
4568 }
4569
4570 #if GDB_SELF_TEST
4571
4572 namespace selftests { namespace dw2_expand_symtabs_matching {
4573
4574 /* A mock .gdb_index/.debug_names-like name index table, enough to
4575 exercise dw2_expand_symtabs_matching_symbol, which works with the
4576 mapped_index_base interface. Builds an index from the symbol list
4577 passed as parameter to the constructor. */
4578 class mock_mapped_index : public mapped_index_base
4579 {
4580 public:
4581 mock_mapped_index (gdb::array_view<const char *> symbols)
4582 : m_symbol_table (symbols)
4583 {}
4584
4585 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
4586
4587 /* Return the number of names in the symbol table. */
4588 size_t symbol_name_count () const override
4589 {
4590 return m_symbol_table.size ();
4591 }
4592
4593 /* Get the name of the symbol at IDX in the symbol table. */
4594 const char *symbol_name_at (offset_type idx) const override
4595 {
4596 return m_symbol_table[idx];
4597 }
4598
4599 private:
4600 gdb::array_view<const char *> m_symbol_table;
4601 };
4602
4603 /* Convenience function that converts a NULL pointer to a "<null>"
4604 string, to pass to print routines. */
4605
4606 static const char *
4607 string_or_null (const char *str)
4608 {
4609 return str != NULL ? str : "<null>";
4610 }
4611
4612 /* Check if a lookup_name_info built from
4613 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4614 index. EXPECTED_LIST is the list of expected matches, in expected
4615 matching order. If no match expected, then an empty list is
4616 specified. Returns true on success. On failure prints a warning
4617 indicating the file:line that failed, and returns false. */
4618
4619 static bool
4620 check_match (const char *file, int line,
4621 mock_mapped_index &mock_index,
4622 const char *name, symbol_name_match_type match_type,
4623 bool completion_mode,
4624 std::initializer_list<const char *> expected_list)
4625 {
4626 lookup_name_info lookup_name (name, match_type, completion_mode);
4627
4628 bool matched = true;
4629
4630 auto mismatch = [&] (const char *expected_str,
4631 const char *got)
4632 {
4633 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4634 "expected=\"%s\", got=\"%s\"\n"),
4635 file, line,
4636 (match_type == symbol_name_match_type::FULL
4637 ? "FULL" : "WILD"),
4638 name, string_or_null (expected_str), string_or_null (got));
4639 matched = false;
4640 };
4641
4642 auto expected_it = expected_list.begin ();
4643 auto expected_end = expected_list.end ();
4644
4645 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4646 NULL, ALL_DOMAIN,
4647 [&] (offset_type idx)
4648 {
4649 const char *matched_name = mock_index.symbol_name_at (idx);
4650 const char *expected_str
4651 = expected_it == expected_end ? NULL : *expected_it++;
4652
4653 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4654 mismatch (expected_str, matched_name);
4655 return true;
4656 });
4657
4658 const char *expected_str
4659 = expected_it == expected_end ? NULL : *expected_it++;
4660 if (expected_str != NULL)
4661 mismatch (expected_str, NULL);
4662
4663 return matched;
4664 }
4665
4666 /* The symbols added to the mock mapped_index for testing (in
4667 canonical form). */
4668 static const char *test_symbols[] = {
4669 "function",
4670 "std::bar",
4671 "std::zfunction",
4672 "std::zfunction2",
4673 "w1::w2",
4674 "ns::foo<char*>",
4675 "ns::foo<int>",
4676 "ns::foo<long>",
4677 "ns2::tmpl<int>::foo2",
4678 "(anonymous namespace)::A::B::C",
4679
4680 /* These are used to check that the increment-last-char in the
4681 matching algorithm for completion doesn't match "t1_fund" when
4682 completing "t1_func". */
4683 "t1_func",
4684 "t1_func1",
4685 "t1_fund",
4686 "t1_fund1",
4687
4688 /* A UTF-8 name with multi-byte sequences to make sure that
4689 cp-name-parser understands this as a single identifier ("função"
4690 is "function" in PT). */
4691 u8"u8função",
4692
4693 /* \377 (0xff) is Latin1 'ÿ'. */
4694 "yfunc\377",
4695
4696 /* \377 (0xff) is Latin1 'ÿ'. */
4697 "\377",
4698 "\377\377123",
4699
4700 /* A name with all sorts of complications. Starts with "z" to make
4701 it easier for the completion tests below. */
4702 #define Z_SYM_NAME \
4703 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4704 "::tuple<(anonymous namespace)::ui*, " \
4705 "std::default_delete<(anonymous namespace)::ui>, void>"
4706
4707 Z_SYM_NAME
4708 };
4709
4710 /* Returns true if the mapped_index_base::find_name_component_bounds
4711 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4712 in completion mode. */
4713
4714 static bool
4715 check_find_bounds_finds (mapped_index_base &index,
4716 const char *search_name,
4717 gdb::array_view<const char *> expected_syms)
4718 {
4719 lookup_name_info lookup_name (search_name,
4720 symbol_name_match_type::FULL, true);
4721
4722 auto bounds = index.find_name_components_bounds (lookup_name,
4723 language_cplus);
4724
4725 size_t distance = std::distance (bounds.first, bounds.second);
4726 if (distance != expected_syms.size ())
4727 return false;
4728
4729 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4730 {
4731 auto nc_elem = bounds.first + exp_elem;
4732 const char *qualified = index.symbol_name_at (nc_elem->idx);
4733 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4734 return false;
4735 }
4736
4737 return true;
4738 }
4739
4740 /* Test the lower-level mapped_index::find_name_component_bounds
4741 method. */
4742
4743 static void
4744 test_mapped_index_find_name_component_bounds ()
4745 {
4746 mock_mapped_index mock_index (test_symbols);
4747
4748 mock_index.build_name_components ();
4749
4750 /* Test the lower-level mapped_index::find_name_component_bounds
4751 method in completion mode. */
4752 {
4753 static const char *expected_syms[] = {
4754 "t1_func",
4755 "t1_func1",
4756 };
4757
4758 SELF_CHECK (check_find_bounds_finds (mock_index,
4759 "t1_func", expected_syms));
4760 }
4761
4762 /* Check that the increment-last-char in the name matching algorithm
4763 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4764 {
4765 static const char *expected_syms1[] = {
4766 "\377",
4767 "\377\377123",
4768 };
4769 SELF_CHECK (check_find_bounds_finds (mock_index,
4770 "\377", expected_syms1));
4771
4772 static const char *expected_syms2[] = {
4773 "\377\377123",
4774 };
4775 SELF_CHECK (check_find_bounds_finds (mock_index,
4776 "\377\377", expected_syms2));
4777 }
4778 }
4779
4780 /* Test dw2_expand_symtabs_matching_symbol. */
4781
4782 static void
4783 test_dw2_expand_symtabs_matching_symbol ()
4784 {
4785 mock_mapped_index mock_index (test_symbols);
4786
4787 /* We let all tests run until the end even if some fails, for debug
4788 convenience. */
4789 bool any_mismatch = false;
4790
4791 /* Create the expected symbols list (an initializer_list). Needed
4792 because lists have commas, and we need to pass them to CHECK,
4793 which is a macro. */
4794 #define EXPECT(...) { __VA_ARGS__ }
4795
4796 /* Wrapper for check_match that passes down the current
4797 __FILE__/__LINE__. */
4798 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4799 any_mismatch |= !check_match (__FILE__, __LINE__, \
4800 mock_index, \
4801 NAME, MATCH_TYPE, COMPLETION_MODE, \
4802 EXPECTED_LIST)
4803
4804 /* Identity checks. */
4805 for (const char *sym : test_symbols)
4806 {
4807 /* Should be able to match all existing symbols. */
4808 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4809 EXPECT (sym));
4810
4811 /* Should be able to match all existing symbols with
4812 parameters. */
4813 std::string with_params = std::string (sym) + "(int)";
4814 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4815 EXPECT (sym));
4816
4817 /* Should be able to match all existing symbols with
4818 parameters and qualifiers. */
4819 with_params = std::string (sym) + " ( int ) const";
4820 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4821 EXPECT (sym));
4822
4823 /* This should really find sym, but cp-name-parser.y doesn't
4824 know about lvalue/rvalue qualifiers yet. */
4825 with_params = std::string (sym) + " ( int ) &&";
4826 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4827 {});
4828 }
4829
4830 /* Check that the name matching algorithm for completion doesn't get
4831 confused with Latin1 'ÿ' / 0xff. */
4832 {
4833 static const char str[] = "\377";
4834 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4835 EXPECT ("\377", "\377\377123"));
4836 }
4837
4838 /* Check that the increment-last-char in the matching algorithm for
4839 completion doesn't match "t1_fund" when completing "t1_func". */
4840 {
4841 static const char str[] = "t1_func";
4842 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4843 EXPECT ("t1_func", "t1_func1"));
4844 }
4845
4846 /* Check that completion mode works at each prefix of the expected
4847 symbol name. */
4848 {
4849 static const char str[] = "function(int)";
4850 size_t len = strlen (str);
4851 std::string lookup;
4852
4853 for (size_t i = 1; i < len; i++)
4854 {
4855 lookup.assign (str, i);
4856 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4857 EXPECT ("function"));
4858 }
4859 }
4860
4861 /* While "w" is a prefix of both components, the match function
4862 should still only be called once. */
4863 {
4864 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4865 EXPECT ("w1::w2"));
4866 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4867 EXPECT ("w1::w2"));
4868 }
4869
4870 /* Same, with a "complicated" symbol. */
4871 {
4872 static const char str[] = Z_SYM_NAME;
4873 size_t len = strlen (str);
4874 std::string lookup;
4875
4876 for (size_t i = 1; i < len; i++)
4877 {
4878 lookup.assign (str, i);
4879 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4880 EXPECT (Z_SYM_NAME));
4881 }
4882 }
4883
4884 /* In FULL mode, an incomplete symbol doesn't match. */
4885 {
4886 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4887 {});
4888 }
4889
4890 /* A complete symbol with parameters matches any overload, since the
4891 index has no overload info. */
4892 {
4893 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4894 EXPECT ("std::zfunction", "std::zfunction2"));
4895 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4896 EXPECT ("std::zfunction", "std::zfunction2"));
4897 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4898 EXPECT ("std::zfunction", "std::zfunction2"));
4899 }
4900
4901 /* Check that whitespace is ignored appropriately. A symbol with a
4902 template argument list. */
4903 {
4904 static const char expected[] = "ns::foo<int>";
4905 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4906 EXPECT (expected));
4907 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4908 EXPECT (expected));
4909 }
4910
4911 /* Check that whitespace is ignored appropriately. A symbol with a
4912 template argument list that includes a pointer. */
4913 {
4914 static const char expected[] = "ns::foo<char*>";
4915 /* Try both completion and non-completion modes. */
4916 static const bool completion_mode[2] = {false, true};
4917 for (size_t i = 0; i < 2; i++)
4918 {
4919 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4920 completion_mode[i], EXPECT (expected));
4921 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4922 completion_mode[i], EXPECT (expected));
4923
4924 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4925 completion_mode[i], EXPECT (expected));
4926 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4927 completion_mode[i], EXPECT (expected));
4928 }
4929 }
4930
4931 {
4932 /* Check method qualifiers are ignored. */
4933 static const char expected[] = "ns::foo<char*>";
4934 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4935 symbol_name_match_type::FULL, true, EXPECT (expected));
4936 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4937 symbol_name_match_type::FULL, true, EXPECT (expected));
4938 CHECK_MATCH ("foo < char * > ( int ) const",
4939 symbol_name_match_type::WILD, true, EXPECT (expected));
4940 CHECK_MATCH ("foo < char * > ( int ) &&",
4941 symbol_name_match_type::WILD, true, EXPECT (expected));
4942 }
4943
4944 /* Test lookup names that don't match anything. */
4945 {
4946 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4947 {});
4948
4949 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4950 {});
4951 }
4952
4953 /* Some wild matching tests, exercising "(anonymous namespace)",
4954 which should not be confused with a parameter list. */
4955 {
4956 static const char *syms[] = {
4957 "A::B::C",
4958 "B::C",
4959 "C",
4960 "A :: B :: C ( int )",
4961 "B :: C ( int )",
4962 "C ( int )",
4963 };
4964
4965 for (const char *s : syms)
4966 {
4967 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4968 EXPECT ("(anonymous namespace)::A::B::C"));
4969 }
4970 }
4971
4972 {
4973 static const char expected[] = "ns2::tmpl<int>::foo2";
4974 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4975 EXPECT (expected));
4976 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4977 EXPECT (expected));
4978 }
4979
4980 SELF_CHECK (!any_mismatch);
4981
4982 #undef EXPECT
4983 #undef CHECK_MATCH
4984 }
4985
4986 static void
4987 run_test ()
4988 {
4989 test_mapped_index_find_name_component_bounds ();
4990 test_dw2_expand_symtabs_matching_symbol ();
4991 }
4992
4993 }} // namespace selftests::dw2_expand_symtabs_matching
4994
4995 #endif /* GDB_SELF_TEST */
4996
4997 /* If FILE_MATCHER is NULL or if PER_CU has
4998 dwarf2_per_cu_quick_data::MARK set (see
4999 dw_expand_symtabs_matching_file_matcher), expand the CU and call
5000 EXPANSION_NOTIFY on it. */
5001
5002 static void
5003 dw2_expand_symtabs_matching_one
5004 (struct dwarf2_per_cu_data *per_cu,
5005 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5006 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
5007 {
5008 if (file_matcher == NULL || per_cu->v.quick->mark)
5009 {
5010 bool symtab_was_null
5011 = (per_cu->v.quick->compunit_symtab == NULL);
5012
5013 dw2_instantiate_symtab (per_cu, false);
5014
5015 if (expansion_notify != NULL
5016 && symtab_was_null
5017 && per_cu->v.quick->compunit_symtab != NULL)
5018 expansion_notify (per_cu->v.quick->compunit_symtab);
5019 }
5020 }
5021
5022 /* Helper for dw2_expand_matching symtabs. Called on each symbol
5023 matched, to expand corresponding CUs that were marked. IDX is the
5024 index of the symbol name that matched. */
5025
5026 static void
5027 dw2_expand_marked_cus
5028 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
5029 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5030 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5031 search_domain kind)
5032 {
5033 offset_type *vec, vec_len, vec_idx;
5034 bool global_seen = false;
5035 mapped_index &index = *dwarf2_per_objfile->index_table;
5036
5037 vec = (offset_type *) (index.constant_pool
5038 + MAYBE_SWAP (index.symbol_table[idx].vec));
5039 vec_len = MAYBE_SWAP (vec[0]);
5040 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
5041 {
5042 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
5043 /* This value is only valid for index versions >= 7. */
5044 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
5045 gdb_index_symbol_kind symbol_kind =
5046 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
5047 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
5048 /* Only check the symbol attributes if they're present.
5049 Indices prior to version 7 don't record them,
5050 and indices >= 7 may elide them for certain symbols
5051 (gold does this). */
5052 int attrs_valid =
5053 (index.version >= 7
5054 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
5055
5056 /* Work around gold/15646. */
5057 if (attrs_valid)
5058 {
5059 if (!is_static && global_seen)
5060 continue;
5061 if (!is_static)
5062 global_seen = true;
5063 }
5064
5065 /* Only check the symbol's kind if it has one. */
5066 if (attrs_valid)
5067 {
5068 switch (kind)
5069 {
5070 case VARIABLES_DOMAIN:
5071 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
5072 continue;
5073 break;
5074 case FUNCTIONS_DOMAIN:
5075 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
5076 continue;
5077 break;
5078 case TYPES_DOMAIN:
5079 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
5080 continue;
5081 break;
5082 case MODULES_DOMAIN:
5083 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
5084 continue;
5085 break;
5086 default:
5087 break;
5088 }
5089 }
5090
5091 /* Don't crash on bad data. */
5092 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
5093 + dwarf2_per_objfile->all_type_units.size ()))
5094 {
5095 complaint (_(".gdb_index entry has bad CU index"
5096 " [in module %s]"),
5097 objfile_name (dwarf2_per_objfile->objfile));
5098 continue;
5099 }
5100
5101 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
5102 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5103 expansion_notify);
5104 }
5105 }
5106
5107 /* If FILE_MATCHER is non-NULL, set all the
5108 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
5109 that match FILE_MATCHER. */
5110
5111 static void
5112 dw_expand_symtabs_matching_file_matcher
5113 (struct dwarf2_per_objfile *dwarf2_per_objfile,
5114 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
5115 {
5116 if (file_matcher == NULL)
5117 return;
5118
5119 objfile *const objfile = dwarf2_per_objfile->objfile;
5120
5121 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
5122 htab_eq_pointer,
5123 NULL, xcalloc, xfree));
5124 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
5125 htab_eq_pointer,
5126 NULL, xcalloc, xfree));
5127
5128 /* The rule is CUs specify all the files, including those used by
5129 any TU, so there's no need to scan TUs here. */
5130
5131 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5132 {
5133 QUIT;
5134
5135 per_cu->v.quick->mark = 0;
5136
5137 /* We only need to look at symtabs not already expanded. */
5138 if (per_cu->v.quick->compunit_symtab)
5139 continue;
5140
5141 quick_file_names *file_data = dw2_get_file_names (per_cu);
5142 if (file_data == NULL)
5143 continue;
5144
5145 if (htab_find (visited_not_found.get (), file_data) != NULL)
5146 continue;
5147 else if (htab_find (visited_found.get (), file_data) != NULL)
5148 {
5149 per_cu->v.quick->mark = 1;
5150 continue;
5151 }
5152
5153 for (int j = 0; j < file_data->num_file_names; ++j)
5154 {
5155 const char *this_real_name;
5156
5157 if (file_matcher (file_data->file_names[j], false))
5158 {
5159 per_cu->v.quick->mark = 1;
5160 break;
5161 }
5162
5163 /* Before we invoke realpath, which can get expensive when many
5164 files are involved, do a quick comparison of the basenames. */
5165 if (!basenames_may_differ
5166 && !file_matcher (lbasename (file_data->file_names[j]),
5167 true))
5168 continue;
5169
5170 this_real_name = dw2_get_real_path (objfile, file_data, j);
5171 if (file_matcher (this_real_name, false))
5172 {
5173 per_cu->v.quick->mark = 1;
5174 break;
5175 }
5176 }
5177
5178 void **slot = htab_find_slot (per_cu->v.quick->mark
5179 ? visited_found.get ()
5180 : visited_not_found.get (),
5181 file_data, INSERT);
5182 *slot = file_data;
5183 }
5184 }
5185
5186 static void
5187 dw2_expand_symtabs_matching
5188 (struct objfile *objfile,
5189 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5190 const lookup_name_info &lookup_name,
5191 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5192 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5193 enum search_domain kind)
5194 {
5195 struct dwarf2_per_objfile *dwarf2_per_objfile
5196 = get_dwarf2_per_objfile (objfile);
5197
5198 /* index_table is NULL if OBJF_READNOW. */
5199 if (!dwarf2_per_objfile->index_table)
5200 return;
5201
5202 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
5203
5204 mapped_index &index = *dwarf2_per_objfile->index_table;
5205
5206 dw2_expand_symtabs_matching_symbol (index, lookup_name,
5207 symbol_matcher,
5208 kind, [&] (offset_type idx)
5209 {
5210 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
5211 expansion_notify, kind);
5212 return true;
5213 });
5214 }
5215
5216 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
5217 symtab. */
5218
5219 static struct compunit_symtab *
5220 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
5221 CORE_ADDR pc)
5222 {
5223 int i;
5224
5225 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
5226 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
5227 return cust;
5228
5229 if (cust->includes == NULL)
5230 return NULL;
5231
5232 for (i = 0; cust->includes[i]; ++i)
5233 {
5234 struct compunit_symtab *s = cust->includes[i];
5235
5236 s = recursively_find_pc_sect_compunit_symtab (s, pc);
5237 if (s != NULL)
5238 return s;
5239 }
5240
5241 return NULL;
5242 }
5243
5244 static struct compunit_symtab *
5245 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
5246 struct bound_minimal_symbol msymbol,
5247 CORE_ADDR pc,
5248 struct obj_section *section,
5249 int warn_if_readin)
5250 {
5251 struct dwarf2_per_cu_data *data;
5252 struct compunit_symtab *result;
5253
5254 if (!objfile->partial_symtabs->psymtabs_addrmap)
5255 return NULL;
5256
5257 CORE_ADDR baseaddr = objfile->section_offsets[SECT_OFF_TEXT (objfile)];
5258 data = (struct dwarf2_per_cu_data *) addrmap_find
5259 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
5260 if (!data)
5261 return NULL;
5262
5263 if (warn_if_readin && data->v.quick->compunit_symtab)
5264 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
5265 paddress (get_objfile_arch (objfile), pc));
5266
5267 result
5268 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data,
5269 false),
5270 pc);
5271 gdb_assert (result != NULL);
5272 return result;
5273 }
5274
5275 static void
5276 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
5277 void *data, int need_fullname)
5278 {
5279 struct dwarf2_per_objfile *dwarf2_per_objfile
5280 = get_dwarf2_per_objfile (objfile);
5281
5282 if (!dwarf2_per_objfile->filenames_cache)
5283 {
5284 dwarf2_per_objfile->filenames_cache.emplace ();
5285
5286 htab_up visited (htab_create_alloc (10,
5287 htab_hash_pointer, htab_eq_pointer,
5288 NULL, xcalloc, xfree));
5289
5290 /* The rule is CUs specify all the files, including those used
5291 by any TU, so there's no need to scan TUs here. We can
5292 ignore file names coming from already-expanded CUs. */
5293
5294 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5295 {
5296 if (per_cu->v.quick->compunit_symtab)
5297 {
5298 void **slot = htab_find_slot (visited.get (),
5299 per_cu->v.quick->file_names,
5300 INSERT);
5301
5302 *slot = per_cu->v.quick->file_names;
5303 }
5304 }
5305
5306 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5307 {
5308 /* We only need to look at symtabs not already expanded. */
5309 if (per_cu->v.quick->compunit_symtab)
5310 continue;
5311
5312 quick_file_names *file_data = dw2_get_file_names (per_cu);
5313 if (file_data == NULL)
5314 continue;
5315
5316 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
5317 if (*slot)
5318 {
5319 /* Already visited. */
5320 continue;
5321 }
5322 *slot = file_data;
5323
5324 for (int j = 0; j < file_data->num_file_names; ++j)
5325 {
5326 const char *filename = file_data->file_names[j];
5327 dwarf2_per_objfile->filenames_cache->seen (filename);
5328 }
5329 }
5330 }
5331
5332 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
5333 {
5334 gdb::unique_xmalloc_ptr<char> this_real_name;
5335
5336 if (need_fullname)
5337 this_real_name = gdb_realpath (filename);
5338 (*fun) (filename, this_real_name.get (), data);
5339 });
5340 }
5341
5342 static int
5343 dw2_has_symbols (struct objfile *objfile)
5344 {
5345 return 1;
5346 }
5347
5348 const struct quick_symbol_functions dwarf2_gdb_index_functions =
5349 {
5350 dw2_has_symbols,
5351 dw2_find_last_source_symtab,
5352 dw2_forget_cached_source_info,
5353 dw2_map_symtabs_matching_filename,
5354 dw2_lookup_symbol,
5355 dw2_print_stats,
5356 dw2_dump,
5357 dw2_expand_symtabs_for_function,
5358 dw2_expand_all_symtabs,
5359 dw2_expand_symtabs_with_fullname,
5360 dw2_map_matching_symbols,
5361 dw2_expand_symtabs_matching,
5362 dw2_find_pc_sect_compunit_symtab,
5363 NULL,
5364 dw2_map_symbol_filenames
5365 };
5366
5367 /* DWARF-5 debug_names reader. */
5368
5369 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5370 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5371
5372 /* A helper function that reads the .debug_names section in SECTION
5373 and fills in MAP. FILENAME is the name of the file containing the
5374 section; it is used for error reporting.
5375
5376 Returns true if all went well, false otherwise. */
5377
5378 static bool
5379 read_debug_names_from_section (struct objfile *objfile,
5380 const char *filename,
5381 struct dwarf2_section_info *section,
5382 mapped_debug_names &map)
5383 {
5384 if (dwarf2_section_empty_p (section))
5385 return false;
5386
5387 /* Older elfutils strip versions could keep the section in the main
5388 executable while splitting it for the separate debug info file. */
5389 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
5390 return false;
5391
5392 dwarf2_read_section (objfile, section);
5393
5394 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
5395
5396 const gdb_byte *addr = section->buffer;
5397
5398 bfd *const abfd = get_section_bfd_owner (section);
5399
5400 unsigned int bytes_read;
5401 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5402 addr += bytes_read;
5403
5404 map.dwarf5_is_dwarf64 = bytes_read != 4;
5405 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5406 if (bytes_read + length != section->size)
5407 {
5408 /* There may be multiple per-CU indices. */
5409 warning (_("Section .debug_names in %s length %s does not match "
5410 "section length %s, ignoring .debug_names."),
5411 filename, plongest (bytes_read + length),
5412 pulongest (section->size));
5413 return false;
5414 }
5415
5416 /* The version number. */
5417 uint16_t version = read_2_bytes (abfd, addr);
5418 addr += 2;
5419 if (version != 5)
5420 {
5421 warning (_("Section .debug_names in %s has unsupported version %d, "
5422 "ignoring .debug_names."),
5423 filename, version);
5424 return false;
5425 }
5426
5427 /* Padding. */
5428 uint16_t padding = read_2_bytes (abfd, addr);
5429 addr += 2;
5430 if (padding != 0)
5431 {
5432 warning (_("Section .debug_names in %s has unsupported padding %d, "
5433 "ignoring .debug_names."),
5434 filename, padding);
5435 return false;
5436 }
5437
5438 /* comp_unit_count - The number of CUs in the CU list. */
5439 map.cu_count = read_4_bytes (abfd, addr);
5440 addr += 4;
5441
5442 /* local_type_unit_count - The number of TUs in the local TU
5443 list. */
5444 map.tu_count = read_4_bytes (abfd, addr);
5445 addr += 4;
5446
5447 /* foreign_type_unit_count - The number of TUs in the foreign TU
5448 list. */
5449 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5450 addr += 4;
5451 if (foreign_tu_count != 0)
5452 {
5453 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5454 "ignoring .debug_names."),
5455 filename, static_cast<unsigned long> (foreign_tu_count));
5456 return false;
5457 }
5458
5459 /* bucket_count - The number of hash buckets in the hash lookup
5460 table. */
5461 map.bucket_count = read_4_bytes (abfd, addr);
5462 addr += 4;
5463
5464 /* name_count - The number of unique names in the index. */
5465 map.name_count = read_4_bytes (abfd, addr);
5466 addr += 4;
5467
5468 /* abbrev_table_size - The size in bytes of the abbreviations
5469 table. */
5470 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5471 addr += 4;
5472
5473 /* augmentation_string_size - The size in bytes of the augmentation
5474 string. This value is rounded up to a multiple of 4. */
5475 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5476 addr += 4;
5477 map.augmentation_is_gdb = ((augmentation_string_size
5478 == sizeof (dwarf5_augmentation))
5479 && memcmp (addr, dwarf5_augmentation,
5480 sizeof (dwarf5_augmentation)) == 0);
5481 augmentation_string_size += (-augmentation_string_size) & 3;
5482 addr += augmentation_string_size;
5483
5484 /* List of CUs */
5485 map.cu_table_reordered = addr;
5486 addr += map.cu_count * map.offset_size;
5487
5488 /* List of Local TUs */
5489 map.tu_table_reordered = addr;
5490 addr += map.tu_count * map.offset_size;
5491
5492 /* Hash Lookup Table */
5493 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5494 addr += map.bucket_count * 4;
5495 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5496 addr += map.name_count * 4;
5497
5498 /* Name Table */
5499 map.name_table_string_offs_reordered = addr;
5500 addr += map.name_count * map.offset_size;
5501 map.name_table_entry_offs_reordered = addr;
5502 addr += map.name_count * map.offset_size;
5503
5504 const gdb_byte *abbrev_table_start = addr;
5505 for (;;)
5506 {
5507 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5508 addr += bytes_read;
5509 if (index_num == 0)
5510 break;
5511
5512 const auto insertpair
5513 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5514 if (!insertpair.second)
5515 {
5516 warning (_("Section .debug_names in %s has duplicate index %s, "
5517 "ignoring .debug_names."),
5518 filename, pulongest (index_num));
5519 return false;
5520 }
5521 mapped_debug_names::index_val &indexval = insertpair.first->second;
5522 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5523 addr += bytes_read;
5524
5525 for (;;)
5526 {
5527 mapped_debug_names::index_val::attr attr;
5528 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5529 addr += bytes_read;
5530 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5531 addr += bytes_read;
5532 if (attr.form == DW_FORM_implicit_const)
5533 {
5534 attr.implicit_const = read_signed_leb128 (abfd, addr,
5535 &bytes_read);
5536 addr += bytes_read;
5537 }
5538 if (attr.dw_idx == 0 && attr.form == 0)
5539 break;
5540 indexval.attr_vec.push_back (std::move (attr));
5541 }
5542 }
5543 if (addr != abbrev_table_start + abbrev_table_size)
5544 {
5545 warning (_("Section .debug_names in %s has abbreviation_table "
5546 "of size %s vs. written as %u, ignoring .debug_names."),
5547 filename, plongest (addr - abbrev_table_start),
5548 abbrev_table_size);
5549 return false;
5550 }
5551 map.entry_pool = addr;
5552
5553 return true;
5554 }
5555
5556 /* A helper for create_cus_from_debug_names that handles the MAP's CU
5557 list. */
5558
5559 static void
5560 create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
5561 const mapped_debug_names &map,
5562 dwarf2_section_info &section,
5563 bool is_dwz)
5564 {
5565 sect_offset sect_off_prev;
5566 for (uint32_t i = 0; i <= map.cu_count; ++i)
5567 {
5568 sect_offset sect_off_next;
5569 if (i < map.cu_count)
5570 {
5571 sect_off_next
5572 = (sect_offset) (extract_unsigned_integer
5573 (map.cu_table_reordered + i * map.offset_size,
5574 map.offset_size,
5575 map.dwarf5_byte_order));
5576 }
5577 else
5578 sect_off_next = (sect_offset) section.size;
5579 if (i >= 1)
5580 {
5581 const ULONGEST length = sect_off_next - sect_off_prev;
5582 dwarf2_per_cu_data *per_cu
5583 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
5584 sect_off_prev, length);
5585 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
5586 }
5587 sect_off_prev = sect_off_next;
5588 }
5589 }
5590
5591 /* Read the CU list from the mapped index, and use it to create all
5592 the CU objects for this dwarf2_per_objfile. */
5593
5594 static void
5595 create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
5596 const mapped_debug_names &map,
5597 const mapped_debug_names &dwz_map)
5598 {
5599 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
5600 dwarf2_per_objfile->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
5601
5602 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5603 dwarf2_per_objfile->info,
5604 false /* is_dwz */);
5605
5606 if (dwz_map.cu_count == 0)
5607 return;
5608
5609 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5610 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
5611 true /* is_dwz */);
5612 }
5613
5614 /* Read .debug_names. If everything went ok, initialize the "quick"
5615 elements of all the CUs and return true. Otherwise, return false. */
5616
5617 static bool
5618 dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
5619 {
5620 std::unique_ptr<mapped_debug_names> map
5621 (new mapped_debug_names (dwarf2_per_objfile));
5622 mapped_debug_names dwz_map (dwarf2_per_objfile);
5623 struct objfile *objfile = dwarf2_per_objfile->objfile;
5624
5625 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5626 &dwarf2_per_objfile->debug_names,
5627 *map))
5628 return false;
5629
5630 /* Don't use the index if it's empty. */
5631 if (map->name_count == 0)
5632 return false;
5633
5634 /* If there is a .dwz file, read it so we can get its CU list as
5635 well. */
5636 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5637 if (dwz != NULL)
5638 {
5639 if (!read_debug_names_from_section (objfile,
5640 bfd_get_filename (dwz->dwz_bfd.get ()),
5641 &dwz->debug_names, dwz_map))
5642 {
5643 warning (_("could not read '.debug_names' section from %s; skipping"),
5644 bfd_get_filename (dwz->dwz_bfd.get ()));
5645 return false;
5646 }
5647 }
5648
5649 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
5650
5651 if (map->tu_count != 0)
5652 {
5653 /* We can only handle a single .debug_types when we have an
5654 index. */
5655 if (dwarf2_per_objfile->types.size () != 1)
5656 return false;
5657
5658 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
5659
5660 create_signatured_type_table_from_debug_names
5661 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->abbrev);
5662 }
5663
5664 create_addrmap_from_aranges (dwarf2_per_objfile,
5665 &dwarf2_per_objfile->debug_aranges);
5666
5667 dwarf2_per_objfile->debug_names_table = std::move (map);
5668 dwarf2_per_objfile->using_index = 1;
5669 dwarf2_per_objfile->quick_file_names_table =
5670 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
5671
5672 return true;
5673 }
5674
5675 /* Type used to manage iterating over all CUs looking for a symbol for
5676 .debug_names. */
5677
5678 class dw2_debug_names_iterator
5679 {
5680 public:
5681 dw2_debug_names_iterator (const mapped_debug_names &map,
5682 gdb::optional<block_enum> block_index,
5683 domain_enum domain,
5684 const char *name)
5685 : m_map (map), m_block_index (block_index), m_domain (domain),
5686 m_addr (find_vec_in_debug_names (map, name))
5687 {}
5688
5689 dw2_debug_names_iterator (const mapped_debug_names &map,
5690 search_domain search, uint32_t namei)
5691 : m_map (map),
5692 m_search (search),
5693 m_addr (find_vec_in_debug_names (map, namei))
5694 {}
5695
5696 dw2_debug_names_iterator (const mapped_debug_names &map,
5697 block_enum block_index, domain_enum domain,
5698 uint32_t namei)
5699 : m_map (map), m_block_index (block_index), m_domain (domain),
5700 m_addr (find_vec_in_debug_names (map, namei))
5701 {}
5702
5703 /* Return the next matching CU or NULL if there are no more. */
5704 dwarf2_per_cu_data *next ();
5705
5706 private:
5707 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5708 const char *name);
5709 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5710 uint32_t namei);
5711
5712 /* The internalized form of .debug_names. */
5713 const mapped_debug_names &m_map;
5714
5715 /* If set, only look for symbols that match that block. Valid values are
5716 GLOBAL_BLOCK and STATIC_BLOCK. */
5717 const gdb::optional<block_enum> m_block_index;
5718
5719 /* The kind of symbol we're looking for. */
5720 const domain_enum m_domain = UNDEF_DOMAIN;
5721 const search_domain m_search = ALL_DOMAIN;
5722
5723 /* The list of CUs from the index entry of the symbol, or NULL if
5724 not found. */
5725 const gdb_byte *m_addr;
5726 };
5727
5728 const char *
5729 mapped_debug_names::namei_to_name (uint32_t namei) const
5730 {
5731 const ULONGEST namei_string_offs
5732 = extract_unsigned_integer ((name_table_string_offs_reordered
5733 + namei * offset_size),
5734 offset_size,
5735 dwarf5_byte_order);
5736 return read_indirect_string_at_offset
5737 (dwarf2_per_objfile, dwarf2_per_objfile->objfile->obfd, namei_string_offs);
5738 }
5739
5740 /* Find a slot in .debug_names for the object named NAME. If NAME is
5741 found, return pointer to its pool data. If NAME cannot be found,
5742 return NULL. */
5743
5744 const gdb_byte *
5745 dw2_debug_names_iterator::find_vec_in_debug_names
5746 (const mapped_debug_names &map, const char *name)
5747 {
5748 int (*cmp) (const char *, const char *);
5749
5750 gdb::unique_xmalloc_ptr<char> without_params;
5751 if (current_language->la_language == language_cplus
5752 || current_language->la_language == language_fortran
5753 || current_language->la_language == language_d)
5754 {
5755 /* NAME is already canonical. Drop any qualifiers as
5756 .debug_names does not contain any. */
5757
5758 if (strchr (name, '(') != NULL)
5759 {
5760 without_params = cp_remove_params (name);
5761 if (without_params != NULL)
5762 name = without_params.get ();
5763 }
5764 }
5765
5766 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5767
5768 const uint32_t full_hash = dwarf5_djb_hash (name);
5769 uint32_t namei
5770 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5771 (map.bucket_table_reordered
5772 + (full_hash % map.bucket_count)), 4,
5773 map.dwarf5_byte_order);
5774 if (namei == 0)
5775 return NULL;
5776 --namei;
5777 if (namei >= map.name_count)
5778 {
5779 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5780 "[in module %s]"),
5781 namei, map.name_count,
5782 objfile_name (map.dwarf2_per_objfile->objfile));
5783 return NULL;
5784 }
5785
5786 for (;;)
5787 {
5788 const uint32_t namei_full_hash
5789 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5790 (map.hash_table_reordered + namei), 4,
5791 map.dwarf5_byte_order);
5792 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5793 return NULL;
5794
5795 if (full_hash == namei_full_hash)
5796 {
5797 const char *const namei_string = map.namei_to_name (namei);
5798
5799 #if 0 /* An expensive sanity check. */
5800 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5801 {
5802 complaint (_("Wrong .debug_names hash for string at index %u "
5803 "[in module %s]"),
5804 namei, objfile_name (dwarf2_per_objfile->objfile));
5805 return NULL;
5806 }
5807 #endif
5808
5809 if (cmp (namei_string, name) == 0)
5810 {
5811 const ULONGEST namei_entry_offs
5812 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5813 + namei * map.offset_size),
5814 map.offset_size, map.dwarf5_byte_order);
5815 return map.entry_pool + namei_entry_offs;
5816 }
5817 }
5818
5819 ++namei;
5820 if (namei >= map.name_count)
5821 return NULL;
5822 }
5823 }
5824
5825 const gdb_byte *
5826 dw2_debug_names_iterator::find_vec_in_debug_names
5827 (const mapped_debug_names &map, uint32_t namei)
5828 {
5829 if (namei >= map.name_count)
5830 {
5831 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5832 "[in module %s]"),
5833 namei, map.name_count,
5834 objfile_name (map.dwarf2_per_objfile->objfile));
5835 return NULL;
5836 }
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 /* See dw2_debug_names_iterator. */
5846
5847 dwarf2_per_cu_data *
5848 dw2_debug_names_iterator::next ()
5849 {
5850 if (m_addr == NULL)
5851 return NULL;
5852
5853 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5854 struct objfile *objfile = dwarf2_per_objfile->objfile;
5855 bfd *const abfd = objfile->obfd;
5856
5857 again:
5858
5859 unsigned int bytes_read;
5860 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5861 m_addr += bytes_read;
5862 if (abbrev == 0)
5863 return NULL;
5864
5865 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5866 if (indexval_it == m_map.abbrev_map.cend ())
5867 {
5868 complaint (_("Wrong .debug_names undefined abbrev code %s "
5869 "[in module %s]"),
5870 pulongest (abbrev), objfile_name (objfile));
5871 return NULL;
5872 }
5873 const mapped_debug_names::index_val &indexval = indexval_it->second;
5874 enum class symbol_linkage {
5875 unknown,
5876 static_,
5877 extern_,
5878 } symbol_linkage_ = symbol_linkage::unknown;
5879 dwarf2_per_cu_data *per_cu = NULL;
5880 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5881 {
5882 ULONGEST ull;
5883 switch (attr.form)
5884 {
5885 case DW_FORM_implicit_const:
5886 ull = attr.implicit_const;
5887 break;
5888 case DW_FORM_flag_present:
5889 ull = 1;
5890 break;
5891 case DW_FORM_udata:
5892 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5893 m_addr += bytes_read;
5894 break;
5895 default:
5896 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5897 dwarf_form_name (attr.form),
5898 objfile_name (objfile));
5899 return NULL;
5900 }
5901 switch (attr.dw_idx)
5902 {
5903 case DW_IDX_compile_unit:
5904 /* Don't crash on bad data. */
5905 if (ull >= dwarf2_per_objfile->all_comp_units.size ())
5906 {
5907 complaint (_(".debug_names entry has bad CU index %s"
5908 " [in module %s]"),
5909 pulongest (ull),
5910 objfile_name (dwarf2_per_objfile->objfile));
5911 continue;
5912 }
5913 per_cu = dwarf2_per_objfile->get_cutu (ull);
5914 break;
5915 case DW_IDX_type_unit:
5916 /* Don't crash on bad data. */
5917 if (ull >= dwarf2_per_objfile->all_type_units.size ())
5918 {
5919 complaint (_(".debug_names entry has bad TU index %s"
5920 " [in module %s]"),
5921 pulongest (ull),
5922 objfile_name (dwarf2_per_objfile->objfile));
5923 continue;
5924 }
5925 per_cu = &dwarf2_per_objfile->get_tu (ull)->per_cu;
5926 break;
5927 case DW_IDX_GNU_internal:
5928 if (!m_map.augmentation_is_gdb)
5929 break;
5930 symbol_linkage_ = symbol_linkage::static_;
5931 break;
5932 case DW_IDX_GNU_external:
5933 if (!m_map.augmentation_is_gdb)
5934 break;
5935 symbol_linkage_ = symbol_linkage::extern_;
5936 break;
5937 }
5938 }
5939
5940 /* Skip if already read in. */
5941 if (per_cu->v.quick->compunit_symtab)
5942 goto again;
5943
5944 /* Check static vs global. */
5945 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
5946 {
5947 const bool want_static = *m_block_index == STATIC_BLOCK;
5948 const bool symbol_is_static =
5949 symbol_linkage_ == symbol_linkage::static_;
5950 if (want_static != symbol_is_static)
5951 goto again;
5952 }
5953
5954 /* Match dw2_symtab_iter_next, symbol_kind
5955 and debug_names::psymbol_tag. */
5956 switch (m_domain)
5957 {
5958 case VAR_DOMAIN:
5959 switch (indexval.dwarf_tag)
5960 {
5961 case DW_TAG_variable:
5962 case DW_TAG_subprogram:
5963 /* Some types are also in VAR_DOMAIN. */
5964 case DW_TAG_typedef:
5965 case DW_TAG_structure_type:
5966 break;
5967 default:
5968 goto again;
5969 }
5970 break;
5971 case STRUCT_DOMAIN:
5972 switch (indexval.dwarf_tag)
5973 {
5974 case DW_TAG_typedef:
5975 case DW_TAG_structure_type:
5976 break;
5977 default:
5978 goto again;
5979 }
5980 break;
5981 case LABEL_DOMAIN:
5982 switch (indexval.dwarf_tag)
5983 {
5984 case 0:
5985 case DW_TAG_variable:
5986 break;
5987 default:
5988 goto again;
5989 }
5990 break;
5991 case MODULE_DOMAIN:
5992 switch (indexval.dwarf_tag)
5993 {
5994 case DW_TAG_module:
5995 break;
5996 default:
5997 goto again;
5998 }
5999 break;
6000 default:
6001 break;
6002 }
6003
6004 /* Match dw2_expand_symtabs_matching, symbol_kind and
6005 debug_names::psymbol_tag. */
6006 switch (m_search)
6007 {
6008 case VARIABLES_DOMAIN:
6009 switch (indexval.dwarf_tag)
6010 {
6011 case DW_TAG_variable:
6012 break;
6013 default:
6014 goto again;
6015 }
6016 break;
6017 case FUNCTIONS_DOMAIN:
6018 switch (indexval.dwarf_tag)
6019 {
6020 case DW_TAG_subprogram:
6021 break;
6022 default:
6023 goto again;
6024 }
6025 break;
6026 case TYPES_DOMAIN:
6027 switch (indexval.dwarf_tag)
6028 {
6029 case DW_TAG_typedef:
6030 case DW_TAG_structure_type:
6031 break;
6032 default:
6033 goto again;
6034 }
6035 break;
6036 case MODULES_DOMAIN:
6037 switch (indexval.dwarf_tag)
6038 {
6039 case DW_TAG_module:
6040 break;
6041 default:
6042 goto again;
6043 }
6044 default:
6045 break;
6046 }
6047
6048 return per_cu;
6049 }
6050
6051 static struct compunit_symtab *
6052 dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
6053 const char *name, domain_enum domain)
6054 {
6055 struct dwarf2_per_objfile *dwarf2_per_objfile
6056 = get_dwarf2_per_objfile (objfile);
6057
6058 const auto &mapp = dwarf2_per_objfile->debug_names_table;
6059 if (!mapp)
6060 {
6061 /* index is NULL if OBJF_READNOW. */
6062 return NULL;
6063 }
6064 const auto &map = *mapp;
6065
6066 dw2_debug_names_iterator iter (map, block_index, domain, name);
6067
6068 struct compunit_symtab *stab_best = NULL;
6069 struct dwarf2_per_cu_data *per_cu;
6070 while ((per_cu = iter.next ()) != NULL)
6071 {
6072 struct symbol *sym, *with_opaque = NULL;
6073 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
6074 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
6075 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
6076
6077 sym = block_find_symbol (block, name, domain,
6078 block_find_non_opaque_type_preferred,
6079 &with_opaque);
6080
6081 /* Some caution must be observed with overloaded functions and
6082 methods, since the index will not contain any overload
6083 information (but NAME might contain it). */
6084
6085 if (sym != NULL
6086 && strcmp_iw (sym->search_name (), name) == 0)
6087 return stab;
6088 if (with_opaque != NULL
6089 && strcmp_iw (with_opaque->search_name (), name) == 0)
6090 stab_best = stab;
6091
6092 /* Keep looking through other CUs. */
6093 }
6094
6095 return stab_best;
6096 }
6097
6098 /* This dumps minimal information about .debug_names. It is called
6099 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
6100 uses this to verify that .debug_names has been loaded. */
6101
6102 static void
6103 dw2_debug_names_dump (struct objfile *objfile)
6104 {
6105 struct dwarf2_per_objfile *dwarf2_per_objfile
6106 = get_dwarf2_per_objfile (objfile);
6107
6108 gdb_assert (dwarf2_per_objfile->using_index);
6109 printf_filtered (".debug_names:");
6110 if (dwarf2_per_objfile->debug_names_table)
6111 printf_filtered (" exists\n");
6112 else
6113 printf_filtered (" faked for \"readnow\"\n");
6114 printf_filtered ("\n");
6115 }
6116
6117 static void
6118 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
6119 const char *func_name)
6120 {
6121 struct dwarf2_per_objfile *dwarf2_per_objfile
6122 = get_dwarf2_per_objfile (objfile);
6123
6124 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
6125 if (dwarf2_per_objfile->debug_names_table)
6126 {
6127 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6128
6129 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
6130
6131 struct dwarf2_per_cu_data *per_cu;
6132 while ((per_cu = iter.next ()) != NULL)
6133 dw2_instantiate_symtab (per_cu, false);
6134 }
6135 }
6136
6137 static void
6138 dw2_debug_names_map_matching_symbols
6139 (struct objfile *objfile,
6140 const lookup_name_info &name, domain_enum domain,
6141 int global,
6142 gdb::function_view<symbol_found_callback_ftype> callback,
6143 symbol_compare_ftype *ordered_compare)
6144 {
6145 struct dwarf2_per_objfile *dwarf2_per_objfile
6146 = get_dwarf2_per_objfile (objfile);
6147
6148 /* debug_names_table is NULL if OBJF_READNOW. */
6149 if (!dwarf2_per_objfile->debug_names_table)
6150 return;
6151
6152 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6153 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
6154
6155 const char *match_name = name.ada ().lookup_name ().c_str ();
6156 auto matcher = [&] (const char *symname)
6157 {
6158 if (ordered_compare == nullptr)
6159 return true;
6160 return ordered_compare (symname, match_name) == 0;
6161 };
6162
6163 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
6164 [&] (offset_type namei)
6165 {
6166 /* The name was matched, now expand corresponding CUs that were
6167 marked. */
6168 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
6169
6170 struct dwarf2_per_cu_data *per_cu;
6171 while ((per_cu = iter.next ()) != NULL)
6172 dw2_expand_symtabs_matching_one (per_cu, nullptr, nullptr);
6173 return true;
6174 });
6175
6176 /* It's a shame we couldn't do this inside the
6177 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
6178 that have already been expanded. Instead, this loop matches what
6179 the psymtab code does. */
6180 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
6181 {
6182 struct compunit_symtab *cust = per_cu->v.quick->compunit_symtab;
6183 if (cust != nullptr)
6184 {
6185 const struct block *block
6186 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
6187 if (!iterate_over_symbols_terminated (block, name,
6188 domain, callback))
6189 break;
6190 }
6191 }
6192 }
6193
6194 static void
6195 dw2_debug_names_expand_symtabs_matching
6196 (struct objfile *objfile,
6197 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
6198 const lookup_name_info &lookup_name,
6199 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
6200 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
6201 enum search_domain kind)
6202 {
6203 struct dwarf2_per_objfile *dwarf2_per_objfile
6204 = get_dwarf2_per_objfile (objfile);
6205
6206 /* debug_names_table is NULL if OBJF_READNOW. */
6207 if (!dwarf2_per_objfile->debug_names_table)
6208 return;
6209
6210 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
6211
6212 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6213
6214 dw2_expand_symtabs_matching_symbol (map, lookup_name,
6215 symbol_matcher,
6216 kind, [&] (offset_type namei)
6217 {
6218 /* The name was matched, now expand corresponding CUs that were
6219 marked. */
6220 dw2_debug_names_iterator iter (map, kind, namei);
6221
6222 struct dwarf2_per_cu_data *per_cu;
6223 while ((per_cu = iter.next ()) != NULL)
6224 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
6225 expansion_notify);
6226 return true;
6227 });
6228 }
6229
6230 const struct quick_symbol_functions dwarf2_debug_names_functions =
6231 {
6232 dw2_has_symbols,
6233 dw2_find_last_source_symtab,
6234 dw2_forget_cached_source_info,
6235 dw2_map_symtabs_matching_filename,
6236 dw2_debug_names_lookup_symbol,
6237 dw2_print_stats,
6238 dw2_debug_names_dump,
6239 dw2_debug_names_expand_symtabs_for_function,
6240 dw2_expand_all_symtabs,
6241 dw2_expand_symtabs_with_fullname,
6242 dw2_debug_names_map_matching_symbols,
6243 dw2_debug_names_expand_symtabs_matching,
6244 dw2_find_pc_sect_compunit_symtab,
6245 NULL,
6246 dw2_map_symbol_filenames
6247 };
6248
6249 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
6250 to either a dwarf2_per_objfile or dwz_file object. */
6251
6252 template <typename T>
6253 static gdb::array_view<const gdb_byte>
6254 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
6255 {
6256 dwarf2_section_info *section = &section_owner->gdb_index;
6257
6258 if (dwarf2_section_empty_p (section))
6259 return {};
6260
6261 /* Older elfutils strip versions could keep the section in the main
6262 executable while splitting it for the separate debug info file. */
6263 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
6264 return {};
6265
6266 dwarf2_read_section (obj, section);
6267
6268 /* dwarf2_section_info::size is a bfd_size_type, while
6269 gdb::array_view works with size_t. On 32-bit hosts, with
6270 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
6271 is 32-bit. So we need an explicit narrowing conversion here.
6272 This is fine, because it's impossible to allocate or mmap an
6273 array/buffer larger than what size_t can represent. */
6274 return gdb::make_array_view (section->buffer, section->size);
6275 }
6276
6277 /* Lookup the index cache for the contents of the index associated to
6278 DWARF2_OBJ. */
6279
6280 static gdb::array_view<const gdb_byte>
6281 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_objfile *dwarf2_obj)
6282 {
6283 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
6284 if (build_id == nullptr)
6285 return {};
6286
6287 return global_index_cache.lookup_gdb_index (build_id,
6288 &dwarf2_obj->index_cache_res);
6289 }
6290
6291 /* Same as the above, but for DWZ. */
6292
6293 static gdb::array_view<const gdb_byte>
6294 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
6295 {
6296 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
6297 if (build_id == nullptr)
6298 return {};
6299
6300 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
6301 }
6302
6303 /* See symfile.h. */
6304
6305 bool
6306 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
6307 {
6308 struct dwarf2_per_objfile *dwarf2_per_objfile
6309 = get_dwarf2_per_objfile (objfile);
6310
6311 /* If we're about to read full symbols, don't bother with the
6312 indices. In this case we also don't care if some other debug
6313 format is making psymtabs, because they are all about to be
6314 expanded anyway. */
6315 if ((objfile->flags & OBJF_READNOW))
6316 {
6317 dwarf2_per_objfile->using_index = 1;
6318 create_all_comp_units (dwarf2_per_objfile);
6319 create_all_type_units (dwarf2_per_objfile);
6320 dwarf2_per_objfile->quick_file_names_table
6321 = create_quick_file_names_table
6322 (dwarf2_per_objfile->all_comp_units.size ());
6323
6324 for (int i = 0; i < (dwarf2_per_objfile->all_comp_units.size ()
6325 + dwarf2_per_objfile->all_type_units.size ()); ++i)
6326 {
6327 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
6328
6329 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6330 struct dwarf2_per_cu_quick_data);
6331 }
6332
6333 /* Return 1 so that gdb sees the "quick" functions. However,
6334 these functions will be no-ops because we will have expanded
6335 all symtabs. */
6336 *index_kind = dw_index_kind::GDB_INDEX;
6337 return true;
6338 }
6339
6340 if (dwarf2_read_debug_names (dwarf2_per_objfile))
6341 {
6342 *index_kind = dw_index_kind::DEBUG_NAMES;
6343 return true;
6344 }
6345
6346 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
6347 get_gdb_index_contents_from_section<struct dwarf2_per_objfile>,
6348 get_gdb_index_contents_from_section<dwz_file>))
6349 {
6350 *index_kind = dw_index_kind::GDB_INDEX;
6351 return true;
6352 }
6353
6354 /* ... otherwise, try to find the index in the index cache. */
6355 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
6356 get_gdb_index_contents_from_cache,
6357 get_gdb_index_contents_from_cache_dwz))
6358 {
6359 global_index_cache.hit ();
6360 *index_kind = dw_index_kind::GDB_INDEX;
6361 return true;
6362 }
6363
6364 global_index_cache.miss ();
6365 return false;
6366 }
6367
6368 \f
6369
6370 /* Build a partial symbol table. */
6371
6372 void
6373 dwarf2_build_psymtabs (struct objfile *objfile)
6374 {
6375 struct dwarf2_per_objfile *dwarf2_per_objfile
6376 = get_dwarf2_per_objfile (objfile);
6377
6378 init_psymbol_list (objfile, 1024);
6379
6380 try
6381 {
6382 /* This isn't really ideal: all the data we allocate on the
6383 objfile's obstack is still uselessly kept around. However,
6384 freeing it seems unsafe. */
6385 psymtab_discarder psymtabs (objfile);
6386 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
6387 psymtabs.keep ();
6388
6389 /* (maybe) store an index in the cache. */
6390 global_index_cache.store (dwarf2_per_objfile);
6391 }
6392 catch (const gdb_exception_error &except)
6393 {
6394 exception_print (gdb_stderr, except);
6395 }
6396 }
6397
6398 /* Return the total length of the CU described by HEADER. */
6399
6400 static unsigned int
6401 get_cu_length (const struct comp_unit_head *header)
6402 {
6403 return header->initial_length_size + header->length;
6404 }
6405
6406 /* Return TRUE if SECT_OFF is within CU_HEADER. */
6407
6408 static inline bool
6409 offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
6410 {
6411 sect_offset bottom = cu_header->sect_off;
6412 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
6413
6414 return sect_off >= bottom && sect_off < top;
6415 }
6416
6417 /* Find the base address of the compilation unit for range lists and
6418 location lists. It will normally be specified by DW_AT_low_pc.
6419 In DWARF-3 draft 4, the base address could be overridden by
6420 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6421 compilation units with discontinuous ranges. */
6422
6423 static void
6424 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6425 {
6426 struct attribute *attr;
6427
6428 cu->base_known = 0;
6429 cu->base_address = 0;
6430
6431 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6432 if (attr != nullptr)
6433 {
6434 cu->base_address = attr_value_as_address (attr);
6435 cu->base_known = 1;
6436 }
6437 else
6438 {
6439 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6440 if (attr != nullptr)
6441 {
6442 cu->base_address = attr_value_as_address (attr);
6443 cu->base_known = 1;
6444 }
6445 }
6446 }
6447
6448 /* Read in the comp unit header information from the debug_info at info_ptr.
6449 Use rcuh_kind::COMPILE as the default type if not known by the caller.
6450 NOTE: This leaves members offset, first_die_offset to be filled in
6451 by the caller. */
6452
6453 static const gdb_byte *
6454 read_comp_unit_head (struct comp_unit_head *cu_header,
6455 const gdb_byte *info_ptr,
6456 struct dwarf2_section_info *section,
6457 rcuh_kind section_kind)
6458 {
6459 int signed_addr;
6460 unsigned int bytes_read;
6461 const char *filename = get_section_file_name (section);
6462 bfd *abfd = get_section_bfd_owner (section);
6463
6464 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
6465 cu_header->initial_length_size = bytes_read;
6466 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
6467 info_ptr += bytes_read;
6468 cu_header->version = read_2_bytes (abfd, info_ptr);
6469 if (cu_header->version < 2 || cu_header->version > 5)
6470 error (_("Dwarf Error: wrong version in compilation unit header "
6471 "(is %d, should be 2, 3, 4 or 5) [in module %s]"),
6472 cu_header->version, filename);
6473 info_ptr += 2;
6474 if (cu_header->version < 5)
6475 switch (section_kind)
6476 {
6477 case rcuh_kind::COMPILE:
6478 cu_header->unit_type = DW_UT_compile;
6479 break;
6480 case rcuh_kind::TYPE:
6481 cu_header->unit_type = DW_UT_type;
6482 break;
6483 default:
6484 internal_error (__FILE__, __LINE__,
6485 _("read_comp_unit_head: invalid section_kind"));
6486 }
6487 else
6488 {
6489 cu_header->unit_type = static_cast<enum dwarf_unit_type>
6490 (read_1_byte (abfd, info_ptr));
6491 info_ptr += 1;
6492 switch (cu_header->unit_type)
6493 {
6494 case DW_UT_compile:
6495 case DW_UT_partial:
6496 case DW_UT_skeleton:
6497 case DW_UT_split_compile:
6498 if (section_kind != rcuh_kind::COMPILE)
6499 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6500 "(is %s, should be %s) [in module %s]"),
6501 dwarf_unit_type_name (cu_header->unit_type),
6502 dwarf_unit_type_name (DW_UT_type), filename);
6503 break;
6504 case DW_UT_type:
6505 case DW_UT_split_type:
6506 section_kind = rcuh_kind::TYPE;
6507 break;
6508 default:
6509 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6510 "(is %#04x, should be one of: %s, %s, %s, %s or %s) "
6511 "[in module %s]"), cu_header->unit_type,
6512 dwarf_unit_type_name (DW_UT_compile),
6513 dwarf_unit_type_name (DW_UT_skeleton),
6514 dwarf_unit_type_name (DW_UT_split_compile),
6515 dwarf_unit_type_name (DW_UT_type),
6516 dwarf_unit_type_name (DW_UT_split_type), filename);
6517 }
6518
6519 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6520 info_ptr += 1;
6521 }
6522 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
6523 cu_header,
6524 &bytes_read);
6525 info_ptr += bytes_read;
6526 if (cu_header->version < 5)
6527 {
6528 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6529 info_ptr += 1;
6530 }
6531 signed_addr = bfd_get_sign_extend_vma (abfd);
6532 if (signed_addr < 0)
6533 internal_error (__FILE__, __LINE__,
6534 _("read_comp_unit_head: dwarf from non elf file"));
6535 cu_header->signed_addr_p = signed_addr;
6536
6537 bool header_has_signature = section_kind == rcuh_kind::TYPE
6538 || cu_header->unit_type == DW_UT_skeleton
6539 || cu_header->unit_type == DW_UT_split_compile;
6540
6541 if (header_has_signature)
6542 {
6543 cu_header->signature = read_8_bytes (abfd, info_ptr);
6544 info_ptr += 8;
6545 }
6546
6547 if (section_kind == rcuh_kind::TYPE)
6548 {
6549 LONGEST type_offset;
6550 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
6551 info_ptr += bytes_read;
6552 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
6553 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
6554 error (_("Dwarf Error: Too big type_offset in compilation unit "
6555 "header (is %s) [in module %s]"), plongest (type_offset),
6556 filename);
6557 }
6558
6559 return info_ptr;
6560 }
6561
6562 /* Helper function that returns the proper abbrev section for
6563 THIS_CU. */
6564
6565 static struct dwarf2_section_info *
6566 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6567 {
6568 struct dwarf2_section_info *abbrev;
6569 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6570
6571 if (this_cu->is_dwz)
6572 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
6573 else
6574 abbrev = &dwarf2_per_objfile->abbrev;
6575
6576 return abbrev;
6577 }
6578
6579 /* Subroutine of read_and_check_comp_unit_head and
6580 read_and_check_type_unit_head to simplify them.
6581 Perform various error checking on the header. */
6582
6583 static void
6584 error_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6585 struct comp_unit_head *header,
6586 struct dwarf2_section_info *section,
6587 struct dwarf2_section_info *abbrev_section)
6588 {
6589 const char *filename = get_section_file_name (section);
6590
6591 if (to_underlying (header->abbrev_sect_off)
6592 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
6593 error (_("Dwarf Error: bad offset (%s) in compilation unit header "
6594 "(offset %s + 6) [in module %s]"),
6595 sect_offset_str (header->abbrev_sect_off),
6596 sect_offset_str (header->sect_off),
6597 filename);
6598
6599 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
6600 avoid potential 32-bit overflow. */
6601 if (((ULONGEST) header->sect_off + get_cu_length (header))
6602 > section->size)
6603 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
6604 "(offset %s + 0) [in module %s]"),
6605 header->length, sect_offset_str (header->sect_off),
6606 filename);
6607 }
6608
6609 /* Read in a CU/TU header and perform some basic error checking.
6610 The contents of the header are stored in HEADER.
6611 The result is a pointer to the start of the first DIE. */
6612
6613 static const gdb_byte *
6614 read_and_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6615 struct comp_unit_head *header,
6616 struct dwarf2_section_info *section,
6617 struct dwarf2_section_info *abbrev_section,
6618 const gdb_byte *info_ptr,
6619 rcuh_kind section_kind)
6620 {
6621 const gdb_byte *beg_of_comp_unit = info_ptr;
6622
6623 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
6624
6625 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
6626
6627 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
6628
6629 error_check_comp_unit_head (dwarf2_per_objfile, header, section,
6630 abbrev_section);
6631
6632 return info_ptr;
6633 }
6634
6635 /* Fetch the abbreviation table offset from a comp or type unit header. */
6636
6637 static sect_offset
6638 read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6639 struct dwarf2_section_info *section,
6640 sect_offset sect_off)
6641 {
6642 bfd *abfd = get_section_bfd_owner (section);
6643 const gdb_byte *info_ptr;
6644 unsigned int initial_length_size, offset_size;
6645 uint16_t version;
6646
6647 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
6648 info_ptr = section->buffer + to_underlying (sect_off);
6649 read_initial_length (abfd, info_ptr, &initial_length_size);
6650 offset_size = initial_length_size == 4 ? 4 : 8;
6651 info_ptr += initial_length_size;
6652
6653 version = read_2_bytes (abfd, info_ptr);
6654 info_ptr += 2;
6655 if (version >= 5)
6656 {
6657 /* Skip unit type and address size. */
6658 info_ptr += 2;
6659 }
6660
6661 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
6662 }
6663
6664 /* Allocate a new partial symtab for file named NAME and mark this new
6665 partial symtab as being an include of PST. */
6666
6667 static void
6668 dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
6669 struct objfile *objfile)
6670 {
6671 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
6672
6673 if (!IS_ABSOLUTE_PATH (subpst->filename))
6674 {
6675 /* It shares objfile->objfile_obstack. */
6676 subpst->dirname = pst->dirname;
6677 }
6678
6679 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
6680 subpst->dependencies[0] = pst;
6681 subpst->number_of_dependencies = 1;
6682
6683 subpst->read_symtab = pst->read_symtab;
6684
6685 /* No private part is necessary for include psymtabs. This property
6686 can be used to differentiate between such include psymtabs and
6687 the regular ones. */
6688 subpst->read_symtab_private = NULL;
6689 }
6690
6691 /* Read the Line Number Program data and extract the list of files
6692 included by the source file represented by PST. Build an include
6693 partial symtab for each of these included files. */
6694
6695 static void
6696 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
6697 struct die_info *die,
6698 struct partial_symtab *pst)
6699 {
6700 line_header_up lh;
6701 struct attribute *attr;
6702
6703 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6704 if (attr != nullptr)
6705 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
6706 if (lh == NULL)
6707 return; /* No linetable, so no includes. */
6708
6709 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6710 that we pass in the raw text_low here; that is ok because we're
6711 only decoding the line table to make include partial symtabs, and
6712 so the addresses aren't really used. */
6713 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
6714 pst->raw_text_low (), 1);
6715 }
6716
6717 static hashval_t
6718 hash_signatured_type (const void *item)
6719 {
6720 const struct signatured_type *sig_type
6721 = (const struct signatured_type *) item;
6722
6723 /* This drops the top 32 bits of the signature, but is ok for a hash. */
6724 return sig_type->signature;
6725 }
6726
6727 static int
6728 eq_signatured_type (const void *item_lhs, const void *item_rhs)
6729 {
6730 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6731 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
6732
6733 return lhs->signature == rhs->signature;
6734 }
6735
6736 /* Allocate a hash table for signatured types. */
6737
6738 static htab_t
6739 allocate_signatured_type_table (struct objfile *objfile)
6740 {
6741 return htab_create_alloc_ex (41,
6742 hash_signatured_type,
6743 eq_signatured_type,
6744 NULL,
6745 &objfile->objfile_obstack,
6746 hashtab_obstack_allocate,
6747 dummy_obstack_deallocate);
6748 }
6749
6750 /* A helper function to add a signatured type CU to a table. */
6751
6752 static int
6753 add_signatured_type_cu_to_table (void **slot, void *datum)
6754 {
6755 struct signatured_type *sigt = (struct signatured_type *) *slot;
6756 std::vector<signatured_type *> *all_type_units
6757 = (std::vector<signatured_type *> *) datum;
6758
6759 all_type_units->push_back (sigt);
6760
6761 return 1;
6762 }
6763
6764 /* A helper for create_debug_types_hash_table. Read types from SECTION
6765 and fill them into TYPES_HTAB. It will process only type units,
6766 therefore DW_UT_type. */
6767
6768 static void
6769 create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6770 struct dwo_file *dwo_file,
6771 dwarf2_section_info *section, htab_t &types_htab,
6772 rcuh_kind section_kind)
6773 {
6774 struct objfile *objfile = dwarf2_per_objfile->objfile;
6775 struct dwarf2_section_info *abbrev_section;
6776 bfd *abfd;
6777 const gdb_byte *info_ptr, *end_ptr;
6778
6779 abbrev_section = (dwo_file != NULL
6780 ? &dwo_file->sections.abbrev
6781 : &dwarf2_per_objfile->abbrev);
6782
6783 if (dwarf_read_debug)
6784 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6785 get_section_name (section),
6786 get_section_file_name (abbrev_section));
6787
6788 dwarf2_read_section (objfile, section);
6789 info_ptr = section->buffer;
6790
6791 if (info_ptr == NULL)
6792 return;
6793
6794 /* We can't set abfd until now because the section may be empty or
6795 not present, in which case the bfd is unknown. */
6796 abfd = get_section_bfd_owner (section);
6797
6798 /* We don't use init_cutu_and_read_dies_simple, or some such, here
6799 because we don't need to read any dies: the signature is in the
6800 header. */
6801
6802 end_ptr = info_ptr + section->size;
6803 while (info_ptr < end_ptr)
6804 {
6805 struct signatured_type *sig_type;
6806 struct dwo_unit *dwo_tu;
6807 void **slot;
6808 const gdb_byte *ptr = info_ptr;
6809 struct comp_unit_head header;
6810 unsigned int length;
6811
6812 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6813
6814 /* Initialize it due to a false compiler warning. */
6815 header.signature = -1;
6816 header.type_cu_offset_in_tu = (cu_offset) -1;
6817
6818 /* We need to read the type's signature in order to build the hash
6819 table, but we don't need anything else just yet. */
6820
6821 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
6822 abbrev_section, ptr, section_kind);
6823
6824 length = get_cu_length (&header);
6825
6826 /* Skip dummy type units. */
6827 if (ptr >= info_ptr + length
6828 || peek_abbrev_code (abfd, ptr) == 0
6829 || header.unit_type != DW_UT_type)
6830 {
6831 info_ptr += length;
6832 continue;
6833 }
6834
6835 if (types_htab == NULL)
6836 {
6837 if (dwo_file)
6838 types_htab = allocate_dwo_unit_table (objfile);
6839 else
6840 types_htab = allocate_signatured_type_table (objfile);
6841 }
6842
6843 if (dwo_file)
6844 {
6845 sig_type = NULL;
6846 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6847 struct dwo_unit);
6848 dwo_tu->dwo_file = dwo_file;
6849 dwo_tu->signature = header.signature;
6850 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6851 dwo_tu->section = section;
6852 dwo_tu->sect_off = sect_off;
6853 dwo_tu->length = length;
6854 }
6855 else
6856 {
6857 /* N.B.: type_offset is not usable if this type uses a DWO file.
6858 The real type_offset is in the DWO file. */
6859 dwo_tu = NULL;
6860 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6861 struct signatured_type);
6862 sig_type->signature = header.signature;
6863 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6864 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6865 sig_type->per_cu.is_debug_types = 1;
6866 sig_type->per_cu.section = section;
6867 sig_type->per_cu.sect_off = sect_off;
6868 sig_type->per_cu.length = length;
6869 }
6870
6871 slot = htab_find_slot (types_htab,
6872 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6873 INSERT);
6874 gdb_assert (slot != NULL);
6875 if (*slot != NULL)
6876 {
6877 sect_offset dup_sect_off;
6878
6879 if (dwo_file)
6880 {
6881 const struct dwo_unit *dup_tu
6882 = (const struct dwo_unit *) *slot;
6883
6884 dup_sect_off = dup_tu->sect_off;
6885 }
6886 else
6887 {
6888 const struct signatured_type *dup_tu
6889 = (const struct signatured_type *) *slot;
6890
6891 dup_sect_off = dup_tu->per_cu.sect_off;
6892 }
6893
6894 complaint (_("debug type entry at offset %s is duplicate to"
6895 " the entry at offset %s, signature %s"),
6896 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
6897 hex_string (header.signature));
6898 }
6899 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
6900
6901 if (dwarf_read_debug > 1)
6902 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6903 sect_offset_str (sect_off),
6904 hex_string (header.signature));
6905
6906 info_ptr += length;
6907 }
6908 }
6909
6910 /* Create the hash table of all entries in the .debug_types
6911 (or .debug_types.dwo) section(s).
6912 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6913 otherwise it is NULL.
6914
6915 The result is a pointer to the hash table or NULL if there are no types.
6916
6917 Note: This function processes DWO files only, not DWP files. */
6918
6919 static void
6920 create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6921 struct dwo_file *dwo_file,
6922 gdb::array_view<dwarf2_section_info> type_sections,
6923 htab_t &types_htab)
6924 {
6925 for (dwarf2_section_info &section : type_sections)
6926 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
6927 types_htab, rcuh_kind::TYPE);
6928 }
6929
6930 /* Create the hash table of all entries in the .debug_types section,
6931 and initialize all_type_units.
6932 The result is zero if there is an error (e.g. missing .debug_types section),
6933 otherwise non-zero. */
6934
6935 static int
6936 create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6937 {
6938 htab_t types_htab = NULL;
6939
6940 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6941 &dwarf2_per_objfile->info, types_htab,
6942 rcuh_kind::COMPILE);
6943 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6944 dwarf2_per_objfile->types, types_htab);
6945 if (types_htab == NULL)
6946 {
6947 dwarf2_per_objfile->signatured_types = NULL;
6948 return 0;
6949 }
6950
6951 dwarf2_per_objfile->signatured_types = types_htab;
6952
6953 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
6954 dwarf2_per_objfile->all_type_units.reserve (htab_elements (types_htab));
6955
6956 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table,
6957 &dwarf2_per_objfile->all_type_units);
6958
6959 return 1;
6960 }
6961
6962 /* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6963 If SLOT is non-NULL, it is the entry to use in the hash table.
6964 Otherwise we find one. */
6965
6966 static struct signatured_type *
6967 add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6968 void **slot)
6969 {
6970 struct objfile *objfile = dwarf2_per_objfile->objfile;
6971
6972 if (dwarf2_per_objfile->all_type_units.size ()
6973 == dwarf2_per_objfile->all_type_units.capacity ())
6974 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
6975
6976 signatured_type *sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6977 struct signatured_type);
6978
6979 dwarf2_per_objfile->all_type_units.push_back (sig_type);
6980 sig_type->signature = sig;
6981 sig_type->per_cu.is_debug_types = 1;
6982 if (dwarf2_per_objfile->using_index)
6983 {
6984 sig_type->per_cu.v.quick =
6985 OBSTACK_ZALLOC (&objfile->objfile_obstack,
6986 struct dwarf2_per_cu_quick_data);
6987 }
6988
6989 if (slot == NULL)
6990 {
6991 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
6992 sig_type, INSERT);
6993 }
6994 gdb_assert (*slot == NULL);
6995 *slot = sig_type;
6996 /* The rest of sig_type must be filled in by the caller. */
6997 return sig_type;
6998 }
6999
7000 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
7001 Fill in SIG_ENTRY with DWO_ENTRY. */
7002
7003 static void
7004 fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
7005 struct signatured_type *sig_entry,
7006 struct dwo_unit *dwo_entry)
7007 {
7008 /* Make sure we're not clobbering something we don't expect to. */
7009 gdb_assert (! sig_entry->per_cu.queued);
7010 gdb_assert (sig_entry->per_cu.cu == NULL);
7011 if (dwarf2_per_objfile->using_index)
7012 {
7013 gdb_assert (sig_entry->per_cu.v.quick != NULL);
7014 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
7015 }
7016 else
7017 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
7018 gdb_assert (sig_entry->signature == dwo_entry->signature);
7019 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
7020 gdb_assert (sig_entry->type_unit_group == NULL);
7021 gdb_assert (sig_entry->dwo_unit == NULL);
7022
7023 sig_entry->per_cu.section = dwo_entry->section;
7024 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7025 sig_entry->per_cu.length = dwo_entry->length;
7026 sig_entry->per_cu.reading_dwo_directly = 1;
7027 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
7028 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
7029 sig_entry->dwo_unit = dwo_entry;
7030 }
7031
7032 /* Subroutine of lookup_signatured_type.
7033 If we haven't read the TU yet, create the signatured_type data structure
7034 for a TU to be read in directly from a DWO file, bypassing the stub.
7035 This is the "Stay in DWO Optimization": When there is no DWP file and we're
7036 using .gdb_index, then when reading a CU we want to stay in the DWO file
7037 containing that CU. Otherwise we could end up reading several other DWO
7038 files (due to comdat folding) to process the transitive closure of all the
7039 mentioned TUs, and that can be slow. The current DWO file will have every
7040 type signature that it needs.
7041 We only do this for .gdb_index because in the psymtab case we already have
7042 to read all the DWOs to build the type unit groups. */
7043
7044 static struct signatured_type *
7045 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7046 {
7047 struct dwarf2_per_objfile *dwarf2_per_objfile
7048 = cu->per_cu->dwarf2_per_objfile;
7049 struct objfile *objfile = dwarf2_per_objfile->objfile;
7050 struct dwo_file *dwo_file;
7051 struct dwo_unit find_dwo_entry, *dwo_entry;
7052 struct signatured_type find_sig_entry, *sig_entry;
7053 void **slot;
7054
7055 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7056
7057 /* If TU skeletons have been removed then we may not have read in any
7058 TUs yet. */
7059 if (dwarf2_per_objfile->signatured_types == NULL)
7060 {
7061 dwarf2_per_objfile->signatured_types
7062 = allocate_signatured_type_table (objfile);
7063 }
7064
7065 /* We only ever need to read in one copy of a signatured type.
7066 Use the global signatured_types array to do our own comdat-folding
7067 of types. If this is the first time we're reading this TU, and
7068 the TU has an entry in .gdb_index, replace the recorded data from
7069 .gdb_index with this TU. */
7070
7071 find_sig_entry.signature = sig;
7072 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7073 &find_sig_entry, INSERT);
7074 sig_entry = (struct signatured_type *) *slot;
7075
7076 /* We can get here with the TU already read, *or* in the process of being
7077 read. Don't reassign the global entry to point to this DWO if that's
7078 the case. Also note that if the TU is already being read, it may not
7079 have come from a DWO, the program may be a mix of Fission-compiled
7080 code and non-Fission-compiled code. */
7081
7082 /* Have we already tried to read this TU?
7083 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7084 needn't exist in the global table yet). */
7085 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
7086 return sig_entry;
7087
7088 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
7089 dwo_unit of the TU itself. */
7090 dwo_file = cu->dwo_unit->dwo_file;
7091
7092 /* Ok, this is the first time we're reading this TU. */
7093 if (dwo_file->tus == NULL)
7094 return NULL;
7095 find_dwo_entry.signature = sig;
7096 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
7097 if (dwo_entry == NULL)
7098 return NULL;
7099
7100 /* If the global table doesn't have an entry for this TU, add one. */
7101 if (sig_entry == NULL)
7102 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
7103
7104 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
7105 sig_entry->per_cu.tu_read = 1;
7106 return sig_entry;
7107 }
7108
7109 /* Subroutine of lookup_signatured_type.
7110 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
7111 then try the DWP file. If the TU stub (skeleton) has been removed then
7112 it won't be in .gdb_index. */
7113
7114 static struct signatured_type *
7115 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7116 {
7117 struct dwarf2_per_objfile *dwarf2_per_objfile
7118 = cu->per_cu->dwarf2_per_objfile;
7119 struct objfile *objfile = dwarf2_per_objfile->objfile;
7120 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
7121 struct dwo_unit *dwo_entry;
7122 struct signatured_type find_sig_entry, *sig_entry;
7123 void **slot;
7124
7125 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7126 gdb_assert (dwp_file != NULL);
7127
7128 /* If TU skeletons have been removed then we may not have read in any
7129 TUs yet. */
7130 if (dwarf2_per_objfile->signatured_types == NULL)
7131 {
7132 dwarf2_per_objfile->signatured_types
7133 = allocate_signatured_type_table (objfile);
7134 }
7135
7136 find_sig_entry.signature = sig;
7137 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7138 &find_sig_entry, INSERT);
7139 sig_entry = (struct signatured_type *) *slot;
7140
7141 /* Have we already tried to read this TU?
7142 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7143 needn't exist in the global table yet). */
7144 if (sig_entry != NULL)
7145 return sig_entry;
7146
7147 if (dwp_file->tus == NULL)
7148 return NULL;
7149 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
7150 sig, 1 /* is_debug_types */);
7151 if (dwo_entry == NULL)
7152 return NULL;
7153
7154 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
7155 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
7156
7157 return sig_entry;
7158 }
7159
7160 /* Lookup a signature based type for DW_FORM_ref_sig8.
7161 Returns NULL if signature SIG is not present in the table.
7162 It is up to the caller to complain about this. */
7163
7164 static struct signatured_type *
7165 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7166 {
7167 struct dwarf2_per_objfile *dwarf2_per_objfile
7168 = cu->per_cu->dwarf2_per_objfile;
7169
7170 if (cu->dwo_unit
7171 && dwarf2_per_objfile->using_index)
7172 {
7173 /* We're in a DWO/DWP file, and we're using .gdb_index.
7174 These cases require special processing. */
7175 if (get_dwp_file (dwarf2_per_objfile) == NULL)
7176 return lookup_dwo_signatured_type (cu, sig);
7177 else
7178 return lookup_dwp_signatured_type (cu, sig);
7179 }
7180 else
7181 {
7182 struct signatured_type find_entry, *entry;
7183
7184 if (dwarf2_per_objfile->signatured_types == NULL)
7185 return NULL;
7186 find_entry.signature = sig;
7187 entry = ((struct signatured_type *)
7188 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
7189 return entry;
7190 }
7191 }
7192
7193 /* Return the address base of the compile unit, which, if exists, is stored
7194 either at the attribute DW_AT_GNU_addr_base, or DW_AT_addr_base. */
7195 static gdb::optional<ULONGEST>
7196 lookup_addr_base (struct die_info *comp_unit_die)
7197 {
7198 struct attribute *attr;
7199 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_addr_base);
7200 if (attr == nullptr)
7201 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_GNU_addr_base);
7202 if (attr == nullptr)
7203 return gdb::optional<ULONGEST> ();
7204 return DW_UNSND (attr);
7205 }
7206
7207 /* Return range lists base of the compile unit, which, if exists, is stored
7208 either at the attribute DW_AT_rnglists_base or DW_AT_GNU_ranges_base. */
7209 static ULONGEST
7210 lookup_ranges_base (struct die_info *comp_unit_die)
7211 {
7212 struct attribute *attr;
7213 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_rnglists_base);
7214 if (attr == nullptr)
7215 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_GNU_ranges_base);
7216 if (attr == nullptr)
7217 return 0;
7218 return DW_UNSND (attr);
7219 }
7220
7221 /* Low level DIE reading support. */
7222
7223 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
7224
7225 static void
7226 init_cu_die_reader (struct die_reader_specs *reader,
7227 struct dwarf2_cu *cu,
7228 struct dwarf2_section_info *section,
7229 struct dwo_file *dwo_file,
7230 struct abbrev_table *abbrev_table)
7231 {
7232 gdb_assert (section->readin && section->buffer != NULL);
7233 reader->abfd = get_section_bfd_owner (section);
7234 reader->cu = cu;
7235 reader->dwo_file = dwo_file;
7236 reader->die_section = section;
7237 reader->buffer = section->buffer;
7238 reader->buffer_end = section->buffer + section->size;
7239 reader->comp_dir = NULL;
7240 reader->abbrev_table = abbrev_table;
7241 }
7242
7243 /* Subroutine of init_cutu_and_read_dies to simplify it.
7244 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
7245 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
7246 already.
7247
7248 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
7249 from it to the DIE in the DWO. If NULL we are skipping the stub.
7250 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
7251 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
7252 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
7253 STUB_COMP_DIR may be non-NULL.
7254 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
7255 are filled in with the info of the DIE from the DWO file.
7256 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
7257 from the dwo. Since *RESULT_READER references this abbrev table, it must be
7258 kept around for at least as long as *RESULT_READER.
7259
7260 The result is non-zero if a valid (non-dummy) DIE was found. */
7261
7262 static int
7263 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
7264 struct dwo_unit *dwo_unit,
7265 struct die_info *stub_comp_unit_die,
7266 const char *stub_comp_dir,
7267 struct die_reader_specs *result_reader,
7268 const gdb_byte **result_info_ptr,
7269 struct die_info **result_comp_unit_die,
7270 int *result_has_children,
7271 abbrev_table_up *result_dwo_abbrev_table)
7272 {
7273 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7274 struct objfile *objfile = dwarf2_per_objfile->objfile;
7275 struct dwarf2_cu *cu = this_cu->cu;
7276 bfd *abfd;
7277 const gdb_byte *begin_info_ptr, *info_ptr;
7278 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
7279 int i,num_extra_attrs;
7280 struct dwarf2_section_info *dwo_abbrev_section;
7281 struct die_info *comp_unit_die;
7282
7283 /* At most one of these may be provided. */
7284 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
7285
7286 /* These attributes aren't processed until later:
7287 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
7288 DW_AT_comp_dir is used now, to find the DWO file, but it is also
7289 referenced later. However, these attributes are found in the stub
7290 which we won't have later. In order to not impose this complication
7291 on the rest of the code, we read them here and copy them to the
7292 DWO CU/TU die. */
7293
7294 stmt_list = NULL;
7295 low_pc = NULL;
7296 high_pc = NULL;
7297 ranges = NULL;
7298 comp_dir = NULL;
7299
7300 if (stub_comp_unit_die != NULL)
7301 {
7302 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
7303 DWO file. */
7304 if (! this_cu->is_debug_types)
7305 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
7306 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
7307 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
7308 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
7309 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
7310
7311 cu->addr_base = lookup_addr_base (stub_comp_unit_die);
7312
7313 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
7314 here (if needed). We need the value before we can process
7315 DW_AT_ranges. */
7316 cu->ranges_base = lookup_ranges_base (stub_comp_unit_die);
7317 }
7318 else if (stub_comp_dir != NULL)
7319 {
7320 /* Reconstruct the comp_dir attribute to simplify the code below. */
7321 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
7322 comp_dir->name = DW_AT_comp_dir;
7323 comp_dir->form = DW_FORM_string;
7324 DW_STRING_IS_CANONICAL (comp_dir) = 0;
7325 DW_STRING (comp_dir) = stub_comp_dir;
7326 }
7327
7328 /* Set up for reading the DWO CU/TU. */
7329 cu->dwo_unit = dwo_unit;
7330 dwarf2_section_info *section = dwo_unit->section;
7331 dwarf2_read_section (objfile, section);
7332 abfd = get_section_bfd_owner (section);
7333 begin_info_ptr = info_ptr = (section->buffer
7334 + to_underlying (dwo_unit->sect_off));
7335 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
7336
7337 if (this_cu->is_debug_types)
7338 {
7339 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
7340
7341 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7342 &cu->header, section,
7343 dwo_abbrev_section,
7344 info_ptr, rcuh_kind::TYPE);
7345 /* This is not an assert because it can be caused by bad debug info. */
7346 if (sig_type->signature != cu->header.signature)
7347 {
7348 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
7349 " TU at offset %s [in module %s]"),
7350 hex_string (sig_type->signature),
7351 hex_string (cu->header.signature),
7352 sect_offset_str (dwo_unit->sect_off),
7353 bfd_get_filename (abfd));
7354 }
7355 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7356 /* For DWOs coming from DWP files, we don't know the CU length
7357 nor the type's offset in the TU until now. */
7358 dwo_unit->length = get_cu_length (&cu->header);
7359 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
7360
7361 /* Establish the type offset that can be used to lookup the type.
7362 For DWO files, we don't know it until now. */
7363 sig_type->type_offset_in_section
7364 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
7365 }
7366 else
7367 {
7368 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7369 &cu->header, section,
7370 dwo_abbrev_section,
7371 info_ptr, rcuh_kind::COMPILE);
7372 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7373 /* For DWOs coming from DWP files, we don't know the CU length
7374 until now. */
7375 dwo_unit->length = get_cu_length (&cu->header);
7376 }
7377
7378 *result_dwo_abbrev_table
7379 = abbrev_table_read_table (dwarf2_per_objfile, dwo_abbrev_section,
7380 cu->header.abbrev_sect_off);
7381 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
7382 result_dwo_abbrev_table->get ());
7383
7384 /* Read in the die, but leave space to copy over the attributes
7385 from the stub. This has the benefit of simplifying the rest of
7386 the code - all the work to maintain the illusion of a single
7387 DW_TAG_{compile,type}_unit DIE is done here. */
7388 num_extra_attrs = ((stmt_list != NULL)
7389 + (low_pc != NULL)
7390 + (high_pc != NULL)
7391 + (ranges != NULL)
7392 + (comp_dir != NULL));
7393 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
7394 result_has_children, num_extra_attrs);
7395
7396 /* Copy over the attributes from the stub to the DIE we just read in. */
7397 comp_unit_die = *result_comp_unit_die;
7398 i = comp_unit_die->num_attrs;
7399 if (stmt_list != NULL)
7400 comp_unit_die->attrs[i++] = *stmt_list;
7401 if (low_pc != NULL)
7402 comp_unit_die->attrs[i++] = *low_pc;
7403 if (high_pc != NULL)
7404 comp_unit_die->attrs[i++] = *high_pc;
7405 if (ranges != NULL)
7406 comp_unit_die->attrs[i++] = *ranges;
7407 if (comp_dir != NULL)
7408 comp_unit_die->attrs[i++] = *comp_dir;
7409 comp_unit_die->num_attrs += num_extra_attrs;
7410
7411 if (dwarf_die_debug)
7412 {
7413 fprintf_unfiltered (gdb_stdlog,
7414 "Read die from %s@0x%x of %s:\n",
7415 get_section_name (section),
7416 (unsigned) (begin_info_ptr - section->buffer),
7417 bfd_get_filename (abfd));
7418 dump_die (comp_unit_die, dwarf_die_debug);
7419 }
7420
7421 /* Save the comp_dir attribute. If there is no DWP file then we'll read
7422 TUs by skipping the stub and going directly to the entry in the DWO file.
7423 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
7424 to get it via circuitous means. Blech. */
7425 if (comp_dir != NULL)
7426 result_reader->comp_dir = DW_STRING (comp_dir);
7427
7428 /* Skip dummy compilation units. */
7429 if (info_ptr >= begin_info_ptr + dwo_unit->length
7430 || peek_abbrev_code (abfd, info_ptr) == 0)
7431 return 0;
7432
7433 *result_info_ptr = info_ptr;
7434 return 1;
7435 }
7436
7437 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
7438 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
7439 signature is part of the header. */
7440 static gdb::optional<ULONGEST>
7441 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
7442 {
7443 if (cu->header.version >= 5)
7444 return cu->header.signature;
7445 struct attribute *attr;
7446 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7447 if (attr == nullptr)
7448 return gdb::optional<ULONGEST> ();
7449 return DW_UNSND (attr);
7450 }
7451
7452 /* Subroutine of init_cutu_and_read_dies to simplify it.
7453 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
7454 Returns NULL if the specified DWO unit cannot be found. */
7455
7456 static struct dwo_unit *
7457 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
7458 struct die_info *comp_unit_die)
7459 {
7460 struct dwarf2_cu *cu = this_cu->cu;
7461 struct dwo_unit *dwo_unit;
7462 const char *comp_dir, *dwo_name;
7463
7464 gdb_assert (cu != NULL);
7465
7466 /* Yeah, we look dwo_name up again, but it simplifies the code. */
7467 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7468 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7469
7470 if (this_cu->is_debug_types)
7471 {
7472 struct signatured_type *sig_type;
7473
7474 /* Since this_cu is the first member of struct signatured_type,
7475 we can go from a pointer to one to a pointer to the other. */
7476 sig_type = (struct signatured_type *) this_cu;
7477 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
7478 }
7479 else
7480 {
7481 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
7482 if (!signature.has_value ())
7483 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7484 " [in module %s]"),
7485 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
7486 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
7487 *signature);
7488 }
7489
7490 return dwo_unit;
7491 }
7492
7493 /* Subroutine of init_cutu_and_read_dies to simplify it.
7494 See it for a description of the parameters.
7495 Read a TU directly from a DWO file, bypassing the stub. */
7496
7497 static void
7498 init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
7499 int use_existing_cu, int keep,
7500 die_reader_func_ftype *die_reader_func,
7501 void *data)
7502 {
7503 std::unique_ptr<dwarf2_cu> new_cu;
7504 struct signatured_type *sig_type;
7505 struct die_reader_specs reader;
7506 const gdb_byte *info_ptr;
7507 struct die_info *comp_unit_die;
7508 int has_children;
7509 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7510
7511 /* Verify we can do the following downcast, and that we have the
7512 data we need. */
7513 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7514 sig_type = (struct signatured_type *) this_cu;
7515 gdb_assert (sig_type->dwo_unit != NULL);
7516
7517 if (use_existing_cu && this_cu->cu != NULL)
7518 {
7519 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
7520 /* There's no need to do the rereading_dwo_cu handling that
7521 init_cutu_and_read_dies does since we don't read the stub. */
7522 }
7523 else
7524 {
7525 /* If !use_existing_cu, this_cu->cu must be NULL. */
7526 gdb_assert (this_cu->cu == NULL);
7527 new_cu.reset (new dwarf2_cu (this_cu));
7528 }
7529
7530 /* A future optimization, if needed, would be to use an existing
7531 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7532 could share abbrev tables. */
7533
7534 /* The abbreviation table used by READER, this must live at least as long as
7535 READER. */
7536 abbrev_table_up dwo_abbrev_table;
7537
7538 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
7539 NULL /* stub_comp_unit_die */,
7540 sig_type->dwo_unit->dwo_file->comp_dir,
7541 &reader, &info_ptr,
7542 &comp_unit_die, &has_children,
7543 &dwo_abbrev_table) == 0)
7544 {
7545 /* Dummy die. */
7546 return;
7547 }
7548
7549 /* All the "real" work is done here. */
7550 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7551
7552 /* This duplicates the code in init_cutu_and_read_dies,
7553 but the alternative is making the latter more complex.
7554 This function is only for the special case of using DWO files directly:
7555 no point in overly complicating the general case just to handle this. */
7556 if (new_cu != NULL && keep)
7557 {
7558 /* Link this CU into read_in_chain. */
7559 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7560 dwarf2_per_objfile->read_in_chain = this_cu;
7561 /* The chain owns it now. */
7562 new_cu.release ();
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 end.
7578
7579 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
7580 linker) then DIE_READER_FUNC will not get called. */
7581
7582 static void
7583 init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
7584 struct abbrev_table *abbrev_table,
7585 int use_existing_cu, int keep,
7586 bool skip_partial,
7587 die_reader_func_ftype *die_reader_func,
7588 void *data)
7589 {
7590 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7591 struct objfile *objfile = dwarf2_per_objfile->objfile;
7592 struct dwarf2_section_info *section = this_cu->section;
7593 bfd *abfd = get_section_bfd_owner (section);
7594 struct dwarf2_cu *cu;
7595 const gdb_byte *begin_info_ptr, *info_ptr;
7596 struct die_reader_specs reader;
7597 struct die_info *comp_unit_die;
7598 int has_children;
7599 struct signatured_type *sig_type = NULL;
7600 struct dwarf2_section_info *abbrev_section;
7601 /* Non-zero if CU currently points to a DWO file and we need to
7602 reread it. When this happens we need to reread the skeleton die
7603 before we can reread the DWO file (this only applies to CUs, not TUs). */
7604 int rereading_dwo_cu = 0;
7605
7606 if (dwarf_die_debug)
7607 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7608 this_cu->is_debug_types ? "type" : "comp",
7609 sect_offset_str (this_cu->sect_off));
7610
7611 if (use_existing_cu)
7612 gdb_assert (keep);
7613
7614 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7615 file (instead of going through the stub), short-circuit all of this. */
7616 if (this_cu->reading_dwo_directly)
7617 {
7618 /* Narrow down the scope of possibilities to have to understand. */
7619 gdb_assert (this_cu->is_debug_types);
7620 gdb_assert (abbrev_table == NULL);
7621 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
7622 die_reader_func, data);
7623 return;
7624 }
7625
7626 /* This is cheap if the section is already read in. */
7627 dwarf2_read_section (objfile, section);
7628
7629 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7630
7631 abbrev_section = get_abbrev_section_for_cu (this_cu);
7632
7633 std::unique_ptr<dwarf2_cu> new_cu;
7634 if (use_existing_cu && this_cu->cu != NULL)
7635 {
7636 cu = this_cu->cu;
7637 /* If this CU is from a DWO file we need to start over, we need to
7638 refetch the attributes from the skeleton CU.
7639 This could be optimized by retrieving those attributes from when we
7640 were here the first time: the previous comp_unit_die was stored in
7641 comp_unit_obstack. But there's no data yet that we need this
7642 optimization. */
7643 if (cu->dwo_unit != NULL)
7644 rereading_dwo_cu = 1;
7645 }
7646 else
7647 {
7648 /* If !use_existing_cu, this_cu->cu must be NULL. */
7649 gdb_assert (this_cu->cu == NULL);
7650 new_cu.reset (new dwarf2_cu (this_cu));
7651 cu = new_cu.get ();
7652 }
7653
7654 /* Get the header. */
7655 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
7656 {
7657 /* We already have the header, there's no need to read it in again. */
7658 info_ptr += to_underlying (cu->header.first_die_cu_offset);
7659 }
7660 else
7661 {
7662 if (this_cu->is_debug_types)
7663 {
7664 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7665 &cu->header, section,
7666 abbrev_section, info_ptr,
7667 rcuh_kind::TYPE);
7668
7669 /* Since per_cu is the first member of struct signatured_type,
7670 we can go from a pointer to one to a pointer to the other. */
7671 sig_type = (struct signatured_type *) this_cu;
7672 gdb_assert (sig_type->signature == cu->header.signature);
7673 gdb_assert (sig_type->type_offset_in_tu
7674 == cu->header.type_cu_offset_in_tu);
7675 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7676
7677 /* LENGTH has not been set yet for type units if we're
7678 using .gdb_index. */
7679 this_cu->length = get_cu_length (&cu->header);
7680
7681 /* Establish the type offset that can be used to lookup the type. */
7682 sig_type->type_offset_in_section =
7683 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
7684
7685 this_cu->dwarf_version = cu->header.version;
7686 }
7687 else
7688 {
7689 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7690 &cu->header, section,
7691 abbrev_section,
7692 info_ptr,
7693 rcuh_kind::COMPILE);
7694
7695 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7696 gdb_assert (this_cu->length == get_cu_length (&cu->header));
7697 this_cu->dwarf_version = cu->header.version;
7698 }
7699 }
7700
7701 /* Skip dummy compilation units. */
7702 if (info_ptr >= begin_info_ptr + this_cu->length
7703 || peek_abbrev_code (abfd, info_ptr) == 0)
7704 return;
7705
7706 /* If we don't have them yet, read the abbrevs for this compilation unit.
7707 And if we need to read them now, make sure they're freed when we're
7708 done (own the table through ABBREV_TABLE_HOLDER). */
7709 abbrev_table_up abbrev_table_holder;
7710 if (abbrev_table != NULL)
7711 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7712 else
7713 {
7714 abbrev_table_holder
7715 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7716 cu->header.abbrev_sect_off);
7717 abbrev_table = abbrev_table_holder.get ();
7718 }
7719
7720 /* Read the top level CU/TU die. */
7721 init_cu_die_reader (&reader, cu, section, NULL, abbrev_table);
7722 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
7723
7724 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
7725 return;
7726
7727 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
7728 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7729 table from the DWO file and pass the ownership over to us. It will be
7730 referenced from READER, so we must make sure to free it after we're done
7731 with READER.
7732
7733 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7734 DWO CU, that this test will fail (the attribute will not be present). */
7735 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7736 abbrev_table_up dwo_abbrev_table;
7737 if (dwo_name != nullptr)
7738 {
7739 struct dwo_unit *dwo_unit;
7740 struct die_info *dwo_comp_unit_die;
7741
7742 if (has_children)
7743 {
7744 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
7745 " has children (offset %s) [in module %s]"),
7746 sect_offset_str (this_cu->sect_off),
7747 bfd_get_filename (abfd));
7748 }
7749 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
7750 if (dwo_unit != NULL)
7751 {
7752 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
7753 comp_unit_die, NULL,
7754 &reader, &info_ptr,
7755 &dwo_comp_unit_die, &has_children,
7756 &dwo_abbrev_table) == 0)
7757 {
7758 /* Dummy die. */
7759 return;
7760 }
7761 comp_unit_die = dwo_comp_unit_die;
7762 }
7763 else
7764 {
7765 /* Yikes, we couldn't find the rest of the DIE, we only have
7766 the stub. A complaint has already been logged. There's
7767 not much more we can do except pass on the stub DIE to
7768 die_reader_func. We don't want to throw an error on bad
7769 debug info. */
7770 }
7771 }
7772
7773 /* All of the above is setup for this call. Yikes. */
7774 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7775
7776 /* Done, clean up. */
7777 if (new_cu != NULL && keep)
7778 {
7779 /* Link this CU into read_in_chain. */
7780 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7781 dwarf2_per_objfile->read_in_chain = this_cu;
7782 /* The chain owns it now. */
7783 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 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
7797 linker) then DIE_READER_FUNC will not get called.
7798
7799 THIS_CU->cu is always freed when done.
7800 This is done in order to not leave THIS_CU->cu in a state where we have
7801 to care whether it refers to the "main" CU or the DWO CU.
7802
7803 When parent_cu is passed, it is used to provide a default value for
7804 str_offsets_base and addr_base from the parent. */
7805
7806 static void
7807 init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
7808 struct dwarf2_cu *parent_cu,
7809 struct dwo_file *dwo_file,
7810 die_reader_func_ftype *die_reader_func,
7811 void *data)
7812 {
7813 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7814 struct objfile *objfile = dwarf2_per_objfile->objfile;
7815 struct dwarf2_section_info *section = this_cu->section;
7816 bfd *abfd = get_section_bfd_owner (section);
7817 struct dwarf2_section_info *abbrev_section;
7818 const gdb_byte *begin_info_ptr, *info_ptr;
7819 struct die_reader_specs reader;
7820 struct die_info *comp_unit_die;
7821 int has_children;
7822
7823 if (dwarf_die_debug)
7824 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7825 this_cu->is_debug_types ? "type" : "comp",
7826 sect_offset_str (this_cu->sect_off));
7827
7828 gdb_assert (this_cu->cu == NULL);
7829
7830 abbrev_section = (dwo_file != NULL
7831 ? &dwo_file->sections.abbrev
7832 : get_abbrev_section_for_cu (this_cu));
7833
7834 /* This is cheap if the section is already read in. */
7835 dwarf2_read_section (objfile, section);
7836
7837 struct dwarf2_cu cu (this_cu);
7838
7839 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7840 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7841 &cu.header, section,
7842 abbrev_section, info_ptr,
7843 (this_cu->is_debug_types
7844 ? rcuh_kind::TYPE
7845 : rcuh_kind::COMPILE));
7846
7847 if (parent_cu != nullptr)
7848 {
7849 cu.str_offsets_base = parent_cu->str_offsets_base;
7850 cu.addr_base = parent_cu->addr_base;
7851 }
7852 this_cu->length = get_cu_length (&cu.header);
7853
7854 /* Skip dummy compilation units. */
7855 if (info_ptr >= begin_info_ptr + this_cu->length
7856 || peek_abbrev_code (abfd, info_ptr) == 0)
7857 return;
7858
7859 abbrev_table_up abbrev_table
7860 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7861 cu.header.abbrev_sect_off);
7862
7863 init_cu_die_reader (&reader, &cu, section, dwo_file, abbrev_table.get ());
7864 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
7865
7866 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7867 }
7868
7869 /* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name
7870 (DW_AT_dwo_name) and does not lookup the specified DWO file.
7871 This cannot be used to read DWO files.
7872
7873 THIS_CU->cu is always freed when done.
7874 This is done in order to not leave THIS_CU->cu in a state where we have
7875 to care whether it refers to the "main" CU or the DWO CU.
7876 We can revisit this if the data shows there's a performance issue. */
7877
7878 static void
7879 init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
7880 die_reader_func_ftype *die_reader_func,
7881 void *data)
7882 {
7883 init_cutu_and_read_dies_no_follow (this_cu, NULL, NULL, die_reader_func, data);
7884 }
7885 \f
7886 /* Type Unit Groups.
7887
7888 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7889 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7890 so that all types coming from the same compilation (.o file) are grouped
7891 together. A future step could be to put the types in the same symtab as
7892 the CU the types ultimately came from. */
7893
7894 static hashval_t
7895 hash_type_unit_group (const void *item)
7896 {
7897 const struct type_unit_group *tu_group
7898 = (const struct type_unit_group *) item;
7899
7900 return hash_stmt_list_entry (&tu_group->hash);
7901 }
7902
7903 static int
7904 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
7905 {
7906 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7907 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
7908
7909 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
7910 }
7911
7912 /* Allocate a hash table for type unit groups. */
7913
7914 static htab_t
7915 allocate_type_unit_groups_table (struct objfile *objfile)
7916 {
7917 return htab_create_alloc_ex (3,
7918 hash_type_unit_group,
7919 eq_type_unit_group,
7920 NULL,
7921 &objfile->objfile_obstack,
7922 hashtab_obstack_allocate,
7923 dummy_obstack_deallocate);
7924 }
7925
7926 /* Type units that don't have DW_AT_stmt_list are grouped into their own
7927 partial symtabs. We combine several TUs per psymtab to not let the size
7928 of any one psymtab grow too big. */
7929 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7930 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
7931
7932 /* Helper routine for get_type_unit_group.
7933 Create the type_unit_group object used to hold one or more TUs. */
7934
7935 static struct type_unit_group *
7936 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
7937 {
7938 struct dwarf2_per_objfile *dwarf2_per_objfile
7939 = cu->per_cu->dwarf2_per_objfile;
7940 struct objfile *objfile = dwarf2_per_objfile->objfile;
7941 struct dwarf2_per_cu_data *per_cu;
7942 struct type_unit_group *tu_group;
7943
7944 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7945 struct type_unit_group);
7946 per_cu = &tu_group->per_cu;
7947 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
7948
7949 if (dwarf2_per_objfile->using_index)
7950 {
7951 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7952 struct dwarf2_per_cu_quick_data);
7953 }
7954 else
7955 {
7956 unsigned int line_offset = to_underlying (line_offset_struct);
7957 struct partial_symtab *pst;
7958 std::string name;
7959
7960 /* Give the symtab a useful name for debug purposes. */
7961 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7962 name = string_printf ("<type_units_%d>",
7963 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7964 else
7965 name = string_printf ("<type_units_at_0x%x>", line_offset);
7966
7967 pst = create_partial_symtab (per_cu, name.c_str ());
7968 pst->anonymous = 1;
7969 }
7970
7971 tu_group->hash.dwo_unit = cu->dwo_unit;
7972 tu_group->hash.line_sect_off = line_offset_struct;
7973
7974 return tu_group;
7975 }
7976
7977 /* Look up the type_unit_group for type unit CU, and create it if necessary.
7978 STMT_LIST is a DW_AT_stmt_list attribute. */
7979
7980 static struct type_unit_group *
7981 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7982 {
7983 struct dwarf2_per_objfile *dwarf2_per_objfile
7984 = cu->per_cu->dwarf2_per_objfile;
7985 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7986 struct type_unit_group *tu_group;
7987 void **slot;
7988 unsigned int line_offset;
7989 struct type_unit_group type_unit_group_for_lookup;
7990
7991 if (dwarf2_per_objfile->type_unit_groups == NULL)
7992 {
7993 dwarf2_per_objfile->type_unit_groups =
7994 allocate_type_unit_groups_table (dwarf2_per_objfile->objfile);
7995 }
7996
7997 /* Do we need to create a new group, or can we use an existing one? */
7998
7999 if (stmt_list)
8000 {
8001 line_offset = DW_UNSND (stmt_list);
8002 ++tu_stats->nr_symtab_sharers;
8003 }
8004 else
8005 {
8006 /* Ugh, no stmt_list. Rare, but we have to handle it.
8007 We can do various things here like create one group per TU or
8008 spread them over multiple groups to split up the expansion work.
8009 To avoid worst case scenarios (too many groups or too large groups)
8010 we, umm, group them in bunches. */
8011 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
8012 | (tu_stats->nr_stmt_less_type_units
8013 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
8014 ++tu_stats->nr_stmt_less_type_units;
8015 }
8016
8017 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
8018 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
8019 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
8020 &type_unit_group_for_lookup, INSERT);
8021 if (*slot != NULL)
8022 {
8023 tu_group = (struct type_unit_group *) *slot;
8024 gdb_assert (tu_group != NULL);
8025 }
8026 else
8027 {
8028 sect_offset line_offset_struct = (sect_offset) line_offset;
8029 tu_group = create_type_unit_group (cu, line_offset_struct);
8030 *slot = tu_group;
8031 ++tu_stats->nr_symtabs;
8032 }
8033
8034 return tu_group;
8035 }
8036 \f
8037 /* Partial symbol tables. */
8038
8039 /* Create a psymtab named NAME and assign it to PER_CU.
8040
8041 The caller must fill in the following details:
8042 dirname, textlow, texthigh. */
8043
8044 static struct partial_symtab *
8045 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
8046 {
8047 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
8048 struct partial_symtab *pst;
8049
8050 pst = start_psymtab_common (objfile, name, 0);
8051
8052 pst->psymtabs_addrmap_supported = 1;
8053
8054 /* This is the glue that links PST into GDB's symbol API. */
8055 pst->read_symtab_private = per_cu;
8056 pst->read_symtab = dwarf2_read_symtab;
8057 per_cu->v.psymtab = pst;
8058
8059 return pst;
8060 }
8061
8062 /* The DATA object passed to process_psymtab_comp_unit_reader has this
8063 type. */
8064
8065 struct process_psymtab_comp_unit_data
8066 {
8067 /* True if we are reading a DW_TAG_partial_unit. */
8068
8069 int want_partial_unit;
8070
8071 /* The "pretend" language that is used if the CU doesn't declare a
8072 language. */
8073
8074 enum language pretend_language;
8075 };
8076
8077 /* die_reader_func for process_psymtab_comp_unit. */
8078
8079 static void
8080 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
8081 const gdb_byte *info_ptr,
8082 struct die_info *comp_unit_die,
8083 int has_children,
8084 void *data)
8085 {
8086 struct dwarf2_cu *cu = reader->cu;
8087 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
8088 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8089 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8090 CORE_ADDR baseaddr;
8091 CORE_ADDR best_lowpc = 0, best_highpc = 0;
8092 struct partial_symtab *pst;
8093 enum pc_bounds_kind cu_bounds_kind;
8094 const char *filename;
8095 struct process_psymtab_comp_unit_data *info
8096 = (struct process_psymtab_comp_unit_data *) data;
8097
8098 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
8099 return;
8100
8101 gdb_assert (! per_cu->is_debug_types);
8102
8103 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
8104
8105 /* Allocate a new partial symbol table structure. */
8106 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
8107 if (filename == NULL)
8108 filename = "";
8109
8110 pst = create_partial_symtab (per_cu, filename);
8111
8112 /* This must be done before calling dwarf2_build_include_psymtabs. */
8113 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
8114
8115 baseaddr = objfile->section_offsets[SECT_OFF_TEXT (objfile)];
8116
8117 dwarf2_find_base_address (comp_unit_die, cu);
8118
8119 /* Possibly set the default values of LOWPC and HIGHPC from
8120 `DW_AT_ranges'. */
8121 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
8122 &best_highpc, cu, pst);
8123 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
8124 {
8125 CORE_ADDR low
8126 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
8127 - baseaddr);
8128 CORE_ADDR high
8129 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
8130 - baseaddr - 1);
8131 /* Store the contiguous range if it is not empty; it can be
8132 empty for CUs with no code. */
8133 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8134 low, high, pst);
8135 }
8136
8137 /* Check if comp unit has_children.
8138 If so, read the rest of the partial symbols from this comp unit.
8139 If not, there's no more debug_info for this comp unit. */
8140 if (has_children)
8141 {
8142 struct partial_die_info *first_die;
8143 CORE_ADDR lowpc, highpc;
8144
8145 lowpc = ((CORE_ADDR) -1);
8146 highpc = ((CORE_ADDR) 0);
8147
8148 first_die = load_partial_dies (reader, info_ptr, 1);
8149
8150 scan_partial_symbols (first_die, &lowpc, &highpc,
8151 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
8152
8153 /* If we didn't find a lowpc, set it to highpc to avoid
8154 complaints from `maint check'. */
8155 if (lowpc == ((CORE_ADDR) -1))
8156 lowpc = highpc;
8157
8158 /* If the compilation unit didn't have an explicit address range,
8159 then use the information extracted from its child dies. */
8160 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
8161 {
8162 best_lowpc = lowpc;
8163 best_highpc = highpc;
8164 }
8165 }
8166 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
8167 best_lowpc + baseaddr)
8168 - baseaddr);
8169 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
8170 best_highpc + baseaddr)
8171 - baseaddr);
8172
8173 end_psymtab_common (objfile, pst);
8174
8175 if (!cu->per_cu->imported_symtabs_empty ())
8176 {
8177 int i;
8178 int len = cu->per_cu->imported_symtabs_size ();
8179
8180 /* Fill in 'dependencies' here; we fill in 'users' in a
8181 post-pass. */
8182 pst->number_of_dependencies = len;
8183 pst->dependencies
8184 = objfile->partial_symtabs->allocate_dependencies (len);
8185 for (i = 0; i < len; ++i)
8186 {
8187 pst->dependencies[i]
8188 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
8189 }
8190
8191 cu->per_cu->imported_symtabs_free ();
8192 }
8193
8194 /* Get the list of files included in the current compilation unit,
8195 and build a psymtab for each of them. */
8196 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
8197
8198 if (dwarf_read_debug)
8199 fprintf_unfiltered (gdb_stdlog,
8200 "Psymtab for %s unit @%s: %s - %s"
8201 ", %d global, %d static syms\n",
8202 per_cu->is_debug_types ? "type" : "comp",
8203 sect_offset_str (per_cu->sect_off),
8204 paddress (gdbarch, pst->text_low (objfile)),
8205 paddress (gdbarch, pst->text_high (objfile)),
8206 pst->n_global_syms, pst->n_static_syms);
8207 }
8208
8209 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8210 Process compilation unit THIS_CU for a psymtab. */
8211
8212 static void
8213 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
8214 int want_partial_unit,
8215 enum language pretend_language)
8216 {
8217 /* If this compilation unit was already read in, free the
8218 cached copy in order to read it in again. This is
8219 necessary because we skipped some symbols when we first
8220 read in the compilation unit (see load_partial_dies).
8221 This problem could be avoided, but the benefit is unclear. */
8222 if (this_cu->cu != NULL)
8223 free_one_cached_comp_unit (this_cu);
8224
8225 if (this_cu->is_debug_types)
8226 init_cutu_and_read_dies (this_cu, NULL, 0, 0, false,
8227 build_type_psymtabs_reader, NULL);
8228 else
8229 {
8230 process_psymtab_comp_unit_data info;
8231 info.want_partial_unit = want_partial_unit;
8232 info.pretend_language = pretend_language;
8233 init_cutu_and_read_dies (this_cu, NULL, 0, 0, false,
8234 process_psymtab_comp_unit_reader, &info);
8235 }
8236
8237 /* Age out any secondary CUs. */
8238 age_cached_comp_units (this_cu->dwarf2_per_objfile);
8239 }
8240
8241 /* Reader function for build_type_psymtabs. */
8242
8243 static void
8244 build_type_psymtabs_reader (const struct die_reader_specs *reader,
8245 const gdb_byte *info_ptr,
8246 struct die_info *type_unit_die,
8247 int has_children,
8248 void *data)
8249 {
8250 struct dwarf2_per_objfile *dwarf2_per_objfile
8251 = reader->cu->per_cu->dwarf2_per_objfile;
8252 struct objfile *objfile = dwarf2_per_objfile->objfile;
8253 struct dwarf2_cu *cu = reader->cu;
8254 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8255 struct signatured_type *sig_type;
8256 struct type_unit_group *tu_group;
8257 struct attribute *attr;
8258 struct partial_die_info *first_die;
8259 CORE_ADDR lowpc, highpc;
8260 struct partial_symtab *pst;
8261
8262 gdb_assert (data == NULL);
8263 gdb_assert (per_cu->is_debug_types);
8264 sig_type = (struct signatured_type *) per_cu;
8265
8266 if (! has_children)
8267 return;
8268
8269 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
8270 tu_group = get_type_unit_group (cu, attr);
8271
8272 if (tu_group->tus == nullptr)
8273 tu_group->tus = new std::vector<signatured_type *>;
8274 tu_group->tus->push_back (sig_type);
8275
8276 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
8277 pst = create_partial_symtab (per_cu, "");
8278 pst->anonymous = 1;
8279
8280 first_die = load_partial_dies (reader, info_ptr, 1);
8281
8282 lowpc = (CORE_ADDR) -1;
8283 highpc = (CORE_ADDR) 0;
8284 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
8285
8286 end_psymtab_common (objfile, pst);
8287 }
8288
8289 /* Struct used to sort TUs by their abbreviation table offset. */
8290
8291 struct tu_abbrev_offset
8292 {
8293 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
8294 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
8295 {}
8296
8297 signatured_type *sig_type;
8298 sect_offset abbrev_offset;
8299 };
8300
8301 /* Helper routine for build_type_psymtabs_1, passed to std::sort. */
8302
8303 static bool
8304 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
8305 const struct tu_abbrev_offset &b)
8306 {
8307 return a.abbrev_offset < b.abbrev_offset;
8308 }
8309
8310 /* Efficiently read all the type units.
8311 This does the bulk of the work for build_type_psymtabs.
8312
8313 The efficiency is because we sort TUs by the abbrev table they use and
8314 only read each abbrev table once. In one program there are 200K TUs
8315 sharing 8K abbrev tables.
8316
8317 The main purpose of this function is to support building the
8318 dwarf2_per_objfile->type_unit_groups table.
8319 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
8320 can collapse the search space by grouping them by stmt_list.
8321 The savings can be significant, in the same program from above the 200K TUs
8322 share 8K stmt_list tables.
8323
8324 FUNC is expected to call get_type_unit_group, which will create the
8325 struct type_unit_group if necessary and add it to
8326 dwarf2_per_objfile->type_unit_groups. */
8327
8328 static void
8329 build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
8330 {
8331 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8332 abbrev_table_up abbrev_table;
8333 sect_offset abbrev_offset;
8334
8335 /* It's up to the caller to not call us multiple times. */
8336 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
8337
8338 if (dwarf2_per_objfile->all_type_units.empty ())
8339 return;
8340
8341 /* TUs typically share abbrev tables, and there can be way more TUs than
8342 abbrev tables. Sort by abbrev table to reduce the number of times we
8343 read each abbrev table in.
8344 Alternatives are to punt or to maintain a cache of abbrev tables.
8345 This is simpler and efficient enough for now.
8346
8347 Later we group TUs by their DW_AT_stmt_list value (as this defines the
8348 symtab to use). Typically TUs with the same abbrev offset have the same
8349 stmt_list value too so in practice this should work well.
8350
8351 The basic algorithm here is:
8352
8353 sort TUs by abbrev table
8354 for each TU with same abbrev table:
8355 read abbrev table if first user
8356 read TU top level DIE
8357 [IWBN if DWO skeletons had DW_AT_stmt_list]
8358 call FUNC */
8359
8360 if (dwarf_read_debug)
8361 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
8362
8363 /* Sort in a separate table to maintain the order of all_type_units
8364 for .gdb_index: TU indices directly index all_type_units. */
8365 std::vector<tu_abbrev_offset> sorted_by_abbrev;
8366 sorted_by_abbrev.reserve (dwarf2_per_objfile->all_type_units.size ());
8367
8368 for (signatured_type *sig_type : dwarf2_per_objfile->all_type_units)
8369 sorted_by_abbrev.emplace_back
8370 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
8371 sig_type->per_cu.section,
8372 sig_type->per_cu.sect_off));
8373
8374 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
8375 sort_tu_by_abbrev_offset);
8376
8377 abbrev_offset = (sect_offset) ~(unsigned) 0;
8378
8379 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
8380 {
8381 /* Switch to the next abbrev table if necessary. */
8382 if (abbrev_table == NULL
8383 || tu.abbrev_offset != abbrev_offset)
8384 {
8385 abbrev_offset = tu.abbrev_offset;
8386 abbrev_table =
8387 abbrev_table_read_table (dwarf2_per_objfile,
8388 &dwarf2_per_objfile->abbrev,
8389 abbrev_offset);
8390 ++tu_stats->nr_uniq_abbrev_tables;
8391 }
8392
8393 init_cutu_and_read_dies (&tu.sig_type->per_cu, abbrev_table.get (),
8394 0, 0, false, build_type_psymtabs_reader, NULL);
8395 }
8396 }
8397
8398 /* Print collected type unit statistics. */
8399
8400 static void
8401 print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
8402 {
8403 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8404
8405 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
8406 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
8407 dwarf2_per_objfile->all_type_units.size ());
8408 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
8409 tu_stats->nr_uniq_abbrev_tables);
8410 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
8411 tu_stats->nr_symtabs);
8412 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
8413 tu_stats->nr_symtab_sharers);
8414 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
8415 tu_stats->nr_stmt_less_type_units);
8416 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
8417 tu_stats->nr_all_type_units_reallocs);
8418 }
8419
8420 /* Traversal function for build_type_psymtabs. */
8421
8422 static int
8423 build_type_psymtab_dependencies (void **slot, void *info)
8424 {
8425 struct dwarf2_per_objfile *dwarf2_per_objfile
8426 = (struct dwarf2_per_objfile *) info;
8427 struct objfile *objfile = dwarf2_per_objfile->objfile;
8428 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
8429 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
8430 struct partial_symtab *pst = per_cu->v.psymtab;
8431 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
8432 int i;
8433
8434 gdb_assert (len > 0);
8435 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
8436
8437 pst->number_of_dependencies = len;
8438 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
8439 for (i = 0; i < len; ++i)
8440 {
8441 struct signatured_type *iter = tu_group->tus->at (i);
8442 gdb_assert (iter->per_cu.is_debug_types);
8443 pst->dependencies[i] = iter->per_cu.v.psymtab;
8444 iter->type_unit_group = tu_group;
8445 }
8446
8447 delete tu_group->tus;
8448 tu_group->tus = nullptr;
8449
8450 return 1;
8451 }
8452
8453 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8454 Build partial symbol tables for the .debug_types comp-units. */
8455
8456 static void
8457 build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
8458 {
8459 if (! create_all_type_units (dwarf2_per_objfile))
8460 return;
8461
8462 build_type_psymtabs_1 (dwarf2_per_objfile);
8463 }
8464
8465 /* Traversal function for process_skeletonless_type_unit.
8466 Read a TU in a DWO file and build partial symbols for it. */
8467
8468 static int
8469 process_skeletonless_type_unit (void **slot, void *info)
8470 {
8471 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
8472 struct dwarf2_per_objfile *dwarf2_per_objfile
8473 = (struct dwarf2_per_objfile *) info;
8474 struct signatured_type find_entry, *entry;
8475
8476 /* If this TU doesn't exist in the global table, add it and read it in. */
8477
8478 if (dwarf2_per_objfile->signatured_types == NULL)
8479 {
8480 dwarf2_per_objfile->signatured_types
8481 = allocate_signatured_type_table (dwarf2_per_objfile->objfile);
8482 }
8483
8484 find_entry.signature = dwo_unit->signature;
8485 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
8486 INSERT);
8487 /* If we've already seen this type there's nothing to do. What's happening
8488 is we're doing our own version of comdat-folding here. */
8489 if (*slot != NULL)
8490 return 1;
8491
8492 /* This does the job that create_all_type_units would have done for
8493 this TU. */
8494 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
8495 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
8496 *slot = entry;
8497
8498 /* This does the job that build_type_psymtabs_1 would have done. */
8499 init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0, false,
8500 build_type_psymtabs_reader, NULL);
8501
8502 return 1;
8503 }
8504
8505 /* Traversal function for process_skeletonless_type_units. */
8506
8507 static int
8508 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8509 {
8510 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8511
8512 if (dwo_file->tus != NULL)
8513 {
8514 htab_traverse_noresize (dwo_file->tus,
8515 process_skeletonless_type_unit, info);
8516 }
8517
8518 return 1;
8519 }
8520
8521 /* Scan all TUs of DWO files, verifying we've processed them.
8522 This is needed in case a TU was emitted without its skeleton.
8523 Note: This can't be done until we know what all the DWO files are. */
8524
8525 static void
8526 process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
8527 {
8528 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
8529 if (get_dwp_file (dwarf2_per_objfile) == NULL
8530 && dwarf2_per_objfile->dwo_files != NULL)
8531 {
8532 htab_traverse_noresize (dwarf2_per_objfile->dwo_files.get (),
8533 process_dwo_file_for_skeletonless_type_units,
8534 dwarf2_per_objfile);
8535 }
8536 }
8537
8538 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
8539
8540 static void
8541 set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
8542 {
8543 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
8544 {
8545 struct partial_symtab *pst = per_cu->v.psymtab;
8546
8547 if (pst == NULL)
8548 continue;
8549
8550 for (int j = 0; j < pst->number_of_dependencies; ++j)
8551 {
8552 /* Set the 'user' field only if it is not already set. */
8553 if (pst->dependencies[j]->user == NULL)
8554 pst->dependencies[j]->user = pst;
8555 }
8556 }
8557 }
8558
8559 /* Build the partial symbol table by doing a quick pass through the
8560 .debug_info and .debug_abbrev sections. */
8561
8562 static void
8563 dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
8564 {
8565 struct objfile *objfile = dwarf2_per_objfile->objfile;
8566
8567 if (dwarf_read_debug)
8568 {
8569 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
8570 objfile_name (objfile));
8571 }
8572
8573 dwarf2_per_objfile->reading_partial_symbols = 1;
8574
8575 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
8576
8577 /* Any cached compilation units will be linked by the per-objfile
8578 read_in_chain. Make sure to free them when we're done. */
8579 free_cached_comp_units freer (dwarf2_per_objfile);
8580
8581 build_type_psymtabs (dwarf2_per_objfile);
8582
8583 create_all_comp_units (dwarf2_per_objfile);
8584
8585 /* Create a temporary address map on a temporary obstack. We later
8586 copy this to the final obstack. */
8587 auto_obstack temp_obstack;
8588
8589 scoped_restore save_psymtabs_addrmap
8590 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
8591 addrmap_create_mutable (&temp_obstack));
8592
8593 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
8594 process_psymtab_comp_unit (per_cu, 0, language_minimal);
8595
8596 /* This has to wait until we read the CUs, we need the list of DWOs. */
8597 process_skeletonless_type_units (dwarf2_per_objfile);
8598
8599 /* Now that all TUs have been processed we can fill in the dependencies. */
8600 if (dwarf2_per_objfile->type_unit_groups != NULL)
8601 {
8602 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
8603 build_type_psymtab_dependencies, dwarf2_per_objfile);
8604 }
8605
8606 if (dwarf_read_debug)
8607 print_tu_stats (dwarf2_per_objfile);
8608
8609 set_partial_user (dwarf2_per_objfile);
8610
8611 objfile->partial_symtabs->psymtabs_addrmap
8612 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
8613 objfile->partial_symtabs->obstack ());
8614 /* At this point we want to keep the address map. */
8615 save_psymtabs_addrmap.release ();
8616
8617 if (dwarf_read_debug)
8618 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
8619 objfile_name (objfile));
8620 }
8621
8622 /* die_reader_func for load_partial_comp_unit. */
8623
8624 static void
8625 load_partial_comp_unit_reader (const struct die_reader_specs *reader,
8626 const gdb_byte *info_ptr,
8627 struct die_info *comp_unit_die,
8628 int has_children,
8629 void *data)
8630 {
8631 struct dwarf2_cu *cu = reader->cu;
8632
8633 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
8634
8635 /* Check if comp unit has_children.
8636 If so, read the rest of the partial symbols from this comp unit.
8637 If not, there's no more debug_info for this comp unit. */
8638 if (has_children)
8639 load_partial_dies (reader, info_ptr, 0);
8640 }
8641
8642 /* Load the partial DIEs for a secondary CU into memory.
8643 This is also used when rereading a primary CU with load_all_dies. */
8644
8645 static void
8646 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
8647 {
8648 init_cutu_and_read_dies (this_cu, NULL, 1, 1, false,
8649 load_partial_comp_unit_reader, NULL);
8650 }
8651
8652 static void
8653 read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
8654 struct dwarf2_section_info *section,
8655 struct dwarf2_section_info *abbrev_section,
8656 unsigned int is_dwz)
8657 {
8658 const gdb_byte *info_ptr;
8659 struct objfile *objfile = dwarf2_per_objfile->objfile;
8660
8661 if (dwarf_read_debug)
8662 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
8663 get_section_name (section),
8664 get_section_file_name (section));
8665
8666 dwarf2_read_section (objfile, section);
8667
8668 info_ptr = section->buffer;
8669
8670 while (info_ptr < section->buffer + section->size)
8671 {
8672 struct dwarf2_per_cu_data *this_cu;
8673
8674 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
8675
8676 comp_unit_head cu_header;
8677 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
8678 abbrev_section, info_ptr,
8679 rcuh_kind::COMPILE);
8680
8681 /* Save the compilation unit for later lookup. */
8682 if (cu_header.unit_type != DW_UT_type)
8683 {
8684 this_cu = XOBNEW (&objfile->objfile_obstack,
8685 struct dwarf2_per_cu_data);
8686 memset (this_cu, 0, sizeof (*this_cu));
8687 }
8688 else
8689 {
8690 auto sig_type = XOBNEW (&objfile->objfile_obstack,
8691 struct signatured_type);
8692 memset (sig_type, 0, sizeof (*sig_type));
8693 sig_type->signature = cu_header.signature;
8694 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8695 this_cu = &sig_type->per_cu;
8696 }
8697 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
8698 this_cu->sect_off = sect_off;
8699 this_cu->length = cu_header.length + cu_header.initial_length_size;
8700 this_cu->is_dwz = is_dwz;
8701 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8702 this_cu->section = section;
8703
8704 dwarf2_per_objfile->all_comp_units.push_back (this_cu);
8705
8706 info_ptr = info_ptr + this_cu->length;
8707 }
8708 }
8709
8710 /* Create a list of all compilation units in OBJFILE.
8711 This is only done for -readnow and building partial symtabs. */
8712
8713 static void
8714 create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
8715 {
8716 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
8717 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
8718 &dwarf2_per_objfile->abbrev, 0);
8719
8720 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
8721 if (dwz != NULL)
8722 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
8723 1);
8724 }
8725
8726 /* Process all loaded DIEs for compilation unit CU, starting at
8727 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
8728 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
8729 DW_AT_ranges). See the comments of add_partial_subprogram on how
8730 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
8731
8732 static void
8733 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
8734 CORE_ADDR *highpc, int set_addrmap,
8735 struct dwarf2_cu *cu)
8736 {
8737 struct partial_die_info *pdi;
8738
8739 /* Now, march along the PDI's, descending into ones which have
8740 interesting children but skipping the children of the other ones,
8741 until we reach the end of the compilation unit. */
8742
8743 pdi = first_die;
8744
8745 while (pdi != NULL)
8746 {
8747 pdi->fixup (cu);
8748
8749 /* Anonymous namespaces or modules have no name but have interesting
8750 children, so we need to look at them. Ditto for anonymous
8751 enums. */
8752
8753 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
8754 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
8755 || pdi->tag == DW_TAG_imported_unit
8756 || pdi->tag == DW_TAG_inlined_subroutine)
8757 {
8758 switch (pdi->tag)
8759 {
8760 case DW_TAG_subprogram:
8761 case DW_TAG_inlined_subroutine:
8762 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8763 break;
8764 case DW_TAG_constant:
8765 case DW_TAG_variable:
8766 case DW_TAG_typedef:
8767 case DW_TAG_union_type:
8768 if (!pdi->is_declaration)
8769 {
8770 add_partial_symbol (pdi, cu);
8771 }
8772 break;
8773 case DW_TAG_class_type:
8774 case DW_TAG_interface_type:
8775 case DW_TAG_structure_type:
8776 if (!pdi->is_declaration)
8777 {
8778 add_partial_symbol (pdi, cu);
8779 }
8780 if ((cu->language == language_rust
8781 || cu->language == language_cplus) && pdi->has_children)
8782 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8783 set_addrmap, cu);
8784 break;
8785 case DW_TAG_enumeration_type:
8786 if (!pdi->is_declaration)
8787 add_partial_enumeration (pdi, cu);
8788 break;
8789 case DW_TAG_base_type:
8790 case DW_TAG_subrange_type:
8791 /* File scope base type definitions are added to the partial
8792 symbol table. */
8793 add_partial_symbol (pdi, cu);
8794 break;
8795 case DW_TAG_namespace:
8796 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
8797 break;
8798 case DW_TAG_module:
8799 if (!pdi->is_declaration)
8800 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
8801 break;
8802 case DW_TAG_imported_unit:
8803 {
8804 struct dwarf2_per_cu_data *per_cu;
8805
8806 /* For now we don't handle imported units in type units. */
8807 if (cu->per_cu->is_debug_types)
8808 {
8809 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8810 " supported in type units [in module %s]"),
8811 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
8812 }
8813
8814 per_cu = dwarf2_find_containing_comp_unit
8815 (pdi->d.sect_off, pdi->is_dwz,
8816 cu->per_cu->dwarf2_per_objfile);
8817
8818 /* Go read the partial unit, if needed. */
8819 if (per_cu->v.psymtab == NULL)
8820 process_psymtab_comp_unit (per_cu, 1, cu->language);
8821
8822 cu->per_cu->imported_symtabs_push (per_cu);
8823 }
8824 break;
8825 case DW_TAG_imported_declaration:
8826 add_partial_symbol (pdi, cu);
8827 break;
8828 default:
8829 break;
8830 }
8831 }
8832
8833 /* If the die has a sibling, skip to the sibling. */
8834
8835 pdi = pdi->die_sibling;
8836 }
8837 }
8838
8839 /* Functions used to compute the fully scoped name of a partial DIE.
8840
8841 Normally, this is simple. For C++, the parent DIE's fully scoped
8842 name is concatenated with "::" and the partial DIE's name.
8843 Enumerators are an exception; they use the scope of their parent
8844 enumeration type, i.e. the name of the enumeration type is not
8845 prepended to the enumerator.
8846
8847 There are two complexities. One is DW_AT_specification; in this
8848 case "parent" means the parent of the target of the specification,
8849 instead of the direct parent of the DIE. The other is compilers
8850 which do not emit DW_TAG_namespace; in this case we try to guess
8851 the fully qualified name of structure types from their members'
8852 linkage names. This must be done using the DIE's children rather
8853 than the children of any DW_AT_specification target. We only need
8854 to do this for structures at the top level, i.e. if the target of
8855 any DW_AT_specification (if any; otherwise the DIE itself) does not
8856 have a parent. */
8857
8858 /* Compute the scope prefix associated with PDI's parent, in
8859 compilation unit CU. The result will be allocated on CU's
8860 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8861 field. NULL is returned if no prefix is necessary. */
8862 static const char *
8863 partial_die_parent_scope (struct partial_die_info *pdi,
8864 struct dwarf2_cu *cu)
8865 {
8866 const char *grandparent_scope;
8867 struct partial_die_info *parent, *real_pdi;
8868
8869 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8870 then this means the parent of the specification DIE. */
8871
8872 real_pdi = pdi;
8873 while (real_pdi->has_specification)
8874 {
8875 auto res = find_partial_die (real_pdi->spec_offset,
8876 real_pdi->spec_is_dwz, cu);
8877 real_pdi = res.pdi;
8878 cu = res.cu;
8879 }
8880
8881 parent = real_pdi->die_parent;
8882 if (parent == NULL)
8883 return NULL;
8884
8885 if (parent->scope_set)
8886 return parent->scope;
8887
8888 parent->fixup (cu);
8889
8890 grandparent_scope = partial_die_parent_scope (parent, cu);
8891
8892 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8893 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8894 Work around this problem here. */
8895 if (cu->language == language_cplus
8896 && parent->tag == DW_TAG_namespace
8897 && strcmp (parent->name, "::") == 0
8898 && grandparent_scope == NULL)
8899 {
8900 parent->scope = NULL;
8901 parent->scope_set = 1;
8902 return NULL;
8903 }
8904
8905 /* Nested subroutines in Fortran get a prefix. */
8906 if (pdi->tag == DW_TAG_enumerator)
8907 /* Enumerators should not get the name of the enumeration as a prefix. */
8908 parent->scope = grandparent_scope;
8909 else if (parent->tag == DW_TAG_namespace
8910 || parent->tag == DW_TAG_module
8911 || parent->tag == DW_TAG_structure_type
8912 || parent->tag == DW_TAG_class_type
8913 || parent->tag == DW_TAG_interface_type
8914 || parent->tag == DW_TAG_union_type
8915 || parent->tag == DW_TAG_enumeration_type
8916 || (cu->language == language_fortran
8917 && parent->tag == DW_TAG_subprogram
8918 && pdi->tag == DW_TAG_subprogram))
8919 {
8920 if (grandparent_scope == NULL)
8921 parent->scope = parent->name;
8922 else
8923 parent->scope = typename_concat (&cu->comp_unit_obstack,
8924 grandparent_scope,
8925 parent->name, 0, cu);
8926 }
8927 else
8928 {
8929 /* FIXME drow/2004-04-01: What should we be doing with
8930 function-local names? For partial symbols, we should probably be
8931 ignoring them. */
8932 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8933 dwarf_tag_name (parent->tag),
8934 sect_offset_str (pdi->sect_off));
8935 parent->scope = grandparent_scope;
8936 }
8937
8938 parent->scope_set = 1;
8939 return parent->scope;
8940 }
8941
8942 /* Return the fully scoped name associated with PDI, from compilation unit
8943 CU. The result will be allocated with malloc. */
8944
8945 static gdb::unique_xmalloc_ptr<char>
8946 partial_die_full_name (struct partial_die_info *pdi,
8947 struct dwarf2_cu *cu)
8948 {
8949 const char *parent_scope;
8950
8951 /* If this is a template instantiation, we can not work out the
8952 template arguments from partial DIEs. So, unfortunately, we have
8953 to go through the full DIEs. At least any work we do building
8954 types here will be reused if full symbols are loaded later. */
8955 if (pdi->has_template_arguments)
8956 {
8957 pdi->fixup (cu);
8958
8959 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8960 {
8961 struct die_info *die;
8962 struct attribute attr;
8963 struct dwarf2_cu *ref_cu = cu;
8964
8965 /* DW_FORM_ref_addr is using section offset. */
8966 attr.name = (enum dwarf_attribute) 0;
8967 attr.form = DW_FORM_ref_addr;
8968 attr.u.unsnd = to_underlying (pdi->sect_off);
8969 die = follow_die_ref (NULL, &attr, &ref_cu);
8970
8971 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8972 }
8973 }
8974
8975 parent_scope = partial_die_parent_scope (pdi, cu);
8976 if (parent_scope == NULL)
8977 return NULL;
8978 else
8979 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8980 pdi->name, 0, cu));
8981 }
8982
8983 static void
8984 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8985 {
8986 struct dwarf2_per_objfile *dwarf2_per_objfile
8987 = cu->per_cu->dwarf2_per_objfile;
8988 struct objfile *objfile = dwarf2_per_objfile->objfile;
8989 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8990 CORE_ADDR addr = 0;
8991 const char *actual_name = NULL;
8992 CORE_ADDR baseaddr;
8993
8994 baseaddr = objfile->section_offsets[SECT_OFF_TEXT (objfile)];
8995
8996 gdb::unique_xmalloc_ptr<char> built_actual_name
8997 = partial_die_full_name (pdi, cu);
8998 if (built_actual_name != NULL)
8999 actual_name = built_actual_name.get ();
9000
9001 if (actual_name == NULL)
9002 actual_name = pdi->name;
9003
9004 switch (pdi->tag)
9005 {
9006 case DW_TAG_inlined_subroutine:
9007 case DW_TAG_subprogram:
9008 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
9009 - baseaddr);
9010 if (pdi->is_external
9011 || cu->language == language_ada
9012 || (cu->language == language_fortran
9013 && pdi->die_parent != NULL
9014 && pdi->die_parent->tag == DW_TAG_subprogram))
9015 {
9016 /* Normally, only "external" DIEs are part of the global scope.
9017 But in Ada and Fortran, we want to be able to access nested
9018 procedures globally. So all Ada and Fortran subprograms are
9019 stored in the global scope. */
9020 add_psymbol_to_list (actual_name,
9021 built_actual_name != NULL,
9022 VAR_DOMAIN, LOC_BLOCK,
9023 SECT_OFF_TEXT (objfile),
9024 psymbol_placement::GLOBAL,
9025 addr,
9026 cu->language, objfile);
9027 }
9028 else
9029 {
9030 add_psymbol_to_list (actual_name,
9031 built_actual_name != NULL,
9032 VAR_DOMAIN, LOC_BLOCK,
9033 SECT_OFF_TEXT (objfile),
9034 psymbol_placement::STATIC,
9035 addr, cu->language, objfile);
9036 }
9037
9038 if (pdi->main_subprogram && actual_name != NULL)
9039 set_objfile_main_name (objfile, actual_name, cu->language);
9040 break;
9041 case DW_TAG_constant:
9042 add_psymbol_to_list (actual_name,
9043 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
9044 -1, (pdi->is_external
9045 ? psymbol_placement::GLOBAL
9046 : psymbol_placement::STATIC),
9047 0, cu->language, objfile);
9048 break;
9049 case DW_TAG_variable:
9050 if (pdi->d.locdesc)
9051 addr = decode_locdesc (pdi->d.locdesc, cu);
9052
9053 if (pdi->d.locdesc
9054 && addr == 0
9055 && !dwarf2_per_objfile->has_section_at_zero)
9056 {
9057 /* A global or static variable may also have been stripped
9058 out by the linker if unused, in which case its address
9059 will be nullified; do not add such variables into partial
9060 symbol table then. */
9061 }
9062 else if (pdi->is_external)
9063 {
9064 /* Global Variable.
9065 Don't enter into the minimal symbol tables as there is
9066 a minimal symbol table entry from the ELF symbols already.
9067 Enter into partial symbol table if it has a location
9068 descriptor or a type.
9069 If the location descriptor is missing, new_symbol will create
9070 a LOC_UNRESOLVED symbol, the address of the variable will then
9071 be determined from the minimal symbol table whenever the variable
9072 is referenced.
9073 The address for the partial symbol table entry is not
9074 used by GDB, but it comes in handy for debugging partial symbol
9075 table building. */
9076
9077 if (pdi->d.locdesc || pdi->has_type)
9078 add_psymbol_to_list (actual_name,
9079 built_actual_name != NULL,
9080 VAR_DOMAIN, LOC_STATIC,
9081 SECT_OFF_TEXT (objfile),
9082 psymbol_placement::GLOBAL,
9083 addr, cu->language, objfile);
9084 }
9085 else
9086 {
9087 int has_loc = pdi->d.locdesc != NULL;
9088
9089 /* Static Variable. Skip symbols whose value we cannot know (those
9090 without location descriptors or constant values). */
9091 if (!has_loc && !pdi->has_const_value)
9092 return;
9093
9094 add_psymbol_to_list (actual_name,
9095 built_actual_name != NULL,
9096 VAR_DOMAIN, LOC_STATIC,
9097 SECT_OFF_TEXT (objfile),
9098 psymbol_placement::STATIC,
9099 has_loc ? addr : 0,
9100 cu->language, objfile);
9101 }
9102 break;
9103 case DW_TAG_typedef:
9104 case DW_TAG_base_type:
9105 case DW_TAG_subrange_type:
9106 add_psymbol_to_list (actual_name,
9107 built_actual_name != NULL,
9108 VAR_DOMAIN, LOC_TYPEDEF, -1,
9109 psymbol_placement::STATIC,
9110 0, cu->language, objfile);
9111 break;
9112 case DW_TAG_imported_declaration:
9113 case DW_TAG_namespace:
9114 add_psymbol_to_list (actual_name,
9115 built_actual_name != NULL,
9116 VAR_DOMAIN, LOC_TYPEDEF, -1,
9117 psymbol_placement::GLOBAL,
9118 0, cu->language, objfile);
9119 break;
9120 case DW_TAG_module:
9121 /* With Fortran 77 there might be a "BLOCK DATA" module
9122 available without any name. If so, we skip the module as it
9123 doesn't bring any value. */
9124 if (actual_name != nullptr)
9125 add_psymbol_to_list (actual_name,
9126 built_actual_name != NULL,
9127 MODULE_DOMAIN, LOC_TYPEDEF, -1,
9128 psymbol_placement::GLOBAL,
9129 0, cu->language, objfile);
9130 break;
9131 case DW_TAG_class_type:
9132 case DW_TAG_interface_type:
9133 case DW_TAG_structure_type:
9134 case DW_TAG_union_type:
9135 case DW_TAG_enumeration_type:
9136 /* Skip external references. The DWARF standard says in the section
9137 about "Structure, Union, and Class Type Entries": "An incomplete
9138 structure, union or class type is represented by a structure,
9139 union or class entry that does not have a byte size attribute
9140 and that has a DW_AT_declaration attribute." */
9141 if (!pdi->has_byte_size && pdi->is_declaration)
9142 return;
9143
9144 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
9145 static vs. global. */
9146 add_psymbol_to_list (actual_name,
9147 built_actual_name != NULL,
9148 STRUCT_DOMAIN, LOC_TYPEDEF, -1,
9149 cu->language == language_cplus
9150 ? psymbol_placement::GLOBAL
9151 : psymbol_placement::STATIC,
9152 0, cu->language, objfile);
9153
9154 break;
9155 case DW_TAG_enumerator:
9156 add_psymbol_to_list (actual_name,
9157 built_actual_name != NULL,
9158 VAR_DOMAIN, LOC_CONST, -1,
9159 cu->language == language_cplus
9160 ? psymbol_placement::GLOBAL
9161 : psymbol_placement::STATIC,
9162 0, cu->language, objfile);
9163 break;
9164 default:
9165 break;
9166 }
9167 }
9168
9169 /* Read a partial die corresponding to a namespace; also, add a symbol
9170 corresponding to that namespace to the symbol table. NAMESPACE is
9171 the name of the enclosing namespace. */
9172
9173 static void
9174 add_partial_namespace (struct partial_die_info *pdi,
9175 CORE_ADDR *lowpc, CORE_ADDR *highpc,
9176 int set_addrmap, struct dwarf2_cu *cu)
9177 {
9178 /* Add a symbol for the namespace. */
9179
9180 add_partial_symbol (pdi, cu);
9181
9182 /* Now scan partial symbols in that namespace. */
9183
9184 if (pdi->has_children)
9185 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
9186 }
9187
9188 /* Read a partial die corresponding to a Fortran module. */
9189
9190 static void
9191 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
9192 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
9193 {
9194 /* Add a symbol for the namespace. */
9195
9196 add_partial_symbol (pdi, cu);
9197
9198 /* Now scan partial symbols in that module. */
9199
9200 if (pdi->has_children)
9201 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
9202 }
9203
9204 /* Read a partial die corresponding to a subprogram or an inlined
9205 subprogram and create a partial symbol for that subprogram.
9206 When the CU language allows it, this routine also defines a partial
9207 symbol for each nested subprogram that this subprogram contains.
9208 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
9209 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
9210
9211 PDI may also be a lexical block, in which case we simply search
9212 recursively for subprograms defined inside that lexical block.
9213 Again, this is only performed when the CU language allows this
9214 type of definitions. */
9215
9216 static void
9217 add_partial_subprogram (struct partial_die_info *pdi,
9218 CORE_ADDR *lowpc, CORE_ADDR *highpc,
9219 int set_addrmap, struct dwarf2_cu *cu)
9220 {
9221 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
9222 {
9223 if (pdi->has_pc_info)
9224 {
9225 if (pdi->lowpc < *lowpc)
9226 *lowpc = pdi->lowpc;
9227 if (pdi->highpc > *highpc)
9228 *highpc = pdi->highpc;
9229 if (set_addrmap)
9230 {
9231 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9232 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9233 CORE_ADDR baseaddr;
9234 CORE_ADDR this_highpc;
9235 CORE_ADDR this_lowpc;
9236
9237 baseaddr = objfile->section_offsets[SECT_OFF_TEXT (objfile)];
9238 this_lowpc
9239 = (gdbarch_adjust_dwarf2_addr (gdbarch,
9240 pdi->lowpc + baseaddr)
9241 - baseaddr);
9242 this_highpc
9243 = (gdbarch_adjust_dwarf2_addr (gdbarch,
9244 pdi->highpc + baseaddr)
9245 - baseaddr);
9246 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
9247 this_lowpc, this_highpc - 1,
9248 cu->per_cu->v.psymtab);
9249 }
9250 }
9251
9252 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
9253 {
9254 if (!pdi->is_declaration)
9255 /* Ignore subprogram DIEs that do not have a name, they are
9256 illegal. Do not emit a complaint at this point, we will
9257 do so when we convert this psymtab into a symtab. */
9258 if (pdi->name)
9259 add_partial_symbol (pdi, cu);
9260 }
9261 }
9262
9263 if (! pdi->has_children)
9264 return;
9265
9266 if (cu->language == language_ada || cu->language == language_fortran)
9267 {
9268 pdi = pdi->die_child;
9269 while (pdi != NULL)
9270 {
9271 pdi->fixup (cu);
9272 if (pdi->tag == DW_TAG_subprogram
9273 || pdi->tag == DW_TAG_inlined_subroutine
9274 || pdi->tag == DW_TAG_lexical_block)
9275 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
9276 pdi = pdi->die_sibling;
9277 }
9278 }
9279 }
9280
9281 /* Read a partial die corresponding to an enumeration type. */
9282
9283 static void
9284 add_partial_enumeration (struct partial_die_info *enum_pdi,
9285 struct dwarf2_cu *cu)
9286 {
9287 struct partial_die_info *pdi;
9288
9289 if (enum_pdi->name != NULL)
9290 add_partial_symbol (enum_pdi, cu);
9291
9292 pdi = enum_pdi->die_child;
9293 while (pdi)
9294 {
9295 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
9296 complaint (_("malformed enumerator DIE ignored"));
9297 else
9298 add_partial_symbol (pdi, cu);
9299 pdi = pdi->die_sibling;
9300 }
9301 }
9302
9303 /* Return the initial uleb128 in the die at INFO_PTR. */
9304
9305 static unsigned int
9306 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
9307 {
9308 unsigned int bytes_read;
9309
9310 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9311 }
9312
9313 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
9314 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
9315
9316 Return the corresponding abbrev, or NULL if the number is zero (indicating
9317 an empty DIE). In either case *BYTES_READ will be set to the length of
9318 the initial number. */
9319
9320 static struct abbrev_info *
9321 peek_die_abbrev (const die_reader_specs &reader,
9322 const gdb_byte *info_ptr, unsigned int *bytes_read)
9323 {
9324 dwarf2_cu *cu = reader.cu;
9325 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
9326 unsigned int abbrev_number
9327 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
9328
9329 if (abbrev_number == 0)
9330 return NULL;
9331
9332 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
9333 if (!abbrev)
9334 {
9335 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9336 " at offset %s [in module %s]"),
9337 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9338 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
9339 }
9340
9341 return abbrev;
9342 }
9343
9344 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9345 Returns a pointer to the end of a series of DIEs, terminated by an empty
9346 DIE. Any children of the skipped DIEs will also be skipped. */
9347
9348 static const gdb_byte *
9349 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
9350 {
9351 while (1)
9352 {
9353 unsigned int bytes_read;
9354 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
9355
9356 if (abbrev == NULL)
9357 return info_ptr + bytes_read;
9358 else
9359 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
9360 }
9361 }
9362
9363 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9364 INFO_PTR should point just after the initial uleb128 of a DIE, and the
9365 abbrev corresponding to that skipped uleb128 should be passed in
9366 ABBREV. Returns a pointer to this DIE's sibling, skipping any
9367 children. */
9368
9369 static const gdb_byte *
9370 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
9371 struct abbrev_info *abbrev)
9372 {
9373 unsigned int bytes_read;
9374 struct attribute attr;
9375 bfd *abfd = reader->abfd;
9376 struct dwarf2_cu *cu = reader->cu;
9377 const gdb_byte *buffer = reader->buffer;
9378 const gdb_byte *buffer_end = reader->buffer_end;
9379 unsigned int form, i;
9380
9381 for (i = 0; i < abbrev->num_attrs; i++)
9382 {
9383 /* The only abbrev we care about is DW_AT_sibling. */
9384 if (abbrev->attrs[i].name == DW_AT_sibling)
9385 {
9386 bool ignored;
9387 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr,
9388 &ignored);
9389 if (attr.form == DW_FORM_ref_addr)
9390 complaint (_("ignoring absolute DW_AT_sibling"));
9391 else
9392 {
9393 sect_offset off = dwarf2_get_ref_die_offset (&attr);
9394 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
9395
9396 if (sibling_ptr < info_ptr)
9397 complaint (_("DW_AT_sibling points backwards"));
9398 else if (sibling_ptr > reader->buffer_end)
9399 dwarf2_section_buffer_overflow_complaint (reader->die_section);
9400 else
9401 return sibling_ptr;
9402 }
9403 }
9404
9405 /* If it isn't DW_AT_sibling, skip this attribute. */
9406 form = abbrev->attrs[i].form;
9407 skip_attribute:
9408 switch (form)
9409 {
9410 case DW_FORM_ref_addr:
9411 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
9412 and later it is offset sized. */
9413 if (cu->header.version == 2)
9414 info_ptr += cu->header.addr_size;
9415 else
9416 info_ptr += cu->header.offset_size;
9417 break;
9418 case DW_FORM_GNU_ref_alt:
9419 info_ptr += cu->header.offset_size;
9420 break;
9421 case DW_FORM_addr:
9422 info_ptr += cu->header.addr_size;
9423 break;
9424 case DW_FORM_data1:
9425 case DW_FORM_ref1:
9426 case DW_FORM_flag:
9427 case DW_FORM_strx1:
9428 info_ptr += 1;
9429 break;
9430 case DW_FORM_flag_present:
9431 case DW_FORM_implicit_const:
9432 break;
9433 case DW_FORM_data2:
9434 case DW_FORM_ref2:
9435 case DW_FORM_strx2:
9436 info_ptr += 2;
9437 break;
9438 case DW_FORM_strx3:
9439 info_ptr += 3;
9440 break;
9441 case DW_FORM_data4:
9442 case DW_FORM_ref4:
9443 case DW_FORM_strx4:
9444 info_ptr += 4;
9445 break;
9446 case DW_FORM_data8:
9447 case DW_FORM_ref8:
9448 case DW_FORM_ref_sig8:
9449 info_ptr += 8;
9450 break;
9451 case DW_FORM_data16:
9452 info_ptr += 16;
9453 break;
9454 case DW_FORM_string:
9455 read_direct_string (abfd, info_ptr, &bytes_read);
9456 info_ptr += bytes_read;
9457 break;
9458 case DW_FORM_sec_offset:
9459 case DW_FORM_strp:
9460 case DW_FORM_GNU_strp_alt:
9461 info_ptr += cu->header.offset_size;
9462 break;
9463 case DW_FORM_exprloc:
9464 case DW_FORM_block:
9465 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9466 info_ptr += bytes_read;
9467 break;
9468 case DW_FORM_block1:
9469 info_ptr += 1 + read_1_byte (abfd, info_ptr);
9470 break;
9471 case DW_FORM_block2:
9472 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
9473 break;
9474 case DW_FORM_block4:
9475 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
9476 break;
9477 case DW_FORM_addrx:
9478 case DW_FORM_strx:
9479 case DW_FORM_sdata:
9480 case DW_FORM_udata:
9481 case DW_FORM_ref_udata:
9482 case DW_FORM_GNU_addr_index:
9483 case DW_FORM_GNU_str_index:
9484 case DW_FORM_rnglistx:
9485 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
9486 break;
9487 case DW_FORM_indirect:
9488 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9489 info_ptr += bytes_read;
9490 /* We need to continue parsing from here, so just go back to
9491 the top. */
9492 goto skip_attribute;
9493
9494 default:
9495 error (_("Dwarf Error: Cannot handle %s "
9496 "in DWARF reader [in module %s]"),
9497 dwarf_form_name (form),
9498 bfd_get_filename (abfd));
9499 }
9500 }
9501
9502 if (abbrev->has_children)
9503 return skip_children (reader, info_ptr);
9504 else
9505 return info_ptr;
9506 }
9507
9508 /* Locate ORIG_PDI's sibling.
9509 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
9510
9511 static const gdb_byte *
9512 locate_pdi_sibling (const struct die_reader_specs *reader,
9513 struct partial_die_info *orig_pdi,
9514 const gdb_byte *info_ptr)
9515 {
9516 /* Do we know the sibling already? */
9517
9518 if (orig_pdi->sibling)
9519 return orig_pdi->sibling;
9520
9521 /* Are there any children to deal with? */
9522
9523 if (!orig_pdi->has_children)
9524 return info_ptr;
9525
9526 /* Skip the children the long way. */
9527
9528 return skip_children (reader, info_ptr);
9529 }
9530
9531 /* Expand this partial symbol table into a full symbol table. SELF is
9532 not NULL. */
9533
9534 static void
9535 dwarf2_read_symtab (struct partial_symtab *self,
9536 struct objfile *objfile)
9537 {
9538 struct dwarf2_per_objfile *dwarf2_per_objfile
9539 = get_dwarf2_per_objfile (objfile);
9540
9541 if (self->readin)
9542 {
9543 warning (_("bug: psymtab for %s is already read in."),
9544 self->filename);
9545 }
9546 else
9547 {
9548 if (info_verbose)
9549 {
9550 printf_filtered (_("Reading in symbols for %s..."),
9551 self->filename);
9552 gdb_flush (gdb_stdout);
9553 }
9554
9555 /* If this psymtab is constructed from a debug-only objfile, the
9556 has_section_at_zero flag will not necessarily be correct. We
9557 can get the correct value for this flag by looking at the data
9558 associated with the (presumably stripped) associated objfile. */
9559 if (objfile->separate_debug_objfile_backlink)
9560 {
9561 struct dwarf2_per_objfile *dpo_backlink
9562 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
9563
9564 dwarf2_per_objfile->has_section_at_zero
9565 = dpo_backlink->has_section_at_zero;
9566 }
9567
9568 dwarf2_per_objfile->reading_partial_symbols = 0;
9569
9570 psymtab_to_symtab_1 (self);
9571
9572 /* Finish up the debug error message. */
9573 if (info_verbose)
9574 printf_filtered (_("done.\n"));
9575 }
9576
9577 process_cu_includes (dwarf2_per_objfile);
9578 }
9579 \f
9580 /* Reading in full CUs. */
9581
9582 /* Add PER_CU to the queue. */
9583
9584 static void
9585 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
9586 enum language pretend_language)
9587 {
9588 struct dwarf2_queue_item *item;
9589
9590 per_cu->queued = 1;
9591 item = XNEW (struct dwarf2_queue_item);
9592 item->per_cu = per_cu;
9593 item->pretend_language = pretend_language;
9594 item->next = NULL;
9595
9596 if (dwarf2_queue == NULL)
9597 dwarf2_queue = item;
9598 else
9599 dwarf2_queue_tail->next = item;
9600
9601 dwarf2_queue_tail = item;
9602 }
9603
9604 /* If PER_CU is not yet queued, add it to the queue.
9605 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9606 dependency.
9607 The result is non-zero if PER_CU was queued, otherwise the result is zero
9608 meaning either PER_CU is already queued or it is already loaded.
9609
9610 N.B. There is an invariant here that if a CU is queued then it is loaded.
9611 The caller is required to load PER_CU if we return non-zero. */
9612
9613 static int
9614 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
9615 struct dwarf2_per_cu_data *per_cu,
9616 enum language pretend_language)
9617 {
9618 /* We may arrive here during partial symbol reading, if we need full
9619 DIEs to process an unusual case (e.g. template arguments). Do
9620 not queue PER_CU, just tell our caller to load its DIEs. */
9621 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
9622 {
9623 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
9624 return 1;
9625 return 0;
9626 }
9627
9628 /* Mark the dependence relation so that we don't flush PER_CU
9629 too early. */
9630 if (dependent_cu != NULL)
9631 dwarf2_add_dependence (dependent_cu, per_cu);
9632
9633 /* If it's already on the queue, we have nothing to do. */
9634 if (per_cu->queued)
9635 return 0;
9636
9637 /* If the compilation unit is already loaded, just mark it as
9638 used. */
9639 if (per_cu->cu != NULL)
9640 {
9641 per_cu->cu->last_used = 0;
9642 return 0;
9643 }
9644
9645 /* Add it to the queue. */
9646 queue_comp_unit (per_cu, pretend_language);
9647
9648 return 1;
9649 }
9650
9651 /* Process the queue. */
9652
9653 static void
9654 process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
9655 {
9656 struct dwarf2_queue_item *item, *next_item;
9657
9658 if (dwarf_read_debug)
9659 {
9660 fprintf_unfiltered (gdb_stdlog,
9661 "Expanding one or more symtabs of objfile %s ...\n",
9662 objfile_name (dwarf2_per_objfile->objfile));
9663 }
9664
9665 /* The queue starts out with one item, but following a DIE reference
9666 may load a new CU, adding it to the end of the queue. */
9667 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
9668 {
9669 if ((dwarf2_per_objfile->using_index
9670 ? !item->per_cu->v.quick->compunit_symtab
9671 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
9672 /* Skip dummy CUs. */
9673 && item->per_cu->cu != NULL)
9674 {
9675 struct dwarf2_per_cu_data *per_cu = item->per_cu;
9676 unsigned int debug_print_threshold;
9677 char buf[100];
9678
9679 if (per_cu->is_debug_types)
9680 {
9681 struct signatured_type *sig_type =
9682 (struct signatured_type *) per_cu;
9683
9684 sprintf (buf, "TU %s at offset %s",
9685 hex_string (sig_type->signature),
9686 sect_offset_str (per_cu->sect_off));
9687 /* There can be 100s of TUs.
9688 Only print them in verbose mode. */
9689 debug_print_threshold = 2;
9690 }
9691 else
9692 {
9693 sprintf (buf, "CU at offset %s",
9694 sect_offset_str (per_cu->sect_off));
9695 debug_print_threshold = 1;
9696 }
9697
9698 if (dwarf_read_debug >= debug_print_threshold)
9699 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
9700
9701 if (per_cu->is_debug_types)
9702 process_full_type_unit (per_cu, item->pretend_language);
9703 else
9704 process_full_comp_unit (per_cu, item->pretend_language);
9705
9706 if (dwarf_read_debug >= debug_print_threshold)
9707 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
9708 }
9709
9710 item->per_cu->queued = 0;
9711 next_item = item->next;
9712 xfree (item);
9713 }
9714
9715 dwarf2_queue_tail = NULL;
9716
9717 if (dwarf_read_debug)
9718 {
9719 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
9720 objfile_name (dwarf2_per_objfile->objfile));
9721 }
9722 }
9723
9724 /* Read in full symbols for PST, and anything it depends on. */
9725
9726 static void
9727 psymtab_to_symtab_1 (struct partial_symtab *pst)
9728 {
9729 struct dwarf2_per_cu_data *per_cu;
9730 int i;
9731
9732 if (pst->readin)
9733 return;
9734
9735 for (i = 0; i < pst->number_of_dependencies; i++)
9736 if (!pst->dependencies[i]->readin
9737 && pst->dependencies[i]->user == NULL)
9738 {
9739 /* Inform about additional files that need to be read in. */
9740 if (info_verbose)
9741 {
9742 /* FIXME: i18n: Need to make this a single string. */
9743 fputs_filtered (" ", gdb_stdout);
9744 wrap_here ("");
9745 fputs_filtered ("and ", gdb_stdout);
9746 wrap_here ("");
9747 printf_filtered ("%s...", pst->dependencies[i]->filename);
9748 wrap_here (""); /* Flush output. */
9749 gdb_flush (gdb_stdout);
9750 }
9751 psymtab_to_symtab_1 (pst->dependencies[i]);
9752 }
9753
9754 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
9755
9756 if (per_cu == NULL)
9757 {
9758 /* It's an include file, no symbols to read for it.
9759 Everything is in the parent symtab. */
9760 pst->readin = 1;
9761 return;
9762 }
9763
9764 dw2_do_instantiate_symtab (per_cu, false);
9765 }
9766
9767 /* Trivial hash function for die_info: the hash value of a DIE
9768 is its offset in .debug_info for this objfile. */
9769
9770 static hashval_t
9771 die_hash (const void *item)
9772 {
9773 const struct die_info *die = (const struct die_info *) item;
9774
9775 return to_underlying (die->sect_off);
9776 }
9777
9778 /* Trivial comparison function for die_info structures: two DIEs
9779 are equal if they have the same offset. */
9780
9781 static int
9782 die_eq (const void *item_lhs, const void *item_rhs)
9783 {
9784 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9785 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
9786
9787 return die_lhs->sect_off == die_rhs->sect_off;
9788 }
9789
9790 /* die_reader_func for load_full_comp_unit.
9791 This is identical to read_signatured_type_reader,
9792 but is kept separate for now. */
9793
9794 static void
9795 load_full_comp_unit_reader (const struct die_reader_specs *reader,
9796 const gdb_byte *info_ptr,
9797 struct die_info *comp_unit_die,
9798 int has_children,
9799 void *data)
9800 {
9801 struct dwarf2_cu *cu = reader->cu;
9802 enum language *language_ptr = (enum language *) data;
9803
9804 gdb_assert (cu->die_hash == NULL);
9805 cu->die_hash =
9806 htab_create_alloc_ex (cu->header.length / 12,
9807 die_hash,
9808 die_eq,
9809 NULL,
9810 &cu->comp_unit_obstack,
9811 hashtab_obstack_allocate,
9812 dummy_obstack_deallocate);
9813
9814 if (has_children)
9815 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
9816 &info_ptr, comp_unit_die);
9817 cu->dies = comp_unit_die;
9818 /* comp_unit_die is not stored in die_hash, no need. */
9819
9820 /* We try not to read any attributes in this function, because not
9821 all CUs needed for references have been loaded yet, and symbol
9822 table processing isn't initialized. But we have to set the CU language,
9823 or we won't be able to build types correctly.
9824 Similarly, if we do not read the producer, we can not apply
9825 producer-specific interpretation. */
9826 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
9827 }
9828
9829 /* Load the DIEs associated with PER_CU into memory. */
9830
9831 static void
9832 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
9833 bool skip_partial,
9834 enum language pretend_language)
9835 {
9836 gdb_assert (! this_cu->is_debug_types);
9837
9838 init_cutu_and_read_dies (this_cu, NULL, 1, 1, skip_partial,
9839 load_full_comp_unit_reader, &pretend_language);
9840 }
9841
9842 /* Add a DIE to the delayed physname list. */
9843
9844 static void
9845 add_to_method_list (struct type *type, int fnfield_index, int index,
9846 const char *name, struct die_info *die,
9847 struct dwarf2_cu *cu)
9848 {
9849 struct delayed_method_info mi;
9850 mi.type = type;
9851 mi.fnfield_index = fnfield_index;
9852 mi.index = index;
9853 mi.name = name;
9854 mi.die = die;
9855 cu->method_list.push_back (mi);
9856 }
9857
9858 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9859 "const" / "volatile". If so, decrements LEN by the length of the
9860 modifier and return true. Otherwise return false. */
9861
9862 template<size_t N>
9863 static bool
9864 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9865 {
9866 size_t mod_len = sizeof (mod) - 1;
9867 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9868 {
9869 len -= mod_len;
9870 return true;
9871 }
9872 return false;
9873 }
9874
9875 /* Compute the physnames of any methods on the CU's method list.
9876
9877 The computation of method physnames is delayed in order to avoid the
9878 (bad) condition that one of the method's formal parameters is of an as yet
9879 incomplete type. */
9880
9881 static void
9882 compute_delayed_physnames (struct dwarf2_cu *cu)
9883 {
9884 /* Only C++ delays computing physnames. */
9885 if (cu->method_list.empty ())
9886 return;
9887 gdb_assert (cu->language == language_cplus);
9888
9889 for (const delayed_method_info &mi : cu->method_list)
9890 {
9891 const char *physname;
9892 struct fn_fieldlist *fn_flp
9893 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9894 physname = dwarf2_physname (mi.name, mi.die, cu);
9895 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
9896 = physname ? physname : "";
9897
9898 /* Since there's no tag to indicate whether a method is a
9899 const/volatile overload, extract that information out of the
9900 demangled name. */
9901 if (physname != NULL)
9902 {
9903 size_t len = strlen (physname);
9904
9905 while (1)
9906 {
9907 if (physname[len] == ')') /* shortcut */
9908 break;
9909 else if (check_modifier (physname, len, " const"))
9910 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
9911 else if (check_modifier (physname, len, " volatile"))
9912 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
9913 else
9914 break;
9915 }
9916 }
9917 }
9918
9919 /* The list is no longer needed. */
9920 cu->method_list.clear ();
9921 }
9922
9923 /* Go objects should be embedded in a DW_TAG_module DIE,
9924 and it's not clear if/how imported objects will appear.
9925 To keep Go support simple until that's worked out,
9926 go back through what we've read and create something usable.
9927 We could do this while processing each DIE, and feels kinda cleaner,
9928 but that way is more invasive.
9929 This is to, for example, allow the user to type "p var" or "b main"
9930 without having to specify the package name, and allow lookups
9931 of module.object to work in contexts that use the expression
9932 parser. */
9933
9934 static void
9935 fixup_go_packaging (struct dwarf2_cu *cu)
9936 {
9937 gdb::unique_xmalloc_ptr<char> package_name;
9938 struct pending *list;
9939 int i;
9940
9941 for (list = *cu->get_builder ()->get_global_symbols ();
9942 list != NULL;
9943 list = list->next)
9944 {
9945 for (i = 0; i < list->nsyms; ++i)
9946 {
9947 struct symbol *sym = list->symbol[i];
9948
9949 if (sym->language () == language_go
9950 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9951 {
9952 gdb::unique_xmalloc_ptr<char> this_package_name
9953 (go_symbol_package_name (sym));
9954
9955 if (this_package_name == NULL)
9956 continue;
9957 if (package_name == NULL)
9958 package_name = std::move (this_package_name);
9959 else
9960 {
9961 struct objfile *objfile
9962 = cu->per_cu->dwarf2_per_objfile->objfile;
9963 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
9964 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
9965 (symbol_symtab (sym) != NULL
9966 ? symtab_to_filename_for_display
9967 (symbol_symtab (sym))
9968 : objfile_name (objfile)),
9969 this_package_name.get (), package_name.get ());
9970 }
9971 }
9972 }
9973 }
9974
9975 if (package_name != NULL)
9976 {
9977 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9978 const char *saved_package_name
9979 = obstack_strdup (&objfile->per_bfd->storage_obstack, package_name.get ());
9980 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9981 saved_package_name);
9982 struct symbol *sym;
9983
9984 sym = allocate_symbol (objfile);
9985 sym->set_language (language_go, &objfile->objfile_obstack);
9986 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
9987 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9988 e.g., "main" finds the "main" module and not C's main(). */
9989 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9990 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9991 SYMBOL_TYPE (sym) = type;
9992
9993 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
9994 }
9995 }
9996
9997 /* Allocate a fully-qualified name consisting of the two parts on the
9998 obstack. */
9999
10000 static const char *
10001 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
10002 {
10003 return obconcat (obstack, p1, "::", p2, (char *) NULL);
10004 }
10005
10006 /* A helper that allocates a struct discriminant_info to attach to a
10007 union type. */
10008
10009 static struct discriminant_info *
10010 alloc_discriminant_info (struct type *type, int discriminant_index,
10011 int default_index)
10012 {
10013 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
10014 gdb_assert (discriminant_index == -1
10015 || (discriminant_index >= 0
10016 && discriminant_index < TYPE_NFIELDS (type)));
10017 gdb_assert (default_index == -1
10018 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
10019
10020 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
10021
10022 struct discriminant_info *disc
10023 = ((struct discriminant_info *)
10024 TYPE_ZALLOC (type,
10025 offsetof (struct discriminant_info, discriminants)
10026 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
10027 disc->default_index = default_index;
10028 disc->discriminant_index = discriminant_index;
10029
10030 struct dynamic_prop prop;
10031 prop.kind = PROP_UNDEFINED;
10032 prop.data.baton = disc;
10033
10034 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
10035
10036 return disc;
10037 }
10038
10039 /* Some versions of rustc emitted enums in an unusual way.
10040
10041 Ordinary enums were emitted as unions. The first element of each
10042 structure in the union was named "RUST$ENUM$DISR". This element
10043 held the discriminant.
10044
10045 These versions of Rust also implemented the "non-zero"
10046 optimization. When the enum had two values, and one is empty and
10047 the other holds a pointer that cannot be zero, the pointer is used
10048 as the discriminant, with a zero value meaning the empty variant.
10049 Here, the union's first member is of the form
10050 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
10051 where the fieldnos are the indices of the fields that should be
10052 traversed in order to find the field (which may be several fields deep)
10053 and the variantname is the name of the variant of the case when the
10054 field is zero.
10055
10056 This function recognizes whether TYPE is of one of these forms,
10057 and, if so, smashes it to be a variant type. */
10058
10059 static void
10060 quirk_rust_enum (struct type *type, struct objfile *objfile)
10061 {
10062 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
10063
10064 /* We don't need to deal with empty enums. */
10065 if (TYPE_NFIELDS (type) == 0)
10066 return;
10067
10068 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
10069 if (TYPE_NFIELDS (type) == 1
10070 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
10071 {
10072 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
10073
10074 /* Decode the field name to find the offset of the
10075 discriminant. */
10076 ULONGEST bit_offset = 0;
10077 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
10078 while (name[0] >= '0' && name[0] <= '9')
10079 {
10080 char *tail;
10081 unsigned long index = strtoul (name, &tail, 10);
10082 name = tail;
10083 if (*name != '$'
10084 || index >= TYPE_NFIELDS (field_type)
10085 || (TYPE_FIELD_LOC_KIND (field_type, index)
10086 != FIELD_LOC_KIND_BITPOS))
10087 {
10088 complaint (_("Could not parse Rust enum encoding string \"%s\""
10089 "[in module %s]"),
10090 TYPE_FIELD_NAME (type, 0),
10091 objfile_name (objfile));
10092 return;
10093 }
10094 ++name;
10095
10096 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
10097 field_type = TYPE_FIELD_TYPE (field_type, index);
10098 }
10099
10100 /* Make a union to hold the variants. */
10101 struct type *union_type = alloc_type (objfile);
10102 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10103 TYPE_NFIELDS (union_type) = 3;
10104 TYPE_FIELDS (union_type)
10105 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
10106 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
10107 set_type_align (union_type, TYPE_RAW_ALIGN (type));
10108
10109 /* Put the discriminant must at index 0. */
10110 TYPE_FIELD_TYPE (union_type, 0) = field_type;
10111 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10112 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10113 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
10114
10115 /* The order of fields doesn't really matter, so put the real
10116 field at index 1 and the data-less field at index 2. */
10117 struct discriminant_info *disc
10118 = alloc_discriminant_info (union_type, 0, 1);
10119 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
10120 TYPE_FIELD_NAME (union_type, 1)
10121 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
10122 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
10123 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
10124 TYPE_FIELD_NAME (union_type, 1));
10125
10126 const char *dataless_name
10127 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
10128 name);
10129 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
10130 dataless_name);
10131 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
10132 /* NAME points into the original discriminant name, which
10133 already has the correct lifetime. */
10134 TYPE_FIELD_NAME (union_type, 2) = name;
10135 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
10136 disc->discriminants[2] = 0;
10137
10138 /* Smash this type to be a structure type. We have to do this
10139 because the type has already been recorded. */
10140 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10141 TYPE_NFIELDS (type) = 1;
10142 TYPE_FIELDS (type)
10143 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
10144
10145 /* Install the variant part. */
10146 TYPE_FIELD_TYPE (type, 0) = union_type;
10147 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10148 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10149 }
10150 /* A union with a single anonymous field is probably an old-style
10151 univariant enum. */
10152 else if (TYPE_NFIELDS (type) == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
10153 {
10154 /* Smash this type to be a structure type. We have to do this
10155 because the type has already been recorded. */
10156 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10157
10158 /* Make a union to hold the variants. */
10159 struct type *union_type = alloc_type (objfile);
10160 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10161 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
10162 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
10163 set_type_align (union_type, TYPE_RAW_ALIGN (type));
10164 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
10165
10166 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
10167 const char *variant_name
10168 = rust_last_path_segment (TYPE_NAME (field_type));
10169 TYPE_FIELD_NAME (union_type, 0) = variant_name;
10170 TYPE_NAME (field_type)
10171 = rust_fully_qualify (&objfile->objfile_obstack,
10172 TYPE_NAME (type), variant_name);
10173
10174 /* Install the union in the outer struct type. */
10175 TYPE_NFIELDS (type) = 1;
10176 TYPE_FIELDS (type)
10177 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
10178 TYPE_FIELD_TYPE (type, 0) = union_type;
10179 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10180 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10181
10182 alloc_discriminant_info (union_type, -1, 0);
10183 }
10184 else
10185 {
10186 struct type *disr_type = nullptr;
10187 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
10188 {
10189 disr_type = TYPE_FIELD_TYPE (type, i);
10190
10191 if (TYPE_CODE (disr_type) != TYPE_CODE_STRUCT)
10192 {
10193 /* All fields of a true enum will be structs. */
10194 return;
10195 }
10196 else if (TYPE_NFIELDS (disr_type) == 0)
10197 {
10198 /* Could be data-less variant, so keep going. */
10199 disr_type = nullptr;
10200 }
10201 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
10202 "RUST$ENUM$DISR") != 0)
10203 {
10204 /* Not a Rust enum. */
10205 return;
10206 }
10207 else
10208 {
10209 /* Found one. */
10210 break;
10211 }
10212 }
10213
10214 /* If we got here without a discriminant, then it's probably
10215 just a union. */
10216 if (disr_type == nullptr)
10217 return;
10218
10219 /* Smash this type to be a structure type. We have to do this
10220 because the type has already been recorded. */
10221 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10222
10223 /* Make a union to hold the variants. */
10224 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
10225 struct type *union_type = alloc_type (objfile);
10226 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10227 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
10228 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
10229 set_type_align (union_type, TYPE_RAW_ALIGN (type));
10230 TYPE_FIELDS (union_type)
10231 = (struct field *) TYPE_ZALLOC (union_type,
10232 (TYPE_NFIELDS (union_type)
10233 * sizeof (struct field)));
10234
10235 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
10236 TYPE_NFIELDS (type) * sizeof (struct field));
10237
10238 /* Install the discriminant at index 0 in the union. */
10239 TYPE_FIELD (union_type, 0) = *disr_field;
10240 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10241 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10242
10243 /* Install the union in the outer struct type. */
10244 TYPE_FIELD_TYPE (type, 0) = union_type;
10245 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10246 TYPE_NFIELDS (type) = 1;
10247
10248 /* Set the size and offset of the union type. */
10249 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10250
10251 /* We need a way to find the correct discriminant given a
10252 variant name. For convenience we build a map here. */
10253 struct type *enum_type = FIELD_TYPE (*disr_field);
10254 std::unordered_map<std::string, ULONGEST> discriminant_map;
10255 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
10256 {
10257 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
10258 {
10259 const char *name
10260 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
10261 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
10262 }
10263 }
10264
10265 int n_fields = TYPE_NFIELDS (union_type);
10266 struct discriminant_info *disc
10267 = alloc_discriminant_info (union_type, 0, -1);
10268 /* Skip the discriminant here. */
10269 for (int i = 1; i < n_fields; ++i)
10270 {
10271 /* Find the final word in the name of this variant's type.
10272 That name can be used to look up the correct
10273 discriminant. */
10274 const char *variant_name
10275 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
10276 i)));
10277
10278 auto iter = discriminant_map.find (variant_name);
10279 if (iter != discriminant_map.end ())
10280 disc->discriminants[i] = iter->second;
10281
10282 /* Remove the discriminant field, if it exists. */
10283 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
10284 if (TYPE_NFIELDS (sub_type) > 0)
10285 {
10286 --TYPE_NFIELDS (sub_type);
10287 ++TYPE_FIELDS (sub_type);
10288 }
10289 TYPE_FIELD_NAME (union_type, i) = variant_name;
10290 TYPE_NAME (sub_type)
10291 = rust_fully_qualify (&objfile->objfile_obstack,
10292 TYPE_NAME (type), variant_name);
10293 }
10294 }
10295 }
10296
10297 /* Rewrite some Rust unions to be structures with variants parts. */
10298
10299 static void
10300 rust_union_quirks (struct dwarf2_cu *cu)
10301 {
10302 gdb_assert (cu->language == language_rust);
10303 for (type *type_ : cu->rust_unions)
10304 quirk_rust_enum (type_, cu->per_cu->dwarf2_per_objfile->objfile);
10305 /* We don't need this any more. */
10306 cu->rust_unions.clear ();
10307 }
10308
10309 /* Return the symtab for PER_CU. This works properly regardless of
10310 whether we're using the index or psymtabs. */
10311
10312 static struct compunit_symtab *
10313 get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
10314 {
10315 return (per_cu->dwarf2_per_objfile->using_index
10316 ? per_cu->v.quick->compunit_symtab
10317 : per_cu->v.psymtab->compunit_symtab);
10318 }
10319
10320 /* A helper function for computing the list of all symbol tables
10321 included by PER_CU. */
10322
10323 static void
10324 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
10325 htab_t all_children, htab_t all_type_symtabs,
10326 struct dwarf2_per_cu_data *per_cu,
10327 struct compunit_symtab *immediate_parent)
10328 {
10329 void **slot;
10330 struct compunit_symtab *cust;
10331
10332 slot = htab_find_slot (all_children, per_cu, INSERT);
10333 if (*slot != NULL)
10334 {
10335 /* This inclusion and its children have been processed. */
10336 return;
10337 }
10338
10339 *slot = per_cu;
10340 /* Only add a CU if it has a symbol table. */
10341 cust = get_compunit_symtab (per_cu);
10342 if (cust != NULL)
10343 {
10344 /* If this is a type unit only add its symbol table if we haven't
10345 seen it yet (type unit per_cu's can share symtabs). */
10346 if (per_cu->is_debug_types)
10347 {
10348 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
10349 if (*slot == NULL)
10350 {
10351 *slot = cust;
10352 result->push_back (cust);
10353 if (cust->user == NULL)
10354 cust->user = immediate_parent;
10355 }
10356 }
10357 else
10358 {
10359 result->push_back (cust);
10360 if (cust->user == NULL)
10361 cust->user = immediate_parent;
10362 }
10363 }
10364
10365 if (!per_cu->imported_symtabs_empty ())
10366 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
10367 {
10368 recursively_compute_inclusions (result, all_children,
10369 all_type_symtabs, ptr, cust);
10370 }
10371 }
10372
10373 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
10374 PER_CU. */
10375
10376 static void
10377 compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
10378 {
10379 gdb_assert (! per_cu->is_debug_types);
10380
10381 if (!per_cu->imported_symtabs_empty ())
10382 {
10383 int len;
10384 std::vector<compunit_symtab *> result_symtabs;
10385 htab_t all_children, all_type_symtabs;
10386 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
10387
10388 /* If we don't have a symtab, we can just skip this case. */
10389 if (cust == NULL)
10390 return;
10391
10392 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10393 NULL, xcalloc, xfree);
10394 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10395 NULL, xcalloc, xfree);
10396
10397 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
10398 {
10399 recursively_compute_inclusions (&result_symtabs, all_children,
10400 all_type_symtabs, ptr, cust);
10401 }
10402
10403 /* Now we have a transitive closure of all the included symtabs. */
10404 len = result_symtabs.size ();
10405 cust->includes
10406 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
10407 struct compunit_symtab *, len + 1);
10408 memcpy (cust->includes, result_symtabs.data (),
10409 len * sizeof (compunit_symtab *));
10410 cust->includes[len] = NULL;
10411
10412 htab_delete (all_children);
10413 htab_delete (all_type_symtabs);
10414 }
10415 }
10416
10417 /* Compute the 'includes' field for the symtabs of all the CUs we just
10418 read. */
10419
10420 static void
10421 process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
10422 {
10423 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->just_read_cus)
10424 {
10425 if (! iter->is_debug_types)
10426 compute_compunit_symtab_includes (iter);
10427 }
10428
10429 dwarf2_per_objfile->just_read_cus.clear ();
10430 }
10431
10432 /* Generate full symbol information for PER_CU, whose DIEs have
10433 already been loaded into memory. */
10434
10435 static void
10436 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
10437 enum language pretend_language)
10438 {
10439 struct dwarf2_cu *cu = per_cu->cu;
10440 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10441 struct objfile *objfile = dwarf2_per_objfile->objfile;
10442 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10443 CORE_ADDR lowpc, highpc;
10444 struct compunit_symtab *cust;
10445 CORE_ADDR baseaddr;
10446 struct block *static_block;
10447 CORE_ADDR addr;
10448
10449 baseaddr = objfile->section_offsets[SECT_OFF_TEXT (objfile)];
10450
10451 /* Clear the list here in case something was left over. */
10452 cu->method_list.clear ();
10453
10454 cu->language = pretend_language;
10455 cu->language_defn = language_def (cu->language);
10456
10457 /* Do line number decoding in read_file_scope () */
10458 process_die (cu->dies, cu);
10459
10460 /* For now fudge the Go package. */
10461 if (cu->language == language_go)
10462 fixup_go_packaging (cu);
10463
10464 /* Now that we have processed all the DIEs in the CU, all the types
10465 should be complete, and it should now be safe to compute all of the
10466 physnames. */
10467 compute_delayed_physnames (cu);
10468
10469 if (cu->language == language_rust)
10470 rust_union_quirks (cu);
10471
10472 /* Some compilers don't define a DW_AT_high_pc attribute for the
10473 compilation unit. If the DW_AT_high_pc is missing, synthesize
10474 it, by scanning the DIE's below the compilation unit. */
10475 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
10476
10477 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
10478 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
10479
10480 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10481 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10482 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10483 addrmap to help ensure it has an accurate map of pc values belonging to
10484 this comp unit. */
10485 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10486
10487 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
10488 SECT_OFF_TEXT (objfile),
10489 0);
10490
10491 if (cust != NULL)
10492 {
10493 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
10494
10495 /* Set symtab language to language from DW_AT_language. If the
10496 compilation is from a C file generated by language preprocessors, do
10497 not set the language if it was already deduced by start_subfile. */
10498 if (!(cu->language == language_c
10499 && COMPUNIT_FILETABS (cust)->language != language_unknown))
10500 COMPUNIT_FILETABS (cust)->language = cu->language;
10501
10502 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10503 produce DW_AT_location with location lists but it can be possibly
10504 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10505 there were bugs in prologue debug info, fixed later in GCC-4.5
10506 by "unwind info for epilogues" patch (which is not directly related).
10507
10508 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10509 needed, it would be wrong due to missing DW_AT_producer there.
10510
10511 Still one can confuse GDB by using non-standard GCC compilation
10512 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
10513 */
10514 if (cu->has_loclist && gcc_4_minor >= 5)
10515 cust->locations_valid = 1;
10516
10517 if (gcc_4_minor >= 5)
10518 cust->epilogue_unwind_valid = 1;
10519
10520 cust->call_site_htab = cu->call_site_htab;
10521 }
10522
10523 if (dwarf2_per_objfile->using_index)
10524 per_cu->v.quick->compunit_symtab = cust;
10525 else
10526 {
10527 struct partial_symtab *pst = per_cu->v.psymtab;
10528 pst->compunit_symtab = cust;
10529 pst->readin = 1;
10530 }
10531
10532 /* Push it for inclusion processing later. */
10533 dwarf2_per_objfile->just_read_cus.push_back (per_cu);
10534
10535 /* Not needed any more. */
10536 cu->reset_builder ();
10537 }
10538
10539 /* Generate full symbol information for type unit PER_CU, whose DIEs have
10540 already been loaded into memory. */
10541
10542 static void
10543 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
10544 enum language pretend_language)
10545 {
10546 struct dwarf2_cu *cu = per_cu->cu;
10547 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10548 struct objfile *objfile = dwarf2_per_objfile->objfile;
10549 struct compunit_symtab *cust;
10550 struct signatured_type *sig_type;
10551
10552 gdb_assert (per_cu->is_debug_types);
10553 sig_type = (struct signatured_type *) per_cu;
10554
10555 /* Clear the list here in case something was left over. */
10556 cu->method_list.clear ();
10557
10558 cu->language = pretend_language;
10559 cu->language_defn = language_def (cu->language);
10560
10561 /* The symbol tables are set up in read_type_unit_scope. */
10562 process_die (cu->dies, cu);
10563
10564 /* For now fudge the Go package. */
10565 if (cu->language == language_go)
10566 fixup_go_packaging (cu);
10567
10568 /* Now that we have processed all the DIEs in the CU, all the types
10569 should be complete, and it should now be safe to compute all of the
10570 physnames. */
10571 compute_delayed_physnames (cu);
10572
10573 if (cu->language == language_rust)
10574 rust_union_quirks (cu);
10575
10576 /* TUs share symbol tables.
10577 If this is the first TU to use this symtab, complete the construction
10578 of it with end_expandable_symtab. Otherwise, complete the addition of
10579 this TU's symbols to the existing symtab. */
10580 if (sig_type->type_unit_group->compunit_symtab == NULL)
10581 {
10582 buildsym_compunit *builder = cu->get_builder ();
10583 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
10584 sig_type->type_unit_group->compunit_symtab = cust;
10585
10586 if (cust != NULL)
10587 {
10588 /* Set symtab language to language from DW_AT_language. If the
10589 compilation is from a C file generated by language preprocessors,
10590 do not set the language if it was already deduced by
10591 start_subfile. */
10592 if (!(cu->language == language_c
10593 && COMPUNIT_FILETABS (cust)->language != language_c))
10594 COMPUNIT_FILETABS (cust)->language = cu->language;
10595 }
10596 }
10597 else
10598 {
10599 cu->get_builder ()->augment_type_symtab ();
10600 cust = sig_type->type_unit_group->compunit_symtab;
10601 }
10602
10603 if (dwarf2_per_objfile->using_index)
10604 per_cu->v.quick->compunit_symtab = cust;
10605 else
10606 {
10607 struct partial_symtab *pst = per_cu->v.psymtab;
10608 pst->compunit_symtab = cust;
10609 pst->readin = 1;
10610 }
10611
10612 /* Not needed any more. */
10613 cu->reset_builder ();
10614 }
10615
10616 /* Process an imported unit DIE. */
10617
10618 static void
10619 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10620 {
10621 struct attribute *attr;
10622
10623 /* For now we don't handle imported units in type units. */
10624 if (cu->per_cu->is_debug_types)
10625 {
10626 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10627 " supported in type units [in module %s]"),
10628 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
10629 }
10630
10631 attr = dwarf2_attr (die, DW_AT_import, cu);
10632 if (attr != NULL)
10633 {
10634 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10635 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10636 dwarf2_per_cu_data *per_cu
10637 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
10638 cu->per_cu->dwarf2_per_objfile);
10639
10640 /* If necessary, add it to the queue and load its DIEs. */
10641 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
10642 load_full_comp_unit (per_cu, false, cu->language);
10643
10644 cu->per_cu->imported_symtabs_push (per_cu);
10645 }
10646 }
10647
10648 /* RAII object that represents a process_die scope: i.e.,
10649 starts/finishes processing a DIE. */
10650 class process_die_scope
10651 {
10652 public:
10653 process_die_scope (die_info *die, dwarf2_cu *cu)
10654 : m_die (die), m_cu (cu)
10655 {
10656 /* We should only be processing DIEs not already in process. */
10657 gdb_assert (!m_die->in_process);
10658 m_die->in_process = true;
10659 }
10660
10661 ~process_die_scope ()
10662 {
10663 m_die->in_process = false;
10664
10665 /* If we're done processing the DIE for the CU that owns the line
10666 header, we don't need the line header anymore. */
10667 if (m_cu->line_header_die_owner == m_die)
10668 {
10669 delete m_cu->line_header;
10670 m_cu->line_header = NULL;
10671 m_cu->line_header_die_owner = NULL;
10672 }
10673 }
10674
10675 private:
10676 die_info *m_die;
10677 dwarf2_cu *m_cu;
10678 };
10679
10680 /* Process a die and its children. */
10681
10682 static void
10683 process_die (struct die_info *die, struct dwarf2_cu *cu)
10684 {
10685 process_die_scope scope (die, cu);
10686
10687 switch (die->tag)
10688 {
10689 case DW_TAG_padding:
10690 break;
10691 case DW_TAG_compile_unit:
10692 case DW_TAG_partial_unit:
10693 read_file_scope (die, cu);
10694 break;
10695 case DW_TAG_type_unit:
10696 read_type_unit_scope (die, cu);
10697 break;
10698 case DW_TAG_subprogram:
10699 /* Nested subprograms in Fortran get a prefix. */
10700 if (cu->language == language_fortran
10701 && die->parent != NULL
10702 && die->parent->tag == DW_TAG_subprogram)
10703 cu->processing_has_namespace_info = true;
10704 /* Fall through. */
10705 case DW_TAG_inlined_subroutine:
10706 read_func_scope (die, cu);
10707 break;
10708 case DW_TAG_lexical_block:
10709 case DW_TAG_try_block:
10710 case DW_TAG_catch_block:
10711 read_lexical_block_scope (die, cu);
10712 break;
10713 case DW_TAG_call_site:
10714 case DW_TAG_GNU_call_site:
10715 read_call_site_scope (die, cu);
10716 break;
10717 case DW_TAG_class_type:
10718 case DW_TAG_interface_type:
10719 case DW_TAG_structure_type:
10720 case DW_TAG_union_type:
10721 process_structure_scope (die, cu);
10722 break;
10723 case DW_TAG_enumeration_type:
10724 process_enumeration_scope (die, cu);
10725 break;
10726
10727 /* These dies have a type, but processing them does not create
10728 a symbol or recurse to process the children. Therefore we can
10729 read them on-demand through read_type_die. */
10730 case DW_TAG_subroutine_type:
10731 case DW_TAG_set_type:
10732 case DW_TAG_array_type:
10733 case DW_TAG_pointer_type:
10734 case DW_TAG_ptr_to_member_type:
10735 case DW_TAG_reference_type:
10736 case DW_TAG_rvalue_reference_type:
10737 case DW_TAG_string_type:
10738 break;
10739
10740 case DW_TAG_base_type:
10741 case DW_TAG_subrange_type:
10742 case DW_TAG_typedef:
10743 /* Add a typedef symbol for the type definition, if it has a
10744 DW_AT_name. */
10745 new_symbol (die, read_type_die (die, cu), cu);
10746 break;
10747 case DW_TAG_common_block:
10748 read_common_block (die, cu);
10749 break;
10750 case DW_TAG_common_inclusion:
10751 break;
10752 case DW_TAG_namespace:
10753 cu->processing_has_namespace_info = true;
10754 read_namespace (die, cu);
10755 break;
10756 case DW_TAG_module:
10757 cu->processing_has_namespace_info = true;
10758 read_module (die, cu);
10759 break;
10760 case DW_TAG_imported_declaration:
10761 cu->processing_has_namespace_info = true;
10762 if (read_namespace_alias (die, cu))
10763 break;
10764 /* The declaration is not a global namespace alias. */
10765 /* Fall through. */
10766 case DW_TAG_imported_module:
10767 cu->processing_has_namespace_info = true;
10768 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10769 || cu->language != language_fortran))
10770 complaint (_("Tag '%s' has unexpected children"),
10771 dwarf_tag_name (die->tag));
10772 read_import_statement (die, cu);
10773 break;
10774
10775 case DW_TAG_imported_unit:
10776 process_imported_unit_die (die, cu);
10777 break;
10778
10779 case DW_TAG_variable:
10780 read_variable (die, cu);
10781 break;
10782
10783 default:
10784 new_symbol (die, NULL, cu);
10785 break;
10786 }
10787 }
10788 \f
10789 /* DWARF name computation. */
10790
10791 /* A helper function for dwarf2_compute_name which determines whether DIE
10792 needs to have the name of the scope prepended to the name listed in the
10793 die. */
10794
10795 static int
10796 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10797 {
10798 struct attribute *attr;
10799
10800 switch (die->tag)
10801 {
10802 case DW_TAG_namespace:
10803 case DW_TAG_typedef:
10804 case DW_TAG_class_type:
10805 case DW_TAG_interface_type:
10806 case DW_TAG_structure_type:
10807 case DW_TAG_union_type:
10808 case DW_TAG_enumeration_type:
10809 case DW_TAG_enumerator:
10810 case DW_TAG_subprogram:
10811 case DW_TAG_inlined_subroutine:
10812 case DW_TAG_member:
10813 case DW_TAG_imported_declaration:
10814 return 1;
10815
10816 case DW_TAG_variable:
10817 case DW_TAG_constant:
10818 /* We only need to prefix "globally" visible variables. These include
10819 any variable marked with DW_AT_external or any variable that
10820 lives in a namespace. [Variables in anonymous namespaces
10821 require prefixing, but they are not DW_AT_external.] */
10822
10823 if (dwarf2_attr (die, DW_AT_specification, cu))
10824 {
10825 struct dwarf2_cu *spec_cu = cu;
10826
10827 return die_needs_namespace (die_specification (die, &spec_cu),
10828 spec_cu);
10829 }
10830
10831 attr = dwarf2_attr (die, DW_AT_external, cu);
10832 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10833 && die->parent->tag != DW_TAG_module)
10834 return 0;
10835 /* A variable in a lexical block of some kind does not need a
10836 namespace, even though in C++ such variables may be external
10837 and have a mangled name. */
10838 if (die->parent->tag == DW_TAG_lexical_block
10839 || die->parent->tag == DW_TAG_try_block
10840 || die->parent->tag == DW_TAG_catch_block
10841 || die->parent->tag == DW_TAG_subprogram)
10842 return 0;
10843 return 1;
10844
10845 default:
10846 return 0;
10847 }
10848 }
10849
10850 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10851 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10852 defined for the given DIE. */
10853
10854 static struct attribute *
10855 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10856 {
10857 struct attribute *attr;
10858
10859 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10860 if (attr == NULL)
10861 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10862
10863 return attr;
10864 }
10865
10866 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10867 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10868 defined for the given DIE. */
10869
10870 static const char *
10871 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10872 {
10873 const char *linkage_name;
10874
10875 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10876 if (linkage_name == NULL)
10877 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10878
10879 return linkage_name;
10880 }
10881
10882 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
10883 compute the physname for the object, which include a method's:
10884 - formal parameters (C++),
10885 - receiver type (Go),
10886
10887 The term "physname" is a bit confusing.
10888 For C++, for example, it is the demangled name.
10889 For Go, for example, it's the mangled name.
10890
10891 For Ada, return the DIE's linkage name rather than the fully qualified
10892 name. PHYSNAME is ignored..
10893
10894 The result is allocated on the objfile_obstack and canonicalized. */
10895
10896 static const char *
10897 dwarf2_compute_name (const char *name,
10898 struct die_info *die, struct dwarf2_cu *cu,
10899 int physname)
10900 {
10901 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10902
10903 if (name == NULL)
10904 name = dwarf2_name (die, cu);
10905
10906 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10907 but otherwise compute it by typename_concat inside GDB.
10908 FIXME: Actually this is not really true, or at least not always true.
10909 It's all very confusing. compute_and_set_names doesn't try to demangle
10910 Fortran names because there is no mangling standard. So new_symbol
10911 will set the demangled name to the result of dwarf2_full_name, and it is
10912 the demangled name that GDB uses if it exists. */
10913 if (cu->language == language_ada
10914 || (cu->language == language_fortran && physname))
10915 {
10916 /* For Ada unit, we prefer the linkage name over the name, as
10917 the former contains the exported name, which the user expects
10918 to be able to reference. Ideally, we want the user to be able
10919 to reference this entity using either natural or linkage name,
10920 but we haven't started looking at this enhancement yet. */
10921 const char *linkage_name = dw2_linkage_name (die, cu);
10922
10923 if (linkage_name != NULL)
10924 return linkage_name;
10925 }
10926
10927 /* These are the only languages we know how to qualify names in. */
10928 if (name != NULL
10929 && (cu->language == language_cplus
10930 || cu->language == language_fortran || cu->language == language_d
10931 || cu->language == language_rust))
10932 {
10933 if (die_needs_namespace (die, cu))
10934 {
10935 const char *prefix;
10936 const char *canonical_name = NULL;
10937
10938 string_file buf;
10939
10940 prefix = determine_prefix (die, cu);
10941 if (*prefix != '\0')
10942 {
10943 gdb::unique_xmalloc_ptr<char> prefixed_name
10944 (typename_concat (NULL, prefix, name, physname, cu));
10945
10946 buf.puts (prefixed_name.get ());
10947 }
10948 else
10949 buf.puts (name);
10950
10951 /* Template parameters may be specified in the DIE's DW_AT_name, or
10952 as children with DW_TAG_template_type_param or
10953 DW_TAG_value_type_param. If the latter, add them to the name
10954 here. If the name already has template parameters, then
10955 skip this step; some versions of GCC emit both, and
10956 it is more efficient to use the pre-computed name.
10957
10958 Something to keep in mind about this process: it is very
10959 unlikely, or in some cases downright impossible, to produce
10960 something that will match the mangled name of a function.
10961 If the definition of the function has the same debug info,
10962 we should be able to match up with it anyway. But fallbacks
10963 using the minimal symbol, for instance to find a method
10964 implemented in a stripped copy of libstdc++, will not work.
10965 If we do not have debug info for the definition, we will have to
10966 match them up some other way.
10967
10968 When we do name matching there is a related problem with function
10969 templates; two instantiated function templates are allowed to
10970 differ only by their return types, which we do not add here. */
10971
10972 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10973 {
10974 struct attribute *attr;
10975 struct die_info *child;
10976 int first = 1;
10977
10978 die->building_fullname = 1;
10979
10980 for (child = die->child; child != NULL; child = child->sibling)
10981 {
10982 struct type *type;
10983 LONGEST value;
10984 const gdb_byte *bytes;
10985 struct dwarf2_locexpr_baton *baton;
10986 struct value *v;
10987
10988 if (child->tag != DW_TAG_template_type_param
10989 && child->tag != DW_TAG_template_value_param)
10990 continue;
10991
10992 if (first)
10993 {
10994 buf.puts ("<");
10995 first = 0;
10996 }
10997 else
10998 buf.puts (", ");
10999
11000 attr = dwarf2_attr (child, DW_AT_type, cu);
11001 if (attr == NULL)
11002 {
11003 complaint (_("template parameter missing DW_AT_type"));
11004 buf.puts ("UNKNOWN_TYPE");
11005 continue;
11006 }
11007 type = die_type (child, cu);
11008
11009 if (child->tag == DW_TAG_template_type_param)
11010 {
11011 c_print_type (type, "", &buf, -1, 0, cu->language,
11012 &type_print_raw_options);
11013 continue;
11014 }
11015
11016 attr = dwarf2_attr (child, DW_AT_const_value, cu);
11017 if (attr == NULL)
11018 {
11019 complaint (_("template parameter missing "
11020 "DW_AT_const_value"));
11021 buf.puts ("UNKNOWN_VALUE");
11022 continue;
11023 }
11024
11025 dwarf2_const_value_attr (attr, type, name,
11026 &cu->comp_unit_obstack, cu,
11027 &value, &bytes, &baton);
11028
11029 if (TYPE_NOSIGN (type))
11030 /* GDB prints characters as NUMBER 'CHAR'. If that's
11031 changed, this can use value_print instead. */
11032 c_printchar (value, type, &buf);
11033 else
11034 {
11035 struct value_print_options opts;
11036
11037 if (baton != NULL)
11038 v = dwarf2_evaluate_loc_desc (type, NULL,
11039 baton->data,
11040 baton->size,
11041 baton->per_cu);
11042 else if (bytes != NULL)
11043 {
11044 v = allocate_value (type);
11045 memcpy (value_contents_writeable (v), bytes,
11046 TYPE_LENGTH (type));
11047 }
11048 else
11049 v = value_from_longest (type, value);
11050
11051 /* Specify decimal so that we do not depend on
11052 the radix. */
11053 get_formatted_print_options (&opts, 'd');
11054 opts.raw = 1;
11055 value_print (v, &buf, &opts);
11056 release_value (v);
11057 }
11058 }
11059
11060 die->building_fullname = 0;
11061
11062 if (!first)
11063 {
11064 /* Close the argument list, with a space if necessary
11065 (nested templates). */
11066 if (!buf.empty () && buf.string ().back () == '>')
11067 buf.puts (" >");
11068 else
11069 buf.puts (">");
11070 }
11071 }
11072
11073 /* For C++ methods, append formal parameter type
11074 information, if PHYSNAME. */
11075
11076 if (physname && die->tag == DW_TAG_subprogram
11077 && cu->language == language_cplus)
11078 {
11079 struct type *type = read_type_die (die, cu);
11080
11081 c_type_print_args (type, &buf, 1, cu->language,
11082 &type_print_raw_options);
11083
11084 if (cu->language == language_cplus)
11085 {
11086 /* Assume that an artificial first parameter is
11087 "this", but do not crash if it is not. RealView
11088 marks unnamed (and thus unused) parameters as
11089 artificial; there is no way to differentiate
11090 the two cases. */
11091 if (TYPE_NFIELDS (type) > 0
11092 && TYPE_FIELD_ARTIFICIAL (type, 0)
11093 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
11094 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
11095 0))))
11096 buf.puts (" const");
11097 }
11098 }
11099
11100 const std::string &intermediate_name = buf.string ();
11101
11102 if (cu->language == language_cplus)
11103 canonical_name
11104 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
11105 &objfile->per_bfd->storage_obstack);
11106
11107 /* If we only computed INTERMEDIATE_NAME, or if
11108 INTERMEDIATE_NAME is already canonical, then we need to
11109 copy it to the appropriate obstack. */
11110 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
11111 name = obstack_strdup (&objfile->per_bfd->storage_obstack,
11112 intermediate_name);
11113 else
11114 name = canonical_name;
11115 }
11116 }
11117
11118 return name;
11119 }
11120
11121 /* Return the fully qualified name of DIE, based on its DW_AT_name.
11122 If scope qualifiers are appropriate they will be added. The result
11123 will be allocated on the storage_obstack, or NULL if the DIE does
11124 not have a name. NAME may either be from a previous call to
11125 dwarf2_name or NULL.
11126
11127 The output string will be canonicalized (if C++). */
11128
11129 static const char *
11130 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
11131 {
11132 return dwarf2_compute_name (name, die, cu, 0);
11133 }
11134
11135 /* Construct a physname for the given DIE in CU. NAME may either be
11136 from a previous call to dwarf2_name or NULL. The result will be
11137 allocated on the objfile_objstack or NULL if the DIE does not have a
11138 name.
11139
11140 The output string will be canonicalized (if C++). */
11141
11142 static const char *
11143 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
11144 {
11145 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
11146 const char *retval, *mangled = NULL, *canon = NULL;
11147 int need_copy = 1;
11148
11149 /* In this case dwarf2_compute_name is just a shortcut not building anything
11150 on its own. */
11151 if (!die_needs_namespace (die, cu))
11152 return dwarf2_compute_name (name, die, cu, 1);
11153
11154 mangled = dw2_linkage_name (die, cu);
11155
11156 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
11157 See https://github.com/rust-lang/rust/issues/32925. */
11158 if (cu->language == language_rust && mangled != NULL
11159 && strchr (mangled, '{') != NULL)
11160 mangled = NULL;
11161
11162 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
11163 has computed. */
11164 gdb::unique_xmalloc_ptr<char> demangled;
11165 if (mangled != NULL)
11166 {
11167
11168 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
11169 {
11170 /* Do nothing (do not demangle the symbol name). */
11171 }
11172 else if (cu->language == language_go)
11173 {
11174 /* This is a lie, but we already lie to the caller new_symbol.
11175 new_symbol assumes we return the mangled name.
11176 This just undoes that lie until things are cleaned up. */
11177 }
11178 else
11179 {
11180 /* Use DMGL_RET_DROP for C++ template functions to suppress
11181 their return type. It is easier for GDB users to search
11182 for such functions as `name(params)' than `long name(params)'.
11183 In such case the minimal symbol names do not match the full
11184 symbol names but for template functions there is never a need
11185 to look up their definition from their declaration so
11186 the only disadvantage remains the minimal symbol variant
11187 `long name(params)' does not have the proper inferior type. */
11188 demangled.reset (gdb_demangle (mangled,
11189 (DMGL_PARAMS | DMGL_ANSI
11190 | DMGL_RET_DROP)));
11191 }
11192 if (demangled)
11193 canon = demangled.get ();
11194 else
11195 {
11196 canon = mangled;
11197 need_copy = 0;
11198 }
11199 }
11200
11201 if (canon == NULL || check_physname)
11202 {
11203 const char *physname = dwarf2_compute_name (name, die, cu, 1);
11204
11205 if (canon != NULL && strcmp (physname, canon) != 0)
11206 {
11207 /* It may not mean a bug in GDB. The compiler could also
11208 compute DW_AT_linkage_name incorrectly. But in such case
11209 GDB would need to be bug-to-bug compatible. */
11210
11211 complaint (_("Computed physname <%s> does not match demangled <%s> "
11212 "(from linkage <%s>) - DIE at %s [in module %s]"),
11213 physname, canon, mangled, sect_offset_str (die->sect_off),
11214 objfile_name (objfile));
11215
11216 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
11217 is available here - over computed PHYSNAME. It is safer
11218 against both buggy GDB and buggy compilers. */
11219
11220 retval = canon;
11221 }
11222 else
11223 {
11224 retval = physname;
11225 need_copy = 0;
11226 }
11227 }
11228 else
11229 retval = canon;
11230
11231 if (need_copy)
11232 retval = obstack_strdup (&objfile->per_bfd->storage_obstack, retval);
11233
11234 return retval;
11235 }
11236
11237 /* Inspect DIE in CU for a namespace alias. If one exists, record
11238 a new symbol for it.
11239
11240 Returns 1 if a namespace alias was recorded, 0 otherwise. */
11241
11242 static int
11243 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
11244 {
11245 struct attribute *attr;
11246
11247 /* If the die does not have a name, this is not a namespace
11248 alias. */
11249 attr = dwarf2_attr (die, DW_AT_name, cu);
11250 if (attr != NULL)
11251 {
11252 int num;
11253 struct die_info *d = die;
11254 struct dwarf2_cu *imported_cu = cu;
11255
11256 /* If the compiler has nested DW_AT_imported_declaration DIEs,
11257 keep inspecting DIEs until we hit the underlying import. */
11258 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
11259 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
11260 {
11261 attr = dwarf2_attr (d, DW_AT_import, cu);
11262 if (attr == NULL)
11263 break;
11264
11265 d = follow_die_ref (d, attr, &imported_cu);
11266 if (d->tag != DW_TAG_imported_declaration)
11267 break;
11268 }
11269
11270 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
11271 {
11272 complaint (_("DIE at %s has too many recursively imported "
11273 "declarations"), sect_offset_str (d->sect_off));
11274 return 0;
11275 }
11276
11277 if (attr != NULL)
11278 {
11279 struct type *type;
11280 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
11281
11282 type = get_die_type_at_offset (sect_off, cu->per_cu);
11283 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
11284 {
11285 /* This declaration is a global namespace alias. Add
11286 a symbol for it whose type is the aliased namespace. */
11287 new_symbol (die, type, cu);
11288 return 1;
11289 }
11290 }
11291 }
11292
11293 return 0;
11294 }
11295
11296 /* Return the using directives repository (global or local?) to use in the
11297 current context for CU.
11298
11299 For Ada, imported declarations can materialize renamings, which *may* be
11300 global. However it is impossible (for now?) in DWARF to distinguish
11301 "external" imported declarations and "static" ones. As all imported
11302 declarations seem to be static in all other languages, make them all CU-wide
11303 global only in Ada. */
11304
11305 static struct using_direct **
11306 using_directives (struct dwarf2_cu *cu)
11307 {
11308 if (cu->language == language_ada
11309 && cu->get_builder ()->outermost_context_p ())
11310 return cu->get_builder ()->get_global_using_directives ();
11311 else
11312 return cu->get_builder ()->get_local_using_directives ();
11313 }
11314
11315 /* Read the import statement specified by the given die and record it. */
11316
11317 static void
11318 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
11319 {
11320 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
11321 struct attribute *import_attr;
11322 struct die_info *imported_die, *child_die;
11323 struct dwarf2_cu *imported_cu;
11324 const char *imported_name;
11325 const char *imported_name_prefix;
11326 const char *canonical_name;
11327 const char *import_alias;
11328 const char *imported_declaration = NULL;
11329 const char *import_prefix;
11330 std::vector<const char *> excludes;
11331
11332 import_attr = dwarf2_attr (die, DW_AT_import, cu);
11333 if (import_attr == NULL)
11334 {
11335 complaint (_("Tag '%s' has no DW_AT_import"),
11336 dwarf_tag_name (die->tag));
11337 return;
11338 }
11339
11340 imported_cu = cu;
11341 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
11342 imported_name = dwarf2_name (imported_die, imported_cu);
11343 if (imported_name == NULL)
11344 {
11345 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
11346
11347 The import in the following code:
11348 namespace A
11349 {
11350 typedef int B;
11351 }
11352
11353 int main ()
11354 {
11355 using A::B;
11356 B b;
11357 return b;
11358 }
11359
11360 ...
11361 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
11362 <52> DW_AT_decl_file : 1
11363 <53> DW_AT_decl_line : 6
11364 <54> DW_AT_import : <0x75>
11365 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
11366 <59> DW_AT_name : B
11367 <5b> DW_AT_decl_file : 1
11368 <5c> DW_AT_decl_line : 2
11369 <5d> DW_AT_type : <0x6e>
11370 ...
11371 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
11372 <76> DW_AT_byte_size : 4
11373 <77> DW_AT_encoding : 5 (signed)
11374
11375 imports the wrong die ( 0x75 instead of 0x58 ).
11376 This case will be ignored until the gcc bug is fixed. */
11377 return;
11378 }
11379
11380 /* Figure out the local name after import. */
11381 import_alias = dwarf2_name (die, cu);
11382
11383 /* Figure out where the statement is being imported to. */
11384 import_prefix = determine_prefix (die, cu);
11385
11386 /* Figure out what the scope of the imported die is and prepend it
11387 to the name of the imported die. */
11388 imported_name_prefix = determine_prefix (imported_die, imported_cu);
11389
11390 if (imported_die->tag != DW_TAG_namespace
11391 && imported_die->tag != DW_TAG_module)
11392 {
11393 imported_declaration = imported_name;
11394 canonical_name = imported_name_prefix;
11395 }
11396 else if (strlen (imported_name_prefix) > 0)
11397 canonical_name = obconcat (&objfile->objfile_obstack,
11398 imported_name_prefix,
11399 (cu->language == language_d ? "." : "::"),
11400 imported_name, (char *) NULL);
11401 else
11402 canonical_name = imported_name;
11403
11404 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
11405 for (child_die = die->child; child_die && child_die->tag;
11406 child_die = sibling_die (child_die))
11407 {
11408 /* DWARF-4: A Fortran use statement with a “rename list” may be
11409 represented by an imported module entry with an import attribute
11410 referring to the module and owned entries corresponding to those
11411 entities that are renamed as part of being imported. */
11412
11413 if (child_die->tag != DW_TAG_imported_declaration)
11414 {
11415 complaint (_("child DW_TAG_imported_declaration expected "
11416 "- DIE at %s [in module %s]"),
11417 sect_offset_str (child_die->sect_off),
11418 objfile_name (objfile));
11419 continue;
11420 }
11421
11422 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
11423 if (import_attr == NULL)
11424 {
11425 complaint (_("Tag '%s' has no DW_AT_import"),
11426 dwarf_tag_name (child_die->tag));
11427 continue;
11428 }
11429
11430 imported_cu = cu;
11431 imported_die = follow_die_ref_or_sig (child_die, import_attr,
11432 &imported_cu);
11433 imported_name = dwarf2_name (imported_die, imported_cu);
11434 if (imported_name == NULL)
11435 {
11436 complaint (_("child DW_TAG_imported_declaration has unknown "
11437 "imported name - DIE at %s [in module %s]"),
11438 sect_offset_str (child_die->sect_off),
11439 objfile_name (objfile));
11440 continue;
11441 }
11442
11443 excludes.push_back (imported_name);
11444
11445 process_die (child_die, cu);
11446 }
11447
11448 add_using_directive (using_directives (cu),
11449 import_prefix,
11450 canonical_name,
11451 import_alias,
11452 imported_declaration,
11453 excludes,
11454 0,
11455 &objfile->objfile_obstack);
11456 }
11457
11458 /* ICC<14 does not output the required DW_AT_declaration on incomplete
11459 types, but gives them a size of zero. Starting with version 14,
11460 ICC is compatible with GCC. */
11461
11462 static bool
11463 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
11464 {
11465 if (!cu->checked_producer)
11466 check_producer (cu);
11467
11468 return cu->producer_is_icc_lt_14;
11469 }
11470
11471 /* ICC generates a DW_AT_type for C void functions. This was observed on
11472 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
11473 which says that void functions should not have a DW_AT_type. */
11474
11475 static bool
11476 producer_is_icc (struct dwarf2_cu *cu)
11477 {
11478 if (!cu->checked_producer)
11479 check_producer (cu);
11480
11481 return cu->producer_is_icc;
11482 }
11483
11484 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11485 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11486 this, it was first present in GCC release 4.3.0. */
11487
11488 static bool
11489 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11490 {
11491 if (!cu->checked_producer)
11492 check_producer (cu);
11493
11494 return cu->producer_is_gcc_lt_4_3;
11495 }
11496
11497 static file_and_directory
11498 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
11499 {
11500 file_and_directory res;
11501
11502 /* Find the filename. Do not use dwarf2_name here, since the filename
11503 is not a source language identifier. */
11504 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11505 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
11506
11507 if (res.comp_dir == NULL
11508 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11509 && IS_ABSOLUTE_PATH (res.name))
11510 {
11511 res.comp_dir_storage = ldirname (res.name);
11512 if (!res.comp_dir_storage.empty ())
11513 res.comp_dir = res.comp_dir_storage.c_str ();
11514 }
11515 if (res.comp_dir != NULL)
11516 {
11517 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11518 directory, get rid of it. */
11519 const char *cp = strchr (res.comp_dir, ':');
11520
11521 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11522 res.comp_dir = cp + 1;
11523 }
11524
11525 if (res.name == NULL)
11526 res.name = "<unknown>";
11527
11528 return res;
11529 }
11530
11531 /* Handle DW_AT_stmt_list for a compilation unit.
11532 DIE is the DW_TAG_compile_unit die for CU.
11533 COMP_DIR is the compilation directory. LOWPC is passed to
11534 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
11535
11536 static void
11537 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
11538 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
11539 {
11540 struct dwarf2_per_objfile *dwarf2_per_objfile
11541 = cu->per_cu->dwarf2_per_objfile;
11542 struct objfile *objfile = dwarf2_per_objfile->objfile;
11543 struct attribute *attr;
11544 struct line_header line_header_local;
11545 hashval_t line_header_local_hash;
11546 void **slot;
11547 int decode_mapping;
11548
11549 gdb_assert (! cu->per_cu->is_debug_types);
11550
11551 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
11552 if (attr == NULL)
11553 return;
11554
11555 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11556
11557 /* The line header hash table is only created if needed (it exists to
11558 prevent redundant reading of the line table for partial_units).
11559 If we're given a partial_unit, we'll need it. If we're given a
11560 compile_unit, then use the line header hash table if it's already
11561 created, but don't create one just yet. */
11562
11563 if (dwarf2_per_objfile->line_header_hash == NULL
11564 && die->tag == DW_TAG_partial_unit)
11565 {
11566 dwarf2_per_objfile->line_header_hash
11567 = htab_create_alloc_ex (127, line_header_hash_voidp,
11568 line_header_eq_voidp,
11569 free_line_header_voidp,
11570 &objfile->objfile_obstack,
11571 hashtab_obstack_allocate,
11572 dummy_obstack_deallocate);
11573 }
11574
11575 line_header_local.sect_off = line_offset;
11576 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11577 line_header_local_hash = line_header_hash (&line_header_local);
11578 if (dwarf2_per_objfile->line_header_hash != NULL)
11579 {
11580 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11581 &line_header_local,
11582 line_header_local_hash, NO_INSERT);
11583
11584 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11585 is not present in *SLOT (since if there is something in *SLOT then
11586 it will be for a partial_unit). */
11587 if (die->tag == DW_TAG_partial_unit && slot != NULL)
11588 {
11589 gdb_assert (*slot != NULL);
11590 cu->line_header = (struct line_header *) *slot;
11591 return;
11592 }
11593 }
11594
11595 /* dwarf_decode_line_header does not yet provide sufficient information.
11596 We always have to call also dwarf_decode_lines for it. */
11597 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11598 if (lh == NULL)
11599 return;
11600
11601 cu->line_header = lh.release ();
11602 cu->line_header_die_owner = die;
11603
11604 if (dwarf2_per_objfile->line_header_hash == NULL)
11605 slot = NULL;
11606 else
11607 {
11608 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11609 &line_header_local,
11610 line_header_local_hash, INSERT);
11611 gdb_assert (slot != NULL);
11612 }
11613 if (slot != NULL && *slot == NULL)
11614 {
11615 /* This newly decoded line number information unit will be owned
11616 by line_header_hash hash table. */
11617 *slot = cu->line_header;
11618 cu->line_header_die_owner = NULL;
11619 }
11620 else
11621 {
11622 /* We cannot free any current entry in (*slot) as that struct line_header
11623 may be already used by multiple CUs. Create only temporary decoded
11624 line_header for this CU - it may happen at most once for each line
11625 number information unit. And if we're not using line_header_hash
11626 then this is what we want as well. */
11627 gdb_assert (die->tag != DW_TAG_partial_unit);
11628 }
11629 decode_mapping = (die->tag != DW_TAG_partial_unit);
11630 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11631 decode_mapping);
11632
11633 }
11634
11635 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
11636
11637 static void
11638 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
11639 {
11640 struct dwarf2_per_objfile *dwarf2_per_objfile
11641 = cu->per_cu->dwarf2_per_objfile;
11642 struct objfile *objfile = dwarf2_per_objfile->objfile;
11643 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11644 CORE_ADDR lowpc = ((CORE_ADDR) -1);
11645 CORE_ADDR highpc = ((CORE_ADDR) 0);
11646 struct attribute *attr;
11647 struct die_info *child_die;
11648 CORE_ADDR baseaddr;
11649
11650 prepare_one_comp_unit (cu, die, cu->language);
11651 baseaddr = objfile->section_offsets[SECT_OFF_TEXT (objfile)];
11652
11653 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
11654
11655 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11656 from finish_block. */
11657 if (lowpc == ((CORE_ADDR) -1))
11658 lowpc = highpc;
11659 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11660
11661 file_and_directory fnd = find_file_and_directory (die, cu);
11662
11663 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11664 standardised yet. As a workaround for the language detection we fall
11665 back to the DW_AT_producer string. */
11666 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11667 cu->language = language_opencl;
11668
11669 /* Similar hack for Go. */
11670 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11671 set_cu_language (DW_LANG_Go, cu);
11672
11673 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
11674
11675 /* Decode line number information if present. We do this before
11676 processing child DIEs, so that the line header table is available
11677 for DW_AT_decl_file. */
11678 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
11679
11680 /* Process all dies in compilation unit. */
11681 if (die->child != NULL)
11682 {
11683 child_die = die->child;
11684 while (child_die && child_die->tag)
11685 {
11686 process_die (child_die, cu);
11687 child_die = sibling_die (child_die);
11688 }
11689 }
11690
11691 /* Decode macro information, if present. Dwarf 2 macro information
11692 refers to information in the line number info statement program
11693 header, so we can only read it if we've read the header
11694 successfully. */
11695 attr = dwarf2_attr (die, DW_AT_macros, cu);
11696 if (attr == NULL)
11697 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
11698 if (attr && cu->line_header)
11699 {
11700 if (dwarf2_attr (die, DW_AT_macro_info, cu))
11701 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
11702
11703 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
11704 }
11705 else
11706 {
11707 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11708 if (attr && cu->line_header)
11709 {
11710 unsigned int macro_offset = DW_UNSND (attr);
11711
11712 dwarf_decode_macros (cu, macro_offset, 0);
11713 }
11714 }
11715 }
11716
11717 void
11718 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
11719 {
11720 struct type_unit_group *tu_group;
11721 int first_time;
11722 struct attribute *attr;
11723 unsigned int i;
11724 struct signatured_type *sig_type;
11725
11726 gdb_assert (per_cu->is_debug_types);
11727 sig_type = (struct signatured_type *) per_cu;
11728
11729 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
11730
11731 /* If we're using .gdb_index (includes -readnow) then
11732 per_cu->type_unit_group may not have been set up yet. */
11733 if (sig_type->type_unit_group == NULL)
11734 sig_type->type_unit_group = get_type_unit_group (this, attr);
11735 tu_group = sig_type->type_unit_group;
11736
11737 /* If we've already processed this stmt_list there's no real need to
11738 do it again, we could fake it and just recreate the part we need
11739 (file name,index -> symtab mapping). If data shows this optimization
11740 is useful we can do it then. */
11741 first_time = tu_group->compunit_symtab == NULL;
11742
11743 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11744 debug info. */
11745 line_header_up lh;
11746 if (attr != NULL)
11747 {
11748 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11749 lh = dwarf_decode_line_header (line_offset, this);
11750 }
11751 if (lh == NULL)
11752 {
11753 if (first_time)
11754 start_symtab ("", NULL, 0);
11755 else
11756 {
11757 gdb_assert (tu_group->symtabs == NULL);
11758 gdb_assert (m_builder == nullptr);
11759 struct compunit_symtab *cust = tu_group->compunit_symtab;
11760 m_builder.reset (new struct buildsym_compunit
11761 (COMPUNIT_OBJFILE (cust), "",
11762 COMPUNIT_DIRNAME (cust),
11763 compunit_language (cust),
11764 0, cust));
11765 }
11766 return;
11767 }
11768
11769 line_header = lh.release ();
11770 line_header_die_owner = die;
11771
11772 if (first_time)
11773 {
11774 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
11775
11776 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11777 still initializing it, and our caller (a few levels up)
11778 process_full_type_unit still needs to know if this is the first
11779 time. */
11780
11781 tu_group->num_symtabs = line_header->file_names_size ();
11782 tu_group->symtabs = XNEWVEC (struct symtab *,
11783 line_header->file_names_size ());
11784
11785 auto &file_names = line_header->file_names ();
11786 for (i = 0; i < file_names.size (); ++i)
11787 {
11788 file_entry &fe = file_names[i];
11789 dwarf2_start_subfile (this, fe.name,
11790 fe.include_dir (line_header));
11791 buildsym_compunit *b = get_builder ();
11792 if (b->get_current_subfile ()->symtab == NULL)
11793 {
11794 /* NOTE: start_subfile will recognize when it's been
11795 passed a file it has already seen. So we can't
11796 assume there's a simple mapping from
11797 cu->line_header->file_names to subfiles, plus
11798 cu->line_header->file_names may contain dups. */
11799 b->get_current_subfile ()->symtab
11800 = allocate_symtab (cust, b->get_current_subfile ()->name);
11801 }
11802
11803 fe.symtab = b->get_current_subfile ()->symtab;
11804 tu_group->symtabs[i] = fe.symtab;
11805 }
11806 }
11807 else
11808 {
11809 gdb_assert (m_builder == nullptr);
11810 struct compunit_symtab *cust = tu_group->compunit_symtab;
11811 m_builder.reset (new struct buildsym_compunit
11812 (COMPUNIT_OBJFILE (cust), "",
11813 COMPUNIT_DIRNAME (cust),
11814 compunit_language (cust),
11815 0, cust));
11816
11817 auto &file_names = line_header->file_names ();
11818 for (i = 0; i < file_names.size (); ++i)
11819 {
11820 file_entry &fe = file_names[i];
11821 fe.symtab = tu_group->symtabs[i];
11822 }
11823 }
11824
11825 /* The main symtab is allocated last. Type units don't have DW_AT_name
11826 so they don't have a "real" (so to speak) symtab anyway.
11827 There is later code that will assign the main symtab to all symbols
11828 that don't have one. We need to handle the case of a symbol with a
11829 missing symtab (DW_AT_decl_file) anyway. */
11830 }
11831
11832 /* Process DW_TAG_type_unit.
11833 For TUs we want to skip the first top level sibling if it's not the
11834 actual type being defined by this TU. In this case the first top
11835 level sibling is there to provide context only. */
11836
11837 static void
11838 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11839 {
11840 struct die_info *child_die;
11841
11842 prepare_one_comp_unit (cu, die, language_minimal);
11843
11844 /* Initialize (or reinitialize) the machinery for building symtabs.
11845 We do this before processing child DIEs, so that the line header table
11846 is available for DW_AT_decl_file. */
11847 cu->setup_type_unit_groups (die);
11848
11849 if (die->child != NULL)
11850 {
11851 child_die = die->child;
11852 while (child_die && child_die->tag)
11853 {
11854 process_die (child_die, cu);
11855 child_die = sibling_die (child_die);
11856 }
11857 }
11858 }
11859 \f
11860 /* DWO/DWP files.
11861
11862 http://gcc.gnu.org/wiki/DebugFission
11863 http://gcc.gnu.org/wiki/DebugFissionDWP
11864
11865 To simplify handling of both DWO files ("object" files with the DWARF info)
11866 and DWP files (a file with the DWOs packaged up into one file), we treat
11867 DWP files as having a collection of virtual DWO files. */
11868
11869 static hashval_t
11870 hash_dwo_file (const void *item)
11871 {
11872 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
11873 hashval_t hash;
11874
11875 hash = htab_hash_string (dwo_file->dwo_name);
11876 if (dwo_file->comp_dir != NULL)
11877 hash += htab_hash_string (dwo_file->comp_dir);
11878 return hash;
11879 }
11880
11881 static int
11882 eq_dwo_file (const void *item_lhs, const void *item_rhs)
11883 {
11884 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11885 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
11886
11887 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11888 return 0;
11889 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11890 return lhs->comp_dir == rhs->comp_dir;
11891 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
11892 }
11893
11894 /* Allocate a hash table for DWO files. */
11895
11896 static htab_up
11897 allocate_dwo_file_hash_table (struct objfile *objfile)
11898 {
11899 auto delete_dwo_file = [] (void *item)
11900 {
11901 struct dwo_file *dwo_file = (struct dwo_file *) item;
11902
11903 delete dwo_file;
11904 };
11905
11906 return htab_up (htab_create_alloc_ex (41,
11907 hash_dwo_file,
11908 eq_dwo_file,
11909 delete_dwo_file,
11910 &objfile->objfile_obstack,
11911 hashtab_obstack_allocate,
11912 dummy_obstack_deallocate));
11913 }
11914
11915 /* Lookup DWO file DWO_NAME. */
11916
11917 static void **
11918 lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11919 const char *dwo_name,
11920 const char *comp_dir)
11921 {
11922 struct dwo_file find_entry;
11923 void **slot;
11924
11925 if (dwarf2_per_objfile->dwo_files == NULL)
11926 dwarf2_per_objfile->dwo_files
11927 = allocate_dwo_file_hash_table (dwarf2_per_objfile->objfile);
11928
11929 find_entry.dwo_name = dwo_name;
11930 find_entry.comp_dir = comp_dir;
11931 slot = htab_find_slot (dwarf2_per_objfile->dwo_files.get (), &find_entry,
11932 INSERT);
11933
11934 return slot;
11935 }
11936
11937 static hashval_t
11938 hash_dwo_unit (const void *item)
11939 {
11940 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11941
11942 /* This drops the top 32 bits of the id, but is ok for a hash. */
11943 return dwo_unit->signature;
11944 }
11945
11946 static int
11947 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11948 {
11949 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11950 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11951
11952 /* The signature is assumed to be unique within the DWO file.
11953 So while object file CU dwo_id's always have the value zero,
11954 that's OK, assuming each object file DWO file has only one CU,
11955 and that's the rule for now. */
11956 return lhs->signature == rhs->signature;
11957 }
11958
11959 /* Allocate a hash table for DWO CUs,TUs.
11960 There is one of these tables for each of CUs,TUs for each DWO file. */
11961
11962 static htab_t
11963 allocate_dwo_unit_table (struct objfile *objfile)
11964 {
11965 /* Start out with a pretty small number.
11966 Generally DWO files contain only one CU and maybe some TUs. */
11967 return htab_create_alloc_ex (3,
11968 hash_dwo_unit,
11969 eq_dwo_unit,
11970 NULL,
11971 &objfile->objfile_obstack,
11972 hashtab_obstack_allocate,
11973 dummy_obstack_deallocate);
11974 }
11975
11976 /* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
11977
11978 struct create_dwo_cu_data
11979 {
11980 struct dwo_file *dwo_file;
11981 struct dwo_unit dwo_unit;
11982 };
11983
11984 /* die_reader_func for create_dwo_cu. */
11985
11986 static void
11987 create_dwo_cu_reader (const struct die_reader_specs *reader,
11988 const gdb_byte *info_ptr,
11989 struct die_info *comp_unit_die,
11990 int has_children,
11991 void *datap)
11992 {
11993 struct dwarf2_cu *cu = reader->cu;
11994 sect_offset sect_off = cu->per_cu->sect_off;
11995 struct dwarf2_section_info *section = cu->per_cu->section;
11996 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
11997 struct dwo_file *dwo_file = data->dwo_file;
11998 struct dwo_unit *dwo_unit = &data->dwo_unit;
11999
12000 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
12001 if (!signature.has_value ())
12002 {
12003 complaint (_("Dwarf Error: debug entry at offset %s is missing"
12004 " its dwo_id [in module %s]"),
12005 sect_offset_str (sect_off), dwo_file->dwo_name);
12006 return;
12007 }
12008
12009 dwo_unit->dwo_file = dwo_file;
12010 dwo_unit->signature = *signature;
12011 dwo_unit->section = section;
12012 dwo_unit->sect_off = sect_off;
12013 dwo_unit->length = cu->per_cu->length;
12014
12015 if (dwarf_read_debug)
12016 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
12017 sect_offset_str (sect_off),
12018 hex_string (dwo_unit->signature));
12019 }
12020
12021 /* Create the dwo_units for the CUs in a DWO_FILE.
12022 Note: This function processes DWO files only, not DWP files. */
12023
12024 static void
12025 create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
12026 dwarf2_cu *cu, struct dwo_file &dwo_file,
12027 dwarf2_section_info &section, htab_t &cus_htab)
12028 {
12029 struct objfile *objfile = dwarf2_per_objfile->objfile;
12030 const gdb_byte *info_ptr, *end_ptr;
12031
12032 dwarf2_read_section (objfile, &section);
12033 info_ptr = section.buffer;
12034
12035 if (info_ptr == NULL)
12036 return;
12037
12038 if (dwarf_read_debug)
12039 {
12040 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
12041 get_section_name (&section),
12042 get_section_file_name (&section));
12043 }
12044
12045 end_ptr = info_ptr + section.size;
12046 while (info_ptr < end_ptr)
12047 {
12048 struct dwarf2_per_cu_data per_cu;
12049 struct create_dwo_cu_data create_dwo_cu_data;
12050 struct dwo_unit *dwo_unit;
12051 void **slot;
12052 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
12053
12054 memset (&create_dwo_cu_data.dwo_unit, 0,
12055 sizeof (create_dwo_cu_data.dwo_unit));
12056 memset (&per_cu, 0, sizeof (per_cu));
12057 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
12058 per_cu.is_debug_types = 0;
12059 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
12060 per_cu.section = &section;
12061 create_dwo_cu_data.dwo_file = &dwo_file;
12062
12063 init_cutu_and_read_dies_no_follow (
12064 &per_cu, cu, &dwo_file, create_dwo_cu_reader, &create_dwo_cu_data);
12065 info_ptr += per_cu.length;
12066
12067 // If the unit could not be parsed, skip it.
12068 if (create_dwo_cu_data.dwo_unit.dwo_file == NULL)
12069 continue;
12070
12071 if (cus_htab == NULL)
12072 cus_htab = allocate_dwo_unit_table (objfile);
12073
12074 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12075 *dwo_unit = create_dwo_cu_data.dwo_unit;
12076 slot = htab_find_slot (cus_htab, dwo_unit, INSERT);
12077 gdb_assert (slot != NULL);
12078 if (*slot != NULL)
12079 {
12080 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
12081 sect_offset dup_sect_off = dup_cu->sect_off;
12082
12083 complaint (_("debug cu entry at offset %s is duplicate to"
12084 " the entry at offset %s, signature %s"),
12085 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
12086 hex_string (dwo_unit->signature));
12087 }
12088 *slot = (void *)dwo_unit;
12089 }
12090 }
12091
12092 /* DWP file .debug_{cu,tu}_index section format:
12093 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
12094
12095 DWP Version 1:
12096
12097 Both index sections have the same format, and serve to map a 64-bit
12098 signature to a set of section numbers. Each section begins with a header,
12099 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
12100 indexes, and a pool of 32-bit section numbers. The index sections will be
12101 aligned at 8-byte boundaries in the file.
12102
12103 The index section header consists of:
12104
12105 V, 32 bit version number
12106 -, 32 bits unused
12107 N, 32 bit number of compilation units or type units in the index
12108 M, 32 bit number of slots in the hash table
12109
12110 Numbers are recorded using the byte order of the application binary.
12111
12112 The hash table begins at offset 16 in the section, and consists of an array
12113 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
12114 order of the application binary). Unused slots in the hash table are 0.
12115 (We rely on the extreme unlikeliness of a signature being exactly 0.)
12116
12117 The parallel table begins immediately after the hash table
12118 (at offset 16 + 8 * M from the beginning of the section), and consists of an
12119 array of 32-bit indexes (using the byte order of the application binary),
12120 corresponding 1-1 with slots in the hash table. Each entry in the parallel
12121 table contains a 32-bit index into the pool of section numbers. For unused
12122 hash table slots, the corresponding entry in the parallel table will be 0.
12123
12124 The pool of section numbers begins immediately following the hash table
12125 (at offset 16 + 12 * M from the beginning of the section). The pool of
12126 section numbers consists of an array of 32-bit words (using the byte order
12127 of the application binary). Each item in the array is indexed starting
12128 from 0. The hash table entry provides the index of the first section
12129 number in the set. Additional section numbers in the set follow, and the
12130 set is terminated by a 0 entry (section number 0 is not used in ELF).
12131
12132 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
12133 section must be the first entry in the set, and the .debug_abbrev.dwo must
12134 be the second entry. Other members of the set may follow in any order.
12135
12136 ---
12137
12138 DWP Version 2:
12139
12140 DWP Version 2 combines all the .debug_info, etc. sections into one,
12141 and the entries in the index tables are now offsets into these sections.
12142 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
12143 section.
12144
12145 Index Section Contents:
12146 Header
12147 Hash Table of Signatures dwp_hash_table.hash_table
12148 Parallel Table of Indices dwp_hash_table.unit_table
12149 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
12150 Table of Section Sizes dwp_hash_table.v2.sizes
12151
12152 The index section header consists of:
12153
12154 V, 32 bit version number
12155 L, 32 bit number of columns in the table of section offsets
12156 N, 32 bit number of compilation units or type units in the index
12157 M, 32 bit number of slots in the hash table
12158
12159 Numbers are recorded using the byte order of the application binary.
12160
12161 The hash table has the same format as version 1.
12162 The parallel table of indices has the same format as version 1,
12163 except that the entries are origin-1 indices into the table of sections
12164 offsets and the table of section sizes.
12165
12166 The table of offsets begins immediately following the parallel table
12167 (at offset 16 + 12 * M from the beginning of the section). The table is
12168 a two-dimensional array of 32-bit words (using the byte order of the
12169 application binary), with L columns and N+1 rows, in row-major order.
12170 Each row in the array is indexed starting from 0. The first row provides
12171 a key to the remaining rows: each column in this row provides an identifier
12172 for a debug section, and the offsets in the same column of subsequent rows
12173 refer to that section. The section identifiers are:
12174
12175 DW_SECT_INFO 1 .debug_info.dwo
12176 DW_SECT_TYPES 2 .debug_types.dwo
12177 DW_SECT_ABBREV 3 .debug_abbrev.dwo
12178 DW_SECT_LINE 4 .debug_line.dwo
12179 DW_SECT_LOC 5 .debug_loc.dwo
12180 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
12181 DW_SECT_MACINFO 7 .debug_macinfo.dwo
12182 DW_SECT_MACRO 8 .debug_macro.dwo
12183
12184 The offsets provided by the CU and TU index sections are the base offsets
12185 for the contributions made by each CU or TU to the corresponding section
12186 in the package file. Each CU and TU header contains an abbrev_offset
12187 field, used to find the abbreviations table for that CU or TU within the
12188 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
12189 be interpreted as relative to the base offset given in the index section.
12190 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
12191 should be interpreted as relative to the base offset for .debug_line.dwo,
12192 and offsets into other debug sections obtained from DWARF attributes should
12193 also be interpreted as relative to the corresponding base offset.
12194
12195 The table of sizes begins immediately following the table of offsets.
12196 Like the table of offsets, it is a two-dimensional array of 32-bit words,
12197 with L columns and N rows, in row-major order. Each row in the array is
12198 indexed starting from 1 (row 0 is shared by the two tables).
12199
12200 ---
12201
12202 Hash table lookup is handled the same in version 1 and 2:
12203
12204 We assume that N and M will not exceed 2^32 - 1.
12205 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
12206
12207 Given a 64-bit compilation unit signature or a type signature S, an entry
12208 in the hash table is located as follows:
12209
12210 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
12211 the low-order k bits all set to 1.
12212
12213 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
12214
12215 3) If the hash table entry at index H matches the signature, use that
12216 entry. If the hash table entry at index H is unused (all zeroes),
12217 terminate the search: the signature is not present in the table.
12218
12219 4) Let H = (H + H') modulo M. Repeat at Step 3.
12220
12221 Because M > N and H' and M are relatively prime, the search is guaranteed
12222 to stop at an unused slot or find the match. */
12223
12224 /* Create a hash table to map DWO IDs to their CU/TU entry in
12225 .debug_{info,types}.dwo in DWP_FILE.
12226 Returns NULL if there isn't one.
12227 Note: This function processes DWP files only, not DWO files. */
12228
12229 static struct dwp_hash_table *
12230 create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
12231 struct dwp_file *dwp_file, int is_debug_types)
12232 {
12233 struct objfile *objfile = dwarf2_per_objfile->objfile;
12234 bfd *dbfd = dwp_file->dbfd.get ();
12235 const gdb_byte *index_ptr, *index_end;
12236 struct dwarf2_section_info *index;
12237 uint32_t version, nr_columns, nr_units, nr_slots;
12238 struct dwp_hash_table *htab;
12239
12240 if (is_debug_types)
12241 index = &dwp_file->sections.tu_index;
12242 else
12243 index = &dwp_file->sections.cu_index;
12244
12245 if (dwarf2_section_empty_p (index))
12246 return NULL;
12247 dwarf2_read_section (objfile, index);
12248
12249 index_ptr = index->buffer;
12250 index_end = index_ptr + index->size;
12251
12252 version = read_4_bytes (dbfd, index_ptr);
12253 index_ptr += 4;
12254 if (version == 2)
12255 nr_columns = read_4_bytes (dbfd, index_ptr);
12256 else
12257 nr_columns = 0;
12258 index_ptr += 4;
12259 nr_units = read_4_bytes (dbfd, index_ptr);
12260 index_ptr += 4;
12261 nr_slots = read_4_bytes (dbfd, index_ptr);
12262 index_ptr += 4;
12263
12264 if (version != 1 && version != 2)
12265 {
12266 error (_("Dwarf Error: unsupported DWP file version (%s)"
12267 " [in module %s]"),
12268 pulongest (version), dwp_file->name);
12269 }
12270 if (nr_slots != (nr_slots & -nr_slots))
12271 {
12272 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
12273 " is not power of 2 [in module %s]"),
12274 pulongest (nr_slots), dwp_file->name);
12275 }
12276
12277 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
12278 htab->version = version;
12279 htab->nr_columns = nr_columns;
12280 htab->nr_units = nr_units;
12281 htab->nr_slots = nr_slots;
12282 htab->hash_table = index_ptr;
12283 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
12284
12285 /* Exit early if the table is empty. */
12286 if (nr_slots == 0 || nr_units == 0
12287 || (version == 2 && nr_columns == 0))
12288 {
12289 /* All must be zero. */
12290 if (nr_slots != 0 || nr_units != 0
12291 || (version == 2 && nr_columns != 0))
12292 {
12293 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
12294 " all zero [in modules %s]"),
12295 dwp_file->name);
12296 }
12297 return htab;
12298 }
12299
12300 if (version == 1)
12301 {
12302 htab->section_pool.v1.indices =
12303 htab->unit_table + sizeof (uint32_t) * nr_slots;
12304 /* It's harder to decide whether the section is too small in v1.
12305 V1 is deprecated anyway so we punt. */
12306 }
12307 else
12308 {
12309 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
12310 int *ids = htab->section_pool.v2.section_ids;
12311 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
12312 /* Reverse map for error checking. */
12313 int ids_seen[DW_SECT_MAX + 1];
12314 int i;
12315
12316 if (nr_columns < 2)
12317 {
12318 error (_("Dwarf Error: bad DWP hash table, too few columns"
12319 " in section table [in module %s]"),
12320 dwp_file->name);
12321 }
12322 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
12323 {
12324 error (_("Dwarf Error: bad DWP hash table, too many columns"
12325 " in section table [in module %s]"),
12326 dwp_file->name);
12327 }
12328 memset (ids, 255, sizeof_ids);
12329 memset (ids_seen, 255, sizeof (ids_seen));
12330 for (i = 0; i < nr_columns; ++i)
12331 {
12332 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
12333
12334 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
12335 {
12336 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
12337 " in section table [in module %s]"),
12338 id, dwp_file->name);
12339 }
12340 if (ids_seen[id] != -1)
12341 {
12342 error (_("Dwarf Error: bad DWP hash table, duplicate section"
12343 " id %d in section table [in module %s]"),
12344 id, dwp_file->name);
12345 }
12346 ids_seen[id] = i;
12347 ids[i] = id;
12348 }
12349 /* Must have exactly one info or types section. */
12350 if (((ids_seen[DW_SECT_INFO] != -1)
12351 + (ids_seen[DW_SECT_TYPES] != -1))
12352 != 1)
12353 {
12354 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
12355 " DWO info/types section [in module %s]"),
12356 dwp_file->name);
12357 }
12358 /* Must have an abbrev section. */
12359 if (ids_seen[DW_SECT_ABBREV] == -1)
12360 {
12361 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
12362 " section [in module %s]"),
12363 dwp_file->name);
12364 }
12365 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
12366 htab->section_pool.v2.sizes =
12367 htab->section_pool.v2.offsets + (sizeof (uint32_t)
12368 * nr_units * nr_columns);
12369 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
12370 * nr_units * nr_columns))
12371 > index_end)
12372 {
12373 error (_("Dwarf Error: DWP index section is corrupt (too small)"
12374 " [in module %s]"),
12375 dwp_file->name);
12376 }
12377 }
12378
12379 return htab;
12380 }
12381
12382 /* Update SECTIONS with the data from SECTP.
12383
12384 This function is like the other "locate" section routines that are
12385 passed to bfd_map_over_sections, but in this context the sections to
12386 read comes from the DWP V1 hash table, not the full ELF section table.
12387
12388 The result is non-zero for success, or zero if an error was found. */
12389
12390 static int
12391 locate_v1_virtual_dwo_sections (asection *sectp,
12392 struct virtual_v1_dwo_sections *sections)
12393 {
12394 const struct dwop_section_names *names = &dwop_section_names;
12395
12396 if (section_is_p (sectp->name, &names->abbrev_dwo))
12397 {
12398 /* There can be only one. */
12399 if (sections->abbrev.s.section != NULL)
12400 return 0;
12401 sections->abbrev.s.section = sectp;
12402 sections->abbrev.size = bfd_section_size (sectp);
12403 }
12404 else if (section_is_p (sectp->name, &names->info_dwo)
12405 || section_is_p (sectp->name, &names->types_dwo))
12406 {
12407 /* There can be only one. */
12408 if (sections->info_or_types.s.section != NULL)
12409 return 0;
12410 sections->info_or_types.s.section = sectp;
12411 sections->info_or_types.size = bfd_section_size (sectp);
12412 }
12413 else if (section_is_p (sectp->name, &names->line_dwo))
12414 {
12415 /* There can be only one. */
12416 if (sections->line.s.section != NULL)
12417 return 0;
12418 sections->line.s.section = sectp;
12419 sections->line.size = bfd_section_size (sectp);
12420 }
12421 else if (section_is_p (sectp->name, &names->loc_dwo))
12422 {
12423 /* There can be only one. */
12424 if (sections->loc.s.section != NULL)
12425 return 0;
12426 sections->loc.s.section = sectp;
12427 sections->loc.size = bfd_section_size (sectp);
12428 }
12429 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12430 {
12431 /* There can be only one. */
12432 if (sections->macinfo.s.section != NULL)
12433 return 0;
12434 sections->macinfo.s.section = sectp;
12435 sections->macinfo.size = bfd_section_size (sectp);
12436 }
12437 else if (section_is_p (sectp->name, &names->macro_dwo))
12438 {
12439 /* There can be only one. */
12440 if (sections->macro.s.section != NULL)
12441 return 0;
12442 sections->macro.s.section = sectp;
12443 sections->macro.size = bfd_section_size (sectp);
12444 }
12445 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12446 {
12447 /* There can be only one. */
12448 if (sections->str_offsets.s.section != NULL)
12449 return 0;
12450 sections->str_offsets.s.section = sectp;
12451 sections->str_offsets.size = bfd_section_size (sectp);
12452 }
12453 else
12454 {
12455 /* No other kind of section is valid. */
12456 return 0;
12457 }
12458
12459 return 1;
12460 }
12461
12462 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12463 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12464 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12465 This is for DWP version 1 files. */
12466
12467 static struct dwo_unit *
12468 create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12469 struct dwp_file *dwp_file,
12470 uint32_t unit_index,
12471 const char *comp_dir,
12472 ULONGEST signature, int is_debug_types)
12473 {
12474 struct objfile *objfile = dwarf2_per_objfile->objfile;
12475 const struct dwp_hash_table *dwp_htab =
12476 is_debug_types ? dwp_file->tus : dwp_file->cus;
12477 bfd *dbfd = dwp_file->dbfd.get ();
12478 const char *kind = is_debug_types ? "TU" : "CU";
12479 struct dwo_file *dwo_file;
12480 struct dwo_unit *dwo_unit;
12481 struct virtual_v1_dwo_sections sections;
12482 void **dwo_file_slot;
12483 int i;
12484
12485 gdb_assert (dwp_file->version == 1);
12486
12487 if (dwarf_read_debug)
12488 {
12489 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
12490 kind,
12491 pulongest (unit_index), hex_string (signature),
12492 dwp_file->name);
12493 }
12494
12495 /* Fetch the sections of this DWO unit.
12496 Put a limit on the number of sections we look for so that bad data
12497 doesn't cause us to loop forever. */
12498
12499 #define MAX_NR_V1_DWO_SECTIONS \
12500 (1 /* .debug_info or .debug_types */ \
12501 + 1 /* .debug_abbrev */ \
12502 + 1 /* .debug_line */ \
12503 + 1 /* .debug_loc */ \
12504 + 1 /* .debug_str_offsets */ \
12505 + 1 /* .debug_macro or .debug_macinfo */ \
12506 + 1 /* trailing zero */)
12507
12508 memset (&sections, 0, sizeof (sections));
12509
12510 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
12511 {
12512 asection *sectp;
12513 uint32_t section_nr =
12514 read_4_bytes (dbfd,
12515 dwp_htab->section_pool.v1.indices
12516 + (unit_index + i) * sizeof (uint32_t));
12517
12518 if (section_nr == 0)
12519 break;
12520 if (section_nr >= dwp_file->num_sections)
12521 {
12522 error (_("Dwarf Error: bad DWP hash table, section number too large"
12523 " [in module %s]"),
12524 dwp_file->name);
12525 }
12526
12527 sectp = dwp_file->elf_sections[section_nr];
12528 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
12529 {
12530 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12531 " [in module %s]"),
12532 dwp_file->name);
12533 }
12534 }
12535
12536 if (i < 2
12537 || dwarf2_section_empty_p (&sections.info_or_types)
12538 || dwarf2_section_empty_p (&sections.abbrev))
12539 {
12540 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12541 " [in module %s]"),
12542 dwp_file->name);
12543 }
12544 if (i == MAX_NR_V1_DWO_SECTIONS)
12545 {
12546 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12547 " [in module %s]"),
12548 dwp_file->name);
12549 }
12550
12551 /* It's easier for the rest of the code if we fake a struct dwo_file and
12552 have dwo_unit "live" in that. At least for now.
12553
12554 The DWP file can be made up of a random collection of CUs and TUs.
12555 However, for each CU + set of TUs that came from the same original DWO
12556 file, we can combine them back into a virtual DWO file to save space
12557 (fewer struct dwo_file objects to allocate). Remember that for really
12558 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12559
12560 std::string virtual_dwo_name =
12561 string_printf ("virtual-dwo/%d-%d-%d-%d",
12562 get_section_id (&sections.abbrev),
12563 get_section_id (&sections.line),
12564 get_section_id (&sections.loc),
12565 get_section_id (&sections.str_offsets));
12566 /* Can we use an existing virtual DWO file? */
12567 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12568 virtual_dwo_name.c_str (),
12569 comp_dir);
12570 /* Create one if necessary. */
12571 if (*dwo_file_slot == NULL)
12572 {
12573 if (dwarf_read_debug)
12574 {
12575 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12576 virtual_dwo_name.c_str ());
12577 }
12578 dwo_file = new struct dwo_file;
12579 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
12580 virtual_dwo_name);
12581 dwo_file->comp_dir = comp_dir;
12582 dwo_file->sections.abbrev = sections.abbrev;
12583 dwo_file->sections.line = sections.line;
12584 dwo_file->sections.loc = sections.loc;
12585 dwo_file->sections.macinfo = sections.macinfo;
12586 dwo_file->sections.macro = sections.macro;
12587 dwo_file->sections.str_offsets = sections.str_offsets;
12588 /* The "str" section is global to the entire DWP file. */
12589 dwo_file->sections.str = dwp_file->sections.str;
12590 /* The info or types section is assigned below to dwo_unit,
12591 there's no need to record it in dwo_file.
12592 Also, we can't simply record type sections in dwo_file because
12593 we record a pointer into the vector in dwo_unit. As we collect more
12594 types we'll grow the vector and eventually have to reallocate space
12595 for it, invalidating all copies of pointers into the previous
12596 contents. */
12597 *dwo_file_slot = dwo_file;
12598 }
12599 else
12600 {
12601 if (dwarf_read_debug)
12602 {
12603 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12604 virtual_dwo_name.c_str ());
12605 }
12606 dwo_file = (struct dwo_file *) *dwo_file_slot;
12607 }
12608
12609 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12610 dwo_unit->dwo_file = dwo_file;
12611 dwo_unit->signature = signature;
12612 dwo_unit->section =
12613 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
12614 *dwo_unit->section = sections.info_or_types;
12615 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12616
12617 return dwo_unit;
12618 }
12619
12620 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
12621 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
12622 piece within that section used by a TU/CU, return a virtual section
12623 of just that piece. */
12624
12625 static struct dwarf2_section_info
12626 create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
12627 struct dwarf2_section_info *section,
12628 bfd_size_type offset, bfd_size_type size)
12629 {
12630 struct dwarf2_section_info result;
12631 asection *sectp;
12632
12633 gdb_assert (section != NULL);
12634 gdb_assert (!section->is_virtual);
12635
12636 memset (&result, 0, sizeof (result));
12637 result.s.containing_section = section;
12638 result.is_virtual = true;
12639
12640 if (size == 0)
12641 return result;
12642
12643 sectp = get_section_bfd_section (section);
12644
12645 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12646 bounds of the real section. This is a pretty-rare event, so just
12647 flag an error (easier) instead of a warning and trying to cope. */
12648 if (sectp == NULL
12649 || offset + size > bfd_section_size (sectp))
12650 {
12651 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
12652 " in section %s [in module %s]"),
12653 sectp ? bfd_section_name (sectp) : "<unknown>",
12654 objfile_name (dwarf2_per_objfile->objfile));
12655 }
12656
12657 result.virtual_offset = offset;
12658 result.size = size;
12659 return result;
12660 }
12661
12662 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12663 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12664 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12665 This is for DWP version 2 files. */
12666
12667 static struct dwo_unit *
12668 create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12669 struct dwp_file *dwp_file,
12670 uint32_t unit_index,
12671 const char *comp_dir,
12672 ULONGEST signature, int is_debug_types)
12673 {
12674 struct objfile *objfile = dwarf2_per_objfile->objfile;
12675 const struct dwp_hash_table *dwp_htab =
12676 is_debug_types ? dwp_file->tus : dwp_file->cus;
12677 bfd *dbfd = dwp_file->dbfd.get ();
12678 const char *kind = is_debug_types ? "TU" : "CU";
12679 struct dwo_file *dwo_file;
12680 struct dwo_unit *dwo_unit;
12681 struct virtual_v2_dwo_sections sections;
12682 void **dwo_file_slot;
12683 int i;
12684
12685 gdb_assert (dwp_file->version == 2);
12686
12687 if (dwarf_read_debug)
12688 {
12689 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12690 kind,
12691 pulongest (unit_index), hex_string (signature),
12692 dwp_file->name);
12693 }
12694
12695 /* Fetch the section offsets of this DWO unit. */
12696
12697 memset (&sections, 0, sizeof (sections));
12698
12699 for (i = 0; i < dwp_htab->nr_columns; ++i)
12700 {
12701 uint32_t offset = read_4_bytes (dbfd,
12702 dwp_htab->section_pool.v2.offsets
12703 + (((unit_index - 1) * dwp_htab->nr_columns
12704 + i)
12705 * sizeof (uint32_t)));
12706 uint32_t size = read_4_bytes (dbfd,
12707 dwp_htab->section_pool.v2.sizes
12708 + (((unit_index - 1) * dwp_htab->nr_columns
12709 + i)
12710 * sizeof (uint32_t)));
12711
12712 switch (dwp_htab->section_pool.v2.section_ids[i])
12713 {
12714 case DW_SECT_INFO:
12715 case DW_SECT_TYPES:
12716 sections.info_or_types_offset = offset;
12717 sections.info_or_types_size = size;
12718 break;
12719 case DW_SECT_ABBREV:
12720 sections.abbrev_offset = offset;
12721 sections.abbrev_size = size;
12722 break;
12723 case DW_SECT_LINE:
12724 sections.line_offset = offset;
12725 sections.line_size = size;
12726 break;
12727 case DW_SECT_LOC:
12728 sections.loc_offset = offset;
12729 sections.loc_size = size;
12730 break;
12731 case DW_SECT_STR_OFFSETS:
12732 sections.str_offsets_offset = offset;
12733 sections.str_offsets_size = size;
12734 break;
12735 case DW_SECT_MACINFO:
12736 sections.macinfo_offset = offset;
12737 sections.macinfo_size = size;
12738 break;
12739 case DW_SECT_MACRO:
12740 sections.macro_offset = offset;
12741 sections.macro_size = size;
12742 break;
12743 }
12744 }
12745
12746 /* It's easier for the rest of the code if we fake a struct dwo_file and
12747 have dwo_unit "live" in that. At least for now.
12748
12749 The DWP file can be made up of a random collection of CUs and TUs.
12750 However, for each CU + set of TUs that came from the same original DWO
12751 file, we can combine them back into a virtual DWO file to save space
12752 (fewer struct dwo_file objects to allocate). Remember that for really
12753 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12754
12755 std::string virtual_dwo_name =
12756 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12757 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12758 (long) (sections.line_size ? sections.line_offset : 0),
12759 (long) (sections.loc_size ? sections.loc_offset : 0),
12760 (long) (sections.str_offsets_size
12761 ? sections.str_offsets_offset : 0));
12762 /* Can we use an existing virtual DWO file? */
12763 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12764 virtual_dwo_name.c_str (),
12765 comp_dir);
12766 /* Create one if necessary. */
12767 if (*dwo_file_slot == NULL)
12768 {
12769 if (dwarf_read_debug)
12770 {
12771 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12772 virtual_dwo_name.c_str ());
12773 }
12774 dwo_file = new struct dwo_file;
12775 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
12776 virtual_dwo_name);
12777 dwo_file->comp_dir = comp_dir;
12778 dwo_file->sections.abbrev =
12779 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
12780 sections.abbrev_offset, sections.abbrev_size);
12781 dwo_file->sections.line =
12782 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
12783 sections.line_offset, sections.line_size);
12784 dwo_file->sections.loc =
12785 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
12786 sections.loc_offset, sections.loc_size);
12787 dwo_file->sections.macinfo =
12788 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
12789 sections.macinfo_offset, sections.macinfo_size);
12790 dwo_file->sections.macro =
12791 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
12792 sections.macro_offset, sections.macro_size);
12793 dwo_file->sections.str_offsets =
12794 create_dwp_v2_section (dwarf2_per_objfile,
12795 &dwp_file->sections.str_offsets,
12796 sections.str_offsets_offset,
12797 sections.str_offsets_size);
12798 /* The "str" section is global to the entire DWP file. */
12799 dwo_file->sections.str = dwp_file->sections.str;
12800 /* The info or types section is assigned below to dwo_unit,
12801 there's no need to record it in dwo_file.
12802 Also, we can't simply record type sections in dwo_file because
12803 we record a pointer into the vector in dwo_unit. As we collect more
12804 types we'll grow the vector and eventually have to reallocate space
12805 for it, invalidating all copies of pointers into the previous
12806 contents. */
12807 *dwo_file_slot = dwo_file;
12808 }
12809 else
12810 {
12811 if (dwarf_read_debug)
12812 {
12813 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12814 virtual_dwo_name.c_str ());
12815 }
12816 dwo_file = (struct dwo_file *) *dwo_file_slot;
12817 }
12818
12819 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12820 dwo_unit->dwo_file = dwo_file;
12821 dwo_unit->signature = signature;
12822 dwo_unit->section =
12823 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
12824 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12825 is_debug_types
12826 ? &dwp_file->sections.types
12827 : &dwp_file->sections.info,
12828 sections.info_or_types_offset,
12829 sections.info_or_types_size);
12830 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12831
12832 return dwo_unit;
12833 }
12834
12835 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12836 Returns NULL if the signature isn't found. */
12837
12838 static struct dwo_unit *
12839 lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12840 struct dwp_file *dwp_file, const char *comp_dir,
12841 ULONGEST signature, int is_debug_types)
12842 {
12843 const struct dwp_hash_table *dwp_htab =
12844 is_debug_types ? dwp_file->tus : dwp_file->cus;
12845 bfd *dbfd = dwp_file->dbfd.get ();
12846 uint32_t mask = dwp_htab->nr_slots - 1;
12847 uint32_t hash = signature & mask;
12848 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12849 unsigned int i;
12850 void **slot;
12851 struct dwo_unit find_dwo_cu;
12852
12853 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12854 find_dwo_cu.signature = signature;
12855 slot = htab_find_slot (is_debug_types
12856 ? dwp_file->loaded_tus
12857 : dwp_file->loaded_cus,
12858 &find_dwo_cu, INSERT);
12859
12860 if (*slot != NULL)
12861 return (struct dwo_unit *) *slot;
12862
12863 /* Use a for loop so that we don't loop forever on bad debug info. */
12864 for (i = 0; i < dwp_htab->nr_slots; ++i)
12865 {
12866 ULONGEST signature_in_table;
12867
12868 signature_in_table =
12869 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
12870 if (signature_in_table == signature)
12871 {
12872 uint32_t unit_index =
12873 read_4_bytes (dbfd,
12874 dwp_htab->unit_table + hash * sizeof (uint32_t));
12875
12876 if (dwp_file->version == 1)
12877 {
12878 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12879 dwp_file, unit_index,
12880 comp_dir, signature,
12881 is_debug_types);
12882 }
12883 else
12884 {
12885 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12886 dwp_file, unit_index,
12887 comp_dir, signature,
12888 is_debug_types);
12889 }
12890 return (struct dwo_unit *) *slot;
12891 }
12892 if (signature_in_table == 0)
12893 return NULL;
12894 hash = (hash + hash2) & mask;
12895 }
12896
12897 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12898 " [in module %s]"),
12899 dwp_file->name);
12900 }
12901
12902 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12903 Open the file specified by FILE_NAME and hand it off to BFD for
12904 preliminary analysis. Return a newly initialized bfd *, which
12905 includes a canonicalized copy of FILE_NAME.
12906 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12907 SEARCH_CWD is true if the current directory is to be searched.
12908 It will be searched before debug-file-directory.
12909 If successful, the file is added to the bfd include table of the
12910 objfile's bfd (see gdb_bfd_record_inclusion).
12911 If unable to find/open the file, return NULL.
12912 NOTE: This function is derived from symfile_bfd_open. */
12913
12914 static gdb_bfd_ref_ptr
12915 try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12916 const char *file_name, int is_dwp, int search_cwd)
12917 {
12918 int desc;
12919 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12920 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12921 to debug_file_directory. */
12922 const char *search_path;
12923 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12924
12925 gdb::unique_xmalloc_ptr<char> search_path_holder;
12926 if (search_cwd)
12927 {
12928 if (*debug_file_directory != '\0')
12929 {
12930 search_path_holder.reset (concat (".", dirname_separator_string,
12931 debug_file_directory,
12932 (char *) NULL));
12933 search_path = search_path_holder.get ();
12934 }
12935 else
12936 search_path = ".";
12937 }
12938 else
12939 search_path = debug_file_directory;
12940
12941 openp_flags flags = OPF_RETURN_REALPATH;
12942 if (is_dwp)
12943 flags |= OPF_SEARCH_IN_PATH;
12944
12945 gdb::unique_xmalloc_ptr<char> absolute_name;
12946 desc = openp (search_path, flags, file_name,
12947 O_RDONLY | O_BINARY, &absolute_name);
12948 if (desc < 0)
12949 return NULL;
12950
12951 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12952 gnutarget, desc));
12953 if (sym_bfd == NULL)
12954 return NULL;
12955 bfd_set_cacheable (sym_bfd.get (), 1);
12956
12957 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12958 return NULL;
12959
12960 /* Success. Record the bfd as having been included by the objfile's bfd.
12961 This is important because things like demangled_names_hash lives in the
12962 objfile's per_bfd space and may have references to things like symbol
12963 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12964 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
12965
12966 return sym_bfd;
12967 }
12968
12969 /* Try to open DWO file FILE_NAME.
12970 COMP_DIR is the DW_AT_comp_dir attribute.
12971 The result is the bfd handle of the file.
12972 If there is a problem finding or opening the file, return NULL.
12973 Upon success, the canonicalized path of the file is stored in the bfd,
12974 same as symfile_bfd_open. */
12975
12976 static gdb_bfd_ref_ptr
12977 open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12978 const char *file_name, const char *comp_dir)
12979 {
12980 if (IS_ABSOLUTE_PATH (file_name))
12981 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12982 0 /*is_dwp*/, 0 /*search_cwd*/);
12983
12984 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12985
12986 if (comp_dir != NULL)
12987 {
12988 gdb::unique_xmalloc_ptr<char> path_to_try
12989 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
12990
12991 /* NOTE: If comp_dir is a relative path, this will also try the
12992 search path, which seems useful. */
12993 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
12994 path_to_try.get (),
12995 0 /*is_dwp*/,
12996 1 /*search_cwd*/));
12997 if (abfd != NULL)
12998 return abfd;
12999 }
13000
13001 /* That didn't work, try debug-file-directory, which, despite its name,
13002 is a list of paths. */
13003
13004 if (*debug_file_directory == '\0')
13005 return NULL;
13006
13007 return try_open_dwop_file (dwarf2_per_objfile, file_name,
13008 0 /*is_dwp*/, 1 /*search_cwd*/);
13009 }
13010
13011 /* This function is mapped across the sections and remembers the offset and
13012 size of each of the DWO debugging sections we are interested in. */
13013
13014 static void
13015 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
13016 {
13017 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
13018 const struct dwop_section_names *names = &dwop_section_names;
13019
13020 if (section_is_p (sectp->name, &names->abbrev_dwo))
13021 {
13022 dwo_sections->abbrev.s.section = sectp;
13023 dwo_sections->abbrev.size = bfd_section_size (sectp);
13024 }
13025 else if (section_is_p (sectp->name, &names->info_dwo))
13026 {
13027 dwo_sections->info.s.section = sectp;
13028 dwo_sections->info.size = bfd_section_size (sectp);
13029 }
13030 else if (section_is_p (sectp->name, &names->line_dwo))
13031 {
13032 dwo_sections->line.s.section = sectp;
13033 dwo_sections->line.size = bfd_section_size (sectp);
13034 }
13035 else if (section_is_p (sectp->name, &names->loc_dwo))
13036 {
13037 dwo_sections->loc.s.section = sectp;
13038 dwo_sections->loc.size = bfd_section_size (sectp);
13039 }
13040 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13041 {
13042 dwo_sections->macinfo.s.section = sectp;
13043 dwo_sections->macinfo.size = bfd_section_size (sectp);
13044 }
13045 else if (section_is_p (sectp->name, &names->macro_dwo))
13046 {
13047 dwo_sections->macro.s.section = sectp;
13048 dwo_sections->macro.size = bfd_section_size (sectp);
13049 }
13050 else if (section_is_p (sectp->name, &names->str_dwo))
13051 {
13052 dwo_sections->str.s.section = sectp;
13053 dwo_sections->str.size = bfd_section_size (sectp);
13054 }
13055 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13056 {
13057 dwo_sections->str_offsets.s.section = sectp;
13058 dwo_sections->str_offsets.size = bfd_section_size (sectp);
13059 }
13060 else if (section_is_p (sectp->name, &names->types_dwo))
13061 {
13062 struct dwarf2_section_info type_section;
13063
13064 memset (&type_section, 0, sizeof (type_section));
13065 type_section.s.section = sectp;
13066 type_section.size = bfd_section_size (sectp);
13067 dwo_sections->types.push_back (type_section);
13068 }
13069 }
13070
13071 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
13072 by PER_CU. This is for the non-DWP case.
13073 The result is NULL if DWO_NAME can't be found. */
13074
13075 static struct dwo_file *
13076 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
13077 const char *dwo_name, const char *comp_dir)
13078 {
13079 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
13080
13081 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
13082 if (dbfd == NULL)
13083 {
13084 if (dwarf_read_debug)
13085 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
13086 return NULL;
13087 }
13088
13089 dwo_file_up dwo_file (new struct dwo_file);
13090 dwo_file->dwo_name = dwo_name;
13091 dwo_file->comp_dir = comp_dir;
13092 dwo_file->dbfd = std::move (dbfd);
13093
13094 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
13095 &dwo_file->sections);
13096
13097 create_cus_hash_table (dwarf2_per_objfile, per_cu->cu, *dwo_file,
13098 dwo_file->sections.info, dwo_file->cus);
13099
13100 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
13101 dwo_file->sections.types, dwo_file->tus);
13102
13103 if (dwarf_read_debug)
13104 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
13105
13106 return dwo_file.release ();
13107 }
13108
13109 /* This function is mapped across the sections and remembers the offset and
13110 size of each of the DWP debugging sections common to version 1 and 2 that
13111 we are interested in. */
13112
13113 static void
13114 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
13115 void *dwp_file_ptr)
13116 {
13117 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
13118 const struct dwop_section_names *names = &dwop_section_names;
13119 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13120
13121 /* Record the ELF section number for later lookup: this is what the
13122 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13123 gdb_assert (elf_section_nr < dwp_file->num_sections);
13124 dwp_file->elf_sections[elf_section_nr] = sectp;
13125
13126 /* Look for specific sections that we need. */
13127 if (section_is_p (sectp->name, &names->str_dwo))
13128 {
13129 dwp_file->sections.str.s.section = sectp;
13130 dwp_file->sections.str.size = bfd_section_size (sectp);
13131 }
13132 else if (section_is_p (sectp->name, &names->cu_index))
13133 {
13134 dwp_file->sections.cu_index.s.section = sectp;
13135 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
13136 }
13137 else if (section_is_p (sectp->name, &names->tu_index))
13138 {
13139 dwp_file->sections.tu_index.s.section = sectp;
13140 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
13141 }
13142 }
13143
13144 /* This function is mapped across the sections and remembers the offset and
13145 size of each of the DWP version 2 debugging sections that we are interested
13146 in. This is split into a separate function because we don't know if we
13147 have version 1 or 2 until we parse the cu_index/tu_index sections. */
13148
13149 static void
13150 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
13151 {
13152 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
13153 const struct dwop_section_names *names = &dwop_section_names;
13154 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13155
13156 /* Record the ELF section number for later lookup: this is what the
13157 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13158 gdb_assert (elf_section_nr < dwp_file->num_sections);
13159 dwp_file->elf_sections[elf_section_nr] = sectp;
13160
13161 /* Look for specific sections that we need. */
13162 if (section_is_p (sectp->name, &names->abbrev_dwo))
13163 {
13164 dwp_file->sections.abbrev.s.section = sectp;
13165 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
13166 }
13167 else if (section_is_p (sectp->name, &names->info_dwo))
13168 {
13169 dwp_file->sections.info.s.section = sectp;
13170 dwp_file->sections.info.size = bfd_section_size (sectp);
13171 }
13172 else if (section_is_p (sectp->name, &names->line_dwo))
13173 {
13174 dwp_file->sections.line.s.section = sectp;
13175 dwp_file->sections.line.size = bfd_section_size (sectp);
13176 }
13177 else if (section_is_p (sectp->name, &names->loc_dwo))
13178 {
13179 dwp_file->sections.loc.s.section = sectp;
13180 dwp_file->sections.loc.size = bfd_section_size (sectp);
13181 }
13182 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13183 {
13184 dwp_file->sections.macinfo.s.section = sectp;
13185 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
13186 }
13187 else if (section_is_p (sectp->name, &names->macro_dwo))
13188 {
13189 dwp_file->sections.macro.s.section = sectp;
13190 dwp_file->sections.macro.size = bfd_section_size (sectp);
13191 }
13192 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13193 {
13194 dwp_file->sections.str_offsets.s.section = sectp;
13195 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
13196 }
13197 else if (section_is_p (sectp->name, &names->types_dwo))
13198 {
13199 dwp_file->sections.types.s.section = sectp;
13200 dwp_file->sections.types.size = bfd_section_size (sectp);
13201 }
13202 }
13203
13204 /* Hash function for dwp_file loaded CUs/TUs. */
13205
13206 static hashval_t
13207 hash_dwp_loaded_cutus (const void *item)
13208 {
13209 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
13210
13211 /* This drops the top 32 bits of the signature, but is ok for a hash. */
13212 return dwo_unit->signature;
13213 }
13214
13215 /* Equality function for dwp_file loaded CUs/TUs. */
13216
13217 static int
13218 eq_dwp_loaded_cutus (const void *a, const void *b)
13219 {
13220 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13221 const struct dwo_unit *dub = (const struct dwo_unit *) b;
13222
13223 return dua->signature == dub->signature;
13224 }
13225
13226 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
13227
13228 static htab_t
13229 allocate_dwp_loaded_cutus_table (struct objfile *objfile)
13230 {
13231 return htab_create_alloc_ex (3,
13232 hash_dwp_loaded_cutus,
13233 eq_dwp_loaded_cutus,
13234 NULL,
13235 &objfile->objfile_obstack,
13236 hashtab_obstack_allocate,
13237 dummy_obstack_deallocate);
13238 }
13239
13240 /* Try to open DWP file FILE_NAME.
13241 The result is the bfd handle of the file.
13242 If there is a problem finding or opening the file, return NULL.
13243 Upon success, the canonicalized path of the file is stored in the bfd,
13244 same as symfile_bfd_open. */
13245
13246 static gdb_bfd_ref_ptr
13247 open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
13248 const char *file_name)
13249 {
13250 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
13251 1 /*is_dwp*/,
13252 1 /*search_cwd*/));
13253 if (abfd != NULL)
13254 return abfd;
13255
13256 /* Work around upstream bug 15652.
13257 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13258 [Whether that's a "bug" is debatable, but it is getting in our way.]
13259 We have no real idea where the dwp file is, because gdb's realpath-ing
13260 of the executable's path may have discarded the needed info.
13261 [IWBN if the dwp file name was recorded in the executable, akin to
13262 .gnu_debuglink, but that doesn't exist yet.]
13263 Strip the directory from FILE_NAME and search again. */
13264 if (*debug_file_directory != '\0')
13265 {
13266 /* Don't implicitly search the current directory here.
13267 If the user wants to search "." to handle this case,
13268 it must be added to debug-file-directory. */
13269 return try_open_dwop_file (dwarf2_per_objfile,
13270 lbasename (file_name), 1 /*is_dwp*/,
13271 0 /*search_cwd*/);
13272 }
13273
13274 return NULL;
13275 }
13276
13277 /* Initialize the use of the DWP file for the current objfile.
13278 By convention the name of the DWP file is ${objfile}.dwp.
13279 The result is NULL if it can't be found. */
13280
13281 static std::unique_ptr<struct dwp_file>
13282 open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
13283 {
13284 struct objfile *objfile = dwarf2_per_objfile->objfile;
13285
13286 /* Try to find first .dwp for the binary file before any symbolic links
13287 resolving. */
13288
13289 /* If the objfile is a debug file, find the name of the real binary
13290 file and get the name of dwp file from there. */
13291 std::string dwp_name;
13292 if (objfile->separate_debug_objfile_backlink != NULL)
13293 {
13294 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13295 const char *backlink_basename = lbasename (backlink->original_name);
13296
13297 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
13298 }
13299 else
13300 dwp_name = objfile->original_name;
13301
13302 dwp_name += ".dwp";
13303
13304 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
13305 if (dbfd == NULL
13306 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13307 {
13308 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
13309 dwp_name = objfile_name (objfile);
13310 dwp_name += ".dwp";
13311 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
13312 }
13313
13314 if (dbfd == NULL)
13315 {
13316 if (dwarf_read_debug)
13317 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
13318 return std::unique_ptr<dwp_file> ();
13319 }
13320
13321 const char *name = bfd_get_filename (dbfd.get ());
13322 std::unique_ptr<struct dwp_file> dwp_file
13323 (new struct dwp_file (name, std::move (dbfd)));
13324
13325 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
13326 dwp_file->elf_sections =
13327 OBSTACK_CALLOC (&objfile->objfile_obstack,
13328 dwp_file->num_sections, asection *);
13329
13330 bfd_map_over_sections (dwp_file->dbfd.get (),
13331 dwarf2_locate_common_dwp_sections,
13332 dwp_file.get ());
13333
13334 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
13335 0);
13336
13337 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
13338 1);
13339
13340 /* The DWP file version is stored in the hash table. Oh well. */
13341 if (dwp_file->cus && dwp_file->tus
13342 && dwp_file->cus->version != dwp_file->tus->version)
13343 {
13344 /* Technically speaking, we should try to limp along, but this is
13345 pretty bizarre. We use pulongest here because that's the established
13346 portability solution (e.g, we cannot use %u for uint32_t). */
13347 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13348 " TU version %s [in DWP file %s]"),
13349 pulongest (dwp_file->cus->version),
13350 pulongest (dwp_file->tus->version), dwp_name.c_str ());
13351 }
13352
13353 if (dwp_file->cus)
13354 dwp_file->version = dwp_file->cus->version;
13355 else if (dwp_file->tus)
13356 dwp_file->version = dwp_file->tus->version;
13357 else
13358 dwp_file->version = 2;
13359
13360 if (dwp_file->version == 2)
13361 bfd_map_over_sections (dwp_file->dbfd.get (),
13362 dwarf2_locate_v2_dwp_sections,
13363 dwp_file.get ());
13364
13365 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
13366 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
13367
13368 if (dwarf_read_debug)
13369 {
13370 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
13371 fprintf_unfiltered (gdb_stdlog,
13372 " %s CUs, %s TUs\n",
13373 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13374 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
13375 }
13376
13377 return dwp_file;
13378 }
13379
13380 /* Wrapper around open_and_init_dwp_file, only open it once. */
13381
13382 static struct dwp_file *
13383 get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
13384 {
13385 if (! dwarf2_per_objfile->dwp_checked)
13386 {
13387 dwarf2_per_objfile->dwp_file
13388 = open_and_init_dwp_file (dwarf2_per_objfile);
13389 dwarf2_per_objfile->dwp_checked = 1;
13390 }
13391 return dwarf2_per_objfile->dwp_file.get ();
13392 }
13393
13394 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13395 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13396 or in the DWP file for the objfile, referenced by THIS_UNIT.
13397 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
13398 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13399
13400 This is called, for example, when wanting to read a variable with a
13401 complex location. Therefore we don't want to do file i/o for every call.
13402 Therefore we don't want to look for a DWO file on every call.
13403 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13404 then we check if we've already seen DWO_NAME, and only THEN do we check
13405 for a DWO file.
13406
13407 The result is a pointer to the dwo_unit object or NULL if we didn't find it
13408 (dwo_id mismatch or couldn't find the DWO/DWP file). */
13409
13410 static struct dwo_unit *
13411 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
13412 const char *dwo_name, const char *comp_dir,
13413 ULONGEST signature, int is_debug_types)
13414 {
13415 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
13416 struct objfile *objfile = dwarf2_per_objfile->objfile;
13417 const char *kind = is_debug_types ? "TU" : "CU";
13418 void **dwo_file_slot;
13419 struct dwo_file *dwo_file;
13420 struct dwp_file *dwp_file;
13421
13422 /* First see if there's a DWP file.
13423 If we have a DWP file but didn't find the DWO inside it, don't
13424 look for the original DWO file. It makes gdb behave differently
13425 depending on whether one is debugging in the build tree. */
13426
13427 dwp_file = get_dwp_file (dwarf2_per_objfile);
13428 if (dwp_file != NULL)
13429 {
13430 const struct dwp_hash_table *dwp_htab =
13431 is_debug_types ? dwp_file->tus : dwp_file->cus;
13432
13433 if (dwp_htab != NULL)
13434 {
13435 struct dwo_unit *dwo_cutu =
13436 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
13437 signature, is_debug_types);
13438
13439 if (dwo_cutu != NULL)
13440 {
13441 if (dwarf_read_debug)
13442 {
13443 fprintf_unfiltered (gdb_stdlog,
13444 "Virtual DWO %s %s found: @%s\n",
13445 kind, hex_string (signature),
13446 host_address_to_string (dwo_cutu));
13447 }
13448 return dwo_cutu;
13449 }
13450 }
13451 }
13452 else
13453 {
13454 /* No DWP file, look for the DWO file. */
13455
13456 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
13457 dwo_name, comp_dir);
13458 if (*dwo_file_slot == NULL)
13459 {
13460 /* Read in the file and build a table of the CUs/TUs it contains. */
13461 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
13462 }
13463 /* NOTE: This will be NULL if unable to open the file. */
13464 dwo_file = (struct dwo_file *) *dwo_file_slot;
13465
13466 if (dwo_file != NULL)
13467 {
13468 struct dwo_unit *dwo_cutu = NULL;
13469
13470 if (is_debug_types && dwo_file->tus)
13471 {
13472 struct dwo_unit find_dwo_cutu;
13473
13474 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13475 find_dwo_cutu.signature = signature;
13476 dwo_cutu
13477 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
13478 }
13479 else if (!is_debug_types && dwo_file->cus)
13480 {
13481 struct dwo_unit find_dwo_cutu;
13482
13483 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13484 find_dwo_cutu.signature = signature;
13485 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus,
13486 &find_dwo_cutu);
13487 }
13488
13489 if (dwo_cutu != NULL)
13490 {
13491 if (dwarf_read_debug)
13492 {
13493 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
13494 kind, dwo_name, hex_string (signature),
13495 host_address_to_string (dwo_cutu));
13496 }
13497 return dwo_cutu;
13498 }
13499 }
13500 }
13501
13502 /* We didn't find it. This could mean a dwo_id mismatch, or
13503 someone deleted the DWO/DWP file, or the search path isn't set up
13504 correctly to find the file. */
13505
13506 if (dwarf_read_debug)
13507 {
13508 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
13509 kind, dwo_name, hex_string (signature));
13510 }
13511
13512 /* This is a warning and not a complaint because it can be caused by
13513 pilot error (e.g., user accidentally deleting the DWO). */
13514 {
13515 /* Print the name of the DWP file if we looked there, helps the user
13516 better diagnose the problem. */
13517 std::string dwp_text;
13518
13519 if (dwp_file != NULL)
13520 dwp_text = string_printf (" [in DWP file %s]",
13521 lbasename (dwp_file->name));
13522
13523 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
13524 " [in module %s]"),
13525 kind, dwo_name, hex_string (signature),
13526 dwp_text.c_str (),
13527 this_unit->is_debug_types ? "TU" : "CU",
13528 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
13529 }
13530 return NULL;
13531 }
13532
13533 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13534 See lookup_dwo_cutu_unit for details. */
13535
13536 static struct dwo_unit *
13537 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
13538 const char *dwo_name, const char *comp_dir,
13539 ULONGEST signature)
13540 {
13541 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
13542 }
13543
13544 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13545 See lookup_dwo_cutu_unit for details. */
13546
13547 static struct dwo_unit *
13548 lookup_dwo_type_unit (struct signatured_type *this_tu,
13549 const char *dwo_name, const char *comp_dir)
13550 {
13551 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
13552 }
13553
13554 /* Traversal function for queue_and_load_all_dwo_tus. */
13555
13556 static int
13557 queue_and_load_dwo_tu (void **slot, void *info)
13558 {
13559 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13560 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
13561 ULONGEST signature = dwo_unit->signature;
13562 struct signatured_type *sig_type =
13563 lookup_dwo_signatured_type (per_cu->cu, signature);
13564
13565 if (sig_type != NULL)
13566 {
13567 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13568
13569 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13570 a real dependency of PER_CU on SIG_TYPE. That is detected later
13571 while processing PER_CU. */
13572 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
13573 load_full_type_unit (sig_cu);
13574 per_cu->imported_symtabs_push (sig_cu);
13575 }
13576
13577 return 1;
13578 }
13579
13580 /* Queue all TUs contained in the DWO of PER_CU to be read in.
13581 The DWO may have the only definition of the type, though it may not be
13582 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13583 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13584
13585 static void
13586 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
13587 {
13588 struct dwo_unit *dwo_unit;
13589 struct dwo_file *dwo_file;
13590
13591 gdb_assert (!per_cu->is_debug_types);
13592 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
13593 gdb_assert (per_cu->cu != NULL);
13594
13595 dwo_unit = per_cu->cu->dwo_unit;
13596 gdb_assert (dwo_unit != NULL);
13597
13598 dwo_file = dwo_unit->dwo_file;
13599 if (dwo_file->tus != NULL)
13600 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
13601 }
13602
13603 /* Read in various DIEs. */
13604
13605 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
13606 Inherit only the children of the DW_AT_abstract_origin DIE not being
13607 already referenced by DW_AT_abstract_origin from the children of the
13608 current DIE. */
13609
13610 static void
13611 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13612 {
13613 struct die_info *child_die;
13614 sect_offset *offsetp;
13615 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13616 struct die_info *origin_die;
13617 /* Iterator of the ORIGIN_DIE children. */
13618 struct die_info *origin_child_die;
13619 struct attribute *attr;
13620 struct dwarf2_cu *origin_cu;
13621 struct pending **origin_previous_list_in_scope;
13622
13623 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13624 if (!attr)
13625 return;
13626
13627 /* Note that following die references may follow to a die in a
13628 different cu. */
13629
13630 origin_cu = cu;
13631 origin_die = follow_die_ref (die, attr, &origin_cu);
13632
13633 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13634 symbols in. */
13635 origin_previous_list_in_scope = origin_cu->list_in_scope;
13636 origin_cu->list_in_scope = cu->list_in_scope;
13637
13638 if (die->tag != origin_die->tag
13639 && !(die->tag == DW_TAG_inlined_subroutine
13640 && origin_die->tag == DW_TAG_subprogram))
13641 complaint (_("DIE %s and its abstract origin %s have different tags"),
13642 sect_offset_str (die->sect_off),
13643 sect_offset_str (origin_die->sect_off));
13644
13645 std::vector<sect_offset> offsets;
13646
13647 for (child_die = die->child;
13648 child_die && child_die->tag;
13649 child_die = sibling_die (child_die))
13650 {
13651 struct die_info *child_origin_die;
13652 struct dwarf2_cu *child_origin_cu;
13653
13654 /* We are trying to process concrete instance entries:
13655 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
13656 it's not relevant to our analysis here. i.e. detecting DIEs that are
13657 present in the abstract instance but not referenced in the concrete
13658 one. */
13659 if (child_die->tag == DW_TAG_call_site
13660 || child_die->tag == DW_TAG_GNU_call_site)
13661 continue;
13662
13663 /* For each CHILD_DIE, find the corresponding child of
13664 ORIGIN_DIE. If there is more than one layer of
13665 DW_AT_abstract_origin, follow them all; there shouldn't be,
13666 but GCC versions at least through 4.4 generate this (GCC PR
13667 40573). */
13668 child_origin_die = child_die;
13669 child_origin_cu = cu;
13670 while (1)
13671 {
13672 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13673 child_origin_cu);
13674 if (attr == NULL)
13675 break;
13676 child_origin_die = follow_die_ref (child_origin_die, attr,
13677 &child_origin_cu);
13678 }
13679
13680 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13681 counterpart may exist. */
13682 if (child_origin_die != child_die)
13683 {
13684 if (child_die->tag != child_origin_die->tag
13685 && !(child_die->tag == DW_TAG_inlined_subroutine
13686 && child_origin_die->tag == DW_TAG_subprogram))
13687 complaint (_("Child DIE %s and its abstract origin %s have "
13688 "different tags"),
13689 sect_offset_str (child_die->sect_off),
13690 sect_offset_str (child_origin_die->sect_off));
13691 if (child_origin_die->parent != origin_die)
13692 complaint (_("Child DIE %s and its abstract origin %s have "
13693 "different parents"),
13694 sect_offset_str (child_die->sect_off),
13695 sect_offset_str (child_origin_die->sect_off));
13696 else
13697 offsets.push_back (child_origin_die->sect_off);
13698 }
13699 }
13700 std::sort (offsets.begin (), offsets.end ());
13701 sect_offset *offsets_end = offsets.data () + offsets.size ();
13702 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
13703 if (offsetp[-1] == *offsetp)
13704 complaint (_("Multiple children of DIE %s refer "
13705 "to DIE %s as their abstract origin"),
13706 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
13707
13708 offsetp = offsets.data ();
13709 origin_child_die = origin_die->child;
13710 while (origin_child_die && origin_child_die->tag)
13711 {
13712 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
13713 while (offsetp < offsets_end
13714 && *offsetp < origin_child_die->sect_off)
13715 offsetp++;
13716 if (offsetp >= offsets_end
13717 || *offsetp > origin_child_die->sect_off)
13718 {
13719 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13720 Check whether we're already processing ORIGIN_CHILD_DIE.
13721 This can happen with mutually referenced abstract_origins.
13722 PR 16581. */
13723 if (!origin_child_die->in_process)
13724 process_die (origin_child_die, origin_cu);
13725 }
13726 origin_child_die = sibling_die (origin_child_die);
13727 }
13728 origin_cu->list_in_scope = origin_previous_list_in_scope;
13729
13730 if (cu != origin_cu)
13731 compute_delayed_physnames (origin_cu);
13732 }
13733
13734 static void
13735 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
13736 {
13737 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13738 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13739 struct context_stack *newobj;
13740 CORE_ADDR lowpc;
13741 CORE_ADDR highpc;
13742 struct die_info *child_die;
13743 struct attribute *attr, *call_line, *call_file;
13744 const char *name;
13745 CORE_ADDR baseaddr;
13746 struct block *block;
13747 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13748 std::vector<struct symbol *> template_args;
13749 struct template_symbol *templ_func = NULL;
13750
13751 if (inlined_func)
13752 {
13753 /* If we do not have call site information, we can't show the
13754 caller of this inlined function. That's too confusing, so
13755 only use the scope for local variables. */
13756 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13757 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13758 if (call_line == NULL || call_file == NULL)
13759 {
13760 read_lexical_block_scope (die, cu);
13761 return;
13762 }
13763 }
13764
13765 baseaddr = objfile->section_offsets[SECT_OFF_TEXT (objfile)];
13766
13767 name = dwarf2_name (die, cu);
13768
13769 /* Ignore functions with missing or empty names. These are actually
13770 illegal according to the DWARF standard. */
13771 if (name == NULL)
13772 {
13773 complaint (_("missing name for subprogram DIE at %s"),
13774 sect_offset_str (die->sect_off));
13775 return;
13776 }
13777
13778 /* Ignore functions with missing or invalid low and high pc attributes. */
13779 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
13780 <= PC_BOUNDS_INVALID)
13781 {
13782 attr = dwarf2_attr (die, DW_AT_external, cu);
13783 if (!attr || !DW_UNSND (attr))
13784 complaint (_("cannot get low and high bounds "
13785 "for subprogram DIE at %s"),
13786 sect_offset_str (die->sect_off));
13787 return;
13788 }
13789
13790 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13791 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13792
13793 /* If we have any template arguments, then we must allocate a
13794 different sort of symbol. */
13795 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
13796 {
13797 if (child_die->tag == DW_TAG_template_type_param
13798 || child_die->tag == DW_TAG_template_value_param)
13799 {
13800 templ_func = allocate_template_symbol (objfile);
13801 templ_func->subclass = SYMBOL_TEMPLATE;
13802 break;
13803 }
13804 }
13805
13806 newobj = cu->get_builder ()->push_context (0, lowpc);
13807 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13808 (struct symbol *) templ_func);
13809
13810 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
13811 set_objfile_main_name (objfile, newobj->name->linkage_name (),
13812 cu->language);
13813
13814 /* If there is a location expression for DW_AT_frame_base, record
13815 it. */
13816 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
13817 if (attr != nullptr)
13818 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
13819
13820 /* If there is a location for the static link, record it. */
13821 newobj->static_link = NULL;
13822 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13823 if (attr != nullptr)
13824 {
13825 newobj->static_link
13826 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
13827 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
13828 dwarf2_per_cu_addr_type (cu->per_cu));
13829 }
13830
13831 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
13832
13833 if (die->child != NULL)
13834 {
13835 child_die = die->child;
13836 while (child_die && child_die->tag)
13837 {
13838 if (child_die->tag == DW_TAG_template_type_param
13839 || child_die->tag == DW_TAG_template_value_param)
13840 {
13841 struct symbol *arg = new_symbol (child_die, NULL, cu);
13842
13843 if (arg != NULL)
13844 template_args.push_back (arg);
13845 }
13846 else
13847 process_die (child_die, cu);
13848 child_die = sibling_die (child_die);
13849 }
13850 }
13851
13852 inherit_abstract_dies (die, cu);
13853
13854 /* If we have a DW_AT_specification, we might need to import using
13855 directives from the context of the specification DIE. See the
13856 comment in determine_prefix. */
13857 if (cu->language == language_cplus
13858 && dwarf2_attr (die, DW_AT_specification, cu))
13859 {
13860 struct dwarf2_cu *spec_cu = cu;
13861 struct die_info *spec_die = die_specification (die, &spec_cu);
13862
13863 while (spec_die)
13864 {
13865 child_die = spec_die->child;
13866 while (child_die && child_die->tag)
13867 {
13868 if (child_die->tag == DW_TAG_imported_module)
13869 process_die (child_die, spec_cu);
13870 child_die = sibling_die (child_die);
13871 }
13872
13873 /* In some cases, GCC generates specification DIEs that
13874 themselves contain DW_AT_specification attributes. */
13875 spec_die = die_specification (spec_die, &spec_cu);
13876 }
13877 }
13878
13879 struct context_stack cstk = cu->get_builder ()->pop_context ();
13880 /* Make a block for the local symbols within. */
13881 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
13882 cstk.static_link, lowpc, highpc);
13883
13884 /* For C++, set the block's scope. */
13885 if ((cu->language == language_cplus
13886 || cu->language == language_fortran
13887 || cu->language == language_d
13888 || cu->language == language_rust)
13889 && cu->processing_has_namespace_info)
13890 block_set_scope (block, determine_prefix (die, cu),
13891 &objfile->objfile_obstack);
13892
13893 /* If we have address ranges, record them. */
13894 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13895
13896 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
13897
13898 /* Attach template arguments to function. */
13899 if (!template_args.empty ())
13900 {
13901 gdb_assert (templ_func != NULL);
13902
13903 templ_func->n_template_arguments = template_args.size ();
13904 templ_func->template_arguments
13905 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13906 templ_func->n_template_arguments);
13907 memcpy (templ_func->template_arguments,
13908 template_args.data (),
13909 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13910
13911 /* Make sure that the symtab is set on the new symbols. Even
13912 though they don't appear in this symtab directly, other parts
13913 of gdb assume that symbols do, and this is reasonably
13914 true. */
13915 for (symbol *sym : template_args)
13916 symbol_set_symtab (sym, symbol_symtab (templ_func));
13917 }
13918
13919 /* In C++, we can have functions nested inside functions (e.g., when
13920 a function declares a class that has methods). This means that
13921 when we finish processing a function scope, we may need to go
13922 back to building a containing block's symbol lists. */
13923 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13924 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13925
13926 /* If we've finished processing a top-level function, subsequent
13927 symbols go in the file symbol list. */
13928 if (cu->get_builder ()->outermost_context_p ())
13929 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
13930 }
13931
13932 /* Process all the DIES contained within a lexical block scope. Start
13933 a new scope, process the dies, and then close the scope. */
13934
13935 static void
13936 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13937 {
13938 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13939 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13940 CORE_ADDR lowpc, highpc;
13941 struct die_info *child_die;
13942 CORE_ADDR baseaddr;
13943
13944 baseaddr = objfile->section_offsets[SECT_OFF_TEXT (objfile)];
13945
13946 /* Ignore blocks with missing or invalid low and high pc attributes. */
13947 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13948 as multiple lexical blocks? Handling children in a sane way would
13949 be nasty. Might be easier to properly extend generic blocks to
13950 describe ranges. */
13951 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13952 {
13953 case PC_BOUNDS_NOT_PRESENT:
13954 /* DW_TAG_lexical_block has no attributes, process its children as if
13955 there was no wrapping by that DW_TAG_lexical_block.
13956 GCC does no longer produces such DWARF since GCC r224161. */
13957 for (child_die = die->child;
13958 child_die != NULL && child_die->tag;
13959 child_die = sibling_die (child_die))
13960 process_die (child_die, cu);
13961 return;
13962 case PC_BOUNDS_INVALID:
13963 return;
13964 }
13965 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13966 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13967
13968 cu->get_builder ()->push_context (0, lowpc);
13969 if (die->child != NULL)
13970 {
13971 child_die = die->child;
13972 while (child_die && child_die->tag)
13973 {
13974 process_die (child_die, cu);
13975 child_die = sibling_die (child_die);
13976 }
13977 }
13978 inherit_abstract_dies (die, cu);
13979 struct context_stack cstk = cu->get_builder ()->pop_context ();
13980
13981 if (*cu->get_builder ()->get_local_symbols () != NULL
13982 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13983 {
13984 struct block *block
13985 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13986 cstk.start_addr, highpc);
13987
13988 /* Note that recording ranges after traversing children, as we
13989 do here, means that recording a parent's ranges entails
13990 walking across all its children's ranges as they appear in
13991 the address map, which is quadratic behavior.
13992
13993 It would be nicer to record the parent's ranges before
13994 traversing its children, simply overriding whatever you find
13995 there. But since we don't even decide whether to create a
13996 block until after we've traversed its children, that's hard
13997 to do. */
13998 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13999 }
14000 *cu->get_builder ()->get_local_symbols () = cstk.locals;
14001 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
14002 }
14003
14004 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
14005
14006 static void
14007 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
14008 {
14009 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14010 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14011 CORE_ADDR pc, baseaddr;
14012 struct attribute *attr;
14013 struct call_site *call_site, call_site_local;
14014 void **slot;
14015 int nparams;
14016 struct die_info *child_die;
14017
14018 baseaddr = objfile->section_offsets[SECT_OFF_TEXT (objfile)];
14019
14020 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
14021 if (attr == NULL)
14022 {
14023 /* This was a pre-DWARF-5 GNU extension alias
14024 for DW_AT_call_return_pc. */
14025 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14026 }
14027 if (!attr)
14028 {
14029 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
14030 "DIE %s [in module %s]"),
14031 sect_offset_str (die->sect_off), objfile_name (objfile));
14032 return;
14033 }
14034 pc = attr_value_as_address (attr) + baseaddr;
14035 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
14036
14037 if (cu->call_site_htab == NULL)
14038 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
14039 NULL, &objfile->objfile_obstack,
14040 hashtab_obstack_allocate, NULL);
14041 call_site_local.pc = pc;
14042 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
14043 if (*slot != NULL)
14044 {
14045 complaint (_("Duplicate PC %s for DW_TAG_call_site "
14046 "DIE %s [in module %s]"),
14047 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
14048 objfile_name (objfile));
14049 return;
14050 }
14051
14052 /* Count parameters at the caller. */
14053
14054 nparams = 0;
14055 for (child_die = die->child; child_die && child_die->tag;
14056 child_die = sibling_die (child_die))
14057 {
14058 if (child_die->tag != DW_TAG_call_site_parameter
14059 && child_die->tag != DW_TAG_GNU_call_site_parameter)
14060 {
14061 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
14062 "DW_TAG_call_site child DIE %s [in module %s]"),
14063 child_die->tag, sect_offset_str (child_die->sect_off),
14064 objfile_name (objfile));
14065 continue;
14066 }
14067
14068 nparams++;
14069 }
14070
14071 call_site
14072 = ((struct call_site *)
14073 obstack_alloc (&objfile->objfile_obstack,
14074 sizeof (*call_site)
14075 + (sizeof (*call_site->parameter) * (nparams - 1))));
14076 *slot = call_site;
14077 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
14078 call_site->pc = pc;
14079
14080 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
14081 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
14082 {
14083 struct die_info *func_die;
14084
14085 /* Skip also over DW_TAG_inlined_subroutine. */
14086 for (func_die = die->parent;
14087 func_die && func_die->tag != DW_TAG_subprogram
14088 && func_die->tag != DW_TAG_subroutine_type;
14089 func_die = func_die->parent);
14090
14091 /* DW_AT_call_all_calls is a superset
14092 of DW_AT_call_all_tail_calls. */
14093 if (func_die
14094 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
14095 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
14096 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
14097 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
14098 {
14099 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
14100 not complete. But keep CALL_SITE for look ups via call_site_htab,
14101 both the initial caller containing the real return address PC and
14102 the final callee containing the current PC of a chain of tail
14103 calls do not need to have the tail call list complete. But any
14104 function candidate for a virtual tail call frame searched via
14105 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
14106 determined unambiguously. */
14107 }
14108 else
14109 {
14110 struct type *func_type = NULL;
14111
14112 if (func_die)
14113 func_type = get_die_type (func_die, cu);
14114 if (func_type != NULL)
14115 {
14116 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
14117
14118 /* Enlist this call site to the function. */
14119 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
14120 TYPE_TAIL_CALL_LIST (func_type) = call_site;
14121 }
14122 else
14123 complaint (_("Cannot find function owning DW_TAG_call_site "
14124 "DIE %s [in module %s]"),
14125 sect_offset_str (die->sect_off), objfile_name (objfile));
14126 }
14127 }
14128
14129 attr = dwarf2_attr (die, DW_AT_call_target, cu);
14130 if (attr == NULL)
14131 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
14132 if (attr == NULL)
14133 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
14134 if (attr == NULL)
14135 {
14136 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
14137 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14138 }
14139 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
14140 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
14141 /* Keep NULL DWARF_BLOCK. */;
14142 else if (attr_form_is_block (attr))
14143 {
14144 struct dwarf2_locexpr_baton *dlbaton;
14145
14146 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
14147 dlbaton->data = DW_BLOCK (attr)->data;
14148 dlbaton->size = DW_BLOCK (attr)->size;
14149 dlbaton->per_cu = cu->per_cu;
14150
14151 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
14152 }
14153 else if (attr_form_is_ref (attr))
14154 {
14155 struct dwarf2_cu *target_cu = cu;
14156 struct die_info *target_die;
14157
14158 target_die = follow_die_ref (die, attr, &target_cu);
14159 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
14160 if (die_is_declaration (target_die, target_cu))
14161 {
14162 const char *target_physname;
14163
14164 /* Prefer the mangled name; otherwise compute the demangled one. */
14165 target_physname = dw2_linkage_name (target_die, target_cu);
14166 if (target_physname == NULL)
14167 target_physname = dwarf2_physname (NULL, target_die, target_cu);
14168 if (target_physname == NULL)
14169 complaint (_("DW_AT_call_target target DIE has invalid "
14170 "physname, for referencing DIE %s [in module %s]"),
14171 sect_offset_str (die->sect_off), objfile_name (objfile));
14172 else
14173 SET_FIELD_PHYSNAME (call_site->target, target_physname);
14174 }
14175 else
14176 {
14177 CORE_ADDR lowpc;
14178
14179 /* DW_AT_entry_pc should be preferred. */
14180 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
14181 <= PC_BOUNDS_INVALID)
14182 complaint (_("DW_AT_call_target target DIE has invalid "
14183 "low pc, for referencing DIE %s [in module %s]"),
14184 sect_offset_str (die->sect_off), objfile_name (objfile));
14185 else
14186 {
14187 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14188 SET_FIELD_PHYSADDR (call_site->target, lowpc);
14189 }
14190 }
14191 }
14192 else
14193 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
14194 "block nor reference, for DIE %s [in module %s]"),
14195 sect_offset_str (die->sect_off), objfile_name (objfile));
14196
14197 call_site->per_cu = cu->per_cu;
14198
14199 for (child_die = die->child;
14200 child_die && child_die->tag;
14201 child_die = sibling_die (child_die))
14202 {
14203 struct call_site_parameter *parameter;
14204 struct attribute *loc, *origin;
14205
14206 if (child_die->tag != DW_TAG_call_site_parameter
14207 && child_die->tag != DW_TAG_GNU_call_site_parameter)
14208 {
14209 /* Already printed the complaint above. */
14210 continue;
14211 }
14212
14213 gdb_assert (call_site->parameter_count < nparams);
14214 parameter = &call_site->parameter[call_site->parameter_count];
14215
14216 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14217 specifies DW_TAG_formal_parameter. Value of the data assumed for the
14218 register is contained in DW_AT_call_value. */
14219
14220 loc = dwarf2_attr (child_die, DW_AT_location, cu);
14221 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14222 if (origin == NULL)
14223 {
14224 /* This was a pre-DWARF-5 GNU extension alias
14225 for DW_AT_call_parameter. */
14226 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14227 }
14228 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
14229 {
14230 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
14231
14232 sect_offset sect_off
14233 = (sect_offset) dwarf2_get_ref_die_offset (origin);
14234 if (!offset_in_cu_p (&cu->header, sect_off))
14235 {
14236 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14237 binding can be done only inside one CU. Such referenced DIE
14238 therefore cannot be even moved to DW_TAG_partial_unit. */
14239 complaint (_("DW_AT_call_parameter offset is not in CU for "
14240 "DW_TAG_call_site child DIE %s [in module %s]"),
14241 sect_offset_str (child_die->sect_off),
14242 objfile_name (objfile));
14243 continue;
14244 }
14245 parameter->u.param_cu_off
14246 = (cu_offset) (sect_off - cu->header.sect_off);
14247 }
14248 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
14249 {
14250 complaint (_("No DW_FORM_block* DW_AT_location for "
14251 "DW_TAG_call_site child DIE %s [in module %s]"),
14252 sect_offset_str (child_die->sect_off), objfile_name (objfile));
14253 continue;
14254 }
14255 else
14256 {
14257 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
14258 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
14259 if (parameter->u.dwarf_reg != -1)
14260 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
14261 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
14262 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
14263 &parameter->u.fb_offset))
14264 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14265 else
14266 {
14267 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
14268 "for DW_FORM_block* DW_AT_location is supported for "
14269 "DW_TAG_call_site child DIE %s "
14270 "[in module %s]"),
14271 sect_offset_str (child_die->sect_off),
14272 objfile_name (objfile));
14273 continue;
14274 }
14275 }
14276
14277 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14278 if (attr == NULL)
14279 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
14280 if (!attr_form_is_block (attr))
14281 {
14282 complaint (_("No DW_FORM_block* DW_AT_call_value for "
14283 "DW_TAG_call_site child DIE %s [in module %s]"),
14284 sect_offset_str (child_die->sect_off),
14285 objfile_name (objfile));
14286 continue;
14287 }
14288 parameter->value = DW_BLOCK (attr)->data;
14289 parameter->value_size = DW_BLOCK (attr)->size;
14290
14291 /* Parameters are not pre-cleared by memset above. */
14292 parameter->data_value = NULL;
14293 parameter->data_value_size = 0;
14294 call_site->parameter_count++;
14295
14296 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14297 if (attr == NULL)
14298 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
14299 if (attr != nullptr)
14300 {
14301 if (!attr_form_is_block (attr))
14302 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
14303 "DW_TAG_call_site child DIE %s [in module %s]"),
14304 sect_offset_str (child_die->sect_off),
14305 objfile_name (objfile));
14306 else
14307 {
14308 parameter->data_value = DW_BLOCK (attr)->data;
14309 parameter->data_value_size = DW_BLOCK (attr)->size;
14310 }
14311 }
14312 }
14313 }
14314
14315 /* Helper function for read_variable. If DIE represents a virtual
14316 table, then return the type of the concrete object that is
14317 associated with the virtual table. Otherwise, return NULL. */
14318
14319 static struct type *
14320 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14321 {
14322 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14323 if (attr == NULL)
14324 return NULL;
14325
14326 /* Find the type DIE. */
14327 struct die_info *type_die = NULL;
14328 struct dwarf2_cu *type_cu = cu;
14329
14330 if (attr_form_is_ref (attr))
14331 type_die = follow_die_ref (die, attr, &type_cu);
14332 if (type_die == NULL)
14333 return NULL;
14334
14335 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14336 return NULL;
14337 return die_containing_type (type_die, type_cu);
14338 }
14339
14340 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14341
14342 static void
14343 read_variable (struct die_info *die, struct dwarf2_cu *cu)
14344 {
14345 struct rust_vtable_symbol *storage = NULL;
14346
14347 if (cu->language == language_rust)
14348 {
14349 struct type *containing_type = rust_containing_type (die, cu);
14350
14351 if (containing_type != NULL)
14352 {
14353 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14354
14355 storage = new (&objfile->objfile_obstack) rust_vtable_symbol ();
14356 initialize_objfile_symbol (storage);
14357 storage->concrete_type = containing_type;
14358 storage->subclass = SYMBOL_RUST_VTABLE;
14359 }
14360 }
14361
14362 struct symbol *res = new_symbol (die, NULL, cu, storage);
14363 struct attribute *abstract_origin
14364 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14365 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
14366 if (res == NULL && loc && abstract_origin)
14367 {
14368 /* We have a variable without a name, but with a location and an abstract
14369 origin. This may be a concrete instance of an abstract variable
14370 referenced from an DW_OP_GNU_variable_value, so save it to find it back
14371 later. */
14372 struct dwarf2_cu *origin_cu = cu;
14373 struct die_info *origin_die
14374 = follow_die_ref (die, abstract_origin, &origin_cu);
14375 dwarf2_per_objfile *dpo = cu->per_cu->dwarf2_per_objfile;
14376 dpo->abstract_to_concrete[origin_die->sect_off].push_back (die->sect_off);
14377 }
14378 }
14379
14380 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14381 reading .debug_rnglists.
14382 Callback's type should be:
14383 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14384 Return true if the attributes are present and valid, otherwise,
14385 return false. */
14386
14387 template <typename Callback>
14388 static bool
14389 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
14390 Callback &&callback)
14391 {
14392 struct dwarf2_per_objfile *dwarf2_per_objfile
14393 = cu->per_cu->dwarf2_per_objfile;
14394 struct objfile *objfile = dwarf2_per_objfile->objfile;
14395 bfd *obfd = objfile->obfd;
14396 /* Base address selection entry. */
14397 CORE_ADDR base;
14398 int found_base;
14399 const gdb_byte *buffer;
14400 CORE_ADDR baseaddr;
14401 bool overflow = false;
14402
14403 found_base = cu->base_known;
14404 base = cu->base_address;
14405
14406 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
14407 if (offset >= dwarf2_per_objfile->rnglists.size)
14408 {
14409 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
14410 offset);
14411 return false;
14412 }
14413 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
14414
14415 baseaddr = objfile->section_offsets[SECT_OFF_TEXT (objfile)];
14416
14417 while (1)
14418 {
14419 /* Initialize it due to a false compiler warning. */
14420 CORE_ADDR range_beginning = 0, range_end = 0;
14421 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
14422 + dwarf2_per_objfile->rnglists.size);
14423 unsigned int bytes_read;
14424
14425 if (buffer == buf_end)
14426 {
14427 overflow = true;
14428 break;
14429 }
14430 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14431 switch (rlet)
14432 {
14433 case DW_RLE_end_of_list:
14434 break;
14435 case DW_RLE_base_address:
14436 if (buffer + cu->header.addr_size > buf_end)
14437 {
14438 overflow = true;
14439 break;
14440 }
14441 base = read_address (obfd, buffer, cu, &bytes_read);
14442 found_base = 1;
14443 buffer += bytes_read;
14444 break;
14445 case DW_RLE_start_length:
14446 if (buffer + cu->header.addr_size > buf_end)
14447 {
14448 overflow = true;
14449 break;
14450 }
14451 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14452 buffer += bytes_read;
14453 range_end = (range_beginning
14454 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14455 buffer += bytes_read;
14456 if (buffer > buf_end)
14457 {
14458 overflow = true;
14459 break;
14460 }
14461 break;
14462 case DW_RLE_offset_pair:
14463 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14464 buffer += bytes_read;
14465 if (buffer > buf_end)
14466 {
14467 overflow = true;
14468 break;
14469 }
14470 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14471 buffer += bytes_read;
14472 if (buffer > buf_end)
14473 {
14474 overflow = true;
14475 break;
14476 }
14477 break;
14478 case DW_RLE_start_end:
14479 if (buffer + 2 * cu->header.addr_size > buf_end)
14480 {
14481 overflow = true;
14482 break;
14483 }
14484 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14485 buffer += bytes_read;
14486 range_end = read_address (obfd, buffer, cu, &bytes_read);
14487 buffer += bytes_read;
14488 break;
14489 default:
14490 complaint (_("Invalid .debug_rnglists data (no base address)"));
14491 return false;
14492 }
14493 if (rlet == DW_RLE_end_of_list || overflow)
14494 break;
14495 if (rlet == DW_RLE_base_address)
14496 continue;
14497
14498 if (!found_base)
14499 {
14500 /* We have no valid base address for the ranges
14501 data. */
14502 complaint (_("Invalid .debug_rnglists data (no base address)"));
14503 return false;
14504 }
14505
14506 if (range_beginning > range_end)
14507 {
14508 /* Inverted range entries are invalid. */
14509 complaint (_("Invalid .debug_rnglists data (inverted range)"));
14510 return false;
14511 }
14512
14513 /* Empty range entries have no effect. */
14514 if (range_beginning == range_end)
14515 continue;
14516
14517 range_beginning += base;
14518 range_end += base;
14519
14520 /* A not-uncommon case of bad debug info.
14521 Don't pollute the addrmap with bad data. */
14522 if (range_beginning + baseaddr == 0
14523 && !dwarf2_per_objfile->has_section_at_zero)
14524 {
14525 complaint (_(".debug_rnglists entry has start address of zero"
14526 " [in module %s]"), objfile_name (objfile));
14527 continue;
14528 }
14529
14530 callback (range_beginning, range_end);
14531 }
14532
14533 if (overflow)
14534 {
14535 complaint (_("Offset %d is not terminated "
14536 "for DW_AT_ranges attribute"),
14537 offset);
14538 return false;
14539 }
14540
14541 return true;
14542 }
14543
14544 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14545 Callback's type should be:
14546 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14547 Return 1 if the attributes are present and valid, otherwise, return 0. */
14548
14549 template <typename Callback>
14550 static int
14551 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
14552 Callback &&callback)
14553 {
14554 struct dwarf2_per_objfile *dwarf2_per_objfile
14555 = cu->per_cu->dwarf2_per_objfile;
14556 struct objfile *objfile = dwarf2_per_objfile->objfile;
14557 struct comp_unit_head *cu_header = &cu->header;
14558 bfd *obfd = objfile->obfd;
14559 unsigned int addr_size = cu_header->addr_size;
14560 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14561 /* Base address selection entry. */
14562 CORE_ADDR base;
14563 int found_base;
14564 unsigned int dummy;
14565 const gdb_byte *buffer;
14566 CORE_ADDR baseaddr;
14567
14568 if (cu_header->version >= 5)
14569 return dwarf2_rnglists_process (offset, cu, callback);
14570
14571 found_base = cu->base_known;
14572 base = cu->base_address;
14573
14574 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
14575 if (offset >= dwarf2_per_objfile->ranges.size)
14576 {
14577 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
14578 offset);
14579 return 0;
14580 }
14581 buffer = dwarf2_per_objfile->ranges.buffer + offset;
14582
14583 baseaddr = objfile->section_offsets[SECT_OFF_TEXT (objfile)];
14584
14585 while (1)
14586 {
14587 CORE_ADDR range_beginning, range_end;
14588
14589 range_beginning = read_address (obfd, buffer, cu, &dummy);
14590 buffer += addr_size;
14591 range_end = read_address (obfd, buffer, cu, &dummy);
14592 buffer += addr_size;
14593 offset += 2 * addr_size;
14594
14595 /* An end of list marker is a pair of zero addresses. */
14596 if (range_beginning == 0 && range_end == 0)
14597 /* Found the end of list entry. */
14598 break;
14599
14600 /* Each base address selection entry is a pair of 2 values.
14601 The first is the largest possible address, the second is
14602 the base address. Check for a base address here. */
14603 if ((range_beginning & mask) == mask)
14604 {
14605 /* If we found the largest possible address, then we already
14606 have the base address in range_end. */
14607 base = range_end;
14608 found_base = 1;
14609 continue;
14610 }
14611
14612 if (!found_base)
14613 {
14614 /* We have no valid base address for the ranges
14615 data. */
14616 complaint (_("Invalid .debug_ranges data (no base address)"));
14617 return 0;
14618 }
14619
14620 if (range_beginning > range_end)
14621 {
14622 /* Inverted range entries are invalid. */
14623 complaint (_("Invalid .debug_ranges data (inverted range)"));
14624 return 0;
14625 }
14626
14627 /* Empty range entries have no effect. */
14628 if (range_beginning == range_end)
14629 continue;
14630
14631 range_beginning += base;
14632 range_end += base;
14633
14634 /* A not-uncommon case of bad debug info.
14635 Don't pollute the addrmap with bad data. */
14636 if (range_beginning + baseaddr == 0
14637 && !dwarf2_per_objfile->has_section_at_zero)
14638 {
14639 complaint (_(".debug_ranges entry has start address of zero"
14640 " [in module %s]"), objfile_name (objfile));
14641 continue;
14642 }
14643
14644 callback (range_beginning, range_end);
14645 }
14646
14647 return 1;
14648 }
14649
14650 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14651 Return 1 if the attributes are present and valid, otherwise, return 0.
14652 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14653
14654 static int
14655 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14656 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14657 struct partial_symtab *ranges_pst)
14658 {
14659 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14660 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14661 const CORE_ADDR baseaddr = objfile->section_offsets[SECT_OFF_TEXT (objfile)];
14662 int low_set = 0;
14663 CORE_ADDR low = 0;
14664 CORE_ADDR high = 0;
14665 int retval;
14666
14667 retval = dwarf2_ranges_process (offset, cu,
14668 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14669 {
14670 if (ranges_pst != NULL)
14671 {
14672 CORE_ADDR lowpc;
14673 CORE_ADDR highpc;
14674
14675 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14676 range_beginning + baseaddr)
14677 - baseaddr);
14678 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14679 range_end + baseaddr)
14680 - baseaddr);
14681 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
14682 lowpc, highpc - 1, ranges_pst);
14683 }
14684
14685 /* FIXME: This is recording everything as a low-high
14686 segment of consecutive addresses. We should have a
14687 data structure for discontiguous block ranges
14688 instead. */
14689 if (! low_set)
14690 {
14691 low = range_beginning;
14692 high = range_end;
14693 low_set = 1;
14694 }
14695 else
14696 {
14697 if (range_beginning < low)
14698 low = range_beginning;
14699 if (range_end > high)
14700 high = range_end;
14701 }
14702 });
14703 if (!retval)
14704 return 0;
14705
14706 if (! low_set)
14707 /* If the first entry is an end-of-list marker, the range
14708 describes an empty scope, i.e. no instructions. */
14709 return 0;
14710
14711 if (low_return)
14712 *low_return = low;
14713 if (high_return)
14714 *high_return = high;
14715 return 1;
14716 }
14717
14718 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
14719 definition for the return value. *LOWPC and *HIGHPC are set iff
14720 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
14721
14722 static enum pc_bounds_kind
14723 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
14724 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14725 struct partial_symtab *pst)
14726 {
14727 struct dwarf2_per_objfile *dwarf2_per_objfile
14728 = cu->per_cu->dwarf2_per_objfile;
14729 struct attribute *attr;
14730 struct attribute *attr_high;
14731 CORE_ADDR low = 0;
14732 CORE_ADDR high = 0;
14733 enum pc_bounds_kind ret;
14734
14735 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14736 if (attr_high)
14737 {
14738 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14739 if (attr != nullptr)
14740 {
14741 low = attr_value_as_address (attr);
14742 high = attr_value_as_address (attr_high);
14743 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14744 high += low;
14745 }
14746 else
14747 /* Found high w/o low attribute. */
14748 return PC_BOUNDS_INVALID;
14749
14750 /* Found consecutive range of addresses. */
14751 ret = PC_BOUNDS_HIGH_LOW;
14752 }
14753 else
14754 {
14755 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14756 if (attr != NULL)
14757 {
14758 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
14759 We take advantage of the fact that DW_AT_ranges does not appear
14760 in DW_TAG_compile_unit of DWO files. */
14761 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14762 unsigned int ranges_offset = (DW_UNSND (attr)
14763 + (need_ranges_base
14764 ? cu->ranges_base
14765 : 0));
14766
14767 /* Value of the DW_AT_ranges attribute is the offset in the
14768 .debug_ranges section. */
14769 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
14770 return PC_BOUNDS_INVALID;
14771 /* Found discontinuous range of addresses. */
14772 ret = PC_BOUNDS_RANGES;
14773 }
14774 else
14775 return PC_BOUNDS_NOT_PRESENT;
14776 }
14777
14778 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
14779 if (high <= low)
14780 return PC_BOUNDS_INVALID;
14781
14782 /* When using the GNU linker, .gnu.linkonce. sections are used to
14783 eliminate duplicate copies of functions and vtables and such.
14784 The linker will arbitrarily choose one and discard the others.
14785 The AT_*_pc values for such functions refer to local labels in
14786 these sections. If the section from that file was discarded, the
14787 labels are not in the output, so the relocs get a value of 0.
14788 If this is a discarded function, mark the pc bounds as invalid,
14789 so that GDB will ignore it. */
14790 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
14791 return PC_BOUNDS_INVALID;
14792
14793 *lowpc = low;
14794 if (highpc)
14795 *highpc = high;
14796 return ret;
14797 }
14798
14799 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
14800 its low and high PC addresses. Do nothing if these addresses could not
14801 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14802 and HIGHPC to the high address if greater than HIGHPC. */
14803
14804 static void
14805 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14806 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14807 struct dwarf2_cu *cu)
14808 {
14809 CORE_ADDR low, high;
14810 struct die_info *child = die->child;
14811
14812 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
14813 {
14814 *lowpc = std::min (*lowpc, low);
14815 *highpc = std::max (*highpc, high);
14816 }
14817
14818 /* If the language does not allow nested subprograms (either inside
14819 subprograms or lexical blocks), we're done. */
14820 if (cu->language != language_ada)
14821 return;
14822
14823 /* Check all the children of the given DIE. If it contains nested
14824 subprograms, then check their pc bounds. Likewise, we need to
14825 check lexical blocks as well, as they may also contain subprogram
14826 definitions. */
14827 while (child && child->tag)
14828 {
14829 if (child->tag == DW_TAG_subprogram
14830 || child->tag == DW_TAG_lexical_block)
14831 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14832 child = sibling_die (child);
14833 }
14834 }
14835
14836 /* Get the low and high pc's represented by the scope DIE, and store
14837 them in *LOWPC and *HIGHPC. If the correct values can't be
14838 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14839
14840 static void
14841 get_scope_pc_bounds (struct die_info *die,
14842 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14843 struct dwarf2_cu *cu)
14844 {
14845 CORE_ADDR best_low = (CORE_ADDR) -1;
14846 CORE_ADDR best_high = (CORE_ADDR) 0;
14847 CORE_ADDR current_low, current_high;
14848
14849 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
14850 >= PC_BOUNDS_RANGES)
14851 {
14852 best_low = current_low;
14853 best_high = current_high;
14854 }
14855 else
14856 {
14857 struct die_info *child = die->child;
14858
14859 while (child && child->tag)
14860 {
14861 switch (child->tag) {
14862 case DW_TAG_subprogram:
14863 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14864 break;
14865 case DW_TAG_namespace:
14866 case DW_TAG_module:
14867 /* FIXME: carlton/2004-01-16: Should we do this for
14868 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14869 that current GCC's always emit the DIEs corresponding
14870 to definitions of methods of classes as children of a
14871 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14872 the DIEs giving the declarations, which could be
14873 anywhere). But I don't see any reason why the
14874 standards says that they have to be there. */
14875 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14876
14877 if (current_low != ((CORE_ADDR) -1))
14878 {
14879 best_low = std::min (best_low, current_low);
14880 best_high = std::max (best_high, current_high);
14881 }
14882 break;
14883 default:
14884 /* Ignore. */
14885 break;
14886 }
14887
14888 child = sibling_die (child);
14889 }
14890 }
14891
14892 *lowpc = best_low;
14893 *highpc = best_high;
14894 }
14895
14896 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14897 in DIE. */
14898
14899 static void
14900 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14901 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14902 {
14903 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14904 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14905 struct attribute *attr;
14906 struct attribute *attr_high;
14907
14908 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14909 if (attr_high)
14910 {
14911 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14912 if (attr != nullptr)
14913 {
14914 CORE_ADDR low = attr_value_as_address (attr);
14915 CORE_ADDR high = attr_value_as_address (attr_high);
14916
14917 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14918 high += low;
14919
14920 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14921 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14922 cu->get_builder ()->record_block_range (block, low, high - 1);
14923 }
14924 }
14925
14926 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14927 if (attr != nullptr)
14928 {
14929 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
14930 We take advantage of the fact that DW_AT_ranges does not appear
14931 in DW_TAG_compile_unit of DWO files. */
14932 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14933
14934 /* The value of the DW_AT_ranges attribute is the offset of the
14935 address range list in the .debug_ranges section. */
14936 unsigned long offset = (DW_UNSND (attr)
14937 + (need_ranges_base ? cu->ranges_base : 0));
14938
14939 std::vector<blockrange> blockvec;
14940 dwarf2_ranges_process (offset, cu,
14941 [&] (CORE_ADDR start, CORE_ADDR end)
14942 {
14943 start += baseaddr;
14944 end += baseaddr;
14945 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14946 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14947 cu->get_builder ()->record_block_range (block, start, end - 1);
14948 blockvec.emplace_back (start, end);
14949 });
14950
14951 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
14952 }
14953 }
14954
14955 /* Check whether the producer field indicates either of GCC < 4.6, or the
14956 Intel C/C++ compiler, and cache the result in CU. */
14957
14958 static void
14959 check_producer (struct dwarf2_cu *cu)
14960 {
14961 int major, minor;
14962
14963 if (cu->producer == NULL)
14964 {
14965 /* For unknown compilers expect their behavior is DWARF version
14966 compliant.
14967
14968 GCC started to support .debug_types sections by -gdwarf-4 since
14969 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14970 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14971 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14972 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14973 }
14974 else if (producer_is_gcc (cu->producer, &major, &minor))
14975 {
14976 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14977 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14978 }
14979 else if (producer_is_icc (cu->producer, &major, &minor))
14980 {
14981 cu->producer_is_icc = true;
14982 cu->producer_is_icc_lt_14 = major < 14;
14983 }
14984 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14985 cu->producer_is_codewarrior = true;
14986 else
14987 {
14988 /* For other non-GCC compilers, expect their behavior is DWARF version
14989 compliant. */
14990 }
14991
14992 cu->checked_producer = true;
14993 }
14994
14995 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14996 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14997 during 4.6.0 experimental. */
14998
14999 static bool
15000 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
15001 {
15002 if (!cu->checked_producer)
15003 check_producer (cu);
15004
15005 return cu->producer_is_gxx_lt_4_6;
15006 }
15007
15008
15009 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
15010 with incorrect is_stmt attributes. */
15011
15012 static bool
15013 producer_is_codewarrior (struct dwarf2_cu *cu)
15014 {
15015 if (!cu->checked_producer)
15016 check_producer (cu);
15017
15018 return cu->producer_is_codewarrior;
15019 }
15020
15021 /* Return the default accessibility type if it is not overridden by
15022 DW_AT_accessibility. */
15023
15024 static enum dwarf_access_attribute
15025 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
15026 {
15027 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
15028 {
15029 /* The default DWARF 2 accessibility for members is public, the default
15030 accessibility for inheritance is private. */
15031
15032 if (die->tag != DW_TAG_inheritance)
15033 return DW_ACCESS_public;
15034 else
15035 return DW_ACCESS_private;
15036 }
15037 else
15038 {
15039 /* DWARF 3+ defines the default accessibility a different way. The same
15040 rules apply now for DW_TAG_inheritance as for the members and it only
15041 depends on the container kind. */
15042
15043 if (die->parent->tag == DW_TAG_class_type)
15044 return DW_ACCESS_private;
15045 else
15046 return DW_ACCESS_public;
15047 }
15048 }
15049
15050 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
15051 offset. If the attribute was not found return 0, otherwise return
15052 1. If it was found but could not properly be handled, set *OFFSET
15053 to 0. */
15054
15055 static int
15056 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
15057 LONGEST *offset)
15058 {
15059 struct attribute *attr;
15060
15061 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
15062 if (attr != NULL)
15063 {
15064 *offset = 0;
15065
15066 /* Note that we do not check for a section offset first here.
15067 This is because DW_AT_data_member_location is new in DWARF 4,
15068 so if we see it, we can assume that a constant form is really
15069 a constant and not a section offset. */
15070 if (attr_form_is_constant (attr))
15071 *offset = dwarf2_get_attr_constant_value (attr, 0);
15072 else if (attr_form_is_section_offset (attr))
15073 dwarf2_complex_location_expr_complaint ();
15074 else if (attr_form_is_block (attr))
15075 *offset = decode_locdesc (DW_BLOCK (attr), cu);
15076 else
15077 dwarf2_complex_location_expr_complaint ();
15078
15079 return 1;
15080 }
15081
15082 return 0;
15083 }
15084
15085 /* Add an aggregate field to the field list. */
15086
15087 static void
15088 dwarf2_add_field (struct field_info *fip, struct die_info *die,
15089 struct dwarf2_cu *cu)
15090 {
15091 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15092 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15093 struct nextfield *new_field;
15094 struct attribute *attr;
15095 struct field *fp;
15096 const char *fieldname = "";
15097
15098 if (die->tag == DW_TAG_inheritance)
15099 {
15100 fip->baseclasses.emplace_back ();
15101 new_field = &fip->baseclasses.back ();
15102 }
15103 else
15104 {
15105 fip->fields.emplace_back ();
15106 new_field = &fip->fields.back ();
15107 }
15108
15109 fip->nfields++;
15110
15111 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15112 if (attr != nullptr)
15113 new_field->accessibility = DW_UNSND (attr);
15114 else
15115 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
15116 if (new_field->accessibility != DW_ACCESS_public)
15117 fip->non_public_fields = 1;
15118
15119 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15120 if (attr != nullptr)
15121 new_field->virtuality = DW_UNSND (attr);
15122 else
15123 new_field->virtuality = DW_VIRTUALITY_none;
15124
15125 fp = &new_field->field;
15126
15127 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
15128 {
15129 LONGEST offset;
15130
15131 /* Data member other than a C++ static data member. */
15132
15133 /* Get type of field. */
15134 fp->type = die_type (die, cu);
15135
15136 SET_FIELD_BITPOS (*fp, 0);
15137
15138 /* Get bit size of field (zero if none). */
15139 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
15140 if (attr != nullptr)
15141 {
15142 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
15143 }
15144 else
15145 {
15146 FIELD_BITSIZE (*fp) = 0;
15147 }
15148
15149 /* Get bit offset of field. */
15150 if (handle_data_member_location (die, cu, &offset))
15151 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
15152 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
15153 if (attr != nullptr)
15154 {
15155 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
15156 {
15157 /* For big endian bits, the DW_AT_bit_offset gives the
15158 additional bit offset from the MSB of the containing
15159 anonymous object to the MSB of the field. We don't
15160 have to do anything special since we don't need to
15161 know the size of the anonymous object. */
15162 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
15163 }
15164 else
15165 {
15166 /* For little endian bits, compute the bit offset to the
15167 MSB of the anonymous object, subtract off the number of
15168 bits from the MSB of the field to the MSB of the
15169 object, and then subtract off the number of bits of
15170 the field itself. The result is the bit offset of
15171 the LSB of the field. */
15172 int anonymous_size;
15173 int bit_offset = DW_UNSND (attr);
15174
15175 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15176 if (attr != nullptr)
15177 {
15178 /* The size of the anonymous object containing
15179 the bit field is explicit, so use the
15180 indicated size (in bytes). */
15181 anonymous_size = DW_UNSND (attr);
15182 }
15183 else
15184 {
15185 /* The size of the anonymous object containing
15186 the bit field must be inferred from the type
15187 attribute of the data member containing the
15188 bit field. */
15189 anonymous_size = TYPE_LENGTH (fp->type);
15190 }
15191 SET_FIELD_BITPOS (*fp,
15192 (FIELD_BITPOS (*fp)
15193 + anonymous_size * bits_per_byte
15194 - bit_offset - FIELD_BITSIZE (*fp)));
15195 }
15196 }
15197 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15198 if (attr != NULL)
15199 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15200 + dwarf2_get_attr_constant_value (attr, 0)));
15201
15202 /* Get name of field. */
15203 fieldname = dwarf2_name (die, cu);
15204 if (fieldname == NULL)
15205 fieldname = "";
15206
15207 /* The name is already allocated along with this objfile, so we don't
15208 need to duplicate it for the type. */
15209 fp->name = fieldname;
15210
15211 /* Change accessibility for artificial fields (e.g. virtual table
15212 pointer or virtual base class pointer) to private. */
15213 if (dwarf2_attr (die, DW_AT_artificial, cu))
15214 {
15215 FIELD_ARTIFICIAL (*fp) = 1;
15216 new_field->accessibility = DW_ACCESS_private;
15217 fip->non_public_fields = 1;
15218 }
15219 }
15220 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
15221 {
15222 /* C++ static member. */
15223
15224 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15225 is a declaration, but all versions of G++ as of this writing
15226 (so through at least 3.2.1) incorrectly generate
15227 DW_TAG_variable tags. */
15228
15229 const char *physname;
15230
15231 /* Get name of field. */
15232 fieldname = dwarf2_name (die, cu);
15233 if (fieldname == NULL)
15234 return;
15235
15236 attr = dwarf2_attr (die, DW_AT_const_value, cu);
15237 if (attr
15238 /* Only create a symbol if this is an external value.
15239 new_symbol checks this and puts the value in the global symbol
15240 table, which we want. If it is not external, new_symbol
15241 will try to put the value in cu->list_in_scope which is wrong. */
15242 && dwarf2_flag_true_p (die, DW_AT_external, cu))
15243 {
15244 /* A static const member, not much different than an enum as far as
15245 we're concerned, except that we can support more types. */
15246 new_symbol (die, NULL, cu);
15247 }
15248
15249 /* Get physical name. */
15250 physname = dwarf2_physname (fieldname, die, cu);
15251
15252 /* The name is already allocated along with this objfile, so we don't
15253 need to duplicate it for the type. */
15254 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
15255 FIELD_TYPE (*fp) = die_type (die, cu);
15256 FIELD_NAME (*fp) = fieldname;
15257 }
15258 else if (die->tag == DW_TAG_inheritance)
15259 {
15260 LONGEST offset;
15261
15262 /* C++ base class field. */
15263 if (handle_data_member_location (die, cu, &offset))
15264 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
15265 FIELD_BITSIZE (*fp) = 0;
15266 FIELD_TYPE (*fp) = die_type (die, cu);
15267 FIELD_NAME (*fp) = TYPE_NAME (fp->type);
15268 }
15269 else if (die->tag == DW_TAG_variant_part)
15270 {
15271 /* process_structure_scope will treat this DIE as a union. */
15272 process_structure_scope (die, cu);
15273
15274 /* The variant part is relative to the start of the enclosing
15275 structure. */
15276 SET_FIELD_BITPOS (*fp, 0);
15277 fp->type = get_die_type (die, cu);
15278 fp->artificial = 1;
15279 fp->name = "<<variant>>";
15280
15281 /* Normally a DW_TAG_variant_part won't have a size, but our
15282 representation requires one, so set it to the maximum of the
15283 child sizes, being sure to account for the offset at which
15284 each child is seen. */
15285 if (TYPE_LENGTH (fp->type) == 0)
15286 {
15287 unsigned max = 0;
15288 for (int i = 0; i < TYPE_NFIELDS (fp->type); ++i)
15289 {
15290 unsigned len = ((TYPE_FIELD_BITPOS (fp->type, i) + 7) / 8
15291 + TYPE_LENGTH (TYPE_FIELD_TYPE (fp->type, i)));
15292 if (len > max)
15293 max = len;
15294 }
15295 TYPE_LENGTH (fp->type) = max;
15296 }
15297 }
15298 else
15299 gdb_assert_not_reached ("missing case in dwarf2_add_field");
15300 }
15301
15302 /* Can the type given by DIE define another type? */
15303
15304 static bool
15305 type_can_define_types (const struct die_info *die)
15306 {
15307 switch (die->tag)
15308 {
15309 case DW_TAG_typedef:
15310 case DW_TAG_class_type:
15311 case DW_TAG_structure_type:
15312 case DW_TAG_union_type:
15313 case DW_TAG_enumeration_type:
15314 return true;
15315
15316 default:
15317 return false;
15318 }
15319 }
15320
15321 /* Add a type definition defined in the scope of the FIP's class. */
15322
15323 static void
15324 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15325 struct dwarf2_cu *cu)
15326 {
15327 struct decl_field fp;
15328 memset (&fp, 0, sizeof (fp));
15329
15330 gdb_assert (type_can_define_types (die));
15331
15332 /* Get name of field. NULL is okay here, meaning an anonymous type. */
15333 fp.name = dwarf2_name (die, cu);
15334 fp.type = read_type_die (die, cu);
15335
15336 /* Save accessibility. */
15337 enum dwarf_access_attribute accessibility;
15338 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15339 if (attr != NULL)
15340 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15341 else
15342 accessibility = dwarf2_default_access_attribute (die, cu);
15343 switch (accessibility)
15344 {
15345 case DW_ACCESS_public:
15346 /* The assumed value if neither private nor protected. */
15347 break;
15348 case DW_ACCESS_private:
15349 fp.is_private = 1;
15350 break;
15351 case DW_ACCESS_protected:
15352 fp.is_protected = 1;
15353 break;
15354 default:
15355 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
15356 }
15357
15358 if (die->tag == DW_TAG_typedef)
15359 fip->typedef_field_list.push_back (fp);
15360 else
15361 fip->nested_types_list.push_back (fp);
15362 }
15363
15364 /* Create the vector of fields, and attach it to the type. */
15365
15366 static void
15367 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
15368 struct dwarf2_cu *cu)
15369 {
15370 int nfields = fip->nfields;
15371
15372 /* Record the field count, allocate space for the array of fields,
15373 and create blank accessibility bitfields if necessary. */
15374 TYPE_NFIELDS (type) = nfields;
15375 TYPE_FIELDS (type) = (struct field *)
15376 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
15377
15378 if (fip->non_public_fields && cu->language != language_ada)
15379 {
15380 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15381
15382 TYPE_FIELD_PRIVATE_BITS (type) =
15383 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15384 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15385
15386 TYPE_FIELD_PROTECTED_BITS (type) =
15387 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15388 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15389
15390 TYPE_FIELD_IGNORE_BITS (type) =
15391 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15392 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
15393 }
15394
15395 /* If the type has baseclasses, allocate and clear a bit vector for
15396 TYPE_FIELD_VIRTUAL_BITS. */
15397 if (!fip->baseclasses.empty () && cu->language != language_ada)
15398 {
15399 int num_bytes = B_BYTES (fip->baseclasses.size ());
15400 unsigned char *pointer;
15401
15402 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15403 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
15404 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
15405 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15406 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
15407 }
15408
15409 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
15410 {
15411 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
15412
15413 for (int index = 0; index < nfields; ++index)
15414 {
15415 struct nextfield &field = fip->fields[index];
15416
15417 if (field.variant.is_discriminant)
15418 di->discriminant_index = index;
15419 else if (field.variant.default_branch)
15420 di->default_index = index;
15421 else
15422 di->discriminants[index] = field.variant.discriminant_value;
15423 }
15424 }
15425
15426 /* Copy the saved-up fields into the field vector. */
15427 for (int i = 0; i < nfields; ++i)
15428 {
15429 struct nextfield &field
15430 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15431 : fip->fields[i - fip->baseclasses.size ()]);
15432
15433 TYPE_FIELD (type, i) = field.field;
15434 switch (field.accessibility)
15435 {
15436 case DW_ACCESS_private:
15437 if (cu->language != language_ada)
15438 SET_TYPE_FIELD_PRIVATE (type, i);
15439 break;
15440
15441 case DW_ACCESS_protected:
15442 if (cu->language != language_ada)
15443 SET_TYPE_FIELD_PROTECTED (type, i);
15444 break;
15445
15446 case DW_ACCESS_public:
15447 break;
15448
15449 default:
15450 /* Unknown accessibility. Complain and treat it as public. */
15451 {
15452 complaint (_("unsupported accessibility %d"),
15453 field.accessibility);
15454 }
15455 break;
15456 }
15457 if (i < fip->baseclasses.size ())
15458 {
15459 switch (field.virtuality)
15460 {
15461 case DW_VIRTUALITY_virtual:
15462 case DW_VIRTUALITY_pure_virtual:
15463 if (cu->language == language_ada)
15464 error (_("unexpected virtuality in component of Ada type"));
15465 SET_TYPE_FIELD_VIRTUAL (type, i);
15466 break;
15467 }
15468 }
15469 }
15470 }
15471
15472 /* Return true if this member function is a constructor, false
15473 otherwise. */
15474
15475 static int
15476 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15477 {
15478 const char *fieldname;
15479 const char *type_name;
15480 int len;
15481
15482 if (die->parent == NULL)
15483 return 0;
15484
15485 if (die->parent->tag != DW_TAG_structure_type
15486 && die->parent->tag != DW_TAG_union_type
15487 && die->parent->tag != DW_TAG_class_type)
15488 return 0;
15489
15490 fieldname = dwarf2_name (die, cu);
15491 type_name = dwarf2_name (die->parent, cu);
15492 if (fieldname == NULL || type_name == NULL)
15493 return 0;
15494
15495 len = strlen (fieldname);
15496 return (strncmp (fieldname, type_name, len) == 0
15497 && (type_name[len] == '\0' || type_name[len] == '<'));
15498 }
15499
15500 /* Check if the given VALUE is a recognized enum
15501 dwarf_defaulted_attribute constant according to DWARF5 spec,
15502 Table 7.24. */
15503
15504 static bool
15505 is_valid_DW_AT_defaulted (ULONGEST value)
15506 {
15507 switch (value)
15508 {
15509 case DW_DEFAULTED_no:
15510 case DW_DEFAULTED_in_class:
15511 case DW_DEFAULTED_out_of_class:
15512 return true;
15513 }
15514
15515 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
15516 return false;
15517 }
15518
15519 /* Add a member function to the proper fieldlist. */
15520
15521 static void
15522 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
15523 struct type *type, struct dwarf2_cu *cu)
15524 {
15525 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15526 struct attribute *attr;
15527 int i;
15528 struct fnfieldlist *flp = nullptr;
15529 struct fn_field *fnp;
15530 const char *fieldname;
15531 struct type *this_type;
15532 enum dwarf_access_attribute accessibility;
15533
15534 if (cu->language == language_ada)
15535 error (_("unexpected member function in Ada type"));
15536
15537 /* Get name of member function. */
15538 fieldname = dwarf2_name (die, cu);
15539 if (fieldname == NULL)
15540 return;
15541
15542 /* Look up member function name in fieldlist. */
15543 for (i = 0; i < fip->fnfieldlists.size (); i++)
15544 {
15545 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
15546 {
15547 flp = &fip->fnfieldlists[i];
15548 break;
15549 }
15550 }
15551
15552 /* Create a new fnfieldlist if necessary. */
15553 if (flp == nullptr)
15554 {
15555 fip->fnfieldlists.emplace_back ();
15556 flp = &fip->fnfieldlists.back ();
15557 flp->name = fieldname;
15558 i = fip->fnfieldlists.size () - 1;
15559 }
15560
15561 /* Create a new member function field and add it to the vector of
15562 fnfieldlists. */
15563 flp->fnfields.emplace_back ();
15564 fnp = &flp->fnfields.back ();
15565
15566 /* Delay processing of the physname until later. */
15567 if (cu->language == language_cplus)
15568 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15569 die, cu);
15570 else
15571 {
15572 const char *physname = dwarf2_physname (fieldname, die, cu);
15573 fnp->physname = physname ? physname : "";
15574 }
15575
15576 fnp->type = alloc_type (objfile);
15577 this_type = read_type_die (die, cu);
15578 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
15579 {
15580 int nparams = TYPE_NFIELDS (this_type);
15581
15582 /* TYPE is the domain of this method, and THIS_TYPE is the type
15583 of the method itself (TYPE_CODE_METHOD). */
15584 smash_to_method_type (fnp->type, type,
15585 TYPE_TARGET_TYPE (this_type),
15586 TYPE_FIELDS (this_type),
15587 TYPE_NFIELDS (this_type),
15588 TYPE_VARARGS (this_type));
15589
15590 /* Handle static member functions.
15591 Dwarf2 has no clean way to discern C++ static and non-static
15592 member functions. G++ helps GDB by marking the first
15593 parameter for non-static member functions (which is the this
15594 pointer) as artificial. We obtain this information from
15595 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15596 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15597 fnp->voffset = VOFFSET_STATIC;
15598 }
15599 else
15600 complaint (_("member function type missing for '%s'"),
15601 dwarf2_full_name (fieldname, die, cu));
15602
15603 /* Get fcontext from DW_AT_containing_type if present. */
15604 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15605 fnp->fcontext = die_containing_type (die, cu);
15606
15607 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15608 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15609
15610 /* Get accessibility. */
15611 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15612 if (attr != nullptr)
15613 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15614 else
15615 accessibility = dwarf2_default_access_attribute (die, cu);
15616 switch (accessibility)
15617 {
15618 case DW_ACCESS_private:
15619 fnp->is_private = 1;
15620 break;
15621 case DW_ACCESS_protected:
15622 fnp->is_protected = 1;
15623 break;
15624 }
15625
15626 /* Check for artificial methods. */
15627 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15628 if (attr && DW_UNSND (attr) != 0)
15629 fnp->is_artificial = 1;
15630
15631 /* Check for defaulted methods. */
15632 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
15633 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
15634 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
15635
15636 /* Check for deleted methods. */
15637 attr = dwarf2_attr (die, DW_AT_deleted, cu);
15638 if (attr != nullptr && DW_UNSND (attr) != 0)
15639 fnp->is_deleted = 1;
15640
15641 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15642
15643 /* Get index in virtual function table if it is a virtual member
15644 function. For older versions of GCC, this is an offset in the
15645 appropriate virtual table, as specified by DW_AT_containing_type.
15646 For everyone else, it is an expression to be evaluated relative
15647 to the object address. */
15648
15649 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15650 if (attr != nullptr)
15651 {
15652 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
15653 {
15654 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15655 {
15656 /* Old-style GCC. */
15657 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15658 }
15659 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15660 || (DW_BLOCK (attr)->size > 1
15661 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15662 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15663 {
15664 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15665 if ((fnp->voffset % cu->header.addr_size) != 0)
15666 dwarf2_complex_location_expr_complaint ();
15667 else
15668 fnp->voffset /= cu->header.addr_size;
15669 fnp->voffset += 2;
15670 }
15671 else
15672 dwarf2_complex_location_expr_complaint ();
15673
15674 if (!fnp->fcontext)
15675 {
15676 /* If there is no `this' field and no DW_AT_containing_type,
15677 we cannot actually find a base class context for the
15678 vtable! */
15679 if (TYPE_NFIELDS (this_type) == 0
15680 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15681 {
15682 complaint (_("cannot determine context for virtual member "
15683 "function \"%s\" (offset %s)"),
15684 fieldname, sect_offset_str (die->sect_off));
15685 }
15686 else
15687 {
15688 fnp->fcontext
15689 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15690 }
15691 }
15692 }
15693 else if (attr_form_is_section_offset (attr))
15694 {
15695 dwarf2_complex_location_expr_complaint ();
15696 }
15697 else
15698 {
15699 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15700 fieldname);
15701 }
15702 }
15703 else
15704 {
15705 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15706 if (attr && DW_UNSND (attr))
15707 {
15708 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15709 complaint (_("Member function \"%s\" (offset %s) is virtual "
15710 "but the vtable offset is not specified"),
15711 fieldname, sect_offset_str (die->sect_off));
15712 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15713 TYPE_CPLUS_DYNAMIC (type) = 1;
15714 }
15715 }
15716 }
15717
15718 /* Create the vector of member function fields, and attach it to the type. */
15719
15720 static void
15721 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15722 struct dwarf2_cu *cu)
15723 {
15724 if (cu->language == language_ada)
15725 error (_("unexpected member functions in Ada type"));
15726
15727 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15728 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15729 TYPE_ALLOC (type,
15730 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
15731
15732 for (int i = 0; i < fip->fnfieldlists.size (); i++)
15733 {
15734 struct fnfieldlist &nf = fip->fnfieldlists[i];
15735 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15736
15737 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15738 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
15739 fn_flp->fn_fields = (struct fn_field *)
15740 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15741
15742 for (int k = 0; k < nf.fnfields.size (); ++k)
15743 fn_flp->fn_fields[k] = nf.fnfields[k];
15744 }
15745
15746 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
15747 }
15748
15749 /* Returns non-zero if NAME is the name of a vtable member in CU's
15750 language, zero otherwise. */
15751 static int
15752 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15753 {
15754 static const char vptr[] = "_vptr";
15755
15756 /* Look for the C++ form of the vtable. */
15757 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15758 return 1;
15759
15760 return 0;
15761 }
15762
15763 /* GCC outputs unnamed structures that are really pointers to member
15764 functions, with the ABI-specified layout. If TYPE describes
15765 such a structure, smash it into a member function type.
15766
15767 GCC shouldn't do this; it should just output pointer to member DIEs.
15768 This is GCC PR debug/28767. */
15769
15770 static void
15771 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15772 {
15773 struct type *pfn_type, *self_type, *new_type;
15774
15775 /* Check for a structure with no name and two children. */
15776 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
15777 return;
15778
15779 /* Check for __pfn and __delta members. */
15780 if (TYPE_FIELD_NAME (type, 0) == NULL
15781 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15782 || TYPE_FIELD_NAME (type, 1) == NULL
15783 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15784 return;
15785
15786 /* Find the type of the method. */
15787 pfn_type = TYPE_FIELD_TYPE (type, 0);
15788 if (pfn_type == NULL
15789 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
15790 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
15791 return;
15792
15793 /* Look for the "this" argument. */
15794 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15795 if (TYPE_NFIELDS (pfn_type) == 0
15796 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
15797 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
15798 return;
15799
15800 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
15801 new_type = alloc_type (objfile);
15802 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15803 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
15804 TYPE_VARARGS (pfn_type));
15805 smash_to_methodptr_type (type, new_type);
15806 }
15807
15808 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
15809 appropriate error checking and issuing complaints if there is a
15810 problem. */
15811
15812 static ULONGEST
15813 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15814 {
15815 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15816
15817 if (attr == nullptr)
15818 return 0;
15819
15820 if (!attr_form_is_constant (attr))
15821 {
15822 complaint (_("DW_AT_alignment must have constant form"
15823 " - DIE at %s [in module %s]"),
15824 sect_offset_str (die->sect_off),
15825 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15826 return 0;
15827 }
15828
15829 ULONGEST align;
15830 if (attr->form == DW_FORM_sdata)
15831 {
15832 LONGEST val = DW_SND (attr);
15833 if (val < 0)
15834 {
15835 complaint (_("DW_AT_alignment value must not be negative"
15836 " - DIE at %s [in module %s]"),
15837 sect_offset_str (die->sect_off),
15838 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15839 return 0;
15840 }
15841 align = val;
15842 }
15843 else
15844 align = DW_UNSND (attr);
15845
15846 if (align == 0)
15847 {
15848 complaint (_("DW_AT_alignment value must not be zero"
15849 " - DIE at %s [in module %s]"),
15850 sect_offset_str (die->sect_off),
15851 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15852 return 0;
15853 }
15854 if ((align & (align - 1)) != 0)
15855 {
15856 complaint (_("DW_AT_alignment value must be a power of 2"
15857 " - DIE at %s [in module %s]"),
15858 sect_offset_str (die->sect_off),
15859 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15860 return 0;
15861 }
15862
15863 return align;
15864 }
15865
15866 /* If the DIE has a DW_AT_alignment attribute, use its value to set
15867 the alignment for TYPE. */
15868
15869 static void
15870 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15871 struct type *type)
15872 {
15873 if (!set_type_align (type, get_alignment (cu, die)))
15874 complaint (_("DW_AT_alignment value too large"
15875 " - DIE at %s [in module %s]"),
15876 sect_offset_str (die->sect_off),
15877 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15878 }
15879
15880 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15881 constant for a type, according to DWARF5 spec, Table 5.5. */
15882
15883 static bool
15884 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15885 {
15886 switch (value)
15887 {
15888 case DW_CC_normal:
15889 case DW_CC_pass_by_reference:
15890 case DW_CC_pass_by_value:
15891 return true;
15892
15893 default:
15894 complaint (_("unrecognized DW_AT_calling_convention value "
15895 "(%s) for a type"), pulongest (value));
15896 return false;
15897 }
15898 }
15899
15900 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15901 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15902 also according to GNU-specific values (see include/dwarf2.h). */
15903
15904 static bool
15905 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15906 {
15907 switch (value)
15908 {
15909 case DW_CC_normal:
15910 case DW_CC_program:
15911 case DW_CC_nocall:
15912 return true;
15913
15914 case DW_CC_GNU_renesas_sh:
15915 case DW_CC_GNU_borland_fastcall_i386:
15916 case DW_CC_GDB_IBM_OpenCL:
15917 return true;
15918
15919 default:
15920 complaint (_("unrecognized DW_AT_calling_convention value "
15921 "(%s) for a subroutine"), pulongest (value));
15922 return false;
15923 }
15924 }
15925
15926 /* Called when we find the DIE that starts a structure or union scope
15927 (definition) to create a type for the structure or union. Fill in
15928 the type's name and general properties; the members will not be
15929 processed until process_structure_scope. A symbol table entry for
15930 the type will also not be done until process_structure_scope (assuming
15931 the type has a name).
15932
15933 NOTE: we need to call these functions regardless of whether or not the
15934 DIE has a DW_AT_name attribute, since it might be an anonymous
15935 structure or union. This gets the type entered into our set of
15936 user defined types. */
15937
15938 static struct type *
15939 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15940 {
15941 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15942 struct type *type;
15943 struct attribute *attr;
15944 const char *name;
15945
15946 /* If the definition of this type lives in .debug_types, read that type.
15947 Don't follow DW_AT_specification though, that will take us back up
15948 the chain and we want to go down. */
15949 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
15950 if (attr != nullptr)
15951 {
15952 type = get_DW_AT_signature_type (die, attr, cu);
15953
15954 /* The type's CU may not be the same as CU.
15955 Ensure TYPE is recorded with CU in die_type_hash. */
15956 return set_die_type (die, type, cu);
15957 }
15958
15959 type = alloc_type (objfile);
15960 INIT_CPLUS_SPECIFIC (type);
15961
15962 name = dwarf2_name (die, cu);
15963 if (name != NULL)
15964 {
15965 if (cu->language == language_cplus
15966 || cu->language == language_d
15967 || cu->language == language_rust)
15968 {
15969 const char *full_name = dwarf2_full_name (name, die, cu);
15970
15971 /* dwarf2_full_name might have already finished building the DIE's
15972 type. If so, there is no need to continue. */
15973 if (get_die_type (die, cu) != NULL)
15974 return get_die_type (die, cu);
15975
15976 TYPE_NAME (type) = full_name;
15977 }
15978 else
15979 {
15980 /* The name is already allocated along with this objfile, so
15981 we don't need to duplicate it for the type. */
15982 TYPE_NAME (type) = name;
15983 }
15984 }
15985
15986 if (die->tag == DW_TAG_structure_type)
15987 {
15988 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15989 }
15990 else if (die->tag == DW_TAG_union_type)
15991 {
15992 TYPE_CODE (type) = TYPE_CODE_UNION;
15993 }
15994 else if (die->tag == DW_TAG_variant_part)
15995 {
15996 TYPE_CODE (type) = TYPE_CODE_UNION;
15997 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
15998 }
15999 else
16000 {
16001 TYPE_CODE (type) = TYPE_CODE_STRUCT;
16002 }
16003
16004 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
16005 TYPE_DECLARED_CLASS (type) = 1;
16006
16007 /* Store the calling convention in the type if it's available in
16008 the die. Otherwise the calling convention remains set to
16009 the default value DW_CC_normal. */
16010 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
16011 if (attr != nullptr
16012 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
16013 {
16014 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16015 TYPE_CPLUS_CALLING_CONVENTION (type)
16016 = (enum dwarf_calling_convention) (DW_UNSND (attr));
16017 }
16018
16019 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16020 if (attr != nullptr)
16021 {
16022 if (attr_form_is_constant (attr))
16023 TYPE_LENGTH (type) = DW_UNSND (attr);
16024 else
16025 {
16026 /* For the moment, dynamic type sizes are not supported
16027 by GDB's struct type. The actual size is determined
16028 on-demand when resolving the type of a given object,
16029 so set the type's length to zero for now. Otherwise,
16030 we record an expression as the length, and that expression
16031 could lead to a very large value, which could eventually
16032 lead to us trying to allocate that much memory when creating
16033 a value of that type. */
16034 TYPE_LENGTH (type) = 0;
16035 }
16036 }
16037 else
16038 {
16039 TYPE_LENGTH (type) = 0;
16040 }
16041
16042 maybe_set_alignment (cu, die, type);
16043
16044 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
16045 {
16046 /* ICC<14 does not output the required DW_AT_declaration on
16047 incomplete types, but gives them a size of zero. */
16048 TYPE_STUB (type) = 1;
16049 }
16050 else
16051 TYPE_STUB_SUPPORTED (type) = 1;
16052
16053 if (die_is_declaration (die, cu))
16054 TYPE_STUB (type) = 1;
16055 else if (attr == NULL && die->child == NULL
16056 && producer_is_realview (cu->producer))
16057 /* RealView does not output the required DW_AT_declaration
16058 on incomplete types. */
16059 TYPE_STUB (type) = 1;
16060
16061 /* We need to add the type field to the die immediately so we don't
16062 infinitely recurse when dealing with pointers to the structure
16063 type within the structure itself. */
16064 set_die_type (die, type, cu);
16065
16066 /* set_die_type should be already done. */
16067 set_descriptive_type (type, die, cu);
16068
16069 return type;
16070 }
16071
16072 /* A helper for process_structure_scope that handles a single member
16073 DIE. */
16074
16075 static void
16076 handle_struct_member_die (struct die_info *child_die, struct type *type,
16077 struct field_info *fi,
16078 std::vector<struct symbol *> *template_args,
16079 struct dwarf2_cu *cu)
16080 {
16081 if (child_die->tag == DW_TAG_member
16082 || child_die->tag == DW_TAG_variable
16083 || child_die->tag == DW_TAG_variant_part)
16084 {
16085 /* NOTE: carlton/2002-11-05: A C++ static data member
16086 should be a DW_TAG_member that is a declaration, but
16087 all versions of G++ as of this writing (so through at
16088 least 3.2.1) incorrectly generate DW_TAG_variable
16089 tags for them instead. */
16090 dwarf2_add_field (fi, child_die, cu);
16091 }
16092 else if (child_die->tag == DW_TAG_subprogram)
16093 {
16094 /* Rust doesn't have member functions in the C++ sense.
16095 However, it does emit ordinary functions as children
16096 of a struct DIE. */
16097 if (cu->language == language_rust)
16098 read_func_scope (child_die, cu);
16099 else
16100 {
16101 /* C++ member function. */
16102 dwarf2_add_member_fn (fi, child_die, type, cu);
16103 }
16104 }
16105 else if (child_die->tag == DW_TAG_inheritance)
16106 {
16107 /* C++ base class field. */
16108 dwarf2_add_field (fi, child_die, cu);
16109 }
16110 else if (type_can_define_types (child_die))
16111 dwarf2_add_type_defn (fi, child_die, cu);
16112 else if (child_die->tag == DW_TAG_template_type_param
16113 || child_die->tag == DW_TAG_template_value_param)
16114 {
16115 struct symbol *arg = new_symbol (child_die, NULL, cu);
16116
16117 if (arg != NULL)
16118 template_args->push_back (arg);
16119 }
16120 else if (child_die->tag == DW_TAG_variant)
16121 {
16122 /* In a variant we want to get the discriminant and also add a
16123 field for our sole member child. */
16124 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
16125
16126 for (die_info *variant_child = child_die->child;
16127 variant_child != NULL;
16128 variant_child = sibling_die (variant_child))
16129 {
16130 if (variant_child->tag == DW_TAG_member)
16131 {
16132 handle_struct_member_die (variant_child, type, fi,
16133 template_args, cu);
16134 /* Only handle the one. */
16135 break;
16136 }
16137 }
16138
16139 /* We don't handle this but we might as well report it if we see
16140 it. */
16141 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
16142 complaint (_("DW_AT_discr_list is not supported yet"
16143 " - DIE at %s [in module %s]"),
16144 sect_offset_str (child_die->sect_off),
16145 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16146
16147 /* The first field was just added, so we can stash the
16148 discriminant there. */
16149 gdb_assert (!fi->fields.empty ());
16150 if (discr == NULL)
16151 fi->fields.back ().variant.default_branch = true;
16152 else
16153 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
16154 }
16155 }
16156
16157 /* Finish creating a structure or union type, including filling in
16158 its members and creating a symbol for it. */
16159
16160 static void
16161 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
16162 {
16163 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16164 struct die_info *child_die;
16165 struct type *type;
16166
16167 type = get_die_type (die, cu);
16168 if (type == NULL)
16169 type = read_structure_type (die, cu);
16170
16171 /* When reading a DW_TAG_variant_part, we need to notice when we
16172 read the discriminant member, so we can record it later in the
16173 discriminant_info. */
16174 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
16175 sect_offset discr_offset {};
16176 bool has_template_parameters = false;
16177
16178 if (is_variant_part)
16179 {
16180 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
16181 if (discr == NULL)
16182 {
16183 /* Maybe it's a univariant form, an extension we support.
16184 In this case arrange not to check the offset. */
16185 is_variant_part = false;
16186 }
16187 else if (attr_form_is_ref (discr))
16188 {
16189 struct dwarf2_cu *target_cu = cu;
16190 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
16191
16192 discr_offset = target_die->sect_off;
16193 }
16194 else
16195 {
16196 complaint (_("DW_AT_discr does not have DIE reference form"
16197 " - DIE at %s [in module %s]"),
16198 sect_offset_str (die->sect_off),
16199 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16200 is_variant_part = false;
16201 }
16202 }
16203
16204 if (die->child != NULL && ! die_is_declaration (die, cu))
16205 {
16206 struct field_info fi;
16207 std::vector<struct symbol *> template_args;
16208
16209 child_die = die->child;
16210
16211 while (child_die && child_die->tag)
16212 {
16213 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
16214
16215 if (is_variant_part && discr_offset == child_die->sect_off)
16216 fi.fields.back ().variant.is_discriminant = true;
16217
16218 child_die = sibling_die (child_die);
16219 }
16220
16221 /* Attach template arguments to type. */
16222 if (!template_args.empty ())
16223 {
16224 has_template_parameters = true;
16225 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16226 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
16227 TYPE_TEMPLATE_ARGUMENTS (type)
16228 = XOBNEWVEC (&objfile->objfile_obstack,
16229 struct symbol *,
16230 TYPE_N_TEMPLATE_ARGUMENTS (type));
16231 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
16232 template_args.data (),
16233 (TYPE_N_TEMPLATE_ARGUMENTS (type)
16234 * sizeof (struct symbol *)));
16235 }
16236
16237 /* Attach fields and member functions to the type. */
16238 if (fi.nfields)
16239 dwarf2_attach_fields_to_type (&fi, type, cu);
16240 if (!fi.fnfieldlists.empty ())
16241 {
16242 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
16243
16244 /* Get the type which refers to the base class (possibly this
16245 class itself) which contains the vtable pointer for the current
16246 class from the DW_AT_containing_type attribute. This use of
16247 DW_AT_containing_type is a GNU extension. */
16248
16249 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
16250 {
16251 struct type *t = die_containing_type (die, cu);
16252
16253 set_type_vptr_basetype (type, t);
16254 if (type == t)
16255 {
16256 int i;
16257
16258 /* Our own class provides vtbl ptr. */
16259 for (i = TYPE_NFIELDS (t) - 1;
16260 i >= TYPE_N_BASECLASSES (t);
16261 --i)
16262 {
16263 const char *fieldname = TYPE_FIELD_NAME (t, i);
16264
16265 if (is_vtable_name (fieldname, cu))
16266 {
16267 set_type_vptr_fieldno (type, i);
16268 break;
16269 }
16270 }
16271
16272 /* Complain if virtual function table field not found. */
16273 if (i < TYPE_N_BASECLASSES (t))
16274 complaint (_("virtual function table pointer "
16275 "not found when defining class '%s'"),
16276 TYPE_NAME (type) ? TYPE_NAME (type) : "");
16277 }
16278 else
16279 {
16280 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
16281 }
16282 }
16283 else if (cu->producer
16284 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
16285 {
16286 /* The IBM XLC compiler does not provide direct indication
16287 of the containing type, but the vtable pointer is
16288 always named __vfp. */
16289
16290 int i;
16291
16292 for (i = TYPE_NFIELDS (type) - 1;
16293 i >= TYPE_N_BASECLASSES (type);
16294 --i)
16295 {
16296 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16297 {
16298 set_type_vptr_fieldno (type, i);
16299 set_type_vptr_basetype (type, type);
16300 break;
16301 }
16302 }
16303 }
16304 }
16305
16306 /* Copy fi.typedef_field_list linked list elements content into the
16307 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
16308 if (!fi.typedef_field_list.empty ())
16309 {
16310 int count = fi.typedef_field_list.size ();
16311
16312 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16313 TYPE_TYPEDEF_FIELD_ARRAY (type)
16314 = ((struct decl_field *)
16315 TYPE_ALLOC (type,
16316 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
16317 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
16318
16319 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
16320 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
16321 }
16322
16323 /* Copy fi.nested_types_list linked list elements content into the
16324 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
16325 if (!fi.nested_types_list.empty () && cu->language != language_ada)
16326 {
16327 int count = fi.nested_types_list.size ();
16328
16329 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16330 TYPE_NESTED_TYPES_ARRAY (type)
16331 = ((struct decl_field *)
16332 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
16333 TYPE_NESTED_TYPES_COUNT (type) = count;
16334
16335 for (int i = 0; i < fi.nested_types_list.size (); ++i)
16336 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
16337 }
16338 }
16339
16340 quirk_gcc_member_function_pointer (type, objfile);
16341 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
16342 cu->rust_unions.push_back (type);
16343
16344 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16345 snapshots) has been known to create a die giving a declaration
16346 for a class that has, as a child, a die giving a definition for a
16347 nested class. So we have to process our children even if the
16348 current die is a declaration. Normally, of course, a declaration
16349 won't have any children at all. */
16350
16351 child_die = die->child;
16352
16353 while (child_die != NULL && child_die->tag)
16354 {
16355 if (child_die->tag == DW_TAG_member
16356 || child_die->tag == DW_TAG_variable
16357 || child_die->tag == DW_TAG_inheritance
16358 || child_die->tag == DW_TAG_template_value_param
16359 || child_die->tag == DW_TAG_template_type_param)
16360 {
16361 /* Do nothing. */
16362 }
16363 else
16364 process_die (child_die, cu);
16365
16366 child_die = sibling_die (child_die);
16367 }
16368
16369 /* Do not consider external references. According to the DWARF standard,
16370 these DIEs are identified by the fact that they have no byte_size
16371 attribute, and a declaration attribute. */
16372 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16373 || !die_is_declaration (die, cu))
16374 {
16375 struct symbol *sym = new_symbol (die, type, cu);
16376
16377 if (has_template_parameters)
16378 {
16379 struct symtab *symtab;
16380 if (sym != nullptr)
16381 symtab = symbol_symtab (sym);
16382 else if (cu->line_header != nullptr)
16383 {
16384 /* Any related symtab will do. */
16385 symtab
16386 = cu->line_header->file_names ()[0].symtab;
16387 }
16388 else
16389 {
16390 symtab = nullptr;
16391 complaint (_("could not find suitable "
16392 "symtab for template parameter"
16393 " - DIE at %s [in module %s]"),
16394 sect_offset_str (die->sect_off),
16395 objfile_name (objfile));
16396 }
16397
16398 if (symtab != nullptr)
16399 {
16400 /* Make sure that the symtab is set on the new symbols.
16401 Even though they don't appear in this symtab directly,
16402 other parts of gdb assume that symbols do, and this is
16403 reasonably true. */
16404 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
16405 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
16406 }
16407 }
16408 }
16409 }
16410
16411 /* Assuming DIE is an enumeration type, and TYPE is its associated type,
16412 update TYPE using some information only available in DIE's children. */
16413
16414 static void
16415 update_enumeration_type_from_children (struct die_info *die,
16416 struct type *type,
16417 struct dwarf2_cu *cu)
16418 {
16419 struct die_info *child_die;
16420 int unsigned_enum = 1;
16421 int flag_enum = 1;
16422 ULONGEST mask = 0;
16423
16424 auto_obstack obstack;
16425
16426 for (child_die = die->child;
16427 child_die != NULL && child_die->tag;
16428 child_die = sibling_die (child_die))
16429 {
16430 struct attribute *attr;
16431 LONGEST value;
16432 const gdb_byte *bytes;
16433 struct dwarf2_locexpr_baton *baton;
16434 const char *name;
16435
16436 if (child_die->tag != DW_TAG_enumerator)
16437 continue;
16438
16439 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16440 if (attr == NULL)
16441 continue;
16442
16443 name = dwarf2_name (child_die, cu);
16444 if (name == NULL)
16445 name = "<anonymous enumerator>";
16446
16447 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16448 &value, &bytes, &baton);
16449 if (value < 0)
16450 {
16451 unsigned_enum = 0;
16452 flag_enum = 0;
16453 }
16454 else if ((mask & value) != 0)
16455 flag_enum = 0;
16456 else
16457 mask |= value;
16458
16459 /* If we already know that the enum type is neither unsigned, nor
16460 a flag type, no need to look at the rest of the enumerates. */
16461 if (!unsigned_enum && !flag_enum)
16462 break;
16463 }
16464
16465 if (unsigned_enum)
16466 TYPE_UNSIGNED (type) = 1;
16467 if (flag_enum)
16468 TYPE_FLAG_ENUM (type) = 1;
16469 }
16470
16471 /* Given a DW_AT_enumeration_type die, set its type. We do not
16472 complete the type's fields yet, or create any symbols. */
16473
16474 static struct type *
16475 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
16476 {
16477 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16478 struct type *type;
16479 struct attribute *attr;
16480 const char *name;
16481
16482 /* If the definition of this type lives in .debug_types, read that type.
16483 Don't follow DW_AT_specification though, that will take us back up
16484 the chain and we want to go down. */
16485 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
16486 if (attr != nullptr)
16487 {
16488 type = get_DW_AT_signature_type (die, attr, cu);
16489
16490 /* The type's CU may not be the same as CU.
16491 Ensure TYPE is recorded with CU in die_type_hash. */
16492 return set_die_type (die, type, cu);
16493 }
16494
16495 type = alloc_type (objfile);
16496
16497 TYPE_CODE (type) = TYPE_CODE_ENUM;
16498 name = dwarf2_full_name (NULL, die, cu);
16499 if (name != NULL)
16500 TYPE_NAME (type) = name;
16501
16502 attr = dwarf2_attr (die, DW_AT_type, cu);
16503 if (attr != NULL)
16504 {
16505 struct type *underlying_type = die_type (die, cu);
16506
16507 TYPE_TARGET_TYPE (type) = underlying_type;
16508 }
16509
16510 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16511 if (attr != nullptr)
16512 {
16513 TYPE_LENGTH (type) = DW_UNSND (attr);
16514 }
16515 else
16516 {
16517 TYPE_LENGTH (type) = 0;
16518 }
16519
16520 maybe_set_alignment (cu, die, type);
16521
16522 /* The enumeration DIE can be incomplete. In Ada, any type can be
16523 declared as private in the package spec, and then defined only
16524 inside the package body. Such types are known as Taft Amendment
16525 Types. When another package uses such a type, an incomplete DIE
16526 may be generated by the compiler. */
16527 if (die_is_declaration (die, cu))
16528 TYPE_STUB (type) = 1;
16529
16530 /* Finish the creation of this type by using the enum's children.
16531 We must call this even when the underlying type has been provided
16532 so that we can determine if we're looking at a "flag" enum. */
16533 update_enumeration_type_from_children (die, type, cu);
16534
16535 /* If this type has an underlying type that is not a stub, then we
16536 may use its attributes. We always use the "unsigned" attribute
16537 in this situation, because ordinarily we guess whether the type
16538 is unsigned -- but the guess can be wrong and the underlying type
16539 can tell us the reality. However, we defer to a local size
16540 attribute if one exists, because this lets the compiler override
16541 the underlying type if needed. */
16542 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16543 {
16544 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
16545 if (TYPE_LENGTH (type) == 0)
16546 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
16547 if (TYPE_RAW_ALIGN (type) == 0
16548 && TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)) != 0)
16549 set_type_align (type, TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)));
16550 }
16551
16552 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16553
16554 return set_die_type (die, type, cu);
16555 }
16556
16557 /* Given a pointer to a die which begins an enumeration, process all
16558 the dies that define the members of the enumeration, and create the
16559 symbol for the enumeration type.
16560
16561 NOTE: We reverse the order of the element list. */
16562
16563 static void
16564 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16565 {
16566 struct type *this_type;
16567
16568 this_type = get_die_type (die, cu);
16569 if (this_type == NULL)
16570 this_type = read_enumeration_type (die, cu);
16571
16572 if (die->child != NULL)
16573 {
16574 struct die_info *child_die;
16575 struct symbol *sym;
16576 std::vector<struct field> fields;
16577 const char *name;
16578
16579 child_die = die->child;
16580 while (child_die && child_die->tag)
16581 {
16582 if (child_die->tag != DW_TAG_enumerator)
16583 {
16584 process_die (child_die, cu);
16585 }
16586 else
16587 {
16588 name = dwarf2_name (child_die, cu);
16589 if (name)
16590 {
16591 sym = new_symbol (child_die, this_type, cu);
16592
16593 fields.emplace_back ();
16594 struct field &field = fields.back ();
16595
16596 FIELD_NAME (field) = sym->linkage_name ();
16597 FIELD_TYPE (field) = NULL;
16598 SET_FIELD_ENUMVAL (field, SYMBOL_VALUE (sym));
16599 FIELD_BITSIZE (field) = 0;
16600 }
16601 }
16602
16603 child_die = sibling_die (child_die);
16604 }
16605
16606 if (!fields.empty ())
16607 {
16608 TYPE_NFIELDS (this_type) = fields.size ();
16609 TYPE_FIELDS (this_type) = (struct field *)
16610 TYPE_ALLOC (this_type, sizeof (struct field) * fields.size ());
16611 memcpy (TYPE_FIELDS (this_type), fields.data (),
16612 sizeof (struct field) * fields.size ());
16613 }
16614 }
16615
16616 /* If we are reading an enum from a .debug_types unit, and the enum
16617 is a declaration, and the enum is not the signatured type in the
16618 unit, then we do not want to add a symbol for it. Adding a
16619 symbol would in some cases obscure the true definition of the
16620 enum, giving users an incomplete type when the definition is
16621 actually available. Note that we do not want to do this for all
16622 enums which are just declarations, because C++0x allows forward
16623 enum declarations. */
16624 if (cu->per_cu->is_debug_types
16625 && die_is_declaration (die, cu))
16626 {
16627 struct signatured_type *sig_type;
16628
16629 sig_type = (struct signatured_type *) cu->per_cu;
16630 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16631 if (sig_type->type_offset_in_section != die->sect_off)
16632 return;
16633 }
16634
16635 new_symbol (die, this_type, cu);
16636 }
16637
16638 /* Extract all information from a DW_TAG_array_type DIE and put it in
16639 the DIE's type field. For now, this only handles one dimensional
16640 arrays. */
16641
16642 static struct type *
16643 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
16644 {
16645 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16646 struct die_info *child_die;
16647 struct type *type;
16648 struct type *element_type, *range_type, *index_type;
16649 struct attribute *attr;
16650 const char *name;
16651 struct dynamic_prop *byte_stride_prop = NULL;
16652 unsigned int bit_stride = 0;
16653
16654 element_type = die_type (die, cu);
16655
16656 /* The die_type call above may have already set the type for this DIE. */
16657 type = get_die_type (die, cu);
16658 if (type)
16659 return type;
16660
16661 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16662 if (attr != NULL)
16663 {
16664 int stride_ok;
16665 struct type *prop_type
16666 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
16667
16668 byte_stride_prop
16669 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16670 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16671 prop_type);
16672 if (!stride_ok)
16673 {
16674 complaint (_("unable to read array DW_AT_byte_stride "
16675 " - DIE at %s [in module %s]"),
16676 sect_offset_str (die->sect_off),
16677 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16678 /* Ignore this attribute. We will likely not be able to print
16679 arrays of this type correctly, but there is little we can do
16680 to help if we cannot read the attribute's value. */
16681 byte_stride_prop = NULL;
16682 }
16683 }
16684
16685 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16686 if (attr != NULL)
16687 bit_stride = DW_UNSND (attr);
16688
16689 /* Irix 6.2 native cc creates array types without children for
16690 arrays with unspecified length. */
16691 if (die->child == NULL)
16692 {
16693 index_type = objfile_type (objfile)->builtin_int;
16694 range_type = create_static_range_type (NULL, index_type, 0, -1);
16695 type = create_array_type_with_stride (NULL, element_type, range_type,
16696 byte_stride_prop, bit_stride);
16697 return set_die_type (die, type, cu);
16698 }
16699
16700 std::vector<struct type *> range_types;
16701 child_die = die->child;
16702 while (child_die && child_die->tag)
16703 {
16704 if (child_die->tag == DW_TAG_subrange_type)
16705 {
16706 struct type *child_type = read_type_die (child_die, cu);
16707
16708 if (child_type != NULL)
16709 {
16710 /* The range type was succesfully read. Save it for the
16711 array type creation. */
16712 range_types.push_back (child_type);
16713 }
16714 }
16715 child_die = sibling_die (child_die);
16716 }
16717
16718 /* Dwarf2 dimensions are output from left to right, create the
16719 necessary array types in backwards order. */
16720
16721 type = element_type;
16722
16723 if (read_array_order (die, cu) == DW_ORD_col_major)
16724 {
16725 int i = 0;
16726
16727 while (i < range_types.size ())
16728 type = create_array_type_with_stride (NULL, type, range_types[i++],
16729 byte_stride_prop, bit_stride);
16730 }
16731 else
16732 {
16733 size_t ndim = range_types.size ();
16734 while (ndim-- > 0)
16735 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16736 byte_stride_prop, bit_stride);
16737 }
16738
16739 /* Understand Dwarf2 support for vector types (like they occur on
16740 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16741 array type. This is not part of the Dwarf2/3 standard yet, but a
16742 custom vendor extension. The main difference between a regular
16743 array and the vector variant is that vectors are passed by value
16744 to functions. */
16745 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
16746 if (attr != nullptr)
16747 make_vector_type (type);
16748
16749 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16750 implementation may choose to implement triple vectors using this
16751 attribute. */
16752 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16753 if (attr != nullptr)
16754 {
16755 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16756 TYPE_LENGTH (type) = DW_UNSND (attr);
16757 else
16758 complaint (_("DW_AT_byte_size for array type smaller "
16759 "than the total size of elements"));
16760 }
16761
16762 name = dwarf2_name (die, cu);
16763 if (name)
16764 TYPE_NAME (type) = name;
16765
16766 maybe_set_alignment (cu, die, type);
16767
16768 /* Install the type in the die. */
16769 set_die_type (die, type, cu);
16770
16771 /* set_die_type should be already done. */
16772 set_descriptive_type (type, die, cu);
16773
16774 return type;
16775 }
16776
16777 static enum dwarf_array_dim_ordering
16778 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
16779 {
16780 struct attribute *attr;
16781
16782 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16783
16784 if (attr != nullptr)
16785 return (enum dwarf_array_dim_ordering) DW_SND (attr);
16786
16787 /* GNU F77 is a special case, as at 08/2004 array type info is the
16788 opposite order to the dwarf2 specification, but data is still
16789 laid out as per normal fortran.
16790
16791 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16792 version checking. */
16793
16794 if (cu->language == language_fortran
16795 && cu->producer && strstr (cu->producer, "GNU F77"))
16796 {
16797 return DW_ORD_row_major;
16798 }
16799
16800 switch (cu->language_defn->la_array_ordering)
16801 {
16802 case array_column_major:
16803 return DW_ORD_col_major;
16804 case array_row_major:
16805 default:
16806 return DW_ORD_row_major;
16807 };
16808 }
16809
16810 /* Extract all information from a DW_TAG_set_type DIE and put it in
16811 the DIE's type field. */
16812
16813 static struct type *
16814 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16815 {
16816 struct type *domain_type, *set_type;
16817 struct attribute *attr;
16818
16819 domain_type = die_type (die, cu);
16820
16821 /* The die_type call above may have already set the type for this DIE. */
16822 set_type = get_die_type (die, cu);
16823 if (set_type)
16824 return set_type;
16825
16826 set_type = create_set_type (NULL, domain_type);
16827
16828 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16829 if (attr != nullptr)
16830 TYPE_LENGTH (set_type) = DW_UNSND (attr);
16831
16832 maybe_set_alignment (cu, die, set_type);
16833
16834 return set_die_type (die, set_type, cu);
16835 }
16836
16837 /* A helper for read_common_block that creates a locexpr baton.
16838 SYM is the symbol which we are marking as computed.
16839 COMMON_DIE is the DIE for the common block.
16840 COMMON_LOC is the location expression attribute for the common
16841 block itself.
16842 MEMBER_LOC is the location expression attribute for the particular
16843 member of the common block that we are processing.
16844 CU is the CU from which the above come. */
16845
16846 static void
16847 mark_common_block_symbol_computed (struct symbol *sym,
16848 struct die_info *common_die,
16849 struct attribute *common_loc,
16850 struct attribute *member_loc,
16851 struct dwarf2_cu *cu)
16852 {
16853 struct dwarf2_per_objfile *dwarf2_per_objfile
16854 = cu->per_cu->dwarf2_per_objfile;
16855 struct objfile *objfile = dwarf2_per_objfile->objfile;
16856 struct dwarf2_locexpr_baton *baton;
16857 gdb_byte *ptr;
16858 unsigned int cu_off;
16859 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
16860 LONGEST offset = 0;
16861
16862 gdb_assert (common_loc && member_loc);
16863 gdb_assert (attr_form_is_block (common_loc));
16864 gdb_assert (attr_form_is_block (member_loc)
16865 || attr_form_is_constant (member_loc));
16866
16867 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
16868 baton->per_cu = cu->per_cu;
16869 gdb_assert (baton->per_cu);
16870
16871 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16872
16873 if (attr_form_is_constant (member_loc))
16874 {
16875 offset = dwarf2_get_attr_constant_value (member_loc, 0);
16876 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16877 }
16878 else
16879 baton->size += DW_BLOCK (member_loc)->size;
16880
16881 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
16882 baton->data = ptr;
16883
16884 *ptr++ = DW_OP_call4;
16885 cu_off = common_die->sect_off - cu->per_cu->sect_off;
16886 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16887 ptr += 4;
16888
16889 if (attr_form_is_constant (member_loc))
16890 {
16891 *ptr++ = DW_OP_addr;
16892 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16893 ptr += cu->header.addr_size;
16894 }
16895 else
16896 {
16897 /* We have to copy the data here, because DW_OP_call4 will only
16898 use a DW_AT_location attribute. */
16899 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16900 ptr += DW_BLOCK (member_loc)->size;
16901 }
16902
16903 *ptr++ = DW_OP_plus;
16904 gdb_assert (ptr - baton->data == baton->size);
16905
16906 SYMBOL_LOCATION_BATON (sym) = baton;
16907 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
16908 }
16909
16910 /* Create appropriate locally-scoped variables for all the
16911 DW_TAG_common_block entries. Also create a struct common_block
16912 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16913 is used to separate the common blocks name namespace from regular
16914 variable names. */
16915
16916 static void
16917 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
16918 {
16919 struct attribute *attr;
16920
16921 attr = dwarf2_attr (die, DW_AT_location, cu);
16922 if (attr != nullptr)
16923 {
16924 /* Support the .debug_loc offsets. */
16925 if (attr_form_is_block (attr))
16926 {
16927 /* Ok. */
16928 }
16929 else if (attr_form_is_section_offset (attr))
16930 {
16931 dwarf2_complex_location_expr_complaint ();
16932 attr = NULL;
16933 }
16934 else
16935 {
16936 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16937 "common block member");
16938 attr = NULL;
16939 }
16940 }
16941
16942 if (die->child != NULL)
16943 {
16944 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16945 struct die_info *child_die;
16946 size_t n_entries = 0, size;
16947 struct common_block *common_block;
16948 struct symbol *sym;
16949
16950 for (child_die = die->child;
16951 child_die && child_die->tag;
16952 child_die = sibling_die (child_die))
16953 ++n_entries;
16954
16955 size = (sizeof (struct common_block)
16956 + (n_entries - 1) * sizeof (struct symbol *));
16957 common_block
16958 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16959 size);
16960 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16961 common_block->n_entries = 0;
16962
16963 for (child_die = die->child;
16964 child_die && child_die->tag;
16965 child_die = sibling_die (child_die))
16966 {
16967 /* Create the symbol in the DW_TAG_common_block block in the current
16968 symbol scope. */
16969 sym = new_symbol (child_die, NULL, cu);
16970 if (sym != NULL)
16971 {
16972 struct attribute *member_loc;
16973
16974 common_block->contents[common_block->n_entries++] = sym;
16975
16976 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16977 cu);
16978 if (member_loc)
16979 {
16980 /* GDB has handled this for a long time, but it is
16981 not specified by DWARF. It seems to have been
16982 emitted by gfortran at least as recently as:
16983 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16984 complaint (_("Variable in common block has "
16985 "DW_AT_data_member_location "
16986 "- DIE at %s [in module %s]"),
16987 sect_offset_str (child_die->sect_off),
16988 objfile_name (objfile));
16989
16990 if (attr_form_is_section_offset (member_loc))
16991 dwarf2_complex_location_expr_complaint ();
16992 else if (attr_form_is_constant (member_loc)
16993 || attr_form_is_block (member_loc))
16994 {
16995 if (attr != nullptr)
16996 mark_common_block_symbol_computed (sym, die, attr,
16997 member_loc, cu);
16998 }
16999 else
17000 dwarf2_complex_location_expr_complaint ();
17001 }
17002 }
17003 }
17004
17005 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
17006 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
17007 }
17008 }
17009
17010 /* Create a type for a C++ namespace. */
17011
17012 static struct type *
17013 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
17014 {
17015 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17016 const char *previous_prefix, *name;
17017 int is_anonymous;
17018 struct type *type;
17019
17020 /* For extensions, reuse the type of the original namespace. */
17021 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
17022 {
17023 struct die_info *ext_die;
17024 struct dwarf2_cu *ext_cu = cu;
17025
17026 ext_die = dwarf2_extension (die, &ext_cu);
17027 type = read_type_die (ext_die, ext_cu);
17028
17029 /* EXT_CU may not be the same as CU.
17030 Ensure TYPE is recorded with CU in die_type_hash. */
17031 return set_die_type (die, type, cu);
17032 }
17033
17034 name = namespace_name (die, &is_anonymous, cu);
17035
17036 /* Now build the name of the current namespace. */
17037
17038 previous_prefix = determine_prefix (die, cu);
17039 if (previous_prefix[0] != '\0')
17040 name = typename_concat (&objfile->objfile_obstack,
17041 previous_prefix, name, 0, cu);
17042
17043 /* Create the type. */
17044 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
17045
17046 return set_die_type (die, type, cu);
17047 }
17048
17049 /* Read a namespace scope. */
17050
17051 static void
17052 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
17053 {
17054 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17055 int is_anonymous;
17056
17057 /* Add a symbol associated to this if we haven't seen the namespace
17058 before. Also, add a using directive if it's an anonymous
17059 namespace. */
17060
17061 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
17062 {
17063 struct type *type;
17064
17065 type = read_type_die (die, cu);
17066 new_symbol (die, type, cu);
17067
17068 namespace_name (die, &is_anonymous, cu);
17069 if (is_anonymous)
17070 {
17071 const char *previous_prefix = determine_prefix (die, cu);
17072
17073 std::vector<const char *> excludes;
17074 add_using_directive (using_directives (cu),
17075 previous_prefix, TYPE_NAME (type), NULL,
17076 NULL, excludes, 0, &objfile->objfile_obstack);
17077 }
17078 }
17079
17080 if (die->child != NULL)
17081 {
17082 struct die_info *child_die = die->child;
17083
17084 while (child_die && child_die->tag)
17085 {
17086 process_die (child_die, cu);
17087 child_die = sibling_die (child_die);
17088 }
17089 }
17090 }
17091
17092 /* Read a Fortran module as type. This DIE can be only a declaration used for
17093 imported module. Still we need that type as local Fortran "use ... only"
17094 declaration imports depend on the created type in determine_prefix. */
17095
17096 static struct type *
17097 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
17098 {
17099 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17100 const char *module_name;
17101 struct type *type;
17102
17103 module_name = dwarf2_name (die, cu);
17104 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
17105
17106 return set_die_type (die, type, cu);
17107 }
17108
17109 /* Read a Fortran module. */
17110
17111 static void
17112 read_module (struct die_info *die, struct dwarf2_cu *cu)
17113 {
17114 struct die_info *child_die = die->child;
17115 struct type *type;
17116
17117 type = read_type_die (die, cu);
17118 new_symbol (die, type, cu);
17119
17120 while (child_die && child_die->tag)
17121 {
17122 process_die (child_die, cu);
17123 child_die = sibling_die (child_die);
17124 }
17125 }
17126
17127 /* Return the name of the namespace represented by DIE. Set
17128 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
17129 namespace. */
17130
17131 static const char *
17132 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
17133 {
17134 struct die_info *current_die;
17135 const char *name = NULL;
17136
17137 /* Loop through the extensions until we find a name. */
17138
17139 for (current_die = die;
17140 current_die != NULL;
17141 current_die = dwarf2_extension (die, &cu))
17142 {
17143 /* We don't use dwarf2_name here so that we can detect the absence
17144 of a name -> anonymous namespace. */
17145 name = dwarf2_string_attr (die, DW_AT_name, cu);
17146
17147 if (name != NULL)
17148 break;
17149 }
17150
17151 /* Is it an anonymous namespace? */
17152
17153 *is_anonymous = (name == NULL);
17154 if (*is_anonymous)
17155 name = CP_ANONYMOUS_NAMESPACE_STR;
17156
17157 return name;
17158 }
17159
17160 /* Extract all information from a DW_TAG_pointer_type DIE and add to
17161 the user defined type vector. */
17162
17163 static struct type *
17164 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
17165 {
17166 struct gdbarch *gdbarch
17167 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
17168 struct comp_unit_head *cu_header = &cu->header;
17169 struct type *type;
17170 struct attribute *attr_byte_size;
17171 struct attribute *attr_address_class;
17172 int byte_size, addr_class;
17173 struct type *target_type;
17174
17175 target_type = die_type (die, cu);
17176
17177 /* The die_type call above may have already set the type for this DIE. */
17178 type = get_die_type (die, cu);
17179 if (type)
17180 return type;
17181
17182 type = lookup_pointer_type (target_type);
17183
17184 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
17185 if (attr_byte_size)
17186 byte_size = DW_UNSND (attr_byte_size);
17187 else
17188 byte_size = cu_header->addr_size;
17189
17190 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
17191 if (attr_address_class)
17192 addr_class = DW_UNSND (attr_address_class);
17193 else
17194 addr_class = DW_ADDR_none;
17195
17196 ULONGEST alignment = get_alignment (cu, die);
17197
17198 /* If the pointer size, alignment, or address class is different
17199 than the default, create a type variant marked as such and set
17200 the length accordingly. */
17201 if (TYPE_LENGTH (type) != byte_size
17202 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
17203 && alignment != TYPE_RAW_ALIGN (type))
17204 || addr_class != DW_ADDR_none)
17205 {
17206 if (gdbarch_address_class_type_flags_p (gdbarch))
17207 {
17208 int type_flags;
17209
17210 type_flags = gdbarch_address_class_type_flags
17211 (gdbarch, byte_size, addr_class);
17212 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17213 == 0);
17214 type = make_type_with_address_space (type, type_flags);
17215 }
17216 else if (TYPE_LENGTH (type) != byte_size)
17217 {
17218 complaint (_("invalid pointer size %d"), byte_size);
17219 }
17220 else if (TYPE_RAW_ALIGN (type) != alignment)
17221 {
17222 complaint (_("Invalid DW_AT_alignment"
17223 " - DIE at %s [in module %s]"),
17224 sect_offset_str (die->sect_off),
17225 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17226 }
17227 else
17228 {
17229 /* Should we also complain about unhandled address classes? */
17230 }
17231 }
17232
17233 TYPE_LENGTH (type) = byte_size;
17234 set_type_align (type, alignment);
17235 return set_die_type (die, type, cu);
17236 }
17237
17238 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17239 the user defined type vector. */
17240
17241 static struct type *
17242 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
17243 {
17244 struct type *type;
17245 struct type *to_type;
17246 struct type *domain;
17247
17248 to_type = die_type (die, cu);
17249 domain = die_containing_type (die, cu);
17250
17251 /* The calls above may have already set the type for this DIE. */
17252 type = get_die_type (die, cu);
17253 if (type)
17254 return type;
17255
17256 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
17257 type = lookup_methodptr_type (to_type);
17258 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
17259 {
17260 struct type *new_type
17261 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
17262
17263 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
17264 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
17265 TYPE_VARARGS (to_type));
17266 type = lookup_methodptr_type (new_type);
17267 }
17268 else
17269 type = lookup_memberptr_type (to_type, domain);
17270
17271 return set_die_type (die, type, cu);
17272 }
17273
17274 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
17275 the user defined type vector. */
17276
17277 static struct type *
17278 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
17279 enum type_code refcode)
17280 {
17281 struct comp_unit_head *cu_header = &cu->header;
17282 struct type *type, *target_type;
17283 struct attribute *attr;
17284
17285 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17286
17287 target_type = die_type (die, cu);
17288
17289 /* The die_type call above may have already set the type for this DIE. */
17290 type = get_die_type (die, cu);
17291 if (type)
17292 return type;
17293
17294 type = lookup_reference_type (target_type, refcode);
17295 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17296 if (attr != nullptr)
17297 {
17298 TYPE_LENGTH (type) = DW_UNSND (attr);
17299 }
17300 else
17301 {
17302 TYPE_LENGTH (type) = cu_header->addr_size;
17303 }
17304 maybe_set_alignment (cu, die, type);
17305 return set_die_type (die, type, cu);
17306 }
17307
17308 /* Add the given cv-qualifiers to the element type of the array. GCC
17309 outputs DWARF type qualifiers that apply to an array, not the
17310 element type. But GDB relies on the array element type to carry
17311 the cv-qualifiers. This mimics section 6.7.3 of the C99
17312 specification. */
17313
17314 static struct type *
17315 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17316 struct type *base_type, int cnst, int voltl)
17317 {
17318 struct type *el_type, *inner_array;
17319
17320 base_type = copy_type (base_type);
17321 inner_array = base_type;
17322
17323 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
17324 {
17325 TYPE_TARGET_TYPE (inner_array) =
17326 copy_type (TYPE_TARGET_TYPE (inner_array));
17327 inner_array = TYPE_TARGET_TYPE (inner_array);
17328 }
17329
17330 el_type = TYPE_TARGET_TYPE (inner_array);
17331 cnst |= TYPE_CONST (el_type);
17332 voltl |= TYPE_VOLATILE (el_type);
17333 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17334
17335 return set_die_type (die, base_type, cu);
17336 }
17337
17338 static struct type *
17339 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
17340 {
17341 struct type *base_type, *cv_type;
17342
17343 base_type = die_type (die, cu);
17344
17345 /* The die_type call above may have already set the type for this DIE. */
17346 cv_type = get_die_type (die, cu);
17347 if (cv_type)
17348 return cv_type;
17349
17350 /* In case the const qualifier is applied to an array type, the element type
17351 is so qualified, not the array type (section 6.7.3 of C99). */
17352 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17353 return add_array_cv_type (die, cu, base_type, 1, 0);
17354
17355 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17356 return set_die_type (die, cv_type, cu);
17357 }
17358
17359 static struct type *
17360 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
17361 {
17362 struct type *base_type, *cv_type;
17363
17364 base_type = die_type (die, cu);
17365
17366 /* The die_type call above may have already set the type for this DIE. */
17367 cv_type = get_die_type (die, cu);
17368 if (cv_type)
17369 return cv_type;
17370
17371 /* In case the volatile qualifier is applied to an array type, the
17372 element type is so qualified, not the array type (section 6.7.3
17373 of C99). */
17374 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17375 return add_array_cv_type (die, cu, base_type, 0, 1);
17376
17377 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17378 return set_die_type (die, cv_type, cu);
17379 }
17380
17381 /* Handle DW_TAG_restrict_type. */
17382
17383 static struct type *
17384 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17385 {
17386 struct type *base_type, *cv_type;
17387
17388 base_type = die_type (die, cu);
17389
17390 /* The die_type call above may have already set the type for this DIE. */
17391 cv_type = get_die_type (die, cu);
17392 if (cv_type)
17393 return cv_type;
17394
17395 cv_type = make_restrict_type (base_type);
17396 return set_die_type (die, cv_type, cu);
17397 }
17398
17399 /* Handle DW_TAG_atomic_type. */
17400
17401 static struct type *
17402 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17403 {
17404 struct type *base_type, *cv_type;
17405
17406 base_type = die_type (die, cu);
17407
17408 /* The die_type call above may have already set the type for this DIE. */
17409 cv_type = get_die_type (die, cu);
17410 if (cv_type)
17411 return cv_type;
17412
17413 cv_type = make_atomic_type (base_type);
17414 return set_die_type (die, cv_type, cu);
17415 }
17416
17417 /* Extract all information from a DW_TAG_string_type DIE and add to
17418 the user defined type vector. It isn't really a user defined type,
17419 but it behaves like one, with other DIE's using an AT_user_def_type
17420 attribute to reference it. */
17421
17422 static struct type *
17423 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
17424 {
17425 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17426 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17427 struct type *type, *range_type, *index_type, *char_type;
17428 struct attribute *attr;
17429 struct dynamic_prop prop;
17430 bool length_is_constant = true;
17431 LONGEST length;
17432
17433 /* There are a couple of places where bit sizes might be made use of
17434 when parsing a DW_TAG_string_type, however, no producer that we know
17435 of make use of these. Handling bit sizes that are a multiple of the
17436 byte size is easy enough, but what about other bit sizes? Lets deal
17437 with that problem when we have to. Warn about these attributes being
17438 unsupported, then parse the type and ignore them like we always
17439 have. */
17440 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
17441 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
17442 {
17443 static bool warning_printed = false;
17444 if (!warning_printed)
17445 {
17446 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
17447 "currently supported on DW_TAG_string_type."));
17448 warning_printed = true;
17449 }
17450 }
17451
17452 attr = dwarf2_attr (die, DW_AT_string_length, cu);
17453 if (attr != nullptr && !attr_form_is_constant (attr))
17454 {
17455 /* The string length describes the location at which the length of
17456 the string can be found. The size of the length field can be
17457 specified with one of the attributes below. */
17458 struct type *prop_type;
17459 struct attribute *len
17460 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
17461 if (len == nullptr)
17462 len = dwarf2_attr (die, DW_AT_byte_size, cu);
17463 if (len != nullptr && attr_form_is_constant (len))
17464 {
17465 /* Pass 0 as the default as we know this attribute is constant
17466 and the default value will not be returned. */
17467 LONGEST sz = dwarf2_get_attr_constant_value (len, 0);
17468 prop_type = dwarf2_per_cu_int_type (cu->per_cu, sz, true);
17469 }
17470 else
17471 {
17472 /* If the size is not specified then we assume it is the size of
17473 an address on this target. */
17474 prop_type = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, true);
17475 }
17476
17477 /* Convert the attribute into a dynamic property. */
17478 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17479 length = 1;
17480 else
17481 length_is_constant = false;
17482 }
17483 else if (attr != nullptr)
17484 {
17485 /* This DW_AT_string_length just contains the length with no
17486 indirection. There's no need to create a dynamic property in this
17487 case. Pass 0 for the default value as we know it will not be
17488 returned in this case. */
17489 length = dwarf2_get_attr_constant_value (attr, 0);
17490 }
17491 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
17492 {
17493 /* We don't currently support non-constant byte sizes for strings. */
17494 length = dwarf2_get_attr_constant_value (attr, 1);
17495 }
17496 else
17497 {
17498 /* Use 1 as a fallback length if we have nothing else. */
17499 length = 1;
17500 }
17501
17502 index_type = objfile_type (objfile)->builtin_int;
17503 if (length_is_constant)
17504 range_type = create_static_range_type (NULL, index_type, 1, length);
17505 else
17506 {
17507 struct dynamic_prop low_bound;
17508
17509 low_bound.kind = PROP_CONST;
17510 low_bound.data.const_val = 1;
17511 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17512 }
17513 char_type = language_string_char_type (cu->language_defn, gdbarch);
17514 type = create_string_type (NULL, char_type, range_type);
17515
17516 return set_die_type (die, type, cu);
17517 }
17518
17519 /* Assuming that DIE corresponds to a function, returns nonzero
17520 if the function is prototyped. */
17521
17522 static int
17523 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17524 {
17525 struct attribute *attr;
17526
17527 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17528 if (attr && (DW_UNSND (attr) != 0))
17529 return 1;
17530
17531 /* The DWARF standard implies that the DW_AT_prototyped attribute
17532 is only meaningful for C, but the concept also extends to other
17533 languages that allow unprototyped functions (Eg: Objective C).
17534 For all other languages, assume that functions are always
17535 prototyped. */
17536 if (cu->language != language_c
17537 && cu->language != language_objc
17538 && cu->language != language_opencl)
17539 return 1;
17540
17541 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17542 prototyped and unprototyped functions; default to prototyped,
17543 since that is more common in modern code (and RealView warns
17544 about unprototyped functions). */
17545 if (producer_is_realview (cu->producer))
17546 return 1;
17547
17548 return 0;
17549 }
17550
17551 /* Handle DIES due to C code like:
17552
17553 struct foo
17554 {
17555 int (*funcp)(int a, long l);
17556 int b;
17557 };
17558
17559 ('funcp' generates a DW_TAG_subroutine_type DIE). */
17560
17561 static struct type *
17562 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
17563 {
17564 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17565 struct type *type; /* Type that this function returns. */
17566 struct type *ftype; /* Function that returns above type. */
17567 struct attribute *attr;
17568
17569 type = die_type (die, cu);
17570
17571 /* The die_type call above may have already set the type for this DIE. */
17572 ftype = get_die_type (die, cu);
17573 if (ftype)
17574 return ftype;
17575
17576 ftype = lookup_function_type (type);
17577
17578 if (prototyped_function_p (die, cu))
17579 TYPE_PROTOTYPED (ftype) = 1;
17580
17581 /* Store the calling convention in the type if it's available in
17582 the subroutine die. Otherwise set the calling convention to
17583 the default value DW_CC_normal. */
17584 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
17585 if (attr != nullptr
17586 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
17587 TYPE_CALLING_CONVENTION (ftype)
17588 = (enum dwarf_calling_convention) (DW_UNSND (attr));
17589 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17590 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17591 else
17592 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
17593
17594 /* Record whether the function returns normally to its caller or not
17595 if the DWARF producer set that information. */
17596 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17597 if (attr && (DW_UNSND (attr) != 0))
17598 TYPE_NO_RETURN (ftype) = 1;
17599
17600 /* We need to add the subroutine type to the die immediately so
17601 we don't infinitely recurse when dealing with parameters
17602 declared as the same subroutine type. */
17603 set_die_type (die, ftype, cu);
17604
17605 if (die->child != NULL)
17606 {
17607 struct type *void_type = objfile_type (objfile)->builtin_void;
17608 struct die_info *child_die;
17609 int nparams, iparams;
17610
17611 /* Count the number of parameters.
17612 FIXME: GDB currently ignores vararg functions, but knows about
17613 vararg member functions. */
17614 nparams = 0;
17615 child_die = die->child;
17616 while (child_die && child_die->tag)
17617 {
17618 if (child_die->tag == DW_TAG_formal_parameter)
17619 nparams++;
17620 else if (child_die->tag == DW_TAG_unspecified_parameters)
17621 TYPE_VARARGS (ftype) = 1;
17622 child_die = sibling_die (child_die);
17623 }
17624
17625 /* Allocate storage for parameters and fill them in. */
17626 TYPE_NFIELDS (ftype) = nparams;
17627 TYPE_FIELDS (ftype) = (struct field *)
17628 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
17629
17630 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17631 even if we error out during the parameters reading below. */
17632 for (iparams = 0; iparams < nparams; iparams++)
17633 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17634
17635 iparams = 0;
17636 child_die = die->child;
17637 while (child_die && child_die->tag)
17638 {
17639 if (child_die->tag == DW_TAG_formal_parameter)
17640 {
17641 struct type *arg_type;
17642
17643 /* DWARF version 2 has no clean way to discern C++
17644 static and non-static member functions. G++ helps
17645 GDB by marking the first parameter for non-static
17646 member functions (which is the this pointer) as
17647 artificial. We pass this information to
17648 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17649
17650 DWARF version 3 added DW_AT_object_pointer, which GCC
17651 4.5 does not yet generate. */
17652 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
17653 if (attr != nullptr)
17654 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17655 else
17656 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
17657 arg_type = die_type (child_die, cu);
17658
17659 /* RealView does not mark THIS as const, which the testsuite
17660 expects. GCC marks THIS as const in method definitions,
17661 but not in the class specifications (GCC PR 43053). */
17662 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17663 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17664 {
17665 int is_this = 0;
17666 struct dwarf2_cu *arg_cu = cu;
17667 const char *name = dwarf2_name (child_die, cu);
17668
17669 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17670 if (attr != nullptr)
17671 {
17672 /* If the compiler emits this, use it. */
17673 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17674 is_this = 1;
17675 }
17676 else if (name && strcmp (name, "this") == 0)
17677 /* Function definitions will have the argument names. */
17678 is_this = 1;
17679 else if (name == NULL && iparams == 0)
17680 /* Declarations may not have the names, so like
17681 elsewhere in GDB, assume an artificial first
17682 argument is "this". */
17683 is_this = 1;
17684
17685 if (is_this)
17686 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17687 arg_type, 0);
17688 }
17689
17690 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
17691 iparams++;
17692 }
17693 child_die = sibling_die (child_die);
17694 }
17695 }
17696
17697 return ftype;
17698 }
17699
17700 static struct type *
17701 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
17702 {
17703 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17704 const char *name = NULL;
17705 struct type *this_type, *target_type;
17706
17707 name = dwarf2_full_name (NULL, die, cu);
17708 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17709 TYPE_TARGET_STUB (this_type) = 1;
17710 set_die_type (die, this_type, cu);
17711 target_type = die_type (die, cu);
17712 if (target_type != this_type)
17713 TYPE_TARGET_TYPE (this_type) = target_type;
17714 else
17715 {
17716 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17717 spec and cause infinite loops in GDB. */
17718 complaint (_("Self-referential DW_TAG_typedef "
17719 "- DIE at %s [in module %s]"),
17720 sect_offset_str (die->sect_off), objfile_name (objfile));
17721 TYPE_TARGET_TYPE (this_type) = NULL;
17722 }
17723 return this_type;
17724 }
17725
17726 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17727 (which may be different from NAME) to the architecture back-end to allow
17728 it to guess the correct format if necessary. */
17729
17730 static struct type *
17731 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17732 const char *name_hint, enum bfd_endian byte_order)
17733 {
17734 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17735 const struct floatformat **format;
17736 struct type *type;
17737
17738 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17739 if (format)
17740 type = init_float_type (objfile, bits, name, format, byte_order);
17741 else
17742 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17743
17744 return type;
17745 }
17746
17747 /* Allocate an integer type of size BITS and name NAME. */
17748
17749 static struct type *
17750 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17751 int bits, int unsigned_p, const char *name)
17752 {
17753 struct type *type;
17754
17755 /* Versions of Intel's C Compiler generate an integer type called "void"
17756 instead of using DW_TAG_unspecified_type. This has been seen on
17757 at least versions 14, 17, and 18. */
17758 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17759 && strcmp (name, "void") == 0)
17760 type = objfile_type (objfile)->builtin_void;
17761 else
17762 type = init_integer_type (objfile, bits, unsigned_p, name);
17763
17764 return type;
17765 }
17766
17767 /* Initialise and return a floating point type of size BITS suitable for
17768 use as a component of a complex number. The NAME_HINT is passed through
17769 when initialising the floating point type and is the name of the complex
17770 type.
17771
17772 As DWARF doesn't currently provide an explicit name for the components
17773 of a complex number, but it can be helpful to have these components
17774 named, we try to select a suitable name based on the size of the
17775 component. */
17776 static struct type *
17777 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17778 struct objfile *objfile,
17779 int bits, const char *name_hint,
17780 enum bfd_endian byte_order)
17781 {
17782 gdbarch *gdbarch = get_objfile_arch (objfile);
17783 struct type *tt = nullptr;
17784
17785 /* Try to find a suitable floating point builtin type of size BITS.
17786 We're going to use the name of this type as the name for the complex
17787 target type that we are about to create. */
17788 switch (cu->language)
17789 {
17790 case language_fortran:
17791 switch (bits)
17792 {
17793 case 32:
17794 tt = builtin_f_type (gdbarch)->builtin_real;
17795 break;
17796 case 64:
17797 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17798 break;
17799 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17800 case 128:
17801 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17802 break;
17803 }
17804 break;
17805 default:
17806 switch (bits)
17807 {
17808 case 32:
17809 tt = builtin_type (gdbarch)->builtin_float;
17810 break;
17811 case 64:
17812 tt = builtin_type (gdbarch)->builtin_double;
17813 break;
17814 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17815 case 128:
17816 tt = builtin_type (gdbarch)->builtin_long_double;
17817 break;
17818 }
17819 break;
17820 }
17821
17822 /* If the type we found doesn't match the size we were looking for, then
17823 pretend we didn't find a type at all, the complex target type we
17824 create will then be nameless. */
17825 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
17826 tt = nullptr;
17827
17828 const char *name = (tt == nullptr) ? nullptr : TYPE_NAME (tt);
17829 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
17830 }
17831
17832 /* Find a representation of a given base type and install
17833 it in the TYPE field of the die. */
17834
17835 static struct type *
17836 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
17837 {
17838 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17839 struct type *type;
17840 struct attribute *attr;
17841 int encoding = 0, bits = 0;
17842 const char *name;
17843 gdbarch *arch;
17844
17845 attr = dwarf2_attr (die, DW_AT_encoding, cu);
17846 if (attr != nullptr)
17847 encoding = DW_UNSND (attr);
17848 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17849 if (attr != nullptr)
17850 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
17851 name = dwarf2_name (die, cu);
17852 if (!name)
17853 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
17854
17855 arch = get_objfile_arch (objfile);
17856 enum bfd_endian byte_order = gdbarch_byte_order (arch);
17857
17858 attr = dwarf2_attr (die, DW_AT_endianity, cu);
17859 if (attr)
17860 {
17861 int endianity = DW_UNSND (attr);
17862
17863 switch (endianity)
17864 {
17865 case DW_END_big:
17866 byte_order = BFD_ENDIAN_BIG;
17867 break;
17868 case DW_END_little:
17869 byte_order = BFD_ENDIAN_LITTLE;
17870 break;
17871 default:
17872 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
17873 break;
17874 }
17875 }
17876
17877 switch (encoding)
17878 {
17879 case DW_ATE_address:
17880 /* Turn DW_ATE_address into a void * pointer. */
17881 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
17882 type = init_pointer_type (objfile, bits, name, type);
17883 break;
17884 case DW_ATE_boolean:
17885 type = init_boolean_type (objfile, bits, 1, name);
17886 break;
17887 case DW_ATE_complex_float:
17888 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
17889 byte_order);
17890 type = init_complex_type (objfile, name, type);
17891 break;
17892 case DW_ATE_decimal_float:
17893 type = init_decfloat_type (objfile, bits, name);
17894 break;
17895 case DW_ATE_float:
17896 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
17897 break;
17898 case DW_ATE_signed:
17899 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17900 break;
17901 case DW_ATE_unsigned:
17902 if (cu->language == language_fortran
17903 && name
17904 && startswith (name, "character("))
17905 type = init_character_type (objfile, bits, 1, name);
17906 else
17907 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17908 break;
17909 case DW_ATE_signed_char:
17910 if (cu->language == language_ada || cu->language == language_m2
17911 || cu->language == language_pascal
17912 || cu->language == language_fortran)
17913 type = init_character_type (objfile, bits, 0, name);
17914 else
17915 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17916 break;
17917 case DW_ATE_unsigned_char:
17918 if (cu->language == language_ada || cu->language == language_m2
17919 || cu->language == language_pascal
17920 || cu->language == language_fortran
17921 || cu->language == language_rust)
17922 type = init_character_type (objfile, bits, 1, name);
17923 else
17924 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17925 break;
17926 case DW_ATE_UTF:
17927 {
17928 if (bits == 16)
17929 type = builtin_type (arch)->builtin_char16;
17930 else if (bits == 32)
17931 type = builtin_type (arch)->builtin_char32;
17932 else
17933 {
17934 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
17935 bits);
17936 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17937 }
17938 return set_die_type (die, type, cu);
17939 }
17940 break;
17941
17942 default:
17943 complaint (_("unsupported DW_AT_encoding: '%s'"),
17944 dwarf_type_encoding_name (encoding));
17945 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17946 break;
17947 }
17948
17949 if (name && strcmp (name, "char") == 0)
17950 TYPE_NOSIGN (type) = 1;
17951
17952 maybe_set_alignment (cu, die, type);
17953
17954 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
17955
17956 return set_die_type (die, type, cu);
17957 }
17958
17959 /* Parse dwarf attribute if it's a block, reference or constant and put the
17960 resulting value of the attribute into struct bound_prop.
17961 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17962
17963 static int
17964 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17965 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17966 struct type *default_type)
17967 {
17968 struct dwarf2_property_baton *baton;
17969 struct obstack *obstack
17970 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
17971
17972 gdb_assert (default_type != NULL);
17973
17974 if (attr == NULL || prop == NULL)
17975 return 0;
17976
17977 if (attr_form_is_block (attr))
17978 {
17979 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17980 baton->property_type = default_type;
17981 baton->locexpr.per_cu = cu->per_cu;
17982 baton->locexpr.size = DW_BLOCK (attr)->size;
17983 baton->locexpr.data = DW_BLOCK (attr)->data;
17984 switch (attr->name)
17985 {
17986 case DW_AT_string_length:
17987 baton->locexpr.is_reference = true;
17988 break;
17989 default:
17990 baton->locexpr.is_reference = false;
17991 break;
17992 }
17993 prop->data.baton = baton;
17994 prop->kind = PROP_LOCEXPR;
17995 gdb_assert (prop->data.baton != NULL);
17996 }
17997 else if (attr_form_is_ref (attr))
17998 {
17999 struct dwarf2_cu *target_cu = cu;
18000 struct die_info *target_die;
18001 struct attribute *target_attr;
18002
18003 target_die = follow_die_ref (die, attr, &target_cu);
18004 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
18005 if (target_attr == NULL)
18006 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
18007 target_cu);
18008 if (target_attr == NULL)
18009 return 0;
18010
18011 switch (target_attr->name)
18012 {
18013 case DW_AT_location:
18014 if (attr_form_is_section_offset (target_attr))
18015 {
18016 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18017 baton->property_type = die_type (target_die, target_cu);
18018 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
18019 prop->data.baton = baton;
18020 prop->kind = PROP_LOCLIST;
18021 gdb_assert (prop->data.baton != NULL);
18022 }
18023 else if (attr_form_is_block (target_attr))
18024 {
18025 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18026 baton->property_type = die_type (target_die, target_cu);
18027 baton->locexpr.per_cu = cu->per_cu;
18028 baton->locexpr.size = DW_BLOCK (target_attr)->size;
18029 baton->locexpr.data = DW_BLOCK (target_attr)->data;
18030 baton->locexpr.is_reference = true;
18031 prop->data.baton = baton;
18032 prop->kind = PROP_LOCEXPR;
18033 gdb_assert (prop->data.baton != NULL);
18034 }
18035 else
18036 {
18037 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18038 "dynamic property");
18039 return 0;
18040 }
18041 break;
18042 case DW_AT_data_member_location:
18043 {
18044 LONGEST offset;
18045
18046 if (!handle_data_member_location (target_die, target_cu,
18047 &offset))
18048 return 0;
18049
18050 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18051 baton->property_type = read_type_die (target_die->parent,
18052 target_cu);
18053 baton->offset_info.offset = offset;
18054 baton->offset_info.type = die_type (target_die, target_cu);
18055 prop->data.baton = baton;
18056 prop->kind = PROP_ADDR_OFFSET;
18057 break;
18058 }
18059 }
18060 }
18061 else if (attr_form_is_constant (attr))
18062 {
18063 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
18064 prop->kind = PROP_CONST;
18065 }
18066 else
18067 {
18068 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
18069 dwarf2_name (die, cu));
18070 return 0;
18071 }
18072
18073 return 1;
18074 }
18075
18076 /* Find an integer type SIZE_IN_BYTES bytes in size and return it.
18077 UNSIGNED_P controls if the integer is unsigned or not. */
18078
18079 static struct type *
18080 dwarf2_per_cu_int_type (struct dwarf2_per_cu_data *per_cu,
18081 int size_in_bytes, bool unsigned_p)
18082 {
18083 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
18084 struct type *int_type;
18085
18086 /* Helper macro to examine the various builtin types. */
18087 #define TRY_TYPE(F) \
18088 int_type = (unsigned_p \
18089 ? objfile_type (objfile)->builtin_unsigned_ ## F \
18090 : objfile_type (objfile)->builtin_ ## F); \
18091 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
18092 return int_type
18093
18094 TRY_TYPE (char);
18095 TRY_TYPE (short);
18096 TRY_TYPE (int);
18097 TRY_TYPE (long);
18098 TRY_TYPE (long_long);
18099
18100 #undef TRY_TYPE
18101
18102 gdb_assert_not_reached ("unable to find suitable integer type");
18103 }
18104
18105 /* Find an integer type the same size as the address size given in the
18106 compilation unit header for PER_CU. UNSIGNED_P controls if the integer
18107 is unsigned or not. */
18108
18109 static struct type *
18110 dwarf2_per_cu_addr_sized_int_type (struct dwarf2_per_cu_data *per_cu,
18111 bool unsigned_p)
18112 {
18113 int addr_size = dwarf2_per_cu_addr_size (per_cu);
18114 return dwarf2_per_cu_int_type (per_cu, addr_size, unsigned_p);
18115 }
18116
18117 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
18118 present (which is valid) then compute the default type based on the
18119 compilation units address size. */
18120
18121 static struct type *
18122 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
18123 {
18124 struct type *index_type = die_type (die, cu);
18125
18126 /* Dwarf-2 specifications explicitly allows to create subrange types
18127 without specifying a base type.
18128 In that case, the base type must be set to the type of
18129 the lower bound, upper bound or count, in that order, if any of these
18130 three attributes references an object that has a type.
18131 If no base type is found, the Dwarf-2 specifications say that
18132 a signed integer type of size equal to the size of an address should
18133 be used.
18134 For the following C code: `extern char gdb_int [];'
18135 GCC produces an empty range DIE.
18136 FIXME: muller/2010-05-28: Possible references to object for low bound,
18137 high bound or count are not yet handled by this code. */
18138 if (TYPE_CODE (index_type) == TYPE_CODE_VOID)
18139 index_type = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
18140
18141 return index_type;
18142 }
18143
18144 /* Read the given DW_AT_subrange DIE. */
18145
18146 static struct type *
18147 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
18148 {
18149 struct type *base_type, *orig_base_type;
18150 struct type *range_type;
18151 struct attribute *attr;
18152 struct dynamic_prop low, high;
18153 int low_default_is_valid;
18154 int high_bound_is_count = 0;
18155 const char *name;
18156 ULONGEST negative_mask;
18157
18158 orig_base_type = read_subrange_index_type (die, cu);
18159
18160 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
18161 whereas the real type might be. So, we use ORIG_BASE_TYPE when
18162 creating the range type, but we use the result of check_typedef
18163 when examining properties of the type. */
18164 base_type = check_typedef (orig_base_type);
18165
18166 /* The die_type call above may have already set the type for this DIE. */
18167 range_type = get_die_type (die, cu);
18168 if (range_type)
18169 return range_type;
18170
18171 low.kind = PROP_CONST;
18172 high.kind = PROP_CONST;
18173 high.data.const_val = 0;
18174
18175 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
18176 omitting DW_AT_lower_bound. */
18177 switch (cu->language)
18178 {
18179 case language_c:
18180 case language_cplus:
18181 low.data.const_val = 0;
18182 low_default_is_valid = 1;
18183 break;
18184 case language_fortran:
18185 low.data.const_val = 1;
18186 low_default_is_valid = 1;
18187 break;
18188 case language_d:
18189 case language_objc:
18190 case language_rust:
18191 low.data.const_val = 0;
18192 low_default_is_valid = (cu->header.version >= 4);
18193 break;
18194 case language_ada:
18195 case language_m2:
18196 case language_pascal:
18197 low.data.const_val = 1;
18198 low_default_is_valid = (cu->header.version >= 4);
18199 break;
18200 default:
18201 low.data.const_val = 0;
18202 low_default_is_valid = 0;
18203 break;
18204 }
18205
18206 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
18207 if (attr != nullptr)
18208 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
18209 else if (!low_default_is_valid)
18210 complaint (_("Missing DW_AT_lower_bound "
18211 "- DIE at %s [in module %s]"),
18212 sect_offset_str (die->sect_off),
18213 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
18214
18215 struct attribute *attr_ub, *attr_count;
18216 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
18217 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
18218 {
18219 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
18220 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
18221 {
18222 /* If bounds are constant do the final calculation here. */
18223 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
18224 high.data.const_val = low.data.const_val + high.data.const_val - 1;
18225 else
18226 high_bound_is_count = 1;
18227 }
18228 else
18229 {
18230 if (attr_ub != NULL)
18231 complaint (_("Unresolved DW_AT_upper_bound "
18232 "- DIE at %s [in module %s]"),
18233 sect_offset_str (die->sect_off),
18234 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
18235 if (attr_count != NULL)
18236 complaint (_("Unresolved DW_AT_count "
18237 "- DIE at %s [in module %s]"),
18238 sect_offset_str (die->sect_off),
18239 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
18240 }
18241 }
18242
18243 LONGEST bias = 0;
18244 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
18245 if (bias_attr != nullptr && attr_form_is_constant (bias_attr))
18246 bias = dwarf2_get_attr_constant_value (bias_attr, 0);
18247
18248 /* Normally, the DWARF producers are expected to use a signed
18249 constant form (Eg. DW_FORM_sdata) to express negative bounds.
18250 But this is unfortunately not always the case, as witnessed
18251 with GCC, for instance, where the ambiguous DW_FORM_dataN form
18252 is used instead. To work around that ambiguity, we treat
18253 the bounds as signed, and thus sign-extend their values, when
18254 the base type is signed. */
18255 negative_mask =
18256 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
18257 if (low.kind == PROP_CONST
18258 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
18259 low.data.const_val |= negative_mask;
18260 if (high.kind == PROP_CONST
18261 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
18262 high.data.const_val |= negative_mask;
18263
18264 /* Check for bit and byte strides. */
18265 struct dynamic_prop byte_stride_prop;
18266 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
18267 if (attr_byte_stride != nullptr)
18268 {
18269 struct type *prop_type
18270 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
18271 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
18272 prop_type);
18273 }
18274
18275 struct dynamic_prop bit_stride_prop;
18276 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
18277 if (attr_bit_stride != nullptr)
18278 {
18279 /* It only makes sense to have either a bit or byte stride. */
18280 if (attr_byte_stride != nullptr)
18281 {
18282 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
18283 "- DIE at %s [in module %s]"),
18284 sect_offset_str (die->sect_off),
18285 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
18286 attr_bit_stride = nullptr;
18287 }
18288 else
18289 {
18290 struct type *prop_type
18291 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
18292 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
18293 prop_type);
18294 }
18295 }
18296
18297 if (attr_byte_stride != nullptr
18298 || attr_bit_stride != nullptr)
18299 {
18300 bool byte_stride_p = (attr_byte_stride != nullptr);
18301 struct dynamic_prop *stride
18302 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
18303
18304 range_type
18305 = create_range_type_with_stride (NULL, orig_base_type, &low,
18306 &high, bias, stride, byte_stride_p);
18307 }
18308 else
18309 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
18310
18311 if (high_bound_is_count)
18312 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
18313
18314 /* Ada expects an empty array on no boundary attributes. */
18315 if (attr == NULL && cu->language != language_ada)
18316 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
18317
18318 name = dwarf2_name (die, cu);
18319 if (name)
18320 TYPE_NAME (range_type) = name;
18321
18322 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
18323 if (attr != nullptr)
18324 TYPE_LENGTH (range_type) = DW_UNSND (attr);
18325
18326 maybe_set_alignment (cu, die, range_type);
18327
18328 set_die_type (die, range_type, cu);
18329
18330 /* set_die_type should be already done. */
18331 set_descriptive_type (range_type, die, cu);
18332
18333 return range_type;
18334 }
18335
18336 static struct type *
18337 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
18338 {
18339 struct type *type;
18340
18341 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
18342 NULL);
18343 TYPE_NAME (type) = dwarf2_name (die, cu);
18344
18345 /* In Ada, an unspecified type is typically used when the description
18346 of the type is deferred to a different unit. When encountering
18347 such a type, we treat it as a stub, and try to resolve it later on,
18348 when needed. */
18349 if (cu->language == language_ada)
18350 TYPE_STUB (type) = 1;
18351
18352 return set_die_type (die, type, cu);
18353 }
18354
18355 /* Read a single die and all its descendents. Set the die's sibling
18356 field to NULL; set other fields in the die correctly, and set all
18357 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
18358 location of the info_ptr after reading all of those dies. PARENT
18359 is the parent of the die in question. */
18360
18361 static struct die_info *
18362 read_die_and_children (const struct die_reader_specs *reader,
18363 const gdb_byte *info_ptr,
18364 const gdb_byte **new_info_ptr,
18365 struct die_info *parent)
18366 {
18367 struct die_info *die;
18368 const gdb_byte *cur_ptr;
18369 int has_children;
18370
18371 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
18372 if (die == NULL)
18373 {
18374 *new_info_ptr = cur_ptr;
18375 return NULL;
18376 }
18377 store_in_ref_table (die, reader->cu);
18378
18379 if (has_children)
18380 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
18381 else
18382 {
18383 die->child = NULL;
18384 *new_info_ptr = cur_ptr;
18385 }
18386
18387 die->sibling = NULL;
18388 die->parent = parent;
18389 return die;
18390 }
18391
18392 /* Read a die, all of its descendents, and all of its siblings; set
18393 all of the fields of all of the dies correctly. Arguments are as
18394 in read_die_and_children. */
18395
18396 static struct die_info *
18397 read_die_and_siblings_1 (const struct die_reader_specs *reader,
18398 const gdb_byte *info_ptr,
18399 const gdb_byte **new_info_ptr,
18400 struct die_info *parent)
18401 {
18402 struct die_info *first_die, *last_sibling;
18403 const gdb_byte *cur_ptr;
18404
18405 cur_ptr = info_ptr;
18406 first_die = last_sibling = NULL;
18407
18408 while (1)
18409 {
18410 struct die_info *die
18411 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
18412
18413 if (die == NULL)
18414 {
18415 *new_info_ptr = cur_ptr;
18416 return first_die;
18417 }
18418
18419 if (!first_die)
18420 first_die = die;
18421 else
18422 last_sibling->sibling = die;
18423
18424 last_sibling = die;
18425 }
18426 }
18427
18428 /* Read a die, all of its descendents, and all of its siblings; set
18429 all of the fields of all of the dies correctly. Arguments are as
18430 in read_die_and_children.
18431 This the main entry point for reading a DIE and all its children. */
18432
18433 static struct die_info *
18434 read_die_and_siblings (const struct die_reader_specs *reader,
18435 const gdb_byte *info_ptr,
18436 const gdb_byte **new_info_ptr,
18437 struct die_info *parent)
18438 {
18439 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
18440 new_info_ptr, parent);
18441
18442 if (dwarf_die_debug)
18443 {
18444 fprintf_unfiltered (gdb_stdlog,
18445 "Read die from %s@0x%x of %s:\n",
18446 get_section_name (reader->die_section),
18447 (unsigned) (info_ptr - reader->die_section->buffer),
18448 bfd_get_filename (reader->abfd));
18449 dump_die (die, dwarf_die_debug);
18450 }
18451
18452 return die;
18453 }
18454
18455 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
18456 attributes.
18457 The caller is responsible for filling in the extra attributes
18458 and updating (*DIEP)->num_attrs.
18459 Set DIEP to point to a newly allocated die with its information,
18460 except for its child, sibling, and parent fields.
18461 Set HAS_CHILDREN to tell whether the die has children or not. */
18462
18463 static const gdb_byte *
18464 read_full_die_1 (const struct die_reader_specs *reader,
18465 struct die_info **diep, const gdb_byte *info_ptr,
18466 int *has_children, int num_extra_attrs)
18467 {
18468 unsigned int abbrev_number, bytes_read, i;
18469 struct abbrev_info *abbrev;
18470 struct die_info *die;
18471 struct dwarf2_cu *cu = reader->cu;
18472 bfd *abfd = reader->abfd;
18473
18474 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
18475 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18476 info_ptr += bytes_read;
18477 if (!abbrev_number)
18478 {
18479 *diep = NULL;
18480 *has_children = 0;
18481 return info_ptr;
18482 }
18483
18484 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
18485 if (!abbrev)
18486 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18487 abbrev_number,
18488 bfd_get_filename (abfd));
18489
18490 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
18491 die->sect_off = sect_off;
18492 die->tag = abbrev->tag;
18493 die->abbrev = abbrev_number;
18494
18495 /* Make the result usable.
18496 The caller needs to update num_attrs after adding the extra
18497 attributes. */
18498 die->num_attrs = abbrev->num_attrs;
18499
18500 std::vector<int> indexes_that_need_reprocess;
18501 for (i = 0; i < abbrev->num_attrs; ++i)
18502 {
18503 bool need_reprocess;
18504 info_ptr =
18505 read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18506 info_ptr, &need_reprocess);
18507 if (need_reprocess)
18508 indexes_that_need_reprocess.push_back (i);
18509 }
18510
18511 struct attribute *attr = dwarf2_attr_no_follow (die, DW_AT_str_offsets_base);
18512 if (attr != nullptr)
18513 cu->str_offsets_base = DW_UNSND (attr);
18514
18515 auto maybe_addr_base = lookup_addr_base(die);
18516 if (maybe_addr_base.has_value ())
18517 cu->addr_base = *maybe_addr_base;
18518 for (int index : indexes_that_need_reprocess)
18519 read_attribute_reprocess (reader, &die->attrs[index]);
18520 *diep = die;
18521 *has_children = abbrev->has_children;
18522 return info_ptr;
18523 }
18524
18525 /* Read a die and all its attributes.
18526 Set DIEP to point to a newly allocated die with its information,
18527 except for its child, sibling, and parent fields.
18528 Set HAS_CHILDREN to tell whether the die has children or not. */
18529
18530 static const gdb_byte *
18531 read_full_die (const struct die_reader_specs *reader,
18532 struct die_info **diep, const gdb_byte *info_ptr,
18533 int *has_children)
18534 {
18535 const gdb_byte *result;
18536
18537 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
18538
18539 if (dwarf_die_debug)
18540 {
18541 fprintf_unfiltered (gdb_stdlog,
18542 "Read die from %s@0x%x of %s:\n",
18543 get_section_name (reader->die_section),
18544 (unsigned) (info_ptr - reader->die_section->buffer),
18545 bfd_get_filename (reader->abfd));
18546 dump_die (*diep, dwarf_die_debug);
18547 }
18548
18549 return result;
18550 }
18551 \f
18552 /* Abbreviation tables.
18553
18554 In DWARF version 2, the description of the debugging information is
18555 stored in a separate .debug_abbrev section. Before we read any
18556 dies from a section we read in all abbreviations and install them
18557 in a hash table. */
18558
18559 /* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
18560
18561 struct abbrev_info *
18562 abbrev_table::alloc_abbrev ()
18563 {
18564 struct abbrev_info *abbrev;
18565
18566 abbrev = XOBNEW (&abbrev_obstack, struct abbrev_info);
18567 memset (abbrev, 0, sizeof (struct abbrev_info));
18568
18569 return abbrev;
18570 }
18571
18572 /* Add an abbreviation to the table. */
18573
18574 void
18575 abbrev_table::add_abbrev (unsigned int abbrev_number,
18576 struct abbrev_info *abbrev)
18577 {
18578 unsigned int hash_number;
18579
18580 hash_number = abbrev_number % ABBREV_HASH_SIZE;
18581 abbrev->next = m_abbrevs[hash_number];
18582 m_abbrevs[hash_number] = abbrev;
18583 }
18584
18585 /* Look up an abbrev in the table.
18586 Returns NULL if the abbrev is not found. */
18587
18588 struct abbrev_info *
18589 abbrev_table::lookup_abbrev (unsigned int abbrev_number)
18590 {
18591 unsigned int hash_number;
18592 struct abbrev_info *abbrev;
18593
18594 hash_number = abbrev_number % ABBREV_HASH_SIZE;
18595 abbrev = m_abbrevs[hash_number];
18596
18597 while (abbrev)
18598 {
18599 if (abbrev->number == abbrev_number)
18600 return abbrev;
18601 abbrev = abbrev->next;
18602 }
18603 return NULL;
18604 }
18605
18606 /* Read in an abbrev table. */
18607
18608 static abbrev_table_up
18609 abbrev_table_read_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
18610 struct dwarf2_section_info *section,
18611 sect_offset sect_off)
18612 {
18613 struct objfile *objfile = dwarf2_per_objfile->objfile;
18614 bfd *abfd = get_section_bfd_owner (section);
18615 const gdb_byte *abbrev_ptr;
18616 struct abbrev_info *cur_abbrev;
18617 unsigned int abbrev_number, bytes_read, abbrev_name;
18618 unsigned int abbrev_form;
18619 std::vector<struct attr_abbrev> cur_attrs;
18620
18621 abbrev_table_up abbrev_table (new struct abbrev_table (sect_off));
18622
18623 dwarf2_read_section (objfile, section);
18624 abbrev_ptr = section->buffer + to_underlying (sect_off);
18625 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18626 abbrev_ptr += bytes_read;
18627
18628 /* Loop until we reach an abbrev number of 0. */
18629 while (abbrev_number)
18630 {
18631 cur_attrs.clear ();
18632 cur_abbrev = abbrev_table->alloc_abbrev ();
18633
18634 /* read in abbrev header */
18635 cur_abbrev->number = abbrev_number;
18636 cur_abbrev->tag
18637 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18638 abbrev_ptr += bytes_read;
18639 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
18640 abbrev_ptr += 1;
18641
18642 /* now read in declarations */
18643 for (;;)
18644 {
18645 LONGEST implicit_const;
18646
18647 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18648 abbrev_ptr += bytes_read;
18649 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18650 abbrev_ptr += bytes_read;
18651 if (abbrev_form == DW_FORM_implicit_const)
18652 {
18653 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
18654 &bytes_read);
18655 abbrev_ptr += bytes_read;
18656 }
18657 else
18658 {
18659 /* Initialize it due to a false compiler warning. */
18660 implicit_const = -1;
18661 }
18662
18663 if (abbrev_name == 0)
18664 break;
18665
18666 cur_attrs.emplace_back ();
18667 struct attr_abbrev &cur_attr = cur_attrs.back ();
18668 cur_attr.name = (enum dwarf_attribute) abbrev_name;
18669 cur_attr.form = (enum dwarf_form) abbrev_form;
18670 cur_attr.implicit_const = implicit_const;
18671 ++cur_abbrev->num_attrs;
18672 }
18673
18674 cur_abbrev->attrs =
18675 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
18676 cur_abbrev->num_attrs);
18677 memcpy (cur_abbrev->attrs, cur_attrs.data (),
18678 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
18679
18680 abbrev_table->add_abbrev (abbrev_number, cur_abbrev);
18681
18682 /* Get next abbreviation.
18683 Under Irix6 the abbreviations for a compilation unit are not
18684 always properly terminated with an abbrev number of 0.
18685 Exit loop if we encounter an abbreviation which we have
18686 already read (which means we are about to read the abbreviations
18687 for the next compile unit) or if the end of the abbreviation
18688 table is reached. */
18689 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
18690 break;
18691 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18692 abbrev_ptr += bytes_read;
18693 if (abbrev_table->lookup_abbrev (abbrev_number) != NULL)
18694 break;
18695 }
18696
18697 return abbrev_table;
18698 }
18699
18700 /* Returns nonzero if TAG represents a type that we might generate a partial
18701 symbol for. */
18702
18703 static int
18704 is_type_tag_for_partial (int tag)
18705 {
18706 switch (tag)
18707 {
18708 #if 0
18709 /* Some types that would be reasonable to generate partial symbols for,
18710 that we don't at present. */
18711 case DW_TAG_array_type:
18712 case DW_TAG_file_type:
18713 case DW_TAG_ptr_to_member_type:
18714 case DW_TAG_set_type:
18715 case DW_TAG_string_type:
18716 case DW_TAG_subroutine_type:
18717 #endif
18718 case DW_TAG_base_type:
18719 case DW_TAG_class_type:
18720 case DW_TAG_interface_type:
18721 case DW_TAG_enumeration_type:
18722 case DW_TAG_structure_type:
18723 case DW_TAG_subrange_type:
18724 case DW_TAG_typedef:
18725 case DW_TAG_union_type:
18726 return 1;
18727 default:
18728 return 0;
18729 }
18730 }
18731
18732 /* Load all DIEs that are interesting for partial symbols into memory. */
18733
18734 static struct partial_die_info *
18735 load_partial_dies (const struct die_reader_specs *reader,
18736 const gdb_byte *info_ptr, int building_psymtab)
18737 {
18738 struct dwarf2_cu *cu = reader->cu;
18739 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18740 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
18741 unsigned int bytes_read;
18742 unsigned int load_all = 0;
18743 int nesting_level = 1;
18744
18745 parent_die = NULL;
18746 last_die = NULL;
18747
18748 gdb_assert (cu->per_cu != NULL);
18749 if (cu->per_cu->load_all_dies)
18750 load_all = 1;
18751
18752 cu->partial_dies
18753 = htab_create_alloc_ex (cu->header.length / 12,
18754 partial_die_hash,
18755 partial_die_eq,
18756 NULL,
18757 &cu->comp_unit_obstack,
18758 hashtab_obstack_allocate,
18759 dummy_obstack_deallocate);
18760
18761 while (1)
18762 {
18763 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
18764
18765 /* A NULL abbrev means the end of a series of children. */
18766 if (abbrev == NULL)
18767 {
18768 if (--nesting_level == 0)
18769 return first_die;
18770
18771 info_ptr += bytes_read;
18772 last_die = parent_die;
18773 parent_die = parent_die->die_parent;
18774 continue;
18775 }
18776
18777 /* Check for template arguments. We never save these; if
18778 they're seen, we just mark the parent, and go on our way. */
18779 if (parent_die != NULL
18780 && cu->language == language_cplus
18781 && (abbrev->tag == DW_TAG_template_type_param
18782 || abbrev->tag == DW_TAG_template_value_param))
18783 {
18784 parent_die->has_template_arguments = 1;
18785
18786 if (!load_all)
18787 {
18788 /* We don't need a partial DIE for the template argument. */
18789 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18790 continue;
18791 }
18792 }
18793
18794 /* We only recurse into c++ subprograms looking for template arguments.
18795 Skip their other children. */
18796 if (!load_all
18797 && cu->language == language_cplus
18798 && parent_die != NULL
18799 && parent_die->tag == DW_TAG_subprogram)
18800 {
18801 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18802 continue;
18803 }
18804
18805 /* Check whether this DIE is interesting enough to save. Normally
18806 we would not be interested in members here, but there may be
18807 later variables referencing them via DW_AT_specification (for
18808 static members). */
18809 if (!load_all
18810 && !is_type_tag_for_partial (abbrev->tag)
18811 && abbrev->tag != DW_TAG_constant
18812 && abbrev->tag != DW_TAG_enumerator
18813 && abbrev->tag != DW_TAG_subprogram
18814 && abbrev->tag != DW_TAG_inlined_subroutine
18815 && abbrev->tag != DW_TAG_lexical_block
18816 && abbrev->tag != DW_TAG_variable
18817 && abbrev->tag != DW_TAG_namespace
18818 && abbrev->tag != DW_TAG_module
18819 && abbrev->tag != DW_TAG_member
18820 && abbrev->tag != DW_TAG_imported_unit
18821 && abbrev->tag != DW_TAG_imported_declaration)
18822 {
18823 /* Otherwise we skip to the next sibling, if any. */
18824 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18825 continue;
18826 }
18827
18828 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18829 abbrev);
18830
18831 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
18832
18833 /* This two-pass algorithm for processing partial symbols has a
18834 high cost in cache pressure. Thus, handle some simple cases
18835 here which cover the majority of C partial symbols. DIEs
18836 which neither have specification tags in them, nor could have
18837 specification tags elsewhere pointing at them, can simply be
18838 processed and discarded.
18839
18840 This segment is also optional; scan_partial_symbols and
18841 add_partial_symbol will handle these DIEs if we chain
18842 them in normally. When compilers which do not emit large
18843 quantities of duplicate debug information are more common,
18844 this code can probably be removed. */
18845
18846 /* Any complete simple types at the top level (pretty much all
18847 of them, for a language without namespaces), can be processed
18848 directly. */
18849 if (parent_die == NULL
18850 && pdi.has_specification == 0
18851 && pdi.is_declaration == 0
18852 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18853 || pdi.tag == DW_TAG_base_type
18854 || pdi.tag == DW_TAG_subrange_type))
18855 {
18856 if (building_psymtab && pdi.name != NULL)
18857 add_psymbol_to_list (pdi.name, false,
18858 VAR_DOMAIN, LOC_TYPEDEF, -1,
18859 psymbol_placement::STATIC,
18860 0, cu->language, objfile);
18861 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18862 continue;
18863 }
18864
18865 /* The exception for DW_TAG_typedef with has_children above is
18866 a workaround of GCC PR debug/47510. In the case of this complaint
18867 type_name_or_error will error on such types later.
18868
18869 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18870 it could not find the child DIEs referenced later, this is checked
18871 above. In correct DWARF DW_TAG_typedef should have no children. */
18872
18873 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
18874 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
18875 "- DIE at %s [in module %s]"),
18876 sect_offset_str (pdi.sect_off), objfile_name (objfile));
18877
18878 /* If we're at the second level, and we're an enumerator, and
18879 our parent has no specification (meaning possibly lives in a
18880 namespace elsewhere), then we can add the partial symbol now
18881 instead of queueing it. */
18882 if (pdi.tag == DW_TAG_enumerator
18883 && parent_die != NULL
18884 && parent_die->die_parent == NULL
18885 && parent_die->tag == DW_TAG_enumeration_type
18886 && parent_die->has_specification == 0)
18887 {
18888 if (pdi.name == NULL)
18889 complaint (_("malformed enumerator DIE ignored"));
18890 else if (building_psymtab)
18891 add_psymbol_to_list (pdi.name, false,
18892 VAR_DOMAIN, LOC_CONST, -1,
18893 cu->language == language_cplus
18894 ? psymbol_placement::GLOBAL
18895 : psymbol_placement::STATIC,
18896 0, cu->language, objfile);
18897
18898 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18899 continue;
18900 }
18901
18902 struct partial_die_info *part_die
18903 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
18904
18905 /* We'll save this DIE so link it in. */
18906 part_die->die_parent = parent_die;
18907 part_die->die_sibling = NULL;
18908 part_die->die_child = NULL;
18909
18910 if (last_die && last_die == parent_die)
18911 last_die->die_child = part_die;
18912 else if (last_die)
18913 last_die->die_sibling = part_die;
18914
18915 last_die = part_die;
18916
18917 if (first_die == NULL)
18918 first_die = part_die;
18919
18920 /* Maybe add the DIE to the hash table. Not all DIEs that we
18921 find interesting need to be in the hash table, because we
18922 also have the parent/sibling/child chains; only those that we
18923 might refer to by offset later during partial symbol reading.
18924
18925 For now this means things that might have be the target of a
18926 DW_AT_specification, DW_AT_abstract_origin, or
18927 DW_AT_extension. DW_AT_extension will refer only to
18928 namespaces; DW_AT_abstract_origin refers to functions (and
18929 many things under the function DIE, but we do not recurse
18930 into function DIEs during partial symbol reading) and
18931 possibly variables as well; DW_AT_specification refers to
18932 declarations. Declarations ought to have the DW_AT_declaration
18933 flag. It happens that GCC forgets to put it in sometimes, but
18934 only for functions, not for types.
18935
18936 Adding more things than necessary to the hash table is harmless
18937 except for the performance cost. Adding too few will result in
18938 wasted time in find_partial_die, when we reread the compilation
18939 unit with load_all_dies set. */
18940
18941 if (load_all
18942 || abbrev->tag == DW_TAG_constant
18943 || abbrev->tag == DW_TAG_subprogram
18944 || abbrev->tag == DW_TAG_variable
18945 || abbrev->tag == DW_TAG_namespace
18946 || part_die->is_declaration)
18947 {
18948 void **slot;
18949
18950 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
18951 to_underlying (part_die->sect_off),
18952 INSERT);
18953 *slot = part_die;
18954 }
18955
18956 /* For some DIEs we want to follow their children (if any). For C
18957 we have no reason to follow the children of structures; for other
18958 languages we have to, so that we can get at method physnames
18959 to infer fully qualified class names, for DW_AT_specification,
18960 and for C++ template arguments. For C++, we also look one level
18961 inside functions to find template arguments (if the name of the
18962 function does not already contain the template arguments).
18963
18964 For Ada and Fortran, we need to scan the children of subprograms
18965 and lexical blocks as well because these languages allow the
18966 definition of nested entities that could be interesting for the
18967 debugger, such as nested subprograms for instance. */
18968 if (last_die->has_children
18969 && (load_all
18970 || last_die->tag == DW_TAG_namespace
18971 || last_die->tag == DW_TAG_module
18972 || last_die->tag == DW_TAG_enumeration_type
18973 || (cu->language == language_cplus
18974 && last_die->tag == DW_TAG_subprogram
18975 && (last_die->name == NULL
18976 || strchr (last_die->name, '<') == NULL))
18977 || (cu->language != language_c
18978 && (last_die->tag == DW_TAG_class_type
18979 || last_die->tag == DW_TAG_interface_type
18980 || last_die->tag == DW_TAG_structure_type
18981 || last_die->tag == DW_TAG_union_type))
18982 || ((cu->language == language_ada
18983 || cu->language == language_fortran)
18984 && (last_die->tag == DW_TAG_subprogram
18985 || last_die->tag == DW_TAG_lexical_block))))
18986 {
18987 nesting_level++;
18988 parent_die = last_die;
18989 continue;
18990 }
18991
18992 /* Otherwise we skip to the next sibling, if any. */
18993 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
18994
18995 /* Back to the top, do it again. */
18996 }
18997 }
18998
18999 partial_die_info::partial_die_info (sect_offset sect_off_,
19000 struct abbrev_info *abbrev)
19001 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
19002 {
19003 }
19004
19005 /* Read a minimal amount of information into the minimal die structure.
19006 INFO_PTR should point just after the initial uleb128 of a DIE. */
19007
19008 const gdb_byte *
19009 partial_die_info::read (const struct die_reader_specs *reader,
19010 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
19011 {
19012 struct dwarf2_cu *cu = reader->cu;
19013 struct dwarf2_per_objfile *dwarf2_per_objfile
19014 = cu->per_cu->dwarf2_per_objfile;
19015 unsigned int i;
19016 int has_low_pc_attr = 0;
19017 int has_high_pc_attr = 0;
19018 int high_pc_relative = 0;
19019
19020 std::vector<struct attribute> attr_vec (abbrev.num_attrs);
19021 for (i = 0; i < abbrev.num_attrs; ++i)
19022 {
19023 bool need_reprocess;
19024 info_ptr = read_attribute (reader, &attr_vec[i], &abbrev.attrs[i],
19025 info_ptr, &need_reprocess);
19026 /* String and address offsets that need to do the reprocessing have
19027 already been read at this point, so there is no need to wait until
19028 the loop terminates to do the reprocessing. */
19029 if (need_reprocess)
19030 read_attribute_reprocess (reader, &attr_vec[i]);
19031 attribute &attr = attr_vec[i];
19032 /* Store the data if it is of an attribute we want to keep in a
19033 partial symbol table. */
19034 switch (attr.name)
19035 {
19036 case DW_AT_name:
19037 switch (tag)
19038 {
19039 case DW_TAG_compile_unit:
19040 case DW_TAG_partial_unit:
19041 case DW_TAG_type_unit:
19042 /* Compilation units have a DW_AT_name that is a filename, not
19043 a source language identifier. */
19044 case DW_TAG_enumeration_type:
19045 case DW_TAG_enumerator:
19046 /* These tags always have simple identifiers already; no need
19047 to canonicalize them. */
19048 name = DW_STRING (&attr);
19049 break;
19050 default:
19051 {
19052 struct objfile *objfile = dwarf2_per_objfile->objfile;
19053
19054 name
19055 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
19056 &objfile->per_bfd->storage_obstack);
19057 }
19058 break;
19059 }
19060 break;
19061 case DW_AT_linkage_name:
19062 case DW_AT_MIPS_linkage_name:
19063 /* Note that both forms of linkage name might appear. We
19064 assume they will be the same, and we only store the last
19065 one we see. */
19066 linkage_name = DW_STRING (&attr);
19067 break;
19068 case DW_AT_low_pc:
19069 has_low_pc_attr = 1;
19070 lowpc = attr_value_as_address (&attr);
19071 break;
19072 case DW_AT_high_pc:
19073 has_high_pc_attr = 1;
19074 highpc = attr_value_as_address (&attr);
19075 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
19076 high_pc_relative = 1;
19077 break;
19078 case DW_AT_location:
19079 /* Support the .debug_loc offsets. */
19080 if (attr_form_is_block (&attr))
19081 {
19082 d.locdesc = DW_BLOCK (&attr);
19083 }
19084 else if (attr_form_is_section_offset (&attr))
19085 {
19086 dwarf2_complex_location_expr_complaint ();
19087 }
19088 else
19089 {
19090 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
19091 "partial symbol information");
19092 }
19093 break;
19094 case DW_AT_external:
19095 is_external = DW_UNSND (&attr);
19096 break;
19097 case DW_AT_declaration:
19098 is_declaration = DW_UNSND (&attr);
19099 break;
19100 case DW_AT_type:
19101 has_type = 1;
19102 break;
19103 case DW_AT_abstract_origin:
19104 case DW_AT_specification:
19105 case DW_AT_extension:
19106 has_specification = 1;
19107 spec_offset = dwarf2_get_ref_die_offset (&attr);
19108 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
19109 || cu->per_cu->is_dwz);
19110 break;
19111 case DW_AT_sibling:
19112 /* Ignore absolute siblings, they might point outside of
19113 the current compile unit. */
19114 if (attr.form == DW_FORM_ref_addr)
19115 complaint (_("ignoring absolute DW_AT_sibling"));
19116 else
19117 {
19118 const gdb_byte *buffer = reader->buffer;
19119 sect_offset off = dwarf2_get_ref_die_offset (&attr);
19120 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
19121
19122 if (sibling_ptr < info_ptr)
19123 complaint (_("DW_AT_sibling points backwards"));
19124 else if (sibling_ptr > reader->buffer_end)
19125 dwarf2_section_buffer_overflow_complaint (reader->die_section);
19126 else
19127 sibling = sibling_ptr;
19128 }
19129 break;
19130 case DW_AT_byte_size:
19131 has_byte_size = 1;
19132 break;
19133 case DW_AT_const_value:
19134 has_const_value = 1;
19135 break;
19136 case DW_AT_calling_convention:
19137 /* DWARF doesn't provide a way to identify a program's source-level
19138 entry point. DW_AT_calling_convention attributes are only meant
19139 to describe functions' calling conventions.
19140
19141 However, because it's a necessary piece of information in
19142 Fortran, and before DWARF 4 DW_CC_program was the only
19143 piece of debugging information whose definition refers to
19144 a 'main program' at all, several compilers marked Fortran
19145 main programs with DW_CC_program --- even when those
19146 functions use the standard calling conventions.
19147
19148 Although DWARF now specifies a way to provide this
19149 information, we support this practice for backward
19150 compatibility. */
19151 if (DW_UNSND (&attr) == DW_CC_program
19152 && cu->language == language_fortran)
19153 main_subprogram = 1;
19154 break;
19155 case DW_AT_inline:
19156 if (DW_UNSND (&attr) == DW_INL_inlined
19157 || DW_UNSND (&attr) == DW_INL_declared_inlined)
19158 may_be_inlined = 1;
19159 break;
19160
19161 case DW_AT_import:
19162 if (tag == DW_TAG_imported_unit)
19163 {
19164 d.sect_off = dwarf2_get_ref_die_offset (&attr);
19165 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
19166 || cu->per_cu->is_dwz);
19167 }
19168 break;
19169
19170 case DW_AT_main_subprogram:
19171 main_subprogram = DW_UNSND (&attr);
19172 break;
19173
19174 case DW_AT_ranges:
19175 {
19176 /* It would be nice to reuse dwarf2_get_pc_bounds here,
19177 but that requires a full DIE, so instead we just
19178 reimplement it. */
19179 int need_ranges_base = tag != DW_TAG_compile_unit;
19180 unsigned int ranges_offset = (DW_UNSND (&attr)
19181 + (need_ranges_base
19182 ? cu->ranges_base
19183 : 0));
19184
19185 /* Value of the DW_AT_ranges attribute is the offset in the
19186 .debug_ranges section. */
19187 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
19188 nullptr))
19189 has_pc_info = 1;
19190 }
19191 break;
19192
19193 default:
19194 break;
19195 }
19196 }
19197
19198 /* For Ada, if both the name and the linkage name appear, we prefer
19199 the latter. This lets "catch exception" work better, regardless
19200 of the order in which the name and linkage name were emitted.
19201 Really, though, this is just a workaround for the fact that gdb
19202 doesn't store both the name and the linkage name. */
19203 if (cu->language == language_ada && linkage_name != nullptr)
19204 name = linkage_name;
19205
19206 if (high_pc_relative)
19207 highpc += lowpc;
19208
19209 if (has_low_pc_attr && has_high_pc_attr)
19210 {
19211 /* When using the GNU linker, .gnu.linkonce. sections are used to
19212 eliminate duplicate copies of functions and vtables and such.
19213 The linker will arbitrarily choose one and discard the others.
19214 The AT_*_pc values for such functions refer to local labels in
19215 these sections. If the section from that file was discarded, the
19216 labels are not in the output, so the relocs get a value of 0.
19217 If this is a discarded function, mark the pc bounds as invalid,
19218 so that GDB will ignore it. */
19219 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
19220 {
19221 struct objfile *objfile = dwarf2_per_objfile->objfile;
19222 struct gdbarch *gdbarch = get_objfile_arch (objfile);
19223
19224 complaint (_("DW_AT_low_pc %s is zero "
19225 "for DIE at %s [in module %s]"),
19226 paddress (gdbarch, lowpc),
19227 sect_offset_str (sect_off),
19228 objfile_name (objfile));
19229 }
19230 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
19231 else if (lowpc >= highpc)
19232 {
19233 struct objfile *objfile = dwarf2_per_objfile->objfile;
19234 struct gdbarch *gdbarch = get_objfile_arch (objfile);
19235
19236 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
19237 "for DIE at %s [in module %s]"),
19238 paddress (gdbarch, lowpc),
19239 paddress (gdbarch, highpc),
19240 sect_offset_str (sect_off),
19241 objfile_name (objfile));
19242 }
19243 else
19244 has_pc_info = 1;
19245 }
19246
19247 return info_ptr;
19248 }
19249
19250 /* Find a cached partial DIE at OFFSET in CU. */
19251
19252 struct partial_die_info *
19253 dwarf2_cu::find_partial_die (sect_offset sect_off)
19254 {
19255 struct partial_die_info *lookup_die = NULL;
19256 struct partial_die_info part_die (sect_off);
19257
19258 lookup_die = ((struct partial_die_info *)
19259 htab_find_with_hash (partial_dies, &part_die,
19260 to_underlying (sect_off)));
19261
19262 return lookup_die;
19263 }
19264
19265 /* Find a partial DIE at OFFSET, which may or may not be in CU,
19266 except in the case of .debug_types DIEs which do not reference
19267 outside their CU (they do however referencing other types via
19268 DW_FORM_ref_sig8). */
19269
19270 static const struct cu_partial_die_info
19271 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
19272 {
19273 struct dwarf2_per_objfile *dwarf2_per_objfile
19274 = cu->per_cu->dwarf2_per_objfile;
19275 struct objfile *objfile = dwarf2_per_objfile->objfile;
19276 struct dwarf2_per_cu_data *per_cu = NULL;
19277 struct partial_die_info *pd = NULL;
19278
19279 if (offset_in_dwz == cu->per_cu->is_dwz
19280 && offset_in_cu_p (&cu->header, sect_off))
19281 {
19282 pd = cu->find_partial_die (sect_off);
19283 if (pd != NULL)
19284 return { cu, pd };
19285 /* We missed recording what we needed.
19286 Load all dies and try again. */
19287 per_cu = cu->per_cu;
19288 }
19289 else
19290 {
19291 /* TUs don't reference other CUs/TUs (except via type signatures). */
19292 if (cu->per_cu->is_debug_types)
19293 {
19294 error (_("Dwarf Error: Type Unit at offset %s contains"
19295 " external reference to offset %s [in module %s].\n"),
19296 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
19297 bfd_get_filename (objfile->obfd));
19298 }
19299 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
19300 dwarf2_per_objfile);
19301
19302 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
19303 load_partial_comp_unit (per_cu);
19304
19305 per_cu->cu->last_used = 0;
19306 pd = per_cu->cu->find_partial_die (sect_off);
19307 }
19308
19309 /* If we didn't find it, and not all dies have been loaded,
19310 load them all and try again. */
19311
19312 if (pd == NULL && per_cu->load_all_dies == 0)
19313 {
19314 per_cu->load_all_dies = 1;
19315
19316 /* This is nasty. When we reread the DIEs, somewhere up the call chain
19317 THIS_CU->cu may already be in use. So we can't just free it and
19318 replace its DIEs with the ones we read in. Instead, we leave those
19319 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
19320 and clobber THIS_CU->cu->partial_dies with the hash table for the new
19321 set. */
19322 load_partial_comp_unit (per_cu);
19323
19324 pd = per_cu->cu->find_partial_die (sect_off);
19325 }
19326
19327 if (pd == NULL)
19328 internal_error (__FILE__, __LINE__,
19329 _("could not find partial DIE %s "
19330 "in cache [from module %s]\n"),
19331 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
19332 return { per_cu->cu, pd };
19333 }
19334
19335 /* See if we can figure out if the class lives in a namespace. We do
19336 this by looking for a member function; its demangled name will
19337 contain namespace info, if there is any. */
19338
19339 static void
19340 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
19341 struct dwarf2_cu *cu)
19342 {
19343 /* NOTE: carlton/2003-10-07: Getting the info this way changes
19344 what template types look like, because the demangler
19345 frequently doesn't give the same name as the debug info. We
19346 could fix this by only using the demangled name to get the
19347 prefix (but see comment in read_structure_type). */
19348
19349 struct partial_die_info *real_pdi;
19350 struct partial_die_info *child_pdi;
19351
19352 /* If this DIE (this DIE's specification, if any) has a parent, then
19353 we should not do this. We'll prepend the parent's fully qualified
19354 name when we create the partial symbol. */
19355
19356 real_pdi = struct_pdi;
19357 while (real_pdi->has_specification)
19358 {
19359 auto res = find_partial_die (real_pdi->spec_offset,
19360 real_pdi->spec_is_dwz, cu);
19361 real_pdi = res.pdi;
19362 cu = res.cu;
19363 }
19364
19365 if (real_pdi->die_parent != NULL)
19366 return;
19367
19368 for (child_pdi = struct_pdi->die_child;
19369 child_pdi != NULL;
19370 child_pdi = child_pdi->die_sibling)
19371 {
19372 if (child_pdi->tag == DW_TAG_subprogram
19373 && child_pdi->linkage_name != NULL)
19374 {
19375 gdb::unique_xmalloc_ptr<char> actual_class_name
19376 (language_class_name_from_physname (cu->language_defn,
19377 child_pdi->linkage_name));
19378 if (actual_class_name != NULL)
19379 {
19380 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19381 struct_pdi->name
19382 = obstack_strdup (&objfile->per_bfd->storage_obstack,
19383 actual_class_name.get ());
19384 }
19385 break;
19386 }
19387 }
19388 }
19389
19390 void
19391 partial_die_info::fixup (struct dwarf2_cu *cu)
19392 {
19393 /* Once we've fixed up a die, there's no point in doing so again.
19394 This also avoids a memory leak if we were to call
19395 guess_partial_die_structure_name multiple times. */
19396 if (fixup_called)
19397 return;
19398
19399 /* If we found a reference attribute and the DIE has no name, try
19400 to find a name in the referred to DIE. */
19401
19402 if (name == NULL && has_specification)
19403 {
19404 struct partial_die_info *spec_die;
19405
19406 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
19407 spec_die = res.pdi;
19408 cu = res.cu;
19409
19410 spec_die->fixup (cu);
19411
19412 if (spec_die->name)
19413 {
19414 name = spec_die->name;
19415
19416 /* Copy DW_AT_external attribute if it is set. */
19417 if (spec_die->is_external)
19418 is_external = spec_die->is_external;
19419 }
19420 }
19421
19422 /* Set default names for some unnamed DIEs. */
19423
19424 if (name == NULL && tag == DW_TAG_namespace)
19425 name = CP_ANONYMOUS_NAMESPACE_STR;
19426
19427 /* If there is no parent die to provide a namespace, and there are
19428 children, see if we can determine the namespace from their linkage
19429 name. */
19430 if (cu->language == language_cplus
19431 && !cu->per_cu->dwarf2_per_objfile->types.empty ()
19432 && die_parent == NULL
19433 && has_children
19434 && (tag == DW_TAG_class_type
19435 || tag == DW_TAG_structure_type
19436 || tag == DW_TAG_union_type))
19437 guess_partial_die_structure_name (this, cu);
19438
19439 /* GCC might emit a nameless struct or union that has a linkage
19440 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
19441 if (name == NULL
19442 && (tag == DW_TAG_class_type
19443 || tag == DW_TAG_interface_type
19444 || tag == DW_TAG_structure_type
19445 || tag == DW_TAG_union_type)
19446 && linkage_name != NULL)
19447 {
19448 gdb::unique_xmalloc_ptr<char> demangled
19449 (gdb_demangle (linkage_name, DMGL_TYPES));
19450 if (demangled != nullptr)
19451 {
19452 const char *base;
19453
19454 /* Strip any leading namespaces/classes, keep only the base name.
19455 DW_AT_name for named DIEs does not contain the prefixes. */
19456 base = strrchr (demangled.get (), ':');
19457 if (base && base > demangled.get () && base[-1] == ':')
19458 base++;
19459 else
19460 base = demangled.get ();
19461
19462 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19463 name = obstack_strdup (&objfile->per_bfd->storage_obstack, base);
19464 }
19465 }
19466
19467 fixup_called = 1;
19468 }
19469
19470 /* Process the attributes that had to be skipped in the first round. These
19471 attributes are the ones that need str_offsets_base or addr_base attributes.
19472 They could not have been processed in the first round, because at the time
19473 the values of str_offsets_base or addr_base may not have been known. */
19474 void read_attribute_reprocess (const struct die_reader_specs *reader,
19475 struct attribute *attr)
19476 {
19477 struct dwarf2_cu *cu = reader->cu;
19478 switch (attr->form)
19479 {
19480 case DW_FORM_addrx:
19481 case DW_FORM_GNU_addr_index:
19482 DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
19483 break;
19484 case DW_FORM_strx:
19485 case DW_FORM_strx1:
19486 case DW_FORM_strx2:
19487 case DW_FORM_strx3:
19488 case DW_FORM_strx4:
19489 case DW_FORM_GNU_str_index:
19490 {
19491 unsigned int str_index = DW_UNSND (attr);
19492 if (reader->dwo_file != NULL)
19493 {
19494 DW_STRING (attr) = read_dwo_str_index (reader, str_index);
19495 DW_STRING_IS_CANONICAL (attr) = 0;
19496 }
19497 else
19498 {
19499 DW_STRING (attr) = read_stub_str_index (cu, str_index);
19500 DW_STRING_IS_CANONICAL (attr) = 0;
19501 }
19502 break;
19503 }
19504 default:
19505 gdb_assert_not_reached (_("Unexpected DWARF form."));
19506 }
19507 }
19508
19509 /* Read an attribute value described by an attribute form. */
19510
19511 static const gdb_byte *
19512 read_attribute_value (const struct die_reader_specs *reader,
19513 struct attribute *attr, unsigned form,
19514 LONGEST implicit_const, const gdb_byte *info_ptr,
19515 bool *need_reprocess)
19516 {
19517 struct dwarf2_cu *cu = reader->cu;
19518 struct dwarf2_per_objfile *dwarf2_per_objfile
19519 = cu->per_cu->dwarf2_per_objfile;
19520 struct objfile *objfile = dwarf2_per_objfile->objfile;
19521 struct gdbarch *gdbarch = get_objfile_arch (objfile);
19522 bfd *abfd = reader->abfd;
19523 struct comp_unit_head *cu_header = &cu->header;
19524 unsigned int bytes_read;
19525 struct dwarf_block *blk;
19526 *need_reprocess = false;
19527
19528 attr->form = (enum dwarf_form) form;
19529 switch (form)
19530 {
19531 case DW_FORM_ref_addr:
19532 if (cu->header.version == 2)
19533 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
19534 else
19535 DW_UNSND (attr) = read_offset (abfd, info_ptr,
19536 &cu->header, &bytes_read);
19537 info_ptr += bytes_read;
19538 break;
19539 case DW_FORM_GNU_ref_alt:
19540 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19541 info_ptr += bytes_read;
19542 break;
19543 case DW_FORM_addr:
19544 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
19545 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
19546 info_ptr += bytes_read;
19547 break;
19548 case DW_FORM_block2:
19549 blk = dwarf_alloc_block (cu);
19550 blk->size = read_2_bytes (abfd, info_ptr);
19551 info_ptr += 2;
19552 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19553 info_ptr += blk->size;
19554 DW_BLOCK (attr) = blk;
19555 break;
19556 case DW_FORM_block4:
19557 blk = dwarf_alloc_block (cu);
19558 blk->size = read_4_bytes (abfd, info_ptr);
19559 info_ptr += 4;
19560 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19561 info_ptr += blk->size;
19562 DW_BLOCK (attr) = blk;
19563 break;
19564 case DW_FORM_data2:
19565 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
19566 info_ptr += 2;
19567 break;
19568 case DW_FORM_data4:
19569 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
19570 info_ptr += 4;
19571 break;
19572 case DW_FORM_data8:
19573 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
19574 info_ptr += 8;
19575 break;
19576 case DW_FORM_data16:
19577 blk = dwarf_alloc_block (cu);
19578 blk->size = 16;
19579 blk->data = read_n_bytes (abfd, info_ptr, 16);
19580 info_ptr += 16;
19581 DW_BLOCK (attr) = blk;
19582 break;
19583 case DW_FORM_sec_offset:
19584 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19585 info_ptr += bytes_read;
19586 break;
19587 case DW_FORM_string:
19588 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
19589 DW_STRING_IS_CANONICAL (attr) = 0;
19590 info_ptr += bytes_read;
19591 break;
19592 case DW_FORM_strp:
19593 if (!cu->per_cu->is_dwz)
19594 {
19595 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
19596 abfd, info_ptr, cu_header,
19597 &bytes_read);
19598 DW_STRING_IS_CANONICAL (attr) = 0;
19599 info_ptr += bytes_read;
19600 break;
19601 }
19602 /* FALLTHROUGH */
19603 case DW_FORM_line_strp:
19604 if (!cu->per_cu->is_dwz)
19605 {
19606 DW_STRING (attr) = read_indirect_line_string (dwarf2_per_objfile,
19607 abfd, info_ptr,
19608 cu_header, &bytes_read);
19609 DW_STRING_IS_CANONICAL (attr) = 0;
19610 info_ptr += bytes_read;
19611 break;
19612 }
19613 /* FALLTHROUGH */
19614 case DW_FORM_GNU_strp_alt:
19615 {
19616 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
19617 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
19618 &bytes_read);
19619
19620 DW_STRING (attr) = read_indirect_string_from_dwz (objfile,
19621 dwz, str_offset);
19622 DW_STRING_IS_CANONICAL (attr) = 0;
19623 info_ptr += bytes_read;
19624 }
19625 break;
19626 case DW_FORM_exprloc:
19627 case DW_FORM_block:
19628 blk = dwarf_alloc_block (cu);
19629 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19630 info_ptr += bytes_read;
19631 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19632 info_ptr += blk->size;
19633 DW_BLOCK (attr) = blk;
19634 break;
19635 case DW_FORM_block1:
19636 blk = dwarf_alloc_block (cu);
19637 blk->size = read_1_byte (abfd, info_ptr);
19638 info_ptr += 1;
19639 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19640 info_ptr += blk->size;
19641 DW_BLOCK (attr) = blk;
19642 break;
19643 case DW_FORM_data1:
19644 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19645 info_ptr += 1;
19646 break;
19647 case DW_FORM_flag:
19648 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19649 info_ptr += 1;
19650 break;
19651 case DW_FORM_flag_present:
19652 DW_UNSND (attr) = 1;
19653 break;
19654 case DW_FORM_sdata:
19655 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19656 info_ptr += bytes_read;
19657 break;
19658 case DW_FORM_udata:
19659 case DW_FORM_rnglistx:
19660 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19661 info_ptr += bytes_read;
19662 break;
19663 case DW_FORM_ref1:
19664 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19665 + read_1_byte (abfd, info_ptr));
19666 info_ptr += 1;
19667 break;
19668 case DW_FORM_ref2:
19669 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19670 + read_2_bytes (abfd, info_ptr));
19671 info_ptr += 2;
19672 break;
19673 case DW_FORM_ref4:
19674 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19675 + read_4_bytes (abfd, info_ptr));
19676 info_ptr += 4;
19677 break;
19678 case DW_FORM_ref8:
19679 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19680 + read_8_bytes (abfd, info_ptr));
19681 info_ptr += 8;
19682 break;
19683 case DW_FORM_ref_sig8:
19684 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
19685 info_ptr += 8;
19686 break;
19687 case DW_FORM_ref_udata:
19688 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19689 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
19690 info_ptr += bytes_read;
19691 break;
19692 case DW_FORM_indirect:
19693 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19694 info_ptr += bytes_read;
19695 if (form == DW_FORM_implicit_const)
19696 {
19697 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19698 info_ptr += bytes_read;
19699 }
19700 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19701 info_ptr, need_reprocess);
19702 break;
19703 case DW_FORM_implicit_const:
19704 DW_SND (attr) = implicit_const;
19705 break;
19706 case DW_FORM_addrx:
19707 case DW_FORM_GNU_addr_index:
19708 *need_reprocess = true;
19709 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19710 info_ptr += bytes_read;
19711 break;
19712 case DW_FORM_strx:
19713 case DW_FORM_strx1:
19714 case DW_FORM_strx2:
19715 case DW_FORM_strx3:
19716 case DW_FORM_strx4:
19717 case DW_FORM_GNU_str_index:
19718 {
19719 ULONGEST str_index;
19720 if (form == DW_FORM_strx1)
19721 {
19722 str_index = read_1_byte (abfd, info_ptr);
19723 info_ptr += 1;
19724 }
19725 else if (form == DW_FORM_strx2)
19726 {
19727 str_index = read_2_bytes (abfd, info_ptr);
19728 info_ptr += 2;
19729 }
19730 else if (form == DW_FORM_strx3)
19731 {
19732 str_index = read_3_bytes (abfd, info_ptr);
19733 info_ptr += 3;
19734 }
19735 else if (form == DW_FORM_strx4)
19736 {
19737 str_index = read_4_bytes (abfd, info_ptr);
19738 info_ptr += 4;
19739 }
19740 else
19741 {
19742 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19743 info_ptr += bytes_read;
19744 }
19745 *need_reprocess = true;
19746 DW_UNSND (attr) = str_index;
19747 }
19748 break;
19749 default:
19750 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
19751 dwarf_form_name (form),
19752 bfd_get_filename (abfd));
19753 }
19754
19755 /* Super hack. */
19756 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
19757 attr->form = DW_FORM_GNU_ref_alt;
19758
19759 /* We have seen instances where the compiler tried to emit a byte
19760 size attribute of -1 which ended up being encoded as an unsigned
19761 0xffffffff. Although 0xffffffff is technically a valid size value,
19762 an object of this size seems pretty unlikely so we can relatively
19763 safely treat these cases as if the size attribute was invalid and
19764 treat them as zero by default. */
19765 if (attr->name == DW_AT_byte_size
19766 && form == DW_FORM_data4
19767 && DW_UNSND (attr) >= 0xffffffff)
19768 {
19769 complaint
19770 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19771 hex_string (DW_UNSND (attr)));
19772 DW_UNSND (attr) = 0;
19773 }
19774
19775 return info_ptr;
19776 }
19777
19778 /* Read an attribute described by an abbreviated attribute. */
19779
19780 static const gdb_byte *
19781 read_attribute (const struct die_reader_specs *reader,
19782 struct attribute *attr, struct attr_abbrev *abbrev,
19783 const gdb_byte *info_ptr, bool *need_reprocess)
19784 {
19785 attr->name = abbrev->name;
19786 return read_attribute_value (reader, attr, abbrev->form,
19787 abbrev->implicit_const, info_ptr,
19788 need_reprocess);
19789 }
19790
19791 /* Read dwarf information from a buffer. */
19792
19793 static unsigned int
19794 read_1_byte (bfd *abfd, const gdb_byte *buf)
19795 {
19796 return bfd_get_8 (abfd, buf);
19797 }
19798
19799 static int
19800 read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
19801 {
19802 return bfd_get_signed_8 (abfd, buf);
19803 }
19804
19805 static unsigned int
19806 read_2_bytes (bfd *abfd, const gdb_byte *buf)
19807 {
19808 return bfd_get_16 (abfd, buf);
19809 }
19810
19811 static int
19812 read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
19813 {
19814 return bfd_get_signed_16 (abfd, buf);
19815 }
19816
19817 static unsigned int
19818 read_3_bytes (bfd *abfd, const gdb_byte *buf)
19819 {
19820 unsigned int result = 0;
19821 for (int i = 0; i < 3; ++i)
19822 {
19823 unsigned char byte = bfd_get_8 (abfd, buf);
19824 buf++;
19825 result |= ((unsigned int) byte << (i * 8));
19826 }
19827 return result;
19828 }
19829
19830 static unsigned int
19831 read_4_bytes (bfd *abfd, const gdb_byte *buf)
19832 {
19833 return bfd_get_32 (abfd, buf);
19834 }
19835
19836 static int
19837 read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
19838 {
19839 return bfd_get_signed_32 (abfd, buf);
19840 }
19841
19842 static ULONGEST
19843 read_8_bytes (bfd *abfd, const gdb_byte *buf)
19844 {
19845 return bfd_get_64 (abfd, buf);
19846 }
19847
19848 static CORE_ADDR
19849 read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
19850 unsigned int *bytes_read)
19851 {
19852 struct comp_unit_head *cu_header = &cu->header;
19853 CORE_ADDR retval = 0;
19854
19855 if (cu_header->signed_addr_p)
19856 {
19857 switch (cu_header->addr_size)
19858 {
19859 case 2:
19860 retval = bfd_get_signed_16 (abfd, buf);
19861 break;
19862 case 4:
19863 retval = bfd_get_signed_32 (abfd, buf);
19864 break;
19865 case 8:
19866 retval = bfd_get_signed_64 (abfd, buf);
19867 break;
19868 default:
19869 internal_error (__FILE__, __LINE__,
19870 _("read_address: bad switch, signed [in module %s]"),
19871 bfd_get_filename (abfd));
19872 }
19873 }
19874 else
19875 {
19876 switch (cu_header->addr_size)
19877 {
19878 case 2:
19879 retval = bfd_get_16 (abfd, buf);
19880 break;
19881 case 4:
19882 retval = bfd_get_32 (abfd, buf);
19883 break;
19884 case 8:
19885 retval = bfd_get_64 (abfd, buf);
19886 break;
19887 default:
19888 internal_error (__FILE__, __LINE__,
19889 _("read_address: bad switch, "
19890 "unsigned [in module %s]"),
19891 bfd_get_filename (abfd));
19892 }
19893 }
19894
19895 *bytes_read = cu_header->addr_size;
19896 return retval;
19897 }
19898
19899 /* Read the initial length from a section. The (draft) DWARF 3
19900 specification allows the initial length to take up either 4 bytes
19901 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
19902 bytes describe the length and all offsets will be 8 bytes in length
19903 instead of 4.
19904
19905 An older, non-standard 64-bit format is also handled by this
19906 function. The older format in question stores the initial length
19907 as an 8-byte quantity without an escape value. Lengths greater
19908 than 2^32 aren't very common which means that the initial 4 bytes
19909 is almost always zero. Since a length value of zero doesn't make
19910 sense for the 32-bit format, this initial zero can be considered to
19911 be an escape value which indicates the presence of the older 64-bit
19912 format. As written, the code can't detect (old format) lengths
19913 greater than 4GB. If it becomes necessary to handle lengths
19914 somewhat larger than 4GB, we could allow other small values (such
19915 as the non-sensical values of 1, 2, and 3) to also be used as
19916 escape values indicating the presence of the old format.
19917
19918 The value returned via bytes_read should be used to increment the
19919 relevant pointer after calling read_initial_length().
19920
19921 [ Note: read_initial_length() and read_offset() are based on the
19922 document entitled "DWARF Debugging Information Format", revision
19923 3, draft 8, dated November 19, 2001. This document was obtained
19924 from:
19925
19926 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
19927
19928 This document is only a draft and is subject to change. (So beware.)
19929
19930 Details regarding the older, non-standard 64-bit format were
19931 determined empirically by examining 64-bit ELF files produced by
19932 the SGI toolchain on an IRIX 6.5 machine.
19933
19934 - Kevin, July 16, 2002
19935 ] */
19936
19937 static LONGEST
19938 read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
19939 {
19940 LONGEST length = bfd_get_32 (abfd, buf);
19941
19942 if (length == 0xffffffff)
19943 {
19944 length = bfd_get_64 (abfd, buf + 4);
19945 *bytes_read = 12;
19946 }
19947 else if (length == 0)
19948 {
19949 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
19950 length = bfd_get_64 (abfd, buf);
19951 *bytes_read = 8;
19952 }
19953 else
19954 {
19955 *bytes_read = 4;
19956 }
19957
19958 return length;
19959 }
19960
19961 /* Cover function for read_initial_length.
19962 Returns the length of the object at BUF, and stores the size of the
19963 initial length in *BYTES_READ and stores the size that offsets will be in
19964 *OFFSET_SIZE.
19965 If the initial length size is not equivalent to that specified in
19966 CU_HEADER then issue a complaint.
19967 This is useful when reading non-comp-unit headers. */
19968
19969 static LONGEST
19970 read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
19971 const struct comp_unit_head *cu_header,
19972 unsigned int *bytes_read,
19973 unsigned int *offset_size)
19974 {
19975 LONGEST length = read_initial_length (abfd, buf, bytes_read);
19976
19977 gdb_assert (cu_header->initial_length_size == 4
19978 || cu_header->initial_length_size == 8
19979 || cu_header->initial_length_size == 12);
19980
19981 if (cu_header->initial_length_size != *bytes_read)
19982 complaint (_("intermixed 32-bit and 64-bit DWARF sections"));
19983
19984 *offset_size = (*bytes_read == 4) ? 4 : 8;
19985 return length;
19986 }
19987
19988 /* Read an offset from the data stream. The size of the offset is
19989 given by cu_header->offset_size. */
19990
19991 static LONGEST
19992 read_offset (bfd *abfd, const gdb_byte *buf,
19993 const struct comp_unit_head *cu_header,
19994 unsigned int *bytes_read)
19995 {
19996 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
19997
19998 *bytes_read = cu_header->offset_size;
19999 return offset;
20000 }
20001
20002 /* Read an offset from the data stream. */
20003
20004 static LONGEST
20005 read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
20006 {
20007 LONGEST retval = 0;
20008
20009 switch (offset_size)
20010 {
20011 case 4:
20012 retval = bfd_get_32 (abfd, buf);
20013 break;
20014 case 8:
20015 retval = bfd_get_64 (abfd, buf);
20016 break;
20017 default:
20018 internal_error (__FILE__, __LINE__,
20019 _("read_offset_1: bad switch [in module %s]"),
20020 bfd_get_filename (abfd));
20021 }
20022
20023 return retval;
20024 }
20025
20026 static const gdb_byte *
20027 read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
20028 {
20029 /* If the size of a host char is 8 bits, we can return a pointer
20030 to the buffer, otherwise we have to copy the data to a buffer
20031 allocated on the temporary obstack. */
20032 gdb_assert (HOST_CHAR_BIT == 8);
20033 return buf;
20034 }
20035
20036 static const char *
20037 read_direct_string (bfd *abfd, const gdb_byte *buf,
20038 unsigned int *bytes_read_ptr)
20039 {
20040 /* If the size of a host char is 8 bits, we can return a pointer
20041 to the string, otherwise we have to copy the string to a buffer
20042 allocated on the temporary obstack. */
20043 gdb_assert (HOST_CHAR_BIT == 8);
20044 if (*buf == '\0')
20045 {
20046 *bytes_read_ptr = 1;
20047 return NULL;
20048 }
20049 *bytes_read_ptr = strlen ((const char *) buf) + 1;
20050 return (const char *) buf;
20051 }
20052
20053 /* Return pointer to string at section SECT offset STR_OFFSET with error
20054 reporting strings FORM_NAME and SECT_NAME. */
20055
20056 static const char *
20057 read_indirect_string_at_offset_from (struct objfile *objfile,
20058 bfd *abfd, LONGEST str_offset,
20059 struct dwarf2_section_info *sect,
20060 const char *form_name,
20061 const char *sect_name)
20062 {
20063 dwarf2_read_section (objfile, sect);
20064 if (sect->buffer == NULL)
20065 error (_("%s used without %s section [in module %s]"),
20066 form_name, sect_name, bfd_get_filename (abfd));
20067 if (str_offset >= sect->size)
20068 error (_("%s pointing outside of %s section [in module %s]"),
20069 form_name, sect_name, bfd_get_filename (abfd));
20070 gdb_assert (HOST_CHAR_BIT == 8);
20071 if (sect->buffer[str_offset] == '\0')
20072 return NULL;
20073 return (const char *) (sect->buffer + str_offset);
20074 }
20075
20076 /* Return pointer to string at .debug_str offset STR_OFFSET. */
20077
20078 static const char *
20079 read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
20080 bfd *abfd, LONGEST str_offset)
20081 {
20082 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
20083 abfd, str_offset,
20084 &dwarf2_per_objfile->str,
20085 "DW_FORM_strp", ".debug_str");
20086 }
20087
20088 /* Return pointer to string at .debug_line_str offset STR_OFFSET. */
20089
20090 static const char *
20091 read_indirect_line_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
20092 bfd *abfd, LONGEST str_offset)
20093 {
20094 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
20095 abfd, str_offset,
20096 &dwarf2_per_objfile->line_str,
20097 "DW_FORM_line_strp",
20098 ".debug_line_str");
20099 }
20100
20101 /* Read a string at offset STR_OFFSET in the .debug_str section from
20102 the .dwz file DWZ. Throw an error if the offset is too large. If
20103 the string consists of a single NUL byte, return NULL; otherwise
20104 return a pointer to the string. */
20105
20106 static const char *
20107 read_indirect_string_from_dwz (struct objfile *objfile, struct dwz_file *dwz,
20108 LONGEST str_offset)
20109 {
20110 dwarf2_read_section (objfile, &dwz->str);
20111
20112 if (dwz->str.buffer == NULL)
20113 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
20114 "section [in module %s]"),
20115 bfd_get_filename (dwz->dwz_bfd.get ()));
20116 if (str_offset >= dwz->str.size)
20117 error (_("DW_FORM_GNU_strp_alt pointing outside of "
20118 ".debug_str section [in module %s]"),
20119 bfd_get_filename (dwz->dwz_bfd.get ()));
20120 gdb_assert (HOST_CHAR_BIT == 8);
20121 if (dwz->str.buffer[str_offset] == '\0')
20122 return NULL;
20123 return (const char *) (dwz->str.buffer + str_offset);
20124 }
20125
20126 /* Return pointer to string at .debug_str offset as read from BUF.
20127 BUF is assumed to be in a compilation unit described by CU_HEADER.
20128 Return *BYTES_READ_PTR count of bytes read from BUF. */
20129
20130 static const char *
20131 read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
20132 const gdb_byte *buf,
20133 const struct comp_unit_head *cu_header,
20134 unsigned int *bytes_read_ptr)
20135 {
20136 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
20137
20138 return read_indirect_string_at_offset (dwarf2_per_objfile, abfd, str_offset);
20139 }
20140
20141 /* Return pointer to string at .debug_line_str offset as read from BUF.
20142 BUF is assumed to be in a compilation unit described by CU_HEADER.
20143 Return *BYTES_READ_PTR count of bytes read from BUF. */
20144
20145 static const char *
20146 read_indirect_line_string (struct dwarf2_per_objfile *dwarf2_per_objfile,
20147 bfd *abfd, const gdb_byte *buf,
20148 const struct comp_unit_head *cu_header,
20149 unsigned int *bytes_read_ptr)
20150 {
20151 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
20152
20153 return read_indirect_line_string_at_offset (dwarf2_per_objfile, abfd,
20154 str_offset);
20155 }
20156
20157 ULONGEST
20158 read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
20159 unsigned int *bytes_read_ptr)
20160 {
20161 ULONGEST result;
20162 unsigned int num_read;
20163 int shift;
20164 unsigned char byte;
20165
20166 result = 0;
20167 shift = 0;
20168 num_read = 0;
20169 while (1)
20170 {
20171 byte = bfd_get_8 (abfd, buf);
20172 buf++;
20173 num_read++;
20174 result |= ((ULONGEST) (byte & 127) << shift);
20175 if ((byte & 128) == 0)
20176 {
20177 break;
20178 }
20179 shift += 7;
20180 }
20181 *bytes_read_ptr = num_read;
20182 return result;
20183 }
20184
20185 static LONGEST
20186 read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
20187 unsigned int *bytes_read_ptr)
20188 {
20189 ULONGEST result;
20190 int shift, num_read;
20191 unsigned char byte;
20192
20193 result = 0;
20194 shift = 0;
20195 num_read = 0;
20196 while (1)
20197 {
20198 byte = bfd_get_8 (abfd, buf);
20199 buf++;
20200 num_read++;
20201 result |= ((ULONGEST) (byte & 127) << shift);
20202 shift += 7;
20203 if ((byte & 128) == 0)
20204 {
20205 break;
20206 }
20207 }
20208 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
20209 result |= -(((ULONGEST) 1) << shift);
20210 *bytes_read_ptr = num_read;
20211 return result;
20212 }
20213
20214 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
20215 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
20216 ADDR_SIZE is the size of addresses from the CU header. */
20217
20218 static CORE_ADDR
20219 read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
20220 unsigned int addr_index, gdb::optional<ULONGEST> addr_base,
20221 int addr_size)
20222 {
20223 struct objfile *objfile = dwarf2_per_objfile->objfile;
20224 bfd *abfd = objfile->obfd;
20225 const gdb_byte *info_ptr;
20226 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
20227
20228 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
20229 if (dwarf2_per_objfile->addr.buffer == NULL)
20230 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
20231 objfile_name (objfile));
20232 if (addr_base_or_zero + addr_index * addr_size
20233 >= dwarf2_per_objfile->addr.size)
20234 error (_("DW_FORM_addr_index pointing outside of "
20235 ".debug_addr section [in module %s]"),
20236 objfile_name (objfile));
20237 info_ptr = (dwarf2_per_objfile->addr.buffer
20238 + addr_base_or_zero + addr_index * addr_size);
20239 if (addr_size == 4)
20240 return bfd_get_32 (abfd, info_ptr);
20241 else
20242 return bfd_get_64 (abfd, info_ptr);
20243 }
20244
20245 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
20246
20247 static CORE_ADDR
20248 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
20249 {
20250 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
20251 cu->addr_base, cu->header.addr_size);
20252 }
20253
20254 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
20255
20256 static CORE_ADDR
20257 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
20258 unsigned int *bytes_read)
20259 {
20260 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
20261 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
20262
20263 return read_addr_index (cu, addr_index);
20264 }
20265
20266 /* Data structure to pass results from dwarf2_read_addr_index_reader
20267 back to dwarf2_read_addr_index. */
20268
20269 struct dwarf2_read_addr_index_data
20270 {
20271 gdb::optional<ULONGEST> addr_base;
20272 int addr_size;
20273 };
20274
20275 /* die_reader_func for dwarf2_read_addr_index. */
20276
20277 static void
20278 dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
20279 const gdb_byte *info_ptr,
20280 struct die_info *comp_unit_die,
20281 int has_children,
20282 void *data)
20283 {
20284 struct dwarf2_cu *cu = reader->cu;
20285 struct dwarf2_read_addr_index_data *aidata =
20286 (struct dwarf2_read_addr_index_data *) data;
20287
20288 aidata->addr_base = cu->addr_base;
20289 aidata->addr_size = cu->header.addr_size;
20290 }
20291
20292 /* Given an index in .debug_addr, fetch the value.
20293 NOTE: This can be called during dwarf expression evaluation,
20294 long after the debug information has been read, and thus per_cu->cu
20295 may no longer exist. */
20296
20297 CORE_ADDR
20298 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
20299 unsigned int addr_index)
20300 {
20301 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
20302 struct dwarf2_cu *cu = per_cu->cu;
20303 gdb::optional<ULONGEST> addr_base;
20304 int addr_size;
20305
20306 /* We need addr_base and addr_size.
20307 If we don't have PER_CU->cu, we have to get it.
20308 Nasty, but the alternative is storing the needed info in PER_CU,
20309 which at this point doesn't seem justified: it's not clear how frequently
20310 it would get used and it would increase the size of every PER_CU.
20311 Entry points like dwarf2_per_cu_addr_size do a similar thing
20312 so we're not in uncharted territory here.
20313 Alas we need to be a bit more complicated as addr_base is contained
20314 in the DIE.
20315
20316 We don't need to read the entire CU(/TU).
20317 We just need the header and top level die.
20318
20319 IWBN to use the aging mechanism to let us lazily later discard the CU.
20320 For now we skip this optimization. */
20321
20322 if (cu != NULL)
20323 {
20324 addr_base = cu->addr_base;
20325 addr_size = cu->header.addr_size;
20326 }
20327 else
20328 {
20329 struct dwarf2_read_addr_index_data aidata;
20330
20331 /* Note: We can't use init_cutu_and_read_dies_simple here,
20332 we need addr_base. */
20333 init_cutu_and_read_dies (per_cu, NULL, 0, 0, false,
20334 dwarf2_read_addr_index_reader, &aidata);
20335 addr_base = aidata.addr_base;
20336 addr_size = aidata.addr_size;
20337 }
20338
20339 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
20340 addr_size);
20341 }
20342
20343 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
20344 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
20345 DWO file. */
20346
20347 static const char *
20348 read_str_index (struct dwarf2_cu *cu,
20349 struct dwarf2_section_info *str_section,
20350 struct dwarf2_section_info *str_offsets_section,
20351 ULONGEST str_offsets_base, ULONGEST str_index)
20352 {
20353 struct dwarf2_per_objfile *dwarf2_per_objfile
20354 = cu->per_cu->dwarf2_per_objfile;
20355 struct objfile *objfile = dwarf2_per_objfile->objfile;
20356 const char *objf_name = objfile_name (objfile);
20357 bfd *abfd = objfile->obfd;
20358 const gdb_byte *info_ptr;
20359 ULONGEST str_offset;
20360 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
20361
20362 dwarf2_read_section (objfile, str_section);
20363 dwarf2_read_section (objfile, str_offsets_section);
20364 if (str_section->buffer == NULL)
20365 error (_("%s used without %s section"
20366 " in CU at offset %s [in module %s]"),
20367 form_name, get_section_name (str_section),
20368 sect_offset_str (cu->header.sect_off), objf_name);
20369 if (str_offsets_section->buffer == NULL)
20370 error (_("%s used without %s section"
20371 " in CU at offset %s [in module %s]"),
20372 form_name, get_section_name (str_section),
20373 sect_offset_str (cu->header.sect_off), objf_name);
20374 info_ptr = (str_offsets_section->buffer
20375 + str_offsets_base
20376 + str_index * cu->header.offset_size);
20377 if (cu->header.offset_size == 4)
20378 str_offset = bfd_get_32 (abfd, info_ptr);
20379 else
20380 str_offset = bfd_get_64 (abfd, info_ptr);
20381 if (str_offset >= str_section->size)
20382 error (_("Offset from %s pointing outside of"
20383 " .debug_str.dwo section in CU at offset %s [in module %s]"),
20384 form_name, sect_offset_str (cu->header.sect_off), objf_name);
20385 return (const char *) (str_section->buffer + str_offset);
20386 }
20387
20388 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
20389
20390 static const char *
20391 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
20392 {
20393 ULONGEST str_offsets_base = reader->cu->header.version >= 5
20394 ? reader->cu->header.addr_size : 0;
20395 return read_str_index (reader->cu,
20396 &reader->dwo_file->sections.str,
20397 &reader->dwo_file->sections.str_offsets,
20398 str_offsets_base, str_index);
20399 }
20400
20401 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
20402
20403 static const char *
20404 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
20405 {
20406 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20407 const char *objf_name = objfile_name (objfile);
20408 static const char form_name[] = "DW_FORM_GNU_str_index";
20409 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
20410
20411 if (!cu->str_offsets_base.has_value ())
20412 error (_("%s used in Fission stub without %s"
20413 " in CU at offset 0x%lx [in module %s]"),
20414 form_name, str_offsets_attr_name,
20415 (long) cu->header.offset_size, objf_name);
20416
20417 return read_str_index (cu,
20418 &cu->per_cu->dwarf2_per_objfile->str,
20419 &cu->per_cu->dwarf2_per_objfile->str_offsets,
20420 *cu->str_offsets_base, str_index);
20421 }
20422
20423 /* Return the length of an LEB128 number in BUF. */
20424
20425 static int
20426 leb128_size (const gdb_byte *buf)
20427 {
20428 const gdb_byte *begin = buf;
20429 gdb_byte byte;
20430
20431 while (1)
20432 {
20433 byte = *buf++;
20434 if ((byte & 128) == 0)
20435 return buf - begin;
20436 }
20437 }
20438
20439 static void
20440 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
20441 {
20442 switch (lang)
20443 {
20444 case DW_LANG_C89:
20445 case DW_LANG_C99:
20446 case DW_LANG_C11:
20447 case DW_LANG_C:
20448 case DW_LANG_UPC:
20449 cu->language = language_c;
20450 break;
20451 case DW_LANG_Java:
20452 case DW_LANG_C_plus_plus:
20453 case DW_LANG_C_plus_plus_11:
20454 case DW_LANG_C_plus_plus_14:
20455 cu->language = language_cplus;
20456 break;
20457 case DW_LANG_D:
20458 cu->language = language_d;
20459 break;
20460 case DW_LANG_Fortran77:
20461 case DW_LANG_Fortran90:
20462 case DW_LANG_Fortran95:
20463 case DW_LANG_Fortran03:
20464 case DW_LANG_Fortran08:
20465 cu->language = language_fortran;
20466 break;
20467 case DW_LANG_Go:
20468 cu->language = language_go;
20469 break;
20470 case DW_LANG_Mips_Assembler:
20471 cu->language = language_asm;
20472 break;
20473 case DW_LANG_Ada83:
20474 case DW_LANG_Ada95:
20475 cu->language = language_ada;
20476 break;
20477 case DW_LANG_Modula2:
20478 cu->language = language_m2;
20479 break;
20480 case DW_LANG_Pascal83:
20481 cu->language = language_pascal;
20482 break;
20483 case DW_LANG_ObjC:
20484 cu->language = language_objc;
20485 break;
20486 case DW_LANG_Rust:
20487 case DW_LANG_Rust_old:
20488 cu->language = language_rust;
20489 break;
20490 case DW_LANG_Cobol74:
20491 case DW_LANG_Cobol85:
20492 default:
20493 cu->language = language_minimal;
20494 break;
20495 }
20496 cu->language_defn = language_def (cu->language);
20497 }
20498
20499 /* Return the named attribute or NULL if not there. */
20500
20501 static struct attribute *
20502 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20503 {
20504 for (;;)
20505 {
20506 unsigned int i;
20507 struct attribute *spec = NULL;
20508
20509 for (i = 0; i < die->num_attrs; ++i)
20510 {
20511 if (die->attrs[i].name == name)
20512 return &die->attrs[i];
20513 if (die->attrs[i].name == DW_AT_specification
20514 || die->attrs[i].name == DW_AT_abstract_origin)
20515 spec = &die->attrs[i];
20516 }
20517
20518 if (!spec)
20519 break;
20520
20521 die = follow_die_ref (die, spec, &cu);
20522 }
20523
20524 return NULL;
20525 }
20526
20527 /* Return the named attribute or NULL if not there,
20528 but do not follow DW_AT_specification, etc.
20529 This is for use in contexts where we're reading .debug_types dies.
20530 Following DW_AT_specification, DW_AT_abstract_origin will take us
20531 back up the chain, and we want to go down. */
20532
20533 static struct attribute *
20534 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
20535 {
20536 unsigned int i;
20537
20538 for (i = 0; i < die->num_attrs; ++i)
20539 if (die->attrs[i].name == name)
20540 return &die->attrs[i];
20541
20542 return NULL;
20543 }
20544
20545 /* Return the string associated with a string-typed attribute, or NULL if it
20546 is either not found or is of an incorrect type. */
20547
20548 static const char *
20549 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20550 {
20551 struct attribute *attr;
20552 const char *str = NULL;
20553
20554 attr = dwarf2_attr (die, name, cu);
20555
20556 if (attr != NULL)
20557 {
20558 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
20559 || attr->form == DW_FORM_string
20560 || attr->form == DW_FORM_strx
20561 || attr->form == DW_FORM_strx1
20562 || attr->form == DW_FORM_strx2
20563 || attr->form == DW_FORM_strx3
20564 || attr->form == DW_FORM_strx4
20565 || attr->form == DW_FORM_GNU_str_index
20566 || attr->form == DW_FORM_GNU_strp_alt)
20567 str = DW_STRING (attr);
20568 else
20569 complaint (_("string type expected for attribute %s for "
20570 "DIE at %s in module %s"),
20571 dwarf_attr_name (name), sect_offset_str (die->sect_off),
20572 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
20573 }
20574
20575 return str;
20576 }
20577
20578 /* Return the dwo name or NULL if not present. If present, it is in either
20579 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
20580 static const char *
20581 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
20582 {
20583 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
20584 if (dwo_name == nullptr)
20585 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
20586 return dwo_name;
20587 }
20588
20589 /* Return non-zero iff the attribute NAME is defined for the given DIE,
20590 and holds a non-zero value. This function should only be used for
20591 DW_FORM_flag or DW_FORM_flag_present attributes. */
20592
20593 static int
20594 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
20595 {
20596 struct attribute *attr = dwarf2_attr (die, name, cu);
20597
20598 return (attr && DW_UNSND (attr));
20599 }
20600
20601 static int
20602 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
20603 {
20604 /* A DIE is a declaration if it has a DW_AT_declaration attribute
20605 which value is non-zero. However, we have to be careful with
20606 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
20607 (via dwarf2_flag_true_p) follows this attribute. So we may
20608 end up accidently finding a declaration attribute that belongs
20609 to a different DIE referenced by the specification attribute,
20610 even though the given DIE does not have a declaration attribute. */
20611 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
20612 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
20613 }
20614
20615 /* Return the die giving the specification for DIE, if there is
20616 one. *SPEC_CU is the CU containing DIE on input, and the CU
20617 containing the return value on output. If there is no
20618 specification, but there is an abstract origin, that is
20619 returned. */
20620
20621 static struct die_info *
20622 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
20623 {
20624 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
20625 *spec_cu);
20626
20627 if (spec_attr == NULL)
20628 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
20629
20630 if (spec_attr == NULL)
20631 return NULL;
20632 else
20633 return follow_die_ref (die, spec_attr, spec_cu);
20634 }
20635
20636 /* Stub for free_line_header to match void * callback types. */
20637
20638 static void
20639 free_line_header_voidp (void *arg)
20640 {
20641 struct line_header *lh = (struct line_header *) arg;
20642
20643 delete lh;
20644 }
20645
20646 void
20647 line_header::add_include_dir (const char *include_dir)
20648 {
20649 if (dwarf_line_debug >= 2)
20650 {
20651 size_t new_size;
20652 if (version >= 5)
20653 new_size = m_include_dirs.size ();
20654 else
20655 new_size = m_include_dirs.size () + 1;
20656 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
20657 new_size, include_dir);
20658 }
20659 m_include_dirs.push_back (include_dir);
20660 }
20661
20662 void
20663 line_header::add_file_name (const char *name,
20664 dir_index d_index,
20665 unsigned int mod_time,
20666 unsigned int length)
20667 {
20668 if (dwarf_line_debug >= 2)
20669 {
20670 size_t new_size;
20671 if (version >= 5)
20672 new_size = file_names_size ();
20673 else
20674 new_size = file_names_size () + 1;
20675 fprintf_unfiltered (gdb_stdlog, "Adding file %zu: %s\n",
20676 new_size, name);
20677 }
20678 m_file_names.emplace_back (name, d_index, mod_time, length);
20679 }
20680
20681 /* A convenience function to find the proper .debug_line section for a CU. */
20682
20683 static struct dwarf2_section_info *
20684 get_debug_line_section (struct dwarf2_cu *cu)
20685 {
20686 struct dwarf2_section_info *section;
20687 struct dwarf2_per_objfile *dwarf2_per_objfile
20688 = cu->per_cu->dwarf2_per_objfile;
20689
20690 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
20691 DWO file. */
20692 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20693 section = &cu->dwo_unit->dwo_file->sections.line;
20694 else if (cu->per_cu->is_dwz)
20695 {
20696 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
20697
20698 section = &dwz->line;
20699 }
20700 else
20701 section = &dwarf2_per_objfile->line;
20702
20703 return section;
20704 }
20705
20706 /* Read directory or file name entry format, starting with byte of
20707 format count entries, ULEB128 pairs of entry formats, ULEB128 of
20708 entries count and the entries themselves in the described entry
20709 format. */
20710
20711 static void
20712 read_formatted_entries (struct dwarf2_per_objfile *dwarf2_per_objfile,
20713 bfd *abfd, const gdb_byte **bufp,
20714 struct line_header *lh,
20715 const struct comp_unit_head *cu_header,
20716 void (*callback) (struct line_header *lh,
20717 const char *name,
20718 dir_index d_index,
20719 unsigned int mod_time,
20720 unsigned int length))
20721 {
20722 gdb_byte format_count, formati;
20723 ULONGEST data_count, datai;
20724 const gdb_byte *buf = *bufp;
20725 const gdb_byte *format_header_data;
20726 unsigned int bytes_read;
20727
20728 format_count = read_1_byte (abfd, buf);
20729 buf += 1;
20730 format_header_data = buf;
20731 for (formati = 0; formati < format_count; formati++)
20732 {
20733 read_unsigned_leb128 (abfd, buf, &bytes_read);
20734 buf += bytes_read;
20735 read_unsigned_leb128 (abfd, buf, &bytes_read);
20736 buf += bytes_read;
20737 }
20738
20739 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
20740 buf += bytes_read;
20741 for (datai = 0; datai < data_count; datai++)
20742 {
20743 const gdb_byte *format = format_header_data;
20744 struct file_entry fe;
20745
20746 for (formati = 0; formati < format_count; formati++)
20747 {
20748 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
20749 format += bytes_read;
20750
20751 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
20752 format += bytes_read;
20753
20754 gdb::optional<const char *> string;
20755 gdb::optional<unsigned int> uint;
20756
20757 switch (form)
20758 {
20759 case DW_FORM_string:
20760 string.emplace (read_direct_string (abfd, buf, &bytes_read));
20761 buf += bytes_read;
20762 break;
20763
20764 case DW_FORM_line_strp:
20765 string.emplace (read_indirect_line_string (dwarf2_per_objfile,
20766 abfd, buf,
20767 cu_header,
20768 &bytes_read));
20769 buf += bytes_read;
20770 break;
20771
20772 case DW_FORM_data1:
20773 uint.emplace (read_1_byte (abfd, buf));
20774 buf += 1;
20775 break;
20776
20777 case DW_FORM_data2:
20778 uint.emplace (read_2_bytes (abfd, buf));
20779 buf += 2;
20780 break;
20781
20782 case DW_FORM_data4:
20783 uint.emplace (read_4_bytes (abfd, buf));
20784 buf += 4;
20785 break;
20786
20787 case DW_FORM_data8:
20788 uint.emplace (read_8_bytes (abfd, buf));
20789 buf += 8;
20790 break;
20791
20792 case DW_FORM_data16:
20793 /* This is used for MD5, but file_entry does not record MD5s. */
20794 buf += 16;
20795 break;
20796
20797 case DW_FORM_udata:
20798 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
20799 buf += bytes_read;
20800 break;
20801
20802 case DW_FORM_block:
20803 /* It is valid only for DW_LNCT_timestamp which is ignored by
20804 current GDB. */
20805 break;
20806 }
20807
20808 switch (content_type)
20809 {
20810 case DW_LNCT_path:
20811 if (string.has_value ())
20812 fe.name = *string;
20813 break;
20814 case DW_LNCT_directory_index:
20815 if (uint.has_value ())
20816 fe.d_index = (dir_index) *uint;
20817 break;
20818 case DW_LNCT_timestamp:
20819 if (uint.has_value ())
20820 fe.mod_time = *uint;
20821 break;
20822 case DW_LNCT_size:
20823 if (uint.has_value ())
20824 fe.length = *uint;
20825 break;
20826 case DW_LNCT_MD5:
20827 break;
20828 default:
20829 complaint (_("Unknown format content type %s"),
20830 pulongest (content_type));
20831 }
20832 }
20833
20834 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
20835 }
20836
20837 *bufp = buf;
20838 }
20839
20840 /* Read the statement program header starting at OFFSET in
20841 .debug_line, or .debug_line.dwo. Return a pointer
20842 to a struct line_header, allocated using xmalloc.
20843 Returns NULL if there is a problem reading the header, e.g., if it
20844 has a version we don't understand.
20845
20846 NOTE: the strings in the include directory and file name tables of
20847 the returned object point into the dwarf line section buffer,
20848 and must not be freed. */
20849
20850 static line_header_up
20851 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
20852 {
20853 const gdb_byte *line_ptr;
20854 unsigned int bytes_read, offset_size;
20855 int i;
20856 const char *cur_dir, *cur_file;
20857 struct dwarf2_section_info *section;
20858 bfd *abfd;
20859 struct dwarf2_per_objfile *dwarf2_per_objfile
20860 = cu->per_cu->dwarf2_per_objfile;
20861
20862 section = get_debug_line_section (cu);
20863 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
20864 if (section->buffer == NULL)
20865 {
20866 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20867 complaint (_("missing .debug_line.dwo section"));
20868 else
20869 complaint (_("missing .debug_line section"));
20870 return 0;
20871 }
20872
20873 /* We can't do this until we know the section is non-empty.
20874 Only then do we know we have such a section. */
20875 abfd = get_section_bfd_owner (section);
20876
20877 /* Make sure that at least there's room for the total_length field.
20878 That could be 12 bytes long, but we're just going to fudge that. */
20879 if (to_underlying (sect_off) + 4 >= section->size)
20880 {
20881 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20882 return 0;
20883 }
20884
20885 line_header_up lh (new line_header ());
20886
20887 lh->sect_off = sect_off;
20888 lh->offset_in_dwz = cu->per_cu->is_dwz;
20889
20890 line_ptr = section->buffer + to_underlying (sect_off);
20891
20892 /* Read in the header. */
20893 lh->total_length =
20894 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
20895 &bytes_read, &offset_size);
20896 line_ptr += bytes_read;
20897
20898 const gdb_byte *start_here = line_ptr;
20899
20900 if (line_ptr + lh->total_length > (section->buffer + section->size))
20901 {
20902 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20903 return 0;
20904 }
20905 lh->statement_program_end = start_here + lh->total_length;
20906 lh->version = read_2_bytes (abfd, line_ptr);
20907 line_ptr += 2;
20908 if (lh->version > 5)
20909 {
20910 /* This is a version we don't understand. The format could have
20911 changed in ways we don't handle properly so just punt. */
20912 complaint (_("unsupported version in .debug_line section"));
20913 return NULL;
20914 }
20915 if (lh->version >= 5)
20916 {
20917 gdb_byte segment_selector_size;
20918
20919 /* Skip address size. */
20920 read_1_byte (abfd, line_ptr);
20921 line_ptr += 1;
20922
20923 segment_selector_size = read_1_byte (abfd, line_ptr);
20924 line_ptr += 1;
20925 if (segment_selector_size != 0)
20926 {
20927 complaint (_("unsupported segment selector size %u "
20928 "in .debug_line section"),
20929 segment_selector_size);
20930 return NULL;
20931 }
20932 }
20933 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
20934 line_ptr += offset_size;
20935 lh->statement_program_start = line_ptr + lh->header_length;
20936 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
20937 line_ptr += 1;
20938 if (lh->version >= 4)
20939 {
20940 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
20941 line_ptr += 1;
20942 }
20943 else
20944 lh->maximum_ops_per_instruction = 1;
20945
20946 if (lh->maximum_ops_per_instruction == 0)
20947 {
20948 lh->maximum_ops_per_instruction = 1;
20949 complaint (_("invalid maximum_ops_per_instruction "
20950 "in `.debug_line' section"));
20951 }
20952
20953 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
20954 line_ptr += 1;
20955 lh->line_base = read_1_signed_byte (abfd, line_ptr);
20956 line_ptr += 1;
20957 lh->line_range = read_1_byte (abfd, line_ptr);
20958 line_ptr += 1;
20959 lh->opcode_base = read_1_byte (abfd, line_ptr);
20960 line_ptr += 1;
20961 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
20962
20963 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
20964 for (i = 1; i < lh->opcode_base; ++i)
20965 {
20966 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
20967 line_ptr += 1;
20968 }
20969
20970 if (lh->version >= 5)
20971 {
20972 /* Read directory table. */
20973 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20974 &cu->header,
20975 [] (struct line_header *header, const char *name,
20976 dir_index d_index, unsigned int mod_time,
20977 unsigned int length)
20978 {
20979 header->add_include_dir (name);
20980 });
20981
20982 /* Read file name table. */
20983 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20984 &cu->header,
20985 [] (struct line_header *header, const char *name,
20986 dir_index d_index, unsigned int mod_time,
20987 unsigned int length)
20988 {
20989 header->add_file_name (name, d_index, mod_time, length);
20990 });
20991 }
20992 else
20993 {
20994 /* Read directory table. */
20995 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20996 {
20997 line_ptr += bytes_read;
20998 lh->add_include_dir (cur_dir);
20999 }
21000 line_ptr += bytes_read;
21001
21002 /* Read file name table. */
21003 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
21004 {
21005 unsigned int mod_time, length;
21006 dir_index d_index;
21007
21008 line_ptr += bytes_read;
21009 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21010 line_ptr += bytes_read;
21011 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21012 line_ptr += bytes_read;
21013 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21014 line_ptr += bytes_read;
21015
21016 lh->add_file_name (cur_file, d_index, mod_time, length);
21017 }
21018 line_ptr += bytes_read;
21019 }
21020
21021 if (line_ptr > (section->buffer + section->size))
21022 complaint (_("line number info header doesn't "
21023 "fit in `.debug_line' section"));
21024
21025 return lh;
21026 }
21027
21028 /* Subroutine of dwarf_decode_lines to simplify it.
21029 Return the file name of the psymtab for the given file_entry.
21030 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21031 If space for the result is malloc'd, *NAME_HOLDER will be set.
21032 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
21033
21034 static const char *
21035 psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
21036 const struct partial_symtab *pst,
21037 const char *comp_dir,
21038 gdb::unique_xmalloc_ptr<char> *name_holder)
21039 {
21040 const char *include_name = fe.name;
21041 const char *include_name_to_compare = include_name;
21042 const char *pst_filename;
21043 int file_is_pst;
21044
21045 const char *dir_name = fe.include_dir (lh);
21046
21047 gdb::unique_xmalloc_ptr<char> hold_compare;
21048 if (!IS_ABSOLUTE_PATH (include_name)
21049 && (dir_name != NULL || comp_dir != NULL))
21050 {
21051 /* Avoid creating a duplicate psymtab for PST.
21052 We do this by comparing INCLUDE_NAME and PST_FILENAME.
21053 Before we do the comparison, however, we need to account
21054 for DIR_NAME and COMP_DIR.
21055 First prepend dir_name (if non-NULL). If we still don't
21056 have an absolute path prepend comp_dir (if non-NULL).
21057 However, the directory we record in the include-file's
21058 psymtab does not contain COMP_DIR (to match the
21059 corresponding symtab(s)).
21060
21061 Example:
21062
21063 bash$ cd /tmp
21064 bash$ gcc -g ./hello.c
21065 include_name = "hello.c"
21066 dir_name = "."
21067 DW_AT_comp_dir = comp_dir = "/tmp"
21068 DW_AT_name = "./hello.c"
21069
21070 */
21071
21072 if (dir_name != NULL)
21073 {
21074 name_holder->reset (concat (dir_name, SLASH_STRING,
21075 include_name, (char *) NULL));
21076 include_name = name_holder->get ();
21077 include_name_to_compare = include_name;
21078 }
21079 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
21080 {
21081 hold_compare.reset (concat (comp_dir, SLASH_STRING,
21082 include_name, (char *) NULL));
21083 include_name_to_compare = hold_compare.get ();
21084 }
21085 }
21086
21087 pst_filename = pst->filename;
21088 gdb::unique_xmalloc_ptr<char> copied_name;
21089 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
21090 {
21091 copied_name.reset (concat (pst->dirname, SLASH_STRING,
21092 pst_filename, (char *) NULL));
21093 pst_filename = copied_name.get ();
21094 }
21095
21096 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
21097
21098 if (file_is_pst)
21099 return NULL;
21100 return include_name;
21101 }
21102
21103 /* State machine to track the state of the line number program. */
21104
21105 class lnp_state_machine
21106 {
21107 public:
21108 /* Initialize a machine state for the start of a line number
21109 program. */
21110 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
21111 bool record_lines_p);
21112
21113 file_entry *current_file ()
21114 {
21115 /* lh->file_names is 0-based, but the file name numbers in the
21116 statement program are 1-based. */
21117 return m_line_header->file_name_at (m_file);
21118 }
21119
21120 /* Record the line in the state machine. END_SEQUENCE is true if
21121 we're processing the end of a sequence. */
21122 void record_line (bool end_sequence);
21123
21124 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
21125 nop-out rest of the lines in this sequence. */
21126 void check_line_address (struct dwarf2_cu *cu,
21127 const gdb_byte *line_ptr,
21128 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
21129
21130 void handle_set_discriminator (unsigned int discriminator)
21131 {
21132 m_discriminator = discriminator;
21133 m_line_has_non_zero_discriminator |= discriminator != 0;
21134 }
21135
21136 /* Handle DW_LNE_set_address. */
21137 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
21138 {
21139 m_op_index = 0;
21140 address += baseaddr;
21141 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
21142 }
21143
21144 /* Handle DW_LNS_advance_pc. */
21145 void handle_advance_pc (CORE_ADDR adjust);
21146
21147 /* Handle a special opcode. */
21148 void handle_special_opcode (unsigned char op_code);
21149
21150 /* Handle DW_LNS_advance_line. */
21151 void handle_advance_line (int line_delta)
21152 {
21153 advance_line (line_delta);
21154 }
21155
21156 /* Handle DW_LNS_set_file. */
21157 void handle_set_file (file_name_index file);
21158
21159 /* Handle DW_LNS_negate_stmt. */
21160 void handle_negate_stmt ()
21161 {
21162 m_is_stmt = !m_is_stmt;
21163 }
21164
21165 /* Handle DW_LNS_const_add_pc. */
21166 void handle_const_add_pc ();
21167
21168 /* Handle DW_LNS_fixed_advance_pc. */
21169 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
21170 {
21171 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21172 m_op_index = 0;
21173 }
21174
21175 /* Handle DW_LNS_copy. */
21176 void handle_copy ()
21177 {
21178 record_line (false);
21179 m_discriminator = 0;
21180 }
21181
21182 /* Handle DW_LNE_end_sequence. */
21183 void handle_end_sequence ()
21184 {
21185 m_currently_recording_lines = true;
21186 }
21187
21188 private:
21189 /* Advance the line by LINE_DELTA. */
21190 void advance_line (int line_delta)
21191 {
21192 m_line += line_delta;
21193
21194 if (line_delta != 0)
21195 m_line_has_non_zero_discriminator = m_discriminator != 0;
21196 }
21197
21198 struct dwarf2_cu *m_cu;
21199
21200 gdbarch *m_gdbarch;
21201
21202 /* True if we're recording lines.
21203 Otherwise we're building partial symtabs and are just interested in
21204 finding include files mentioned by the line number program. */
21205 bool m_record_lines_p;
21206
21207 /* The line number header. */
21208 line_header *m_line_header;
21209
21210 /* These are part of the standard DWARF line number state machine,
21211 and initialized according to the DWARF spec. */
21212
21213 unsigned char m_op_index = 0;
21214 /* The line table index of the current file. */
21215 file_name_index m_file = 1;
21216 unsigned int m_line = 1;
21217
21218 /* These are initialized in the constructor. */
21219
21220 CORE_ADDR m_address;
21221 bool m_is_stmt;
21222 unsigned int m_discriminator;
21223
21224 /* Additional bits of state we need to track. */
21225
21226 /* The last file that we called dwarf2_start_subfile for.
21227 This is only used for TLLs. */
21228 unsigned int m_last_file = 0;
21229 /* The last file a line number was recorded for. */
21230 struct subfile *m_last_subfile = NULL;
21231
21232 /* When true, record the lines we decode. */
21233 bool m_currently_recording_lines = false;
21234
21235 /* The last line number that was recorded, used to coalesce
21236 consecutive entries for the same line. This can happen, for
21237 example, when discriminators are present. PR 17276. */
21238 unsigned int m_last_line = 0;
21239 bool m_line_has_non_zero_discriminator = false;
21240 };
21241
21242 void
21243 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
21244 {
21245 CORE_ADDR addr_adj = (((m_op_index + adjust)
21246 / m_line_header->maximum_ops_per_instruction)
21247 * m_line_header->minimum_instruction_length);
21248 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21249 m_op_index = ((m_op_index + adjust)
21250 % m_line_header->maximum_ops_per_instruction);
21251 }
21252
21253 void
21254 lnp_state_machine::handle_special_opcode (unsigned char op_code)
21255 {
21256 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
21257 CORE_ADDR addr_adj = (((m_op_index
21258 + (adj_opcode / m_line_header->line_range))
21259 / m_line_header->maximum_ops_per_instruction)
21260 * m_line_header->minimum_instruction_length);
21261 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21262 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
21263 % m_line_header->maximum_ops_per_instruction);
21264
21265 int line_delta = (m_line_header->line_base
21266 + (adj_opcode % m_line_header->line_range));
21267 advance_line (line_delta);
21268 record_line (false);
21269 m_discriminator = 0;
21270 }
21271
21272 void
21273 lnp_state_machine::handle_set_file (file_name_index file)
21274 {
21275 m_file = file;
21276
21277 const file_entry *fe = current_file ();
21278 if (fe == NULL)
21279 dwarf2_debug_line_missing_file_complaint ();
21280 else if (m_record_lines_p)
21281 {
21282 const char *dir = fe->include_dir (m_line_header);
21283
21284 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
21285 m_line_has_non_zero_discriminator = m_discriminator != 0;
21286 dwarf2_start_subfile (m_cu, fe->name, dir);
21287 }
21288 }
21289
21290 void
21291 lnp_state_machine::handle_const_add_pc ()
21292 {
21293 CORE_ADDR adjust
21294 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
21295
21296 CORE_ADDR addr_adj
21297 = (((m_op_index + adjust)
21298 / m_line_header->maximum_ops_per_instruction)
21299 * m_line_header->minimum_instruction_length);
21300
21301 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21302 m_op_index = ((m_op_index + adjust)
21303 % m_line_header->maximum_ops_per_instruction);
21304 }
21305
21306 /* Return non-zero if we should add LINE to the line number table.
21307 LINE is the line to add, LAST_LINE is the last line that was added,
21308 LAST_SUBFILE is the subfile for LAST_LINE.
21309 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
21310 had a non-zero discriminator.
21311
21312 We have to be careful in the presence of discriminators.
21313 E.g., for this line:
21314
21315 for (i = 0; i < 100000; i++);
21316
21317 clang can emit four line number entries for that one line,
21318 each with a different discriminator.
21319 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
21320
21321 However, we want gdb to coalesce all four entries into one.
21322 Otherwise the user could stepi into the middle of the line and
21323 gdb would get confused about whether the pc really was in the
21324 middle of the line.
21325
21326 Things are further complicated by the fact that two consecutive
21327 line number entries for the same line is a heuristic used by gcc
21328 to denote the end of the prologue. So we can't just discard duplicate
21329 entries, we have to be selective about it. The heuristic we use is
21330 that we only collapse consecutive entries for the same line if at least
21331 one of those entries has a non-zero discriminator. PR 17276.
21332
21333 Note: Addresses in the line number state machine can never go backwards
21334 within one sequence, thus this coalescing is ok. */
21335
21336 static int
21337 dwarf_record_line_p (struct dwarf2_cu *cu,
21338 unsigned int line, unsigned int last_line,
21339 int line_has_non_zero_discriminator,
21340 struct subfile *last_subfile)
21341 {
21342 if (cu->get_builder ()->get_current_subfile () != last_subfile)
21343 return 1;
21344 if (line != last_line)
21345 return 1;
21346 /* Same line for the same file that we've seen already.
21347 As a last check, for pr 17276, only record the line if the line
21348 has never had a non-zero discriminator. */
21349 if (!line_has_non_zero_discriminator)
21350 return 1;
21351 return 0;
21352 }
21353
21354 /* Use the CU's builder to record line number LINE beginning at
21355 address ADDRESS in the line table of subfile SUBFILE. */
21356
21357 static void
21358 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
21359 unsigned int line, CORE_ADDR address,
21360 struct dwarf2_cu *cu)
21361 {
21362 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
21363
21364 if (dwarf_line_debug)
21365 {
21366 fprintf_unfiltered (gdb_stdlog,
21367 "Recording line %u, file %s, address %s\n",
21368 line, lbasename (subfile->name),
21369 paddress (gdbarch, address));
21370 }
21371
21372 if (cu != nullptr)
21373 cu->get_builder ()->record_line (subfile, line, addr);
21374 }
21375
21376 /* Subroutine of dwarf_decode_lines_1 to simplify it.
21377 Mark the end of a set of line number records.
21378 The arguments are the same as for dwarf_record_line_1.
21379 If SUBFILE is NULL the request is ignored. */
21380
21381 static void
21382 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
21383 CORE_ADDR address, struct dwarf2_cu *cu)
21384 {
21385 if (subfile == NULL)
21386 return;
21387
21388 if (dwarf_line_debug)
21389 {
21390 fprintf_unfiltered (gdb_stdlog,
21391 "Finishing current line, file %s, address %s\n",
21392 lbasename (subfile->name),
21393 paddress (gdbarch, address));
21394 }
21395
21396 dwarf_record_line_1 (gdbarch, subfile, 0, address, cu);
21397 }
21398
21399 void
21400 lnp_state_machine::record_line (bool end_sequence)
21401 {
21402 if (dwarf_line_debug)
21403 {
21404 fprintf_unfiltered (gdb_stdlog,
21405 "Processing actual line %u: file %u,"
21406 " address %s, is_stmt %u, discrim %u\n",
21407 m_line, m_file,
21408 paddress (m_gdbarch, m_address),
21409 m_is_stmt, m_discriminator);
21410 }
21411
21412 file_entry *fe = current_file ();
21413
21414 if (fe == NULL)
21415 dwarf2_debug_line_missing_file_complaint ();
21416 /* For now we ignore lines not starting on an instruction boundary.
21417 But not when processing end_sequence for compatibility with the
21418 previous version of the code. */
21419 else if (m_op_index == 0 || end_sequence)
21420 {
21421 fe->included_p = 1;
21422 if (m_record_lines_p && (producer_is_codewarrior (m_cu) || m_is_stmt))
21423 {
21424 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
21425 || end_sequence)
21426 {
21427 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
21428 m_currently_recording_lines ? m_cu : nullptr);
21429 }
21430
21431 if (!end_sequence)
21432 {
21433 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
21434 m_line_has_non_zero_discriminator,
21435 m_last_subfile))
21436 {
21437 buildsym_compunit *builder = m_cu->get_builder ();
21438 dwarf_record_line_1 (m_gdbarch,
21439 builder->get_current_subfile (),
21440 m_line, m_address,
21441 m_currently_recording_lines ? m_cu : nullptr);
21442 }
21443 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
21444 m_last_line = m_line;
21445 }
21446 }
21447 }
21448 }
21449
21450 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
21451 line_header *lh, bool record_lines_p)
21452 {
21453 m_cu = cu;
21454 m_gdbarch = arch;
21455 m_record_lines_p = record_lines_p;
21456 m_line_header = lh;
21457
21458 m_currently_recording_lines = true;
21459
21460 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
21461 was a line entry for it so that the backend has a chance to adjust it
21462 and also record it in case it needs it. This is currently used by MIPS
21463 code, cf. `mips_adjust_dwarf2_line'. */
21464 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
21465 m_is_stmt = lh->default_is_stmt;
21466 m_discriminator = 0;
21467 }
21468
21469 void
21470 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
21471 const gdb_byte *line_ptr,
21472 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
21473 {
21474 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
21475 the pc range of the CU. However, we restrict the test to only ADDRESS
21476 values of zero to preserve GDB's previous behaviour which is to handle
21477 the specific case of a function being GC'd by the linker. */
21478
21479 if (address == 0 && address < unrelocated_lowpc)
21480 {
21481 /* This line table is for a function which has been
21482 GCd by the linker. Ignore it. PR gdb/12528 */
21483
21484 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21485 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
21486
21487 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
21488 line_offset, objfile_name (objfile));
21489 m_currently_recording_lines = false;
21490 /* Note: m_currently_recording_lines is left as false until we see
21491 DW_LNE_end_sequence. */
21492 }
21493 }
21494
21495 /* Subroutine of dwarf_decode_lines to simplify it.
21496 Process the line number information in LH.
21497 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
21498 program in order to set included_p for every referenced header. */
21499
21500 static void
21501 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
21502 const int decode_for_pst_p, CORE_ADDR lowpc)
21503 {
21504 const gdb_byte *line_ptr, *extended_end;
21505 const gdb_byte *line_end;
21506 unsigned int bytes_read, extended_len;
21507 unsigned char op_code, extended_op;
21508 CORE_ADDR baseaddr;
21509 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21510 bfd *abfd = objfile->obfd;
21511 struct gdbarch *gdbarch = get_objfile_arch (objfile);
21512 /* True if we're recording line info (as opposed to building partial
21513 symtabs and just interested in finding include files mentioned by
21514 the line number program). */
21515 bool record_lines_p = !decode_for_pst_p;
21516
21517 baseaddr = objfile->section_offsets[SECT_OFF_TEXT (objfile)];
21518
21519 line_ptr = lh->statement_program_start;
21520 line_end = lh->statement_program_end;
21521
21522 /* Read the statement sequences until there's nothing left. */
21523 while (line_ptr < line_end)
21524 {
21525 /* The DWARF line number program state machine. Reset the state
21526 machine at the start of each sequence. */
21527 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
21528 bool end_sequence = false;
21529
21530 if (record_lines_p)
21531 {
21532 /* Start a subfile for the current file of the state
21533 machine. */
21534 const file_entry *fe = state_machine.current_file ();
21535
21536 if (fe != NULL)
21537 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
21538 }
21539
21540 /* Decode the table. */
21541 while (line_ptr < line_end && !end_sequence)
21542 {
21543 op_code = read_1_byte (abfd, line_ptr);
21544 line_ptr += 1;
21545
21546 if (op_code >= lh->opcode_base)
21547 {
21548 /* Special opcode. */
21549 state_machine.handle_special_opcode (op_code);
21550 }
21551 else switch (op_code)
21552 {
21553 case DW_LNS_extended_op:
21554 extended_len = read_unsigned_leb128 (abfd, line_ptr,
21555 &bytes_read);
21556 line_ptr += bytes_read;
21557 extended_end = line_ptr + extended_len;
21558 extended_op = read_1_byte (abfd, line_ptr);
21559 line_ptr += 1;
21560 switch (extended_op)
21561 {
21562 case DW_LNE_end_sequence:
21563 state_machine.handle_end_sequence ();
21564 end_sequence = true;
21565 break;
21566 case DW_LNE_set_address:
21567 {
21568 CORE_ADDR address
21569 = read_address (abfd, line_ptr, cu, &bytes_read);
21570 line_ptr += bytes_read;
21571
21572 state_machine.check_line_address (cu, line_ptr,
21573 lowpc - baseaddr, address);
21574 state_machine.handle_set_address (baseaddr, address);
21575 }
21576 break;
21577 case DW_LNE_define_file:
21578 {
21579 const char *cur_file;
21580 unsigned int mod_time, length;
21581 dir_index dindex;
21582
21583 cur_file = read_direct_string (abfd, line_ptr,
21584 &bytes_read);
21585 line_ptr += bytes_read;
21586 dindex = (dir_index)
21587 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21588 line_ptr += bytes_read;
21589 mod_time =
21590 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21591 line_ptr += bytes_read;
21592 length =
21593 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21594 line_ptr += bytes_read;
21595 lh->add_file_name (cur_file, dindex, mod_time, length);
21596 }
21597 break;
21598 case DW_LNE_set_discriminator:
21599 {
21600 /* The discriminator is not interesting to the
21601 debugger; just ignore it. We still need to
21602 check its value though:
21603 if there are consecutive entries for the same
21604 (non-prologue) line we want to coalesce them.
21605 PR 17276. */
21606 unsigned int discr
21607 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21608 line_ptr += bytes_read;
21609
21610 state_machine.handle_set_discriminator (discr);
21611 }
21612 break;
21613 default:
21614 complaint (_("mangled .debug_line section"));
21615 return;
21616 }
21617 /* Make sure that we parsed the extended op correctly. If e.g.
21618 we expected a different address size than the producer used,
21619 we may have read the wrong number of bytes. */
21620 if (line_ptr != extended_end)
21621 {
21622 complaint (_("mangled .debug_line section"));
21623 return;
21624 }
21625 break;
21626 case DW_LNS_copy:
21627 state_machine.handle_copy ();
21628 break;
21629 case DW_LNS_advance_pc:
21630 {
21631 CORE_ADDR adjust
21632 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21633 line_ptr += bytes_read;
21634
21635 state_machine.handle_advance_pc (adjust);
21636 }
21637 break;
21638 case DW_LNS_advance_line:
21639 {
21640 int line_delta
21641 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
21642 line_ptr += bytes_read;
21643
21644 state_machine.handle_advance_line (line_delta);
21645 }
21646 break;
21647 case DW_LNS_set_file:
21648 {
21649 file_name_index file
21650 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21651 &bytes_read);
21652 line_ptr += bytes_read;
21653
21654 state_machine.handle_set_file (file);
21655 }
21656 break;
21657 case DW_LNS_set_column:
21658 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21659 line_ptr += bytes_read;
21660 break;
21661 case DW_LNS_negate_stmt:
21662 state_machine.handle_negate_stmt ();
21663 break;
21664 case DW_LNS_set_basic_block:
21665 break;
21666 /* Add to the address register of the state machine the
21667 address increment value corresponding to special opcode
21668 255. I.e., this value is scaled by the minimum
21669 instruction length since special opcode 255 would have
21670 scaled the increment. */
21671 case DW_LNS_const_add_pc:
21672 state_machine.handle_const_add_pc ();
21673 break;
21674 case DW_LNS_fixed_advance_pc:
21675 {
21676 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
21677 line_ptr += 2;
21678
21679 state_machine.handle_fixed_advance_pc (addr_adj);
21680 }
21681 break;
21682 default:
21683 {
21684 /* Unknown standard opcode, ignore it. */
21685 int i;
21686
21687 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
21688 {
21689 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21690 line_ptr += bytes_read;
21691 }
21692 }
21693 }
21694 }
21695
21696 if (!end_sequence)
21697 dwarf2_debug_line_missing_end_sequence_complaint ();
21698
21699 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21700 in which case we still finish recording the last line). */
21701 state_machine.record_line (true);
21702 }
21703 }
21704
21705 /* Decode the Line Number Program (LNP) for the given line_header
21706 structure and CU. The actual information extracted and the type
21707 of structures created from the LNP depends on the value of PST.
21708
21709 1. If PST is NULL, then this procedure uses the data from the program
21710 to create all necessary symbol tables, and their linetables.
21711
21712 2. If PST is not NULL, this procedure reads the program to determine
21713 the list of files included by the unit represented by PST, and
21714 builds all the associated partial symbol tables.
21715
21716 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21717 It is used for relative paths in the line table.
21718 NOTE: When processing partial symtabs (pst != NULL),
21719 comp_dir == pst->dirname.
21720
21721 NOTE: It is important that psymtabs have the same file name (via strcmp)
21722 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21723 symtab we don't use it in the name of the psymtabs we create.
21724 E.g. expand_line_sal requires this when finding psymtabs to expand.
21725 A good testcase for this is mb-inline.exp.
21726
21727 LOWPC is the lowest address in CU (or 0 if not known).
21728
21729 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21730 for its PC<->lines mapping information. Otherwise only the filename
21731 table is read in. */
21732
21733 static void
21734 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
21735 struct dwarf2_cu *cu, struct partial_symtab *pst,
21736 CORE_ADDR lowpc, int decode_mapping)
21737 {
21738 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21739 const int decode_for_pst_p = (pst != NULL);
21740
21741 if (decode_mapping)
21742 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
21743
21744 if (decode_for_pst_p)
21745 {
21746 /* Now that we're done scanning the Line Header Program, we can
21747 create the psymtab of each included file. */
21748 for (auto &file_entry : lh->file_names ())
21749 if (file_entry.included_p == 1)
21750 {
21751 gdb::unique_xmalloc_ptr<char> name_holder;
21752 const char *include_name =
21753 psymtab_include_file_name (lh, file_entry, pst,
21754 comp_dir, &name_holder);
21755 if (include_name != NULL)
21756 dwarf2_create_include_psymtab (include_name, pst, objfile);
21757 }
21758 }
21759 else
21760 {
21761 /* Make sure a symtab is created for every file, even files
21762 which contain only variables (i.e. no code with associated
21763 line numbers). */
21764 buildsym_compunit *builder = cu->get_builder ();
21765 struct compunit_symtab *cust = builder->get_compunit_symtab ();
21766
21767 for (auto &fe : lh->file_names ())
21768 {
21769 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
21770 if (builder->get_current_subfile ()->symtab == NULL)
21771 {
21772 builder->get_current_subfile ()->symtab
21773 = allocate_symtab (cust,
21774 builder->get_current_subfile ()->name);
21775 }
21776 fe.symtab = builder->get_current_subfile ()->symtab;
21777 }
21778 }
21779 }
21780
21781 /* Start a subfile for DWARF. FILENAME is the name of the file and
21782 DIRNAME the name of the source directory which contains FILENAME
21783 or NULL if not known.
21784 This routine tries to keep line numbers from identical absolute and
21785 relative file names in a common subfile.
21786
21787 Using the `list' example from the GDB testsuite, which resides in
21788 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21789 of /srcdir/list0.c yields the following debugging information for list0.c:
21790
21791 DW_AT_name: /srcdir/list0.c
21792 DW_AT_comp_dir: /compdir
21793 files.files[0].name: list0.h
21794 files.files[0].dir: /srcdir
21795 files.files[1].name: list0.c
21796 files.files[1].dir: /srcdir
21797
21798 The line number information for list0.c has to end up in a single
21799 subfile, so that `break /srcdir/list0.c:1' works as expected.
21800 start_subfile will ensure that this happens provided that we pass the
21801 concatenation of files.files[1].dir and files.files[1].name as the
21802 subfile's name. */
21803
21804 static void
21805 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
21806 const char *dirname)
21807 {
21808 gdb::unique_xmalloc_ptr<char> copy;
21809
21810 /* In order not to lose the line information directory,
21811 we concatenate it to the filename when it makes sense.
21812 Note that the Dwarf3 standard says (speaking of filenames in line
21813 information): ``The directory index is ignored for file names
21814 that represent full path names''. Thus ignoring dirname in the
21815 `else' branch below isn't an issue. */
21816
21817 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
21818 {
21819 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
21820 filename = copy.get ();
21821 }
21822
21823 cu->get_builder ()->start_subfile (filename);
21824 }
21825
21826 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
21827 buildsym_compunit constructor. */
21828
21829 struct compunit_symtab *
21830 dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
21831 CORE_ADDR low_pc)
21832 {
21833 gdb_assert (m_builder == nullptr);
21834
21835 m_builder.reset (new struct buildsym_compunit
21836 (per_cu->dwarf2_per_objfile->objfile,
21837 name, comp_dir, language, low_pc));
21838
21839 list_in_scope = get_builder ()->get_file_symbols ();
21840
21841 get_builder ()->record_debugformat ("DWARF 2");
21842 get_builder ()->record_producer (producer);
21843
21844 processing_has_namespace_info = false;
21845
21846 return get_builder ()->get_compunit_symtab ();
21847 }
21848
21849 static void
21850 var_decode_location (struct attribute *attr, struct symbol *sym,
21851 struct dwarf2_cu *cu)
21852 {
21853 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21854 struct comp_unit_head *cu_header = &cu->header;
21855
21856 /* NOTE drow/2003-01-30: There used to be a comment and some special
21857 code here to turn a symbol with DW_AT_external and a
21858 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21859 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21860 with some versions of binutils) where shared libraries could have
21861 relocations against symbols in their debug information - the
21862 minimal symbol would have the right address, but the debug info
21863 would not. It's no longer necessary, because we will explicitly
21864 apply relocations when we read in the debug information now. */
21865
21866 /* A DW_AT_location attribute with no contents indicates that a
21867 variable has been optimized away. */
21868 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
21869 {
21870 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21871 return;
21872 }
21873
21874 /* Handle one degenerate form of location expression specially, to
21875 preserve GDB's previous behavior when section offsets are
21876 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
21877 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
21878
21879 if (attr_form_is_block (attr)
21880 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
21881 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
21882 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
21883 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
21884 && (DW_BLOCK (attr)->size
21885 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
21886 {
21887 unsigned int dummy;
21888
21889 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
21890 SET_SYMBOL_VALUE_ADDRESS (sym,
21891 read_address (objfile->obfd,
21892 DW_BLOCK (attr)->data + 1,
21893 cu, &dummy));
21894 else
21895 SET_SYMBOL_VALUE_ADDRESS
21896 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
21897 &dummy));
21898 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
21899 fixup_symbol_section (sym, objfile);
21900 SET_SYMBOL_VALUE_ADDRESS
21901 (sym,
21902 SYMBOL_VALUE_ADDRESS (sym)
21903 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
21904 return;
21905 }
21906
21907 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21908 expression evaluator, and use LOC_COMPUTED only when necessary
21909 (i.e. when the value of a register or memory location is
21910 referenced, or a thread-local block, etc.). Then again, it might
21911 not be worthwhile. I'm assuming that it isn't unless performance
21912 or memory numbers show me otherwise. */
21913
21914 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
21915
21916 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
21917 cu->has_loclist = true;
21918 }
21919
21920 /* Given a pointer to a DWARF information entry, figure out if we need
21921 to make a symbol table entry for it, and if so, create a new entry
21922 and return a pointer to it.
21923 If TYPE is NULL, determine symbol type from the die, otherwise
21924 used the passed type.
21925 If SPACE is not NULL, use it to hold the new symbol. If it is
21926 NULL, allocate a new symbol on the objfile's obstack. */
21927
21928 static struct symbol *
21929 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21930 struct symbol *space)
21931 {
21932 struct dwarf2_per_objfile *dwarf2_per_objfile
21933 = cu->per_cu->dwarf2_per_objfile;
21934 struct objfile *objfile = dwarf2_per_objfile->objfile;
21935 struct gdbarch *gdbarch = get_objfile_arch (objfile);
21936 struct symbol *sym = NULL;
21937 const char *name;
21938 struct attribute *attr = NULL;
21939 struct attribute *attr2 = NULL;
21940 CORE_ADDR baseaddr;
21941 struct pending **list_to_add = NULL;
21942
21943 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
21944
21945 baseaddr = objfile->section_offsets[SECT_OFF_TEXT (objfile)];
21946
21947 name = dwarf2_name (die, cu);
21948 if (name)
21949 {
21950 const char *linkagename;
21951 int suppress_add = 0;
21952
21953 if (space)
21954 sym = space;
21955 else
21956 sym = allocate_symbol (objfile);
21957 OBJSTAT (objfile, n_syms++);
21958
21959 /* Cache this symbol's name and the name's demangled form (if any). */
21960 sym->set_language (cu->language, &objfile->objfile_obstack);
21961 linkagename = dwarf2_physname (name, die, cu);
21962 sym->compute_and_set_names (linkagename, false, objfile->per_bfd);
21963
21964 /* Fortran does not have mangling standard and the mangling does differ
21965 between gfortran, iFort etc. */
21966 if (cu->language == language_fortran
21967 && symbol_get_demangled_name (sym) == NULL)
21968 symbol_set_demangled_name (sym,
21969 dwarf2_full_name (name, die, cu),
21970 NULL);
21971
21972 /* Default assumptions.
21973 Use the passed type or decode it from the die. */
21974 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21975 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21976 if (type != NULL)
21977 SYMBOL_TYPE (sym) = type;
21978 else
21979 SYMBOL_TYPE (sym) = die_type (die, cu);
21980 attr = dwarf2_attr (die,
21981 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21982 cu);
21983 if (attr != nullptr)
21984 {
21985 SYMBOL_LINE (sym) = DW_UNSND (attr);
21986 }
21987
21988 attr = dwarf2_attr (die,
21989 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21990 cu);
21991 if (attr != nullptr)
21992 {
21993 file_name_index file_index = (file_name_index) DW_UNSND (attr);
21994 struct file_entry *fe;
21995
21996 if (cu->line_header != NULL)
21997 fe = cu->line_header->file_name_at (file_index);
21998 else
21999 fe = NULL;
22000
22001 if (fe == NULL)
22002 complaint (_("file index out of range"));
22003 else
22004 symbol_set_symtab (sym, fe->symtab);
22005 }
22006
22007 switch (die->tag)
22008 {
22009 case DW_TAG_label:
22010 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
22011 if (attr != nullptr)
22012 {
22013 CORE_ADDR addr;
22014
22015 addr = attr_value_as_address (attr);
22016 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
22017 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
22018 }
22019 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
22020 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
22021 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
22022 add_symbol_to_list (sym, cu->list_in_scope);
22023 break;
22024 case DW_TAG_subprogram:
22025 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
22026 finish_block. */
22027 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
22028 attr2 = dwarf2_attr (die, DW_AT_external, cu);
22029 if ((attr2 && (DW_UNSND (attr2) != 0))
22030 || cu->language == language_ada
22031 || cu->language == language_fortran)
22032 {
22033 /* Subprograms marked external are stored as a global symbol.
22034 Ada and Fortran subprograms, whether marked external or
22035 not, are always stored as a global symbol, because we want
22036 to be able to access them globally. For instance, we want
22037 to be able to break on a nested subprogram without having
22038 to specify the context. */
22039 list_to_add = cu->get_builder ()->get_global_symbols ();
22040 }
22041 else
22042 {
22043 list_to_add = cu->list_in_scope;
22044 }
22045 break;
22046 case DW_TAG_inlined_subroutine:
22047 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
22048 finish_block. */
22049 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
22050 SYMBOL_INLINED (sym) = 1;
22051 list_to_add = cu->list_in_scope;
22052 break;
22053 case DW_TAG_template_value_param:
22054 suppress_add = 1;
22055 /* Fall through. */
22056 case DW_TAG_constant:
22057 case DW_TAG_variable:
22058 case DW_TAG_member:
22059 /* Compilation with minimal debug info may result in
22060 variables with missing type entries. Change the
22061 misleading `void' type to something sensible. */
22062 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
22063 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
22064
22065 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22066 /* In the case of DW_TAG_member, we should only be called for
22067 static const members. */
22068 if (die->tag == DW_TAG_member)
22069 {
22070 /* dwarf2_add_field uses die_is_declaration,
22071 so we do the same. */
22072 gdb_assert (die_is_declaration (die, cu));
22073 gdb_assert (attr);
22074 }
22075 if (attr != nullptr)
22076 {
22077 dwarf2_const_value (attr, sym, cu);
22078 attr2 = dwarf2_attr (die, DW_AT_external, cu);
22079 if (!suppress_add)
22080 {
22081 if (attr2 && (DW_UNSND (attr2) != 0))
22082 list_to_add = cu->get_builder ()->get_global_symbols ();
22083 else
22084 list_to_add = cu->list_in_scope;
22085 }
22086 break;
22087 }
22088 attr = dwarf2_attr (die, DW_AT_location, cu);
22089 if (attr != nullptr)
22090 {
22091 var_decode_location (attr, sym, cu);
22092 attr2 = dwarf2_attr (die, DW_AT_external, cu);
22093
22094 /* Fortran explicitly imports any global symbols to the local
22095 scope by DW_TAG_common_block. */
22096 if (cu->language == language_fortran && die->parent
22097 && die->parent->tag == DW_TAG_common_block)
22098 attr2 = NULL;
22099
22100 if (SYMBOL_CLASS (sym) == LOC_STATIC
22101 && SYMBOL_VALUE_ADDRESS (sym) == 0
22102 && !dwarf2_per_objfile->has_section_at_zero)
22103 {
22104 /* When a static variable is eliminated by the linker,
22105 the corresponding debug information is not stripped
22106 out, but the variable address is set to null;
22107 do not add such variables into symbol table. */
22108 }
22109 else if (attr2 && (DW_UNSND (attr2) != 0))
22110 {
22111 if (SYMBOL_CLASS (sym) == LOC_STATIC
22112 && (objfile->flags & OBJF_MAINLINE) == 0
22113 && dwarf2_per_objfile->can_copy)
22114 {
22115 /* A global static variable might be subject to
22116 copy relocation. We first check for a local
22117 minsym, though, because maybe the symbol was
22118 marked hidden, in which case this would not
22119 apply. */
22120 bound_minimal_symbol found
22121 = (lookup_minimal_symbol_linkage
22122 (sym->linkage_name (), objfile));
22123 if (found.minsym != nullptr)
22124 sym->maybe_copied = 1;
22125 }
22126
22127 /* A variable with DW_AT_external is never static,
22128 but it may be block-scoped. */
22129 list_to_add
22130 = ((cu->list_in_scope
22131 == cu->get_builder ()->get_file_symbols ())
22132 ? cu->get_builder ()->get_global_symbols ()
22133 : cu->list_in_scope);
22134 }
22135 else
22136 list_to_add = cu->list_in_scope;
22137 }
22138 else
22139 {
22140 /* We do not know the address of this symbol.
22141 If it is an external symbol and we have type information
22142 for it, enter the symbol as a LOC_UNRESOLVED symbol.
22143 The address of the variable will then be determined from
22144 the minimal symbol table whenever the variable is
22145 referenced. */
22146 attr2 = dwarf2_attr (die, DW_AT_external, cu);
22147
22148 /* Fortran explicitly imports any global symbols to the local
22149 scope by DW_TAG_common_block. */
22150 if (cu->language == language_fortran && die->parent
22151 && die->parent->tag == DW_TAG_common_block)
22152 {
22153 /* SYMBOL_CLASS doesn't matter here because
22154 read_common_block is going to reset it. */
22155 if (!suppress_add)
22156 list_to_add = cu->list_in_scope;
22157 }
22158 else if (attr2 && (DW_UNSND (attr2) != 0)
22159 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
22160 {
22161 /* A variable with DW_AT_external is never static, but it
22162 may be block-scoped. */
22163 list_to_add
22164 = ((cu->list_in_scope
22165 == cu->get_builder ()->get_file_symbols ())
22166 ? cu->get_builder ()->get_global_symbols ()
22167 : cu->list_in_scope);
22168
22169 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
22170 }
22171 else if (!die_is_declaration (die, cu))
22172 {
22173 /* Use the default LOC_OPTIMIZED_OUT class. */
22174 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
22175 if (!suppress_add)
22176 list_to_add = cu->list_in_scope;
22177 }
22178 }
22179 break;
22180 case DW_TAG_formal_parameter:
22181 {
22182 /* If we are inside a function, mark this as an argument. If
22183 not, we might be looking at an argument to an inlined function
22184 when we do not have enough information to show inlined frames;
22185 pretend it's a local variable in that case so that the user can
22186 still see it. */
22187 struct context_stack *curr
22188 = cu->get_builder ()->get_current_context_stack ();
22189 if (curr != nullptr && curr->name != nullptr)
22190 SYMBOL_IS_ARGUMENT (sym) = 1;
22191 attr = dwarf2_attr (die, DW_AT_location, cu);
22192 if (attr != nullptr)
22193 {
22194 var_decode_location (attr, sym, cu);
22195 }
22196 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22197 if (attr != nullptr)
22198 {
22199 dwarf2_const_value (attr, sym, cu);
22200 }
22201
22202 list_to_add = cu->list_in_scope;
22203 }
22204 break;
22205 case DW_TAG_unspecified_parameters:
22206 /* From varargs functions; gdb doesn't seem to have any
22207 interest in this information, so just ignore it for now.
22208 (FIXME?) */
22209 break;
22210 case DW_TAG_template_type_param:
22211 suppress_add = 1;
22212 /* Fall through. */
22213 case DW_TAG_class_type:
22214 case DW_TAG_interface_type:
22215 case DW_TAG_structure_type:
22216 case DW_TAG_union_type:
22217 case DW_TAG_set_type:
22218 case DW_TAG_enumeration_type:
22219 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22220 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
22221
22222 {
22223 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
22224 really ever be static objects: otherwise, if you try
22225 to, say, break of a class's method and you're in a file
22226 which doesn't mention that class, it won't work unless
22227 the check for all static symbols in lookup_symbol_aux
22228 saves you. See the OtherFileClass tests in
22229 gdb.c++/namespace.exp. */
22230
22231 if (!suppress_add)
22232 {
22233 buildsym_compunit *builder = cu->get_builder ();
22234 list_to_add
22235 = (cu->list_in_scope == builder->get_file_symbols ()
22236 && cu->language == language_cplus
22237 ? builder->get_global_symbols ()
22238 : cu->list_in_scope);
22239
22240 /* The semantics of C++ state that "struct foo {
22241 ... }" also defines a typedef for "foo". */
22242 if (cu->language == language_cplus
22243 || cu->language == language_ada
22244 || cu->language == language_d
22245 || cu->language == language_rust)
22246 {
22247 /* The symbol's name is already allocated along
22248 with this objfile, so we don't need to
22249 duplicate it for the type. */
22250 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
22251 TYPE_NAME (SYMBOL_TYPE (sym)) = sym->search_name ();
22252 }
22253 }
22254 }
22255 break;
22256 case DW_TAG_typedef:
22257 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22258 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
22259 list_to_add = cu->list_in_scope;
22260 break;
22261 case DW_TAG_base_type:
22262 case DW_TAG_subrange_type:
22263 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22264 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
22265 list_to_add = cu->list_in_scope;
22266 break;
22267 case DW_TAG_enumerator:
22268 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22269 if (attr != nullptr)
22270 {
22271 dwarf2_const_value (attr, sym, cu);
22272 }
22273 {
22274 /* NOTE: carlton/2003-11-10: See comment above in the
22275 DW_TAG_class_type, etc. block. */
22276
22277 list_to_add
22278 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
22279 && cu->language == language_cplus
22280 ? cu->get_builder ()->get_global_symbols ()
22281 : cu->list_in_scope);
22282 }
22283 break;
22284 case DW_TAG_imported_declaration:
22285 case DW_TAG_namespace:
22286 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22287 list_to_add = cu->get_builder ()->get_global_symbols ();
22288 break;
22289 case DW_TAG_module:
22290 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22291 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
22292 list_to_add = cu->get_builder ()->get_global_symbols ();
22293 break;
22294 case DW_TAG_common_block:
22295 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
22296 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
22297 add_symbol_to_list (sym, cu->list_in_scope);
22298 break;
22299 default:
22300 /* Not a tag we recognize. Hopefully we aren't processing
22301 trash data, but since we must specifically ignore things
22302 we don't recognize, there is nothing else we should do at
22303 this point. */
22304 complaint (_("unsupported tag: '%s'"),
22305 dwarf_tag_name (die->tag));
22306 break;
22307 }
22308
22309 if (suppress_add)
22310 {
22311 sym->hash_next = objfile->template_symbols;
22312 objfile->template_symbols = sym;
22313 list_to_add = NULL;
22314 }
22315
22316 if (list_to_add != NULL)
22317 add_symbol_to_list (sym, list_to_add);
22318
22319 /* For the benefit of old versions of GCC, check for anonymous
22320 namespaces based on the demangled name. */
22321 if (!cu->processing_has_namespace_info
22322 && cu->language == language_cplus)
22323 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
22324 }
22325 return (sym);
22326 }
22327
22328 /* Given an attr with a DW_FORM_dataN value in host byte order,
22329 zero-extend it as appropriate for the symbol's type. The DWARF
22330 standard (v4) is not entirely clear about the meaning of using
22331 DW_FORM_dataN for a constant with a signed type, where the type is
22332 wider than the data. The conclusion of a discussion on the DWARF
22333 list was that this is unspecified. We choose to always zero-extend
22334 because that is the interpretation long in use by GCC. */
22335
22336 static gdb_byte *
22337 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
22338 struct dwarf2_cu *cu, LONGEST *value, int bits)
22339 {
22340 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22341 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
22342 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
22343 LONGEST l = DW_UNSND (attr);
22344
22345 if (bits < sizeof (*value) * 8)
22346 {
22347 l &= ((LONGEST) 1 << bits) - 1;
22348 *value = l;
22349 }
22350 else if (bits == sizeof (*value) * 8)
22351 *value = l;
22352 else
22353 {
22354 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
22355 store_unsigned_integer (bytes, bits / 8, byte_order, l);
22356 return bytes;
22357 }
22358
22359 return NULL;
22360 }
22361
22362 /* Read a constant value from an attribute. Either set *VALUE, or if
22363 the value does not fit in *VALUE, set *BYTES - either already
22364 allocated on the objfile obstack, or newly allocated on OBSTACK,
22365 or, set *BATON, if we translated the constant to a location
22366 expression. */
22367
22368 static void
22369 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
22370 const char *name, struct obstack *obstack,
22371 struct dwarf2_cu *cu,
22372 LONGEST *value, const gdb_byte **bytes,
22373 struct dwarf2_locexpr_baton **baton)
22374 {
22375 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22376 struct comp_unit_head *cu_header = &cu->header;
22377 struct dwarf_block *blk;
22378 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
22379 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22380
22381 *value = 0;
22382 *bytes = NULL;
22383 *baton = NULL;
22384
22385 switch (attr->form)
22386 {
22387 case DW_FORM_addr:
22388 case DW_FORM_addrx:
22389 case DW_FORM_GNU_addr_index:
22390 {
22391 gdb_byte *data;
22392
22393 if (TYPE_LENGTH (type) != cu_header->addr_size)
22394 dwarf2_const_value_length_mismatch_complaint (name,
22395 cu_header->addr_size,
22396 TYPE_LENGTH (type));
22397 /* Symbols of this form are reasonably rare, so we just
22398 piggyback on the existing location code rather than writing
22399 a new implementation of symbol_computed_ops. */
22400 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
22401 (*baton)->per_cu = cu->per_cu;
22402 gdb_assert ((*baton)->per_cu);
22403
22404 (*baton)->size = 2 + cu_header->addr_size;
22405 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
22406 (*baton)->data = data;
22407
22408 data[0] = DW_OP_addr;
22409 store_unsigned_integer (&data[1], cu_header->addr_size,
22410 byte_order, DW_ADDR (attr));
22411 data[cu_header->addr_size + 1] = DW_OP_stack_value;
22412 }
22413 break;
22414 case DW_FORM_string:
22415 case DW_FORM_strp:
22416 case DW_FORM_strx:
22417 case DW_FORM_GNU_str_index:
22418 case DW_FORM_GNU_strp_alt:
22419 /* DW_STRING is already allocated on the objfile obstack, point
22420 directly to it. */
22421 *bytes = (const gdb_byte *) DW_STRING (attr);
22422 break;
22423 case DW_FORM_block1:
22424 case DW_FORM_block2:
22425 case DW_FORM_block4:
22426 case DW_FORM_block:
22427 case DW_FORM_exprloc:
22428 case DW_FORM_data16:
22429 blk = DW_BLOCK (attr);
22430 if (TYPE_LENGTH (type) != blk->size)
22431 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
22432 TYPE_LENGTH (type));
22433 *bytes = blk->data;
22434 break;
22435
22436 /* The DW_AT_const_value attributes are supposed to carry the
22437 symbol's value "represented as it would be on the target
22438 architecture." By the time we get here, it's already been
22439 converted to host endianness, so we just need to sign- or
22440 zero-extend it as appropriate. */
22441 case DW_FORM_data1:
22442 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
22443 break;
22444 case DW_FORM_data2:
22445 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
22446 break;
22447 case DW_FORM_data4:
22448 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
22449 break;
22450 case DW_FORM_data8:
22451 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
22452 break;
22453
22454 case DW_FORM_sdata:
22455 case DW_FORM_implicit_const:
22456 *value = DW_SND (attr);
22457 break;
22458
22459 case DW_FORM_udata:
22460 *value = DW_UNSND (attr);
22461 break;
22462
22463 default:
22464 complaint (_("unsupported const value attribute form: '%s'"),
22465 dwarf_form_name (attr->form));
22466 *value = 0;
22467 break;
22468 }
22469 }
22470
22471
22472 /* Copy constant value from an attribute to a symbol. */
22473
22474 static void
22475 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
22476 struct dwarf2_cu *cu)
22477 {
22478 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22479 LONGEST value;
22480 const gdb_byte *bytes;
22481 struct dwarf2_locexpr_baton *baton;
22482
22483 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
22484 sym->print_name (),
22485 &objfile->objfile_obstack, cu,
22486 &value, &bytes, &baton);
22487
22488 if (baton != NULL)
22489 {
22490 SYMBOL_LOCATION_BATON (sym) = baton;
22491 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
22492 }
22493 else if (bytes != NULL)
22494 {
22495 SYMBOL_VALUE_BYTES (sym) = bytes;
22496 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
22497 }
22498 else
22499 {
22500 SYMBOL_VALUE (sym) = value;
22501 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
22502 }
22503 }
22504
22505 /* Return the type of the die in question using its DW_AT_type attribute. */
22506
22507 static struct type *
22508 die_type (struct die_info *die, struct dwarf2_cu *cu)
22509 {
22510 struct attribute *type_attr;
22511
22512 type_attr = dwarf2_attr (die, DW_AT_type, cu);
22513 if (!type_attr)
22514 {
22515 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22516 /* A missing DW_AT_type represents a void type. */
22517 return objfile_type (objfile)->builtin_void;
22518 }
22519
22520 return lookup_die_type (die, type_attr, cu);
22521 }
22522
22523 /* True iff CU's producer generates GNAT Ada auxiliary information
22524 that allows to find parallel types through that information instead
22525 of having to do expensive parallel lookups by type name. */
22526
22527 static int
22528 need_gnat_info (struct dwarf2_cu *cu)
22529 {
22530 /* Assume that the Ada compiler was GNAT, which always produces
22531 the auxiliary information. */
22532 return (cu->language == language_ada);
22533 }
22534
22535 /* Return the auxiliary type of the die in question using its
22536 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
22537 attribute is not present. */
22538
22539 static struct type *
22540 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
22541 {
22542 struct attribute *type_attr;
22543
22544 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
22545 if (!type_attr)
22546 return NULL;
22547
22548 return lookup_die_type (die, type_attr, cu);
22549 }
22550
22551 /* If DIE has a descriptive_type attribute, then set the TYPE's
22552 descriptive type accordingly. */
22553
22554 static void
22555 set_descriptive_type (struct type *type, struct die_info *die,
22556 struct dwarf2_cu *cu)
22557 {
22558 struct type *descriptive_type = die_descriptive_type (die, cu);
22559
22560 if (descriptive_type)
22561 {
22562 ALLOCATE_GNAT_AUX_TYPE (type);
22563 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
22564 }
22565 }
22566
22567 /* Return the containing type of the die in question using its
22568 DW_AT_containing_type attribute. */
22569
22570 static struct type *
22571 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
22572 {
22573 struct attribute *type_attr;
22574 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22575
22576 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
22577 if (!type_attr)
22578 error (_("Dwarf Error: Problem turning containing type into gdb type "
22579 "[in module %s]"), objfile_name (objfile));
22580
22581 return lookup_die_type (die, type_attr, cu);
22582 }
22583
22584 /* Return an error marker type to use for the ill formed type in DIE/CU. */
22585
22586 static struct type *
22587 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22588 {
22589 struct dwarf2_per_objfile *dwarf2_per_objfile
22590 = cu->per_cu->dwarf2_per_objfile;
22591 struct objfile *objfile = dwarf2_per_objfile->objfile;
22592 char *saved;
22593
22594 std::string message
22595 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
22596 objfile_name (objfile),
22597 sect_offset_str (cu->header.sect_off),
22598 sect_offset_str (die->sect_off));
22599 saved = obstack_strdup (&objfile->objfile_obstack, message);
22600
22601 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
22602 }
22603
22604 /* Look up the type of DIE in CU using its type attribute ATTR.
22605 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22606 DW_AT_containing_type.
22607 If there is no type substitute an error marker. */
22608
22609 static struct type *
22610 lookup_die_type (struct die_info *die, const struct attribute *attr,
22611 struct dwarf2_cu *cu)
22612 {
22613 struct dwarf2_per_objfile *dwarf2_per_objfile
22614 = cu->per_cu->dwarf2_per_objfile;
22615 struct objfile *objfile = dwarf2_per_objfile->objfile;
22616 struct type *this_type;
22617
22618 gdb_assert (attr->name == DW_AT_type
22619 || attr->name == DW_AT_GNAT_descriptive_type
22620 || attr->name == DW_AT_containing_type);
22621
22622 /* First see if we have it cached. */
22623
22624 if (attr->form == DW_FORM_GNU_ref_alt)
22625 {
22626 struct dwarf2_per_cu_data *per_cu;
22627 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
22628
22629 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
22630 dwarf2_per_objfile);
22631 this_type = get_die_type_at_offset (sect_off, per_cu);
22632 }
22633 else if (attr_form_is_ref (attr))
22634 {
22635 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
22636
22637 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
22638 }
22639 else if (attr->form == DW_FORM_ref_sig8)
22640 {
22641 ULONGEST signature = DW_SIGNATURE (attr);
22642
22643 return get_signatured_type (die, signature, cu);
22644 }
22645 else
22646 {
22647 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
22648 " at %s [in module %s]"),
22649 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
22650 objfile_name (objfile));
22651 return build_error_marker_type (cu, die);
22652 }
22653
22654 /* If not cached we need to read it in. */
22655
22656 if (this_type == NULL)
22657 {
22658 struct die_info *type_die = NULL;
22659 struct dwarf2_cu *type_cu = cu;
22660
22661 if (attr_form_is_ref (attr))
22662 type_die = follow_die_ref (die, attr, &type_cu);
22663 if (type_die == NULL)
22664 return build_error_marker_type (cu, die);
22665 /* If we find the type now, it's probably because the type came
22666 from an inter-CU reference and the type's CU got expanded before
22667 ours. */
22668 this_type = read_type_die (type_die, type_cu);
22669 }
22670
22671 /* If we still don't have a type use an error marker. */
22672
22673 if (this_type == NULL)
22674 return build_error_marker_type (cu, die);
22675
22676 return this_type;
22677 }
22678
22679 /* Return the type in DIE, CU.
22680 Returns NULL for invalid types.
22681
22682 This first does a lookup in die_type_hash,
22683 and only reads the die in if necessary.
22684
22685 NOTE: This can be called when reading in partial or full symbols. */
22686
22687 static struct type *
22688 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
22689 {
22690 struct type *this_type;
22691
22692 this_type = get_die_type (die, cu);
22693 if (this_type)
22694 return this_type;
22695
22696 return read_type_die_1 (die, cu);
22697 }
22698
22699 /* Read the type in DIE, CU.
22700 Returns NULL for invalid types. */
22701
22702 static struct type *
22703 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22704 {
22705 struct type *this_type = NULL;
22706
22707 switch (die->tag)
22708 {
22709 case DW_TAG_class_type:
22710 case DW_TAG_interface_type:
22711 case DW_TAG_structure_type:
22712 case DW_TAG_union_type:
22713 this_type = read_structure_type (die, cu);
22714 break;
22715 case DW_TAG_enumeration_type:
22716 this_type = read_enumeration_type (die, cu);
22717 break;
22718 case DW_TAG_subprogram:
22719 case DW_TAG_subroutine_type:
22720 case DW_TAG_inlined_subroutine:
22721 this_type = read_subroutine_type (die, cu);
22722 break;
22723 case DW_TAG_array_type:
22724 this_type = read_array_type (die, cu);
22725 break;
22726 case DW_TAG_set_type:
22727 this_type = read_set_type (die, cu);
22728 break;
22729 case DW_TAG_pointer_type:
22730 this_type = read_tag_pointer_type (die, cu);
22731 break;
22732 case DW_TAG_ptr_to_member_type:
22733 this_type = read_tag_ptr_to_member_type (die, cu);
22734 break;
22735 case DW_TAG_reference_type:
22736 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22737 break;
22738 case DW_TAG_rvalue_reference_type:
22739 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
22740 break;
22741 case DW_TAG_const_type:
22742 this_type = read_tag_const_type (die, cu);
22743 break;
22744 case DW_TAG_volatile_type:
22745 this_type = read_tag_volatile_type (die, cu);
22746 break;
22747 case DW_TAG_restrict_type:
22748 this_type = read_tag_restrict_type (die, cu);
22749 break;
22750 case DW_TAG_string_type:
22751 this_type = read_tag_string_type (die, cu);
22752 break;
22753 case DW_TAG_typedef:
22754 this_type = read_typedef (die, cu);
22755 break;
22756 case DW_TAG_subrange_type:
22757 this_type = read_subrange_type (die, cu);
22758 break;
22759 case DW_TAG_base_type:
22760 this_type = read_base_type (die, cu);
22761 break;
22762 case DW_TAG_unspecified_type:
22763 this_type = read_unspecified_type (die, cu);
22764 break;
22765 case DW_TAG_namespace:
22766 this_type = read_namespace_type (die, cu);
22767 break;
22768 case DW_TAG_module:
22769 this_type = read_module_type (die, cu);
22770 break;
22771 case DW_TAG_atomic_type:
22772 this_type = read_tag_atomic_type (die, cu);
22773 break;
22774 default:
22775 complaint (_("unexpected tag in read_type_die: '%s'"),
22776 dwarf_tag_name (die->tag));
22777 break;
22778 }
22779
22780 return this_type;
22781 }
22782
22783 /* See if we can figure out if the class lives in a namespace. We do
22784 this by looking for a member function; its demangled name will
22785 contain namespace info, if there is any.
22786 Return the computed name or NULL.
22787 Space for the result is allocated on the objfile's obstack.
22788 This is the full-die version of guess_partial_die_structure_name.
22789 In this case we know DIE has no useful parent. */
22790
22791 static const char *
22792 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22793 {
22794 struct die_info *spec_die;
22795 struct dwarf2_cu *spec_cu;
22796 struct die_info *child;
22797 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22798
22799 spec_cu = cu;
22800 spec_die = die_specification (die, &spec_cu);
22801 if (spec_die != NULL)
22802 {
22803 die = spec_die;
22804 cu = spec_cu;
22805 }
22806
22807 for (child = die->child;
22808 child != NULL;
22809 child = child->sibling)
22810 {
22811 if (child->tag == DW_TAG_subprogram)
22812 {
22813 const char *linkage_name = dw2_linkage_name (child, cu);
22814
22815 if (linkage_name != NULL)
22816 {
22817 gdb::unique_xmalloc_ptr<char> actual_name
22818 (language_class_name_from_physname (cu->language_defn,
22819 linkage_name));
22820 const char *name = NULL;
22821
22822 if (actual_name != NULL)
22823 {
22824 const char *die_name = dwarf2_name (die, cu);
22825
22826 if (die_name != NULL
22827 && strcmp (die_name, actual_name.get ()) != 0)
22828 {
22829 /* Strip off the class name from the full name.
22830 We want the prefix. */
22831 int die_name_len = strlen (die_name);
22832 int actual_name_len = strlen (actual_name.get ());
22833 const char *ptr = actual_name.get ();
22834
22835 /* Test for '::' as a sanity check. */
22836 if (actual_name_len > die_name_len + 2
22837 && ptr[actual_name_len - die_name_len - 1] == ':')
22838 name = obstack_strndup (
22839 &objfile->per_bfd->storage_obstack,
22840 ptr, actual_name_len - die_name_len - 2);
22841 }
22842 }
22843 return name;
22844 }
22845 }
22846 }
22847
22848 return NULL;
22849 }
22850
22851 /* GCC might emit a nameless typedef that has a linkage name. Determine the
22852 prefix part in such case. See
22853 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22854
22855 static const char *
22856 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22857 {
22858 struct attribute *attr;
22859 const char *base;
22860
22861 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22862 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22863 return NULL;
22864
22865 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
22866 return NULL;
22867
22868 attr = dw2_linkage_name_attr (die, cu);
22869 if (attr == NULL || DW_STRING (attr) == NULL)
22870 return NULL;
22871
22872 /* dwarf2_name had to be already called. */
22873 gdb_assert (DW_STRING_IS_CANONICAL (attr));
22874
22875 /* Strip the base name, keep any leading namespaces/classes. */
22876 base = strrchr (DW_STRING (attr), ':');
22877 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
22878 return "";
22879
22880 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22881 return obstack_strndup (&objfile->per_bfd->storage_obstack,
22882 DW_STRING (attr),
22883 &base[-1] - DW_STRING (attr));
22884 }
22885
22886 /* Return the name of the namespace/class that DIE is defined within,
22887 or "" if we can't tell. The caller should not xfree the result.
22888
22889 For example, if we're within the method foo() in the following
22890 code:
22891
22892 namespace N {
22893 class C {
22894 void foo () {
22895 }
22896 };
22897 }
22898
22899 then determine_prefix on foo's die will return "N::C". */
22900
22901 static const char *
22902 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
22903 {
22904 struct dwarf2_per_objfile *dwarf2_per_objfile
22905 = cu->per_cu->dwarf2_per_objfile;
22906 struct die_info *parent, *spec_die;
22907 struct dwarf2_cu *spec_cu;
22908 struct type *parent_type;
22909 const char *retval;
22910
22911 if (cu->language != language_cplus
22912 && cu->language != language_fortran && cu->language != language_d
22913 && cu->language != language_rust)
22914 return "";
22915
22916 retval = anonymous_struct_prefix (die, cu);
22917 if (retval)
22918 return retval;
22919
22920 /* We have to be careful in the presence of DW_AT_specification.
22921 For example, with GCC 3.4, given the code
22922
22923 namespace N {
22924 void foo() {
22925 // Definition of N::foo.
22926 }
22927 }
22928
22929 then we'll have a tree of DIEs like this:
22930
22931 1: DW_TAG_compile_unit
22932 2: DW_TAG_namespace // N
22933 3: DW_TAG_subprogram // declaration of N::foo
22934 4: DW_TAG_subprogram // definition of N::foo
22935 DW_AT_specification // refers to die #3
22936
22937 Thus, when processing die #4, we have to pretend that we're in
22938 the context of its DW_AT_specification, namely the contex of die
22939 #3. */
22940 spec_cu = cu;
22941 spec_die = die_specification (die, &spec_cu);
22942 if (spec_die == NULL)
22943 parent = die->parent;
22944 else
22945 {
22946 parent = spec_die->parent;
22947 cu = spec_cu;
22948 }
22949
22950 if (parent == NULL)
22951 return "";
22952 else if (parent->building_fullname)
22953 {
22954 const char *name;
22955 const char *parent_name;
22956
22957 /* It has been seen on RealView 2.2 built binaries,
22958 DW_TAG_template_type_param types actually _defined_ as
22959 children of the parent class:
22960
22961 enum E {};
22962 template class <class Enum> Class{};
22963 Class<enum E> class_e;
22964
22965 1: DW_TAG_class_type (Class)
22966 2: DW_TAG_enumeration_type (E)
22967 3: DW_TAG_enumerator (enum1:0)
22968 3: DW_TAG_enumerator (enum2:1)
22969 ...
22970 2: DW_TAG_template_type_param
22971 DW_AT_type DW_FORM_ref_udata (E)
22972
22973 Besides being broken debug info, it can put GDB into an
22974 infinite loop. Consider:
22975
22976 When we're building the full name for Class<E>, we'll start
22977 at Class, and go look over its template type parameters,
22978 finding E. We'll then try to build the full name of E, and
22979 reach here. We're now trying to build the full name of E,
22980 and look over the parent DIE for containing scope. In the
22981 broken case, if we followed the parent DIE of E, we'd again
22982 find Class, and once again go look at its template type
22983 arguments, etc., etc. Simply don't consider such parent die
22984 as source-level parent of this die (it can't be, the language
22985 doesn't allow it), and break the loop here. */
22986 name = dwarf2_name (die, cu);
22987 parent_name = dwarf2_name (parent, cu);
22988 complaint (_("template param type '%s' defined within parent '%s'"),
22989 name ? name : "<unknown>",
22990 parent_name ? parent_name : "<unknown>");
22991 return "";
22992 }
22993 else
22994 switch (parent->tag)
22995 {
22996 case DW_TAG_namespace:
22997 parent_type = read_type_die (parent, cu);
22998 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22999 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
23000 Work around this problem here. */
23001 if (cu->language == language_cplus
23002 && strcmp (TYPE_NAME (parent_type), "::") == 0)
23003 return "";
23004 /* We give a name to even anonymous namespaces. */
23005 return TYPE_NAME (parent_type);
23006 case DW_TAG_class_type:
23007 case DW_TAG_interface_type:
23008 case DW_TAG_structure_type:
23009 case DW_TAG_union_type:
23010 case DW_TAG_module:
23011 parent_type = read_type_die (parent, cu);
23012 if (TYPE_NAME (parent_type) != NULL)
23013 return TYPE_NAME (parent_type);
23014 else
23015 /* An anonymous structure is only allowed non-static data
23016 members; no typedefs, no member functions, et cetera.
23017 So it does not need a prefix. */
23018 return "";
23019 case DW_TAG_compile_unit:
23020 case DW_TAG_partial_unit:
23021 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
23022 if (cu->language == language_cplus
23023 && !dwarf2_per_objfile->types.empty ()
23024 && die->child != NULL
23025 && (die->tag == DW_TAG_class_type
23026 || die->tag == DW_TAG_structure_type
23027 || die->tag == DW_TAG_union_type))
23028 {
23029 const char *name = guess_full_die_structure_name (die, cu);
23030 if (name != NULL)
23031 return name;
23032 }
23033 return "";
23034 case DW_TAG_subprogram:
23035 /* Nested subroutines in Fortran get a prefix with the name
23036 of the parent's subroutine. */
23037 if (cu->language == language_fortran)
23038 {
23039 if ((die->tag == DW_TAG_subprogram)
23040 && (dwarf2_name (parent, cu) != NULL))
23041 return dwarf2_name (parent, cu);
23042 }
23043 return determine_prefix (parent, cu);
23044 case DW_TAG_enumeration_type:
23045 parent_type = read_type_die (parent, cu);
23046 if (TYPE_DECLARED_CLASS (parent_type))
23047 {
23048 if (TYPE_NAME (parent_type) != NULL)
23049 return TYPE_NAME (parent_type);
23050 return "";
23051 }
23052 /* Fall through. */
23053 default:
23054 return determine_prefix (parent, cu);
23055 }
23056 }
23057
23058 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
23059 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
23060 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
23061 an obconcat, otherwise allocate storage for the result. The CU argument is
23062 used to determine the language and hence, the appropriate separator. */
23063
23064 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
23065
23066 static char *
23067 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
23068 int physname, struct dwarf2_cu *cu)
23069 {
23070 const char *lead = "";
23071 const char *sep;
23072
23073 if (suffix == NULL || suffix[0] == '\0'
23074 || prefix == NULL || prefix[0] == '\0')
23075 sep = "";
23076 else if (cu->language == language_d)
23077 {
23078 /* For D, the 'main' function could be defined in any module, but it
23079 should never be prefixed. */
23080 if (strcmp (suffix, "D main") == 0)
23081 {
23082 prefix = "";
23083 sep = "";
23084 }
23085 else
23086 sep = ".";
23087 }
23088 else if (cu->language == language_fortran && physname)
23089 {
23090 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
23091 DW_AT_MIPS_linkage_name is preferred and used instead. */
23092
23093 lead = "__";
23094 sep = "_MOD_";
23095 }
23096 else
23097 sep = "::";
23098
23099 if (prefix == NULL)
23100 prefix = "";
23101 if (suffix == NULL)
23102 suffix = "";
23103
23104 if (obs == NULL)
23105 {
23106 char *retval
23107 = ((char *)
23108 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
23109
23110 strcpy (retval, lead);
23111 strcat (retval, prefix);
23112 strcat (retval, sep);
23113 strcat (retval, suffix);
23114 return retval;
23115 }
23116 else
23117 {
23118 /* We have an obstack. */
23119 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
23120 }
23121 }
23122
23123 /* Return sibling of die, NULL if no sibling. */
23124
23125 static struct die_info *
23126 sibling_die (struct die_info *die)
23127 {
23128 return die->sibling;
23129 }
23130
23131 /* Get name of a die, return NULL if not found. */
23132
23133 static const char *
23134 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
23135 struct obstack *obstack)
23136 {
23137 if (name && cu->language == language_cplus)
23138 {
23139 std::string canon_name = cp_canonicalize_string (name);
23140
23141 if (!canon_name.empty ())
23142 {
23143 if (canon_name != name)
23144 name = obstack_strdup (obstack, canon_name);
23145 }
23146 }
23147
23148 return name;
23149 }
23150
23151 /* Get name of a die, return NULL if not found.
23152 Anonymous namespaces are converted to their magic string. */
23153
23154 static const char *
23155 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
23156 {
23157 struct attribute *attr;
23158 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
23159
23160 attr = dwarf2_attr (die, DW_AT_name, cu);
23161 if ((!attr || !DW_STRING (attr))
23162 && die->tag != DW_TAG_namespace
23163 && die->tag != DW_TAG_class_type
23164 && die->tag != DW_TAG_interface_type
23165 && die->tag != DW_TAG_structure_type
23166 && die->tag != DW_TAG_union_type)
23167 return NULL;
23168
23169 switch (die->tag)
23170 {
23171 case DW_TAG_compile_unit:
23172 case DW_TAG_partial_unit:
23173 /* Compilation units have a DW_AT_name that is a filename, not
23174 a source language identifier. */
23175 case DW_TAG_enumeration_type:
23176 case DW_TAG_enumerator:
23177 /* These tags always have simple identifiers already; no need
23178 to canonicalize them. */
23179 return DW_STRING (attr);
23180
23181 case DW_TAG_namespace:
23182 if (attr != NULL && DW_STRING (attr) != NULL)
23183 return DW_STRING (attr);
23184 return CP_ANONYMOUS_NAMESPACE_STR;
23185
23186 case DW_TAG_class_type:
23187 case DW_TAG_interface_type:
23188 case DW_TAG_structure_type:
23189 case DW_TAG_union_type:
23190 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
23191 structures or unions. These were of the form "._%d" in GCC 4.1,
23192 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
23193 and GCC 4.4. We work around this problem by ignoring these. */
23194 if (attr && DW_STRING (attr)
23195 && (startswith (DW_STRING (attr), "._")
23196 || startswith (DW_STRING (attr), "<anonymous")))
23197 return NULL;
23198
23199 /* GCC might emit a nameless typedef that has a linkage name. See
23200 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
23201 if (!attr || DW_STRING (attr) == NULL)
23202 {
23203 attr = dw2_linkage_name_attr (die, cu);
23204 if (attr == NULL || DW_STRING (attr) == NULL)
23205 return NULL;
23206
23207 /* Avoid demangling DW_STRING (attr) the second time on a second
23208 call for the same DIE. */
23209 if (!DW_STRING_IS_CANONICAL (attr))
23210 {
23211 gdb::unique_xmalloc_ptr<char> demangled
23212 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
23213
23214 const char *base;
23215
23216 /* FIXME: we already did this for the partial symbol... */
23217 DW_STRING (attr)
23218 = obstack_strdup (&objfile->per_bfd->storage_obstack,
23219 demangled.get ());
23220 DW_STRING_IS_CANONICAL (attr) = 1;
23221
23222 /* Strip any leading namespaces/classes, keep only the base name.
23223 DW_AT_name for named DIEs does not contain the prefixes. */
23224 base = strrchr (DW_STRING (attr), ':');
23225 if (base && base > DW_STRING (attr) && base[-1] == ':')
23226 return &base[1];
23227 else
23228 return DW_STRING (attr);
23229 }
23230 }
23231 break;
23232
23233 default:
23234 break;
23235 }
23236
23237 if (!DW_STRING_IS_CANONICAL (attr))
23238 {
23239 DW_STRING (attr)
23240 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
23241 &objfile->per_bfd->storage_obstack);
23242 DW_STRING_IS_CANONICAL (attr) = 1;
23243 }
23244 return DW_STRING (attr);
23245 }
23246
23247 /* Return the die that this die in an extension of, or NULL if there
23248 is none. *EXT_CU is the CU containing DIE on input, and the CU
23249 containing the return value on output. */
23250
23251 static struct die_info *
23252 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
23253 {
23254 struct attribute *attr;
23255
23256 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
23257 if (attr == NULL)
23258 return NULL;
23259
23260 return follow_die_ref (die, attr, ext_cu);
23261 }
23262
23263 /* A convenience function that returns an "unknown" DWARF name,
23264 including the value of V. STR is the name of the entity being
23265 printed, e.g., "TAG". */
23266
23267 static const char *
23268 dwarf_unknown (const char *str, unsigned v)
23269 {
23270 char *cell = get_print_cell ();
23271 xsnprintf (cell, PRINT_CELL_SIZE, "DW_%s_<unknown: %u>", str, v);
23272 return cell;
23273 }
23274
23275 /* Convert a DIE tag into its string name. */
23276
23277 static const char *
23278 dwarf_tag_name (unsigned tag)
23279 {
23280 const char *name = get_DW_TAG_name (tag);
23281
23282 if (name == NULL)
23283 return dwarf_unknown ("TAG", tag);
23284
23285 return name;
23286 }
23287
23288 /* Convert a DWARF attribute code into its string name. */
23289
23290 static const char *
23291 dwarf_attr_name (unsigned attr)
23292 {
23293 const char *name;
23294
23295 #ifdef MIPS /* collides with DW_AT_HP_block_index */
23296 if (attr == DW_AT_MIPS_fde)
23297 return "DW_AT_MIPS_fde";
23298 #else
23299 if (attr == DW_AT_HP_block_index)
23300 return "DW_AT_HP_block_index";
23301 #endif
23302
23303 name = get_DW_AT_name (attr);
23304
23305 if (name == NULL)
23306 return dwarf_unknown ("AT", attr);
23307
23308 return name;
23309 }
23310
23311 /* Convert a unit type to corresponding DW_UT name. */
23312
23313 static const char *
23314 dwarf_unit_type_name (int unit_type) {
23315 switch (unit_type)
23316 {
23317 case 0x01:
23318 return "DW_UT_compile (0x01)";
23319 case 0x02:
23320 return "DW_UT_type (0x02)";
23321 case 0x03:
23322 return "DW_UT_partial (0x03)";
23323 case 0x04:
23324 return "DW_UT_skeleton (0x04)";
23325 case 0x05:
23326 return "DW_UT_split_compile (0x05)";
23327 case 0x06:
23328 return "DW_UT_split_type (0x06)";
23329 case 0x80:
23330 return "DW_UT_lo_user (0x80)";
23331 case 0xff:
23332 return "DW_UT_hi_user (0xff)";
23333 default:
23334 return nullptr;
23335 }
23336 }
23337
23338 /* Convert a DWARF value form code into its string name. */
23339
23340 static const char *
23341 dwarf_form_name (unsigned form)
23342 {
23343 const char *name = get_DW_FORM_name (form);
23344
23345 if (name == NULL)
23346 return dwarf_unknown ("FORM", form);
23347
23348 return name;
23349 }
23350
23351 static const char *
23352 dwarf_bool_name (unsigned mybool)
23353 {
23354 if (mybool)
23355 return "TRUE";
23356 else
23357 return "FALSE";
23358 }
23359
23360 /* Convert a DWARF type code into its string name. */
23361
23362 static const char *
23363 dwarf_type_encoding_name (unsigned enc)
23364 {
23365 const char *name = get_DW_ATE_name (enc);
23366
23367 if (name == NULL)
23368 return dwarf_unknown ("ATE", enc);
23369
23370 return name;
23371 }
23372
23373 static void
23374 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
23375 {
23376 unsigned int i;
23377
23378 print_spaces (indent, f);
23379 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
23380 dwarf_tag_name (die->tag), die->abbrev,
23381 sect_offset_str (die->sect_off));
23382
23383 if (die->parent != NULL)
23384 {
23385 print_spaces (indent, f);
23386 fprintf_unfiltered (f, " parent at offset: %s\n",
23387 sect_offset_str (die->parent->sect_off));
23388 }
23389
23390 print_spaces (indent, f);
23391 fprintf_unfiltered (f, " has children: %s\n",
23392 dwarf_bool_name (die->child != NULL));
23393
23394 print_spaces (indent, f);
23395 fprintf_unfiltered (f, " attributes:\n");
23396
23397 for (i = 0; i < die->num_attrs; ++i)
23398 {
23399 print_spaces (indent, f);
23400 fprintf_unfiltered (f, " %s (%s) ",
23401 dwarf_attr_name (die->attrs[i].name),
23402 dwarf_form_name (die->attrs[i].form));
23403
23404 switch (die->attrs[i].form)
23405 {
23406 case DW_FORM_addr:
23407 case DW_FORM_addrx:
23408 case DW_FORM_GNU_addr_index:
23409 fprintf_unfiltered (f, "address: ");
23410 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
23411 break;
23412 case DW_FORM_block2:
23413 case DW_FORM_block4:
23414 case DW_FORM_block:
23415 case DW_FORM_block1:
23416 fprintf_unfiltered (f, "block: size %s",
23417 pulongest (DW_BLOCK (&die->attrs[i])->size));
23418 break;
23419 case DW_FORM_exprloc:
23420 fprintf_unfiltered (f, "expression: size %s",
23421 pulongest (DW_BLOCK (&die->attrs[i])->size));
23422 break;
23423 case DW_FORM_data16:
23424 fprintf_unfiltered (f, "constant of 16 bytes");
23425 break;
23426 case DW_FORM_ref_addr:
23427 fprintf_unfiltered (f, "ref address: ");
23428 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
23429 break;
23430 case DW_FORM_GNU_ref_alt:
23431 fprintf_unfiltered (f, "alt ref address: ");
23432 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
23433 break;
23434 case DW_FORM_ref1:
23435 case DW_FORM_ref2:
23436 case DW_FORM_ref4:
23437 case DW_FORM_ref8:
23438 case DW_FORM_ref_udata:
23439 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
23440 (long) (DW_UNSND (&die->attrs[i])));
23441 break;
23442 case DW_FORM_data1:
23443 case DW_FORM_data2:
23444 case DW_FORM_data4:
23445 case DW_FORM_data8:
23446 case DW_FORM_udata:
23447 case DW_FORM_sdata:
23448 fprintf_unfiltered (f, "constant: %s",
23449 pulongest (DW_UNSND (&die->attrs[i])));
23450 break;
23451 case DW_FORM_sec_offset:
23452 fprintf_unfiltered (f, "section offset: %s",
23453 pulongest (DW_UNSND (&die->attrs[i])));
23454 break;
23455 case DW_FORM_ref_sig8:
23456 fprintf_unfiltered (f, "signature: %s",
23457 hex_string (DW_SIGNATURE (&die->attrs[i])));
23458 break;
23459 case DW_FORM_string:
23460 case DW_FORM_strp:
23461 case DW_FORM_line_strp:
23462 case DW_FORM_strx:
23463 case DW_FORM_GNU_str_index:
23464 case DW_FORM_GNU_strp_alt:
23465 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
23466 DW_STRING (&die->attrs[i])
23467 ? DW_STRING (&die->attrs[i]) : "",
23468 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
23469 break;
23470 case DW_FORM_flag:
23471 if (DW_UNSND (&die->attrs[i]))
23472 fprintf_unfiltered (f, "flag: TRUE");
23473 else
23474 fprintf_unfiltered (f, "flag: FALSE");
23475 break;
23476 case DW_FORM_flag_present:
23477 fprintf_unfiltered (f, "flag: TRUE");
23478 break;
23479 case DW_FORM_indirect:
23480 /* The reader will have reduced the indirect form to
23481 the "base form" so this form should not occur. */
23482 fprintf_unfiltered (f,
23483 "unexpected attribute form: DW_FORM_indirect");
23484 break;
23485 case DW_FORM_implicit_const:
23486 fprintf_unfiltered (f, "constant: %s",
23487 plongest (DW_SND (&die->attrs[i])));
23488 break;
23489 default:
23490 fprintf_unfiltered (f, "unsupported attribute form: %d.",
23491 die->attrs[i].form);
23492 break;
23493 }
23494 fprintf_unfiltered (f, "\n");
23495 }
23496 }
23497
23498 static void
23499 dump_die_for_error (struct die_info *die)
23500 {
23501 dump_die_shallow (gdb_stderr, 0, die);
23502 }
23503
23504 static void
23505 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
23506 {
23507 int indent = level * 4;
23508
23509 gdb_assert (die != NULL);
23510
23511 if (level >= max_level)
23512 return;
23513
23514 dump_die_shallow (f, indent, die);
23515
23516 if (die->child != NULL)
23517 {
23518 print_spaces (indent, f);
23519 fprintf_unfiltered (f, " Children:");
23520 if (level + 1 < max_level)
23521 {
23522 fprintf_unfiltered (f, "\n");
23523 dump_die_1 (f, level + 1, max_level, die->child);
23524 }
23525 else
23526 {
23527 fprintf_unfiltered (f,
23528 " [not printed, max nesting level reached]\n");
23529 }
23530 }
23531
23532 if (die->sibling != NULL && level > 0)
23533 {
23534 dump_die_1 (f, level, max_level, die->sibling);
23535 }
23536 }
23537
23538 /* This is called from the pdie macro in gdbinit.in.
23539 It's not static so gcc will keep a copy callable from gdb. */
23540
23541 void
23542 dump_die (struct die_info *die, int max_level)
23543 {
23544 dump_die_1 (gdb_stdlog, 0, max_level, die);
23545 }
23546
23547 static void
23548 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
23549 {
23550 void **slot;
23551
23552 slot = htab_find_slot_with_hash (cu->die_hash, die,
23553 to_underlying (die->sect_off),
23554 INSERT);
23555
23556 *slot = die;
23557 }
23558
23559 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
23560 required kind. */
23561
23562 static sect_offset
23563 dwarf2_get_ref_die_offset (const struct attribute *attr)
23564 {
23565 if (attr_form_is_ref (attr))
23566 return (sect_offset) DW_UNSND (attr);
23567
23568 complaint (_("unsupported die ref attribute form: '%s'"),
23569 dwarf_form_name (attr->form));
23570 return {};
23571 }
23572
23573 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
23574 * the value held by the attribute is not constant. */
23575
23576 static LONGEST
23577 dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
23578 {
23579 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
23580 return DW_SND (attr);
23581 else if (attr->form == DW_FORM_udata
23582 || attr->form == DW_FORM_data1
23583 || attr->form == DW_FORM_data2
23584 || attr->form == DW_FORM_data4
23585 || attr->form == DW_FORM_data8)
23586 return DW_UNSND (attr);
23587 else
23588 {
23589 /* For DW_FORM_data16 see attr_form_is_constant. */
23590 complaint (_("Attribute value is not a constant (%s)"),
23591 dwarf_form_name (attr->form));
23592 return default_value;
23593 }
23594 }
23595
23596 /* Follow reference or signature attribute ATTR of SRC_DIE.
23597 On entry *REF_CU is the CU of SRC_DIE.
23598 On exit *REF_CU is the CU of the result. */
23599
23600 static struct die_info *
23601 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
23602 struct dwarf2_cu **ref_cu)
23603 {
23604 struct die_info *die;
23605
23606 if (attr_form_is_ref (attr))
23607 die = follow_die_ref (src_die, attr, ref_cu);
23608 else if (attr->form == DW_FORM_ref_sig8)
23609 die = follow_die_sig (src_die, attr, ref_cu);
23610 else
23611 {
23612 dump_die_for_error (src_die);
23613 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
23614 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23615 }
23616
23617 return die;
23618 }
23619
23620 /* Follow reference OFFSET.
23621 On entry *REF_CU is the CU of the source die referencing OFFSET.
23622 On exit *REF_CU is the CU of the result.
23623 Returns NULL if OFFSET is invalid. */
23624
23625 static struct die_info *
23626 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
23627 struct dwarf2_cu **ref_cu)
23628 {
23629 struct die_info temp_die;
23630 struct dwarf2_cu *target_cu, *cu = *ref_cu;
23631 struct dwarf2_per_objfile *dwarf2_per_objfile
23632 = cu->per_cu->dwarf2_per_objfile;
23633
23634 gdb_assert (cu->per_cu != NULL);
23635
23636 target_cu = cu;
23637
23638 if (cu->per_cu->is_debug_types)
23639 {
23640 /* .debug_types CUs cannot reference anything outside their CU.
23641 If they need to, they have to reference a signatured type via
23642 DW_FORM_ref_sig8. */
23643 if (!offset_in_cu_p (&cu->header, sect_off))
23644 return NULL;
23645 }
23646 else if (offset_in_dwz != cu->per_cu->is_dwz
23647 || !offset_in_cu_p (&cu->header, sect_off))
23648 {
23649 struct dwarf2_per_cu_data *per_cu;
23650
23651 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
23652 dwarf2_per_objfile);
23653
23654 /* If necessary, add it to the queue and load its DIEs. */
23655 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
23656 load_full_comp_unit (per_cu, false, cu->language);
23657
23658 target_cu = per_cu->cu;
23659 }
23660 else if (cu->dies == NULL)
23661 {
23662 /* We're loading full DIEs during partial symbol reading. */
23663 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
23664 load_full_comp_unit (cu->per_cu, false, language_minimal);
23665 }
23666
23667 *ref_cu = target_cu;
23668 temp_die.sect_off = sect_off;
23669
23670 if (target_cu != cu)
23671 target_cu->ancestor = cu;
23672
23673 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
23674 &temp_die,
23675 to_underlying (sect_off));
23676 }
23677
23678 /* Follow reference attribute ATTR of SRC_DIE.
23679 On entry *REF_CU is the CU of SRC_DIE.
23680 On exit *REF_CU is the CU of the result. */
23681
23682 static struct die_info *
23683 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
23684 struct dwarf2_cu **ref_cu)
23685 {
23686 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
23687 struct dwarf2_cu *cu = *ref_cu;
23688 struct die_info *die;
23689
23690 die = follow_die_offset (sect_off,
23691 (attr->form == DW_FORM_GNU_ref_alt
23692 || cu->per_cu->is_dwz),
23693 ref_cu);
23694 if (!die)
23695 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
23696 "at %s [in module %s]"),
23697 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
23698 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
23699
23700 return die;
23701 }
23702
23703 /* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
23704 Returned value is intended for DW_OP_call*. Returned
23705 dwarf2_locexpr_baton->data has lifetime of
23706 PER_CU->DWARF2_PER_OBJFILE->OBJFILE. */
23707
23708 struct dwarf2_locexpr_baton
23709 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
23710 struct dwarf2_per_cu_data *per_cu,
23711 CORE_ADDR (*get_frame_pc) (void *baton),
23712 void *baton, bool resolve_abstract_p)
23713 {
23714 struct dwarf2_cu *cu;
23715 struct die_info *die;
23716 struct attribute *attr;
23717 struct dwarf2_locexpr_baton retval;
23718 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
23719 struct objfile *objfile = dwarf2_per_objfile->objfile;
23720
23721 if (per_cu->cu == NULL)
23722 load_cu (per_cu, false);
23723 cu = per_cu->cu;
23724 if (cu == NULL)
23725 {
23726 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23727 Instead just throw an error, not much else we can do. */
23728 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23729 sect_offset_str (sect_off), objfile_name (objfile));
23730 }
23731
23732 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23733 if (!die)
23734 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23735 sect_offset_str (sect_off), objfile_name (objfile));
23736
23737 attr = dwarf2_attr (die, DW_AT_location, cu);
23738 if (!attr && resolve_abstract_p
23739 && (dwarf2_per_objfile->abstract_to_concrete.find (die->sect_off)
23740 != dwarf2_per_objfile->abstract_to_concrete.end ()))
23741 {
23742 CORE_ADDR pc = (*get_frame_pc) (baton);
23743 CORE_ADDR baseaddr = objfile->section_offsets[SECT_OFF_TEXT (objfile)];
23744 struct gdbarch *gdbarch = get_objfile_arch (objfile);
23745
23746 for (const auto &cand_off
23747 : dwarf2_per_objfile->abstract_to_concrete[die->sect_off])
23748 {
23749 struct dwarf2_cu *cand_cu = cu;
23750 struct die_info *cand
23751 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
23752 if (!cand
23753 || !cand->parent
23754 || cand->parent->tag != DW_TAG_subprogram)
23755 continue;
23756
23757 CORE_ADDR pc_low, pc_high;
23758 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
23759 if (pc_low == ((CORE_ADDR) -1))
23760 continue;
23761 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
23762 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
23763 if (!(pc_low <= pc && pc < pc_high))
23764 continue;
23765
23766 die = cand;
23767 attr = dwarf2_attr (die, DW_AT_location, cu);
23768 break;
23769 }
23770 }
23771
23772 if (!attr)
23773 {
23774 /* DWARF: "If there is no such attribute, then there is no effect.".
23775 DATA is ignored if SIZE is 0. */
23776
23777 retval.data = NULL;
23778 retval.size = 0;
23779 }
23780 else if (attr_form_is_section_offset (attr))
23781 {
23782 struct dwarf2_loclist_baton loclist_baton;
23783 CORE_ADDR pc = (*get_frame_pc) (baton);
23784 size_t size;
23785
23786 fill_in_loclist_baton (cu, &loclist_baton, attr);
23787
23788 retval.data = dwarf2_find_location_expression (&loclist_baton,
23789 &size, pc);
23790 retval.size = size;
23791 }
23792 else
23793 {
23794 if (!attr_form_is_block (attr))
23795 error (_("Dwarf Error: DIE at %s referenced in module %s "
23796 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
23797 sect_offset_str (sect_off), objfile_name (objfile));
23798
23799 retval.data = DW_BLOCK (attr)->data;
23800 retval.size = DW_BLOCK (attr)->size;
23801 }
23802 retval.per_cu = cu->per_cu;
23803
23804 age_cached_comp_units (dwarf2_per_objfile);
23805
23806 return retval;
23807 }
23808
23809 /* Like dwarf2_fetch_die_loc_sect_off, but take a CU
23810 offset. */
23811
23812 struct dwarf2_locexpr_baton
23813 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
23814 struct dwarf2_per_cu_data *per_cu,
23815 CORE_ADDR (*get_frame_pc) (void *baton),
23816 void *baton)
23817 {
23818 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
23819
23820 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
23821 }
23822
23823 /* Write a constant of a given type as target-ordered bytes into
23824 OBSTACK. */
23825
23826 static const gdb_byte *
23827 write_constant_as_bytes (struct obstack *obstack,
23828 enum bfd_endian byte_order,
23829 struct type *type,
23830 ULONGEST value,
23831 LONGEST *len)
23832 {
23833 gdb_byte *result;
23834
23835 *len = TYPE_LENGTH (type);
23836 result = (gdb_byte *) obstack_alloc (obstack, *len);
23837 store_unsigned_integer (result, *len, byte_order, value);
23838
23839 return result;
23840 }
23841
23842 /* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
23843 pointer to the constant bytes and set LEN to the length of the
23844 data. If memory is needed, allocate it on OBSTACK. If the DIE
23845 does not have a DW_AT_const_value, return NULL. */
23846
23847 const gdb_byte *
23848 dwarf2_fetch_constant_bytes (sect_offset sect_off,
23849 struct dwarf2_per_cu_data *per_cu,
23850 struct obstack *obstack,
23851 LONGEST *len)
23852 {
23853 struct dwarf2_cu *cu;
23854 struct die_info *die;
23855 struct attribute *attr;
23856 const gdb_byte *result = NULL;
23857 struct type *type;
23858 LONGEST value;
23859 enum bfd_endian byte_order;
23860 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
23861
23862 if (per_cu->cu == NULL)
23863 load_cu (per_cu, false);
23864 cu = per_cu->cu;
23865 if (cu == NULL)
23866 {
23867 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23868 Instead just throw an error, not much else we can do. */
23869 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23870 sect_offset_str (sect_off), objfile_name (objfile));
23871 }
23872
23873 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23874 if (!die)
23875 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23876 sect_offset_str (sect_off), objfile_name (objfile));
23877
23878 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23879 if (attr == NULL)
23880 return NULL;
23881
23882 byte_order = (bfd_big_endian (objfile->obfd)
23883 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23884
23885 switch (attr->form)
23886 {
23887 case DW_FORM_addr:
23888 case DW_FORM_addrx:
23889 case DW_FORM_GNU_addr_index:
23890 {
23891 gdb_byte *tem;
23892
23893 *len = cu->header.addr_size;
23894 tem = (gdb_byte *) obstack_alloc (obstack, *len);
23895 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
23896 result = tem;
23897 }
23898 break;
23899 case DW_FORM_string:
23900 case DW_FORM_strp:
23901 case DW_FORM_strx:
23902 case DW_FORM_GNU_str_index:
23903 case DW_FORM_GNU_strp_alt:
23904 /* DW_STRING is already allocated on the objfile obstack, point
23905 directly to it. */
23906 result = (const gdb_byte *) DW_STRING (attr);
23907 *len = strlen (DW_STRING (attr));
23908 break;
23909 case DW_FORM_block1:
23910 case DW_FORM_block2:
23911 case DW_FORM_block4:
23912 case DW_FORM_block:
23913 case DW_FORM_exprloc:
23914 case DW_FORM_data16:
23915 result = DW_BLOCK (attr)->data;
23916 *len = DW_BLOCK (attr)->size;
23917 break;
23918
23919 /* The DW_AT_const_value attributes are supposed to carry the
23920 symbol's value "represented as it would be on the target
23921 architecture." By the time we get here, it's already been
23922 converted to host endianness, so we just need to sign- or
23923 zero-extend it as appropriate. */
23924 case DW_FORM_data1:
23925 type = die_type (die, cu);
23926 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23927 if (result == NULL)
23928 result = write_constant_as_bytes (obstack, byte_order,
23929 type, value, len);
23930 break;
23931 case DW_FORM_data2:
23932 type = die_type (die, cu);
23933 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23934 if (result == NULL)
23935 result = write_constant_as_bytes (obstack, byte_order,
23936 type, value, len);
23937 break;
23938 case DW_FORM_data4:
23939 type = die_type (die, cu);
23940 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23941 if (result == NULL)
23942 result = write_constant_as_bytes (obstack, byte_order,
23943 type, value, len);
23944 break;
23945 case DW_FORM_data8:
23946 type = die_type (die, cu);
23947 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23948 if (result == NULL)
23949 result = write_constant_as_bytes (obstack, byte_order,
23950 type, value, len);
23951 break;
23952
23953 case DW_FORM_sdata:
23954 case DW_FORM_implicit_const:
23955 type = die_type (die, cu);
23956 result = write_constant_as_bytes (obstack, byte_order,
23957 type, DW_SND (attr), len);
23958 break;
23959
23960 case DW_FORM_udata:
23961 type = die_type (die, cu);
23962 result = write_constant_as_bytes (obstack, byte_order,
23963 type, DW_UNSND (attr), len);
23964 break;
23965
23966 default:
23967 complaint (_("unsupported const value attribute form: '%s'"),
23968 dwarf_form_name (attr->form));
23969 break;
23970 }
23971
23972 return result;
23973 }
23974
23975 /* Return the type of the die at OFFSET in PER_CU. Return NULL if no
23976 valid type for this die is found. */
23977
23978 struct type *
23979 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
23980 struct dwarf2_per_cu_data *per_cu)
23981 {
23982 struct dwarf2_cu *cu;
23983 struct die_info *die;
23984
23985 if (per_cu->cu == NULL)
23986 load_cu (per_cu, false);
23987 cu = per_cu->cu;
23988 if (!cu)
23989 return NULL;
23990
23991 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23992 if (!die)
23993 return NULL;
23994
23995 return die_type (die, cu);
23996 }
23997
23998 /* Return the type of the DIE at DIE_OFFSET in the CU named by
23999 PER_CU. */
24000
24001 struct type *
24002 dwarf2_get_die_type (cu_offset die_offset,
24003 struct dwarf2_per_cu_data *per_cu)
24004 {
24005 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
24006 return get_die_type_at_offset (die_offset_sect, per_cu);
24007 }
24008
24009 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
24010 On entry *REF_CU is the CU of SRC_DIE.
24011 On exit *REF_CU is the CU of the result.
24012 Returns NULL if the referenced DIE isn't found. */
24013
24014 static struct die_info *
24015 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
24016 struct dwarf2_cu **ref_cu)
24017 {
24018 struct die_info temp_die;
24019 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
24020 struct die_info *die;
24021
24022 /* While it might be nice to assert sig_type->type == NULL here,
24023 we can get here for DW_AT_imported_declaration where we need
24024 the DIE not the type. */
24025
24026 /* If necessary, add it to the queue and load its DIEs. */
24027
24028 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
24029 read_signatured_type (sig_type);
24030
24031 sig_cu = sig_type->per_cu.cu;
24032 gdb_assert (sig_cu != NULL);
24033 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
24034 temp_die.sect_off = sig_type->type_offset_in_section;
24035 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
24036 to_underlying (temp_die.sect_off));
24037 if (die)
24038 {
24039 struct dwarf2_per_objfile *dwarf2_per_objfile
24040 = (*ref_cu)->per_cu->dwarf2_per_objfile;
24041
24042 /* For .gdb_index version 7 keep track of included TUs.
24043 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
24044 if (dwarf2_per_objfile->index_table != NULL
24045 && dwarf2_per_objfile->index_table->version <= 7)
24046 {
24047 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
24048 }
24049
24050 *ref_cu = sig_cu;
24051 if (sig_cu != cu)
24052 sig_cu->ancestor = cu;
24053
24054 return die;
24055 }
24056
24057 return NULL;
24058 }
24059
24060 /* Follow signatured type referenced by ATTR in SRC_DIE.
24061 On entry *REF_CU is the CU of SRC_DIE.
24062 On exit *REF_CU is the CU of the result.
24063 The result is the DIE of the type.
24064 If the referenced type cannot be found an error is thrown. */
24065
24066 static struct die_info *
24067 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
24068 struct dwarf2_cu **ref_cu)
24069 {
24070 ULONGEST signature = DW_SIGNATURE (attr);
24071 struct signatured_type *sig_type;
24072 struct die_info *die;
24073
24074 gdb_assert (attr->form == DW_FORM_ref_sig8);
24075
24076 sig_type = lookup_signatured_type (*ref_cu, signature);
24077 /* sig_type will be NULL if the signatured type is missing from
24078 the debug info. */
24079 if (sig_type == NULL)
24080 {
24081 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
24082 " from DIE at %s [in module %s]"),
24083 hex_string (signature), sect_offset_str (src_die->sect_off),
24084 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
24085 }
24086
24087 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
24088 if (die == NULL)
24089 {
24090 dump_die_for_error (src_die);
24091 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
24092 " from DIE at %s [in module %s]"),
24093 hex_string (signature), sect_offset_str (src_die->sect_off),
24094 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
24095 }
24096
24097 return die;
24098 }
24099
24100 /* Get the type specified by SIGNATURE referenced in DIE/CU,
24101 reading in and processing the type unit if necessary. */
24102
24103 static struct type *
24104 get_signatured_type (struct die_info *die, ULONGEST signature,
24105 struct dwarf2_cu *cu)
24106 {
24107 struct dwarf2_per_objfile *dwarf2_per_objfile
24108 = cu->per_cu->dwarf2_per_objfile;
24109 struct signatured_type *sig_type;
24110 struct dwarf2_cu *type_cu;
24111 struct die_info *type_die;
24112 struct type *type;
24113
24114 sig_type = lookup_signatured_type (cu, signature);
24115 /* sig_type will be NULL if the signatured type is missing from
24116 the debug info. */
24117 if (sig_type == NULL)
24118 {
24119 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
24120 " from DIE at %s [in module %s]"),
24121 hex_string (signature), sect_offset_str (die->sect_off),
24122 objfile_name (dwarf2_per_objfile->objfile));
24123 return build_error_marker_type (cu, die);
24124 }
24125
24126 /* If we already know the type we're done. */
24127 if (sig_type->type != NULL)
24128 return sig_type->type;
24129
24130 type_cu = cu;
24131 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
24132 if (type_die != NULL)
24133 {
24134 /* N.B. We need to call get_die_type to ensure only one type for this DIE
24135 is created. This is important, for example, because for c++ classes
24136 we need TYPE_NAME set which is only done by new_symbol. Blech. */
24137 type = read_type_die (type_die, type_cu);
24138 if (type == NULL)
24139 {
24140 complaint (_("Dwarf Error: Cannot build signatured type %s"
24141 " referenced from DIE at %s [in module %s]"),
24142 hex_string (signature), sect_offset_str (die->sect_off),
24143 objfile_name (dwarf2_per_objfile->objfile));
24144 type = build_error_marker_type (cu, die);
24145 }
24146 }
24147 else
24148 {
24149 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
24150 " from DIE at %s [in module %s]"),
24151 hex_string (signature), sect_offset_str (die->sect_off),
24152 objfile_name (dwarf2_per_objfile->objfile));
24153 type = build_error_marker_type (cu, die);
24154 }
24155 sig_type->type = type;
24156
24157 return type;
24158 }
24159
24160 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
24161 reading in and processing the type unit if necessary. */
24162
24163 static struct type *
24164 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
24165 struct dwarf2_cu *cu) /* ARI: editCase function */
24166 {
24167 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
24168 if (attr_form_is_ref (attr))
24169 {
24170 struct dwarf2_cu *type_cu = cu;
24171 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
24172
24173 return read_type_die (type_die, type_cu);
24174 }
24175 else if (attr->form == DW_FORM_ref_sig8)
24176 {
24177 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
24178 }
24179 else
24180 {
24181 struct dwarf2_per_objfile *dwarf2_per_objfile
24182 = cu->per_cu->dwarf2_per_objfile;
24183
24184 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
24185 " at %s [in module %s]"),
24186 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
24187 objfile_name (dwarf2_per_objfile->objfile));
24188 return build_error_marker_type (cu, die);
24189 }
24190 }
24191
24192 /* Load the DIEs associated with type unit PER_CU into memory. */
24193
24194 static void
24195 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
24196 {
24197 struct signatured_type *sig_type;
24198
24199 /* Caller is responsible for ensuring type_unit_groups don't get here. */
24200 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
24201
24202 /* We have the per_cu, but we need the signatured_type.
24203 Fortunately this is an easy translation. */
24204 gdb_assert (per_cu->is_debug_types);
24205 sig_type = (struct signatured_type *) per_cu;
24206
24207 gdb_assert (per_cu->cu == NULL);
24208
24209 read_signatured_type (sig_type);
24210
24211 gdb_assert (per_cu->cu != NULL);
24212 }
24213
24214 /* die_reader_func for read_signatured_type.
24215 This is identical to load_full_comp_unit_reader,
24216 but is kept separate for now. */
24217
24218 static void
24219 read_signatured_type_reader (const struct die_reader_specs *reader,
24220 const gdb_byte *info_ptr,
24221 struct die_info *comp_unit_die,
24222 int has_children,
24223 void *data)
24224 {
24225 struct dwarf2_cu *cu = reader->cu;
24226
24227 gdb_assert (cu->die_hash == NULL);
24228 cu->die_hash =
24229 htab_create_alloc_ex (cu->header.length / 12,
24230 die_hash,
24231 die_eq,
24232 NULL,
24233 &cu->comp_unit_obstack,
24234 hashtab_obstack_allocate,
24235 dummy_obstack_deallocate);
24236
24237 if (has_children)
24238 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
24239 &info_ptr, comp_unit_die);
24240 cu->dies = comp_unit_die;
24241 /* comp_unit_die is not stored in die_hash, no need. */
24242
24243 /* We try not to read any attributes in this function, because not
24244 all CUs needed for references have been loaded yet, and symbol
24245 table processing isn't initialized. But we have to set the CU language,
24246 or we won't be able to build types correctly.
24247 Similarly, if we do not read the producer, we can not apply
24248 producer-specific interpretation. */
24249 prepare_one_comp_unit (cu, cu->dies, language_minimal);
24250 }
24251
24252 /* Read in a signatured type and build its CU and DIEs.
24253 If the type is a stub for the real type in a DWO file,
24254 read in the real type from the DWO file as well. */
24255
24256 static void
24257 read_signatured_type (struct signatured_type *sig_type)
24258 {
24259 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
24260
24261 gdb_assert (per_cu->is_debug_types);
24262 gdb_assert (per_cu->cu == NULL);
24263
24264 init_cutu_and_read_dies (per_cu, NULL, 0, 1, false,
24265 read_signatured_type_reader, NULL);
24266 sig_type->per_cu.tu_read = 1;
24267 }
24268
24269 /* Decode simple location descriptions.
24270 Given a pointer to a dwarf block that defines a location, compute
24271 the location and return the value.
24272
24273 NOTE drow/2003-11-18: This function is called in two situations
24274 now: for the address of static or global variables (partial symbols
24275 only) and for offsets into structures which are expected to be
24276 (more or less) constant. The partial symbol case should go away,
24277 and only the constant case should remain. That will let this
24278 function complain more accurately. A few special modes are allowed
24279 without complaint for global variables (for instance, global
24280 register values and thread-local values).
24281
24282 A location description containing no operations indicates that the
24283 object is optimized out. The return value is 0 for that case.
24284 FIXME drow/2003-11-16: No callers check for this case any more; soon all
24285 callers will only want a very basic result and this can become a
24286 complaint.
24287
24288 Note that stack[0] is unused except as a default error return. */
24289
24290 static CORE_ADDR
24291 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
24292 {
24293 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
24294 size_t i;
24295 size_t size = blk->size;
24296 const gdb_byte *data = blk->data;
24297 CORE_ADDR stack[64];
24298 int stacki;
24299 unsigned int bytes_read, unsnd;
24300 gdb_byte op;
24301
24302 i = 0;
24303 stacki = 0;
24304 stack[stacki] = 0;
24305 stack[++stacki] = 0;
24306
24307 while (i < size)
24308 {
24309 op = data[i++];
24310 switch (op)
24311 {
24312 case DW_OP_lit0:
24313 case DW_OP_lit1:
24314 case DW_OP_lit2:
24315 case DW_OP_lit3:
24316 case DW_OP_lit4:
24317 case DW_OP_lit5:
24318 case DW_OP_lit6:
24319 case DW_OP_lit7:
24320 case DW_OP_lit8:
24321 case DW_OP_lit9:
24322 case DW_OP_lit10:
24323 case DW_OP_lit11:
24324 case DW_OP_lit12:
24325 case DW_OP_lit13:
24326 case DW_OP_lit14:
24327 case DW_OP_lit15:
24328 case DW_OP_lit16:
24329 case DW_OP_lit17:
24330 case DW_OP_lit18:
24331 case DW_OP_lit19:
24332 case DW_OP_lit20:
24333 case DW_OP_lit21:
24334 case DW_OP_lit22:
24335 case DW_OP_lit23:
24336 case DW_OP_lit24:
24337 case DW_OP_lit25:
24338 case DW_OP_lit26:
24339 case DW_OP_lit27:
24340 case DW_OP_lit28:
24341 case DW_OP_lit29:
24342 case DW_OP_lit30:
24343 case DW_OP_lit31:
24344 stack[++stacki] = op - DW_OP_lit0;
24345 break;
24346
24347 case DW_OP_reg0:
24348 case DW_OP_reg1:
24349 case DW_OP_reg2:
24350 case DW_OP_reg3:
24351 case DW_OP_reg4:
24352 case DW_OP_reg5:
24353 case DW_OP_reg6:
24354 case DW_OP_reg7:
24355 case DW_OP_reg8:
24356 case DW_OP_reg9:
24357 case DW_OP_reg10:
24358 case DW_OP_reg11:
24359 case DW_OP_reg12:
24360 case DW_OP_reg13:
24361 case DW_OP_reg14:
24362 case DW_OP_reg15:
24363 case DW_OP_reg16:
24364 case DW_OP_reg17:
24365 case DW_OP_reg18:
24366 case DW_OP_reg19:
24367 case DW_OP_reg20:
24368 case DW_OP_reg21:
24369 case DW_OP_reg22:
24370 case DW_OP_reg23:
24371 case DW_OP_reg24:
24372 case DW_OP_reg25:
24373 case DW_OP_reg26:
24374 case DW_OP_reg27:
24375 case DW_OP_reg28:
24376 case DW_OP_reg29:
24377 case DW_OP_reg30:
24378 case DW_OP_reg31:
24379 stack[++stacki] = op - DW_OP_reg0;
24380 if (i < size)
24381 dwarf2_complex_location_expr_complaint ();
24382 break;
24383
24384 case DW_OP_regx:
24385 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
24386 i += bytes_read;
24387 stack[++stacki] = unsnd;
24388 if (i < size)
24389 dwarf2_complex_location_expr_complaint ();
24390 break;
24391
24392 case DW_OP_addr:
24393 stack[++stacki] = read_address (objfile->obfd, &data[i],
24394 cu, &bytes_read);
24395 i += bytes_read;
24396 break;
24397
24398 case DW_OP_const1u:
24399 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
24400 i += 1;
24401 break;
24402
24403 case DW_OP_const1s:
24404 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
24405 i += 1;
24406 break;
24407
24408 case DW_OP_const2u:
24409 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
24410 i += 2;
24411 break;
24412
24413 case DW_OP_const2s:
24414 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
24415 i += 2;
24416 break;
24417
24418 case DW_OP_const4u:
24419 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
24420 i += 4;
24421 break;
24422
24423 case DW_OP_const4s:
24424 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
24425 i += 4;
24426 break;
24427
24428 case DW_OP_const8u:
24429 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
24430 i += 8;
24431 break;
24432
24433 case DW_OP_constu:
24434 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
24435 &bytes_read);
24436 i += bytes_read;
24437 break;
24438
24439 case DW_OP_consts:
24440 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
24441 i += bytes_read;
24442 break;
24443
24444 case DW_OP_dup:
24445 stack[stacki + 1] = stack[stacki];
24446 stacki++;
24447 break;
24448
24449 case DW_OP_plus:
24450 stack[stacki - 1] += stack[stacki];
24451 stacki--;
24452 break;
24453
24454 case DW_OP_plus_uconst:
24455 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
24456 &bytes_read);
24457 i += bytes_read;
24458 break;
24459
24460 case DW_OP_minus:
24461 stack[stacki - 1] -= stack[stacki];
24462 stacki--;
24463 break;
24464
24465 case DW_OP_deref:
24466 /* If we're not the last op, then we definitely can't encode
24467 this using GDB's address_class enum. This is valid for partial
24468 global symbols, although the variable's address will be bogus
24469 in the psymtab. */
24470 if (i < size)
24471 dwarf2_complex_location_expr_complaint ();
24472 break;
24473
24474 case DW_OP_GNU_push_tls_address:
24475 case DW_OP_form_tls_address:
24476 /* The top of the stack has the offset from the beginning
24477 of the thread control block at which the variable is located. */
24478 /* Nothing should follow this operator, so the top of stack would
24479 be returned. */
24480 /* This is valid for partial global symbols, but the variable's
24481 address will be bogus in the psymtab. Make it always at least
24482 non-zero to not look as a variable garbage collected by linker
24483 which have DW_OP_addr 0. */
24484 if (i < size)
24485 dwarf2_complex_location_expr_complaint ();
24486 stack[stacki]++;
24487 break;
24488
24489 case DW_OP_GNU_uninit:
24490 break;
24491
24492 case DW_OP_addrx:
24493 case DW_OP_GNU_addr_index:
24494 case DW_OP_GNU_const_index:
24495 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
24496 &bytes_read);
24497 i += bytes_read;
24498 break;
24499
24500 default:
24501 {
24502 const char *name = get_DW_OP_name (op);
24503
24504 if (name)
24505 complaint (_("unsupported stack op: '%s'"),
24506 name);
24507 else
24508 complaint (_("unsupported stack op: '%02x'"),
24509 op);
24510 }
24511
24512 return (stack[stacki]);
24513 }
24514
24515 /* Enforce maximum stack depth of SIZE-1 to avoid writing
24516 outside of the allocated space. Also enforce minimum>0. */
24517 if (stacki >= ARRAY_SIZE (stack) - 1)
24518 {
24519 complaint (_("location description stack overflow"));
24520 return 0;
24521 }
24522
24523 if (stacki <= 0)
24524 {
24525 complaint (_("location description stack underflow"));
24526 return 0;
24527 }
24528 }
24529 return (stack[stacki]);
24530 }
24531
24532 /* memory allocation interface */
24533
24534 static struct dwarf_block *
24535 dwarf_alloc_block (struct dwarf2_cu *cu)
24536 {
24537 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
24538 }
24539
24540 static struct die_info *
24541 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
24542 {
24543 struct die_info *die;
24544 size_t size = sizeof (struct die_info);
24545
24546 if (num_attrs > 1)
24547 size += (num_attrs - 1) * sizeof (struct attribute);
24548
24549 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
24550 memset (die, 0, sizeof (struct die_info));
24551 return (die);
24552 }
24553
24554 \f
24555 /* Macro support. */
24556
24557 /* Return file name relative to the compilation directory of file number I in
24558 *LH's file name table. The result is allocated using xmalloc; the caller is
24559 responsible for freeing it. */
24560
24561 static char *
24562 file_file_name (int file, struct line_header *lh)
24563 {
24564 /* Is the file number a valid index into the line header's file name
24565 table? Remember that file numbers start with one, not zero. */
24566 if (lh->is_valid_file_index (file))
24567 {
24568 const file_entry *fe = lh->file_name_at (file);
24569
24570 if (!IS_ABSOLUTE_PATH (fe->name))
24571 {
24572 const char *dir = fe->include_dir (lh);
24573 if (dir != NULL)
24574 return concat (dir, SLASH_STRING, fe->name, (char *) NULL);
24575 }
24576 return xstrdup (fe->name);
24577 }
24578 else
24579 {
24580 /* The compiler produced a bogus file number. We can at least
24581 record the macro definitions made in the file, even if we
24582 won't be able to find the file by name. */
24583 char fake_name[80];
24584
24585 xsnprintf (fake_name, sizeof (fake_name),
24586 "<bad macro file number %d>", file);
24587
24588 complaint (_("bad file number in macro information (%d)"),
24589 file);
24590
24591 return xstrdup (fake_name);
24592 }
24593 }
24594
24595 /* Return the full name of file number I in *LH's file name table.
24596 Use COMP_DIR as the name of the current directory of the
24597 compilation. The result is allocated using xmalloc; the caller is
24598 responsible for freeing it. */
24599 static char *
24600 file_full_name (int file, struct line_header *lh, const char *comp_dir)
24601 {
24602 /* Is the file number a valid index into the line header's file name
24603 table? Remember that file numbers start with one, not zero. */
24604 if (lh->is_valid_file_index (file))
24605 {
24606 char *relative = file_file_name (file, lh);
24607
24608 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
24609 return relative;
24610 return reconcat (relative, comp_dir, SLASH_STRING,
24611 relative, (char *) NULL);
24612 }
24613 else
24614 return file_file_name (file, lh);
24615 }
24616
24617
24618 static struct macro_source_file *
24619 macro_start_file (struct dwarf2_cu *cu,
24620 int file, int line,
24621 struct macro_source_file *current_file,
24622 struct line_header *lh)
24623 {
24624 /* File name relative to the compilation directory of this source file. */
24625 char *file_name = file_file_name (file, lh);
24626
24627 if (! current_file)
24628 {
24629 /* Note: We don't create a macro table for this compilation unit
24630 at all until we actually get a filename. */
24631 struct macro_table *macro_table = cu->get_builder ()->get_macro_table ();
24632
24633 /* If we have no current file, then this must be the start_file
24634 directive for the compilation unit's main source file. */
24635 current_file = macro_set_main (macro_table, file_name);
24636 macro_define_special (macro_table);
24637 }
24638 else
24639 current_file = macro_include (current_file, line, file_name);
24640
24641 xfree (file_name);
24642
24643 return current_file;
24644 }
24645
24646 static const char *
24647 consume_improper_spaces (const char *p, const char *body)
24648 {
24649 if (*p == ' ')
24650 {
24651 complaint (_("macro definition contains spaces "
24652 "in formal argument list:\n`%s'"),
24653 body);
24654
24655 while (*p == ' ')
24656 p++;
24657 }
24658
24659 return p;
24660 }
24661
24662
24663 static void
24664 parse_macro_definition (struct macro_source_file *file, int line,
24665 const char *body)
24666 {
24667 const char *p;
24668
24669 /* The body string takes one of two forms. For object-like macro
24670 definitions, it should be:
24671
24672 <macro name> " " <definition>
24673
24674 For function-like macro definitions, it should be:
24675
24676 <macro name> "() " <definition>
24677 or
24678 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
24679
24680 Spaces may appear only where explicitly indicated, and in the
24681 <definition>.
24682
24683 The Dwarf 2 spec says that an object-like macro's name is always
24684 followed by a space, but versions of GCC around March 2002 omit
24685 the space when the macro's definition is the empty string.
24686
24687 The Dwarf 2 spec says that there should be no spaces between the
24688 formal arguments in a function-like macro's formal argument list,
24689 but versions of GCC around March 2002 include spaces after the
24690 commas. */
24691
24692
24693 /* Find the extent of the macro name. The macro name is terminated
24694 by either a space or null character (for an object-like macro) or
24695 an opening paren (for a function-like macro). */
24696 for (p = body; *p; p++)
24697 if (*p == ' ' || *p == '(')
24698 break;
24699
24700 if (*p == ' ' || *p == '\0')
24701 {
24702 /* It's an object-like macro. */
24703 int name_len = p - body;
24704 std::string name (body, name_len);
24705 const char *replacement;
24706
24707 if (*p == ' ')
24708 replacement = body + name_len + 1;
24709 else
24710 {
24711 dwarf2_macro_malformed_definition_complaint (body);
24712 replacement = body + name_len;
24713 }
24714
24715 macro_define_object (file, line, name.c_str (), replacement);
24716 }
24717 else if (*p == '(')
24718 {
24719 /* It's a function-like macro. */
24720 std::string name (body, p - body);
24721 int argc = 0;
24722 int argv_size = 1;
24723 char **argv = XNEWVEC (char *, argv_size);
24724
24725 p++;
24726
24727 p = consume_improper_spaces (p, body);
24728
24729 /* Parse the formal argument list. */
24730 while (*p && *p != ')')
24731 {
24732 /* Find the extent of the current argument name. */
24733 const char *arg_start = p;
24734
24735 while (*p && *p != ',' && *p != ')' && *p != ' ')
24736 p++;
24737
24738 if (! *p || p == arg_start)
24739 dwarf2_macro_malformed_definition_complaint (body);
24740 else
24741 {
24742 /* Make sure argv has room for the new argument. */
24743 if (argc >= argv_size)
24744 {
24745 argv_size *= 2;
24746 argv = XRESIZEVEC (char *, argv, argv_size);
24747 }
24748
24749 argv[argc++] = savestring (arg_start, p - arg_start);
24750 }
24751
24752 p = consume_improper_spaces (p, body);
24753
24754 /* Consume the comma, if present. */
24755 if (*p == ',')
24756 {
24757 p++;
24758
24759 p = consume_improper_spaces (p, body);
24760 }
24761 }
24762
24763 if (*p == ')')
24764 {
24765 p++;
24766
24767 if (*p == ' ')
24768 /* Perfectly formed definition, no complaints. */
24769 macro_define_function (file, line, name.c_str (),
24770 argc, (const char **) argv,
24771 p + 1);
24772 else if (*p == '\0')
24773 {
24774 /* Complain, but do define it. */
24775 dwarf2_macro_malformed_definition_complaint (body);
24776 macro_define_function (file, line, name.c_str (),
24777 argc, (const char **) argv,
24778 p);
24779 }
24780 else
24781 /* Just complain. */
24782 dwarf2_macro_malformed_definition_complaint (body);
24783 }
24784 else
24785 /* Just complain. */
24786 dwarf2_macro_malformed_definition_complaint (body);
24787
24788 {
24789 int i;
24790
24791 for (i = 0; i < argc; i++)
24792 xfree (argv[i]);
24793 }
24794 xfree (argv);
24795 }
24796 else
24797 dwarf2_macro_malformed_definition_complaint (body);
24798 }
24799
24800 /* Skip some bytes from BYTES according to the form given in FORM.
24801 Returns the new pointer. */
24802
24803 static const gdb_byte *
24804 skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
24805 enum dwarf_form form,
24806 unsigned int offset_size,
24807 struct dwarf2_section_info *section)
24808 {
24809 unsigned int bytes_read;
24810
24811 switch (form)
24812 {
24813 case DW_FORM_data1:
24814 case DW_FORM_flag:
24815 ++bytes;
24816 break;
24817
24818 case DW_FORM_data2:
24819 bytes += 2;
24820 break;
24821
24822 case DW_FORM_data4:
24823 bytes += 4;
24824 break;
24825
24826 case DW_FORM_data8:
24827 bytes += 8;
24828 break;
24829
24830 case DW_FORM_data16:
24831 bytes += 16;
24832 break;
24833
24834 case DW_FORM_string:
24835 read_direct_string (abfd, bytes, &bytes_read);
24836 bytes += bytes_read;
24837 break;
24838
24839 case DW_FORM_sec_offset:
24840 case DW_FORM_strp:
24841 case DW_FORM_GNU_strp_alt:
24842 bytes += offset_size;
24843 break;
24844
24845 case DW_FORM_block:
24846 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
24847 bytes += bytes_read;
24848 break;
24849
24850 case DW_FORM_block1:
24851 bytes += 1 + read_1_byte (abfd, bytes);
24852 break;
24853 case DW_FORM_block2:
24854 bytes += 2 + read_2_bytes (abfd, bytes);
24855 break;
24856 case DW_FORM_block4:
24857 bytes += 4 + read_4_bytes (abfd, bytes);
24858 break;
24859
24860 case DW_FORM_addrx:
24861 case DW_FORM_sdata:
24862 case DW_FORM_strx:
24863 case DW_FORM_udata:
24864 case DW_FORM_GNU_addr_index:
24865 case DW_FORM_GNU_str_index:
24866 bytes = gdb_skip_leb128 (bytes, buffer_end);
24867 if (bytes == NULL)
24868 {
24869 dwarf2_section_buffer_overflow_complaint (section);
24870 return NULL;
24871 }
24872 break;
24873
24874 case DW_FORM_implicit_const:
24875 break;
24876
24877 default:
24878 {
24879 complaint (_("invalid form 0x%x in `%s'"),
24880 form, get_section_name (section));
24881 return NULL;
24882 }
24883 }
24884
24885 return bytes;
24886 }
24887
24888 /* A helper for dwarf_decode_macros that handles skipping an unknown
24889 opcode. Returns an updated pointer to the macro data buffer; or,
24890 on error, issues a complaint and returns NULL. */
24891
24892 static const gdb_byte *
24893 skip_unknown_opcode (unsigned int opcode,
24894 const gdb_byte **opcode_definitions,
24895 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
24896 bfd *abfd,
24897 unsigned int offset_size,
24898 struct dwarf2_section_info *section)
24899 {
24900 unsigned int bytes_read, i;
24901 unsigned long arg;
24902 const gdb_byte *defn;
24903
24904 if (opcode_definitions[opcode] == NULL)
24905 {
24906 complaint (_("unrecognized DW_MACFINO opcode 0x%x"),
24907 opcode);
24908 return NULL;
24909 }
24910
24911 defn = opcode_definitions[opcode];
24912 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
24913 defn += bytes_read;
24914
24915 for (i = 0; i < arg; ++i)
24916 {
24917 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
24918 (enum dwarf_form) defn[i], offset_size,
24919 section);
24920 if (mac_ptr == NULL)
24921 {
24922 /* skip_form_bytes already issued the complaint. */
24923 return NULL;
24924 }
24925 }
24926
24927 return mac_ptr;
24928 }
24929
24930 /* A helper function which parses the header of a macro section.
24931 If the macro section is the extended (for now called "GNU") type,
24932 then this updates *OFFSET_SIZE. Returns a pointer to just after
24933 the header, or issues a complaint and returns NULL on error. */
24934
24935 static const gdb_byte *
24936 dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
24937 bfd *abfd,
24938 const gdb_byte *mac_ptr,
24939 unsigned int *offset_size,
24940 int section_is_gnu)
24941 {
24942 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
24943
24944 if (section_is_gnu)
24945 {
24946 unsigned int version, flags;
24947
24948 version = read_2_bytes (abfd, mac_ptr);
24949 if (version != 4 && version != 5)
24950 {
24951 complaint (_("unrecognized version `%d' in .debug_macro section"),
24952 version);
24953 return NULL;
24954 }
24955 mac_ptr += 2;
24956
24957 flags = read_1_byte (abfd, mac_ptr);
24958 ++mac_ptr;
24959 *offset_size = (flags & 1) ? 8 : 4;
24960
24961 if ((flags & 2) != 0)
24962 /* We don't need the line table offset. */
24963 mac_ptr += *offset_size;
24964
24965 /* Vendor opcode descriptions. */
24966 if ((flags & 4) != 0)
24967 {
24968 unsigned int i, count;
24969
24970 count = read_1_byte (abfd, mac_ptr);
24971 ++mac_ptr;
24972 for (i = 0; i < count; ++i)
24973 {
24974 unsigned int opcode, bytes_read;
24975 unsigned long arg;
24976
24977 opcode = read_1_byte (abfd, mac_ptr);
24978 ++mac_ptr;
24979 opcode_definitions[opcode] = mac_ptr;
24980 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24981 mac_ptr += bytes_read;
24982 mac_ptr += arg;
24983 }
24984 }
24985 }
24986
24987 return mac_ptr;
24988 }
24989
24990 /* A helper for dwarf_decode_macros that handles the GNU extensions,
24991 including DW_MACRO_import. */
24992
24993 static void
24994 dwarf_decode_macro_bytes (struct dwarf2_cu *cu,
24995 bfd *abfd,
24996 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
24997 struct macro_source_file *current_file,
24998 struct line_header *lh,
24999 struct dwarf2_section_info *section,
25000 int section_is_gnu, int section_is_dwz,
25001 unsigned int offset_size,
25002 htab_t include_hash)
25003 {
25004 struct dwarf2_per_objfile *dwarf2_per_objfile
25005 = cu->per_cu->dwarf2_per_objfile;
25006 struct objfile *objfile = dwarf2_per_objfile->objfile;
25007 enum dwarf_macro_record_type macinfo_type;
25008 int at_commandline;
25009 const gdb_byte *opcode_definitions[256];
25010
25011 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
25012 &offset_size, section_is_gnu);
25013 if (mac_ptr == NULL)
25014 {
25015 /* We already issued a complaint. */
25016 return;
25017 }
25018
25019 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
25020 GDB is still reading the definitions from command line. First
25021 DW_MACINFO_start_file will need to be ignored as it was already executed
25022 to create CURRENT_FILE for the main source holding also the command line
25023 definitions. On first met DW_MACINFO_start_file this flag is reset to
25024 normally execute all the remaining DW_MACINFO_start_file macinfos. */
25025
25026 at_commandline = 1;
25027
25028 do
25029 {
25030 /* Do we at least have room for a macinfo type byte? */
25031 if (mac_ptr >= mac_end)
25032 {
25033 dwarf2_section_buffer_overflow_complaint (section);
25034 break;
25035 }
25036
25037 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
25038 mac_ptr++;
25039
25040 /* Note that we rely on the fact that the corresponding GNU and
25041 DWARF constants are the same. */
25042 DIAGNOSTIC_PUSH
25043 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
25044 switch (macinfo_type)
25045 {
25046 /* A zero macinfo type indicates the end of the macro
25047 information. */
25048 case 0:
25049 break;
25050
25051 case DW_MACRO_define:
25052 case DW_MACRO_undef:
25053 case DW_MACRO_define_strp:
25054 case DW_MACRO_undef_strp:
25055 case DW_MACRO_define_sup:
25056 case DW_MACRO_undef_sup:
25057 {
25058 unsigned int bytes_read;
25059 int line;
25060 const char *body;
25061 int is_define;
25062
25063 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25064 mac_ptr += bytes_read;
25065
25066 if (macinfo_type == DW_MACRO_define
25067 || macinfo_type == DW_MACRO_undef)
25068 {
25069 body = read_direct_string (abfd, mac_ptr, &bytes_read);
25070 mac_ptr += bytes_read;
25071 }
25072 else
25073 {
25074 LONGEST str_offset;
25075
25076 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
25077 mac_ptr += offset_size;
25078
25079 if (macinfo_type == DW_MACRO_define_sup
25080 || macinfo_type == DW_MACRO_undef_sup
25081 || section_is_dwz)
25082 {
25083 struct dwz_file *dwz
25084 = dwarf2_get_dwz_file (dwarf2_per_objfile);
25085
25086 body = read_indirect_string_from_dwz (objfile,
25087 dwz, str_offset);
25088 }
25089 else
25090 body = read_indirect_string_at_offset (dwarf2_per_objfile,
25091 abfd, str_offset);
25092 }
25093
25094 is_define = (macinfo_type == DW_MACRO_define
25095 || macinfo_type == DW_MACRO_define_strp
25096 || macinfo_type == DW_MACRO_define_sup);
25097 if (! current_file)
25098 {
25099 /* DWARF violation as no main source is present. */
25100 complaint (_("debug info with no main source gives macro %s "
25101 "on line %d: %s"),
25102 is_define ? _("definition") : _("undefinition"),
25103 line, body);
25104 break;
25105 }
25106 if ((line == 0 && !at_commandline)
25107 || (line != 0 && at_commandline))
25108 complaint (_("debug info gives %s macro %s with %s line %d: %s"),
25109 at_commandline ? _("command-line") : _("in-file"),
25110 is_define ? _("definition") : _("undefinition"),
25111 line == 0 ? _("zero") : _("non-zero"), line, body);
25112
25113 if (body == NULL)
25114 {
25115 /* Fedora's rpm-build's "debugedit" binary
25116 corrupted .debug_macro sections.
25117
25118 For more info, see
25119 https://bugzilla.redhat.com/show_bug.cgi?id=1708786 */
25120 complaint (_("debug info gives %s invalid macro %s "
25121 "without body (corrupted?) at line %d "
25122 "on file %s"),
25123 at_commandline ? _("command-line") : _("in-file"),
25124 is_define ? _("definition") : _("undefinition"),
25125 line, current_file->filename);
25126 }
25127 else if (is_define)
25128 parse_macro_definition (current_file, line, body);
25129 else
25130 {
25131 gdb_assert (macinfo_type == DW_MACRO_undef
25132 || macinfo_type == DW_MACRO_undef_strp
25133 || macinfo_type == DW_MACRO_undef_sup);
25134 macro_undef (current_file, line, body);
25135 }
25136 }
25137 break;
25138
25139 case DW_MACRO_start_file:
25140 {
25141 unsigned int bytes_read;
25142 int line, file;
25143
25144 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25145 mac_ptr += bytes_read;
25146 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25147 mac_ptr += bytes_read;
25148
25149 if ((line == 0 && !at_commandline)
25150 || (line != 0 && at_commandline))
25151 complaint (_("debug info gives source %d included "
25152 "from %s at %s line %d"),
25153 file, at_commandline ? _("command-line") : _("file"),
25154 line == 0 ? _("zero") : _("non-zero"), line);
25155
25156 if (at_commandline)
25157 {
25158 /* This DW_MACRO_start_file was executed in the
25159 pass one. */
25160 at_commandline = 0;
25161 }
25162 else
25163 current_file = macro_start_file (cu, file, line, current_file,
25164 lh);
25165 }
25166 break;
25167
25168 case DW_MACRO_end_file:
25169 if (! current_file)
25170 complaint (_("macro debug info has an unmatched "
25171 "`close_file' directive"));
25172 else
25173 {
25174 current_file = current_file->included_by;
25175 if (! current_file)
25176 {
25177 enum dwarf_macro_record_type next_type;
25178
25179 /* GCC circa March 2002 doesn't produce the zero
25180 type byte marking the end of the compilation
25181 unit. Complain if it's not there, but exit no
25182 matter what. */
25183
25184 /* Do we at least have room for a macinfo type byte? */
25185 if (mac_ptr >= mac_end)
25186 {
25187 dwarf2_section_buffer_overflow_complaint (section);
25188 return;
25189 }
25190
25191 /* We don't increment mac_ptr here, so this is just
25192 a look-ahead. */
25193 next_type
25194 = (enum dwarf_macro_record_type) read_1_byte (abfd,
25195 mac_ptr);
25196 if (next_type != 0)
25197 complaint (_("no terminating 0-type entry for "
25198 "macros in `.debug_macinfo' section"));
25199
25200 return;
25201 }
25202 }
25203 break;
25204
25205 case DW_MACRO_import:
25206 case DW_MACRO_import_sup:
25207 {
25208 LONGEST offset;
25209 void **slot;
25210 bfd *include_bfd = abfd;
25211 struct dwarf2_section_info *include_section = section;
25212 const gdb_byte *include_mac_end = mac_end;
25213 int is_dwz = section_is_dwz;
25214 const gdb_byte *new_mac_ptr;
25215
25216 offset = read_offset_1 (abfd, mac_ptr, offset_size);
25217 mac_ptr += offset_size;
25218
25219 if (macinfo_type == DW_MACRO_import_sup)
25220 {
25221 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
25222
25223 dwarf2_read_section (objfile, &dwz->macro);
25224
25225 include_section = &dwz->macro;
25226 include_bfd = get_section_bfd_owner (include_section);
25227 include_mac_end = dwz->macro.buffer + dwz->macro.size;
25228 is_dwz = 1;
25229 }
25230
25231 new_mac_ptr = include_section->buffer + offset;
25232 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
25233
25234 if (*slot != NULL)
25235 {
25236 /* This has actually happened; see
25237 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
25238 complaint (_("recursive DW_MACRO_import in "
25239 ".debug_macro section"));
25240 }
25241 else
25242 {
25243 *slot = (void *) new_mac_ptr;
25244
25245 dwarf_decode_macro_bytes (cu, include_bfd, new_mac_ptr,
25246 include_mac_end, current_file, lh,
25247 section, section_is_gnu, is_dwz,
25248 offset_size, include_hash);
25249
25250 htab_remove_elt (include_hash, (void *) new_mac_ptr);
25251 }
25252 }
25253 break;
25254
25255 case DW_MACINFO_vendor_ext:
25256 if (!section_is_gnu)
25257 {
25258 unsigned int bytes_read;
25259
25260 /* This reads the constant, but since we don't recognize
25261 any vendor extensions, we ignore it. */
25262 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25263 mac_ptr += bytes_read;
25264 read_direct_string (abfd, mac_ptr, &bytes_read);
25265 mac_ptr += bytes_read;
25266
25267 /* We don't recognize any vendor extensions. */
25268 break;
25269 }
25270 /* FALLTHROUGH */
25271
25272 default:
25273 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
25274 mac_ptr, mac_end, abfd, offset_size,
25275 section);
25276 if (mac_ptr == NULL)
25277 return;
25278 break;
25279 }
25280 DIAGNOSTIC_POP
25281 } while (macinfo_type != 0);
25282 }
25283
25284 static void
25285 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
25286 int section_is_gnu)
25287 {
25288 struct dwarf2_per_objfile *dwarf2_per_objfile
25289 = cu->per_cu->dwarf2_per_objfile;
25290 struct objfile *objfile = dwarf2_per_objfile->objfile;
25291 struct line_header *lh = cu->line_header;
25292 bfd *abfd;
25293 const gdb_byte *mac_ptr, *mac_end;
25294 struct macro_source_file *current_file = 0;
25295 enum dwarf_macro_record_type macinfo_type;
25296 unsigned int offset_size = cu->header.offset_size;
25297 const gdb_byte *opcode_definitions[256];
25298 void **slot;
25299 struct dwarf2_section_info *section;
25300 const char *section_name;
25301
25302 if (cu->dwo_unit != NULL)
25303 {
25304 if (section_is_gnu)
25305 {
25306 section = &cu->dwo_unit->dwo_file->sections.macro;
25307 section_name = ".debug_macro.dwo";
25308 }
25309 else
25310 {
25311 section = &cu->dwo_unit->dwo_file->sections.macinfo;
25312 section_name = ".debug_macinfo.dwo";
25313 }
25314 }
25315 else
25316 {
25317 if (section_is_gnu)
25318 {
25319 section = &dwarf2_per_objfile->macro;
25320 section_name = ".debug_macro";
25321 }
25322 else
25323 {
25324 section = &dwarf2_per_objfile->macinfo;
25325 section_name = ".debug_macinfo";
25326 }
25327 }
25328
25329 dwarf2_read_section (objfile, section);
25330 if (section->buffer == NULL)
25331 {
25332 complaint (_("missing %s section"), section_name);
25333 return;
25334 }
25335 abfd = get_section_bfd_owner (section);
25336
25337 /* First pass: Find the name of the base filename.
25338 This filename is needed in order to process all macros whose definition
25339 (or undefinition) comes from the command line. These macros are defined
25340 before the first DW_MACINFO_start_file entry, and yet still need to be
25341 associated to the base file.
25342
25343 To determine the base file name, we scan the macro definitions until we
25344 reach the first DW_MACINFO_start_file entry. We then initialize
25345 CURRENT_FILE accordingly so that any macro definition found before the
25346 first DW_MACINFO_start_file can still be associated to the base file. */
25347
25348 mac_ptr = section->buffer + offset;
25349 mac_end = section->buffer + section->size;
25350
25351 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
25352 &offset_size, section_is_gnu);
25353 if (mac_ptr == NULL)
25354 {
25355 /* We already issued a complaint. */
25356 return;
25357 }
25358
25359 do
25360 {
25361 /* Do we at least have room for a macinfo type byte? */
25362 if (mac_ptr >= mac_end)
25363 {
25364 /* Complaint is printed during the second pass as GDB will probably
25365 stop the first pass earlier upon finding
25366 DW_MACINFO_start_file. */
25367 break;
25368 }
25369
25370 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
25371 mac_ptr++;
25372
25373 /* Note that we rely on the fact that the corresponding GNU and
25374 DWARF constants are the same. */
25375 DIAGNOSTIC_PUSH
25376 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
25377 switch (macinfo_type)
25378 {
25379 /* A zero macinfo type indicates the end of the macro
25380 information. */
25381 case 0:
25382 break;
25383
25384 case DW_MACRO_define:
25385 case DW_MACRO_undef:
25386 /* Only skip the data by MAC_PTR. */
25387 {
25388 unsigned int bytes_read;
25389
25390 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25391 mac_ptr += bytes_read;
25392 read_direct_string (abfd, mac_ptr, &bytes_read);
25393 mac_ptr += bytes_read;
25394 }
25395 break;
25396
25397 case DW_MACRO_start_file:
25398 {
25399 unsigned int bytes_read;
25400 int line, file;
25401
25402 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25403 mac_ptr += bytes_read;
25404 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25405 mac_ptr += bytes_read;
25406
25407 current_file = macro_start_file (cu, file, line, current_file, lh);
25408 }
25409 break;
25410
25411 case DW_MACRO_end_file:
25412 /* No data to skip by MAC_PTR. */
25413 break;
25414
25415 case DW_MACRO_define_strp:
25416 case DW_MACRO_undef_strp:
25417 case DW_MACRO_define_sup:
25418 case DW_MACRO_undef_sup:
25419 {
25420 unsigned int bytes_read;
25421
25422 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25423 mac_ptr += bytes_read;
25424 mac_ptr += offset_size;
25425 }
25426 break;
25427
25428 case DW_MACRO_import:
25429 case DW_MACRO_import_sup:
25430 /* Note that, according to the spec, a transparent include
25431 chain cannot call DW_MACRO_start_file. So, we can just
25432 skip this opcode. */
25433 mac_ptr += offset_size;
25434 break;
25435
25436 case DW_MACINFO_vendor_ext:
25437 /* Only skip the data by MAC_PTR. */
25438 if (!section_is_gnu)
25439 {
25440 unsigned int bytes_read;
25441
25442 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25443 mac_ptr += bytes_read;
25444 read_direct_string (abfd, mac_ptr, &bytes_read);
25445 mac_ptr += bytes_read;
25446 }
25447 /* FALLTHROUGH */
25448
25449 default:
25450 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
25451 mac_ptr, mac_end, abfd, offset_size,
25452 section);
25453 if (mac_ptr == NULL)
25454 return;
25455 break;
25456 }
25457 DIAGNOSTIC_POP
25458 } while (macinfo_type != 0 && current_file == NULL);
25459
25460 /* Second pass: Process all entries.
25461
25462 Use the AT_COMMAND_LINE flag to determine whether we are still processing
25463 command-line macro definitions/undefinitions. This flag is unset when we
25464 reach the first DW_MACINFO_start_file entry. */
25465
25466 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
25467 htab_eq_pointer,
25468 NULL, xcalloc, xfree));
25469 mac_ptr = section->buffer + offset;
25470 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
25471 *slot = (void *) mac_ptr;
25472 dwarf_decode_macro_bytes (cu, abfd, mac_ptr, mac_end,
25473 current_file, lh, section,
25474 section_is_gnu, 0, offset_size,
25475 include_hash.get ());
25476 }
25477
25478 /* Check if the attribute's form is a DW_FORM_block*
25479 if so return true else false. */
25480
25481 static int
25482 attr_form_is_block (const struct attribute *attr)
25483 {
25484 return (attr == NULL ? 0 :
25485 attr->form == DW_FORM_block1
25486 || attr->form == DW_FORM_block2
25487 || attr->form == DW_FORM_block4
25488 || attr->form == DW_FORM_block
25489 || attr->form == DW_FORM_exprloc);
25490 }
25491
25492 /* Return non-zero if ATTR's value is a section offset --- classes
25493 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
25494 You may use DW_UNSND (attr) to retrieve such offsets.
25495
25496 Section 7.5.4, "Attribute Encodings", explains that no attribute
25497 may have a value that belongs to more than one of these classes; it
25498 would be ambiguous if we did, because we use the same forms for all
25499 of them. */
25500
25501 static int
25502 attr_form_is_section_offset (const struct attribute *attr)
25503 {
25504 return (attr->form == DW_FORM_data4
25505 || attr->form == DW_FORM_data8
25506 || attr->form == DW_FORM_sec_offset);
25507 }
25508
25509 /* Return non-zero if ATTR's value falls in the 'constant' class, or
25510 zero otherwise. When this function returns true, you can apply
25511 dwarf2_get_attr_constant_value to it.
25512
25513 However, note that for some attributes you must check
25514 attr_form_is_section_offset before using this test. DW_FORM_data4
25515 and DW_FORM_data8 are members of both the constant class, and of
25516 the classes that contain offsets into other debug sections
25517 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
25518 that, if an attribute's can be either a constant or one of the
25519 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
25520 taken as section offsets, not constants.
25521
25522 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
25523 cannot handle that. */
25524
25525 static int
25526 attr_form_is_constant (const struct attribute *attr)
25527 {
25528 switch (attr->form)
25529 {
25530 case DW_FORM_sdata:
25531 case DW_FORM_udata:
25532 case DW_FORM_data1:
25533 case DW_FORM_data2:
25534 case DW_FORM_data4:
25535 case DW_FORM_data8:
25536 case DW_FORM_implicit_const:
25537 return 1;
25538 default:
25539 return 0;
25540 }
25541 }
25542
25543
25544 /* DW_ADDR is always stored already as sect_offset; despite for the forms
25545 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
25546
25547 static int
25548 attr_form_is_ref (const struct attribute *attr)
25549 {
25550 switch (attr->form)
25551 {
25552 case DW_FORM_ref_addr:
25553 case DW_FORM_ref1:
25554 case DW_FORM_ref2:
25555 case DW_FORM_ref4:
25556 case DW_FORM_ref8:
25557 case DW_FORM_ref_udata:
25558 case DW_FORM_GNU_ref_alt:
25559 return 1;
25560 default:
25561 return 0;
25562 }
25563 }
25564
25565 /* Return the .debug_loc section to use for CU.
25566 For DWO files use .debug_loc.dwo. */
25567
25568 static struct dwarf2_section_info *
25569 cu_debug_loc_section (struct dwarf2_cu *cu)
25570 {
25571 struct dwarf2_per_objfile *dwarf2_per_objfile
25572 = cu->per_cu->dwarf2_per_objfile;
25573
25574 if (cu->dwo_unit)
25575 {
25576 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
25577
25578 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
25579 }
25580 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
25581 : &dwarf2_per_objfile->loc);
25582 }
25583
25584 /* A helper function that fills in a dwarf2_loclist_baton. */
25585
25586 static void
25587 fill_in_loclist_baton (struct dwarf2_cu *cu,
25588 struct dwarf2_loclist_baton *baton,
25589 const struct attribute *attr)
25590 {
25591 struct dwarf2_per_objfile *dwarf2_per_objfile
25592 = cu->per_cu->dwarf2_per_objfile;
25593 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
25594
25595 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
25596
25597 baton->per_cu = cu->per_cu;
25598 gdb_assert (baton->per_cu);
25599 /* We don't know how long the location list is, but make sure we
25600 don't run off the edge of the section. */
25601 baton->size = section->size - DW_UNSND (attr);
25602 baton->data = section->buffer + DW_UNSND (attr);
25603 baton->base_address = cu->base_address;
25604 baton->from_dwo = cu->dwo_unit != NULL;
25605 }
25606
25607 static void
25608 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
25609 struct dwarf2_cu *cu, int is_block)
25610 {
25611 struct dwarf2_per_objfile *dwarf2_per_objfile
25612 = cu->per_cu->dwarf2_per_objfile;
25613 struct objfile *objfile = dwarf2_per_objfile->objfile;
25614 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
25615
25616 if (attr_form_is_section_offset (attr)
25617 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
25618 the section. If so, fall through to the complaint in the
25619 other branch. */
25620 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
25621 {
25622 struct dwarf2_loclist_baton *baton;
25623
25624 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
25625
25626 fill_in_loclist_baton (cu, baton, attr);
25627
25628 if (cu->base_known == 0)
25629 complaint (_("Location list used without "
25630 "specifying the CU base address."));
25631
25632 SYMBOL_ACLASS_INDEX (sym) = (is_block
25633 ? dwarf2_loclist_block_index
25634 : dwarf2_loclist_index);
25635 SYMBOL_LOCATION_BATON (sym) = baton;
25636 }
25637 else
25638 {
25639 struct dwarf2_locexpr_baton *baton;
25640
25641 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
25642 baton->per_cu = cu->per_cu;
25643 gdb_assert (baton->per_cu);
25644
25645 if (attr_form_is_block (attr))
25646 {
25647 /* Note that we're just copying the block's data pointer
25648 here, not the actual data. We're still pointing into the
25649 info_buffer for SYM's objfile; right now we never release
25650 that buffer, but when we do clean up properly this may
25651 need to change. */
25652 baton->size = DW_BLOCK (attr)->size;
25653 baton->data = DW_BLOCK (attr)->data;
25654 }
25655 else
25656 {
25657 dwarf2_invalid_attrib_class_complaint ("location description",
25658 sym->natural_name ());
25659 baton->size = 0;
25660 }
25661
25662 SYMBOL_ACLASS_INDEX (sym) = (is_block
25663 ? dwarf2_locexpr_block_index
25664 : dwarf2_locexpr_index);
25665 SYMBOL_LOCATION_BATON (sym) = baton;
25666 }
25667 }
25668
25669 /* Return the OBJFILE associated with the compilation unit CU. If CU
25670 came from a separate debuginfo file, then the master objfile is
25671 returned. */
25672
25673 struct objfile *
25674 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
25675 {
25676 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
25677
25678 /* Return the master objfile, so that we can report and look up the
25679 correct file containing this variable. */
25680 if (objfile->separate_debug_objfile_backlink)
25681 objfile = objfile->separate_debug_objfile_backlink;
25682
25683 return objfile;
25684 }
25685
25686 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
25687 (CU_HEADERP is unused in such case) or prepare a temporary copy at
25688 CU_HEADERP first. */
25689
25690 static const struct comp_unit_head *
25691 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
25692 struct dwarf2_per_cu_data *per_cu)
25693 {
25694 const gdb_byte *info_ptr;
25695
25696 if (per_cu->cu)
25697 return &per_cu->cu->header;
25698
25699 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
25700
25701 memset (cu_headerp, 0, sizeof (*cu_headerp));
25702 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
25703 rcuh_kind::COMPILE);
25704
25705 return cu_headerp;
25706 }
25707
25708 /* Return the address size given in the compilation unit header for CU. */
25709
25710 int
25711 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
25712 {
25713 struct comp_unit_head cu_header_local;
25714 const struct comp_unit_head *cu_headerp;
25715
25716 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25717
25718 return cu_headerp->addr_size;
25719 }
25720
25721 /* Return the offset size given in the compilation unit header for CU. */
25722
25723 int
25724 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
25725 {
25726 struct comp_unit_head cu_header_local;
25727 const struct comp_unit_head *cu_headerp;
25728
25729 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25730
25731 return cu_headerp->offset_size;
25732 }
25733
25734 /* See its dwarf2loc.h declaration. */
25735
25736 int
25737 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
25738 {
25739 struct comp_unit_head cu_header_local;
25740 const struct comp_unit_head *cu_headerp;
25741
25742 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25743
25744 if (cu_headerp->version == 2)
25745 return cu_headerp->addr_size;
25746 else
25747 return cu_headerp->offset_size;
25748 }
25749
25750 /* Return the text offset of the CU. The returned offset comes from
25751 this CU's objfile. If this objfile came from a separate debuginfo
25752 file, then the offset may be different from the corresponding
25753 offset in the parent objfile. */
25754
25755 CORE_ADDR
25756 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
25757 {
25758 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
25759
25760 return objfile->section_offsets[SECT_OFF_TEXT (objfile)];
25761 }
25762
25763 /* Return a type that is a generic pointer type, the size of which matches
25764 the address size given in the compilation unit header for PER_CU. */
25765 static struct type *
25766 dwarf2_per_cu_addr_type (struct dwarf2_per_cu_data *per_cu)
25767 {
25768 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
25769 struct type *void_type = objfile_type (objfile)->builtin_void;
25770 struct type *addr_type = lookup_pointer_type (void_type);
25771 int addr_size = dwarf2_per_cu_addr_size (per_cu);
25772
25773 if (TYPE_LENGTH (addr_type) == addr_size)
25774 return addr_type;
25775
25776 addr_type
25777 = dwarf2_per_cu_addr_sized_int_type (per_cu, TYPE_UNSIGNED (addr_type));
25778 return addr_type;
25779 }
25780
25781 /* Return DWARF version number of PER_CU. */
25782
25783 short
25784 dwarf2_version (struct dwarf2_per_cu_data *per_cu)
25785 {
25786 return per_cu->dwarf_version;
25787 }
25788
25789 /* Locate the .debug_info compilation unit from CU's objfile which contains
25790 the DIE at OFFSET. Raises an error on failure. */
25791
25792 static struct dwarf2_per_cu_data *
25793 dwarf2_find_containing_comp_unit (sect_offset sect_off,
25794 unsigned int offset_in_dwz,
25795 struct dwarf2_per_objfile *dwarf2_per_objfile)
25796 {
25797 struct dwarf2_per_cu_data *this_cu;
25798 int low, high;
25799
25800 low = 0;
25801 high = dwarf2_per_objfile->all_comp_units.size () - 1;
25802 while (high > low)
25803 {
25804 struct dwarf2_per_cu_data *mid_cu;
25805 int mid = low + (high - low) / 2;
25806
25807 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
25808 if (mid_cu->is_dwz > offset_in_dwz
25809 || (mid_cu->is_dwz == offset_in_dwz
25810 && mid_cu->sect_off + mid_cu->length >= sect_off))
25811 high = mid;
25812 else
25813 low = mid + 1;
25814 }
25815 gdb_assert (low == high);
25816 this_cu = dwarf2_per_objfile->all_comp_units[low];
25817 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
25818 {
25819 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
25820 error (_("Dwarf Error: could not find partial DIE containing "
25821 "offset %s [in module %s]"),
25822 sect_offset_str (sect_off),
25823 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
25824
25825 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
25826 <= sect_off);
25827 return dwarf2_per_objfile->all_comp_units[low-1];
25828 }
25829 else
25830 {
25831 if (low == dwarf2_per_objfile->all_comp_units.size () - 1
25832 && sect_off >= this_cu->sect_off + this_cu->length)
25833 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
25834 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
25835 return this_cu;
25836 }
25837 }
25838
25839 /* Initialize dwarf2_cu CU, owned by PER_CU. */
25840
25841 dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
25842 : per_cu (per_cu_),
25843 mark (false),
25844 has_loclist (false),
25845 checked_producer (false),
25846 producer_is_gxx_lt_4_6 (false),
25847 producer_is_gcc_lt_4_3 (false),
25848 producer_is_icc (false),
25849 producer_is_icc_lt_14 (false),
25850 producer_is_codewarrior (false),
25851 processing_has_namespace_info (false)
25852 {
25853 per_cu->cu = this;
25854 }
25855
25856 /* Destroy a dwarf2_cu. */
25857
25858 dwarf2_cu::~dwarf2_cu ()
25859 {
25860 per_cu->cu = NULL;
25861 }
25862
25863 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
25864
25865 static void
25866 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
25867 enum language pretend_language)
25868 {
25869 struct attribute *attr;
25870
25871 /* Set the language we're debugging. */
25872 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
25873 if (attr != nullptr)
25874 set_cu_language (DW_UNSND (attr), cu);
25875 else
25876 {
25877 cu->language = pretend_language;
25878 cu->language_defn = language_def (cu->language);
25879 }
25880
25881 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
25882 }
25883
25884 /* Increase the age counter on each cached compilation unit, and free
25885 any that are too old. */
25886
25887 static void
25888 age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
25889 {
25890 struct dwarf2_per_cu_data *per_cu, **last_chain;
25891
25892 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
25893 per_cu = dwarf2_per_objfile->read_in_chain;
25894 while (per_cu != NULL)
25895 {
25896 per_cu->cu->last_used ++;
25897 if (per_cu->cu->last_used <= dwarf_max_cache_age)
25898 dwarf2_mark (per_cu->cu);
25899 per_cu = per_cu->cu->read_in_chain;
25900 }
25901
25902 per_cu = dwarf2_per_objfile->read_in_chain;
25903 last_chain = &dwarf2_per_objfile->read_in_chain;
25904 while (per_cu != NULL)
25905 {
25906 struct dwarf2_per_cu_data *next_cu;
25907
25908 next_cu = per_cu->cu->read_in_chain;
25909
25910 if (!per_cu->cu->mark)
25911 {
25912 delete per_cu->cu;
25913 *last_chain = next_cu;
25914 }
25915 else
25916 last_chain = &per_cu->cu->read_in_chain;
25917
25918 per_cu = next_cu;
25919 }
25920 }
25921
25922 /* Remove a single compilation unit from the cache. */
25923
25924 static void
25925 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
25926 {
25927 struct dwarf2_per_cu_data *per_cu, **last_chain;
25928 struct dwarf2_per_objfile *dwarf2_per_objfile
25929 = target_per_cu->dwarf2_per_objfile;
25930
25931 per_cu = dwarf2_per_objfile->read_in_chain;
25932 last_chain = &dwarf2_per_objfile->read_in_chain;
25933 while (per_cu != NULL)
25934 {
25935 struct dwarf2_per_cu_data *next_cu;
25936
25937 next_cu = per_cu->cu->read_in_chain;
25938
25939 if (per_cu == target_per_cu)
25940 {
25941 delete per_cu->cu;
25942 per_cu->cu = NULL;
25943 *last_chain = next_cu;
25944 break;
25945 }
25946 else
25947 last_chain = &per_cu->cu->read_in_chain;
25948
25949 per_cu = next_cu;
25950 }
25951 }
25952
25953 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25954 We store these in a hash table separate from the DIEs, and preserve them
25955 when the DIEs are flushed out of cache.
25956
25957 The CU "per_cu" pointer is needed because offset alone is not enough to
25958 uniquely identify the type. A file may have multiple .debug_types sections,
25959 or the type may come from a DWO file. Furthermore, while it's more logical
25960 to use per_cu->section+offset, with Fission the section with the data is in
25961 the DWO file but we don't know that section at the point we need it.
25962 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25963 because we can enter the lookup routine, get_die_type_at_offset, from
25964 outside this file, and thus won't necessarily have PER_CU->cu.
25965 Fortunately, PER_CU is stable for the life of the objfile. */
25966
25967 struct dwarf2_per_cu_offset_and_type
25968 {
25969 const struct dwarf2_per_cu_data *per_cu;
25970 sect_offset sect_off;
25971 struct type *type;
25972 };
25973
25974 /* Hash function for a dwarf2_per_cu_offset_and_type. */
25975
25976 static hashval_t
25977 per_cu_offset_and_type_hash (const void *item)
25978 {
25979 const struct dwarf2_per_cu_offset_and_type *ofs
25980 = (const struct dwarf2_per_cu_offset_and_type *) item;
25981
25982 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
25983 }
25984
25985 /* Equality function for a dwarf2_per_cu_offset_and_type. */
25986
25987 static int
25988 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
25989 {
25990 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25991 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25992 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25993 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
25994
25995 return (ofs_lhs->per_cu == ofs_rhs->per_cu
25996 && ofs_lhs->sect_off == ofs_rhs->sect_off);
25997 }
25998
25999 /* Set the type associated with DIE to TYPE. Save it in CU's hash
26000 table if necessary. For convenience, return TYPE.
26001
26002 The DIEs reading must have careful ordering to:
26003 * Not cause infinite loops trying to read in DIEs as a prerequisite for
26004 reading current DIE.
26005 * Not trying to dereference contents of still incompletely read in types
26006 while reading in other DIEs.
26007 * Enable referencing still incompletely read in types just by a pointer to
26008 the type without accessing its fields.
26009
26010 Therefore caller should follow these rules:
26011 * Try to fetch any prerequisite types we may need to build this DIE type
26012 before building the type and calling set_die_type.
26013 * After building type call set_die_type for current DIE as soon as
26014 possible before fetching more types to complete the current type.
26015 * Make the type as complete as possible before fetching more types. */
26016
26017 static struct type *
26018 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
26019 {
26020 struct dwarf2_per_objfile *dwarf2_per_objfile
26021 = cu->per_cu->dwarf2_per_objfile;
26022 struct dwarf2_per_cu_offset_and_type **slot, ofs;
26023 struct objfile *objfile = dwarf2_per_objfile->objfile;
26024 struct attribute *attr;
26025 struct dynamic_prop prop;
26026
26027 /* For Ada types, make sure that the gnat-specific data is always
26028 initialized (if not already set). There are a few types where
26029 we should not be doing so, because the type-specific area is
26030 already used to hold some other piece of info (eg: TYPE_CODE_FLT
26031 where the type-specific area is used to store the floatformat).
26032 But this is not a problem, because the gnat-specific information
26033 is actually not needed for these types. */
26034 if (need_gnat_info (cu)
26035 && TYPE_CODE (type) != TYPE_CODE_FUNC
26036 && TYPE_CODE (type) != TYPE_CODE_FLT
26037 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
26038 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
26039 && TYPE_CODE (type) != TYPE_CODE_METHOD
26040 && !HAVE_GNAT_AUX_INFO (type))
26041 INIT_GNAT_SPECIFIC (type);
26042
26043 /* Read DW_AT_allocated and set in type. */
26044 attr = dwarf2_attr (die, DW_AT_allocated, cu);
26045 if (attr_form_is_block (attr))
26046 {
26047 struct type *prop_type
26048 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
26049 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
26050 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
26051 }
26052 else if (attr != NULL)
26053 {
26054 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
26055 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
26056 sect_offset_str (die->sect_off));
26057 }
26058
26059 /* Read DW_AT_associated and set in type. */
26060 attr = dwarf2_attr (die, DW_AT_associated, cu);
26061 if (attr_form_is_block (attr))
26062 {
26063 struct type *prop_type
26064 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
26065 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
26066 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
26067 }
26068 else if (attr != NULL)
26069 {
26070 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
26071 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
26072 sect_offset_str (die->sect_off));
26073 }
26074
26075 /* Read DW_AT_data_location and set in type. */
26076 attr = dwarf2_attr (die, DW_AT_data_location, cu);
26077 if (attr_to_dynamic_prop (attr, die, cu, &prop,
26078 dwarf2_per_cu_addr_type (cu->per_cu)))
26079 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
26080
26081 if (dwarf2_per_objfile->die_type_hash == NULL)
26082 {
26083 dwarf2_per_objfile->die_type_hash =
26084 htab_create_alloc_ex (127,
26085 per_cu_offset_and_type_hash,
26086 per_cu_offset_and_type_eq,
26087 NULL,
26088 &objfile->objfile_obstack,
26089 hashtab_obstack_allocate,
26090 dummy_obstack_deallocate);
26091 }
26092
26093 ofs.per_cu = cu->per_cu;
26094 ofs.sect_off = die->sect_off;
26095 ofs.type = type;
26096 slot = (struct dwarf2_per_cu_offset_and_type **)
26097 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
26098 if (*slot)
26099 complaint (_("A problem internal to GDB: DIE %s has type already set"),
26100 sect_offset_str (die->sect_off));
26101 *slot = XOBNEW (&objfile->objfile_obstack,
26102 struct dwarf2_per_cu_offset_and_type);
26103 **slot = ofs;
26104 return type;
26105 }
26106
26107 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
26108 or return NULL if the die does not have a saved type. */
26109
26110 static struct type *
26111 get_die_type_at_offset (sect_offset sect_off,
26112 struct dwarf2_per_cu_data *per_cu)
26113 {
26114 struct dwarf2_per_cu_offset_and_type *slot, ofs;
26115 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
26116
26117 if (dwarf2_per_objfile->die_type_hash == NULL)
26118 return NULL;
26119
26120 ofs.per_cu = per_cu;
26121 ofs.sect_off = sect_off;
26122 slot = ((struct dwarf2_per_cu_offset_and_type *)
26123 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
26124 if (slot)
26125 return slot->type;
26126 else
26127 return NULL;
26128 }
26129
26130 /* Look up the type for DIE in CU in die_type_hash,
26131 or return NULL if DIE does not have a saved type. */
26132
26133 static struct type *
26134 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
26135 {
26136 return get_die_type_at_offset (die->sect_off, cu->per_cu);
26137 }
26138
26139 /* Add a dependence relationship from CU to REF_PER_CU. */
26140
26141 static void
26142 dwarf2_add_dependence (struct dwarf2_cu *cu,
26143 struct dwarf2_per_cu_data *ref_per_cu)
26144 {
26145 void **slot;
26146
26147 if (cu->dependencies == NULL)
26148 cu->dependencies
26149 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
26150 NULL, &cu->comp_unit_obstack,
26151 hashtab_obstack_allocate,
26152 dummy_obstack_deallocate);
26153
26154 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
26155 if (*slot == NULL)
26156 *slot = ref_per_cu;
26157 }
26158
26159 /* Subroutine of dwarf2_mark to pass to htab_traverse.
26160 Set the mark field in every compilation unit in the
26161 cache that we must keep because we are keeping CU. */
26162
26163 static int
26164 dwarf2_mark_helper (void **slot, void *data)
26165 {
26166 struct dwarf2_per_cu_data *per_cu;
26167
26168 per_cu = (struct dwarf2_per_cu_data *) *slot;
26169
26170 /* cu->dependencies references may not yet have been ever read if QUIT aborts
26171 reading of the chain. As such dependencies remain valid it is not much
26172 useful to track and undo them during QUIT cleanups. */
26173 if (per_cu->cu == NULL)
26174 return 1;
26175
26176 if (per_cu->cu->mark)
26177 return 1;
26178 per_cu->cu->mark = true;
26179
26180 if (per_cu->cu->dependencies != NULL)
26181 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
26182
26183 return 1;
26184 }
26185
26186 /* Set the mark field in CU and in every other compilation unit in the
26187 cache that we must keep because we are keeping CU. */
26188
26189 static void
26190 dwarf2_mark (struct dwarf2_cu *cu)
26191 {
26192 if (cu->mark)
26193 return;
26194 cu->mark = true;
26195 if (cu->dependencies != NULL)
26196 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
26197 }
26198
26199 static void
26200 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
26201 {
26202 while (per_cu)
26203 {
26204 per_cu->cu->mark = false;
26205 per_cu = per_cu->cu->read_in_chain;
26206 }
26207 }
26208
26209 /* Trivial hash function for partial_die_info: the hash value of a DIE
26210 is its offset in .debug_info for this objfile. */
26211
26212 static hashval_t
26213 partial_die_hash (const void *item)
26214 {
26215 const struct partial_die_info *part_die
26216 = (const struct partial_die_info *) item;
26217
26218 return to_underlying (part_die->sect_off);
26219 }
26220
26221 /* Trivial comparison function for partial_die_info structures: two DIEs
26222 are equal if they have the same offset. */
26223
26224 static int
26225 partial_die_eq (const void *item_lhs, const void *item_rhs)
26226 {
26227 const struct partial_die_info *part_die_lhs
26228 = (const struct partial_die_info *) item_lhs;
26229 const struct partial_die_info *part_die_rhs
26230 = (const struct partial_die_info *) item_rhs;
26231
26232 return part_die_lhs->sect_off == part_die_rhs->sect_off;
26233 }
26234
26235 struct cmd_list_element *set_dwarf_cmdlist;
26236 struct cmd_list_element *show_dwarf_cmdlist;
26237
26238 static void
26239 set_dwarf_cmd (const char *args, int from_tty)
26240 {
26241 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
26242 gdb_stdout);
26243 }
26244
26245 static void
26246 show_dwarf_cmd (const char *args, int from_tty)
26247 {
26248 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
26249 }
26250
26251 bool dwarf_always_disassemble;
26252
26253 static void
26254 show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
26255 struct cmd_list_element *c, const char *value)
26256 {
26257 fprintf_filtered (file,
26258 _("Whether to always disassemble "
26259 "DWARF expressions is %s.\n"),
26260 value);
26261 }
26262
26263 static void
26264 show_check_physname (struct ui_file *file, int from_tty,
26265 struct cmd_list_element *c, const char *value)
26266 {
26267 fprintf_filtered (file,
26268 _("Whether to check \"physname\" is %s.\n"),
26269 value);
26270 }
26271
26272 void _initialize_dwarf2_read ();
26273 void
26274 _initialize_dwarf2_read ()
26275 {
26276 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
26277 Set DWARF specific variables.\n\
26278 Configure DWARF variables such as the cache size."),
26279 &set_dwarf_cmdlist, "maintenance set dwarf ",
26280 0/*allow-unknown*/, &maintenance_set_cmdlist);
26281
26282 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
26283 Show DWARF specific variables.\n\
26284 Show DWARF variables such as the cache size."),
26285 &show_dwarf_cmdlist, "maintenance show dwarf ",
26286 0/*allow-unknown*/, &maintenance_show_cmdlist);
26287
26288 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
26289 &dwarf_max_cache_age, _("\
26290 Set the upper bound on the age of cached DWARF compilation units."), _("\
26291 Show the upper bound on the age of cached DWARF compilation units."), _("\
26292 A higher limit means that cached compilation units will be stored\n\
26293 in memory longer, and more total memory will be used. Zero disables\n\
26294 caching, which can slow down startup."),
26295 NULL,
26296 show_dwarf_max_cache_age,
26297 &set_dwarf_cmdlist,
26298 &show_dwarf_cmdlist);
26299
26300 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
26301 &dwarf_always_disassemble, _("\
26302 Set whether `info address' always disassembles DWARF expressions."), _("\
26303 Show whether `info address' always disassembles DWARF expressions."), _("\
26304 When enabled, DWARF expressions are always printed in an assembly-like\n\
26305 syntax. When disabled, expressions will be printed in a more\n\
26306 conversational style, when possible."),
26307 NULL,
26308 show_dwarf_always_disassemble,
26309 &set_dwarf_cmdlist,
26310 &show_dwarf_cmdlist);
26311
26312 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
26313 Set debugging of the DWARF reader."), _("\
26314 Show debugging of the DWARF reader."), _("\
26315 When enabled (non-zero), debugging messages are printed during DWARF\n\
26316 reading and symtab expansion. A value of 1 (one) provides basic\n\
26317 information. A value greater than 1 provides more verbose information."),
26318 NULL,
26319 NULL,
26320 &setdebuglist, &showdebuglist);
26321
26322 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
26323 Set debugging of the DWARF DIE reader."), _("\
26324 Show debugging of the DWARF DIE reader."), _("\
26325 When enabled (non-zero), DIEs are dumped after they are read in.\n\
26326 The value is the maximum depth to print."),
26327 NULL,
26328 NULL,
26329 &setdebuglist, &showdebuglist);
26330
26331 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
26332 Set debugging of the dwarf line reader."), _("\
26333 Show debugging of the dwarf line reader."), _("\
26334 When enabled (non-zero), line number entries are dumped as they are read in.\n\
26335 A value of 1 (one) provides basic information.\n\
26336 A value greater than 1 provides more verbose information."),
26337 NULL,
26338 NULL,
26339 &setdebuglist, &showdebuglist);
26340
26341 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
26342 Set cross-checking of \"physname\" code against demangler."), _("\
26343 Show cross-checking of \"physname\" code against demangler."), _("\
26344 When enabled, GDB's internal \"physname\" code is checked against\n\
26345 the demangler."),
26346 NULL, show_check_physname,
26347 &setdebuglist, &showdebuglist);
26348
26349 add_setshow_boolean_cmd ("use-deprecated-index-sections",
26350 no_class, &use_deprecated_index_sections, _("\
26351 Set whether to use deprecated gdb_index sections."), _("\
26352 Show whether to use deprecated gdb_index sections."), _("\
26353 When enabled, deprecated .gdb_index sections are used anyway.\n\
26354 Normally they are ignored either because of a missing feature or\n\
26355 performance issue.\n\
26356 Warning: This option must be enabled before gdb reads the file."),
26357 NULL,
26358 NULL,
26359 &setlist, &showlist);
26360
26361 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
26362 &dwarf2_locexpr_funcs);
26363 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
26364 &dwarf2_loclist_funcs);
26365
26366 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
26367 &dwarf2_block_frame_base_locexpr_funcs);
26368 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
26369 &dwarf2_block_frame_base_loclist_funcs);
26370
26371 #if GDB_SELF_TEST
26372 selftests::register_test ("dw2_expand_symtabs_matching",
26373 selftests::dw2_expand_symtabs_matching::run_test);
26374 #endif
26375 }