dafe01d94a08240e146d12b4b4a3055730024dcb
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
1 /* DWARF 2 debugging format support for GDB.
2
3 Copyright (C) 1994-2020 Free Software Foundation, Inc.
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
10 support.
11
12 This file is part of GDB.
13
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26
27 /* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
31 #include "defs.h"
32 #include "dwarf2read.h"
33 #include "dwarf-index-cache.h"
34 #include "dwarf-index-common.h"
35 #include "bfd.h"
36 #include "elf-bfd.h"
37 #include "symtab.h"
38 #include "gdbtypes.h"
39 #include "objfiles.h"
40 #include "dwarf2.h"
41 #include "buildsym.h"
42 #include "demangle.h"
43 #include "gdb-demangle.h"
44 #include "filenames.h" /* for DOSish file names */
45 #include "macrotab.h"
46 #include "language.h"
47 #include "complaints.h"
48 #include "dwarf2expr.h"
49 #include "dwarf2loc.h"
50 #include "cp-support.h"
51 #include "hashtab.h"
52 #include "command.h"
53 #include "gdbcmd.h"
54 #include "block.h"
55 #include "addrmap.h"
56 #include "typeprint.h"
57 #include "psympriv.h"
58 #include "c-lang.h"
59 #include "go-lang.h"
60 #include "valprint.h"
61 #include "gdbcore.h" /* for gnutarget */
62 #include "gdb/gdb-index.h"
63 #include "gdb_bfd.h"
64 #include "f-lang.h"
65 #include "source.h"
66 #include "build-id.h"
67 #include "namespace.h"
68 #include "gdbsupport/function-view.h"
69 #include "gdbsupport/gdb_optional.h"
70 #include "gdbsupport/underlying.h"
71 #include "gdbsupport/hash_enum.h"
72 #include "filename-seen-cache.h"
73 #include "producer.h"
74 #include <fcntl.h>
75 #include <algorithm>
76 #include <unordered_map>
77 #include "gdbsupport/selftest.h"
78 #include "rust-lang.h"
79 #include "gdbsupport/pathstuff.h"
80
81 /* When == 1, print basic high level tracing messages.
82 When > 1, be more verbose.
83 This is in contrast to the low level DIE reading of dwarf_die_debug. */
84 static unsigned int dwarf_read_debug = 0;
85
86 /* When non-zero, dump DIEs after they are read in. */
87 static unsigned int dwarf_die_debug = 0;
88
89 /* When non-zero, dump line number entries as they are read in. */
90 static unsigned int dwarf_line_debug = 0;
91
92 /* When true, cross-check physname against demangler. */
93 static bool check_physname = false;
94
95 /* When true, do not reject deprecated .gdb_index sections. */
96 static bool use_deprecated_index_sections = false;
97
98 static const struct objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
99
100 /* The "aclass" indices for various kinds of computed DWARF symbols. */
101
102 static int dwarf2_locexpr_index;
103 static int dwarf2_loclist_index;
104 static int dwarf2_locexpr_block_index;
105 static int dwarf2_loclist_block_index;
106
107 /* An index into a (C++) symbol name component in a symbol name as
108 recorded in the mapped_index's symbol table. For each C++ symbol
109 in the symbol table, we record one entry for the start of each
110 component in the symbol in a table of name components, and then
111 sort the table, in order to be able to binary search symbol names,
112 ignoring leading namespaces, both completion and regular look up.
113 For example, for symbol "A::B::C", we'll have an entry that points
114 to "A::B::C", another that points to "B::C", and another for "C".
115 Note that function symbols in GDB index have no parameter
116 information, just the function/method names. You can convert a
117 name_component to a "const char *" using the
118 'mapped_index::symbol_name_at(offset_type)' method. */
119
120 struct name_component
121 {
122 /* Offset in the symbol name where the component starts. Stored as
123 a (32-bit) offset instead of a pointer to save memory and improve
124 locality on 64-bit architectures. */
125 offset_type name_offset;
126
127 /* The symbol's index in the symbol and constant pool tables of a
128 mapped_index. */
129 offset_type idx;
130 };
131
132 /* Base class containing bits shared by both .gdb_index and
133 .debug_name indexes. */
134
135 struct mapped_index_base
136 {
137 mapped_index_base () = default;
138 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
139
140 /* The name_component table (a sorted vector). See name_component's
141 description above. */
142 std::vector<name_component> name_components;
143
144 /* How NAME_COMPONENTS is sorted. */
145 enum case_sensitivity name_components_casing;
146
147 /* Return the number of names in the symbol table. */
148 virtual size_t symbol_name_count () const = 0;
149
150 /* Get the name of the symbol at IDX in the symbol table. */
151 virtual const char *symbol_name_at (offset_type idx) const = 0;
152
153 /* Return whether the name at IDX in the symbol table should be
154 ignored. */
155 virtual bool symbol_name_slot_invalid (offset_type idx) const
156 {
157 return false;
158 }
159
160 /* Build the symbol name component sorted vector, if we haven't
161 yet. */
162 void build_name_components ();
163
164 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
165 possible matches for LN_NO_PARAMS in the name component
166 vector. */
167 std::pair<std::vector<name_component>::const_iterator,
168 std::vector<name_component>::const_iterator>
169 find_name_components_bounds (const lookup_name_info &ln_no_params,
170 enum language lang) const;
171
172 /* Prevent deleting/destroying via a base class pointer. */
173 protected:
174 ~mapped_index_base() = default;
175 };
176
177 /* A description of the mapped index. The file format is described in
178 a comment by the code that writes the index. */
179 struct mapped_index final : public mapped_index_base
180 {
181 /* A slot/bucket in the symbol table hash. */
182 struct symbol_table_slot
183 {
184 const offset_type name;
185 const offset_type vec;
186 };
187
188 /* Index data format version. */
189 int version = 0;
190
191 /* The address table data. */
192 gdb::array_view<const gdb_byte> address_table;
193
194 /* The symbol table, implemented as a hash table. */
195 gdb::array_view<symbol_table_slot> symbol_table;
196
197 /* A pointer to the constant pool. */
198 const char *constant_pool = nullptr;
199
200 bool symbol_name_slot_invalid (offset_type idx) const override
201 {
202 const auto &bucket = this->symbol_table[idx];
203 return bucket.name == 0 && bucket.vec == 0;
204 }
205
206 /* Convenience method to get at the name of the symbol at IDX in the
207 symbol table. */
208 const char *symbol_name_at (offset_type idx) const override
209 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
210
211 size_t symbol_name_count () const override
212 { return this->symbol_table.size (); }
213 };
214
215 /* A description of the mapped .debug_names.
216 Uninitialized map has CU_COUNT 0. */
217 struct mapped_debug_names final : public mapped_index_base
218 {
219 mapped_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile_)
220 : dwarf2_per_objfile (dwarf2_per_objfile_)
221 {}
222
223 struct dwarf2_per_objfile *dwarf2_per_objfile;
224 bfd_endian dwarf5_byte_order;
225 bool dwarf5_is_dwarf64;
226 bool augmentation_is_gdb;
227 uint8_t offset_size;
228 uint32_t cu_count = 0;
229 uint32_t tu_count, bucket_count, name_count;
230 const gdb_byte *cu_table_reordered, *tu_table_reordered;
231 const uint32_t *bucket_table_reordered, *hash_table_reordered;
232 const gdb_byte *name_table_string_offs_reordered;
233 const gdb_byte *name_table_entry_offs_reordered;
234 const gdb_byte *entry_pool;
235
236 struct index_val
237 {
238 ULONGEST dwarf_tag;
239 struct attr
240 {
241 /* Attribute name DW_IDX_*. */
242 ULONGEST dw_idx;
243
244 /* Attribute form DW_FORM_*. */
245 ULONGEST form;
246
247 /* Value if FORM is DW_FORM_implicit_const. */
248 LONGEST implicit_const;
249 };
250 std::vector<attr> attr_vec;
251 };
252
253 std::unordered_map<ULONGEST, index_val> abbrev_map;
254
255 const char *namei_to_name (uint32_t namei) const;
256
257 /* Implementation of the mapped_index_base virtual interface, for
258 the name_components cache. */
259
260 const char *symbol_name_at (offset_type idx) const override
261 { return namei_to_name (idx); }
262
263 size_t symbol_name_count () const override
264 { return this->name_count; }
265 };
266
267 /* See dwarf2read.h. */
268
269 dwarf2_per_objfile *
270 get_dwarf2_per_objfile (struct objfile *objfile)
271 {
272 return dwarf2_objfile_data_key.get (objfile);
273 }
274
275 /* Default names of the debugging sections. */
276
277 /* Note that if the debugging section has been compressed, it might
278 have a name like .zdebug_info. */
279
280 static const struct dwarf2_debug_sections dwarf2_elf_names =
281 {
282 { ".debug_info", ".zdebug_info" },
283 { ".debug_abbrev", ".zdebug_abbrev" },
284 { ".debug_line", ".zdebug_line" },
285 { ".debug_loc", ".zdebug_loc" },
286 { ".debug_loclists", ".zdebug_loclists" },
287 { ".debug_macinfo", ".zdebug_macinfo" },
288 { ".debug_macro", ".zdebug_macro" },
289 { ".debug_str", ".zdebug_str" },
290 { ".debug_str_offsets", ".zdebug_str_offsets" },
291 { ".debug_line_str", ".zdebug_line_str" },
292 { ".debug_ranges", ".zdebug_ranges" },
293 { ".debug_rnglists", ".zdebug_rnglists" },
294 { ".debug_types", ".zdebug_types" },
295 { ".debug_addr", ".zdebug_addr" },
296 { ".debug_frame", ".zdebug_frame" },
297 { ".eh_frame", NULL },
298 { ".gdb_index", ".zgdb_index" },
299 { ".debug_names", ".zdebug_names" },
300 { ".debug_aranges", ".zdebug_aranges" },
301 23
302 };
303
304 /* List of DWO/DWP sections. */
305
306 static const struct dwop_section_names
307 {
308 struct dwarf2_section_names abbrev_dwo;
309 struct dwarf2_section_names info_dwo;
310 struct dwarf2_section_names line_dwo;
311 struct dwarf2_section_names loc_dwo;
312 struct dwarf2_section_names loclists_dwo;
313 struct dwarf2_section_names macinfo_dwo;
314 struct dwarf2_section_names macro_dwo;
315 struct dwarf2_section_names str_dwo;
316 struct dwarf2_section_names str_offsets_dwo;
317 struct dwarf2_section_names types_dwo;
318 struct dwarf2_section_names cu_index;
319 struct dwarf2_section_names tu_index;
320 }
321 dwop_section_names =
322 {
323 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
324 { ".debug_info.dwo", ".zdebug_info.dwo" },
325 { ".debug_line.dwo", ".zdebug_line.dwo" },
326 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
327 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
328 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
329 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
330 { ".debug_str.dwo", ".zdebug_str.dwo" },
331 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
332 { ".debug_types.dwo", ".zdebug_types.dwo" },
333 { ".debug_cu_index", ".zdebug_cu_index" },
334 { ".debug_tu_index", ".zdebug_tu_index" },
335 };
336
337 /* local data types */
338
339 /* The data in a compilation unit header, after target2host
340 translation, looks like this. */
341 struct comp_unit_head
342 {
343 unsigned int length;
344 short version;
345 unsigned char addr_size;
346 unsigned char signed_addr_p;
347 sect_offset abbrev_sect_off;
348
349 /* Size of file offsets; either 4 or 8. */
350 unsigned int offset_size;
351
352 /* Size of the length field; either 4 or 12. */
353 unsigned int initial_length_size;
354
355 enum dwarf_unit_type unit_type;
356
357 /* Offset to the first byte of this compilation unit header in the
358 .debug_info section, for resolving relative reference dies. */
359 sect_offset sect_off;
360
361 /* Offset to first die in this cu from the start of the cu.
362 This will be the first byte following the compilation unit header. */
363 cu_offset first_die_cu_offset;
364
365
366 /* 64-bit signature of this unit. For type units, it denotes the signature of
367 the type (DW_UT_type in DWARF 4, additionally DW_UT_split_type in DWARF 5).
368 Also used in DWARF 5, to denote the dwo id when the unit type is
369 DW_UT_skeleton or DW_UT_split_compile. */
370 ULONGEST signature;
371
372 /* For types, offset in the type's DIE of the type defined by this TU. */
373 cu_offset type_cu_offset_in_tu;
374 };
375
376 /* Type used for delaying computation of method physnames.
377 See comments for compute_delayed_physnames. */
378 struct delayed_method_info
379 {
380 /* The type to which the method is attached, i.e., its parent class. */
381 struct type *type;
382
383 /* The index of the method in the type's function fieldlists. */
384 int fnfield_index;
385
386 /* The index of the method in the fieldlist. */
387 int index;
388
389 /* The name of the DIE. */
390 const char *name;
391
392 /* The DIE associated with this method. */
393 struct die_info *die;
394 };
395
396 /* Internal state when decoding a particular compilation unit. */
397 struct dwarf2_cu
398 {
399 explicit dwarf2_cu (struct dwarf2_per_cu_data *per_cu);
400 ~dwarf2_cu ();
401
402 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
403
404 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
405 Create the set of symtabs used by this TU, or if this TU is sharing
406 symtabs with another TU and the symtabs have already been created
407 then restore those symtabs in the line header.
408 We don't need the pc/line-number mapping for type units. */
409 void setup_type_unit_groups (struct die_info *die);
410
411 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
412 buildsym_compunit constructor. */
413 struct compunit_symtab *start_symtab (const char *name,
414 const char *comp_dir,
415 CORE_ADDR low_pc);
416
417 /* Reset the builder. */
418 void reset_builder () { m_builder.reset (); }
419
420 /* The header of the compilation unit. */
421 struct comp_unit_head header {};
422
423 /* Base address of this compilation unit. */
424 CORE_ADDR base_address = 0;
425
426 /* Non-zero if base_address has been set. */
427 int base_known = 0;
428
429 /* The language we are debugging. */
430 enum language language = language_unknown;
431 const struct language_defn *language_defn = nullptr;
432
433 const char *producer = nullptr;
434
435 private:
436 /* The symtab builder for this CU. This is only non-NULL when full
437 symbols are being read. */
438 std::unique_ptr<buildsym_compunit> m_builder;
439
440 public:
441 /* The generic symbol table building routines have separate lists for
442 file scope symbols and all all other scopes (local scopes). So
443 we need to select the right one to pass to add_symbol_to_list().
444 We do it by keeping a pointer to the correct list in list_in_scope.
445
446 FIXME: The original dwarf code just treated the file scope as the
447 first local scope, and all other local scopes as nested local
448 scopes, and worked fine. Check to see if we really need to
449 distinguish these in buildsym.c. */
450 struct pending **list_in_scope = nullptr;
451
452 /* Hash table holding all the loaded partial DIEs
453 with partial_die->offset.SECT_OFF as hash. */
454 htab_t partial_dies = nullptr;
455
456 /* Storage for things with the same lifetime as this read-in compilation
457 unit, including partial DIEs. */
458 auto_obstack comp_unit_obstack;
459
460 /* When multiple dwarf2_cu structures are living in memory, this field
461 chains them all together, so that they can be released efficiently.
462 We will probably also want a generation counter so that most-recently-used
463 compilation units are cached... */
464 struct dwarf2_per_cu_data *read_in_chain = nullptr;
465
466 /* Backlink to our per_cu entry. */
467 struct dwarf2_per_cu_data *per_cu;
468
469 /* How many compilation units ago was this CU last referenced? */
470 int last_used = 0;
471
472 /* A hash table of DIE cu_offset for following references with
473 die_info->offset.sect_off as hash. */
474 htab_t die_hash = nullptr;
475
476 /* Full DIEs if read in. */
477 struct die_info *dies = nullptr;
478
479 /* A set of pointers to dwarf2_per_cu_data objects for compilation
480 units referenced by this one. Only set during full symbol processing;
481 partial symbol tables do not have dependencies. */
482 htab_t dependencies = nullptr;
483
484 /* Header data from the line table, during full symbol processing. */
485 struct line_header *line_header = nullptr;
486 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
487 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
488 this is the DW_TAG_compile_unit die for this CU. We'll hold on
489 to the line header as long as this DIE is being processed. See
490 process_die_scope. */
491 die_info *line_header_die_owner = nullptr;
492
493 /* A list of methods which need to have physnames computed
494 after all type information has been read. */
495 std::vector<delayed_method_info> method_list;
496
497 /* To be copied to symtab->call_site_htab. */
498 htab_t call_site_htab = nullptr;
499
500 /* Non-NULL if this CU came from a DWO file.
501 There is an invariant here that is important to remember:
502 Except for attributes copied from the top level DIE in the "main"
503 (or "stub") file in preparation for reading the DWO file
504 (e.g., DW_AT_addr_base), we KISS: there is only *one* CU.
505 Either there isn't a DWO file (in which case this is NULL and the point
506 is moot), or there is and either we're not going to read it (in which
507 case this is NULL) or there is and we are reading it (in which case this
508 is non-NULL). */
509 struct dwo_unit *dwo_unit = nullptr;
510
511 /* The DW_AT_addr_base (DW_AT_GNU_addr_base) attribute if present.
512 Note this value comes from the Fission stub CU/TU's DIE. */
513 gdb::optional<ULONGEST> addr_base;
514
515 /* The DW_AT_rnglists_base attribute if present.
516 Note this value comes from the Fission stub CU/TU's DIE.
517 Also note that the value is zero in the non-DWO case so this value can
518 be used without needing to know whether DWO files are in use or not.
519 N.B. This does not apply to DW_AT_ranges appearing in
520 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
521 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
522 DW_AT_rnglists_base *would* have to be applied, and we'd have to care
523 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
524 ULONGEST ranges_base = 0;
525
526 /* When reading debug info generated by older versions of rustc, we
527 have to rewrite some union types to be struct types with a
528 variant part. This rewriting must be done after the CU is fully
529 read in, because otherwise at the point of rewriting some struct
530 type might not have been fully processed. So, we keep a list of
531 all such types here and process them after expansion. */
532 std::vector<struct type *> rust_unions;
533
534 /* The DW_AT_str_offsets_base attribute if present. For DWARF 4 version DWO
535 files, the value is implicitly zero. For DWARF 5 version DWO files, the
536 value is often implicit and is the size of the header of
537 .debug_str_offsets section (8 or 4, depending on the address size). */
538 gdb::optional<ULONGEST> str_offsets_base;
539
540 /* Mark used when releasing cached dies. */
541 bool mark : 1;
542
543 /* This CU references .debug_loc. See the symtab->locations_valid field.
544 This test is imperfect as there may exist optimized debug code not using
545 any location list and still facing inlining issues if handled as
546 unoptimized code. For a future better test see GCC PR other/32998. */
547 bool has_loclist : 1;
548
549 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
550 if all the producer_is_* fields are valid. This information is cached
551 because profiling CU expansion showed excessive time spent in
552 producer_is_gxx_lt_4_6. */
553 bool checked_producer : 1;
554 bool producer_is_gxx_lt_4_6 : 1;
555 bool producer_is_gcc_lt_4_3 : 1;
556 bool producer_is_icc : 1;
557 bool producer_is_icc_lt_14 : 1;
558 bool producer_is_codewarrior : 1;
559
560 /* When true, the file that we're processing is known to have
561 debugging info for C++ namespaces. GCC 3.3.x did not produce
562 this information, but later versions do. */
563
564 bool processing_has_namespace_info : 1;
565
566 struct partial_die_info *find_partial_die (sect_offset sect_off);
567
568 /* If this CU was inherited by another CU (via specification,
569 abstract_origin, etc), this is the ancestor CU. */
570 dwarf2_cu *ancestor;
571
572 /* Get the buildsym_compunit for this CU. */
573 buildsym_compunit *get_builder ()
574 {
575 /* If this CU has a builder associated with it, use that. */
576 if (m_builder != nullptr)
577 return m_builder.get ();
578
579 /* Otherwise, search ancestors for a valid builder. */
580 if (ancestor != nullptr)
581 return ancestor->get_builder ();
582
583 return nullptr;
584 }
585 };
586
587 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
588 This includes type_unit_group and quick_file_names. */
589
590 struct stmt_list_hash
591 {
592 /* The DWO unit this table is from or NULL if there is none. */
593 struct dwo_unit *dwo_unit;
594
595 /* Offset in .debug_line or .debug_line.dwo. */
596 sect_offset line_sect_off;
597 };
598
599 /* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
600 an object of this type. */
601
602 struct type_unit_group
603 {
604 /* dwarf2read.c's main "handle" on a TU symtab.
605 To simplify things we create an artificial CU that "includes" all the
606 type units using this stmt_list so that the rest of the code still has
607 a "per_cu" handle on the symtab.
608 This PER_CU is recognized by having no section. */
609 #define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
610 struct dwarf2_per_cu_data per_cu;
611
612 /* The TUs that share this DW_AT_stmt_list entry.
613 This is added to while parsing type units to build partial symtabs,
614 and is deleted afterwards and not used again. */
615 std::vector<signatured_type *> *tus;
616
617 /* The compunit symtab.
618 Type units in a group needn't all be defined in the same source file,
619 so we create an essentially anonymous symtab as the compunit symtab. */
620 struct compunit_symtab *compunit_symtab;
621
622 /* The data used to construct the hash key. */
623 struct stmt_list_hash hash;
624
625 /* The number of symtabs from the line header.
626 The value here must match line_header.num_file_names. */
627 unsigned int num_symtabs;
628
629 /* The symbol tables for this TU (obtained from the files listed in
630 DW_AT_stmt_list).
631 WARNING: The order of entries here must match the order of entries
632 in the line header. After the first TU using this type_unit_group, the
633 line header for the subsequent TUs is recreated from this. This is done
634 because we need to use the same symtabs for each TU using the same
635 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
636 there's no guarantee the line header doesn't have duplicate entries. */
637 struct symtab **symtabs;
638 };
639
640 /* These sections are what may appear in a (real or virtual) DWO file. */
641
642 struct dwo_sections
643 {
644 struct dwarf2_section_info abbrev;
645 struct dwarf2_section_info line;
646 struct dwarf2_section_info loc;
647 struct dwarf2_section_info loclists;
648 struct dwarf2_section_info macinfo;
649 struct dwarf2_section_info macro;
650 struct dwarf2_section_info str;
651 struct dwarf2_section_info str_offsets;
652 /* In the case of a virtual DWO file, these two are unused. */
653 struct dwarf2_section_info info;
654 std::vector<dwarf2_section_info> types;
655 };
656
657 /* CUs/TUs in DWP/DWO files. */
658
659 struct dwo_unit
660 {
661 /* Backlink to the containing struct dwo_file. */
662 struct dwo_file *dwo_file;
663
664 /* The "id" that distinguishes this CU/TU.
665 .debug_info calls this "dwo_id", .debug_types calls this "signature".
666 Since signatures came first, we stick with it for consistency. */
667 ULONGEST signature;
668
669 /* The section this CU/TU lives in, in the DWO file. */
670 struct dwarf2_section_info *section;
671
672 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
673 sect_offset sect_off;
674 unsigned int length;
675
676 /* For types, offset in the type's DIE of the type defined by this TU. */
677 cu_offset type_offset_in_tu;
678 };
679
680 /* include/dwarf2.h defines the DWP section codes.
681 It defines a max value but it doesn't define a min value, which we
682 use for error checking, so provide one. */
683
684 enum dwp_v2_section_ids
685 {
686 DW_SECT_MIN = 1
687 };
688
689 /* Data for one DWO file.
690
691 This includes virtual DWO files (a virtual DWO file is a DWO file as it
692 appears in a DWP file). DWP files don't really have DWO files per se -
693 comdat folding of types "loses" the DWO file they came from, and from
694 a high level view DWP files appear to contain a mass of random types.
695 However, to maintain consistency with the non-DWP case we pretend DWP
696 files contain virtual DWO files, and we assign each TU with one virtual
697 DWO file (generally based on the line and abbrev section offsets -
698 a heuristic that seems to work in practice). */
699
700 struct dwo_file
701 {
702 dwo_file () = default;
703 DISABLE_COPY_AND_ASSIGN (dwo_file);
704
705 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
706 For virtual DWO files the name is constructed from the section offsets
707 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
708 from related CU+TUs. */
709 const char *dwo_name = nullptr;
710
711 /* The DW_AT_comp_dir attribute. */
712 const char *comp_dir = nullptr;
713
714 /* The bfd, when the file is open. Otherwise this is NULL.
715 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
716 gdb_bfd_ref_ptr dbfd;
717
718 /* The sections that make up this DWO file.
719 Remember that for virtual DWO files in DWP V2, these are virtual
720 sections (for lack of a better name). */
721 struct dwo_sections sections {};
722
723 /* The CUs in the file.
724 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
725 an extension to handle LLVM's Link Time Optimization output (where
726 multiple source files may be compiled into a single object/dwo pair). */
727 htab_t cus {};
728
729 /* Table of TUs in the file.
730 Each element is a struct dwo_unit. */
731 htab_t tus {};
732 };
733
734 /* These sections are what may appear in a DWP file. */
735
736 struct dwp_sections
737 {
738 /* These are used by both DWP version 1 and 2. */
739 struct dwarf2_section_info str;
740 struct dwarf2_section_info cu_index;
741 struct dwarf2_section_info tu_index;
742
743 /* These are only used by DWP version 2 files.
744 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
745 sections are referenced by section number, and are not recorded here.
746 In DWP version 2 there is at most one copy of all these sections, each
747 section being (effectively) comprised of the concatenation of all of the
748 individual sections that exist in the version 1 format.
749 To keep the code simple we treat each of these concatenated pieces as a
750 section itself (a virtual section?). */
751 struct dwarf2_section_info abbrev;
752 struct dwarf2_section_info info;
753 struct dwarf2_section_info line;
754 struct dwarf2_section_info loc;
755 struct dwarf2_section_info macinfo;
756 struct dwarf2_section_info macro;
757 struct dwarf2_section_info str_offsets;
758 struct dwarf2_section_info types;
759 };
760
761 /* These sections are what may appear in a virtual DWO file in DWP version 1.
762 A virtual DWO file is a DWO file as it appears in a DWP file. */
763
764 struct virtual_v1_dwo_sections
765 {
766 struct dwarf2_section_info abbrev;
767 struct dwarf2_section_info line;
768 struct dwarf2_section_info loc;
769 struct dwarf2_section_info macinfo;
770 struct dwarf2_section_info macro;
771 struct dwarf2_section_info str_offsets;
772 /* Each DWP hash table entry records one CU or one TU.
773 That is recorded here, and copied to dwo_unit.section. */
774 struct dwarf2_section_info info_or_types;
775 };
776
777 /* Similar to virtual_v1_dwo_sections, but for DWP version 2.
778 In version 2, the sections of the DWO files are concatenated together
779 and stored in one section of that name. Thus each ELF section contains
780 several "virtual" sections. */
781
782 struct virtual_v2_dwo_sections
783 {
784 bfd_size_type abbrev_offset;
785 bfd_size_type abbrev_size;
786
787 bfd_size_type line_offset;
788 bfd_size_type line_size;
789
790 bfd_size_type loc_offset;
791 bfd_size_type loc_size;
792
793 bfd_size_type macinfo_offset;
794 bfd_size_type macinfo_size;
795
796 bfd_size_type macro_offset;
797 bfd_size_type macro_size;
798
799 bfd_size_type str_offsets_offset;
800 bfd_size_type str_offsets_size;
801
802 /* Each DWP hash table entry records one CU or one TU.
803 That is recorded here, and copied to dwo_unit.section. */
804 bfd_size_type info_or_types_offset;
805 bfd_size_type info_or_types_size;
806 };
807
808 /* Contents of DWP hash tables. */
809
810 struct dwp_hash_table
811 {
812 uint32_t version, nr_columns;
813 uint32_t nr_units, nr_slots;
814 const gdb_byte *hash_table, *unit_table;
815 union
816 {
817 struct
818 {
819 const gdb_byte *indices;
820 } v1;
821 struct
822 {
823 /* This is indexed by column number and gives the id of the section
824 in that column. */
825 #define MAX_NR_V2_DWO_SECTIONS \
826 (1 /* .debug_info or .debug_types */ \
827 + 1 /* .debug_abbrev */ \
828 + 1 /* .debug_line */ \
829 + 1 /* .debug_loc */ \
830 + 1 /* .debug_str_offsets */ \
831 + 1 /* .debug_macro or .debug_macinfo */)
832 int section_ids[MAX_NR_V2_DWO_SECTIONS];
833 const gdb_byte *offsets;
834 const gdb_byte *sizes;
835 } v2;
836 } section_pool;
837 };
838
839 /* Data for one DWP file. */
840
841 struct dwp_file
842 {
843 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
844 : name (name_),
845 dbfd (std::move (abfd))
846 {
847 }
848
849 /* Name of the file. */
850 const char *name;
851
852 /* File format version. */
853 int version = 0;
854
855 /* The bfd. */
856 gdb_bfd_ref_ptr dbfd;
857
858 /* Section info for this file. */
859 struct dwp_sections sections {};
860
861 /* Table of CUs in the file. */
862 const struct dwp_hash_table *cus = nullptr;
863
864 /* Table of TUs in the file. */
865 const struct dwp_hash_table *tus = nullptr;
866
867 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
868 htab_t loaded_cus {};
869 htab_t loaded_tus {};
870
871 /* Table to map ELF section numbers to their sections.
872 This is only needed for the DWP V1 file format. */
873 unsigned int num_sections = 0;
874 asection **elf_sections = nullptr;
875 };
876
877 struct abbrev_table;
878 typedef std::unique_ptr<struct abbrev_table> abbrev_table_up;
879
880 /* Struct used to pass misc. parameters to read_die_and_children, et
881 al. which are used for both .debug_info and .debug_types dies.
882 All parameters here are unchanging for the life of the call. This
883 struct exists to abstract away the constant parameters of die reading. */
884
885 struct die_reader_specs
886 {
887 /* The bfd of die_section. */
888 bfd* abfd;
889
890 /* The CU of the DIE we are parsing. */
891 struct dwarf2_cu *cu;
892
893 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
894 struct dwo_file *dwo_file;
895
896 /* The section the die comes from.
897 This is either .debug_info or .debug_types, or the .dwo variants. */
898 struct dwarf2_section_info *die_section;
899
900 /* die_section->buffer. */
901 const gdb_byte *buffer;
902
903 /* The end of the buffer. */
904 const gdb_byte *buffer_end;
905
906 /* The value of the DW_AT_comp_dir attribute. */
907 const char *comp_dir;
908
909 /* The abbreviation table to use when reading the DIEs. */
910 struct abbrev_table *abbrev_table;
911 };
912
913 /* A subclass of die_reader_specs that holds storage and has complex
914 constructor and destructor behavior. */
915
916 class cutu_reader : public die_reader_specs
917 {
918 public:
919
920 cutu_reader (struct dwarf2_per_cu_data *this_cu,
921 struct abbrev_table *abbrev_table,
922 int use_existing_cu, int keep,
923 bool skip_partial);
924
925 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
926 struct dwarf2_cu *parent_cu = nullptr,
927 struct dwo_file *dwo_file = nullptr);
928
929 ~cutu_reader ();
930
931 DISABLE_COPY_AND_ASSIGN (cutu_reader);
932
933 const gdb_byte *info_ptr = nullptr;
934 struct die_info *comp_unit_die = nullptr;
935 int has_children = 0;
936 bool dummy_p = false;
937
938 private:
939 void init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
940 int use_existing_cu, int keep);
941
942 struct dwarf2_per_cu_data *m_this_cu;
943 int m_keep = 0;
944 std::unique_ptr<dwarf2_cu> m_new_cu;
945
946 /* The ordinary abbreviation table. */
947 abbrev_table_up m_abbrev_table_holder;
948
949 /* The DWO abbreviation table. */
950 abbrev_table_up m_dwo_abbrev_table;
951 };
952
953 /* dir_index is 1-based in DWARF 4 and before, and is 0-based in DWARF 5 and
954 later. */
955 typedef int dir_index;
956
957 /* file_name_index is 1-based in DWARF 4 and before, and is 0-based in DWARF 5
958 and later. */
959 typedef int file_name_index;
960
961 struct file_entry
962 {
963 file_entry () = default;
964
965 file_entry (const char *name_, dir_index d_index_,
966 unsigned int mod_time_, unsigned int length_)
967 : name (name_),
968 d_index (d_index_),
969 mod_time (mod_time_),
970 length (length_)
971 {}
972
973 /* Return the include directory at D_INDEX stored in LH. Returns
974 NULL if D_INDEX is out of bounds. */
975 const char *include_dir (const line_header *lh) const;
976
977 /* The file name. Note this is an observing pointer. The memory is
978 owned by debug_line_buffer. */
979 const char *name {};
980
981 /* The directory index (1-based). */
982 dir_index d_index {};
983
984 unsigned int mod_time {};
985
986 unsigned int length {};
987
988 /* True if referenced by the Line Number Program. */
989 bool included_p {};
990
991 /* The associated symbol table, if any. */
992 struct symtab *symtab {};
993 };
994
995 /* The line number information for a compilation unit (found in the
996 .debug_line section) begins with a "statement program header",
997 which contains the following information. */
998 struct line_header
999 {
1000 line_header ()
1001 : offset_in_dwz {}
1002 {}
1003
1004 /* Add an entry to the include directory table. */
1005 void add_include_dir (const char *include_dir);
1006
1007 /* Add an entry to the file name table. */
1008 void add_file_name (const char *name, dir_index d_index,
1009 unsigned int mod_time, unsigned int length);
1010
1011 /* Return the include dir at INDEX (0-based in DWARF 5 and 1-based before).
1012 Returns NULL if INDEX is out of bounds. */
1013 const char *include_dir_at (dir_index index) const
1014 {
1015 int vec_index;
1016 if (version >= 5)
1017 vec_index = index;
1018 else
1019 vec_index = index - 1;
1020 if (vec_index < 0 || vec_index >= m_include_dirs.size ())
1021 return NULL;
1022 return m_include_dirs[vec_index];
1023 }
1024
1025 bool is_valid_file_index (int file_index)
1026 {
1027 if (version >= 5)
1028 return 0 <= file_index && file_index < file_names_size ();
1029 return 1 <= file_index && file_index <= file_names_size ();
1030 }
1031
1032 /* Return the file name at INDEX (0-based in DWARF 5 and 1-based before).
1033 Returns NULL if INDEX is out of bounds. */
1034 file_entry *file_name_at (file_name_index index)
1035 {
1036 int vec_index;
1037 if (version >= 5)
1038 vec_index = index;
1039 else
1040 vec_index = index - 1;
1041 if (vec_index < 0 || vec_index >= m_file_names.size ())
1042 return NULL;
1043 return &m_file_names[vec_index];
1044 }
1045
1046 /* The indexes are 0-based in DWARF 5 and 1-based in DWARF 4. Therefore,
1047 this method should only be used to iterate through all file entries in an
1048 index-agnostic manner. */
1049 std::vector<file_entry> &file_names ()
1050 { return m_file_names; }
1051
1052 /* Offset of line number information in .debug_line section. */
1053 sect_offset sect_off {};
1054
1055 /* OFFSET is for struct dwz_file associated with dwarf2_per_objfile. */
1056 unsigned offset_in_dwz : 1; /* Can't initialize bitfields in-class. */
1057
1058 unsigned int total_length {};
1059 unsigned short version {};
1060 unsigned int header_length {};
1061 unsigned char minimum_instruction_length {};
1062 unsigned char maximum_ops_per_instruction {};
1063 unsigned char default_is_stmt {};
1064 int line_base {};
1065 unsigned char line_range {};
1066 unsigned char opcode_base {};
1067
1068 /* standard_opcode_lengths[i] is the number of operands for the
1069 standard opcode whose value is i. This means that
1070 standard_opcode_lengths[0] is unused, and the last meaningful
1071 element is standard_opcode_lengths[opcode_base - 1]. */
1072 std::unique_ptr<unsigned char[]> standard_opcode_lengths;
1073
1074 int file_names_size ()
1075 { return m_file_names.size(); }
1076
1077 /* The start and end of the statement program following this
1078 header. These point into dwarf2_per_objfile->line_buffer. */
1079 const gdb_byte *statement_program_start {}, *statement_program_end {};
1080
1081 private:
1082 /* The include_directories table. Note these are observing
1083 pointers. The memory is owned by debug_line_buffer. */
1084 std::vector<const char *> m_include_dirs;
1085
1086 /* The file_names table. This is private because the meaning of indexes
1087 differs among DWARF versions (The first valid index is 1 in DWARF 4 and
1088 before, and is 0 in DWARF 5 and later). So the client should use
1089 file_name_at method for access. */
1090 std::vector<file_entry> m_file_names;
1091 };
1092
1093 typedef std::unique_ptr<line_header> line_header_up;
1094
1095 const char *
1096 file_entry::include_dir (const line_header *lh) const
1097 {
1098 return lh->include_dir_at (d_index);
1099 }
1100
1101 /* When we construct a partial symbol table entry we only
1102 need this much information. */
1103 struct partial_die_info : public allocate_on_obstack
1104 {
1105 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
1106
1107 /* Disable assign but still keep copy ctor, which is needed
1108 load_partial_dies. */
1109 partial_die_info& operator=(const partial_die_info& rhs) = delete;
1110
1111 /* Adjust the partial die before generating a symbol for it. This
1112 function may set the is_external flag or change the DIE's
1113 name. */
1114 void fixup (struct dwarf2_cu *cu);
1115
1116 /* Read a minimal amount of information into the minimal die
1117 structure. */
1118 const gdb_byte *read (const struct die_reader_specs *reader,
1119 const struct abbrev_info &abbrev,
1120 const gdb_byte *info_ptr);
1121
1122 /* Offset of this DIE. */
1123 const sect_offset sect_off;
1124
1125 /* DWARF-2 tag for this DIE. */
1126 const ENUM_BITFIELD(dwarf_tag) tag : 16;
1127
1128 /* Assorted flags describing the data found in this DIE. */
1129 const unsigned int has_children : 1;
1130
1131 unsigned int is_external : 1;
1132 unsigned int is_declaration : 1;
1133 unsigned int has_type : 1;
1134 unsigned int has_specification : 1;
1135 unsigned int has_pc_info : 1;
1136 unsigned int may_be_inlined : 1;
1137
1138 /* This DIE has been marked DW_AT_main_subprogram. */
1139 unsigned int main_subprogram : 1;
1140
1141 /* Flag set if the SCOPE field of this structure has been
1142 computed. */
1143 unsigned int scope_set : 1;
1144
1145 /* Flag set if the DIE has a byte_size attribute. */
1146 unsigned int has_byte_size : 1;
1147
1148 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1149 unsigned int has_const_value : 1;
1150
1151 /* Flag set if any of the DIE's children are template arguments. */
1152 unsigned int has_template_arguments : 1;
1153
1154 /* Flag set if fixup has been called on this die. */
1155 unsigned int fixup_called : 1;
1156
1157 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1158 unsigned int is_dwz : 1;
1159
1160 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1161 unsigned int spec_is_dwz : 1;
1162
1163 /* The name of this DIE. Normally the value of DW_AT_name, but
1164 sometimes a default name for unnamed DIEs. */
1165 const char *name = nullptr;
1166
1167 /* The linkage name, if present. */
1168 const char *linkage_name = nullptr;
1169
1170 /* The scope to prepend to our children. This is generally
1171 allocated on the comp_unit_obstack, so will disappear
1172 when this compilation unit leaves the cache. */
1173 const char *scope = nullptr;
1174
1175 /* Some data associated with the partial DIE. The tag determines
1176 which field is live. */
1177 union
1178 {
1179 /* The location description associated with this DIE, if any. */
1180 struct dwarf_block *locdesc;
1181 /* The offset of an import, for DW_TAG_imported_unit. */
1182 sect_offset sect_off;
1183 } d {};
1184
1185 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1186 CORE_ADDR lowpc = 0;
1187 CORE_ADDR highpc = 0;
1188
1189 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1190 DW_AT_sibling, if any. */
1191 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1192 could return DW_AT_sibling values to its caller load_partial_dies. */
1193 const gdb_byte *sibling = nullptr;
1194
1195 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1196 DW_AT_specification (or DW_AT_abstract_origin or
1197 DW_AT_extension). */
1198 sect_offset spec_offset {};
1199
1200 /* Pointers to this DIE's parent, first child, and next sibling,
1201 if any. */
1202 struct partial_die_info *die_parent = nullptr;
1203 struct partial_die_info *die_child = nullptr;
1204 struct partial_die_info *die_sibling = nullptr;
1205
1206 friend struct partial_die_info *
1207 dwarf2_cu::find_partial_die (sect_offset sect_off);
1208
1209 private:
1210 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1211 partial_die_info (sect_offset sect_off)
1212 : partial_die_info (sect_off, DW_TAG_padding, 0)
1213 {
1214 }
1215
1216 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1217 int has_children_)
1218 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1219 {
1220 is_external = 0;
1221 is_declaration = 0;
1222 has_type = 0;
1223 has_specification = 0;
1224 has_pc_info = 0;
1225 may_be_inlined = 0;
1226 main_subprogram = 0;
1227 scope_set = 0;
1228 has_byte_size = 0;
1229 has_const_value = 0;
1230 has_template_arguments = 0;
1231 fixup_called = 0;
1232 is_dwz = 0;
1233 spec_is_dwz = 0;
1234 }
1235 };
1236
1237 /* This data structure holds the information of an abbrev. */
1238 struct abbrev_info
1239 {
1240 unsigned int number; /* number identifying abbrev */
1241 enum dwarf_tag tag; /* dwarf tag */
1242 unsigned short has_children; /* boolean */
1243 unsigned short num_attrs; /* number of attributes */
1244 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1245 struct abbrev_info *next; /* next in chain */
1246 };
1247
1248 struct attr_abbrev
1249 {
1250 ENUM_BITFIELD(dwarf_attribute) name : 16;
1251 ENUM_BITFIELD(dwarf_form) form : 16;
1252
1253 /* It is valid only if FORM is DW_FORM_implicit_const. */
1254 LONGEST implicit_const;
1255 };
1256
1257 /* Size of abbrev_table.abbrev_hash_table. */
1258 #define ABBREV_HASH_SIZE 121
1259
1260 /* Top level data structure to contain an abbreviation table. */
1261
1262 struct abbrev_table
1263 {
1264 explicit abbrev_table (sect_offset off)
1265 : sect_off (off)
1266 {
1267 m_abbrevs =
1268 XOBNEWVEC (&abbrev_obstack, struct abbrev_info *, ABBREV_HASH_SIZE);
1269 memset (m_abbrevs, 0, ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
1270 }
1271
1272 DISABLE_COPY_AND_ASSIGN (abbrev_table);
1273
1274 /* Allocate space for a struct abbrev_info object in
1275 ABBREV_TABLE. */
1276 struct abbrev_info *alloc_abbrev ();
1277
1278 /* Add an abbreviation to the table. */
1279 void add_abbrev (unsigned int abbrev_number, struct abbrev_info *abbrev);
1280
1281 /* Look up an abbrev in the table.
1282 Returns NULL if the abbrev is not found. */
1283
1284 struct abbrev_info *lookup_abbrev (unsigned int abbrev_number);
1285
1286
1287 /* Where the abbrev table came from.
1288 This is used as a sanity check when the table is used. */
1289 const sect_offset sect_off;
1290
1291 /* Storage for the abbrev table. */
1292 auto_obstack abbrev_obstack;
1293
1294 private:
1295
1296 /* Hash table of abbrevs.
1297 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1298 It could be statically allocated, but the previous code didn't so we
1299 don't either. */
1300 struct abbrev_info **m_abbrevs;
1301 };
1302
1303 /* Attributes have a name and a value. */
1304 struct attribute
1305 {
1306 ENUM_BITFIELD(dwarf_attribute) name : 16;
1307 ENUM_BITFIELD(dwarf_form) form : 15;
1308
1309 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1310 field should be in u.str (existing only for DW_STRING) but it is kept
1311 here for better struct attribute alignment. */
1312 unsigned int string_is_canonical : 1;
1313
1314 union
1315 {
1316 const char *str;
1317 struct dwarf_block *blk;
1318 ULONGEST unsnd;
1319 LONGEST snd;
1320 CORE_ADDR addr;
1321 ULONGEST signature;
1322 }
1323 u;
1324 };
1325
1326 /* This data structure holds a complete die structure. */
1327 struct die_info
1328 {
1329 /* DWARF-2 tag for this DIE. */
1330 ENUM_BITFIELD(dwarf_tag) tag : 16;
1331
1332 /* Number of attributes */
1333 unsigned char num_attrs;
1334
1335 /* True if we're presently building the full type name for the
1336 type derived from this DIE. */
1337 unsigned char building_fullname : 1;
1338
1339 /* True if this die is in process. PR 16581. */
1340 unsigned char in_process : 1;
1341
1342 /* Abbrev number */
1343 unsigned int abbrev;
1344
1345 /* Offset in .debug_info or .debug_types section. */
1346 sect_offset sect_off;
1347
1348 /* The dies in a compilation unit form an n-ary tree. PARENT
1349 points to this die's parent; CHILD points to the first child of
1350 this node; and all the children of a given node are chained
1351 together via their SIBLING fields. */
1352 struct die_info *child; /* Its first child, if any. */
1353 struct die_info *sibling; /* Its next sibling, if any. */
1354 struct die_info *parent; /* Its parent, if any. */
1355
1356 /* An array of attributes, with NUM_ATTRS elements. There may be
1357 zero, but it's not common and zero-sized arrays are not
1358 sufficiently portable C. */
1359 struct attribute attrs[1];
1360 };
1361
1362 /* Get at parts of an attribute structure. */
1363
1364 #define DW_STRING(attr) ((attr)->u.str)
1365 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
1366 #define DW_UNSND(attr) ((attr)->u.unsnd)
1367 #define DW_BLOCK(attr) ((attr)->u.blk)
1368 #define DW_SND(attr) ((attr)->u.snd)
1369 #define DW_ADDR(attr) ((attr)->u.addr)
1370 #define DW_SIGNATURE(attr) ((attr)->u.signature)
1371
1372 /* Blocks are a bunch of untyped bytes. */
1373 struct dwarf_block
1374 {
1375 size_t size;
1376
1377 /* Valid only if SIZE is not zero. */
1378 const gdb_byte *data;
1379 };
1380
1381 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1382 but this would require a corresponding change in unpack_field_as_long
1383 and friends. */
1384 static int bits_per_byte = 8;
1385
1386 /* When reading a variant or variant part, we track a bit more
1387 information about the field, and store it in an object of this
1388 type. */
1389
1390 struct variant_field
1391 {
1392 /* If we see a DW_TAG_variant, then this will be the discriminant
1393 value. */
1394 ULONGEST discriminant_value;
1395 /* If we see a DW_TAG_variant, then this will be set if this is the
1396 default branch. */
1397 bool default_branch;
1398 /* While reading a DW_TAG_variant_part, this will be set if this
1399 field is the discriminant. */
1400 bool is_discriminant;
1401 };
1402
1403 struct nextfield
1404 {
1405 int accessibility = 0;
1406 int virtuality = 0;
1407 /* Extra information to describe a variant or variant part. */
1408 struct variant_field variant {};
1409 struct field field {};
1410 };
1411
1412 struct fnfieldlist
1413 {
1414 const char *name = nullptr;
1415 std::vector<struct fn_field> fnfields;
1416 };
1417
1418 /* The routines that read and process dies for a C struct or C++ class
1419 pass lists of data member fields and lists of member function fields
1420 in an instance of a field_info structure, as defined below. */
1421 struct field_info
1422 {
1423 /* List of data member and baseclasses fields. */
1424 std::vector<struct nextfield> fields;
1425 std::vector<struct nextfield> baseclasses;
1426
1427 /* Number of fields (including baseclasses). */
1428 int nfields = 0;
1429
1430 /* Set if the accessibility of one of the fields is not public. */
1431 int non_public_fields = 0;
1432
1433 /* Member function fieldlist array, contains name of possibly overloaded
1434 member function, number of overloaded member functions and a pointer
1435 to the head of the member function field chain. */
1436 std::vector<struct fnfieldlist> fnfieldlists;
1437
1438 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1439 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1440 std::vector<struct decl_field> typedef_field_list;
1441
1442 /* Nested types defined by this class and the number of elements in this
1443 list. */
1444 std::vector<struct decl_field> nested_types_list;
1445 };
1446
1447 /* One item on the queue of compilation units to read in full symbols
1448 for. */
1449 struct dwarf2_queue_item
1450 {
1451 struct dwarf2_per_cu_data *per_cu;
1452 enum language pretend_language;
1453 struct dwarf2_queue_item *next;
1454 };
1455
1456 /* The current queue. */
1457 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1458
1459 /* Loaded secondary compilation units are kept in memory until they
1460 have not been referenced for the processing of this many
1461 compilation units. Set this to zero to disable caching. Cache
1462 sizes of up to at least twenty will improve startup time for
1463 typical inter-CU-reference binaries, at an obvious memory cost. */
1464 static int dwarf_max_cache_age = 5;
1465 static void
1466 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1467 struct cmd_list_element *c, const char *value)
1468 {
1469 fprintf_filtered (file, _("The upper bound on the age of cached "
1470 "DWARF compilation units is %s.\n"),
1471 value);
1472 }
1473 \f
1474 /* local function prototypes */
1475
1476 static const char *get_section_name (const struct dwarf2_section_info *);
1477
1478 static const char *get_section_file_name (const struct dwarf2_section_info *);
1479
1480 static void dwarf2_find_base_address (struct die_info *die,
1481 struct dwarf2_cu *cu);
1482
1483 static dwarf2_psymtab *create_partial_symtab
1484 (struct dwarf2_per_cu_data *per_cu, const char *name);
1485
1486 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1487 const gdb_byte *info_ptr,
1488 struct die_info *type_unit_die,
1489 int has_children);
1490
1491 static void dwarf2_build_psymtabs_hard
1492 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1493
1494 static void scan_partial_symbols (struct partial_die_info *,
1495 CORE_ADDR *, CORE_ADDR *,
1496 int, struct dwarf2_cu *);
1497
1498 static void add_partial_symbol (struct partial_die_info *,
1499 struct dwarf2_cu *);
1500
1501 static void add_partial_namespace (struct partial_die_info *pdi,
1502 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1503 int set_addrmap, struct dwarf2_cu *cu);
1504
1505 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1506 CORE_ADDR *highpc, int set_addrmap,
1507 struct dwarf2_cu *cu);
1508
1509 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1510 struct dwarf2_cu *cu);
1511
1512 static void add_partial_subprogram (struct partial_die_info *pdi,
1513 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1514 int need_pc, struct dwarf2_cu *cu);
1515
1516 static abbrev_table_up abbrev_table_read_table
1517 (struct dwarf2_per_objfile *dwarf2_per_objfile, struct dwarf2_section_info *,
1518 sect_offset);
1519
1520 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1521
1522 static struct partial_die_info *load_partial_dies
1523 (const struct die_reader_specs *, const gdb_byte *, int);
1524
1525 /* A pair of partial_die_info and compilation unit. */
1526 struct cu_partial_die_info
1527 {
1528 /* The compilation unit of the partial_die_info. */
1529 struct dwarf2_cu *cu;
1530 /* A partial_die_info. */
1531 struct partial_die_info *pdi;
1532
1533 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1534 : cu (cu),
1535 pdi (pdi)
1536 { /* Nothing. */ }
1537
1538 private:
1539 cu_partial_die_info () = delete;
1540 };
1541
1542 static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1543 struct dwarf2_cu *);
1544
1545 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1546 struct attribute *, struct attr_abbrev *,
1547 const gdb_byte *, bool *need_reprocess);
1548
1549 static void read_attribute_reprocess (const struct die_reader_specs *reader,
1550 struct attribute *attr);
1551
1552 static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
1553
1554 static unsigned int read_1_byte (bfd *, const gdb_byte *);
1555
1556 static int read_1_signed_byte (bfd *, const gdb_byte *);
1557
1558 static unsigned int read_2_bytes (bfd *, const gdb_byte *);
1559
1560 /* Read the next three bytes (little-endian order) as an unsigned integer. */
1561 static unsigned int read_3_bytes (bfd *, const gdb_byte *);
1562
1563 static unsigned int read_4_bytes (bfd *, const gdb_byte *);
1564
1565 static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
1566
1567 static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
1568 unsigned int *);
1569
1570 static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
1571
1572 static LONGEST read_checked_initial_length_and_offset
1573 (bfd *, const gdb_byte *, const struct comp_unit_head *,
1574 unsigned int *, unsigned int *);
1575
1576 static LONGEST read_offset (bfd *, const gdb_byte *,
1577 const struct comp_unit_head *,
1578 unsigned int *);
1579
1580 static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
1581
1582 static sect_offset read_abbrev_offset
1583 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1584 struct dwarf2_section_info *, sect_offset);
1585
1586 static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
1587
1588 static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
1589
1590 static const char *read_indirect_string
1591 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1592 const struct comp_unit_head *, unsigned int *);
1593
1594 static const char *read_indirect_line_string
1595 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1596 const struct comp_unit_head *, unsigned int *);
1597
1598 static const char *read_indirect_string_at_offset
1599 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
1600 LONGEST str_offset);
1601
1602 static const char *read_indirect_string_from_dwz
1603 (struct objfile *objfile, struct dwz_file *, LONGEST);
1604
1605 static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
1606
1607 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1608 const gdb_byte *,
1609 unsigned int *);
1610
1611 static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1612 ULONGEST str_index);
1613
1614 static const char *read_stub_str_index (struct dwarf2_cu *cu,
1615 ULONGEST str_index);
1616
1617 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1618
1619 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1620 struct dwarf2_cu *);
1621
1622 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1623 unsigned int);
1624
1625 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1626 struct dwarf2_cu *cu);
1627
1628 static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1629
1630 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1631 struct dwarf2_cu *cu);
1632
1633 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1634
1635 static struct die_info *die_specification (struct die_info *die,
1636 struct dwarf2_cu **);
1637
1638 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1639 struct dwarf2_cu *cu);
1640
1641 static void dwarf_decode_lines (struct line_header *, const char *,
1642 struct dwarf2_cu *, dwarf2_psymtab *,
1643 CORE_ADDR, int decode_mapping);
1644
1645 static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1646 const char *);
1647
1648 static struct symbol *new_symbol (struct die_info *, struct type *,
1649 struct dwarf2_cu *, struct symbol * = NULL);
1650
1651 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1652 struct dwarf2_cu *);
1653
1654 static void dwarf2_const_value_attr (const struct attribute *attr,
1655 struct type *type,
1656 const char *name,
1657 struct obstack *obstack,
1658 struct dwarf2_cu *cu, LONGEST *value,
1659 const gdb_byte **bytes,
1660 struct dwarf2_locexpr_baton **baton);
1661
1662 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1663
1664 static int need_gnat_info (struct dwarf2_cu *);
1665
1666 static struct type *die_descriptive_type (struct die_info *,
1667 struct dwarf2_cu *);
1668
1669 static void set_descriptive_type (struct type *, struct die_info *,
1670 struct dwarf2_cu *);
1671
1672 static struct type *die_containing_type (struct die_info *,
1673 struct dwarf2_cu *);
1674
1675 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1676 struct dwarf2_cu *);
1677
1678 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1679
1680 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1681
1682 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1683
1684 static char *typename_concat (struct obstack *obs, const char *prefix,
1685 const char *suffix, int physname,
1686 struct dwarf2_cu *cu);
1687
1688 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1689
1690 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1691
1692 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1693
1694 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1695
1696 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1697
1698 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1699
1700 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1701 struct dwarf2_cu *, dwarf2_psymtab *);
1702
1703 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1704 values. Keep the items ordered with increasing constraints compliance. */
1705 enum pc_bounds_kind
1706 {
1707 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1708 PC_BOUNDS_NOT_PRESENT,
1709
1710 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1711 were present but they do not form a valid range of PC addresses. */
1712 PC_BOUNDS_INVALID,
1713
1714 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1715 PC_BOUNDS_RANGES,
1716
1717 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1718 PC_BOUNDS_HIGH_LOW,
1719 };
1720
1721 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1722 CORE_ADDR *, CORE_ADDR *,
1723 struct dwarf2_cu *,
1724 dwarf2_psymtab *);
1725
1726 static void get_scope_pc_bounds (struct die_info *,
1727 CORE_ADDR *, CORE_ADDR *,
1728 struct dwarf2_cu *);
1729
1730 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1731 CORE_ADDR, struct dwarf2_cu *);
1732
1733 static void dwarf2_add_field (struct field_info *, struct die_info *,
1734 struct dwarf2_cu *);
1735
1736 static void dwarf2_attach_fields_to_type (struct field_info *,
1737 struct type *, struct dwarf2_cu *);
1738
1739 static void dwarf2_add_member_fn (struct field_info *,
1740 struct die_info *, struct type *,
1741 struct dwarf2_cu *);
1742
1743 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1744 struct type *,
1745 struct dwarf2_cu *);
1746
1747 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1748
1749 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1750
1751 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1752
1753 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1754
1755 static struct using_direct **using_directives (struct dwarf2_cu *cu);
1756
1757 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1758
1759 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1760
1761 static struct type *read_module_type (struct die_info *die,
1762 struct dwarf2_cu *cu);
1763
1764 static const char *namespace_name (struct die_info *die,
1765 int *is_anonymous, struct dwarf2_cu *);
1766
1767 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1768
1769 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1770
1771 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1772 struct dwarf2_cu *);
1773
1774 static struct die_info *read_die_and_siblings_1
1775 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1776 struct die_info *);
1777
1778 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1779 const gdb_byte *info_ptr,
1780 const gdb_byte **new_info_ptr,
1781 struct die_info *parent);
1782
1783 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1784 struct die_info **, const gdb_byte *,
1785 int *, int);
1786
1787 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1788 struct die_info **, const gdb_byte *,
1789 int *);
1790
1791 static void process_die (struct die_info *, struct dwarf2_cu *);
1792
1793 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1794 struct obstack *);
1795
1796 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1797
1798 static const char *dwarf2_full_name (const char *name,
1799 struct die_info *die,
1800 struct dwarf2_cu *cu);
1801
1802 static const char *dwarf2_physname (const char *name, struct die_info *die,
1803 struct dwarf2_cu *cu);
1804
1805 static struct die_info *dwarf2_extension (struct die_info *die,
1806 struct dwarf2_cu **);
1807
1808 static const char *dwarf_tag_name (unsigned int);
1809
1810 static const char *dwarf_attr_name (unsigned int);
1811
1812 static const char *dwarf_unit_type_name (int unit_type);
1813
1814 static const char *dwarf_form_name (unsigned int);
1815
1816 static const char *dwarf_bool_name (unsigned int);
1817
1818 static const char *dwarf_type_encoding_name (unsigned int);
1819
1820 static struct die_info *sibling_die (struct die_info *);
1821
1822 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1823
1824 static void dump_die_for_error (struct die_info *);
1825
1826 static void dump_die_1 (struct ui_file *, int level, int max_level,
1827 struct die_info *);
1828
1829 /*static*/ void dump_die (struct die_info *, int max_level);
1830
1831 static void store_in_ref_table (struct die_info *,
1832 struct dwarf2_cu *);
1833
1834 static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
1835
1836 static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
1837
1838 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1839 const struct attribute *,
1840 struct dwarf2_cu **);
1841
1842 static struct die_info *follow_die_ref (struct die_info *,
1843 const struct attribute *,
1844 struct dwarf2_cu **);
1845
1846 static struct die_info *follow_die_sig (struct die_info *,
1847 const struct attribute *,
1848 struct dwarf2_cu **);
1849
1850 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1851 struct dwarf2_cu *);
1852
1853 static struct type *get_DW_AT_signature_type (struct die_info *,
1854 const struct attribute *,
1855 struct dwarf2_cu *);
1856
1857 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1858
1859 static void read_signatured_type (struct signatured_type *);
1860
1861 static int attr_to_dynamic_prop (const struct attribute *attr,
1862 struct die_info *die, struct dwarf2_cu *cu,
1863 struct dynamic_prop *prop, struct type *type);
1864
1865 /* memory allocation interface */
1866
1867 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1868
1869 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1870
1871 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1872
1873 static int attr_form_is_block (const struct attribute *);
1874
1875 static int attr_form_is_section_offset (const struct attribute *);
1876
1877 static int attr_form_is_constant (const struct attribute *);
1878
1879 static int attr_form_is_ref (const struct attribute *);
1880
1881 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1882 struct dwarf2_loclist_baton *baton,
1883 const struct attribute *attr);
1884
1885 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1886 struct symbol *sym,
1887 struct dwarf2_cu *cu,
1888 int is_block);
1889
1890 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1891 const gdb_byte *info_ptr,
1892 struct abbrev_info *abbrev);
1893
1894 static hashval_t partial_die_hash (const void *item);
1895
1896 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1897
1898 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1899 (sect_offset sect_off, unsigned int offset_in_dwz,
1900 struct dwarf2_per_objfile *dwarf2_per_objfile);
1901
1902 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1903 struct die_info *comp_unit_die,
1904 enum language pretend_language);
1905
1906 static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1907
1908 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1909
1910 static struct type *set_die_type (struct die_info *, struct type *,
1911 struct dwarf2_cu *);
1912
1913 static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1914
1915 static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1916
1917 static void load_full_comp_unit (struct dwarf2_per_cu_data *, bool,
1918 enum language);
1919
1920 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1921 enum language);
1922
1923 static void process_full_type_unit (struct dwarf2_per_cu_data *,
1924 enum language);
1925
1926 static void dwarf2_add_dependence (struct dwarf2_cu *,
1927 struct dwarf2_per_cu_data *);
1928
1929 static void dwarf2_mark (struct dwarf2_cu *);
1930
1931 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1932
1933 static struct type *get_die_type_at_offset (sect_offset,
1934 struct dwarf2_per_cu_data *);
1935
1936 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1937
1938 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1939 enum language pretend_language);
1940
1941 static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
1942
1943 static struct type *dwarf2_per_cu_addr_type (struct dwarf2_per_cu_data *per_cu);
1944 static struct type *dwarf2_per_cu_addr_sized_int_type
1945 (struct dwarf2_per_cu_data *per_cu, bool unsigned_p);
1946 static struct type *dwarf2_per_cu_int_type
1947 (struct dwarf2_per_cu_data *per_cu, int size_in_bytes,
1948 bool unsigned_p);
1949
1950 /* Class, the destructor of which frees all allocated queue entries. This
1951 will only have work to do if an error was thrown while processing the
1952 dwarf. If no error was thrown then the queue entries should have all
1953 been processed, and freed, as we went along. */
1954
1955 class dwarf2_queue_guard
1956 {
1957 public:
1958 dwarf2_queue_guard () = default;
1959
1960 /* Free any entries remaining on the queue. There should only be
1961 entries left if we hit an error while processing the dwarf. */
1962 ~dwarf2_queue_guard ()
1963 {
1964 struct dwarf2_queue_item *item, *last;
1965
1966 item = dwarf2_queue;
1967 while (item)
1968 {
1969 /* Anything still marked queued is likely to be in an
1970 inconsistent state, so discard it. */
1971 if (item->per_cu->queued)
1972 {
1973 if (item->per_cu->cu != NULL)
1974 free_one_cached_comp_unit (item->per_cu);
1975 item->per_cu->queued = 0;
1976 }
1977
1978 last = item;
1979 item = item->next;
1980 xfree (last);
1981 }
1982
1983 dwarf2_queue = dwarf2_queue_tail = NULL;
1984 }
1985 };
1986
1987 /* The return type of find_file_and_directory. Note, the enclosed
1988 string pointers are only valid while this object is valid. */
1989
1990 struct file_and_directory
1991 {
1992 /* The filename. This is never NULL. */
1993 const char *name;
1994
1995 /* The compilation directory. NULL if not known. If we needed to
1996 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1997 points directly to the DW_AT_comp_dir string attribute owned by
1998 the obstack that owns the DIE. */
1999 const char *comp_dir;
2000
2001 /* If we needed to build a new string for comp_dir, this is what
2002 owns the storage. */
2003 std::string comp_dir_storage;
2004 };
2005
2006 static file_and_directory find_file_and_directory (struct die_info *die,
2007 struct dwarf2_cu *cu);
2008
2009 static char *file_full_name (int file, struct line_header *lh,
2010 const char *comp_dir);
2011
2012 /* Expected enum dwarf_unit_type for read_comp_unit_head. */
2013 enum class rcuh_kind { COMPILE, TYPE };
2014
2015 static const gdb_byte *read_and_check_comp_unit_head
2016 (struct dwarf2_per_objfile* dwarf2_per_objfile,
2017 struct comp_unit_head *header,
2018 struct dwarf2_section_info *section,
2019 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
2020 rcuh_kind section_kind);
2021
2022 static htab_t allocate_signatured_type_table (struct objfile *objfile);
2023
2024 static htab_t allocate_dwo_unit_table (struct objfile *objfile);
2025
2026 static struct dwo_unit *lookup_dwo_unit_in_dwp
2027 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2028 struct dwp_file *dwp_file, const char *comp_dir,
2029 ULONGEST signature, int is_debug_types);
2030
2031 static struct dwp_file *get_dwp_file
2032 (struct dwarf2_per_objfile *dwarf2_per_objfile);
2033
2034 static struct dwo_unit *lookup_dwo_comp_unit
2035 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
2036
2037 static struct dwo_unit *lookup_dwo_type_unit
2038 (struct signatured_type *, const char *, const char *);
2039
2040 static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
2041
2042 /* A unique pointer to a dwo_file. */
2043
2044 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
2045
2046 static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
2047
2048 static void check_producer (struct dwarf2_cu *cu);
2049
2050 static void free_line_header_voidp (void *arg);
2051 \f
2052 /* Various complaints about symbol reading that don't abort the process. */
2053
2054 static void
2055 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2056 {
2057 complaint (_("statement list doesn't fit in .debug_line section"));
2058 }
2059
2060 static void
2061 dwarf2_debug_line_missing_file_complaint (void)
2062 {
2063 complaint (_(".debug_line section has line data without a file"));
2064 }
2065
2066 static void
2067 dwarf2_debug_line_missing_end_sequence_complaint (void)
2068 {
2069 complaint (_(".debug_line section has line "
2070 "program sequence without an end"));
2071 }
2072
2073 static void
2074 dwarf2_complex_location_expr_complaint (void)
2075 {
2076 complaint (_("location expression too complex"));
2077 }
2078
2079 static void
2080 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
2081 int arg3)
2082 {
2083 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
2084 arg1, arg2, arg3);
2085 }
2086
2087 static void
2088 dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2089 {
2090 complaint (_("debug info runs off end of %s section"
2091 " [in module %s]"),
2092 get_section_name (section),
2093 get_section_file_name (section));
2094 }
2095
2096 static void
2097 dwarf2_macro_malformed_definition_complaint (const char *arg1)
2098 {
2099 complaint (_("macro debug info contains a "
2100 "malformed macro definition:\n`%s'"),
2101 arg1);
2102 }
2103
2104 static void
2105 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
2106 {
2107 complaint (_("invalid attribute class or form for '%s' in '%s'"),
2108 arg1, arg2);
2109 }
2110
2111 /* Hash function for line_header_hash. */
2112
2113 static hashval_t
2114 line_header_hash (const struct line_header *ofs)
2115 {
2116 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
2117 }
2118
2119 /* Hash function for htab_create_alloc_ex for line_header_hash. */
2120
2121 static hashval_t
2122 line_header_hash_voidp (const void *item)
2123 {
2124 const struct line_header *ofs = (const struct line_header *) item;
2125
2126 return line_header_hash (ofs);
2127 }
2128
2129 /* Equality function for line_header_hash. */
2130
2131 static int
2132 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
2133 {
2134 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
2135 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
2136
2137 return (ofs_lhs->sect_off == ofs_rhs->sect_off
2138 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
2139 }
2140
2141 \f
2142
2143 /* Read the given attribute value as an address, taking the attribute's
2144 form into account. */
2145
2146 static CORE_ADDR
2147 attr_value_as_address (struct attribute *attr)
2148 {
2149 CORE_ADDR addr;
2150
2151 if (attr->form != DW_FORM_addr && attr->form != DW_FORM_addrx
2152 && attr->form != DW_FORM_GNU_addr_index)
2153 {
2154 /* Aside from a few clearly defined exceptions, attributes that
2155 contain an address must always be in DW_FORM_addr form.
2156 Unfortunately, some compilers happen to be violating this
2157 requirement by encoding addresses using other forms, such
2158 as DW_FORM_data4 for example. For those broken compilers,
2159 we try to do our best, without any guarantee of success,
2160 to interpret the address correctly. It would also be nice
2161 to generate a complaint, but that would require us to maintain
2162 a list of legitimate cases where a non-address form is allowed,
2163 as well as update callers to pass in at least the CU's DWARF
2164 version. This is more overhead than what we're willing to
2165 expand for a pretty rare case. */
2166 addr = DW_UNSND (attr);
2167 }
2168 else
2169 addr = DW_ADDR (attr);
2170
2171 return addr;
2172 }
2173
2174 /* See declaration. */
2175
2176 dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
2177 const dwarf2_debug_sections *names,
2178 bool can_copy_)
2179 : objfile (objfile_),
2180 can_copy (can_copy_)
2181 {
2182 if (names == NULL)
2183 names = &dwarf2_elf_names;
2184
2185 bfd *obfd = objfile->obfd;
2186
2187 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
2188 locate_sections (obfd, sec, *names);
2189 }
2190
2191 dwarf2_per_objfile::~dwarf2_per_objfile ()
2192 {
2193 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
2194 free_cached_comp_units ();
2195
2196 if (quick_file_names_table)
2197 htab_delete (quick_file_names_table);
2198
2199 if (line_header_hash)
2200 htab_delete (line_header_hash);
2201
2202 for (dwarf2_per_cu_data *per_cu : all_comp_units)
2203 per_cu->imported_symtabs_free ();
2204
2205 for (signatured_type *sig_type : all_type_units)
2206 sig_type->per_cu.imported_symtabs_free ();
2207
2208 /* Everything else should be on the objfile obstack. */
2209 }
2210
2211 /* See declaration. */
2212
2213 void
2214 dwarf2_per_objfile::free_cached_comp_units ()
2215 {
2216 dwarf2_per_cu_data *per_cu = read_in_chain;
2217 dwarf2_per_cu_data **last_chain = &read_in_chain;
2218 while (per_cu != NULL)
2219 {
2220 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
2221
2222 delete per_cu->cu;
2223 *last_chain = next_cu;
2224 per_cu = next_cu;
2225 }
2226 }
2227
2228 /* A helper class that calls free_cached_comp_units on
2229 destruction. */
2230
2231 class free_cached_comp_units
2232 {
2233 public:
2234
2235 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
2236 : m_per_objfile (per_objfile)
2237 {
2238 }
2239
2240 ~free_cached_comp_units ()
2241 {
2242 m_per_objfile->free_cached_comp_units ();
2243 }
2244
2245 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
2246
2247 private:
2248
2249 dwarf2_per_objfile *m_per_objfile;
2250 };
2251
2252 /* Try to locate the sections we need for DWARF 2 debugging
2253 information and return true if we have enough to do something.
2254 NAMES points to the dwarf2 section names, or is NULL if the standard
2255 ELF names are used. CAN_COPY is true for formats where symbol
2256 interposition is possible and so symbol values must follow copy
2257 relocation rules. */
2258
2259 int
2260 dwarf2_has_info (struct objfile *objfile,
2261 const struct dwarf2_debug_sections *names,
2262 bool can_copy)
2263 {
2264 if (objfile->flags & OBJF_READNEVER)
2265 return 0;
2266
2267 struct dwarf2_per_objfile *dwarf2_per_objfile
2268 = get_dwarf2_per_objfile (objfile);
2269
2270 if (dwarf2_per_objfile == NULL)
2271 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile,
2272 names,
2273 can_copy);
2274
2275 return (!dwarf2_per_objfile->info.is_virtual
2276 && dwarf2_per_objfile->info.s.section != NULL
2277 && !dwarf2_per_objfile->abbrev.is_virtual
2278 && dwarf2_per_objfile->abbrev.s.section != NULL);
2279 }
2280
2281 /* Return the containing section of virtual section SECTION. */
2282
2283 static struct dwarf2_section_info *
2284 get_containing_section (const struct dwarf2_section_info *section)
2285 {
2286 gdb_assert (section->is_virtual);
2287 return section->s.containing_section;
2288 }
2289
2290 /* Return the bfd owner of SECTION. */
2291
2292 static struct bfd *
2293 get_section_bfd_owner (const struct dwarf2_section_info *section)
2294 {
2295 if (section->is_virtual)
2296 {
2297 section = get_containing_section (section);
2298 gdb_assert (!section->is_virtual);
2299 }
2300 return section->s.section->owner;
2301 }
2302
2303 /* Return the bfd section of SECTION.
2304 Returns NULL if the section is not present. */
2305
2306 static asection *
2307 get_section_bfd_section (const struct dwarf2_section_info *section)
2308 {
2309 if (section->is_virtual)
2310 {
2311 section = get_containing_section (section);
2312 gdb_assert (!section->is_virtual);
2313 }
2314 return section->s.section;
2315 }
2316
2317 /* Return the name of SECTION. */
2318
2319 static const char *
2320 get_section_name (const struct dwarf2_section_info *section)
2321 {
2322 asection *sectp = get_section_bfd_section (section);
2323
2324 gdb_assert (sectp != NULL);
2325 return bfd_section_name (sectp);
2326 }
2327
2328 /* Return the name of the file SECTION is in. */
2329
2330 static const char *
2331 get_section_file_name (const struct dwarf2_section_info *section)
2332 {
2333 bfd *abfd = get_section_bfd_owner (section);
2334
2335 return bfd_get_filename (abfd);
2336 }
2337
2338 /* Return the id of SECTION.
2339 Returns 0 if SECTION doesn't exist. */
2340
2341 static int
2342 get_section_id (const struct dwarf2_section_info *section)
2343 {
2344 asection *sectp = get_section_bfd_section (section);
2345
2346 if (sectp == NULL)
2347 return 0;
2348 return sectp->id;
2349 }
2350
2351 /* Return the flags of SECTION.
2352 SECTION (or containing section if this is a virtual section) must exist. */
2353
2354 static int
2355 get_section_flags (const struct dwarf2_section_info *section)
2356 {
2357 asection *sectp = get_section_bfd_section (section);
2358
2359 gdb_assert (sectp != NULL);
2360 return bfd_section_flags (sectp);
2361 }
2362
2363 /* When loading sections, we look either for uncompressed section or for
2364 compressed section names. */
2365
2366 static int
2367 section_is_p (const char *section_name,
2368 const struct dwarf2_section_names *names)
2369 {
2370 if (names->normal != NULL
2371 && strcmp (section_name, names->normal) == 0)
2372 return 1;
2373 if (names->compressed != NULL
2374 && strcmp (section_name, names->compressed) == 0)
2375 return 1;
2376 return 0;
2377 }
2378
2379 /* See declaration. */
2380
2381 void
2382 dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
2383 const dwarf2_debug_sections &names)
2384 {
2385 flagword aflag = bfd_section_flags (sectp);
2386
2387 if ((aflag & SEC_HAS_CONTENTS) == 0)
2388 {
2389 }
2390 else if (elf_section_data (sectp)->this_hdr.sh_size
2391 > bfd_get_file_size (abfd))
2392 {
2393 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
2394 warning (_("Discarding section %s which has a section size (%s"
2395 ") larger than the file size [in module %s]"),
2396 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
2397 bfd_get_filename (abfd));
2398 }
2399 else if (section_is_p (sectp->name, &names.info))
2400 {
2401 this->info.s.section = sectp;
2402 this->info.size = bfd_section_size (sectp);
2403 }
2404 else if (section_is_p (sectp->name, &names.abbrev))
2405 {
2406 this->abbrev.s.section = sectp;
2407 this->abbrev.size = bfd_section_size (sectp);
2408 }
2409 else if (section_is_p (sectp->name, &names.line))
2410 {
2411 this->line.s.section = sectp;
2412 this->line.size = bfd_section_size (sectp);
2413 }
2414 else if (section_is_p (sectp->name, &names.loc))
2415 {
2416 this->loc.s.section = sectp;
2417 this->loc.size = bfd_section_size (sectp);
2418 }
2419 else if (section_is_p (sectp->name, &names.loclists))
2420 {
2421 this->loclists.s.section = sectp;
2422 this->loclists.size = bfd_section_size (sectp);
2423 }
2424 else if (section_is_p (sectp->name, &names.macinfo))
2425 {
2426 this->macinfo.s.section = sectp;
2427 this->macinfo.size = bfd_section_size (sectp);
2428 }
2429 else if (section_is_p (sectp->name, &names.macro))
2430 {
2431 this->macro.s.section = sectp;
2432 this->macro.size = bfd_section_size (sectp);
2433 }
2434 else if (section_is_p (sectp->name, &names.str))
2435 {
2436 this->str.s.section = sectp;
2437 this->str.size = bfd_section_size (sectp);
2438 }
2439 else if (section_is_p (sectp->name, &names.str_offsets))
2440 {
2441 this->str_offsets.s.section = sectp;
2442 this->str_offsets.size = bfd_section_size (sectp);
2443 }
2444 else if (section_is_p (sectp->name, &names.line_str))
2445 {
2446 this->line_str.s.section = sectp;
2447 this->line_str.size = bfd_section_size (sectp);
2448 }
2449 else if (section_is_p (sectp->name, &names.addr))
2450 {
2451 this->addr.s.section = sectp;
2452 this->addr.size = bfd_section_size (sectp);
2453 }
2454 else if (section_is_p (sectp->name, &names.frame))
2455 {
2456 this->frame.s.section = sectp;
2457 this->frame.size = bfd_section_size (sectp);
2458 }
2459 else if (section_is_p (sectp->name, &names.eh_frame))
2460 {
2461 this->eh_frame.s.section = sectp;
2462 this->eh_frame.size = bfd_section_size (sectp);
2463 }
2464 else if (section_is_p (sectp->name, &names.ranges))
2465 {
2466 this->ranges.s.section = sectp;
2467 this->ranges.size = bfd_section_size (sectp);
2468 }
2469 else if (section_is_p (sectp->name, &names.rnglists))
2470 {
2471 this->rnglists.s.section = sectp;
2472 this->rnglists.size = bfd_section_size (sectp);
2473 }
2474 else if (section_is_p (sectp->name, &names.types))
2475 {
2476 struct dwarf2_section_info type_section;
2477
2478 memset (&type_section, 0, sizeof (type_section));
2479 type_section.s.section = sectp;
2480 type_section.size = bfd_section_size (sectp);
2481
2482 this->types.push_back (type_section);
2483 }
2484 else if (section_is_p (sectp->name, &names.gdb_index))
2485 {
2486 this->gdb_index.s.section = sectp;
2487 this->gdb_index.size = bfd_section_size (sectp);
2488 }
2489 else if (section_is_p (sectp->name, &names.debug_names))
2490 {
2491 this->debug_names.s.section = sectp;
2492 this->debug_names.size = bfd_section_size (sectp);
2493 }
2494 else if (section_is_p (sectp->name, &names.debug_aranges))
2495 {
2496 this->debug_aranges.s.section = sectp;
2497 this->debug_aranges.size = bfd_section_size (sectp);
2498 }
2499
2500 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2501 && bfd_section_vma (sectp) == 0)
2502 this->has_section_at_zero = true;
2503 }
2504
2505 /* A helper function that decides whether a section is empty,
2506 or not present. */
2507
2508 static int
2509 dwarf2_section_empty_p (const struct dwarf2_section_info *section)
2510 {
2511 if (section->is_virtual)
2512 return section->size == 0;
2513 return section->s.section == NULL || section->size == 0;
2514 }
2515
2516 /* See dwarf2read.h. */
2517
2518 void
2519 dwarf2_read_section (struct objfile *objfile, dwarf2_section_info *info)
2520 {
2521 asection *sectp;
2522 bfd *abfd;
2523 gdb_byte *buf, *retbuf;
2524
2525 if (info->readin)
2526 return;
2527 info->buffer = NULL;
2528 info->readin = true;
2529
2530 if (dwarf2_section_empty_p (info))
2531 return;
2532
2533 sectp = get_section_bfd_section (info);
2534
2535 /* If this is a virtual section we need to read in the real one first. */
2536 if (info->is_virtual)
2537 {
2538 struct dwarf2_section_info *containing_section =
2539 get_containing_section (info);
2540
2541 gdb_assert (sectp != NULL);
2542 if ((sectp->flags & SEC_RELOC) != 0)
2543 {
2544 error (_("Dwarf Error: DWP format V2 with relocations is not"
2545 " supported in section %s [in module %s]"),
2546 get_section_name (info), get_section_file_name (info));
2547 }
2548 dwarf2_read_section (objfile, containing_section);
2549 /* Other code should have already caught virtual sections that don't
2550 fit. */
2551 gdb_assert (info->virtual_offset + info->size
2552 <= containing_section->size);
2553 /* If the real section is empty or there was a problem reading the
2554 section we shouldn't get here. */
2555 gdb_assert (containing_section->buffer != NULL);
2556 info->buffer = containing_section->buffer + info->virtual_offset;
2557 return;
2558 }
2559
2560 /* If the section has relocations, we must read it ourselves.
2561 Otherwise we attach it to the BFD. */
2562 if ((sectp->flags & SEC_RELOC) == 0)
2563 {
2564 info->buffer = gdb_bfd_map_section (sectp, &info->size);
2565 return;
2566 }
2567
2568 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, info->size);
2569 info->buffer = buf;
2570
2571 /* When debugging .o files, we may need to apply relocations; see
2572 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2573 We never compress sections in .o files, so we only need to
2574 try this when the section is not compressed. */
2575 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
2576 if (retbuf != NULL)
2577 {
2578 info->buffer = retbuf;
2579 return;
2580 }
2581
2582 abfd = get_section_bfd_owner (info);
2583 gdb_assert (abfd != NULL);
2584
2585 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2586 || bfd_bread (buf, info->size, abfd) != info->size)
2587 {
2588 error (_("Dwarf Error: Can't read DWARF data"
2589 " in section %s [in module %s]"),
2590 bfd_section_name (sectp), bfd_get_filename (abfd));
2591 }
2592 }
2593
2594 /* A helper function that returns the size of a section in a safe way.
2595 If you are positive that the section has been read before using the
2596 size, then it is safe to refer to the dwarf2_section_info object's
2597 "size" field directly. In other cases, you must call this
2598 function, because for compressed sections the size field is not set
2599 correctly until the section has been read. */
2600
2601 static bfd_size_type
2602 dwarf2_section_size (struct objfile *objfile,
2603 struct dwarf2_section_info *info)
2604 {
2605 if (!info->readin)
2606 dwarf2_read_section (objfile, info);
2607 return info->size;
2608 }
2609
2610 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2611 SECTION_NAME. */
2612
2613 void
2614 dwarf2_get_section_info (struct objfile *objfile,
2615 enum dwarf2_section_enum sect,
2616 asection **sectp, const gdb_byte **bufp,
2617 bfd_size_type *sizep)
2618 {
2619 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
2620 struct dwarf2_section_info *info;
2621
2622 /* We may see an objfile without any DWARF, in which case we just
2623 return nothing. */
2624 if (data == NULL)
2625 {
2626 *sectp = NULL;
2627 *bufp = NULL;
2628 *sizep = 0;
2629 return;
2630 }
2631 switch (sect)
2632 {
2633 case DWARF2_DEBUG_FRAME:
2634 info = &data->frame;
2635 break;
2636 case DWARF2_EH_FRAME:
2637 info = &data->eh_frame;
2638 break;
2639 default:
2640 gdb_assert_not_reached ("unexpected section");
2641 }
2642
2643 dwarf2_read_section (objfile, info);
2644
2645 *sectp = get_section_bfd_section (info);
2646 *bufp = info->buffer;
2647 *sizep = info->size;
2648 }
2649
2650 /* A helper function to find the sections for a .dwz file. */
2651
2652 static void
2653 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2654 {
2655 struct dwz_file *dwz_file = (struct dwz_file *) arg;
2656
2657 /* Note that we only support the standard ELF names, because .dwz
2658 is ELF-only (at the time of writing). */
2659 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2660 {
2661 dwz_file->abbrev.s.section = sectp;
2662 dwz_file->abbrev.size = bfd_section_size (sectp);
2663 }
2664 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2665 {
2666 dwz_file->info.s.section = sectp;
2667 dwz_file->info.size = bfd_section_size (sectp);
2668 }
2669 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2670 {
2671 dwz_file->str.s.section = sectp;
2672 dwz_file->str.size = bfd_section_size (sectp);
2673 }
2674 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2675 {
2676 dwz_file->line.s.section = sectp;
2677 dwz_file->line.size = bfd_section_size (sectp);
2678 }
2679 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2680 {
2681 dwz_file->macro.s.section = sectp;
2682 dwz_file->macro.size = bfd_section_size (sectp);
2683 }
2684 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2685 {
2686 dwz_file->gdb_index.s.section = sectp;
2687 dwz_file->gdb_index.size = bfd_section_size (sectp);
2688 }
2689 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2690 {
2691 dwz_file->debug_names.s.section = sectp;
2692 dwz_file->debug_names.size = bfd_section_size (sectp);
2693 }
2694 }
2695
2696 /* See dwarf2read.h. */
2697
2698 struct dwz_file *
2699 dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
2700 {
2701 const char *filename;
2702 bfd_size_type buildid_len_arg;
2703 size_t buildid_len;
2704 bfd_byte *buildid;
2705
2706 if (dwarf2_per_objfile->dwz_file != NULL)
2707 return dwarf2_per_objfile->dwz_file.get ();
2708
2709 bfd_set_error (bfd_error_no_error);
2710 gdb::unique_xmalloc_ptr<char> data
2711 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2712 &buildid_len_arg, &buildid));
2713 if (data == NULL)
2714 {
2715 if (bfd_get_error () == bfd_error_no_error)
2716 return NULL;
2717 error (_("could not read '.gnu_debugaltlink' section: %s"),
2718 bfd_errmsg (bfd_get_error ()));
2719 }
2720
2721 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
2722
2723 buildid_len = (size_t) buildid_len_arg;
2724
2725 filename = data.get ();
2726
2727 std::string abs_storage;
2728 if (!IS_ABSOLUTE_PATH (filename))
2729 {
2730 gdb::unique_xmalloc_ptr<char> abs
2731 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
2732
2733 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
2734 filename = abs_storage.c_str ();
2735 }
2736
2737 /* First try the file name given in the section. If that doesn't
2738 work, try to use the build-id instead. */
2739 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
2740 if (dwz_bfd != NULL)
2741 {
2742 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2743 dwz_bfd.reset (nullptr);
2744 }
2745
2746 if (dwz_bfd == NULL)
2747 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2748
2749 if (dwz_bfd == NULL)
2750 error (_("could not find '.gnu_debugaltlink' file for %s"),
2751 objfile_name (dwarf2_per_objfile->objfile));
2752
2753 std::unique_ptr<struct dwz_file> result
2754 (new struct dwz_file (std::move (dwz_bfd)));
2755
2756 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2757 result.get ());
2758
2759 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd,
2760 result->dwz_bfd.get ());
2761 dwarf2_per_objfile->dwz_file = std::move (result);
2762 return dwarf2_per_objfile->dwz_file.get ();
2763 }
2764 \f
2765 /* DWARF quick_symbols_functions support. */
2766
2767 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2768 unique line tables, so we maintain a separate table of all .debug_line
2769 derived entries to support the sharing.
2770 All the quick functions need is the list of file names. We discard the
2771 line_header when we're done and don't need to record it here. */
2772 struct quick_file_names
2773 {
2774 /* The data used to construct the hash key. */
2775 struct stmt_list_hash hash;
2776
2777 /* The number of entries in file_names, real_names. */
2778 unsigned int num_file_names;
2779
2780 /* The file names from the line table, after being run through
2781 file_full_name. */
2782 const char **file_names;
2783
2784 /* The file names from the line table after being run through
2785 gdb_realpath. These are computed lazily. */
2786 const char **real_names;
2787 };
2788
2789 /* When using the index (and thus not using psymtabs), each CU has an
2790 object of this type. This is used to hold information needed by
2791 the various "quick" methods. */
2792 struct dwarf2_per_cu_quick_data
2793 {
2794 /* The file table. This can be NULL if there was no file table
2795 or it's currently not read in.
2796 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2797 struct quick_file_names *file_names;
2798
2799 /* The corresponding symbol table. This is NULL if symbols for this
2800 CU have not yet been read. */
2801 struct compunit_symtab *compunit_symtab;
2802
2803 /* A temporary mark bit used when iterating over all CUs in
2804 expand_symtabs_matching. */
2805 unsigned int mark : 1;
2806
2807 /* True if we've tried to read the file table and found there isn't one.
2808 There will be no point in trying to read it again next time. */
2809 unsigned int no_file_data : 1;
2810 };
2811
2812 /* Utility hash function for a stmt_list_hash. */
2813
2814 static hashval_t
2815 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2816 {
2817 hashval_t v = 0;
2818
2819 if (stmt_list_hash->dwo_unit != NULL)
2820 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2821 v += to_underlying (stmt_list_hash->line_sect_off);
2822 return v;
2823 }
2824
2825 /* Utility equality function for a stmt_list_hash. */
2826
2827 static int
2828 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2829 const struct stmt_list_hash *rhs)
2830 {
2831 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2832 return 0;
2833 if (lhs->dwo_unit != NULL
2834 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2835 return 0;
2836
2837 return lhs->line_sect_off == rhs->line_sect_off;
2838 }
2839
2840 /* Hash function for a quick_file_names. */
2841
2842 static hashval_t
2843 hash_file_name_entry (const void *e)
2844 {
2845 const struct quick_file_names *file_data
2846 = (const struct quick_file_names *) e;
2847
2848 return hash_stmt_list_entry (&file_data->hash);
2849 }
2850
2851 /* Equality function for a quick_file_names. */
2852
2853 static int
2854 eq_file_name_entry (const void *a, const void *b)
2855 {
2856 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2857 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2858
2859 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2860 }
2861
2862 /* Delete function for a quick_file_names. */
2863
2864 static void
2865 delete_file_name_entry (void *e)
2866 {
2867 struct quick_file_names *file_data = (struct quick_file_names *) e;
2868 int i;
2869
2870 for (i = 0; i < file_data->num_file_names; ++i)
2871 {
2872 xfree ((void*) file_data->file_names[i]);
2873 if (file_data->real_names)
2874 xfree ((void*) file_data->real_names[i]);
2875 }
2876
2877 /* The space for the struct itself lives on objfile_obstack,
2878 so we don't free it here. */
2879 }
2880
2881 /* Create a quick_file_names hash table. */
2882
2883 static htab_t
2884 create_quick_file_names_table (unsigned int nr_initial_entries)
2885 {
2886 return htab_create_alloc (nr_initial_entries,
2887 hash_file_name_entry, eq_file_name_entry,
2888 delete_file_name_entry, xcalloc, xfree);
2889 }
2890
2891 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2892 have to be created afterwards. You should call age_cached_comp_units after
2893 processing PER_CU->CU. dw2_setup must have been already called. */
2894
2895 static void
2896 load_cu (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2897 {
2898 if (per_cu->is_debug_types)
2899 load_full_type_unit (per_cu);
2900 else
2901 load_full_comp_unit (per_cu, skip_partial, language_minimal);
2902
2903 if (per_cu->cu == NULL)
2904 return; /* Dummy CU. */
2905
2906 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2907 }
2908
2909 /* Read in the symbols for PER_CU. */
2910
2911 static void
2912 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2913 {
2914 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2915
2916 /* Skip type_unit_groups, reading the type units they contain
2917 is handled elsewhere. */
2918 if (IS_TYPE_UNIT_GROUP (per_cu))
2919 return;
2920
2921 /* The destructor of dwarf2_queue_guard frees any entries left on
2922 the queue. After this point we're guaranteed to leave this function
2923 with the dwarf queue empty. */
2924 dwarf2_queue_guard q_guard;
2925
2926 if (dwarf2_per_objfile->using_index
2927 ? per_cu->v.quick->compunit_symtab == NULL
2928 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2929 {
2930 queue_comp_unit (per_cu, language_minimal);
2931 load_cu (per_cu, skip_partial);
2932
2933 /* If we just loaded a CU from a DWO, and we're working with an index
2934 that may badly handle TUs, load all the TUs in that DWO as well.
2935 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2936 if (!per_cu->is_debug_types
2937 && per_cu->cu != NULL
2938 && per_cu->cu->dwo_unit != NULL
2939 && dwarf2_per_objfile->index_table != NULL
2940 && dwarf2_per_objfile->index_table->version <= 7
2941 /* DWP files aren't supported yet. */
2942 && get_dwp_file (dwarf2_per_objfile) == NULL)
2943 queue_and_load_all_dwo_tus (per_cu);
2944 }
2945
2946 process_queue (dwarf2_per_objfile);
2947
2948 /* Age the cache, releasing compilation units that have not
2949 been used recently. */
2950 age_cached_comp_units (dwarf2_per_objfile);
2951 }
2952
2953 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2954 the objfile from which this CU came. Returns the resulting symbol
2955 table. */
2956
2957 static struct compunit_symtab *
2958 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2959 {
2960 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2961
2962 gdb_assert (dwarf2_per_objfile->using_index);
2963 if (!per_cu->v.quick->compunit_symtab)
2964 {
2965 free_cached_comp_units freer (dwarf2_per_objfile);
2966 scoped_restore decrementer = increment_reading_symtab ();
2967 dw2_do_instantiate_symtab (per_cu, skip_partial);
2968 process_cu_includes (dwarf2_per_objfile);
2969 }
2970
2971 return per_cu->v.quick->compunit_symtab;
2972 }
2973
2974 /* See declaration. */
2975
2976 dwarf2_per_cu_data *
2977 dwarf2_per_objfile::get_cutu (int index)
2978 {
2979 if (index >= this->all_comp_units.size ())
2980 {
2981 index -= this->all_comp_units.size ();
2982 gdb_assert (index < this->all_type_units.size ());
2983 return &this->all_type_units[index]->per_cu;
2984 }
2985
2986 return this->all_comp_units[index];
2987 }
2988
2989 /* See declaration. */
2990
2991 dwarf2_per_cu_data *
2992 dwarf2_per_objfile::get_cu (int index)
2993 {
2994 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
2995
2996 return this->all_comp_units[index];
2997 }
2998
2999 /* See declaration. */
3000
3001 signatured_type *
3002 dwarf2_per_objfile::get_tu (int index)
3003 {
3004 gdb_assert (index >= 0 && index < this->all_type_units.size ());
3005
3006 return this->all_type_units[index];
3007 }
3008
3009 /* Return a new dwarf2_per_cu_data allocated on OBJFILE's
3010 objfile_obstack, and constructed with the specified field
3011 values. */
3012
3013 static dwarf2_per_cu_data *
3014 create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
3015 struct dwarf2_section_info *section,
3016 int is_dwz,
3017 sect_offset sect_off, ULONGEST length)
3018 {
3019 struct objfile *objfile = dwarf2_per_objfile->objfile;
3020 dwarf2_per_cu_data *the_cu
3021 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3022 struct dwarf2_per_cu_data);
3023 the_cu->sect_off = sect_off;
3024 the_cu->length = length;
3025 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
3026 the_cu->section = section;
3027 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3028 struct dwarf2_per_cu_quick_data);
3029 the_cu->is_dwz = is_dwz;
3030 return the_cu;
3031 }
3032
3033 /* A helper for create_cus_from_index that handles a given list of
3034 CUs. */
3035
3036 static void
3037 create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
3038 const gdb_byte *cu_list, offset_type n_elements,
3039 struct dwarf2_section_info *section,
3040 int is_dwz)
3041 {
3042 for (offset_type i = 0; i < n_elements; i += 2)
3043 {
3044 gdb_static_assert (sizeof (ULONGEST) >= 8);
3045
3046 sect_offset sect_off
3047 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
3048 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
3049 cu_list += 2 * 8;
3050
3051 dwarf2_per_cu_data *per_cu
3052 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
3053 sect_off, length);
3054 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
3055 }
3056 }
3057
3058 /* Read the CU list from the mapped index, and use it to create all
3059 the CU objects for this objfile. */
3060
3061 static void
3062 create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
3063 const gdb_byte *cu_list, offset_type cu_list_elements,
3064 const gdb_byte *dwz_list, offset_type dwz_elements)
3065 {
3066 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
3067 dwarf2_per_objfile->all_comp_units.reserve
3068 ((cu_list_elements + dwz_elements) / 2);
3069
3070 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
3071 &dwarf2_per_objfile->info, 0);
3072
3073 if (dwz_elements == 0)
3074 return;
3075
3076 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3077 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
3078 &dwz->info, 1);
3079 }
3080
3081 /* Create the signatured type hash table from the index. */
3082
3083 static void
3084 create_signatured_type_table_from_index
3085 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3086 struct dwarf2_section_info *section,
3087 const gdb_byte *bytes,
3088 offset_type elements)
3089 {
3090 struct objfile *objfile = dwarf2_per_objfile->objfile;
3091
3092 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
3093 dwarf2_per_objfile->all_type_units.reserve (elements / 3);
3094
3095 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
3096
3097 for (offset_type i = 0; i < elements; i += 3)
3098 {
3099 struct signatured_type *sig_type;
3100 ULONGEST signature;
3101 void **slot;
3102 cu_offset type_offset_in_tu;
3103
3104 gdb_static_assert (sizeof (ULONGEST) >= 8);
3105 sect_offset sect_off
3106 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
3107 type_offset_in_tu
3108 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
3109 BFD_ENDIAN_LITTLE);
3110 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
3111 bytes += 3 * 8;
3112
3113 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3114 struct signatured_type);
3115 sig_type->signature = signature;
3116 sig_type->type_offset_in_tu = type_offset_in_tu;
3117 sig_type->per_cu.is_debug_types = 1;
3118 sig_type->per_cu.section = section;
3119 sig_type->per_cu.sect_off = sect_off;
3120 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3121 sig_type->per_cu.v.quick
3122 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3123 struct dwarf2_per_cu_quick_data);
3124
3125 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3126 *slot = sig_type;
3127
3128 dwarf2_per_objfile->all_type_units.push_back (sig_type);
3129 }
3130
3131 dwarf2_per_objfile->signatured_types = sig_types_hash;
3132 }
3133
3134 /* Create the signatured type hash table from .debug_names. */
3135
3136 static void
3137 create_signatured_type_table_from_debug_names
3138 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3139 const mapped_debug_names &map,
3140 struct dwarf2_section_info *section,
3141 struct dwarf2_section_info *abbrev_section)
3142 {
3143 struct objfile *objfile = dwarf2_per_objfile->objfile;
3144
3145 dwarf2_read_section (objfile, section);
3146 dwarf2_read_section (objfile, abbrev_section);
3147
3148 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
3149 dwarf2_per_objfile->all_type_units.reserve (map.tu_count);
3150
3151 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
3152
3153 for (uint32_t i = 0; i < map.tu_count; ++i)
3154 {
3155 struct signatured_type *sig_type;
3156 void **slot;
3157
3158 sect_offset sect_off
3159 = (sect_offset) (extract_unsigned_integer
3160 (map.tu_table_reordered + i * map.offset_size,
3161 map.offset_size,
3162 map.dwarf5_byte_order));
3163
3164 comp_unit_head cu_header;
3165 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
3166 abbrev_section,
3167 section->buffer + to_underlying (sect_off),
3168 rcuh_kind::TYPE);
3169
3170 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3171 struct signatured_type);
3172 sig_type->signature = cu_header.signature;
3173 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
3174 sig_type->per_cu.is_debug_types = 1;
3175 sig_type->per_cu.section = section;
3176 sig_type->per_cu.sect_off = sect_off;
3177 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3178 sig_type->per_cu.v.quick
3179 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3180 struct dwarf2_per_cu_quick_data);
3181
3182 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3183 *slot = sig_type;
3184
3185 dwarf2_per_objfile->all_type_units.push_back (sig_type);
3186 }
3187
3188 dwarf2_per_objfile->signatured_types = sig_types_hash;
3189 }
3190
3191 /* Read the address map data from the mapped index, and use it to
3192 populate the objfile's psymtabs_addrmap. */
3193
3194 static void
3195 create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
3196 struct mapped_index *index)
3197 {
3198 struct objfile *objfile = dwarf2_per_objfile->objfile;
3199 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3200 const gdb_byte *iter, *end;
3201 struct addrmap *mutable_map;
3202 CORE_ADDR baseaddr;
3203
3204 auto_obstack temp_obstack;
3205
3206 mutable_map = addrmap_create_mutable (&temp_obstack);
3207
3208 iter = index->address_table.data ();
3209 end = iter + index->address_table.size ();
3210
3211 baseaddr = objfile->text_section_offset ();
3212
3213 while (iter < end)
3214 {
3215 ULONGEST hi, lo, cu_index;
3216 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3217 iter += 8;
3218 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3219 iter += 8;
3220 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
3221 iter += 4;
3222
3223 if (lo > hi)
3224 {
3225 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
3226 hex_string (lo), hex_string (hi));
3227 continue;
3228 }
3229
3230 if (cu_index >= dwarf2_per_objfile->all_comp_units.size ())
3231 {
3232 complaint (_(".gdb_index address table has invalid CU number %u"),
3233 (unsigned) cu_index);
3234 continue;
3235 }
3236
3237 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
3238 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
3239 addrmap_set_empty (mutable_map, lo, hi - 1,
3240 dwarf2_per_objfile->get_cu (cu_index));
3241 }
3242
3243 objfile->partial_symtabs->psymtabs_addrmap
3244 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
3245 }
3246
3247 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
3248 populate the objfile's psymtabs_addrmap. */
3249
3250 static void
3251 create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
3252 struct dwarf2_section_info *section)
3253 {
3254 struct objfile *objfile = dwarf2_per_objfile->objfile;
3255 bfd *abfd = objfile->obfd;
3256 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3257 const CORE_ADDR baseaddr = objfile->text_section_offset ();
3258
3259 auto_obstack temp_obstack;
3260 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
3261
3262 std::unordered_map<sect_offset,
3263 dwarf2_per_cu_data *,
3264 gdb::hash_enum<sect_offset>>
3265 debug_info_offset_to_per_cu;
3266 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3267 {
3268 const auto insertpair
3269 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
3270 if (!insertpair.second)
3271 {
3272 warning (_("Section .debug_aranges in %s has duplicate "
3273 "debug_info_offset %s, ignoring .debug_aranges."),
3274 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
3275 return;
3276 }
3277 }
3278
3279 dwarf2_read_section (objfile, section);
3280
3281 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
3282
3283 const gdb_byte *addr = section->buffer;
3284
3285 while (addr < section->buffer + section->size)
3286 {
3287 const gdb_byte *const entry_addr = addr;
3288 unsigned int bytes_read;
3289
3290 const LONGEST entry_length = read_initial_length (abfd, addr,
3291 &bytes_read);
3292 addr += bytes_read;
3293
3294 const gdb_byte *const entry_end = addr + entry_length;
3295 const bool dwarf5_is_dwarf64 = bytes_read != 4;
3296 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
3297 if (addr + entry_length > section->buffer + section->size)
3298 {
3299 warning (_("Section .debug_aranges in %s entry at offset %s "
3300 "length %s exceeds section length %s, "
3301 "ignoring .debug_aranges."),
3302 objfile_name (objfile),
3303 plongest (entry_addr - section->buffer),
3304 plongest (bytes_read + entry_length),
3305 pulongest (section->size));
3306 return;
3307 }
3308
3309 /* The version number. */
3310 const uint16_t version = read_2_bytes (abfd, addr);
3311 addr += 2;
3312 if (version != 2)
3313 {
3314 warning (_("Section .debug_aranges in %s entry at offset %s "
3315 "has unsupported version %d, ignoring .debug_aranges."),
3316 objfile_name (objfile),
3317 plongest (entry_addr - section->buffer), version);
3318 return;
3319 }
3320
3321 const uint64_t debug_info_offset
3322 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
3323 addr += offset_size;
3324 const auto per_cu_it
3325 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
3326 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
3327 {
3328 warning (_("Section .debug_aranges in %s entry at offset %s "
3329 "debug_info_offset %s does not exists, "
3330 "ignoring .debug_aranges."),
3331 objfile_name (objfile),
3332 plongest (entry_addr - section->buffer),
3333 pulongest (debug_info_offset));
3334 return;
3335 }
3336 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
3337
3338 const uint8_t address_size = *addr++;
3339 if (address_size < 1 || address_size > 8)
3340 {
3341 warning (_("Section .debug_aranges in %s entry at offset %s "
3342 "address_size %u is invalid, ignoring .debug_aranges."),
3343 objfile_name (objfile),
3344 plongest (entry_addr - section->buffer), address_size);
3345 return;
3346 }
3347
3348 const uint8_t segment_selector_size = *addr++;
3349 if (segment_selector_size != 0)
3350 {
3351 warning (_("Section .debug_aranges in %s entry at offset %s "
3352 "segment_selector_size %u is not supported, "
3353 "ignoring .debug_aranges."),
3354 objfile_name (objfile),
3355 plongest (entry_addr - section->buffer),
3356 segment_selector_size);
3357 return;
3358 }
3359
3360 /* Must pad to an alignment boundary that is twice the address
3361 size. It is undocumented by the DWARF standard but GCC does
3362 use it. */
3363 for (size_t padding = ((-(addr - section->buffer))
3364 & (2 * address_size - 1));
3365 padding > 0; padding--)
3366 if (*addr++ != 0)
3367 {
3368 warning (_("Section .debug_aranges in %s entry at offset %s "
3369 "padding is not zero, ignoring .debug_aranges."),
3370 objfile_name (objfile),
3371 plongest (entry_addr - section->buffer));
3372 return;
3373 }
3374
3375 for (;;)
3376 {
3377 if (addr + 2 * address_size > entry_end)
3378 {
3379 warning (_("Section .debug_aranges in %s entry at offset %s "
3380 "address list is not properly terminated, "
3381 "ignoring .debug_aranges."),
3382 objfile_name (objfile),
3383 plongest (entry_addr - section->buffer));
3384 return;
3385 }
3386 ULONGEST start = extract_unsigned_integer (addr, address_size,
3387 dwarf5_byte_order);
3388 addr += address_size;
3389 ULONGEST length = extract_unsigned_integer (addr, address_size,
3390 dwarf5_byte_order);
3391 addr += address_size;
3392 if (start == 0 && length == 0)
3393 break;
3394 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
3395 {
3396 /* Symbol was eliminated due to a COMDAT group. */
3397 continue;
3398 }
3399 ULONGEST end = start + length;
3400 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
3401 - baseaddr);
3402 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
3403 - baseaddr);
3404 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
3405 }
3406 }
3407
3408 objfile->partial_symtabs->psymtabs_addrmap
3409 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
3410 }
3411
3412 /* Find a slot in the mapped index INDEX for the object named NAME.
3413 If NAME is found, set *VEC_OUT to point to the CU vector in the
3414 constant pool and return true. If NAME cannot be found, return
3415 false. */
3416
3417 static bool
3418 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3419 offset_type **vec_out)
3420 {
3421 offset_type hash;
3422 offset_type slot, step;
3423 int (*cmp) (const char *, const char *);
3424
3425 gdb::unique_xmalloc_ptr<char> without_params;
3426 if (current_language->la_language == language_cplus
3427 || current_language->la_language == language_fortran
3428 || current_language->la_language == language_d)
3429 {
3430 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3431 not contain any. */
3432
3433 if (strchr (name, '(') != NULL)
3434 {
3435 without_params = cp_remove_params (name);
3436
3437 if (without_params != NULL)
3438 name = without_params.get ();
3439 }
3440 }
3441
3442 /* Index version 4 did not support case insensitive searches. But the
3443 indices for case insensitive languages are built in lowercase, therefore
3444 simulate our NAME being searched is also lowercased. */
3445 hash = mapped_index_string_hash ((index->version == 4
3446 && case_sensitivity == case_sensitive_off
3447 ? 5 : index->version),
3448 name);
3449
3450 slot = hash & (index->symbol_table.size () - 1);
3451 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
3452 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
3453
3454 for (;;)
3455 {
3456 const char *str;
3457
3458 const auto &bucket = index->symbol_table[slot];
3459 if (bucket.name == 0 && bucket.vec == 0)
3460 return false;
3461
3462 str = index->constant_pool + MAYBE_SWAP (bucket.name);
3463 if (!cmp (name, str))
3464 {
3465 *vec_out = (offset_type *) (index->constant_pool
3466 + MAYBE_SWAP (bucket.vec));
3467 return true;
3468 }
3469
3470 slot = (slot + step) & (index->symbol_table.size () - 1);
3471 }
3472 }
3473
3474 /* A helper function that reads the .gdb_index from BUFFER and fills
3475 in MAP. FILENAME is the name of the file containing the data;
3476 it is used for error reporting. DEPRECATED_OK is true if it is
3477 ok to use deprecated sections.
3478
3479 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3480 out parameters that are filled in with information about the CU and
3481 TU lists in the section.
3482
3483 Returns true if all went well, false otherwise. */
3484
3485 static bool
3486 read_gdb_index_from_buffer (struct objfile *objfile,
3487 const char *filename,
3488 bool deprecated_ok,
3489 gdb::array_view<const gdb_byte> buffer,
3490 struct mapped_index *map,
3491 const gdb_byte **cu_list,
3492 offset_type *cu_list_elements,
3493 const gdb_byte **types_list,
3494 offset_type *types_list_elements)
3495 {
3496 const gdb_byte *addr = &buffer[0];
3497
3498 /* Version check. */
3499 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
3500 /* Versions earlier than 3 emitted every copy of a psymbol. This
3501 causes the index to behave very poorly for certain requests. Version 3
3502 contained incomplete addrmap. So, it seems better to just ignore such
3503 indices. */
3504 if (version < 4)
3505 {
3506 static int warning_printed = 0;
3507 if (!warning_printed)
3508 {
3509 warning (_("Skipping obsolete .gdb_index section in %s."),
3510 filename);
3511 warning_printed = 1;
3512 }
3513 return 0;
3514 }
3515 /* Index version 4 uses a different hash function than index version
3516 5 and later.
3517
3518 Versions earlier than 6 did not emit psymbols for inlined
3519 functions. Using these files will cause GDB not to be able to
3520 set breakpoints on inlined functions by name, so we ignore these
3521 indices unless the user has done
3522 "set use-deprecated-index-sections on". */
3523 if (version < 6 && !deprecated_ok)
3524 {
3525 static int warning_printed = 0;
3526 if (!warning_printed)
3527 {
3528 warning (_("\
3529 Skipping deprecated .gdb_index section in %s.\n\
3530 Do \"set use-deprecated-index-sections on\" before the file is read\n\
3531 to use the section anyway."),
3532 filename);
3533 warning_printed = 1;
3534 }
3535 return 0;
3536 }
3537 /* Version 7 indices generated by gold refer to the CU for a symbol instead
3538 of the TU (for symbols coming from TUs),
3539 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3540 Plus gold-generated indices can have duplicate entries for global symbols,
3541 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3542 These are just performance bugs, and we can't distinguish gdb-generated
3543 indices from gold-generated ones, so issue no warning here. */
3544
3545 /* Indexes with higher version than the one supported by GDB may be no
3546 longer backward compatible. */
3547 if (version > 8)
3548 return 0;
3549
3550 map->version = version;
3551
3552 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
3553
3554 int i = 0;
3555 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3556 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3557 / 8);
3558 ++i;
3559
3560 *types_list = addr + MAYBE_SWAP (metadata[i]);
3561 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3562 - MAYBE_SWAP (metadata[i]))
3563 / 8);
3564 ++i;
3565
3566 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3567 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3568 map->address_table
3569 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
3570 ++i;
3571
3572 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3573 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3574 map->symbol_table
3575 = gdb::array_view<mapped_index::symbol_table_slot>
3576 ((mapped_index::symbol_table_slot *) symbol_table,
3577 (mapped_index::symbol_table_slot *) symbol_table_end);
3578
3579 ++i;
3580 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
3581
3582 return 1;
3583 }
3584
3585 /* Callback types for dwarf2_read_gdb_index. */
3586
3587 typedef gdb::function_view
3588 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_objfile *)>
3589 get_gdb_index_contents_ftype;
3590 typedef gdb::function_view
3591 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3592 get_gdb_index_contents_dwz_ftype;
3593
3594 /* Read .gdb_index. If everything went ok, initialize the "quick"
3595 elements of all the CUs and return 1. Otherwise, return 0. */
3596
3597 static int
3598 dwarf2_read_gdb_index
3599 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3600 get_gdb_index_contents_ftype get_gdb_index_contents,
3601 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
3602 {
3603 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3604 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3605 struct dwz_file *dwz;
3606 struct objfile *objfile = dwarf2_per_objfile->objfile;
3607
3608 gdb::array_view<const gdb_byte> main_index_contents
3609 = get_gdb_index_contents (objfile, dwarf2_per_objfile);
3610
3611 if (main_index_contents.empty ())
3612 return 0;
3613
3614 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3615 if (!read_gdb_index_from_buffer (objfile, objfile_name (objfile),
3616 use_deprecated_index_sections,
3617 main_index_contents, map.get (), &cu_list,
3618 &cu_list_elements, &types_list,
3619 &types_list_elements))
3620 return 0;
3621
3622 /* Don't use the index if it's empty. */
3623 if (map->symbol_table.empty ())
3624 return 0;
3625
3626 /* If there is a .dwz file, read it so we can get its CU list as
3627 well. */
3628 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3629 if (dwz != NULL)
3630 {
3631 struct mapped_index dwz_map;
3632 const gdb_byte *dwz_types_ignore;
3633 offset_type dwz_types_elements_ignore;
3634
3635 gdb::array_view<const gdb_byte> dwz_index_content
3636 = get_gdb_index_contents_dwz (objfile, dwz);
3637
3638 if (dwz_index_content.empty ())
3639 return 0;
3640
3641 if (!read_gdb_index_from_buffer (objfile,
3642 bfd_get_filename (dwz->dwz_bfd.get ()),
3643 1, dwz_index_content, &dwz_map,
3644 &dwz_list, &dwz_list_elements,
3645 &dwz_types_ignore,
3646 &dwz_types_elements_ignore))
3647 {
3648 warning (_("could not read '.gdb_index' section from %s; skipping"),
3649 bfd_get_filename (dwz->dwz_bfd.get ()));
3650 return 0;
3651 }
3652 }
3653
3654 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3655 dwz_list, dwz_list_elements);
3656
3657 if (types_list_elements)
3658 {
3659 /* We can only handle a single .debug_types when we have an
3660 index. */
3661 if (dwarf2_per_objfile->types.size () != 1)
3662 return 0;
3663
3664 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
3665
3666 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3667 types_list, types_list_elements);
3668 }
3669
3670 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
3671
3672 dwarf2_per_objfile->index_table = std::move (map);
3673 dwarf2_per_objfile->using_index = 1;
3674 dwarf2_per_objfile->quick_file_names_table =
3675 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
3676
3677 return 1;
3678 }
3679
3680 /* die_reader_func for dw2_get_file_names. */
3681
3682 static void
3683 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3684 const gdb_byte *info_ptr,
3685 struct die_info *comp_unit_die,
3686 int has_children)
3687 {
3688 struct dwarf2_cu *cu = reader->cu;
3689 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3690 struct dwarf2_per_objfile *dwarf2_per_objfile
3691 = cu->per_cu->dwarf2_per_objfile;
3692 struct objfile *objfile = dwarf2_per_objfile->objfile;
3693 struct dwarf2_per_cu_data *lh_cu;
3694 struct attribute *attr;
3695 void **slot;
3696 struct quick_file_names *qfn;
3697
3698 gdb_assert (! this_cu->is_debug_types);
3699
3700 /* Our callers never want to match partial units -- instead they
3701 will match the enclosing full CU. */
3702 if (comp_unit_die->tag == DW_TAG_partial_unit)
3703 {
3704 this_cu->v.quick->no_file_data = 1;
3705 return;
3706 }
3707
3708 lh_cu = this_cu;
3709 slot = NULL;
3710
3711 line_header_up lh;
3712 sect_offset line_offset {};
3713
3714 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3715 if (attr != nullptr)
3716 {
3717 struct quick_file_names find_entry;
3718
3719 line_offset = (sect_offset) DW_UNSND (attr);
3720
3721 /* We may have already read in this line header (TU line header sharing).
3722 If we have we're done. */
3723 find_entry.hash.dwo_unit = cu->dwo_unit;
3724 find_entry.hash.line_sect_off = line_offset;
3725 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3726 &find_entry, INSERT);
3727 if (*slot != NULL)
3728 {
3729 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3730 return;
3731 }
3732
3733 lh = dwarf_decode_line_header (line_offset, cu);
3734 }
3735 if (lh == NULL)
3736 {
3737 lh_cu->v.quick->no_file_data = 1;
3738 return;
3739 }
3740
3741 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
3742 qfn->hash.dwo_unit = cu->dwo_unit;
3743 qfn->hash.line_sect_off = line_offset;
3744 gdb_assert (slot != NULL);
3745 *slot = qfn;
3746
3747 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3748
3749 int offset = 0;
3750 if (strcmp (fnd.name, "<unknown>") != 0)
3751 ++offset;
3752
3753 qfn->num_file_names = offset + lh->file_names_size ();
3754 qfn->file_names =
3755 XOBNEWVEC (&objfile->objfile_obstack, const char *, qfn->num_file_names);
3756 if (offset != 0)
3757 qfn->file_names[0] = xstrdup (fnd.name);
3758 for (int i = 0; i < lh->file_names_size (); ++i)
3759 qfn->file_names[i + offset] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
3760 qfn->real_names = NULL;
3761
3762 lh_cu->v.quick->file_names = qfn;
3763 }
3764
3765 /* A helper for the "quick" functions which attempts to read the line
3766 table for THIS_CU. */
3767
3768 static struct quick_file_names *
3769 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
3770 {
3771 /* This should never be called for TUs. */
3772 gdb_assert (! this_cu->is_debug_types);
3773 /* Nor type unit groups. */
3774 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
3775
3776 if (this_cu->v.quick->file_names != NULL)
3777 return this_cu->v.quick->file_names;
3778 /* If we know there is no line data, no point in looking again. */
3779 if (this_cu->v.quick->no_file_data)
3780 return NULL;
3781
3782 cutu_reader reader (this_cu);
3783 if (!reader.dummy_p)
3784 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die,
3785 reader.has_children);
3786
3787 if (this_cu->v.quick->no_file_data)
3788 return NULL;
3789 return this_cu->v.quick->file_names;
3790 }
3791
3792 /* A helper for the "quick" functions which computes and caches the
3793 real path for a given file name from the line table. */
3794
3795 static const char *
3796 dw2_get_real_path (struct objfile *objfile,
3797 struct quick_file_names *qfn, int index)
3798 {
3799 if (qfn->real_names == NULL)
3800 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
3801 qfn->num_file_names, const char *);
3802
3803 if (qfn->real_names[index] == NULL)
3804 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3805
3806 return qfn->real_names[index];
3807 }
3808
3809 static struct symtab *
3810 dw2_find_last_source_symtab (struct objfile *objfile)
3811 {
3812 struct dwarf2_per_objfile *dwarf2_per_objfile
3813 = get_dwarf2_per_objfile (objfile);
3814 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->all_comp_units.back ();
3815 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, false);
3816
3817 if (cust == NULL)
3818 return NULL;
3819
3820 return compunit_primary_filetab (cust);
3821 }
3822
3823 /* Traversal function for dw2_forget_cached_source_info. */
3824
3825 static int
3826 dw2_free_cached_file_names (void **slot, void *info)
3827 {
3828 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3829
3830 if (file_data->real_names)
3831 {
3832 int i;
3833
3834 for (i = 0; i < file_data->num_file_names; ++i)
3835 {
3836 xfree ((void*) file_data->real_names[i]);
3837 file_data->real_names[i] = NULL;
3838 }
3839 }
3840
3841 return 1;
3842 }
3843
3844 static void
3845 dw2_forget_cached_source_info (struct objfile *objfile)
3846 {
3847 struct dwarf2_per_objfile *dwarf2_per_objfile
3848 = get_dwarf2_per_objfile (objfile);
3849
3850 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3851 dw2_free_cached_file_names, NULL);
3852 }
3853
3854 /* Helper function for dw2_map_symtabs_matching_filename that expands
3855 the symtabs and calls the iterator. */
3856
3857 static int
3858 dw2_map_expand_apply (struct objfile *objfile,
3859 struct dwarf2_per_cu_data *per_cu,
3860 const char *name, const char *real_path,
3861 gdb::function_view<bool (symtab *)> callback)
3862 {
3863 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3864
3865 /* Don't visit already-expanded CUs. */
3866 if (per_cu->v.quick->compunit_symtab)
3867 return 0;
3868
3869 /* This may expand more than one symtab, and we want to iterate over
3870 all of them. */
3871 dw2_instantiate_symtab (per_cu, false);
3872
3873 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3874 last_made, callback);
3875 }
3876
3877 /* Implementation of the map_symtabs_matching_filename method. */
3878
3879 static bool
3880 dw2_map_symtabs_matching_filename
3881 (struct objfile *objfile, const char *name, const char *real_path,
3882 gdb::function_view<bool (symtab *)> callback)
3883 {
3884 const char *name_basename = lbasename (name);
3885 struct dwarf2_per_objfile *dwarf2_per_objfile
3886 = get_dwarf2_per_objfile (objfile);
3887
3888 /* The rule is CUs specify all the files, including those used by
3889 any TU, so there's no need to scan TUs here. */
3890
3891 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3892 {
3893 /* We only need to look at symtabs not already expanded. */
3894 if (per_cu->v.quick->compunit_symtab)
3895 continue;
3896
3897 quick_file_names *file_data = dw2_get_file_names (per_cu);
3898 if (file_data == NULL)
3899 continue;
3900
3901 for (int j = 0; j < file_data->num_file_names; ++j)
3902 {
3903 const char *this_name = file_data->file_names[j];
3904 const char *this_real_name;
3905
3906 if (compare_filenames_for_search (this_name, name))
3907 {
3908 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3909 callback))
3910 return true;
3911 continue;
3912 }
3913
3914 /* Before we invoke realpath, which can get expensive when many
3915 files are involved, do a quick comparison of the basenames. */
3916 if (! basenames_may_differ
3917 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3918 continue;
3919
3920 this_real_name = dw2_get_real_path (objfile, file_data, j);
3921 if (compare_filenames_for_search (this_real_name, name))
3922 {
3923 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3924 callback))
3925 return true;
3926 continue;
3927 }
3928
3929 if (real_path != NULL)
3930 {
3931 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3932 gdb_assert (IS_ABSOLUTE_PATH (name));
3933 if (this_real_name != NULL
3934 && FILENAME_CMP (real_path, this_real_name) == 0)
3935 {
3936 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3937 callback))
3938 return true;
3939 continue;
3940 }
3941 }
3942 }
3943 }
3944
3945 return false;
3946 }
3947
3948 /* Struct used to manage iterating over all CUs looking for a symbol. */
3949
3950 struct dw2_symtab_iterator
3951 {
3952 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3953 struct dwarf2_per_objfile *dwarf2_per_objfile;
3954 /* If set, only look for symbols that match that block. Valid values are
3955 GLOBAL_BLOCK and STATIC_BLOCK. */
3956 gdb::optional<block_enum> block_index;
3957 /* The kind of symbol we're looking for. */
3958 domain_enum domain;
3959 /* The list of CUs from the index entry of the symbol,
3960 or NULL if not found. */
3961 offset_type *vec;
3962 /* The next element in VEC to look at. */
3963 int next;
3964 /* The number of elements in VEC, or zero if there is no match. */
3965 int length;
3966 /* Have we seen a global version of the symbol?
3967 If so we can ignore all further global instances.
3968 This is to work around gold/15646, inefficient gold-generated
3969 indices. */
3970 int global_seen;
3971 };
3972
3973 /* Initialize the index symtab iterator ITER. */
3974
3975 static void
3976 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3977 struct dwarf2_per_objfile *dwarf2_per_objfile,
3978 gdb::optional<block_enum> block_index,
3979 domain_enum domain,
3980 const char *name)
3981 {
3982 iter->dwarf2_per_objfile = dwarf2_per_objfile;
3983 iter->block_index = block_index;
3984 iter->domain = domain;
3985 iter->next = 0;
3986 iter->global_seen = 0;
3987
3988 mapped_index *index = dwarf2_per_objfile->index_table.get ();
3989
3990 /* index is NULL if OBJF_READNOW. */
3991 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
3992 iter->length = MAYBE_SWAP (*iter->vec);
3993 else
3994 {
3995 iter->vec = NULL;
3996 iter->length = 0;
3997 }
3998 }
3999
4000 /* Return the next matching CU or NULL if there are no more. */
4001
4002 static struct dwarf2_per_cu_data *
4003 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
4004 {
4005 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
4006
4007 for ( ; iter->next < iter->length; ++iter->next)
4008 {
4009 offset_type cu_index_and_attrs =
4010 MAYBE_SWAP (iter->vec[iter->next + 1]);
4011 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4012 gdb_index_symbol_kind symbol_kind =
4013 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4014 /* Only check the symbol attributes if they're present.
4015 Indices prior to version 7 don't record them,
4016 and indices >= 7 may elide them for certain symbols
4017 (gold does this). */
4018 int attrs_valid =
4019 (dwarf2_per_objfile->index_table->version >= 7
4020 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4021
4022 /* Don't crash on bad data. */
4023 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
4024 + dwarf2_per_objfile->all_type_units.size ()))
4025 {
4026 complaint (_(".gdb_index entry has bad CU index"
4027 " [in module %s]"),
4028 objfile_name (dwarf2_per_objfile->objfile));
4029 continue;
4030 }
4031
4032 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
4033
4034 /* Skip if already read in. */
4035 if (per_cu->v.quick->compunit_symtab)
4036 continue;
4037
4038 /* Check static vs global. */
4039 if (attrs_valid)
4040 {
4041 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4042
4043 if (iter->block_index.has_value ())
4044 {
4045 bool want_static = *iter->block_index == STATIC_BLOCK;
4046
4047 if (is_static != want_static)
4048 continue;
4049 }
4050
4051 /* Work around gold/15646. */
4052 if (!is_static && iter->global_seen)
4053 continue;
4054 if (!is_static)
4055 iter->global_seen = 1;
4056 }
4057
4058 /* Only check the symbol's kind if it has one. */
4059 if (attrs_valid)
4060 {
4061 switch (iter->domain)
4062 {
4063 case VAR_DOMAIN:
4064 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
4065 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
4066 /* Some types are also in VAR_DOMAIN. */
4067 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4068 continue;
4069 break;
4070 case STRUCT_DOMAIN:
4071 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4072 continue;
4073 break;
4074 case LABEL_DOMAIN:
4075 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4076 continue;
4077 break;
4078 case MODULE_DOMAIN:
4079 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4080 continue;
4081 break;
4082 default:
4083 break;
4084 }
4085 }
4086
4087 ++iter->next;
4088 return per_cu;
4089 }
4090
4091 return NULL;
4092 }
4093
4094 static struct compunit_symtab *
4095 dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
4096 const char *name, domain_enum domain)
4097 {
4098 struct compunit_symtab *stab_best = NULL;
4099 struct dwarf2_per_objfile *dwarf2_per_objfile
4100 = get_dwarf2_per_objfile (objfile);
4101
4102 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
4103
4104 struct dw2_symtab_iterator iter;
4105 struct dwarf2_per_cu_data *per_cu;
4106
4107 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
4108
4109 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4110 {
4111 struct symbol *sym, *with_opaque = NULL;
4112 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
4113 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
4114 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
4115
4116 sym = block_find_symbol (block, name, domain,
4117 block_find_non_opaque_type_preferred,
4118 &with_opaque);
4119
4120 /* Some caution must be observed with overloaded functions
4121 and methods, since the index will not contain any overload
4122 information (but NAME might contain it). */
4123
4124 if (sym != NULL
4125 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
4126 return stab;
4127 if (with_opaque != NULL
4128 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
4129 stab_best = stab;
4130
4131 /* Keep looking through other CUs. */
4132 }
4133
4134 return stab_best;
4135 }
4136
4137 static void
4138 dw2_print_stats (struct objfile *objfile)
4139 {
4140 struct dwarf2_per_objfile *dwarf2_per_objfile
4141 = get_dwarf2_per_objfile (objfile);
4142 int total = (dwarf2_per_objfile->all_comp_units.size ()
4143 + dwarf2_per_objfile->all_type_units.size ());
4144 int count = 0;
4145
4146 for (int i = 0; i < total; ++i)
4147 {
4148 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
4149
4150 if (!per_cu->v.quick->compunit_symtab)
4151 ++count;
4152 }
4153 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
4154 printf_filtered (_(" Number of unread CUs: %d\n"), count);
4155 }
4156
4157 /* This dumps minimal information about the index.
4158 It is called via "mt print objfiles".
4159 One use is to verify .gdb_index has been loaded by the
4160 gdb.dwarf2/gdb-index.exp testcase. */
4161
4162 static void
4163 dw2_dump (struct objfile *objfile)
4164 {
4165 struct dwarf2_per_objfile *dwarf2_per_objfile
4166 = get_dwarf2_per_objfile (objfile);
4167
4168 gdb_assert (dwarf2_per_objfile->using_index);
4169 printf_filtered (".gdb_index:");
4170 if (dwarf2_per_objfile->index_table != NULL)
4171 {
4172 printf_filtered (" version %d\n",
4173 dwarf2_per_objfile->index_table->version);
4174 }
4175 else
4176 printf_filtered (" faked for \"readnow\"\n");
4177 printf_filtered ("\n");
4178 }
4179
4180 static void
4181 dw2_expand_symtabs_for_function (struct objfile *objfile,
4182 const char *func_name)
4183 {
4184 struct dwarf2_per_objfile *dwarf2_per_objfile
4185 = get_dwarf2_per_objfile (objfile);
4186
4187 struct dw2_symtab_iterator iter;
4188 struct dwarf2_per_cu_data *per_cu;
4189
4190 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
4191
4192 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4193 dw2_instantiate_symtab (per_cu, false);
4194
4195 }
4196
4197 static void
4198 dw2_expand_all_symtabs (struct objfile *objfile)
4199 {
4200 struct dwarf2_per_objfile *dwarf2_per_objfile
4201 = get_dwarf2_per_objfile (objfile);
4202 int total_units = (dwarf2_per_objfile->all_comp_units.size ()
4203 + dwarf2_per_objfile->all_type_units.size ());
4204
4205 for (int i = 0; i < total_units; ++i)
4206 {
4207 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
4208
4209 /* We don't want to directly expand a partial CU, because if we
4210 read it with the wrong language, then assertion failures can
4211 be triggered later on. See PR symtab/23010. So, tell
4212 dw2_instantiate_symtab to skip partial CUs -- any important
4213 partial CU will be read via DW_TAG_imported_unit anyway. */
4214 dw2_instantiate_symtab (per_cu, true);
4215 }
4216 }
4217
4218 static void
4219 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
4220 const char *fullname)
4221 {
4222 struct dwarf2_per_objfile *dwarf2_per_objfile
4223 = get_dwarf2_per_objfile (objfile);
4224
4225 /* We don't need to consider type units here.
4226 This is only called for examining code, e.g. expand_line_sal.
4227 There can be an order of magnitude (or more) more type units
4228 than comp units, and we avoid them if we can. */
4229
4230 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4231 {
4232 /* We only need to look at symtabs not already expanded. */
4233 if (per_cu->v.quick->compunit_symtab)
4234 continue;
4235
4236 quick_file_names *file_data = dw2_get_file_names (per_cu);
4237 if (file_data == NULL)
4238 continue;
4239
4240 for (int j = 0; j < file_data->num_file_names; ++j)
4241 {
4242 const char *this_fullname = file_data->file_names[j];
4243
4244 if (filename_cmp (this_fullname, fullname) == 0)
4245 {
4246 dw2_instantiate_symtab (per_cu, false);
4247 break;
4248 }
4249 }
4250 }
4251 }
4252
4253 static void
4254 dw2_map_matching_symbols
4255 (struct objfile *objfile,
4256 const lookup_name_info &name, domain_enum domain,
4257 int global,
4258 gdb::function_view<symbol_found_callback_ftype> callback,
4259 symbol_compare_ftype *ordered_compare)
4260 {
4261 /* Currently unimplemented; used for Ada. The function can be called if the
4262 current language is Ada for a non-Ada objfile using GNU index. As Ada
4263 does not look for non-Ada symbols this function should just return. */
4264 }
4265
4266 /* Starting from a search name, return the string that finds the upper
4267 bound of all strings that start with SEARCH_NAME in a sorted name
4268 list. Returns the empty string to indicate that the upper bound is
4269 the end of the list. */
4270
4271 static std::string
4272 make_sort_after_prefix_name (const char *search_name)
4273 {
4274 /* When looking to complete "func", we find the upper bound of all
4275 symbols that start with "func" by looking for where we'd insert
4276 the closest string that would follow "func" in lexicographical
4277 order. Usually, that's "func"-with-last-character-incremented,
4278 i.e. "fund". Mind non-ASCII characters, though. Usually those
4279 will be UTF-8 multi-byte sequences, but we can't be certain.
4280 Especially mind the 0xff character, which is a valid character in
4281 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
4282 rule out compilers allowing it in identifiers. Note that
4283 conveniently, strcmp/strcasecmp are specified to compare
4284 characters interpreted as unsigned char. So what we do is treat
4285 the whole string as a base 256 number composed of a sequence of
4286 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
4287 to 0, and carries 1 to the following more-significant position.
4288 If the very first character in SEARCH_NAME ends up incremented
4289 and carries/overflows, then the upper bound is the end of the
4290 list. The string after the empty string is also the empty
4291 string.
4292
4293 Some examples of this operation:
4294
4295 SEARCH_NAME => "+1" RESULT
4296
4297 "abc" => "abd"
4298 "ab\xff" => "ac"
4299 "\xff" "a" "\xff" => "\xff" "b"
4300 "\xff" => ""
4301 "\xff\xff" => ""
4302 "" => ""
4303
4304 Then, with these symbols for example:
4305
4306 func
4307 func1
4308 fund
4309
4310 completing "func" looks for symbols between "func" and
4311 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
4312 which finds "func" and "func1", but not "fund".
4313
4314 And with:
4315
4316 funcÿ (Latin1 'ÿ' [0xff])
4317 funcÿ1
4318 fund
4319
4320 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
4321 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
4322
4323 And with:
4324
4325 ÿÿ (Latin1 'ÿ' [0xff])
4326 ÿÿ1
4327
4328 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
4329 the end of the list.
4330 */
4331 std::string after = search_name;
4332 while (!after.empty () && (unsigned char) after.back () == 0xff)
4333 after.pop_back ();
4334 if (!after.empty ())
4335 after.back () = (unsigned char) after.back () + 1;
4336 return after;
4337 }
4338
4339 /* See declaration. */
4340
4341 std::pair<std::vector<name_component>::const_iterator,
4342 std::vector<name_component>::const_iterator>
4343 mapped_index_base::find_name_components_bounds
4344 (const lookup_name_info &lookup_name_without_params, language lang) const
4345 {
4346 auto *name_cmp
4347 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4348
4349 const char *lang_name
4350 = lookup_name_without_params.language_lookup_name (lang).c_str ();
4351
4352 /* Comparison function object for lower_bound that matches against a
4353 given symbol name. */
4354 auto lookup_compare_lower = [&] (const name_component &elem,
4355 const char *name)
4356 {
4357 const char *elem_qualified = this->symbol_name_at (elem.idx);
4358 const char *elem_name = elem_qualified + elem.name_offset;
4359 return name_cmp (elem_name, name) < 0;
4360 };
4361
4362 /* Comparison function object for upper_bound that matches against a
4363 given symbol name. */
4364 auto lookup_compare_upper = [&] (const char *name,
4365 const name_component &elem)
4366 {
4367 const char *elem_qualified = this->symbol_name_at (elem.idx);
4368 const char *elem_name = elem_qualified + elem.name_offset;
4369 return name_cmp (name, elem_name) < 0;
4370 };
4371
4372 auto begin = this->name_components.begin ();
4373 auto end = this->name_components.end ();
4374
4375 /* Find the lower bound. */
4376 auto lower = [&] ()
4377 {
4378 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
4379 return begin;
4380 else
4381 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
4382 } ();
4383
4384 /* Find the upper bound. */
4385 auto upper = [&] ()
4386 {
4387 if (lookup_name_without_params.completion_mode ())
4388 {
4389 /* In completion mode, we want UPPER to point past all
4390 symbols names that have the same prefix. I.e., with
4391 these symbols, and completing "func":
4392
4393 function << lower bound
4394 function1
4395 other_function << upper bound
4396
4397 We find the upper bound by looking for the insertion
4398 point of "func"-with-last-character-incremented,
4399 i.e. "fund". */
4400 std::string after = make_sort_after_prefix_name (lang_name);
4401 if (after.empty ())
4402 return end;
4403 return std::lower_bound (lower, end, after.c_str (),
4404 lookup_compare_lower);
4405 }
4406 else
4407 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
4408 } ();
4409
4410 return {lower, upper};
4411 }
4412
4413 /* See declaration. */
4414
4415 void
4416 mapped_index_base::build_name_components ()
4417 {
4418 if (!this->name_components.empty ())
4419 return;
4420
4421 this->name_components_casing = case_sensitivity;
4422 auto *name_cmp
4423 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4424
4425 /* The code below only knows how to break apart components of C++
4426 symbol names (and other languages that use '::' as
4427 namespace/module separator) and Ada symbol names. */
4428 auto count = this->symbol_name_count ();
4429 for (offset_type idx = 0; idx < count; idx++)
4430 {
4431 if (this->symbol_name_slot_invalid (idx))
4432 continue;
4433
4434 const char *name = this->symbol_name_at (idx);
4435
4436 /* Add each name component to the name component table. */
4437 unsigned int previous_len = 0;
4438
4439 if (strstr (name, "::") != nullptr)
4440 {
4441 for (unsigned int current_len = cp_find_first_component (name);
4442 name[current_len] != '\0';
4443 current_len += cp_find_first_component (name + current_len))
4444 {
4445 gdb_assert (name[current_len] == ':');
4446 this->name_components.push_back ({previous_len, idx});
4447 /* Skip the '::'. */
4448 current_len += 2;
4449 previous_len = current_len;
4450 }
4451 }
4452 else
4453 {
4454 /* Handle the Ada encoded (aka mangled) form here. */
4455 for (const char *iter = strstr (name, "__");
4456 iter != nullptr;
4457 iter = strstr (iter, "__"))
4458 {
4459 this->name_components.push_back ({previous_len, idx});
4460 iter += 2;
4461 previous_len = iter - name;
4462 }
4463 }
4464
4465 this->name_components.push_back ({previous_len, idx});
4466 }
4467
4468 /* Sort name_components elements by name. */
4469 auto name_comp_compare = [&] (const name_component &left,
4470 const name_component &right)
4471 {
4472 const char *left_qualified = this->symbol_name_at (left.idx);
4473 const char *right_qualified = this->symbol_name_at (right.idx);
4474
4475 const char *left_name = left_qualified + left.name_offset;
4476 const char *right_name = right_qualified + right.name_offset;
4477
4478 return name_cmp (left_name, right_name) < 0;
4479 };
4480
4481 std::sort (this->name_components.begin (),
4482 this->name_components.end (),
4483 name_comp_compare);
4484 }
4485
4486 /* Helper for dw2_expand_symtabs_matching that works with a
4487 mapped_index_base instead of the containing objfile. This is split
4488 to a separate function in order to be able to unit test the
4489 name_components matching using a mock mapped_index_base. For each
4490 symbol name that matches, calls MATCH_CALLBACK, passing it the
4491 symbol's index in the mapped_index_base symbol table. */
4492
4493 static void
4494 dw2_expand_symtabs_matching_symbol
4495 (mapped_index_base &index,
4496 const lookup_name_info &lookup_name_in,
4497 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4498 enum search_domain kind,
4499 gdb::function_view<bool (offset_type)> match_callback)
4500 {
4501 lookup_name_info lookup_name_without_params
4502 = lookup_name_in.make_ignore_params ();
4503
4504 /* Build the symbol name component sorted vector, if we haven't
4505 yet. */
4506 index.build_name_components ();
4507
4508 /* The same symbol may appear more than once in the range though.
4509 E.g., if we're looking for symbols that complete "w", and we have
4510 a symbol named "w1::w2", we'll find the two name components for
4511 that same symbol in the range. To be sure we only call the
4512 callback once per symbol, we first collect the symbol name
4513 indexes that matched in a temporary vector and ignore
4514 duplicates. */
4515 std::vector<offset_type> matches;
4516
4517 struct name_and_matcher
4518 {
4519 symbol_name_matcher_ftype *matcher;
4520 const std::string &name;
4521
4522 bool operator== (const name_and_matcher &other) const
4523 {
4524 return matcher == other.matcher && name == other.name;
4525 }
4526 };
4527
4528 /* A vector holding all the different symbol name matchers, for all
4529 languages. */
4530 std::vector<name_and_matcher> matchers;
4531
4532 for (int i = 0; i < nr_languages; i++)
4533 {
4534 enum language lang_e = (enum language) i;
4535
4536 const language_defn *lang = language_def (lang_e);
4537 symbol_name_matcher_ftype *name_matcher
4538 = get_symbol_name_matcher (lang, lookup_name_without_params);
4539
4540 name_and_matcher key {
4541 name_matcher,
4542 lookup_name_without_params.language_lookup_name (lang_e)
4543 };
4544
4545 /* Don't insert the same comparison routine more than once.
4546 Note that we do this linear walk. This is not a problem in
4547 practice because the number of supported languages is
4548 low. */
4549 if (std::find (matchers.begin (), matchers.end (), key)
4550 != matchers.end ())
4551 continue;
4552 matchers.push_back (std::move (key));
4553
4554 auto bounds
4555 = index.find_name_components_bounds (lookup_name_without_params,
4556 lang_e);
4557
4558 /* Now for each symbol name in range, check to see if we have a name
4559 match, and if so, call the MATCH_CALLBACK callback. */
4560
4561 for (; bounds.first != bounds.second; ++bounds.first)
4562 {
4563 const char *qualified = index.symbol_name_at (bounds.first->idx);
4564
4565 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4566 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4567 continue;
4568
4569 matches.push_back (bounds.first->idx);
4570 }
4571 }
4572
4573 std::sort (matches.begin (), matches.end ());
4574
4575 /* Finally call the callback, once per match. */
4576 ULONGEST prev = -1;
4577 for (offset_type idx : matches)
4578 {
4579 if (prev != idx)
4580 {
4581 if (!match_callback (idx))
4582 break;
4583 prev = idx;
4584 }
4585 }
4586
4587 /* Above we use a type wider than idx's for 'prev', since 0 and
4588 (offset_type)-1 are both possible values. */
4589 static_assert (sizeof (prev) > sizeof (offset_type), "");
4590 }
4591
4592 #if GDB_SELF_TEST
4593
4594 namespace selftests { namespace dw2_expand_symtabs_matching {
4595
4596 /* A mock .gdb_index/.debug_names-like name index table, enough to
4597 exercise dw2_expand_symtabs_matching_symbol, which works with the
4598 mapped_index_base interface. Builds an index from the symbol list
4599 passed as parameter to the constructor. */
4600 class mock_mapped_index : public mapped_index_base
4601 {
4602 public:
4603 mock_mapped_index (gdb::array_view<const char *> symbols)
4604 : m_symbol_table (symbols)
4605 {}
4606
4607 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
4608
4609 /* Return the number of names in the symbol table. */
4610 size_t symbol_name_count () const override
4611 {
4612 return m_symbol_table.size ();
4613 }
4614
4615 /* Get the name of the symbol at IDX in the symbol table. */
4616 const char *symbol_name_at (offset_type idx) const override
4617 {
4618 return m_symbol_table[idx];
4619 }
4620
4621 private:
4622 gdb::array_view<const char *> m_symbol_table;
4623 };
4624
4625 /* Convenience function that converts a NULL pointer to a "<null>"
4626 string, to pass to print routines. */
4627
4628 static const char *
4629 string_or_null (const char *str)
4630 {
4631 return str != NULL ? str : "<null>";
4632 }
4633
4634 /* Check if a lookup_name_info built from
4635 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4636 index. EXPECTED_LIST is the list of expected matches, in expected
4637 matching order. If no match expected, then an empty list is
4638 specified. Returns true on success. On failure prints a warning
4639 indicating the file:line that failed, and returns false. */
4640
4641 static bool
4642 check_match (const char *file, int line,
4643 mock_mapped_index &mock_index,
4644 const char *name, symbol_name_match_type match_type,
4645 bool completion_mode,
4646 std::initializer_list<const char *> expected_list)
4647 {
4648 lookup_name_info lookup_name (name, match_type, completion_mode);
4649
4650 bool matched = true;
4651
4652 auto mismatch = [&] (const char *expected_str,
4653 const char *got)
4654 {
4655 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4656 "expected=\"%s\", got=\"%s\"\n"),
4657 file, line,
4658 (match_type == symbol_name_match_type::FULL
4659 ? "FULL" : "WILD"),
4660 name, string_or_null (expected_str), string_or_null (got));
4661 matched = false;
4662 };
4663
4664 auto expected_it = expected_list.begin ();
4665 auto expected_end = expected_list.end ();
4666
4667 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4668 NULL, ALL_DOMAIN,
4669 [&] (offset_type idx)
4670 {
4671 const char *matched_name = mock_index.symbol_name_at (idx);
4672 const char *expected_str
4673 = expected_it == expected_end ? NULL : *expected_it++;
4674
4675 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4676 mismatch (expected_str, matched_name);
4677 return true;
4678 });
4679
4680 const char *expected_str
4681 = expected_it == expected_end ? NULL : *expected_it++;
4682 if (expected_str != NULL)
4683 mismatch (expected_str, NULL);
4684
4685 return matched;
4686 }
4687
4688 /* The symbols added to the mock mapped_index for testing (in
4689 canonical form). */
4690 static const char *test_symbols[] = {
4691 "function",
4692 "std::bar",
4693 "std::zfunction",
4694 "std::zfunction2",
4695 "w1::w2",
4696 "ns::foo<char*>",
4697 "ns::foo<int>",
4698 "ns::foo<long>",
4699 "ns2::tmpl<int>::foo2",
4700 "(anonymous namespace)::A::B::C",
4701
4702 /* These are used to check that the increment-last-char in the
4703 matching algorithm for completion doesn't match "t1_fund" when
4704 completing "t1_func". */
4705 "t1_func",
4706 "t1_func1",
4707 "t1_fund",
4708 "t1_fund1",
4709
4710 /* A UTF-8 name with multi-byte sequences to make sure that
4711 cp-name-parser understands this as a single identifier ("função"
4712 is "function" in PT). */
4713 u8"u8função",
4714
4715 /* \377 (0xff) is Latin1 'ÿ'. */
4716 "yfunc\377",
4717
4718 /* \377 (0xff) is Latin1 'ÿ'. */
4719 "\377",
4720 "\377\377123",
4721
4722 /* A name with all sorts of complications. Starts with "z" to make
4723 it easier for the completion tests below. */
4724 #define Z_SYM_NAME \
4725 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4726 "::tuple<(anonymous namespace)::ui*, " \
4727 "std::default_delete<(anonymous namespace)::ui>, void>"
4728
4729 Z_SYM_NAME
4730 };
4731
4732 /* Returns true if the mapped_index_base::find_name_component_bounds
4733 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4734 in completion mode. */
4735
4736 static bool
4737 check_find_bounds_finds (mapped_index_base &index,
4738 const char *search_name,
4739 gdb::array_view<const char *> expected_syms)
4740 {
4741 lookup_name_info lookup_name (search_name,
4742 symbol_name_match_type::FULL, true);
4743
4744 auto bounds = index.find_name_components_bounds (lookup_name,
4745 language_cplus);
4746
4747 size_t distance = std::distance (bounds.first, bounds.second);
4748 if (distance != expected_syms.size ())
4749 return false;
4750
4751 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4752 {
4753 auto nc_elem = bounds.first + exp_elem;
4754 const char *qualified = index.symbol_name_at (nc_elem->idx);
4755 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4756 return false;
4757 }
4758
4759 return true;
4760 }
4761
4762 /* Test the lower-level mapped_index::find_name_component_bounds
4763 method. */
4764
4765 static void
4766 test_mapped_index_find_name_component_bounds ()
4767 {
4768 mock_mapped_index mock_index (test_symbols);
4769
4770 mock_index.build_name_components ();
4771
4772 /* Test the lower-level mapped_index::find_name_component_bounds
4773 method in completion mode. */
4774 {
4775 static const char *expected_syms[] = {
4776 "t1_func",
4777 "t1_func1",
4778 };
4779
4780 SELF_CHECK (check_find_bounds_finds (mock_index,
4781 "t1_func", expected_syms));
4782 }
4783
4784 /* Check that the increment-last-char in the name matching algorithm
4785 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4786 {
4787 static const char *expected_syms1[] = {
4788 "\377",
4789 "\377\377123",
4790 };
4791 SELF_CHECK (check_find_bounds_finds (mock_index,
4792 "\377", expected_syms1));
4793
4794 static const char *expected_syms2[] = {
4795 "\377\377123",
4796 };
4797 SELF_CHECK (check_find_bounds_finds (mock_index,
4798 "\377\377", expected_syms2));
4799 }
4800 }
4801
4802 /* Test dw2_expand_symtabs_matching_symbol. */
4803
4804 static void
4805 test_dw2_expand_symtabs_matching_symbol ()
4806 {
4807 mock_mapped_index mock_index (test_symbols);
4808
4809 /* We let all tests run until the end even if some fails, for debug
4810 convenience. */
4811 bool any_mismatch = false;
4812
4813 /* Create the expected symbols list (an initializer_list). Needed
4814 because lists have commas, and we need to pass them to CHECK,
4815 which is a macro. */
4816 #define EXPECT(...) { __VA_ARGS__ }
4817
4818 /* Wrapper for check_match that passes down the current
4819 __FILE__/__LINE__. */
4820 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4821 any_mismatch |= !check_match (__FILE__, __LINE__, \
4822 mock_index, \
4823 NAME, MATCH_TYPE, COMPLETION_MODE, \
4824 EXPECTED_LIST)
4825
4826 /* Identity checks. */
4827 for (const char *sym : test_symbols)
4828 {
4829 /* Should be able to match all existing symbols. */
4830 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4831 EXPECT (sym));
4832
4833 /* Should be able to match all existing symbols with
4834 parameters. */
4835 std::string with_params = std::string (sym) + "(int)";
4836 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4837 EXPECT (sym));
4838
4839 /* Should be able to match all existing symbols with
4840 parameters and qualifiers. */
4841 with_params = std::string (sym) + " ( int ) const";
4842 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4843 EXPECT (sym));
4844
4845 /* This should really find sym, but cp-name-parser.y doesn't
4846 know about lvalue/rvalue qualifiers yet. */
4847 with_params = std::string (sym) + " ( int ) &&";
4848 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4849 {});
4850 }
4851
4852 /* Check that the name matching algorithm for completion doesn't get
4853 confused with Latin1 'ÿ' / 0xff. */
4854 {
4855 static const char str[] = "\377";
4856 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4857 EXPECT ("\377", "\377\377123"));
4858 }
4859
4860 /* Check that the increment-last-char in the matching algorithm for
4861 completion doesn't match "t1_fund" when completing "t1_func". */
4862 {
4863 static const char str[] = "t1_func";
4864 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4865 EXPECT ("t1_func", "t1_func1"));
4866 }
4867
4868 /* Check that completion mode works at each prefix of the expected
4869 symbol name. */
4870 {
4871 static const char str[] = "function(int)";
4872 size_t len = strlen (str);
4873 std::string lookup;
4874
4875 for (size_t i = 1; i < len; i++)
4876 {
4877 lookup.assign (str, i);
4878 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4879 EXPECT ("function"));
4880 }
4881 }
4882
4883 /* While "w" is a prefix of both components, the match function
4884 should still only be called once. */
4885 {
4886 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4887 EXPECT ("w1::w2"));
4888 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4889 EXPECT ("w1::w2"));
4890 }
4891
4892 /* Same, with a "complicated" symbol. */
4893 {
4894 static const char str[] = Z_SYM_NAME;
4895 size_t len = strlen (str);
4896 std::string lookup;
4897
4898 for (size_t i = 1; i < len; i++)
4899 {
4900 lookup.assign (str, i);
4901 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4902 EXPECT (Z_SYM_NAME));
4903 }
4904 }
4905
4906 /* In FULL mode, an incomplete symbol doesn't match. */
4907 {
4908 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4909 {});
4910 }
4911
4912 /* A complete symbol with parameters matches any overload, since the
4913 index has no overload info. */
4914 {
4915 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4916 EXPECT ("std::zfunction", "std::zfunction2"));
4917 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4918 EXPECT ("std::zfunction", "std::zfunction2"));
4919 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4920 EXPECT ("std::zfunction", "std::zfunction2"));
4921 }
4922
4923 /* Check that whitespace is ignored appropriately. A symbol with a
4924 template argument list. */
4925 {
4926 static const char expected[] = "ns::foo<int>";
4927 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4928 EXPECT (expected));
4929 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4930 EXPECT (expected));
4931 }
4932
4933 /* Check that whitespace is ignored appropriately. A symbol with a
4934 template argument list that includes a pointer. */
4935 {
4936 static const char expected[] = "ns::foo<char*>";
4937 /* Try both completion and non-completion modes. */
4938 static const bool completion_mode[2] = {false, true};
4939 for (size_t i = 0; i < 2; i++)
4940 {
4941 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4942 completion_mode[i], EXPECT (expected));
4943 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4944 completion_mode[i], EXPECT (expected));
4945
4946 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4947 completion_mode[i], EXPECT (expected));
4948 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4949 completion_mode[i], EXPECT (expected));
4950 }
4951 }
4952
4953 {
4954 /* Check method qualifiers are ignored. */
4955 static const char expected[] = "ns::foo<char*>";
4956 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4957 symbol_name_match_type::FULL, true, EXPECT (expected));
4958 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4959 symbol_name_match_type::FULL, true, EXPECT (expected));
4960 CHECK_MATCH ("foo < char * > ( int ) const",
4961 symbol_name_match_type::WILD, true, EXPECT (expected));
4962 CHECK_MATCH ("foo < char * > ( int ) &&",
4963 symbol_name_match_type::WILD, true, EXPECT (expected));
4964 }
4965
4966 /* Test lookup names that don't match anything. */
4967 {
4968 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4969 {});
4970
4971 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4972 {});
4973 }
4974
4975 /* Some wild matching tests, exercising "(anonymous namespace)",
4976 which should not be confused with a parameter list. */
4977 {
4978 static const char *syms[] = {
4979 "A::B::C",
4980 "B::C",
4981 "C",
4982 "A :: B :: C ( int )",
4983 "B :: C ( int )",
4984 "C ( int )",
4985 };
4986
4987 for (const char *s : syms)
4988 {
4989 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4990 EXPECT ("(anonymous namespace)::A::B::C"));
4991 }
4992 }
4993
4994 {
4995 static const char expected[] = "ns2::tmpl<int>::foo2";
4996 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4997 EXPECT (expected));
4998 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4999 EXPECT (expected));
5000 }
5001
5002 SELF_CHECK (!any_mismatch);
5003
5004 #undef EXPECT
5005 #undef CHECK_MATCH
5006 }
5007
5008 static void
5009 run_test ()
5010 {
5011 test_mapped_index_find_name_component_bounds ();
5012 test_dw2_expand_symtabs_matching_symbol ();
5013 }
5014
5015 }} // namespace selftests::dw2_expand_symtabs_matching
5016
5017 #endif /* GDB_SELF_TEST */
5018
5019 /* If FILE_MATCHER is NULL or if PER_CU has
5020 dwarf2_per_cu_quick_data::MARK set (see
5021 dw_expand_symtabs_matching_file_matcher), expand the CU and call
5022 EXPANSION_NOTIFY on it. */
5023
5024 static void
5025 dw2_expand_symtabs_matching_one
5026 (struct dwarf2_per_cu_data *per_cu,
5027 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5028 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
5029 {
5030 if (file_matcher == NULL || per_cu->v.quick->mark)
5031 {
5032 bool symtab_was_null
5033 = (per_cu->v.quick->compunit_symtab == NULL);
5034
5035 dw2_instantiate_symtab (per_cu, false);
5036
5037 if (expansion_notify != NULL
5038 && symtab_was_null
5039 && per_cu->v.quick->compunit_symtab != NULL)
5040 expansion_notify (per_cu->v.quick->compunit_symtab);
5041 }
5042 }
5043
5044 /* Helper for dw2_expand_matching symtabs. Called on each symbol
5045 matched, to expand corresponding CUs that were marked. IDX is the
5046 index of the symbol name that matched. */
5047
5048 static void
5049 dw2_expand_marked_cus
5050 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
5051 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5052 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5053 search_domain kind)
5054 {
5055 offset_type *vec, vec_len, vec_idx;
5056 bool global_seen = false;
5057 mapped_index &index = *dwarf2_per_objfile->index_table;
5058
5059 vec = (offset_type *) (index.constant_pool
5060 + MAYBE_SWAP (index.symbol_table[idx].vec));
5061 vec_len = MAYBE_SWAP (vec[0]);
5062 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
5063 {
5064 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
5065 /* This value is only valid for index versions >= 7. */
5066 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
5067 gdb_index_symbol_kind symbol_kind =
5068 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
5069 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
5070 /* Only check the symbol attributes if they're present.
5071 Indices prior to version 7 don't record them,
5072 and indices >= 7 may elide them for certain symbols
5073 (gold does this). */
5074 int attrs_valid =
5075 (index.version >= 7
5076 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
5077
5078 /* Work around gold/15646. */
5079 if (attrs_valid)
5080 {
5081 if (!is_static && global_seen)
5082 continue;
5083 if (!is_static)
5084 global_seen = true;
5085 }
5086
5087 /* Only check the symbol's kind if it has one. */
5088 if (attrs_valid)
5089 {
5090 switch (kind)
5091 {
5092 case VARIABLES_DOMAIN:
5093 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
5094 continue;
5095 break;
5096 case FUNCTIONS_DOMAIN:
5097 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
5098 continue;
5099 break;
5100 case TYPES_DOMAIN:
5101 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
5102 continue;
5103 break;
5104 case MODULES_DOMAIN:
5105 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
5106 continue;
5107 break;
5108 default:
5109 break;
5110 }
5111 }
5112
5113 /* Don't crash on bad data. */
5114 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
5115 + dwarf2_per_objfile->all_type_units.size ()))
5116 {
5117 complaint (_(".gdb_index entry has bad CU index"
5118 " [in module %s]"),
5119 objfile_name (dwarf2_per_objfile->objfile));
5120 continue;
5121 }
5122
5123 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
5124 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5125 expansion_notify);
5126 }
5127 }
5128
5129 /* If FILE_MATCHER is non-NULL, set all the
5130 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
5131 that match FILE_MATCHER. */
5132
5133 static void
5134 dw_expand_symtabs_matching_file_matcher
5135 (struct dwarf2_per_objfile *dwarf2_per_objfile,
5136 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
5137 {
5138 if (file_matcher == NULL)
5139 return;
5140
5141 objfile *const objfile = dwarf2_per_objfile->objfile;
5142
5143 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
5144 htab_eq_pointer,
5145 NULL, xcalloc, xfree));
5146 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
5147 htab_eq_pointer,
5148 NULL, xcalloc, xfree));
5149
5150 /* The rule is CUs specify all the files, including those used by
5151 any TU, so there's no need to scan TUs here. */
5152
5153 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5154 {
5155 QUIT;
5156
5157 per_cu->v.quick->mark = 0;
5158
5159 /* We only need to look at symtabs not already expanded. */
5160 if (per_cu->v.quick->compunit_symtab)
5161 continue;
5162
5163 quick_file_names *file_data = dw2_get_file_names (per_cu);
5164 if (file_data == NULL)
5165 continue;
5166
5167 if (htab_find (visited_not_found.get (), file_data) != NULL)
5168 continue;
5169 else if (htab_find (visited_found.get (), file_data) != NULL)
5170 {
5171 per_cu->v.quick->mark = 1;
5172 continue;
5173 }
5174
5175 for (int j = 0; j < file_data->num_file_names; ++j)
5176 {
5177 const char *this_real_name;
5178
5179 if (file_matcher (file_data->file_names[j], false))
5180 {
5181 per_cu->v.quick->mark = 1;
5182 break;
5183 }
5184
5185 /* Before we invoke realpath, which can get expensive when many
5186 files are involved, do a quick comparison of the basenames. */
5187 if (!basenames_may_differ
5188 && !file_matcher (lbasename (file_data->file_names[j]),
5189 true))
5190 continue;
5191
5192 this_real_name = dw2_get_real_path (objfile, file_data, j);
5193 if (file_matcher (this_real_name, false))
5194 {
5195 per_cu->v.quick->mark = 1;
5196 break;
5197 }
5198 }
5199
5200 void **slot = htab_find_slot (per_cu->v.quick->mark
5201 ? visited_found.get ()
5202 : visited_not_found.get (),
5203 file_data, INSERT);
5204 *slot = file_data;
5205 }
5206 }
5207
5208 static void
5209 dw2_expand_symtabs_matching
5210 (struct objfile *objfile,
5211 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5212 const lookup_name_info &lookup_name,
5213 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5214 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5215 enum search_domain kind)
5216 {
5217 struct dwarf2_per_objfile *dwarf2_per_objfile
5218 = get_dwarf2_per_objfile (objfile);
5219
5220 /* index_table is NULL if OBJF_READNOW. */
5221 if (!dwarf2_per_objfile->index_table)
5222 return;
5223
5224 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
5225
5226 mapped_index &index = *dwarf2_per_objfile->index_table;
5227
5228 dw2_expand_symtabs_matching_symbol (index, lookup_name,
5229 symbol_matcher,
5230 kind, [&] (offset_type idx)
5231 {
5232 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
5233 expansion_notify, kind);
5234 return true;
5235 });
5236 }
5237
5238 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
5239 symtab. */
5240
5241 static struct compunit_symtab *
5242 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
5243 CORE_ADDR pc)
5244 {
5245 int i;
5246
5247 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
5248 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
5249 return cust;
5250
5251 if (cust->includes == NULL)
5252 return NULL;
5253
5254 for (i = 0; cust->includes[i]; ++i)
5255 {
5256 struct compunit_symtab *s = cust->includes[i];
5257
5258 s = recursively_find_pc_sect_compunit_symtab (s, pc);
5259 if (s != NULL)
5260 return s;
5261 }
5262
5263 return NULL;
5264 }
5265
5266 static struct compunit_symtab *
5267 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
5268 struct bound_minimal_symbol msymbol,
5269 CORE_ADDR pc,
5270 struct obj_section *section,
5271 int warn_if_readin)
5272 {
5273 struct dwarf2_per_cu_data *data;
5274 struct compunit_symtab *result;
5275
5276 if (!objfile->partial_symtabs->psymtabs_addrmap)
5277 return NULL;
5278
5279 CORE_ADDR baseaddr = objfile->text_section_offset ();
5280 data = (struct dwarf2_per_cu_data *) addrmap_find
5281 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
5282 if (!data)
5283 return NULL;
5284
5285 if (warn_if_readin && data->v.quick->compunit_symtab)
5286 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
5287 paddress (get_objfile_arch (objfile), pc));
5288
5289 result
5290 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data,
5291 false),
5292 pc);
5293 gdb_assert (result != NULL);
5294 return result;
5295 }
5296
5297 static void
5298 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
5299 void *data, int need_fullname)
5300 {
5301 struct dwarf2_per_objfile *dwarf2_per_objfile
5302 = get_dwarf2_per_objfile (objfile);
5303
5304 if (!dwarf2_per_objfile->filenames_cache)
5305 {
5306 dwarf2_per_objfile->filenames_cache.emplace ();
5307
5308 htab_up visited (htab_create_alloc (10,
5309 htab_hash_pointer, htab_eq_pointer,
5310 NULL, xcalloc, xfree));
5311
5312 /* The rule is CUs specify all the files, including those used
5313 by any TU, so there's no need to scan TUs here. We can
5314 ignore file names coming from already-expanded CUs. */
5315
5316 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5317 {
5318 if (per_cu->v.quick->compunit_symtab)
5319 {
5320 void **slot = htab_find_slot (visited.get (),
5321 per_cu->v.quick->file_names,
5322 INSERT);
5323
5324 *slot = per_cu->v.quick->file_names;
5325 }
5326 }
5327
5328 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5329 {
5330 /* We only need to look at symtabs not already expanded. */
5331 if (per_cu->v.quick->compunit_symtab)
5332 continue;
5333
5334 quick_file_names *file_data = dw2_get_file_names (per_cu);
5335 if (file_data == NULL)
5336 continue;
5337
5338 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
5339 if (*slot)
5340 {
5341 /* Already visited. */
5342 continue;
5343 }
5344 *slot = file_data;
5345
5346 for (int j = 0; j < file_data->num_file_names; ++j)
5347 {
5348 const char *filename = file_data->file_names[j];
5349 dwarf2_per_objfile->filenames_cache->seen (filename);
5350 }
5351 }
5352 }
5353
5354 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
5355 {
5356 gdb::unique_xmalloc_ptr<char> this_real_name;
5357
5358 if (need_fullname)
5359 this_real_name = gdb_realpath (filename);
5360 (*fun) (filename, this_real_name.get (), data);
5361 });
5362 }
5363
5364 static int
5365 dw2_has_symbols (struct objfile *objfile)
5366 {
5367 return 1;
5368 }
5369
5370 const struct quick_symbol_functions dwarf2_gdb_index_functions =
5371 {
5372 dw2_has_symbols,
5373 dw2_find_last_source_symtab,
5374 dw2_forget_cached_source_info,
5375 dw2_map_symtabs_matching_filename,
5376 dw2_lookup_symbol,
5377 dw2_print_stats,
5378 dw2_dump,
5379 dw2_expand_symtabs_for_function,
5380 dw2_expand_all_symtabs,
5381 dw2_expand_symtabs_with_fullname,
5382 dw2_map_matching_symbols,
5383 dw2_expand_symtabs_matching,
5384 dw2_find_pc_sect_compunit_symtab,
5385 NULL,
5386 dw2_map_symbol_filenames
5387 };
5388
5389 /* DWARF-5 debug_names reader. */
5390
5391 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5392 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5393
5394 /* A helper function that reads the .debug_names section in SECTION
5395 and fills in MAP. FILENAME is the name of the file containing the
5396 section; it is used for error reporting.
5397
5398 Returns true if all went well, false otherwise. */
5399
5400 static bool
5401 read_debug_names_from_section (struct objfile *objfile,
5402 const char *filename,
5403 struct dwarf2_section_info *section,
5404 mapped_debug_names &map)
5405 {
5406 if (dwarf2_section_empty_p (section))
5407 return false;
5408
5409 /* Older elfutils strip versions could keep the section in the main
5410 executable while splitting it for the separate debug info file. */
5411 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
5412 return false;
5413
5414 dwarf2_read_section (objfile, section);
5415
5416 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
5417
5418 const gdb_byte *addr = section->buffer;
5419
5420 bfd *const abfd = get_section_bfd_owner (section);
5421
5422 unsigned int bytes_read;
5423 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5424 addr += bytes_read;
5425
5426 map.dwarf5_is_dwarf64 = bytes_read != 4;
5427 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5428 if (bytes_read + length != section->size)
5429 {
5430 /* There may be multiple per-CU indices. */
5431 warning (_("Section .debug_names in %s length %s does not match "
5432 "section length %s, ignoring .debug_names."),
5433 filename, plongest (bytes_read + length),
5434 pulongest (section->size));
5435 return false;
5436 }
5437
5438 /* The version number. */
5439 uint16_t version = read_2_bytes (abfd, addr);
5440 addr += 2;
5441 if (version != 5)
5442 {
5443 warning (_("Section .debug_names in %s has unsupported version %d, "
5444 "ignoring .debug_names."),
5445 filename, version);
5446 return false;
5447 }
5448
5449 /* Padding. */
5450 uint16_t padding = read_2_bytes (abfd, addr);
5451 addr += 2;
5452 if (padding != 0)
5453 {
5454 warning (_("Section .debug_names in %s has unsupported padding %d, "
5455 "ignoring .debug_names."),
5456 filename, padding);
5457 return false;
5458 }
5459
5460 /* comp_unit_count - The number of CUs in the CU list. */
5461 map.cu_count = read_4_bytes (abfd, addr);
5462 addr += 4;
5463
5464 /* local_type_unit_count - The number of TUs in the local TU
5465 list. */
5466 map.tu_count = read_4_bytes (abfd, addr);
5467 addr += 4;
5468
5469 /* foreign_type_unit_count - The number of TUs in the foreign TU
5470 list. */
5471 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5472 addr += 4;
5473 if (foreign_tu_count != 0)
5474 {
5475 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5476 "ignoring .debug_names."),
5477 filename, static_cast<unsigned long> (foreign_tu_count));
5478 return false;
5479 }
5480
5481 /* bucket_count - The number of hash buckets in the hash lookup
5482 table. */
5483 map.bucket_count = read_4_bytes (abfd, addr);
5484 addr += 4;
5485
5486 /* name_count - The number of unique names in the index. */
5487 map.name_count = read_4_bytes (abfd, addr);
5488 addr += 4;
5489
5490 /* abbrev_table_size - The size in bytes of the abbreviations
5491 table. */
5492 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5493 addr += 4;
5494
5495 /* augmentation_string_size - The size in bytes of the augmentation
5496 string. This value is rounded up to a multiple of 4. */
5497 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5498 addr += 4;
5499 map.augmentation_is_gdb = ((augmentation_string_size
5500 == sizeof (dwarf5_augmentation))
5501 && memcmp (addr, dwarf5_augmentation,
5502 sizeof (dwarf5_augmentation)) == 0);
5503 augmentation_string_size += (-augmentation_string_size) & 3;
5504 addr += augmentation_string_size;
5505
5506 /* List of CUs */
5507 map.cu_table_reordered = addr;
5508 addr += map.cu_count * map.offset_size;
5509
5510 /* List of Local TUs */
5511 map.tu_table_reordered = addr;
5512 addr += map.tu_count * map.offset_size;
5513
5514 /* Hash Lookup Table */
5515 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5516 addr += map.bucket_count * 4;
5517 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5518 addr += map.name_count * 4;
5519
5520 /* Name Table */
5521 map.name_table_string_offs_reordered = addr;
5522 addr += map.name_count * map.offset_size;
5523 map.name_table_entry_offs_reordered = addr;
5524 addr += map.name_count * map.offset_size;
5525
5526 const gdb_byte *abbrev_table_start = addr;
5527 for (;;)
5528 {
5529 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5530 addr += bytes_read;
5531 if (index_num == 0)
5532 break;
5533
5534 const auto insertpair
5535 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5536 if (!insertpair.second)
5537 {
5538 warning (_("Section .debug_names in %s has duplicate index %s, "
5539 "ignoring .debug_names."),
5540 filename, pulongest (index_num));
5541 return false;
5542 }
5543 mapped_debug_names::index_val &indexval = insertpair.first->second;
5544 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5545 addr += bytes_read;
5546
5547 for (;;)
5548 {
5549 mapped_debug_names::index_val::attr attr;
5550 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5551 addr += bytes_read;
5552 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5553 addr += bytes_read;
5554 if (attr.form == DW_FORM_implicit_const)
5555 {
5556 attr.implicit_const = read_signed_leb128 (abfd, addr,
5557 &bytes_read);
5558 addr += bytes_read;
5559 }
5560 if (attr.dw_idx == 0 && attr.form == 0)
5561 break;
5562 indexval.attr_vec.push_back (std::move (attr));
5563 }
5564 }
5565 if (addr != abbrev_table_start + abbrev_table_size)
5566 {
5567 warning (_("Section .debug_names in %s has abbreviation_table "
5568 "of size %s vs. written as %u, ignoring .debug_names."),
5569 filename, plongest (addr - abbrev_table_start),
5570 abbrev_table_size);
5571 return false;
5572 }
5573 map.entry_pool = addr;
5574
5575 return true;
5576 }
5577
5578 /* A helper for create_cus_from_debug_names that handles the MAP's CU
5579 list. */
5580
5581 static void
5582 create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
5583 const mapped_debug_names &map,
5584 dwarf2_section_info &section,
5585 bool is_dwz)
5586 {
5587 sect_offset sect_off_prev;
5588 for (uint32_t i = 0; i <= map.cu_count; ++i)
5589 {
5590 sect_offset sect_off_next;
5591 if (i < map.cu_count)
5592 {
5593 sect_off_next
5594 = (sect_offset) (extract_unsigned_integer
5595 (map.cu_table_reordered + i * map.offset_size,
5596 map.offset_size,
5597 map.dwarf5_byte_order));
5598 }
5599 else
5600 sect_off_next = (sect_offset) section.size;
5601 if (i >= 1)
5602 {
5603 const ULONGEST length = sect_off_next - sect_off_prev;
5604 dwarf2_per_cu_data *per_cu
5605 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
5606 sect_off_prev, length);
5607 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
5608 }
5609 sect_off_prev = sect_off_next;
5610 }
5611 }
5612
5613 /* Read the CU list from the mapped index, and use it to create all
5614 the CU objects for this dwarf2_per_objfile. */
5615
5616 static void
5617 create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
5618 const mapped_debug_names &map,
5619 const mapped_debug_names &dwz_map)
5620 {
5621 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
5622 dwarf2_per_objfile->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
5623
5624 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5625 dwarf2_per_objfile->info,
5626 false /* is_dwz */);
5627
5628 if (dwz_map.cu_count == 0)
5629 return;
5630
5631 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5632 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
5633 true /* is_dwz */);
5634 }
5635
5636 /* Read .debug_names. If everything went ok, initialize the "quick"
5637 elements of all the CUs and return true. Otherwise, return false. */
5638
5639 static bool
5640 dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
5641 {
5642 std::unique_ptr<mapped_debug_names> map
5643 (new mapped_debug_names (dwarf2_per_objfile));
5644 mapped_debug_names dwz_map (dwarf2_per_objfile);
5645 struct objfile *objfile = dwarf2_per_objfile->objfile;
5646
5647 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5648 &dwarf2_per_objfile->debug_names,
5649 *map))
5650 return false;
5651
5652 /* Don't use the index if it's empty. */
5653 if (map->name_count == 0)
5654 return false;
5655
5656 /* If there is a .dwz file, read it so we can get its CU list as
5657 well. */
5658 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5659 if (dwz != NULL)
5660 {
5661 if (!read_debug_names_from_section (objfile,
5662 bfd_get_filename (dwz->dwz_bfd.get ()),
5663 &dwz->debug_names, dwz_map))
5664 {
5665 warning (_("could not read '.debug_names' section from %s; skipping"),
5666 bfd_get_filename (dwz->dwz_bfd.get ()));
5667 return false;
5668 }
5669 }
5670
5671 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
5672
5673 if (map->tu_count != 0)
5674 {
5675 /* We can only handle a single .debug_types when we have an
5676 index. */
5677 if (dwarf2_per_objfile->types.size () != 1)
5678 return false;
5679
5680 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
5681
5682 create_signatured_type_table_from_debug_names
5683 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->abbrev);
5684 }
5685
5686 create_addrmap_from_aranges (dwarf2_per_objfile,
5687 &dwarf2_per_objfile->debug_aranges);
5688
5689 dwarf2_per_objfile->debug_names_table = std::move (map);
5690 dwarf2_per_objfile->using_index = 1;
5691 dwarf2_per_objfile->quick_file_names_table =
5692 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
5693
5694 return true;
5695 }
5696
5697 /* Type used to manage iterating over all CUs looking for a symbol for
5698 .debug_names. */
5699
5700 class dw2_debug_names_iterator
5701 {
5702 public:
5703 dw2_debug_names_iterator (const mapped_debug_names &map,
5704 gdb::optional<block_enum> block_index,
5705 domain_enum domain,
5706 const char *name)
5707 : m_map (map), m_block_index (block_index), m_domain (domain),
5708 m_addr (find_vec_in_debug_names (map, name))
5709 {}
5710
5711 dw2_debug_names_iterator (const mapped_debug_names &map,
5712 search_domain search, uint32_t namei)
5713 : m_map (map),
5714 m_search (search),
5715 m_addr (find_vec_in_debug_names (map, namei))
5716 {}
5717
5718 dw2_debug_names_iterator (const mapped_debug_names &map,
5719 block_enum block_index, domain_enum domain,
5720 uint32_t namei)
5721 : m_map (map), m_block_index (block_index), m_domain (domain),
5722 m_addr (find_vec_in_debug_names (map, namei))
5723 {}
5724
5725 /* Return the next matching CU or NULL if there are no more. */
5726 dwarf2_per_cu_data *next ();
5727
5728 private:
5729 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5730 const char *name);
5731 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5732 uint32_t namei);
5733
5734 /* The internalized form of .debug_names. */
5735 const mapped_debug_names &m_map;
5736
5737 /* If set, only look for symbols that match that block. Valid values are
5738 GLOBAL_BLOCK and STATIC_BLOCK. */
5739 const gdb::optional<block_enum> m_block_index;
5740
5741 /* The kind of symbol we're looking for. */
5742 const domain_enum m_domain = UNDEF_DOMAIN;
5743 const search_domain m_search = ALL_DOMAIN;
5744
5745 /* The list of CUs from the index entry of the symbol, or NULL if
5746 not found. */
5747 const gdb_byte *m_addr;
5748 };
5749
5750 const char *
5751 mapped_debug_names::namei_to_name (uint32_t namei) const
5752 {
5753 const ULONGEST namei_string_offs
5754 = extract_unsigned_integer ((name_table_string_offs_reordered
5755 + namei * offset_size),
5756 offset_size,
5757 dwarf5_byte_order);
5758 return read_indirect_string_at_offset
5759 (dwarf2_per_objfile, dwarf2_per_objfile->objfile->obfd, namei_string_offs);
5760 }
5761
5762 /* Find a slot in .debug_names for the object named NAME. If NAME is
5763 found, return pointer to its pool data. If NAME cannot be found,
5764 return NULL. */
5765
5766 const gdb_byte *
5767 dw2_debug_names_iterator::find_vec_in_debug_names
5768 (const mapped_debug_names &map, const char *name)
5769 {
5770 int (*cmp) (const char *, const char *);
5771
5772 gdb::unique_xmalloc_ptr<char> without_params;
5773 if (current_language->la_language == language_cplus
5774 || current_language->la_language == language_fortran
5775 || current_language->la_language == language_d)
5776 {
5777 /* NAME is already canonical. Drop any qualifiers as
5778 .debug_names does not contain any. */
5779
5780 if (strchr (name, '(') != NULL)
5781 {
5782 without_params = cp_remove_params (name);
5783 if (without_params != NULL)
5784 name = without_params.get ();
5785 }
5786 }
5787
5788 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5789
5790 const uint32_t full_hash = dwarf5_djb_hash (name);
5791 uint32_t namei
5792 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5793 (map.bucket_table_reordered
5794 + (full_hash % map.bucket_count)), 4,
5795 map.dwarf5_byte_order);
5796 if (namei == 0)
5797 return NULL;
5798 --namei;
5799 if (namei >= map.name_count)
5800 {
5801 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5802 "[in module %s]"),
5803 namei, map.name_count,
5804 objfile_name (map.dwarf2_per_objfile->objfile));
5805 return NULL;
5806 }
5807
5808 for (;;)
5809 {
5810 const uint32_t namei_full_hash
5811 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5812 (map.hash_table_reordered + namei), 4,
5813 map.dwarf5_byte_order);
5814 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5815 return NULL;
5816
5817 if (full_hash == namei_full_hash)
5818 {
5819 const char *const namei_string = map.namei_to_name (namei);
5820
5821 #if 0 /* An expensive sanity check. */
5822 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5823 {
5824 complaint (_("Wrong .debug_names hash for string at index %u "
5825 "[in module %s]"),
5826 namei, objfile_name (dwarf2_per_objfile->objfile));
5827 return NULL;
5828 }
5829 #endif
5830
5831 if (cmp (namei_string, name) == 0)
5832 {
5833 const ULONGEST namei_entry_offs
5834 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5835 + namei * map.offset_size),
5836 map.offset_size, map.dwarf5_byte_order);
5837 return map.entry_pool + namei_entry_offs;
5838 }
5839 }
5840
5841 ++namei;
5842 if (namei >= map.name_count)
5843 return NULL;
5844 }
5845 }
5846
5847 const gdb_byte *
5848 dw2_debug_names_iterator::find_vec_in_debug_names
5849 (const mapped_debug_names &map, uint32_t namei)
5850 {
5851 if (namei >= map.name_count)
5852 {
5853 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5854 "[in module %s]"),
5855 namei, map.name_count,
5856 objfile_name (map.dwarf2_per_objfile->objfile));
5857 return NULL;
5858 }
5859
5860 const ULONGEST namei_entry_offs
5861 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5862 + namei * map.offset_size),
5863 map.offset_size, map.dwarf5_byte_order);
5864 return map.entry_pool + namei_entry_offs;
5865 }
5866
5867 /* See dw2_debug_names_iterator. */
5868
5869 dwarf2_per_cu_data *
5870 dw2_debug_names_iterator::next ()
5871 {
5872 if (m_addr == NULL)
5873 return NULL;
5874
5875 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5876 struct objfile *objfile = dwarf2_per_objfile->objfile;
5877 bfd *const abfd = objfile->obfd;
5878
5879 again:
5880
5881 unsigned int bytes_read;
5882 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5883 m_addr += bytes_read;
5884 if (abbrev == 0)
5885 return NULL;
5886
5887 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5888 if (indexval_it == m_map.abbrev_map.cend ())
5889 {
5890 complaint (_("Wrong .debug_names undefined abbrev code %s "
5891 "[in module %s]"),
5892 pulongest (abbrev), objfile_name (objfile));
5893 return NULL;
5894 }
5895 const mapped_debug_names::index_val &indexval = indexval_it->second;
5896 enum class symbol_linkage {
5897 unknown,
5898 static_,
5899 extern_,
5900 } symbol_linkage_ = symbol_linkage::unknown;
5901 dwarf2_per_cu_data *per_cu = NULL;
5902 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5903 {
5904 ULONGEST ull;
5905 switch (attr.form)
5906 {
5907 case DW_FORM_implicit_const:
5908 ull = attr.implicit_const;
5909 break;
5910 case DW_FORM_flag_present:
5911 ull = 1;
5912 break;
5913 case DW_FORM_udata:
5914 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5915 m_addr += bytes_read;
5916 break;
5917 default:
5918 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5919 dwarf_form_name (attr.form),
5920 objfile_name (objfile));
5921 return NULL;
5922 }
5923 switch (attr.dw_idx)
5924 {
5925 case DW_IDX_compile_unit:
5926 /* Don't crash on bad data. */
5927 if (ull >= dwarf2_per_objfile->all_comp_units.size ())
5928 {
5929 complaint (_(".debug_names entry has bad CU index %s"
5930 " [in module %s]"),
5931 pulongest (ull),
5932 objfile_name (dwarf2_per_objfile->objfile));
5933 continue;
5934 }
5935 per_cu = dwarf2_per_objfile->get_cutu (ull);
5936 break;
5937 case DW_IDX_type_unit:
5938 /* Don't crash on bad data. */
5939 if (ull >= dwarf2_per_objfile->all_type_units.size ())
5940 {
5941 complaint (_(".debug_names entry has bad TU index %s"
5942 " [in module %s]"),
5943 pulongest (ull),
5944 objfile_name (dwarf2_per_objfile->objfile));
5945 continue;
5946 }
5947 per_cu = &dwarf2_per_objfile->get_tu (ull)->per_cu;
5948 break;
5949 case DW_IDX_GNU_internal:
5950 if (!m_map.augmentation_is_gdb)
5951 break;
5952 symbol_linkage_ = symbol_linkage::static_;
5953 break;
5954 case DW_IDX_GNU_external:
5955 if (!m_map.augmentation_is_gdb)
5956 break;
5957 symbol_linkage_ = symbol_linkage::extern_;
5958 break;
5959 }
5960 }
5961
5962 /* Skip if already read in. */
5963 if (per_cu->v.quick->compunit_symtab)
5964 goto again;
5965
5966 /* Check static vs global. */
5967 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
5968 {
5969 const bool want_static = *m_block_index == STATIC_BLOCK;
5970 const bool symbol_is_static =
5971 symbol_linkage_ == symbol_linkage::static_;
5972 if (want_static != symbol_is_static)
5973 goto again;
5974 }
5975
5976 /* Match dw2_symtab_iter_next, symbol_kind
5977 and debug_names::psymbol_tag. */
5978 switch (m_domain)
5979 {
5980 case VAR_DOMAIN:
5981 switch (indexval.dwarf_tag)
5982 {
5983 case DW_TAG_variable:
5984 case DW_TAG_subprogram:
5985 /* Some types are also in VAR_DOMAIN. */
5986 case DW_TAG_typedef:
5987 case DW_TAG_structure_type:
5988 break;
5989 default:
5990 goto again;
5991 }
5992 break;
5993 case STRUCT_DOMAIN:
5994 switch (indexval.dwarf_tag)
5995 {
5996 case DW_TAG_typedef:
5997 case DW_TAG_structure_type:
5998 break;
5999 default:
6000 goto again;
6001 }
6002 break;
6003 case LABEL_DOMAIN:
6004 switch (indexval.dwarf_tag)
6005 {
6006 case 0:
6007 case DW_TAG_variable:
6008 break;
6009 default:
6010 goto again;
6011 }
6012 break;
6013 case MODULE_DOMAIN:
6014 switch (indexval.dwarf_tag)
6015 {
6016 case DW_TAG_module:
6017 break;
6018 default:
6019 goto again;
6020 }
6021 break;
6022 default:
6023 break;
6024 }
6025
6026 /* Match dw2_expand_symtabs_matching, symbol_kind and
6027 debug_names::psymbol_tag. */
6028 switch (m_search)
6029 {
6030 case VARIABLES_DOMAIN:
6031 switch (indexval.dwarf_tag)
6032 {
6033 case DW_TAG_variable:
6034 break;
6035 default:
6036 goto again;
6037 }
6038 break;
6039 case FUNCTIONS_DOMAIN:
6040 switch (indexval.dwarf_tag)
6041 {
6042 case DW_TAG_subprogram:
6043 break;
6044 default:
6045 goto again;
6046 }
6047 break;
6048 case TYPES_DOMAIN:
6049 switch (indexval.dwarf_tag)
6050 {
6051 case DW_TAG_typedef:
6052 case DW_TAG_structure_type:
6053 break;
6054 default:
6055 goto again;
6056 }
6057 break;
6058 case MODULES_DOMAIN:
6059 switch (indexval.dwarf_tag)
6060 {
6061 case DW_TAG_module:
6062 break;
6063 default:
6064 goto again;
6065 }
6066 default:
6067 break;
6068 }
6069
6070 return per_cu;
6071 }
6072
6073 static struct compunit_symtab *
6074 dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
6075 const char *name, domain_enum domain)
6076 {
6077 struct dwarf2_per_objfile *dwarf2_per_objfile
6078 = get_dwarf2_per_objfile (objfile);
6079
6080 const auto &mapp = dwarf2_per_objfile->debug_names_table;
6081 if (!mapp)
6082 {
6083 /* index is NULL if OBJF_READNOW. */
6084 return NULL;
6085 }
6086 const auto &map = *mapp;
6087
6088 dw2_debug_names_iterator iter (map, block_index, domain, name);
6089
6090 struct compunit_symtab *stab_best = NULL;
6091 struct dwarf2_per_cu_data *per_cu;
6092 while ((per_cu = iter.next ()) != NULL)
6093 {
6094 struct symbol *sym, *with_opaque = NULL;
6095 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
6096 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
6097 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
6098
6099 sym = block_find_symbol (block, name, domain,
6100 block_find_non_opaque_type_preferred,
6101 &with_opaque);
6102
6103 /* Some caution must be observed with overloaded functions and
6104 methods, since the index will not contain any overload
6105 information (but NAME might contain it). */
6106
6107 if (sym != NULL
6108 && strcmp_iw (sym->search_name (), name) == 0)
6109 return stab;
6110 if (with_opaque != NULL
6111 && strcmp_iw (with_opaque->search_name (), name) == 0)
6112 stab_best = stab;
6113
6114 /* Keep looking through other CUs. */
6115 }
6116
6117 return stab_best;
6118 }
6119
6120 /* This dumps minimal information about .debug_names. It is called
6121 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
6122 uses this to verify that .debug_names has been loaded. */
6123
6124 static void
6125 dw2_debug_names_dump (struct objfile *objfile)
6126 {
6127 struct dwarf2_per_objfile *dwarf2_per_objfile
6128 = get_dwarf2_per_objfile (objfile);
6129
6130 gdb_assert (dwarf2_per_objfile->using_index);
6131 printf_filtered (".debug_names:");
6132 if (dwarf2_per_objfile->debug_names_table)
6133 printf_filtered (" exists\n");
6134 else
6135 printf_filtered (" faked for \"readnow\"\n");
6136 printf_filtered ("\n");
6137 }
6138
6139 static void
6140 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
6141 const char *func_name)
6142 {
6143 struct dwarf2_per_objfile *dwarf2_per_objfile
6144 = get_dwarf2_per_objfile (objfile);
6145
6146 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
6147 if (dwarf2_per_objfile->debug_names_table)
6148 {
6149 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6150
6151 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
6152
6153 struct dwarf2_per_cu_data *per_cu;
6154 while ((per_cu = iter.next ()) != NULL)
6155 dw2_instantiate_symtab (per_cu, false);
6156 }
6157 }
6158
6159 static void
6160 dw2_debug_names_map_matching_symbols
6161 (struct objfile *objfile,
6162 const lookup_name_info &name, domain_enum domain,
6163 int global,
6164 gdb::function_view<symbol_found_callback_ftype> callback,
6165 symbol_compare_ftype *ordered_compare)
6166 {
6167 struct dwarf2_per_objfile *dwarf2_per_objfile
6168 = get_dwarf2_per_objfile (objfile);
6169
6170 /* debug_names_table is NULL if OBJF_READNOW. */
6171 if (!dwarf2_per_objfile->debug_names_table)
6172 return;
6173
6174 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6175 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
6176
6177 const char *match_name = name.ada ().lookup_name ().c_str ();
6178 auto matcher = [&] (const char *symname)
6179 {
6180 if (ordered_compare == nullptr)
6181 return true;
6182 return ordered_compare (symname, match_name) == 0;
6183 };
6184
6185 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
6186 [&] (offset_type namei)
6187 {
6188 /* The name was matched, now expand corresponding CUs that were
6189 marked. */
6190 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
6191
6192 struct dwarf2_per_cu_data *per_cu;
6193 while ((per_cu = iter.next ()) != NULL)
6194 dw2_expand_symtabs_matching_one (per_cu, nullptr, nullptr);
6195 return true;
6196 });
6197
6198 /* It's a shame we couldn't do this inside the
6199 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
6200 that have already been expanded. Instead, this loop matches what
6201 the psymtab code does. */
6202 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
6203 {
6204 struct compunit_symtab *cust = per_cu->v.quick->compunit_symtab;
6205 if (cust != nullptr)
6206 {
6207 const struct block *block
6208 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
6209 if (!iterate_over_symbols_terminated (block, name,
6210 domain, callback))
6211 break;
6212 }
6213 }
6214 }
6215
6216 static void
6217 dw2_debug_names_expand_symtabs_matching
6218 (struct objfile *objfile,
6219 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
6220 const lookup_name_info &lookup_name,
6221 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
6222 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
6223 enum search_domain kind)
6224 {
6225 struct dwarf2_per_objfile *dwarf2_per_objfile
6226 = get_dwarf2_per_objfile (objfile);
6227
6228 /* debug_names_table is NULL if OBJF_READNOW. */
6229 if (!dwarf2_per_objfile->debug_names_table)
6230 return;
6231
6232 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
6233
6234 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6235
6236 dw2_expand_symtabs_matching_symbol (map, lookup_name,
6237 symbol_matcher,
6238 kind, [&] (offset_type namei)
6239 {
6240 /* The name was matched, now expand corresponding CUs that were
6241 marked. */
6242 dw2_debug_names_iterator iter (map, kind, namei);
6243
6244 struct dwarf2_per_cu_data *per_cu;
6245 while ((per_cu = iter.next ()) != NULL)
6246 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
6247 expansion_notify);
6248 return true;
6249 });
6250 }
6251
6252 const struct quick_symbol_functions dwarf2_debug_names_functions =
6253 {
6254 dw2_has_symbols,
6255 dw2_find_last_source_symtab,
6256 dw2_forget_cached_source_info,
6257 dw2_map_symtabs_matching_filename,
6258 dw2_debug_names_lookup_symbol,
6259 dw2_print_stats,
6260 dw2_debug_names_dump,
6261 dw2_debug_names_expand_symtabs_for_function,
6262 dw2_expand_all_symtabs,
6263 dw2_expand_symtabs_with_fullname,
6264 dw2_debug_names_map_matching_symbols,
6265 dw2_debug_names_expand_symtabs_matching,
6266 dw2_find_pc_sect_compunit_symtab,
6267 NULL,
6268 dw2_map_symbol_filenames
6269 };
6270
6271 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
6272 to either a dwarf2_per_objfile or dwz_file object. */
6273
6274 template <typename T>
6275 static gdb::array_view<const gdb_byte>
6276 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
6277 {
6278 dwarf2_section_info *section = &section_owner->gdb_index;
6279
6280 if (dwarf2_section_empty_p (section))
6281 return {};
6282
6283 /* Older elfutils strip versions could keep the section in the main
6284 executable while splitting it for the separate debug info file. */
6285 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
6286 return {};
6287
6288 dwarf2_read_section (obj, section);
6289
6290 /* dwarf2_section_info::size is a bfd_size_type, while
6291 gdb::array_view works with size_t. On 32-bit hosts, with
6292 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
6293 is 32-bit. So we need an explicit narrowing conversion here.
6294 This is fine, because it's impossible to allocate or mmap an
6295 array/buffer larger than what size_t can represent. */
6296 return gdb::make_array_view (section->buffer, section->size);
6297 }
6298
6299 /* Lookup the index cache for the contents of the index associated to
6300 DWARF2_OBJ. */
6301
6302 static gdb::array_view<const gdb_byte>
6303 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_objfile *dwarf2_obj)
6304 {
6305 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
6306 if (build_id == nullptr)
6307 return {};
6308
6309 return global_index_cache.lookup_gdb_index (build_id,
6310 &dwarf2_obj->index_cache_res);
6311 }
6312
6313 /* Same as the above, but for DWZ. */
6314
6315 static gdb::array_view<const gdb_byte>
6316 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
6317 {
6318 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
6319 if (build_id == nullptr)
6320 return {};
6321
6322 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
6323 }
6324
6325 /* See symfile.h. */
6326
6327 bool
6328 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
6329 {
6330 struct dwarf2_per_objfile *dwarf2_per_objfile
6331 = get_dwarf2_per_objfile (objfile);
6332
6333 /* If we're about to read full symbols, don't bother with the
6334 indices. In this case we also don't care if some other debug
6335 format is making psymtabs, because they are all about to be
6336 expanded anyway. */
6337 if ((objfile->flags & OBJF_READNOW))
6338 {
6339 dwarf2_per_objfile->using_index = 1;
6340 create_all_comp_units (dwarf2_per_objfile);
6341 create_all_type_units (dwarf2_per_objfile);
6342 dwarf2_per_objfile->quick_file_names_table
6343 = create_quick_file_names_table
6344 (dwarf2_per_objfile->all_comp_units.size ());
6345
6346 for (int i = 0; i < (dwarf2_per_objfile->all_comp_units.size ()
6347 + dwarf2_per_objfile->all_type_units.size ()); ++i)
6348 {
6349 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
6350
6351 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6352 struct dwarf2_per_cu_quick_data);
6353 }
6354
6355 /* Return 1 so that gdb sees the "quick" functions. However,
6356 these functions will be no-ops because we will have expanded
6357 all symtabs. */
6358 *index_kind = dw_index_kind::GDB_INDEX;
6359 return true;
6360 }
6361
6362 if (dwarf2_read_debug_names (dwarf2_per_objfile))
6363 {
6364 *index_kind = dw_index_kind::DEBUG_NAMES;
6365 return true;
6366 }
6367
6368 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
6369 get_gdb_index_contents_from_section<struct dwarf2_per_objfile>,
6370 get_gdb_index_contents_from_section<dwz_file>))
6371 {
6372 *index_kind = dw_index_kind::GDB_INDEX;
6373 return true;
6374 }
6375
6376 /* ... otherwise, try to find the index in the index cache. */
6377 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
6378 get_gdb_index_contents_from_cache,
6379 get_gdb_index_contents_from_cache_dwz))
6380 {
6381 global_index_cache.hit ();
6382 *index_kind = dw_index_kind::GDB_INDEX;
6383 return true;
6384 }
6385
6386 global_index_cache.miss ();
6387 return false;
6388 }
6389
6390 \f
6391
6392 /* Build a partial symbol table. */
6393
6394 void
6395 dwarf2_build_psymtabs (struct objfile *objfile)
6396 {
6397 struct dwarf2_per_objfile *dwarf2_per_objfile
6398 = get_dwarf2_per_objfile (objfile);
6399
6400 init_psymbol_list (objfile, 1024);
6401
6402 try
6403 {
6404 /* This isn't really ideal: all the data we allocate on the
6405 objfile's obstack is still uselessly kept around. However,
6406 freeing it seems unsafe. */
6407 psymtab_discarder psymtabs (objfile);
6408 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
6409 psymtabs.keep ();
6410
6411 /* (maybe) store an index in the cache. */
6412 global_index_cache.store (dwarf2_per_objfile);
6413 }
6414 catch (const gdb_exception_error &except)
6415 {
6416 exception_print (gdb_stderr, except);
6417 }
6418 }
6419
6420 /* Return the total length of the CU described by HEADER. */
6421
6422 static unsigned int
6423 get_cu_length (const struct comp_unit_head *header)
6424 {
6425 return header->initial_length_size + header->length;
6426 }
6427
6428 /* Return TRUE if SECT_OFF is within CU_HEADER. */
6429
6430 static inline bool
6431 offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
6432 {
6433 sect_offset bottom = cu_header->sect_off;
6434 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
6435
6436 return sect_off >= bottom && sect_off < top;
6437 }
6438
6439 /* Find the base address of the compilation unit for range lists and
6440 location lists. It will normally be specified by DW_AT_low_pc.
6441 In DWARF-3 draft 4, the base address could be overridden by
6442 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6443 compilation units with discontinuous ranges. */
6444
6445 static void
6446 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6447 {
6448 struct attribute *attr;
6449
6450 cu->base_known = 0;
6451 cu->base_address = 0;
6452
6453 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6454 if (attr != nullptr)
6455 {
6456 cu->base_address = attr_value_as_address (attr);
6457 cu->base_known = 1;
6458 }
6459 else
6460 {
6461 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6462 if (attr != nullptr)
6463 {
6464 cu->base_address = attr_value_as_address (attr);
6465 cu->base_known = 1;
6466 }
6467 }
6468 }
6469
6470 /* Read in the comp unit header information from the debug_info at info_ptr.
6471 Use rcuh_kind::COMPILE as the default type if not known by the caller.
6472 NOTE: This leaves members offset, first_die_offset to be filled in
6473 by the caller. */
6474
6475 static const gdb_byte *
6476 read_comp_unit_head (struct comp_unit_head *cu_header,
6477 const gdb_byte *info_ptr,
6478 struct dwarf2_section_info *section,
6479 rcuh_kind section_kind)
6480 {
6481 int signed_addr;
6482 unsigned int bytes_read;
6483 const char *filename = get_section_file_name (section);
6484 bfd *abfd = get_section_bfd_owner (section);
6485
6486 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
6487 cu_header->initial_length_size = bytes_read;
6488 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
6489 info_ptr += bytes_read;
6490 cu_header->version = read_2_bytes (abfd, info_ptr);
6491 if (cu_header->version < 2 || cu_header->version > 5)
6492 error (_("Dwarf Error: wrong version in compilation unit header "
6493 "(is %d, should be 2, 3, 4 or 5) [in module %s]"),
6494 cu_header->version, filename);
6495 info_ptr += 2;
6496 if (cu_header->version < 5)
6497 switch (section_kind)
6498 {
6499 case rcuh_kind::COMPILE:
6500 cu_header->unit_type = DW_UT_compile;
6501 break;
6502 case rcuh_kind::TYPE:
6503 cu_header->unit_type = DW_UT_type;
6504 break;
6505 default:
6506 internal_error (__FILE__, __LINE__,
6507 _("read_comp_unit_head: invalid section_kind"));
6508 }
6509 else
6510 {
6511 cu_header->unit_type = static_cast<enum dwarf_unit_type>
6512 (read_1_byte (abfd, info_ptr));
6513 info_ptr += 1;
6514 switch (cu_header->unit_type)
6515 {
6516 case DW_UT_compile:
6517 case DW_UT_partial:
6518 case DW_UT_skeleton:
6519 case DW_UT_split_compile:
6520 if (section_kind != rcuh_kind::COMPILE)
6521 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6522 "(is %s, should be %s) [in module %s]"),
6523 dwarf_unit_type_name (cu_header->unit_type),
6524 dwarf_unit_type_name (DW_UT_type), filename);
6525 break;
6526 case DW_UT_type:
6527 case DW_UT_split_type:
6528 section_kind = rcuh_kind::TYPE;
6529 break;
6530 default:
6531 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6532 "(is %#04x, should be one of: %s, %s, %s, %s or %s) "
6533 "[in module %s]"), cu_header->unit_type,
6534 dwarf_unit_type_name (DW_UT_compile),
6535 dwarf_unit_type_name (DW_UT_skeleton),
6536 dwarf_unit_type_name (DW_UT_split_compile),
6537 dwarf_unit_type_name (DW_UT_type),
6538 dwarf_unit_type_name (DW_UT_split_type), filename);
6539 }
6540
6541 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6542 info_ptr += 1;
6543 }
6544 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
6545 cu_header,
6546 &bytes_read);
6547 info_ptr += bytes_read;
6548 if (cu_header->version < 5)
6549 {
6550 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6551 info_ptr += 1;
6552 }
6553 signed_addr = bfd_get_sign_extend_vma (abfd);
6554 if (signed_addr < 0)
6555 internal_error (__FILE__, __LINE__,
6556 _("read_comp_unit_head: dwarf from non elf file"));
6557 cu_header->signed_addr_p = signed_addr;
6558
6559 bool header_has_signature = section_kind == rcuh_kind::TYPE
6560 || cu_header->unit_type == DW_UT_skeleton
6561 || cu_header->unit_type == DW_UT_split_compile;
6562
6563 if (header_has_signature)
6564 {
6565 cu_header->signature = read_8_bytes (abfd, info_ptr);
6566 info_ptr += 8;
6567 }
6568
6569 if (section_kind == rcuh_kind::TYPE)
6570 {
6571 LONGEST type_offset;
6572 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
6573 info_ptr += bytes_read;
6574 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
6575 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
6576 error (_("Dwarf Error: Too big type_offset in compilation unit "
6577 "header (is %s) [in module %s]"), plongest (type_offset),
6578 filename);
6579 }
6580
6581 return info_ptr;
6582 }
6583
6584 /* Helper function that returns the proper abbrev section for
6585 THIS_CU. */
6586
6587 static struct dwarf2_section_info *
6588 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6589 {
6590 struct dwarf2_section_info *abbrev;
6591 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6592
6593 if (this_cu->is_dwz)
6594 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
6595 else
6596 abbrev = &dwarf2_per_objfile->abbrev;
6597
6598 return abbrev;
6599 }
6600
6601 /* Subroutine of read_and_check_comp_unit_head and
6602 read_and_check_type_unit_head to simplify them.
6603 Perform various error checking on the header. */
6604
6605 static void
6606 error_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6607 struct comp_unit_head *header,
6608 struct dwarf2_section_info *section,
6609 struct dwarf2_section_info *abbrev_section)
6610 {
6611 const char *filename = get_section_file_name (section);
6612
6613 if (to_underlying (header->abbrev_sect_off)
6614 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
6615 error (_("Dwarf Error: bad offset (%s) in compilation unit header "
6616 "(offset %s + 6) [in module %s]"),
6617 sect_offset_str (header->abbrev_sect_off),
6618 sect_offset_str (header->sect_off),
6619 filename);
6620
6621 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
6622 avoid potential 32-bit overflow. */
6623 if (((ULONGEST) header->sect_off + get_cu_length (header))
6624 > section->size)
6625 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
6626 "(offset %s + 0) [in module %s]"),
6627 header->length, sect_offset_str (header->sect_off),
6628 filename);
6629 }
6630
6631 /* Read in a CU/TU header and perform some basic error checking.
6632 The contents of the header are stored in HEADER.
6633 The result is a pointer to the start of the first DIE. */
6634
6635 static const gdb_byte *
6636 read_and_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6637 struct comp_unit_head *header,
6638 struct dwarf2_section_info *section,
6639 struct dwarf2_section_info *abbrev_section,
6640 const gdb_byte *info_ptr,
6641 rcuh_kind section_kind)
6642 {
6643 const gdb_byte *beg_of_comp_unit = info_ptr;
6644
6645 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
6646
6647 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
6648
6649 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
6650
6651 error_check_comp_unit_head (dwarf2_per_objfile, header, section,
6652 abbrev_section);
6653
6654 return info_ptr;
6655 }
6656
6657 /* Fetch the abbreviation table offset from a comp or type unit header. */
6658
6659 static sect_offset
6660 read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6661 struct dwarf2_section_info *section,
6662 sect_offset sect_off)
6663 {
6664 bfd *abfd = get_section_bfd_owner (section);
6665 const gdb_byte *info_ptr;
6666 unsigned int initial_length_size, offset_size;
6667 uint16_t version;
6668
6669 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
6670 info_ptr = section->buffer + to_underlying (sect_off);
6671 read_initial_length (abfd, info_ptr, &initial_length_size);
6672 offset_size = initial_length_size == 4 ? 4 : 8;
6673 info_ptr += initial_length_size;
6674
6675 version = read_2_bytes (abfd, info_ptr);
6676 info_ptr += 2;
6677 if (version >= 5)
6678 {
6679 /* Skip unit type and address size. */
6680 info_ptr += 2;
6681 }
6682
6683 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
6684 }
6685
6686 /* Allocate a new partial symtab for file named NAME and mark this new
6687 partial symtab as being an include of PST. */
6688
6689 static void
6690 dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
6691 struct objfile *objfile)
6692 {
6693 dwarf2_psymtab *subpst = new dwarf2_psymtab (name, objfile);
6694
6695 if (!IS_ABSOLUTE_PATH (subpst->filename))
6696 {
6697 /* It shares objfile->objfile_obstack. */
6698 subpst->dirname = pst->dirname;
6699 }
6700
6701 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
6702 subpst->dependencies[0] = pst;
6703 subpst->number_of_dependencies = 1;
6704
6705 /* No private part is necessary for include psymtabs. This property
6706 can be used to differentiate between such include psymtabs and
6707 the regular ones. */
6708 subpst->per_cu_data = nullptr;
6709 }
6710
6711 /* Read the Line Number Program data and extract the list of files
6712 included by the source file represented by PST. Build an include
6713 partial symtab for each of these included files. */
6714
6715 static void
6716 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
6717 struct die_info *die,
6718 dwarf2_psymtab *pst)
6719 {
6720 line_header_up lh;
6721 struct attribute *attr;
6722
6723 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6724 if (attr != nullptr)
6725 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
6726 if (lh == NULL)
6727 return; /* No linetable, so no includes. */
6728
6729 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6730 that we pass in the raw text_low here; that is ok because we're
6731 only decoding the line table to make include partial symtabs, and
6732 so the addresses aren't really used. */
6733 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
6734 pst->raw_text_low (), 1);
6735 }
6736
6737 static hashval_t
6738 hash_signatured_type (const void *item)
6739 {
6740 const struct signatured_type *sig_type
6741 = (const struct signatured_type *) item;
6742
6743 /* This drops the top 32 bits of the signature, but is ok for a hash. */
6744 return sig_type->signature;
6745 }
6746
6747 static int
6748 eq_signatured_type (const void *item_lhs, const void *item_rhs)
6749 {
6750 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6751 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
6752
6753 return lhs->signature == rhs->signature;
6754 }
6755
6756 /* Allocate a hash table for signatured types. */
6757
6758 static htab_t
6759 allocate_signatured_type_table (struct objfile *objfile)
6760 {
6761 return htab_create_alloc_ex (41,
6762 hash_signatured_type,
6763 eq_signatured_type,
6764 NULL,
6765 &objfile->objfile_obstack,
6766 hashtab_obstack_allocate,
6767 dummy_obstack_deallocate);
6768 }
6769
6770 /* A helper function to add a signatured type CU to a table. */
6771
6772 static int
6773 add_signatured_type_cu_to_table (void **slot, void *datum)
6774 {
6775 struct signatured_type *sigt = (struct signatured_type *) *slot;
6776 std::vector<signatured_type *> *all_type_units
6777 = (std::vector<signatured_type *> *) datum;
6778
6779 all_type_units->push_back (sigt);
6780
6781 return 1;
6782 }
6783
6784 /* A helper for create_debug_types_hash_table. Read types from SECTION
6785 and fill them into TYPES_HTAB. It will process only type units,
6786 therefore DW_UT_type. */
6787
6788 static void
6789 create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6790 struct dwo_file *dwo_file,
6791 dwarf2_section_info *section, htab_t &types_htab,
6792 rcuh_kind section_kind)
6793 {
6794 struct objfile *objfile = dwarf2_per_objfile->objfile;
6795 struct dwarf2_section_info *abbrev_section;
6796 bfd *abfd;
6797 const gdb_byte *info_ptr, *end_ptr;
6798
6799 abbrev_section = (dwo_file != NULL
6800 ? &dwo_file->sections.abbrev
6801 : &dwarf2_per_objfile->abbrev);
6802
6803 if (dwarf_read_debug)
6804 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6805 get_section_name (section),
6806 get_section_file_name (abbrev_section));
6807
6808 dwarf2_read_section (objfile, section);
6809 info_ptr = section->buffer;
6810
6811 if (info_ptr == NULL)
6812 return;
6813
6814 /* We can't set abfd until now because the section may be empty or
6815 not present, in which case the bfd is unknown. */
6816 abfd = get_section_bfd_owner (section);
6817
6818 /* We don't use cutu_reader here because we don't need to read
6819 any dies: the signature is in the header. */
6820
6821 end_ptr = info_ptr + section->size;
6822 while (info_ptr < end_ptr)
6823 {
6824 struct signatured_type *sig_type;
6825 struct dwo_unit *dwo_tu;
6826 void **slot;
6827 const gdb_byte *ptr = info_ptr;
6828 struct comp_unit_head header;
6829 unsigned int length;
6830
6831 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6832
6833 /* Initialize it due to a false compiler warning. */
6834 header.signature = -1;
6835 header.type_cu_offset_in_tu = (cu_offset) -1;
6836
6837 /* We need to read the type's signature in order to build the hash
6838 table, but we don't need anything else just yet. */
6839
6840 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
6841 abbrev_section, ptr, section_kind);
6842
6843 length = get_cu_length (&header);
6844
6845 /* Skip dummy type units. */
6846 if (ptr >= info_ptr + length
6847 || peek_abbrev_code (abfd, ptr) == 0
6848 || header.unit_type != DW_UT_type)
6849 {
6850 info_ptr += length;
6851 continue;
6852 }
6853
6854 if (types_htab == NULL)
6855 {
6856 if (dwo_file)
6857 types_htab = allocate_dwo_unit_table (objfile);
6858 else
6859 types_htab = allocate_signatured_type_table (objfile);
6860 }
6861
6862 if (dwo_file)
6863 {
6864 sig_type = NULL;
6865 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6866 struct dwo_unit);
6867 dwo_tu->dwo_file = dwo_file;
6868 dwo_tu->signature = header.signature;
6869 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6870 dwo_tu->section = section;
6871 dwo_tu->sect_off = sect_off;
6872 dwo_tu->length = length;
6873 }
6874 else
6875 {
6876 /* N.B.: type_offset is not usable if this type uses a DWO file.
6877 The real type_offset is in the DWO file. */
6878 dwo_tu = NULL;
6879 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6880 struct signatured_type);
6881 sig_type->signature = header.signature;
6882 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6883 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6884 sig_type->per_cu.is_debug_types = 1;
6885 sig_type->per_cu.section = section;
6886 sig_type->per_cu.sect_off = sect_off;
6887 sig_type->per_cu.length = length;
6888 }
6889
6890 slot = htab_find_slot (types_htab,
6891 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6892 INSERT);
6893 gdb_assert (slot != NULL);
6894 if (*slot != NULL)
6895 {
6896 sect_offset dup_sect_off;
6897
6898 if (dwo_file)
6899 {
6900 const struct dwo_unit *dup_tu
6901 = (const struct dwo_unit *) *slot;
6902
6903 dup_sect_off = dup_tu->sect_off;
6904 }
6905 else
6906 {
6907 const struct signatured_type *dup_tu
6908 = (const struct signatured_type *) *slot;
6909
6910 dup_sect_off = dup_tu->per_cu.sect_off;
6911 }
6912
6913 complaint (_("debug type entry at offset %s is duplicate to"
6914 " the entry at offset %s, signature %s"),
6915 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
6916 hex_string (header.signature));
6917 }
6918 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
6919
6920 if (dwarf_read_debug > 1)
6921 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6922 sect_offset_str (sect_off),
6923 hex_string (header.signature));
6924
6925 info_ptr += length;
6926 }
6927 }
6928
6929 /* Create the hash table of all entries in the .debug_types
6930 (or .debug_types.dwo) section(s).
6931 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6932 otherwise it is NULL.
6933
6934 The result is a pointer to the hash table or NULL if there are no types.
6935
6936 Note: This function processes DWO files only, not DWP files. */
6937
6938 static void
6939 create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6940 struct dwo_file *dwo_file,
6941 gdb::array_view<dwarf2_section_info> type_sections,
6942 htab_t &types_htab)
6943 {
6944 for (dwarf2_section_info &section : type_sections)
6945 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
6946 types_htab, rcuh_kind::TYPE);
6947 }
6948
6949 /* Create the hash table of all entries in the .debug_types section,
6950 and initialize all_type_units.
6951 The result is zero if there is an error (e.g. missing .debug_types section),
6952 otherwise non-zero. */
6953
6954 static int
6955 create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6956 {
6957 htab_t types_htab = NULL;
6958
6959 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6960 &dwarf2_per_objfile->info, types_htab,
6961 rcuh_kind::COMPILE);
6962 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6963 dwarf2_per_objfile->types, types_htab);
6964 if (types_htab == NULL)
6965 {
6966 dwarf2_per_objfile->signatured_types = NULL;
6967 return 0;
6968 }
6969
6970 dwarf2_per_objfile->signatured_types = types_htab;
6971
6972 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
6973 dwarf2_per_objfile->all_type_units.reserve (htab_elements (types_htab));
6974
6975 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table,
6976 &dwarf2_per_objfile->all_type_units);
6977
6978 return 1;
6979 }
6980
6981 /* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6982 If SLOT is non-NULL, it is the entry to use in the hash table.
6983 Otherwise we find one. */
6984
6985 static struct signatured_type *
6986 add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6987 void **slot)
6988 {
6989 struct objfile *objfile = dwarf2_per_objfile->objfile;
6990
6991 if (dwarf2_per_objfile->all_type_units.size ()
6992 == dwarf2_per_objfile->all_type_units.capacity ())
6993 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
6994
6995 signatured_type *sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6996 struct signatured_type);
6997
6998 dwarf2_per_objfile->all_type_units.push_back (sig_type);
6999 sig_type->signature = sig;
7000 sig_type->per_cu.is_debug_types = 1;
7001 if (dwarf2_per_objfile->using_index)
7002 {
7003 sig_type->per_cu.v.quick =
7004 OBSTACK_ZALLOC (&objfile->objfile_obstack,
7005 struct dwarf2_per_cu_quick_data);
7006 }
7007
7008 if (slot == NULL)
7009 {
7010 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7011 sig_type, INSERT);
7012 }
7013 gdb_assert (*slot == NULL);
7014 *slot = sig_type;
7015 /* The rest of sig_type must be filled in by the caller. */
7016 return sig_type;
7017 }
7018
7019 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
7020 Fill in SIG_ENTRY with DWO_ENTRY. */
7021
7022 static void
7023 fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
7024 struct signatured_type *sig_entry,
7025 struct dwo_unit *dwo_entry)
7026 {
7027 /* Make sure we're not clobbering something we don't expect to. */
7028 gdb_assert (! sig_entry->per_cu.queued);
7029 gdb_assert (sig_entry->per_cu.cu == NULL);
7030 if (dwarf2_per_objfile->using_index)
7031 {
7032 gdb_assert (sig_entry->per_cu.v.quick != NULL);
7033 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
7034 }
7035 else
7036 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
7037 gdb_assert (sig_entry->signature == dwo_entry->signature);
7038 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
7039 gdb_assert (sig_entry->type_unit_group == NULL);
7040 gdb_assert (sig_entry->dwo_unit == NULL);
7041
7042 sig_entry->per_cu.section = dwo_entry->section;
7043 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7044 sig_entry->per_cu.length = dwo_entry->length;
7045 sig_entry->per_cu.reading_dwo_directly = 1;
7046 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
7047 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
7048 sig_entry->dwo_unit = dwo_entry;
7049 }
7050
7051 /* Subroutine of lookup_signatured_type.
7052 If we haven't read the TU yet, create the signatured_type data structure
7053 for a TU to be read in directly from a DWO file, bypassing the stub.
7054 This is the "Stay in DWO Optimization": When there is no DWP file and we're
7055 using .gdb_index, then when reading a CU we want to stay in the DWO file
7056 containing that CU. Otherwise we could end up reading several other DWO
7057 files (due to comdat folding) to process the transitive closure of all the
7058 mentioned TUs, and that can be slow. The current DWO file will have every
7059 type signature that it needs.
7060 We only do this for .gdb_index because in the psymtab case we already have
7061 to read all the DWOs to build the type unit groups. */
7062
7063 static struct signatured_type *
7064 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7065 {
7066 struct dwarf2_per_objfile *dwarf2_per_objfile
7067 = cu->per_cu->dwarf2_per_objfile;
7068 struct objfile *objfile = dwarf2_per_objfile->objfile;
7069 struct dwo_file *dwo_file;
7070 struct dwo_unit find_dwo_entry, *dwo_entry;
7071 struct signatured_type find_sig_entry, *sig_entry;
7072 void **slot;
7073
7074 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7075
7076 /* If TU skeletons have been removed then we may not have read in any
7077 TUs yet. */
7078 if (dwarf2_per_objfile->signatured_types == NULL)
7079 {
7080 dwarf2_per_objfile->signatured_types
7081 = allocate_signatured_type_table (objfile);
7082 }
7083
7084 /* We only ever need to read in one copy of a signatured type.
7085 Use the global signatured_types array to do our own comdat-folding
7086 of types. If this is the first time we're reading this TU, and
7087 the TU has an entry in .gdb_index, replace the recorded data from
7088 .gdb_index with this TU. */
7089
7090 find_sig_entry.signature = sig;
7091 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7092 &find_sig_entry, INSERT);
7093 sig_entry = (struct signatured_type *) *slot;
7094
7095 /* We can get here with the TU already read, *or* in the process of being
7096 read. Don't reassign the global entry to point to this DWO if that's
7097 the case. Also note that if the TU is already being read, it may not
7098 have come from a DWO, the program may be a mix of Fission-compiled
7099 code and non-Fission-compiled code. */
7100
7101 /* Have we already tried to read this TU?
7102 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7103 needn't exist in the global table yet). */
7104 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
7105 return sig_entry;
7106
7107 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
7108 dwo_unit of the TU itself. */
7109 dwo_file = cu->dwo_unit->dwo_file;
7110
7111 /* Ok, this is the first time we're reading this TU. */
7112 if (dwo_file->tus == NULL)
7113 return NULL;
7114 find_dwo_entry.signature = sig;
7115 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
7116 if (dwo_entry == NULL)
7117 return NULL;
7118
7119 /* If the global table doesn't have an entry for this TU, add one. */
7120 if (sig_entry == NULL)
7121 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
7122
7123 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
7124 sig_entry->per_cu.tu_read = 1;
7125 return sig_entry;
7126 }
7127
7128 /* Subroutine of lookup_signatured_type.
7129 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
7130 then try the DWP file. If the TU stub (skeleton) has been removed then
7131 it won't be in .gdb_index. */
7132
7133 static struct signatured_type *
7134 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7135 {
7136 struct dwarf2_per_objfile *dwarf2_per_objfile
7137 = cu->per_cu->dwarf2_per_objfile;
7138 struct objfile *objfile = dwarf2_per_objfile->objfile;
7139 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
7140 struct dwo_unit *dwo_entry;
7141 struct signatured_type find_sig_entry, *sig_entry;
7142 void **slot;
7143
7144 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7145 gdb_assert (dwp_file != NULL);
7146
7147 /* If TU skeletons have been removed then we may not have read in any
7148 TUs yet. */
7149 if (dwarf2_per_objfile->signatured_types == NULL)
7150 {
7151 dwarf2_per_objfile->signatured_types
7152 = allocate_signatured_type_table (objfile);
7153 }
7154
7155 find_sig_entry.signature = sig;
7156 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7157 &find_sig_entry, INSERT);
7158 sig_entry = (struct signatured_type *) *slot;
7159
7160 /* Have we already tried to read this TU?
7161 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7162 needn't exist in the global table yet). */
7163 if (sig_entry != NULL)
7164 return sig_entry;
7165
7166 if (dwp_file->tus == NULL)
7167 return NULL;
7168 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
7169 sig, 1 /* is_debug_types */);
7170 if (dwo_entry == NULL)
7171 return NULL;
7172
7173 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
7174 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
7175
7176 return sig_entry;
7177 }
7178
7179 /* Lookup a signature based type for DW_FORM_ref_sig8.
7180 Returns NULL if signature SIG is not present in the table.
7181 It is up to the caller to complain about this. */
7182
7183 static struct signatured_type *
7184 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7185 {
7186 struct dwarf2_per_objfile *dwarf2_per_objfile
7187 = cu->per_cu->dwarf2_per_objfile;
7188
7189 if (cu->dwo_unit
7190 && dwarf2_per_objfile->using_index)
7191 {
7192 /* We're in a DWO/DWP file, and we're using .gdb_index.
7193 These cases require special processing. */
7194 if (get_dwp_file (dwarf2_per_objfile) == NULL)
7195 return lookup_dwo_signatured_type (cu, sig);
7196 else
7197 return lookup_dwp_signatured_type (cu, sig);
7198 }
7199 else
7200 {
7201 struct signatured_type find_entry, *entry;
7202
7203 if (dwarf2_per_objfile->signatured_types == NULL)
7204 return NULL;
7205 find_entry.signature = sig;
7206 entry = ((struct signatured_type *)
7207 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
7208 return entry;
7209 }
7210 }
7211
7212 /* Return the address base of the compile unit, which, if exists, is stored
7213 either at the attribute DW_AT_GNU_addr_base, or DW_AT_addr_base. */
7214 static gdb::optional<ULONGEST>
7215 lookup_addr_base (struct die_info *comp_unit_die)
7216 {
7217 struct attribute *attr;
7218 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_addr_base);
7219 if (attr == nullptr)
7220 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_GNU_addr_base);
7221 if (attr == nullptr)
7222 return gdb::optional<ULONGEST> ();
7223 return DW_UNSND (attr);
7224 }
7225
7226 /* Return range lists base of the compile unit, which, if exists, is stored
7227 either at the attribute DW_AT_rnglists_base or DW_AT_GNU_ranges_base. */
7228 static ULONGEST
7229 lookup_ranges_base (struct die_info *comp_unit_die)
7230 {
7231 struct attribute *attr;
7232 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_rnglists_base);
7233 if (attr == nullptr)
7234 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_GNU_ranges_base);
7235 if (attr == nullptr)
7236 return 0;
7237 return DW_UNSND (attr);
7238 }
7239
7240 /* Low level DIE reading support. */
7241
7242 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
7243
7244 static void
7245 init_cu_die_reader (struct die_reader_specs *reader,
7246 struct dwarf2_cu *cu,
7247 struct dwarf2_section_info *section,
7248 struct dwo_file *dwo_file,
7249 struct abbrev_table *abbrev_table)
7250 {
7251 gdb_assert (section->readin && section->buffer != NULL);
7252 reader->abfd = get_section_bfd_owner (section);
7253 reader->cu = cu;
7254 reader->dwo_file = dwo_file;
7255 reader->die_section = section;
7256 reader->buffer = section->buffer;
7257 reader->buffer_end = section->buffer + section->size;
7258 reader->comp_dir = NULL;
7259 reader->abbrev_table = abbrev_table;
7260 }
7261
7262 /* Subroutine of cutu_reader to simplify it.
7263 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
7264 There's just a lot of work to do, and cutu_reader is big enough
7265 already.
7266
7267 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
7268 from it to the DIE in the DWO. If NULL we are skipping the stub.
7269 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
7270 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
7271 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
7272 STUB_COMP_DIR may be non-NULL.
7273 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
7274 are filled in with the info of the DIE from the DWO file.
7275 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
7276 from the dwo. Since *RESULT_READER references this abbrev table, it must be
7277 kept around for at least as long as *RESULT_READER.
7278
7279 The result is non-zero if a valid (non-dummy) DIE was found. */
7280
7281 static int
7282 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
7283 struct dwo_unit *dwo_unit,
7284 struct die_info *stub_comp_unit_die,
7285 const char *stub_comp_dir,
7286 struct die_reader_specs *result_reader,
7287 const gdb_byte **result_info_ptr,
7288 struct die_info **result_comp_unit_die,
7289 int *result_has_children,
7290 abbrev_table_up *result_dwo_abbrev_table)
7291 {
7292 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7293 struct objfile *objfile = dwarf2_per_objfile->objfile;
7294 struct dwarf2_cu *cu = this_cu->cu;
7295 bfd *abfd;
7296 const gdb_byte *begin_info_ptr, *info_ptr;
7297 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
7298 int i,num_extra_attrs;
7299 struct dwarf2_section_info *dwo_abbrev_section;
7300 struct die_info *comp_unit_die;
7301
7302 /* At most one of these may be provided. */
7303 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
7304
7305 /* These attributes aren't processed until later:
7306 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
7307 DW_AT_comp_dir is used now, to find the DWO file, but it is also
7308 referenced later. However, these attributes are found in the stub
7309 which we won't have later. In order to not impose this complication
7310 on the rest of the code, we read them here and copy them to the
7311 DWO CU/TU die. */
7312
7313 stmt_list = NULL;
7314 low_pc = NULL;
7315 high_pc = NULL;
7316 ranges = NULL;
7317 comp_dir = NULL;
7318
7319 if (stub_comp_unit_die != NULL)
7320 {
7321 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
7322 DWO file. */
7323 if (! this_cu->is_debug_types)
7324 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
7325 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
7326 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
7327 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
7328 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
7329
7330 cu->addr_base = lookup_addr_base (stub_comp_unit_die);
7331
7332 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
7333 here (if needed). We need the value before we can process
7334 DW_AT_ranges. */
7335 cu->ranges_base = lookup_ranges_base (stub_comp_unit_die);
7336 }
7337 else if (stub_comp_dir != NULL)
7338 {
7339 /* Reconstruct the comp_dir attribute to simplify the code below. */
7340 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
7341 comp_dir->name = DW_AT_comp_dir;
7342 comp_dir->form = DW_FORM_string;
7343 DW_STRING_IS_CANONICAL (comp_dir) = 0;
7344 DW_STRING (comp_dir) = stub_comp_dir;
7345 }
7346
7347 /* Set up for reading the DWO CU/TU. */
7348 cu->dwo_unit = dwo_unit;
7349 dwarf2_section_info *section = dwo_unit->section;
7350 dwarf2_read_section (objfile, section);
7351 abfd = get_section_bfd_owner (section);
7352 begin_info_ptr = info_ptr = (section->buffer
7353 + to_underlying (dwo_unit->sect_off));
7354 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
7355
7356 if (this_cu->is_debug_types)
7357 {
7358 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
7359
7360 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7361 &cu->header, section,
7362 dwo_abbrev_section,
7363 info_ptr, rcuh_kind::TYPE);
7364 /* This is not an assert because it can be caused by bad debug info. */
7365 if (sig_type->signature != cu->header.signature)
7366 {
7367 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
7368 " TU at offset %s [in module %s]"),
7369 hex_string (sig_type->signature),
7370 hex_string (cu->header.signature),
7371 sect_offset_str (dwo_unit->sect_off),
7372 bfd_get_filename (abfd));
7373 }
7374 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7375 /* For DWOs coming from DWP files, we don't know the CU length
7376 nor the type's offset in the TU until now. */
7377 dwo_unit->length = get_cu_length (&cu->header);
7378 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
7379
7380 /* Establish the type offset that can be used to lookup the type.
7381 For DWO files, we don't know it until now. */
7382 sig_type->type_offset_in_section
7383 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
7384 }
7385 else
7386 {
7387 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7388 &cu->header, section,
7389 dwo_abbrev_section,
7390 info_ptr, rcuh_kind::COMPILE);
7391 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7392 /* For DWOs coming from DWP files, we don't know the CU length
7393 until now. */
7394 dwo_unit->length = get_cu_length (&cu->header);
7395 }
7396
7397 *result_dwo_abbrev_table
7398 = abbrev_table_read_table (dwarf2_per_objfile, dwo_abbrev_section,
7399 cu->header.abbrev_sect_off);
7400 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
7401 result_dwo_abbrev_table->get ());
7402
7403 /* Read in the die, but leave space to copy over the attributes
7404 from the stub. This has the benefit of simplifying the rest of
7405 the code - all the work to maintain the illusion of a single
7406 DW_TAG_{compile,type}_unit DIE is done here. */
7407 num_extra_attrs = ((stmt_list != NULL)
7408 + (low_pc != NULL)
7409 + (high_pc != NULL)
7410 + (ranges != NULL)
7411 + (comp_dir != NULL));
7412 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
7413 result_has_children, num_extra_attrs);
7414
7415 /* Copy over the attributes from the stub to the DIE we just read in. */
7416 comp_unit_die = *result_comp_unit_die;
7417 i = comp_unit_die->num_attrs;
7418 if (stmt_list != NULL)
7419 comp_unit_die->attrs[i++] = *stmt_list;
7420 if (low_pc != NULL)
7421 comp_unit_die->attrs[i++] = *low_pc;
7422 if (high_pc != NULL)
7423 comp_unit_die->attrs[i++] = *high_pc;
7424 if (ranges != NULL)
7425 comp_unit_die->attrs[i++] = *ranges;
7426 if (comp_dir != NULL)
7427 comp_unit_die->attrs[i++] = *comp_dir;
7428 comp_unit_die->num_attrs += num_extra_attrs;
7429
7430 if (dwarf_die_debug)
7431 {
7432 fprintf_unfiltered (gdb_stdlog,
7433 "Read die from %s@0x%x of %s:\n",
7434 get_section_name (section),
7435 (unsigned) (begin_info_ptr - section->buffer),
7436 bfd_get_filename (abfd));
7437 dump_die (comp_unit_die, dwarf_die_debug);
7438 }
7439
7440 /* Save the comp_dir attribute. If there is no DWP file then we'll read
7441 TUs by skipping the stub and going directly to the entry in the DWO file.
7442 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
7443 to get it via circuitous means. Blech. */
7444 if (comp_dir != NULL)
7445 result_reader->comp_dir = DW_STRING (comp_dir);
7446
7447 /* Skip dummy compilation units. */
7448 if (info_ptr >= begin_info_ptr + dwo_unit->length
7449 || peek_abbrev_code (abfd, info_ptr) == 0)
7450 return 0;
7451
7452 *result_info_ptr = info_ptr;
7453 return 1;
7454 }
7455
7456 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
7457 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
7458 signature is part of the header. */
7459 static gdb::optional<ULONGEST>
7460 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
7461 {
7462 if (cu->header.version >= 5)
7463 return cu->header.signature;
7464 struct attribute *attr;
7465 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7466 if (attr == nullptr)
7467 return gdb::optional<ULONGEST> ();
7468 return DW_UNSND (attr);
7469 }
7470
7471 /* Subroutine of cutu_reader to simplify it.
7472 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
7473 Returns NULL if the specified DWO unit cannot be found. */
7474
7475 static struct dwo_unit *
7476 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
7477 struct die_info *comp_unit_die,
7478 const char *dwo_name)
7479 {
7480 struct dwarf2_cu *cu = this_cu->cu;
7481 struct dwo_unit *dwo_unit;
7482 const char *comp_dir;
7483
7484 gdb_assert (cu != NULL);
7485
7486 /* Yeah, we look dwo_name up again, but it simplifies the code. */
7487 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7488 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7489
7490 if (this_cu->is_debug_types)
7491 {
7492 struct signatured_type *sig_type;
7493
7494 /* Since this_cu is the first member of struct signatured_type,
7495 we can go from a pointer to one to a pointer to the other. */
7496 sig_type = (struct signatured_type *) this_cu;
7497 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
7498 }
7499 else
7500 {
7501 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
7502 if (!signature.has_value ())
7503 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7504 " [in module %s]"),
7505 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
7506 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
7507 *signature);
7508 }
7509
7510 return dwo_unit;
7511 }
7512
7513 /* Subroutine of cutu_reader to simplify it.
7514 See it for a description of the parameters.
7515 Read a TU directly from a DWO file, bypassing the stub. */
7516
7517 void
7518 cutu_reader::init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
7519 int use_existing_cu, int keep)
7520 {
7521 struct signatured_type *sig_type;
7522 struct die_reader_specs reader;
7523
7524 /* Verify we can do the following downcast, and that we have the
7525 data we need. */
7526 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7527 sig_type = (struct signatured_type *) this_cu;
7528 gdb_assert (sig_type->dwo_unit != NULL);
7529
7530 if (use_existing_cu && this_cu->cu != NULL)
7531 {
7532 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
7533 /* There's no need to do the rereading_dwo_cu handling that
7534 cutu_reader does since we don't read the stub. */
7535 }
7536 else
7537 {
7538 /* If !use_existing_cu, this_cu->cu must be NULL. */
7539 gdb_assert (this_cu->cu == NULL);
7540 m_new_cu.reset (new dwarf2_cu (this_cu));
7541 }
7542
7543 /* A future optimization, if needed, would be to use an existing
7544 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7545 could share abbrev tables. */
7546
7547 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
7548 NULL /* stub_comp_unit_die */,
7549 sig_type->dwo_unit->dwo_file->comp_dir,
7550 &reader, &info_ptr,
7551 &comp_unit_die, &has_children,
7552 &m_dwo_abbrev_table) == 0)
7553 {
7554 /* Dummy die. */
7555 dummy_p = true;
7556 }
7557 }
7558
7559 /* Initialize a CU (or TU) and read its DIEs.
7560 If the CU defers to a DWO file, read the DWO file as well.
7561
7562 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7563 Otherwise the table specified in the comp unit header is read in and used.
7564 This is an optimization for when we already have the abbrev table.
7565
7566 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
7567 Otherwise, a new CU is allocated with xmalloc.
7568
7569 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
7570 read_in_chain. Otherwise the dwarf2_cu data is freed at the
7571 end. */
7572
7573 cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
7574 struct abbrev_table *abbrev_table,
7575 int use_existing_cu, int keep,
7576 bool skip_partial)
7577 : die_reader_specs {},
7578 m_this_cu (this_cu),
7579 m_keep (keep)
7580 {
7581 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7582 struct objfile *objfile = dwarf2_per_objfile->objfile;
7583 struct dwarf2_section_info *section = this_cu->section;
7584 bfd *abfd = get_section_bfd_owner (section);
7585 struct dwarf2_cu *cu;
7586 const gdb_byte *begin_info_ptr;
7587 struct signatured_type *sig_type = NULL;
7588 struct dwarf2_section_info *abbrev_section;
7589 /* Non-zero if CU currently points to a DWO file and we need to
7590 reread it. When this happens we need to reread the skeleton die
7591 before we can reread the DWO file (this only applies to CUs, not TUs). */
7592 int rereading_dwo_cu = 0;
7593
7594 if (dwarf_die_debug)
7595 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7596 this_cu->is_debug_types ? "type" : "comp",
7597 sect_offset_str (this_cu->sect_off));
7598
7599 if (use_existing_cu)
7600 gdb_assert (keep);
7601
7602 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7603 file (instead of going through the stub), short-circuit all of this. */
7604 if (this_cu->reading_dwo_directly)
7605 {
7606 /* Narrow down the scope of possibilities to have to understand. */
7607 gdb_assert (this_cu->is_debug_types);
7608 gdb_assert (abbrev_table == NULL);
7609 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep);
7610 return;
7611 }
7612
7613 /* This is cheap if the section is already read in. */
7614 dwarf2_read_section (objfile, section);
7615
7616 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7617
7618 abbrev_section = get_abbrev_section_for_cu (this_cu);
7619
7620 if (use_existing_cu && this_cu->cu != NULL)
7621 {
7622 cu = this_cu->cu;
7623 /* If this CU is from a DWO file we need to start over, we need to
7624 refetch the attributes from the skeleton CU.
7625 This could be optimized by retrieving those attributes from when we
7626 were here the first time: the previous comp_unit_die was stored in
7627 comp_unit_obstack. But there's no data yet that we need this
7628 optimization. */
7629 if (cu->dwo_unit != NULL)
7630 rereading_dwo_cu = 1;
7631 }
7632 else
7633 {
7634 /* If !use_existing_cu, this_cu->cu must be NULL. */
7635 gdb_assert (this_cu->cu == NULL);
7636 m_new_cu.reset (new dwarf2_cu (this_cu));
7637 cu = m_new_cu.get ();
7638 }
7639
7640 /* Get the header. */
7641 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
7642 {
7643 /* We already have the header, there's no need to read it in again. */
7644 info_ptr += to_underlying (cu->header.first_die_cu_offset);
7645 }
7646 else
7647 {
7648 if (this_cu->is_debug_types)
7649 {
7650 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7651 &cu->header, section,
7652 abbrev_section, info_ptr,
7653 rcuh_kind::TYPE);
7654
7655 /* Since per_cu is the first member of struct signatured_type,
7656 we can go from a pointer to one to a pointer to the other. */
7657 sig_type = (struct signatured_type *) this_cu;
7658 gdb_assert (sig_type->signature == cu->header.signature);
7659 gdb_assert (sig_type->type_offset_in_tu
7660 == cu->header.type_cu_offset_in_tu);
7661 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7662
7663 /* LENGTH has not been set yet for type units if we're
7664 using .gdb_index. */
7665 this_cu->length = get_cu_length (&cu->header);
7666
7667 /* Establish the type offset that can be used to lookup the type. */
7668 sig_type->type_offset_in_section =
7669 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
7670
7671 this_cu->dwarf_version = cu->header.version;
7672 }
7673 else
7674 {
7675 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7676 &cu->header, section,
7677 abbrev_section,
7678 info_ptr,
7679 rcuh_kind::COMPILE);
7680
7681 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7682 gdb_assert (this_cu->length == get_cu_length (&cu->header));
7683 this_cu->dwarf_version = cu->header.version;
7684 }
7685 }
7686
7687 /* Skip dummy compilation units. */
7688 if (info_ptr >= begin_info_ptr + this_cu->length
7689 || peek_abbrev_code (abfd, info_ptr) == 0)
7690 {
7691 dummy_p = true;
7692 return;
7693 }
7694
7695 /* If we don't have them yet, read the abbrevs for this compilation unit.
7696 And if we need to read them now, make sure they're freed when we're
7697 done. */
7698 if (abbrev_table != NULL)
7699 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7700 else
7701 {
7702 m_abbrev_table_holder
7703 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7704 cu->header.abbrev_sect_off);
7705 abbrev_table = m_abbrev_table_holder.get ();
7706 }
7707
7708 /* Read the top level CU/TU die. */
7709 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
7710 info_ptr = read_full_die (this, &comp_unit_die, info_ptr, &has_children);
7711
7712 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
7713 {
7714 dummy_p = true;
7715 return;
7716 }
7717
7718 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
7719 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7720 table from the DWO file and pass the ownership over to us. It will be
7721 referenced from READER, so we must make sure to free it after we're done
7722 with READER.
7723
7724 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7725 DWO CU, that this test will fail (the attribute will not be present). */
7726 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7727 if (dwo_name != nullptr)
7728 {
7729 struct dwo_unit *dwo_unit;
7730 struct die_info *dwo_comp_unit_die;
7731
7732 if (has_children)
7733 {
7734 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
7735 " has children (offset %s) [in module %s]"),
7736 sect_offset_str (this_cu->sect_off),
7737 bfd_get_filename (abfd));
7738 }
7739 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die, dwo_name);
7740 if (dwo_unit != NULL)
7741 {
7742 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
7743 comp_unit_die, NULL,
7744 this, &info_ptr,
7745 &dwo_comp_unit_die, &has_children,
7746 &m_dwo_abbrev_table) == 0)
7747 {
7748 /* Dummy die. */
7749 dummy_p = true;
7750 return;
7751 }
7752 comp_unit_die = dwo_comp_unit_die;
7753 }
7754 else
7755 {
7756 /* Yikes, we couldn't find the rest of the DIE, we only have
7757 the stub. A complaint has already been logged. There's
7758 not much more we can do except pass on the stub DIE to
7759 die_reader_func. We don't want to throw an error on bad
7760 debug info. */
7761 }
7762 }
7763 }
7764
7765 cutu_reader::~cutu_reader ()
7766 {
7767 /* Done, clean up. */
7768 if (m_new_cu != NULL && m_keep && !dummy_p)
7769 {
7770 struct dwarf2_per_objfile *dwarf2_per_objfile
7771 = m_this_cu->dwarf2_per_objfile;
7772 /* Link this CU into read_in_chain. */
7773 m_this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7774 dwarf2_per_objfile->read_in_chain = m_this_cu;
7775 /* The chain owns it now. */
7776 m_new_cu.release ();
7777 }
7778 }
7779
7780 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
7781 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
7782 assumed to have already done the lookup to find the DWO file).
7783
7784 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
7785 THIS_CU->is_debug_types, but nothing else.
7786
7787 We fill in THIS_CU->length.
7788
7789 THIS_CU->cu is always freed when done.
7790 This is done in order to not leave THIS_CU->cu in a state where we have
7791 to care whether it refers to the "main" CU or the DWO CU.
7792
7793 When parent_cu is passed, it is used to provide a default value for
7794 str_offsets_base and addr_base from the parent. */
7795
7796 cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
7797 struct dwarf2_cu *parent_cu,
7798 struct dwo_file *dwo_file)
7799 : die_reader_specs {},
7800 m_this_cu (this_cu)
7801 {
7802 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7803 struct objfile *objfile = dwarf2_per_objfile->objfile;
7804 struct dwarf2_section_info *section = this_cu->section;
7805 bfd *abfd = get_section_bfd_owner (section);
7806 struct dwarf2_section_info *abbrev_section;
7807 const gdb_byte *begin_info_ptr, *info_ptr;
7808 int has_children;
7809
7810 if (dwarf_die_debug)
7811 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7812 this_cu->is_debug_types ? "type" : "comp",
7813 sect_offset_str (this_cu->sect_off));
7814
7815 gdb_assert (this_cu->cu == NULL);
7816
7817 abbrev_section = (dwo_file != NULL
7818 ? &dwo_file->sections.abbrev
7819 : get_abbrev_section_for_cu (this_cu));
7820
7821 /* This is cheap if the section is already read in. */
7822 dwarf2_read_section (objfile, section);
7823
7824 m_new_cu.reset (new dwarf2_cu (this_cu));
7825
7826 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7827 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7828 &m_new_cu->header, section,
7829 abbrev_section, info_ptr,
7830 (this_cu->is_debug_types
7831 ? rcuh_kind::TYPE
7832 : rcuh_kind::COMPILE));
7833
7834 if (parent_cu != nullptr)
7835 {
7836 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7837 m_new_cu->addr_base = parent_cu->addr_base;
7838 }
7839 this_cu->length = get_cu_length (&m_new_cu->header);
7840
7841 /* Skip dummy compilation units. */
7842 if (info_ptr >= begin_info_ptr + this_cu->length
7843 || peek_abbrev_code (abfd, info_ptr) == 0)
7844 {
7845 dummy_p = true;
7846 return;
7847 }
7848
7849 m_abbrev_table_holder
7850 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7851 m_new_cu->header.abbrev_sect_off);
7852
7853 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7854 m_abbrev_table_holder.get ());
7855 info_ptr = read_full_die (this, &comp_unit_die, info_ptr, &has_children);
7856 }
7857
7858 \f
7859 /* Type Unit Groups.
7860
7861 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7862 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7863 so that all types coming from the same compilation (.o file) are grouped
7864 together. A future step could be to put the types in the same symtab as
7865 the CU the types ultimately came from. */
7866
7867 static hashval_t
7868 hash_type_unit_group (const void *item)
7869 {
7870 const struct type_unit_group *tu_group
7871 = (const struct type_unit_group *) item;
7872
7873 return hash_stmt_list_entry (&tu_group->hash);
7874 }
7875
7876 static int
7877 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
7878 {
7879 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7880 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
7881
7882 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
7883 }
7884
7885 /* Allocate a hash table for type unit groups. */
7886
7887 static htab_t
7888 allocate_type_unit_groups_table (struct objfile *objfile)
7889 {
7890 return htab_create_alloc_ex (3,
7891 hash_type_unit_group,
7892 eq_type_unit_group,
7893 NULL,
7894 &objfile->objfile_obstack,
7895 hashtab_obstack_allocate,
7896 dummy_obstack_deallocate);
7897 }
7898
7899 /* Type units that don't have DW_AT_stmt_list are grouped into their own
7900 partial symtabs. We combine several TUs per psymtab to not let the size
7901 of any one psymtab grow too big. */
7902 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7903 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
7904
7905 /* Helper routine for get_type_unit_group.
7906 Create the type_unit_group object used to hold one or more TUs. */
7907
7908 static struct type_unit_group *
7909 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
7910 {
7911 struct dwarf2_per_objfile *dwarf2_per_objfile
7912 = cu->per_cu->dwarf2_per_objfile;
7913 struct objfile *objfile = dwarf2_per_objfile->objfile;
7914 struct dwarf2_per_cu_data *per_cu;
7915 struct type_unit_group *tu_group;
7916
7917 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7918 struct type_unit_group);
7919 per_cu = &tu_group->per_cu;
7920 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
7921
7922 if (dwarf2_per_objfile->using_index)
7923 {
7924 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7925 struct dwarf2_per_cu_quick_data);
7926 }
7927 else
7928 {
7929 unsigned int line_offset = to_underlying (line_offset_struct);
7930 dwarf2_psymtab *pst;
7931 std::string name;
7932
7933 /* Give the symtab a useful name for debug purposes. */
7934 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7935 name = string_printf ("<type_units_%d>",
7936 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7937 else
7938 name = string_printf ("<type_units_at_0x%x>", line_offset);
7939
7940 pst = create_partial_symtab (per_cu, name.c_str ());
7941 pst->anonymous = true;
7942 }
7943
7944 tu_group->hash.dwo_unit = cu->dwo_unit;
7945 tu_group->hash.line_sect_off = line_offset_struct;
7946
7947 return tu_group;
7948 }
7949
7950 /* Look up the type_unit_group for type unit CU, and create it if necessary.
7951 STMT_LIST is a DW_AT_stmt_list attribute. */
7952
7953 static struct type_unit_group *
7954 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7955 {
7956 struct dwarf2_per_objfile *dwarf2_per_objfile
7957 = cu->per_cu->dwarf2_per_objfile;
7958 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7959 struct type_unit_group *tu_group;
7960 void **slot;
7961 unsigned int line_offset;
7962 struct type_unit_group type_unit_group_for_lookup;
7963
7964 if (dwarf2_per_objfile->type_unit_groups == NULL)
7965 {
7966 dwarf2_per_objfile->type_unit_groups =
7967 allocate_type_unit_groups_table (dwarf2_per_objfile->objfile);
7968 }
7969
7970 /* Do we need to create a new group, or can we use an existing one? */
7971
7972 if (stmt_list)
7973 {
7974 line_offset = DW_UNSND (stmt_list);
7975 ++tu_stats->nr_symtab_sharers;
7976 }
7977 else
7978 {
7979 /* Ugh, no stmt_list. Rare, but we have to handle it.
7980 We can do various things here like create one group per TU or
7981 spread them over multiple groups to split up the expansion work.
7982 To avoid worst case scenarios (too many groups or too large groups)
7983 we, umm, group them in bunches. */
7984 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7985 | (tu_stats->nr_stmt_less_type_units
7986 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7987 ++tu_stats->nr_stmt_less_type_units;
7988 }
7989
7990 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7991 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7992 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
7993 &type_unit_group_for_lookup, INSERT);
7994 if (*slot != NULL)
7995 {
7996 tu_group = (struct type_unit_group *) *slot;
7997 gdb_assert (tu_group != NULL);
7998 }
7999 else
8000 {
8001 sect_offset line_offset_struct = (sect_offset) line_offset;
8002 tu_group = create_type_unit_group (cu, line_offset_struct);
8003 *slot = tu_group;
8004 ++tu_stats->nr_symtabs;
8005 }
8006
8007 return tu_group;
8008 }
8009 \f
8010 /* Partial symbol tables. */
8011
8012 /* Create a psymtab named NAME and assign it to PER_CU.
8013
8014 The caller must fill in the following details:
8015 dirname, textlow, texthigh. */
8016
8017 static dwarf2_psymtab *
8018 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
8019 {
8020 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
8021 dwarf2_psymtab *pst;
8022
8023 pst = new dwarf2_psymtab (name, objfile, 0);
8024
8025 pst->psymtabs_addrmap_supported = true;
8026
8027 /* This is the glue that links PST into GDB's symbol API. */
8028 pst->per_cu_data = per_cu;
8029 per_cu->v.psymtab = pst;
8030
8031 return pst;
8032 }
8033
8034 /* DIE reader function for process_psymtab_comp_unit. */
8035
8036 static void
8037 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
8038 const gdb_byte *info_ptr,
8039 struct die_info *comp_unit_die,
8040 int has_children,
8041 int want_partial_unit,
8042 enum language pretend_language)
8043 {
8044 struct dwarf2_cu *cu = reader->cu;
8045 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
8046 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8047 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8048 CORE_ADDR baseaddr;
8049 CORE_ADDR best_lowpc = 0, best_highpc = 0;
8050 dwarf2_psymtab *pst;
8051 enum pc_bounds_kind cu_bounds_kind;
8052 const char *filename;
8053
8054 if (comp_unit_die->tag == DW_TAG_partial_unit && !want_partial_unit)
8055 return;
8056
8057 gdb_assert (! per_cu->is_debug_types);
8058
8059 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
8060
8061 /* Allocate a new partial symbol table structure. */
8062 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
8063 if (filename == NULL)
8064 filename = "";
8065
8066 pst = create_partial_symtab (per_cu, filename);
8067
8068 /* This must be done before calling dwarf2_build_include_psymtabs. */
8069 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
8070
8071 baseaddr = objfile->text_section_offset ();
8072
8073 dwarf2_find_base_address (comp_unit_die, cu);
8074
8075 /* Possibly set the default values of LOWPC and HIGHPC from
8076 `DW_AT_ranges'. */
8077 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
8078 &best_highpc, cu, pst);
8079 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
8080 {
8081 CORE_ADDR low
8082 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
8083 - baseaddr);
8084 CORE_ADDR high
8085 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
8086 - baseaddr - 1);
8087 /* Store the contiguous range if it is not empty; it can be
8088 empty for CUs with no code. */
8089 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8090 low, high, pst);
8091 }
8092
8093 /* Check if comp unit has_children.
8094 If so, read the rest of the partial symbols from this comp unit.
8095 If not, there's no more debug_info for this comp unit. */
8096 if (has_children)
8097 {
8098 struct partial_die_info *first_die;
8099 CORE_ADDR lowpc, highpc;
8100
8101 lowpc = ((CORE_ADDR) -1);
8102 highpc = ((CORE_ADDR) 0);
8103
8104 first_die = load_partial_dies (reader, info_ptr, 1);
8105
8106 scan_partial_symbols (first_die, &lowpc, &highpc,
8107 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
8108
8109 /* If we didn't find a lowpc, set it to highpc to avoid
8110 complaints from `maint check'. */
8111 if (lowpc == ((CORE_ADDR) -1))
8112 lowpc = highpc;
8113
8114 /* If the compilation unit didn't have an explicit address range,
8115 then use the information extracted from its child dies. */
8116 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
8117 {
8118 best_lowpc = lowpc;
8119 best_highpc = highpc;
8120 }
8121 }
8122 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
8123 best_lowpc + baseaddr)
8124 - baseaddr);
8125 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
8126 best_highpc + baseaddr)
8127 - baseaddr);
8128
8129 end_psymtab_common (objfile, pst);
8130
8131 if (!cu->per_cu->imported_symtabs_empty ())
8132 {
8133 int i;
8134 int len = cu->per_cu->imported_symtabs_size ();
8135
8136 /* Fill in 'dependencies' here; we fill in 'users' in a
8137 post-pass. */
8138 pst->number_of_dependencies = len;
8139 pst->dependencies
8140 = objfile->partial_symtabs->allocate_dependencies (len);
8141 for (i = 0; i < len; ++i)
8142 {
8143 pst->dependencies[i]
8144 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
8145 }
8146
8147 cu->per_cu->imported_symtabs_free ();
8148 }
8149
8150 /* Get the list of files included in the current compilation unit,
8151 and build a psymtab for each of them. */
8152 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
8153
8154 if (dwarf_read_debug)
8155 fprintf_unfiltered (gdb_stdlog,
8156 "Psymtab for %s unit @%s: %s - %s"
8157 ", %d global, %d static syms\n",
8158 per_cu->is_debug_types ? "type" : "comp",
8159 sect_offset_str (per_cu->sect_off),
8160 paddress (gdbarch, pst->text_low (objfile)),
8161 paddress (gdbarch, pst->text_high (objfile)),
8162 pst->n_global_syms, pst->n_static_syms);
8163 }
8164
8165 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8166 Process compilation unit THIS_CU for a psymtab. */
8167
8168 static void
8169 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
8170 int want_partial_unit,
8171 enum language pretend_language)
8172 {
8173 /* If this compilation unit was already read in, free the
8174 cached copy in order to read it in again. This is
8175 necessary because we skipped some symbols when we first
8176 read in the compilation unit (see load_partial_dies).
8177 This problem could be avoided, but the benefit is unclear. */
8178 if (this_cu->cu != NULL)
8179 free_one_cached_comp_unit (this_cu);
8180
8181 cutu_reader reader (this_cu, NULL, 0, 0, false);
8182
8183 if (reader.dummy_p)
8184 {
8185 /* Nothing. */
8186 }
8187 else if (this_cu->is_debug_types)
8188 build_type_psymtabs_reader (&reader, reader.info_ptr, reader.comp_unit_die,
8189 reader.has_children);
8190 else
8191 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
8192 reader.comp_unit_die,
8193 reader.has_children,
8194 want_partial_unit,
8195 pretend_language);
8196
8197 /* Age out any secondary CUs. */
8198 age_cached_comp_units (this_cu->dwarf2_per_objfile);
8199 }
8200
8201 /* Reader function for build_type_psymtabs. */
8202
8203 static void
8204 build_type_psymtabs_reader (const struct die_reader_specs *reader,
8205 const gdb_byte *info_ptr,
8206 struct die_info *type_unit_die,
8207 int has_children)
8208 {
8209 struct dwarf2_per_objfile *dwarf2_per_objfile
8210 = reader->cu->per_cu->dwarf2_per_objfile;
8211 struct objfile *objfile = dwarf2_per_objfile->objfile;
8212 struct dwarf2_cu *cu = reader->cu;
8213 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8214 struct signatured_type *sig_type;
8215 struct type_unit_group *tu_group;
8216 struct attribute *attr;
8217 struct partial_die_info *first_die;
8218 CORE_ADDR lowpc, highpc;
8219 dwarf2_psymtab *pst;
8220
8221 gdb_assert (per_cu->is_debug_types);
8222 sig_type = (struct signatured_type *) per_cu;
8223
8224 if (! has_children)
8225 return;
8226
8227 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
8228 tu_group = get_type_unit_group (cu, attr);
8229
8230 if (tu_group->tus == nullptr)
8231 tu_group->tus = new std::vector<signatured_type *>;
8232 tu_group->tus->push_back (sig_type);
8233
8234 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
8235 pst = create_partial_symtab (per_cu, "");
8236 pst->anonymous = true;
8237
8238 first_die = load_partial_dies (reader, info_ptr, 1);
8239
8240 lowpc = (CORE_ADDR) -1;
8241 highpc = (CORE_ADDR) 0;
8242 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
8243
8244 end_psymtab_common (objfile, pst);
8245 }
8246
8247 /* Struct used to sort TUs by their abbreviation table offset. */
8248
8249 struct tu_abbrev_offset
8250 {
8251 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
8252 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
8253 {}
8254
8255 signatured_type *sig_type;
8256 sect_offset abbrev_offset;
8257 };
8258
8259 /* Helper routine for build_type_psymtabs_1, passed to std::sort. */
8260
8261 static bool
8262 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
8263 const struct tu_abbrev_offset &b)
8264 {
8265 return a.abbrev_offset < b.abbrev_offset;
8266 }
8267
8268 /* Efficiently read all the type units.
8269 This does the bulk of the work for build_type_psymtabs.
8270
8271 The efficiency is because we sort TUs by the abbrev table they use and
8272 only read each abbrev table once. In one program there are 200K TUs
8273 sharing 8K abbrev tables.
8274
8275 The main purpose of this function is to support building the
8276 dwarf2_per_objfile->type_unit_groups table.
8277 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
8278 can collapse the search space by grouping them by stmt_list.
8279 The savings can be significant, in the same program from above the 200K TUs
8280 share 8K stmt_list tables.
8281
8282 FUNC is expected to call get_type_unit_group, which will create the
8283 struct type_unit_group if necessary and add it to
8284 dwarf2_per_objfile->type_unit_groups. */
8285
8286 static void
8287 build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
8288 {
8289 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8290 abbrev_table_up abbrev_table;
8291 sect_offset abbrev_offset;
8292
8293 /* It's up to the caller to not call us multiple times. */
8294 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
8295
8296 if (dwarf2_per_objfile->all_type_units.empty ())
8297 return;
8298
8299 /* TUs typically share abbrev tables, and there can be way more TUs than
8300 abbrev tables. Sort by abbrev table to reduce the number of times we
8301 read each abbrev table in.
8302 Alternatives are to punt or to maintain a cache of abbrev tables.
8303 This is simpler and efficient enough for now.
8304
8305 Later we group TUs by their DW_AT_stmt_list value (as this defines the
8306 symtab to use). Typically TUs with the same abbrev offset have the same
8307 stmt_list value too so in practice this should work well.
8308
8309 The basic algorithm here is:
8310
8311 sort TUs by abbrev table
8312 for each TU with same abbrev table:
8313 read abbrev table if first user
8314 read TU top level DIE
8315 [IWBN if DWO skeletons had DW_AT_stmt_list]
8316 call FUNC */
8317
8318 if (dwarf_read_debug)
8319 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
8320
8321 /* Sort in a separate table to maintain the order of all_type_units
8322 for .gdb_index: TU indices directly index all_type_units. */
8323 std::vector<tu_abbrev_offset> sorted_by_abbrev;
8324 sorted_by_abbrev.reserve (dwarf2_per_objfile->all_type_units.size ());
8325
8326 for (signatured_type *sig_type : dwarf2_per_objfile->all_type_units)
8327 sorted_by_abbrev.emplace_back
8328 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
8329 sig_type->per_cu.section,
8330 sig_type->per_cu.sect_off));
8331
8332 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
8333 sort_tu_by_abbrev_offset);
8334
8335 abbrev_offset = (sect_offset) ~(unsigned) 0;
8336
8337 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
8338 {
8339 /* Switch to the next abbrev table if necessary. */
8340 if (abbrev_table == NULL
8341 || tu.abbrev_offset != abbrev_offset)
8342 {
8343 abbrev_offset = tu.abbrev_offset;
8344 abbrev_table =
8345 abbrev_table_read_table (dwarf2_per_objfile,
8346 &dwarf2_per_objfile->abbrev,
8347 abbrev_offset);
8348 ++tu_stats->nr_uniq_abbrev_tables;
8349 }
8350
8351 cutu_reader reader (&tu.sig_type->per_cu, abbrev_table.get (),
8352 0, 0, false);
8353 if (!reader.dummy_p)
8354 build_type_psymtabs_reader (&reader, reader.info_ptr,
8355 reader.comp_unit_die,
8356 reader.has_children);
8357 }
8358 }
8359
8360 /* Print collected type unit statistics. */
8361
8362 static void
8363 print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
8364 {
8365 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8366
8367 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
8368 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
8369 dwarf2_per_objfile->all_type_units.size ());
8370 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
8371 tu_stats->nr_uniq_abbrev_tables);
8372 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
8373 tu_stats->nr_symtabs);
8374 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
8375 tu_stats->nr_symtab_sharers);
8376 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
8377 tu_stats->nr_stmt_less_type_units);
8378 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
8379 tu_stats->nr_all_type_units_reallocs);
8380 }
8381
8382 /* Traversal function for build_type_psymtabs. */
8383
8384 static int
8385 build_type_psymtab_dependencies (void **slot, void *info)
8386 {
8387 struct dwarf2_per_objfile *dwarf2_per_objfile
8388 = (struct dwarf2_per_objfile *) info;
8389 struct objfile *objfile = dwarf2_per_objfile->objfile;
8390 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
8391 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
8392 dwarf2_psymtab *pst = per_cu->v.psymtab;
8393 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
8394 int i;
8395
8396 gdb_assert (len > 0);
8397 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
8398
8399 pst->number_of_dependencies = len;
8400 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
8401 for (i = 0; i < len; ++i)
8402 {
8403 struct signatured_type *iter = tu_group->tus->at (i);
8404 gdb_assert (iter->per_cu.is_debug_types);
8405 pst->dependencies[i] = iter->per_cu.v.psymtab;
8406 iter->type_unit_group = tu_group;
8407 }
8408
8409 delete tu_group->tus;
8410 tu_group->tus = nullptr;
8411
8412 return 1;
8413 }
8414
8415 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8416 Build partial symbol tables for the .debug_types comp-units. */
8417
8418 static void
8419 build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
8420 {
8421 if (! create_all_type_units (dwarf2_per_objfile))
8422 return;
8423
8424 build_type_psymtabs_1 (dwarf2_per_objfile);
8425 }
8426
8427 /* Traversal function for process_skeletonless_type_unit.
8428 Read a TU in a DWO file and build partial symbols for it. */
8429
8430 static int
8431 process_skeletonless_type_unit (void **slot, void *info)
8432 {
8433 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
8434 struct dwarf2_per_objfile *dwarf2_per_objfile
8435 = (struct dwarf2_per_objfile *) info;
8436 struct signatured_type find_entry, *entry;
8437
8438 /* If this TU doesn't exist in the global table, add it and read it in. */
8439
8440 if (dwarf2_per_objfile->signatured_types == NULL)
8441 {
8442 dwarf2_per_objfile->signatured_types
8443 = allocate_signatured_type_table (dwarf2_per_objfile->objfile);
8444 }
8445
8446 find_entry.signature = dwo_unit->signature;
8447 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
8448 INSERT);
8449 /* If we've already seen this type there's nothing to do. What's happening
8450 is we're doing our own version of comdat-folding here. */
8451 if (*slot != NULL)
8452 return 1;
8453
8454 /* This does the job that create_all_type_units would have done for
8455 this TU. */
8456 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
8457 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
8458 *slot = entry;
8459
8460 /* This does the job that build_type_psymtabs_1 would have done. */
8461 cutu_reader reader (&entry->per_cu, NULL, 0, 0, false);
8462 if (!reader.dummy_p)
8463 build_type_psymtabs_reader (&reader, reader.info_ptr,
8464 reader.comp_unit_die, reader.has_children);
8465
8466 return 1;
8467 }
8468
8469 /* Traversal function for process_skeletonless_type_units. */
8470
8471 static int
8472 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8473 {
8474 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8475
8476 if (dwo_file->tus != NULL)
8477 {
8478 htab_traverse_noresize (dwo_file->tus,
8479 process_skeletonless_type_unit, info);
8480 }
8481
8482 return 1;
8483 }
8484
8485 /* Scan all TUs of DWO files, verifying we've processed them.
8486 This is needed in case a TU was emitted without its skeleton.
8487 Note: This can't be done until we know what all the DWO files are. */
8488
8489 static void
8490 process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
8491 {
8492 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
8493 if (get_dwp_file (dwarf2_per_objfile) == NULL
8494 && dwarf2_per_objfile->dwo_files != NULL)
8495 {
8496 htab_traverse_noresize (dwarf2_per_objfile->dwo_files.get (),
8497 process_dwo_file_for_skeletonless_type_units,
8498 dwarf2_per_objfile);
8499 }
8500 }
8501
8502 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
8503
8504 static void
8505 set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
8506 {
8507 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
8508 {
8509 dwarf2_psymtab *pst = per_cu->v.psymtab;
8510
8511 if (pst == NULL)
8512 continue;
8513
8514 for (int j = 0; j < pst->number_of_dependencies; ++j)
8515 {
8516 /* Set the 'user' field only if it is not already set. */
8517 if (pst->dependencies[j]->user == NULL)
8518 pst->dependencies[j]->user = pst;
8519 }
8520 }
8521 }
8522
8523 /* Build the partial symbol table by doing a quick pass through the
8524 .debug_info and .debug_abbrev sections. */
8525
8526 static void
8527 dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
8528 {
8529 struct objfile *objfile = dwarf2_per_objfile->objfile;
8530
8531 if (dwarf_read_debug)
8532 {
8533 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
8534 objfile_name (objfile));
8535 }
8536
8537 dwarf2_per_objfile->reading_partial_symbols = 1;
8538
8539 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
8540
8541 /* Any cached compilation units will be linked by the per-objfile
8542 read_in_chain. Make sure to free them when we're done. */
8543 free_cached_comp_units freer (dwarf2_per_objfile);
8544
8545 build_type_psymtabs (dwarf2_per_objfile);
8546
8547 create_all_comp_units (dwarf2_per_objfile);
8548
8549 /* Create a temporary address map on a temporary obstack. We later
8550 copy this to the final obstack. */
8551 auto_obstack temp_obstack;
8552
8553 scoped_restore save_psymtabs_addrmap
8554 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
8555 addrmap_create_mutable (&temp_obstack));
8556
8557 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
8558 process_psymtab_comp_unit (per_cu, 0, language_minimal);
8559
8560 /* This has to wait until we read the CUs, we need the list of DWOs. */
8561 process_skeletonless_type_units (dwarf2_per_objfile);
8562
8563 /* Now that all TUs have been processed we can fill in the dependencies. */
8564 if (dwarf2_per_objfile->type_unit_groups != NULL)
8565 {
8566 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
8567 build_type_psymtab_dependencies, dwarf2_per_objfile);
8568 }
8569
8570 if (dwarf_read_debug)
8571 print_tu_stats (dwarf2_per_objfile);
8572
8573 set_partial_user (dwarf2_per_objfile);
8574
8575 objfile->partial_symtabs->psymtabs_addrmap
8576 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
8577 objfile->partial_symtabs->obstack ());
8578 /* At this point we want to keep the address map. */
8579 save_psymtabs_addrmap.release ();
8580
8581 if (dwarf_read_debug)
8582 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
8583 objfile_name (objfile));
8584 }
8585
8586 /* Load the partial DIEs for a secondary CU into memory.
8587 This is also used when rereading a primary CU with load_all_dies. */
8588
8589 static void
8590 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
8591 {
8592 cutu_reader reader (this_cu, NULL, 1, 1, false);
8593
8594 if (!reader.dummy_p)
8595 {
8596 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
8597 language_minimal);
8598
8599 /* Check if comp unit has_children.
8600 If so, read the rest of the partial symbols from this comp unit.
8601 If not, there's no more debug_info for this comp unit. */
8602 if (reader.has_children)
8603 load_partial_dies (&reader, reader.info_ptr, 0);
8604 }
8605 }
8606
8607 static void
8608 read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
8609 struct dwarf2_section_info *section,
8610 struct dwarf2_section_info *abbrev_section,
8611 unsigned int is_dwz)
8612 {
8613 const gdb_byte *info_ptr;
8614 struct objfile *objfile = dwarf2_per_objfile->objfile;
8615
8616 if (dwarf_read_debug)
8617 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
8618 get_section_name (section),
8619 get_section_file_name (section));
8620
8621 dwarf2_read_section (objfile, section);
8622
8623 info_ptr = section->buffer;
8624
8625 while (info_ptr < section->buffer + section->size)
8626 {
8627 struct dwarf2_per_cu_data *this_cu;
8628
8629 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
8630
8631 comp_unit_head cu_header;
8632 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
8633 abbrev_section, info_ptr,
8634 rcuh_kind::COMPILE);
8635
8636 /* Save the compilation unit for later lookup. */
8637 if (cu_header.unit_type != DW_UT_type)
8638 {
8639 this_cu = XOBNEW (&objfile->objfile_obstack,
8640 struct dwarf2_per_cu_data);
8641 memset (this_cu, 0, sizeof (*this_cu));
8642 }
8643 else
8644 {
8645 auto sig_type = XOBNEW (&objfile->objfile_obstack,
8646 struct signatured_type);
8647 memset (sig_type, 0, sizeof (*sig_type));
8648 sig_type->signature = cu_header.signature;
8649 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8650 this_cu = &sig_type->per_cu;
8651 }
8652 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
8653 this_cu->sect_off = sect_off;
8654 this_cu->length = cu_header.length + cu_header.initial_length_size;
8655 this_cu->is_dwz = is_dwz;
8656 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8657 this_cu->section = section;
8658
8659 dwarf2_per_objfile->all_comp_units.push_back (this_cu);
8660
8661 info_ptr = info_ptr + this_cu->length;
8662 }
8663 }
8664
8665 /* Create a list of all compilation units in OBJFILE.
8666 This is only done for -readnow and building partial symtabs. */
8667
8668 static void
8669 create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
8670 {
8671 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
8672 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
8673 &dwarf2_per_objfile->abbrev, 0);
8674
8675 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
8676 if (dwz != NULL)
8677 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
8678 1);
8679 }
8680
8681 /* Process all loaded DIEs for compilation unit CU, starting at
8682 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
8683 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
8684 DW_AT_ranges). See the comments of add_partial_subprogram on how
8685 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
8686
8687 static void
8688 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
8689 CORE_ADDR *highpc, int set_addrmap,
8690 struct dwarf2_cu *cu)
8691 {
8692 struct partial_die_info *pdi;
8693
8694 /* Now, march along the PDI's, descending into ones which have
8695 interesting children but skipping the children of the other ones,
8696 until we reach the end of the compilation unit. */
8697
8698 pdi = first_die;
8699
8700 while (pdi != NULL)
8701 {
8702 pdi->fixup (cu);
8703
8704 /* Anonymous namespaces or modules have no name but have interesting
8705 children, so we need to look at them. Ditto for anonymous
8706 enums. */
8707
8708 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
8709 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
8710 || pdi->tag == DW_TAG_imported_unit
8711 || pdi->tag == DW_TAG_inlined_subroutine)
8712 {
8713 switch (pdi->tag)
8714 {
8715 case DW_TAG_subprogram:
8716 case DW_TAG_inlined_subroutine:
8717 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8718 break;
8719 case DW_TAG_constant:
8720 case DW_TAG_variable:
8721 case DW_TAG_typedef:
8722 case DW_TAG_union_type:
8723 if (!pdi->is_declaration)
8724 {
8725 add_partial_symbol (pdi, cu);
8726 }
8727 break;
8728 case DW_TAG_class_type:
8729 case DW_TAG_interface_type:
8730 case DW_TAG_structure_type:
8731 if (!pdi->is_declaration)
8732 {
8733 add_partial_symbol (pdi, cu);
8734 }
8735 if ((cu->language == language_rust
8736 || cu->language == language_cplus) && pdi->has_children)
8737 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8738 set_addrmap, cu);
8739 break;
8740 case DW_TAG_enumeration_type:
8741 if (!pdi->is_declaration)
8742 add_partial_enumeration (pdi, cu);
8743 break;
8744 case DW_TAG_base_type:
8745 case DW_TAG_subrange_type:
8746 /* File scope base type definitions are added to the partial
8747 symbol table. */
8748 add_partial_symbol (pdi, cu);
8749 break;
8750 case DW_TAG_namespace:
8751 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
8752 break;
8753 case DW_TAG_module:
8754 if (!pdi->is_declaration)
8755 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
8756 break;
8757 case DW_TAG_imported_unit:
8758 {
8759 struct dwarf2_per_cu_data *per_cu;
8760
8761 /* For now we don't handle imported units in type units. */
8762 if (cu->per_cu->is_debug_types)
8763 {
8764 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8765 " supported in type units [in module %s]"),
8766 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
8767 }
8768
8769 per_cu = dwarf2_find_containing_comp_unit
8770 (pdi->d.sect_off, pdi->is_dwz,
8771 cu->per_cu->dwarf2_per_objfile);
8772
8773 /* Go read the partial unit, if needed. */
8774 if (per_cu->v.psymtab == NULL)
8775 process_psymtab_comp_unit (per_cu, 1, cu->language);
8776
8777 cu->per_cu->imported_symtabs_push (per_cu);
8778 }
8779 break;
8780 case DW_TAG_imported_declaration:
8781 add_partial_symbol (pdi, cu);
8782 break;
8783 default:
8784 break;
8785 }
8786 }
8787
8788 /* If the die has a sibling, skip to the sibling. */
8789
8790 pdi = pdi->die_sibling;
8791 }
8792 }
8793
8794 /* Functions used to compute the fully scoped name of a partial DIE.
8795
8796 Normally, this is simple. For C++, the parent DIE's fully scoped
8797 name is concatenated with "::" and the partial DIE's name.
8798 Enumerators are an exception; they use the scope of their parent
8799 enumeration type, i.e. the name of the enumeration type is not
8800 prepended to the enumerator.
8801
8802 There are two complexities. One is DW_AT_specification; in this
8803 case "parent" means the parent of the target of the specification,
8804 instead of the direct parent of the DIE. The other is compilers
8805 which do not emit DW_TAG_namespace; in this case we try to guess
8806 the fully qualified name of structure types from their members'
8807 linkage names. This must be done using the DIE's children rather
8808 than the children of any DW_AT_specification target. We only need
8809 to do this for structures at the top level, i.e. if the target of
8810 any DW_AT_specification (if any; otherwise the DIE itself) does not
8811 have a parent. */
8812
8813 /* Compute the scope prefix associated with PDI's parent, in
8814 compilation unit CU. The result will be allocated on CU's
8815 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8816 field. NULL is returned if no prefix is necessary. */
8817 static const char *
8818 partial_die_parent_scope (struct partial_die_info *pdi,
8819 struct dwarf2_cu *cu)
8820 {
8821 const char *grandparent_scope;
8822 struct partial_die_info *parent, *real_pdi;
8823
8824 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8825 then this means the parent of the specification DIE. */
8826
8827 real_pdi = pdi;
8828 while (real_pdi->has_specification)
8829 {
8830 auto res = find_partial_die (real_pdi->spec_offset,
8831 real_pdi->spec_is_dwz, cu);
8832 real_pdi = res.pdi;
8833 cu = res.cu;
8834 }
8835
8836 parent = real_pdi->die_parent;
8837 if (parent == NULL)
8838 return NULL;
8839
8840 if (parent->scope_set)
8841 return parent->scope;
8842
8843 parent->fixup (cu);
8844
8845 grandparent_scope = partial_die_parent_scope (parent, cu);
8846
8847 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8848 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8849 Work around this problem here. */
8850 if (cu->language == language_cplus
8851 && parent->tag == DW_TAG_namespace
8852 && strcmp (parent->name, "::") == 0
8853 && grandparent_scope == NULL)
8854 {
8855 parent->scope = NULL;
8856 parent->scope_set = 1;
8857 return NULL;
8858 }
8859
8860 /* Nested subroutines in Fortran get a prefix. */
8861 if (pdi->tag == DW_TAG_enumerator)
8862 /* Enumerators should not get the name of the enumeration as a prefix. */
8863 parent->scope = grandparent_scope;
8864 else if (parent->tag == DW_TAG_namespace
8865 || parent->tag == DW_TAG_module
8866 || parent->tag == DW_TAG_structure_type
8867 || parent->tag == DW_TAG_class_type
8868 || parent->tag == DW_TAG_interface_type
8869 || parent->tag == DW_TAG_union_type
8870 || parent->tag == DW_TAG_enumeration_type
8871 || (cu->language == language_fortran
8872 && parent->tag == DW_TAG_subprogram
8873 && pdi->tag == DW_TAG_subprogram))
8874 {
8875 if (grandparent_scope == NULL)
8876 parent->scope = parent->name;
8877 else
8878 parent->scope = typename_concat (&cu->comp_unit_obstack,
8879 grandparent_scope,
8880 parent->name, 0, cu);
8881 }
8882 else
8883 {
8884 /* FIXME drow/2004-04-01: What should we be doing with
8885 function-local names? For partial symbols, we should probably be
8886 ignoring them. */
8887 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8888 dwarf_tag_name (parent->tag),
8889 sect_offset_str (pdi->sect_off));
8890 parent->scope = grandparent_scope;
8891 }
8892
8893 parent->scope_set = 1;
8894 return parent->scope;
8895 }
8896
8897 /* Return the fully scoped name associated with PDI, from compilation unit
8898 CU. The result will be allocated with malloc. */
8899
8900 static gdb::unique_xmalloc_ptr<char>
8901 partial_die_full_name (struct partial_die_info *pdi,
8902 struct dwarf2_cu *cu)
8903 {
8904 const char *parent_scope;
8905
8906 /* If this is a template instantiation, we can not work out the
8907 template arguments from partial DIEs. So, unfortunately, we have
8908 to go through the full DIEs. At least any work we do building
8909 types here will be reused if full symbols are loaded later. */
8910 if (pdi->has_template_arguments)
8911 {
8912 pdi->fixup (cu);
8913
8914 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8915 {
8916 struct die_info *die;
8917 struct attribute attr;
8918 struct dwarf2_cu *ref_cu = cu;
8919
8920 /* DW_FORM_ref_addr is using section offset. */
8921 attr.name = (enum dwarf_attribute) 0;
8922 attr.form = DW_FORM_ref_addr;
8923 attr.u.unsnd = to_underlying (pdi->sect_off);
8924 die = follow_die_ref (NULL, &attr, &ref_cu);
8925
8926 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8927 }
8928 }
8929
8930 parent_scope = partial_die_parent_scope (pdi, cu);
8931 if (parent_scope == NULL)
8932 return NULL;
8933 else
8934 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8935 pdi->name, 0, cu));
8936 }
8937
8938 static void
8939 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8940 {
8941 struct dwarf2_per_objfile *dwarf2_per_objfile
8942 = cu->per_cu->dwarf2_per_objfile;
8943 struct objfile *objfile = dwarf2_per_objfile->objfile;
8944 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8945 CORE_ADDR addr = 0;
8946 const char *actual_name = NULL;
8947 CORE_ADDR baseaddr;
8948
8949 baseaddr = objfile->text_section_offset ();
8950
8951 gdb::unique_xmalloc_ptr<char> built_actual_name
8952 = partial_die_full_name (pdi, cu);
8953 if (built_actual_name != NULL)
8954 actual_name = built_actual_name.get ();
8955
8956 if (actual_name == NULL)
8957 actual_name = pdi->name;
8958
8959 switch (pdi->tag)
8960 {
8961 case DW_TAG_inlined_subroutine:
8962 case DW_TAG_subprogram:
8963 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8964 - baseaddr);
8965 if (pdi->is_external
8966 || cu->language == language_ada
8967 || (cu->language == language_fortran
8968 && pdi->die_parent != NULL
8969 && pdi->die_parent->tag == DW_TAG_subprogram))
8970 {
8971 /* Normally, only "external" DIEs are part of the global scope.
8972 But in Ada and Fortran, we want to be able to access nested
8973 procedures globally. So all Ada and Fortran subprograms are
8974 stored in the global scope. */
8975 add_psymbol_to_list (actual_name,
8976 built_actual_name != NULL,
8977 VAR_DOMAIN, LOC_BLOCK,
8978 SECT_OFF_TEXT (objfile),
8979 psymbol_placement::GLOBAL,
8980 addr,
8981 cu->language, objfile);
8982 }
8983 else
8984 {
8985 add_psymbol_to_list (actual_name,
8986 built_actual_name != NULL,
8987 VAR_DOMAIN, LOC_BLOCK,
8988 SECT_OFF_TEXT (objfile),
8989 psymbol_placement::STATIC,
8990 addr, cu->language, objfile);
8991 }
8992
8993 if (pdi->main_subprogram && actual_name != NULL)
8994 set_objfile_main_name (objfile, actual_name, cu->language);
8995 break;
8996 case DW_TAG_constant:
8997 add_psymbol_to_list (actual_name,
8998 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
8999 -1, (pdi->is_external
9000 ? psymbol_placement::GLOBAL
9001 : psymbol_placement::STATIC),
9002 0, cu->language, objfile);
9003 break;
9004 case DW_TAG_variable:
9005 if (pdi->d.locdesc)
9006 addr = decode_locdesc (pdi->d.locdesc, cu);
9007
9008 if (pdi->d.locdesc
9009 && addr == 0
9010 && !dwarf2_per_objfile->has_section_at_zero)
9011 {
9012 /* A global or static variable may also have been stripped
9013 out by the linker if unused, in which case its address
9014 will be nullified; do not add such variables into partial
9015 symbol table then. */
9016 }
9017 else if (pdi->is_external)
9018 {
9019 /* Global Variable.
9020 Don't enter into the minimal symbol tables as there is
9021 a minimal symbol table entry from the ELF symbols already.
9022 Enter into partial symbol table if it has a location
9023 descriptor or a type.
9024 If the location descriptor is missing, new_symbol will create
9025 a LOC_UNRESOLVED symbol, the address of the variable will then
9026 be determined from the minimal symbol table whenever the variable
9027 is referenced.
9028 The address for the partial symbol table entry is not
9029 used by GDB, but it comes in handy for debugging partial symbol
9030 table building. */
9031
9032 if (pdi->d.locdesc || pdi->has_type)
9033 add_psymbol_to_list (actual_name,
9034 built_actual_name != NULL,
9035 VAR_DOMAIN, LOC_STATIC,
9036 SECT_OFF_TEXT (objfile),
9037 psymbol_placement::GLOBAL,
9038 addr, cu->language, objfile);
9039 }
9040 else
9041 {
9042 int has_loc = pdi->d.locdesc != NULL;
9043
9044 /* Static Variable. Skip symbols whose value we cannot know (those
9045 without location descriptors or constant values). */
9046 if (!has_loc && !pdi->has_const_value)
9047 return;
9048
9049 add_psymbol_to_list (actual_name,
9050 built_actual_name != NULL,
9051 VAR_DOMAIN, LOC_STATIC,
9052 SECT_OFF_TEXT (objfile),
9053 psymbol_placement::STATIC,
9054 has_loc ? addr : 0,
9055 cu->language, objfile);
9056 }
9057 break;
9058 case DW_TAG_typedef:
9059 case DW_TAG_base_type:
9060 case DW_TAG_subrange_type:
9061 add_psymbol_to_list (actual_name,
9062 built_actual_name != NULL,
9063 VAR_DOMAIN, LOC_TYPEDEF, -1,
9064 psymbol_placement::STATIC,
9065 0, cu->language, objfile);
9066 break;
9067 case DW_TAG_imported_declaration:
9068 case DW_TAG_namespace:
9069 add_psymbol_to_list (actual_name,
9070 built_actual_name != NULL,
9071 VAR_DOMAIN, LOC_TYPEDEF, -1,
9072 psymbol_placement::GLOBAL,
9073 0, cu->language, objfile);
9074 break;
9075 case DW_TAG_module:
9076 /* With Fortran 77 there might be a "BLOCK DATA" module
9077 available without any name. If so, we skip the module as it
9078 doesn't bring any value. */
9079 if (actual_name != nullptr)
9080 add_psymbol_to_list (actual_name,
9081 built_actual_name != NULL,
9082 MODULE_DOMAIN, LOC_TYPEDEF, -1,
9083 psymbol_placement::GLOBAL,
9084 0, cu->language, objfile);
9085 break;
9086 case DW_TAG_class_type:
9087 case DW_TAG_interface_type:
9088 case DW_TAG_structure_type:
9089 case DW_TAG_union_type:
9090 case DW_TAG_enumeration_type:
9091 /* Skip external references. The DWARF standard says in the section
9092 about "Structure, Union, and Class Type Entries": "An incomplete
9093 structure, union or class type is represented by a structure,
9094 union or class entry that does not have a byte size attribute
9095 and that has a DW_AT_declaration attribute." */
9096 if (!pdi->has_byte_size && pdi->is_declaration)
9097 return;
9098
9099 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
9100 static vs. global. */
9101 add_psymbol_to_list (actual_name,
9102 built_actual_name != NULL,
9103 STRUCT_DOMAIN, LOC_TYPEDEF, -1,
9104 cu->language == language_cplus
9105 ? psymbol_placement::GLOBAL
9106 : psymbol_placement::STATIC,
9107 0, cu->language, objfile);
9108
9109 break;
9110 case DW_TAG_enumerator:
9111 add_psymbol_to_list (actual_name,
9112 built_actual_name != NULL,
9113 VAR_DOMAIN, LOC_CONST, -1,
9114 cu->language == language_cplus
9115 ? psymbol_placement::GLOBAL
9116 : psymbol_placement::STATIC,
9117 0, cu->language, objfile);
9118 break;
9119 default:
9120 break;
9121 }
9122 }
9123
9124 /* Read a partial die corresponding to a namespace; also, add a symbol
9125 corresponding to that namespace to the symbol table. NAMESPACE is
9126 the name of the enclosing namespace. */
9127
9128 static void
9129 add_partial_namespace (struct partial_die_info *pdi,
9130 CORE_ADDR *lowpc, CORE_ADDR *highpc,
9131 int set_addrmap, struct dwarf2_cu *cu)
9132 {
9133 /* Add a symbol for the namespace. */
9134
9135 add_partial_symbol (pdi, cu);
9136
9137 /* Now scan partial symbols in that namespace. */
9138
9139 if (pdi->has_children)
9140 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
9141 }
9142
9143 /* Read a partial die corresponding to a Fortran module. */
9144
9145 static void
9146 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
9147 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
9148 {
9149 /* Add a symbol for the namespace. */
9150
9151 add_partial_symbol (pdi, cu);
9152
9153 /* Now scan partial symbols in that module. */
9154
9155 if (pdi->has_children)
9156 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
9157 }
9158
9159 /* Read a partial die corresponding to a subprogram or an inlined
9160 subprogram and create a partial symbol for that subprogram.
9161 When the CU language allows it, this routine also defines a partial
9162 symbol for each nested subprogram that this subprogram contains.
9163 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
9164 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
9165
9166 PDI may also be a lexical block, in which case we simply search
9167 recursively for subprograms defined inside that lexical block.
9168 Again, this is only performed when the CU language allows this
9169 type of definitions. */
9170
9171 static void
9172 add_partial_subprogram (struct partial_die_info *pdi,
9173 CORE_ADDR *lowpc, CORE_ADDR *highpc,
9174 int set_addrmap, struct dwarf2_cu *cu)
9175 {
9176 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
9177 {
9178 if (pdi->has_pc_info)
9179 {
9180 if (pdi->lowpc < *lowpc)
9181 *lowpc = pdi->lowpc;
9182 if (pdi->highpc > *highpc)
9183 *highpc = pdi->highpc;
9184 if (set_addrmap)
9185 {
9186 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9187 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9188 CORE_ADDR baseaddr;
9189 CORE_ADDR this_highpc;
9190 CORE_ADDR this_lowpc;
9191
9192 baseaddr = objfile->text_section_offset ();
9193 this_lowpc
9194 = (gdbarch_adjust_dwarf2_addr (gdbarch,
9195 pdi->lowpc + baseaddr)
9196 - baseaddr);
9197 this_highpc
9198 = (gdbarch_adjust_dwarf2_addr (gdbarch,
9199 pdi->highpc + baseaddr)
9200 - baseaddr);
9201 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
9202 this_lowpc, this_highpc - 1,
9203 cu->per_cu->v.psymtab);
9204 }
9205 }
9206
9207 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
9208 {
9209 if (!pdi->is_declaration)
9210 /* Ignore subprogram DIEs that do not have a name, they are
9211 illegal. Do not emit a complaint at this point, we will
9212 do so when we convert this psymtab into a symtab. */
9213 if (pdi->name)
9214 add_partial_symbol (pdi, cu);
9215 }
9216 }
9217
9218 if (! pdi->has_children)
9219 return;
9220
9221 if (cu->language == language_ada || cu->language == language_fortran)
9222 {
9223 pdi = pdi->die_child;
9224 while (pdi != NULL)
9225 {
9226 pdi->fixup (cu);
9227 if (pdi->tag == DW_TAG_subprogram
9228 || pdi->tag == DW_TAG_inlined_subroutine
9229 || pdi->tag == DW_TAG_lexical_block)
9230 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
9231 pdi = pdi->die_sibling;
9232 }
9233 }
9234 }
9235
9236 /* Read a partial die corresponding to an enumeration type. */
9237
9238 static void
9239 add_partial_enumeration (struct partial_die_info *enum_pdi,
9240 struct dwarf2_cu *cu)
9241 {
9242 struct partial_die_info *pdi;
9243
9244 if (enum_pdi->name != NULL)
9245 add_partial_symbol (enum_pdi, cu);
9246
9247 pdi = enum_pdi->die_child;
9248 while (pdi)
9249 {
9250 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
9251 complaint (_("malformed enumerator DIE ignored"));
9252 else
9253 add_partial_symbol (pdi, cu);
9254 pdi = pdi->die_sibling;
9255 }
9256 }
9257
9258 /* Return the initial uleb128 in the die at INFO_PTR. */
9259
9260 static unsigned int
9261 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
9262 {
9263 unsigned int bytes_read;
9264
9265 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9266 }
9267
9268 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
9269 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
9270
9271 Return the corresponding abbrev, or NULL if the number is zero (indicating
9272 an empty DIE). In either case *BYTES_READ will be set to the length of
9273 the initial number. */
9274
9275 static struct abbrev_info *
9276 peek_die_abbrev (const die_reader_specs &reader,
9277 const gdb_byte *info_ptr, unsigned int *bytes_read)
9278 {
9279 dwarf2_cu *cu = reader.cu;
9280 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
9281 unsigned int abbrev_number
9282 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
9283
9284 if (abbrev_number == 0)
9285 return NULL;
9286
9287 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
9288 if (!abbrev)
9289 {
9290 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9291 " at offset %s [in module %s]"),
9292 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9293 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
9294 }
9295
9296 return abbrev;
9297 }
9298
9299 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9300 Returns a pointer to the end of a series of DIEs, terminated by an empty
9301 DIE. Any children of the skipped DIEs will also be skipped. */
9302
9303 static const gdb_byte *
9304 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
9305 {
9306 while (1)
9307 {
9308 unsigned int bytes_read;
9309 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
9310
9311 if (abbrev == NULL)
9312 return info_ptr + bytes_read;
9313 else
9314 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
9315 }
9316 }
9317
9318 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9319 INFO_PTR should point just after the initial uleb128 of a DIE, and the
9320 abbrev corresponding to that skipped uleb128 should be passed in
9321 ABBREV. Returns a pointer to this DIE's sibling, skipping any
9322 children. */
9323
9324 static const gdb_byte *
9325 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
9326 struct abbrev_info *abbrev)
9327 {
9328 unsigned int bytes_read;
9329 struct attribute attr;
9330 bfd *abfd = reader->abfd;
9331 struct dwarf2_cu *cu = reader->cu;
9332 const gdb_byte *buffer = reader->buffer;
9333 const gdb_byte *buffer_end = reader->buffer_end;
9334 unsigned int form, i;
9335
9336 for (i = 0; i < abbrev->num_attrs; i++)
9337 {
9338 /* The only abbrev we care about is DW_AT_sibling. */
9339 if (abbrev->attrs[i].name == DW_AT_sibling)
9340 {
9341 bool ignored;
9342 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr,
9343 &ignored);
9344 if (attr.form == DW_FORM_ref_addr)
9345 complaint (_("ignoring absolute DW_AT_sibling"));
9346 else
9347 {
9348 sect_offset off = dwarf2_get_ref_die_offset (&attr);
9349 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
9350
9351 if (sibling_ptr < info_ptr)
9352 complaint (_("DW_AT_sibling points backwards"));
9353 else if (sibling_ptr > reader->buffer_end)
9354 dwarf2_section_buffer_overflow_complaint (reader->die_section);
9355 else
9356 return sibling_ptr;
9357 }
9358 }
9359
9360 /* If it isn't DW_AT_sibling, skip this attribute. */
9361 form = abbrev->attrs[i].form;
9362 skip_attribute:
9363 switch (form)
9364 {
9365 case DW_FORM_ref_addr:
9366 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
9367 and later it is offset sized. */
9368 if (cu->header.version == 2)
9369 info_ptr += cu->header.addr_size;
9370 else
9371 info_ptr += cu->header.offset_size;
9372 break;
9373 case DW_FORM_GNU_ref_alt:
9374 info_ptr += cu->header.offset_size;
9375 break;
9376 case DW_FORM_addr:
9377 info_ptr += cu->header.addr_size;
9378 break;
9379 case DW_FORM_data1:
9380 case DW_FORM_ref1:
9381 case DW_FORM_flag:
9382 case DW_FORM_strx1:
9383 info_ptr += 1;
9384 break;
9385 case DW_FORM_flag_present:
9386 case DW_FORM_implicit_const:
9387 break;
9388 case DW_FORM_data2:
9389 case DW_FORM_ref2:
9390 case DW_FORM_strx2:
9391 info_ptr += 2;
9392 break;
9393 case DW_FORM_strx3:
9394 info_ptr += 3;
9395 break;
9396 case DW_FORM_data4:
9397 case DW_FORM_ref4:
9398 case DW_FORM_strx4:
9399 info_ptr += 4;
9400 break;
9401 case DW_FORM_data8:
9402 case DW_FORM_ref8:
9403 case DW_FORM_ref_sig8:
9404 info_ptr += 8;
9405 break;
9406 case DW_FORM_data16:
9407 info_ptr += 16;
9408 break;
9409 case DW_FORM_string:
9410 read_direct_string (abfd, info_ptr, &bytes_read);
9411 info_ptr += bytes_read;
9412 break;
9413 case DW_FORM_sec_offset:
9414 case DW_FORM_strp:
9415 case DW_FORM_GNU_strp_alt:
9416 info_ptr += cu->header.offset_size;
9417 break;
9418 case DW_FORM_exprloc:
9419 case DW_FORM_block:
9420 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9421 info_ptr += bytes_read;
9422 break;
9423 case DW_FORM_block1:
9424 info_ptr += 1 + read_1_byte (abfd, info_ptr);
9425 break;
9426 case DW_FORM_block2:
9427 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
9428 break;
9429 case DW_FORM_block4:
9430 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
9431 break;
9432 case DW_FORM_addrx:
9433 case DW_FORM_strx:
9434 case DW_FORM_sdata:
9435 case DW_FORM_udata:
9436 case DW_FORM_ref_udata:
9437 case DW_FORM_GNU_addr_index:
9438 case DW_FORM_GNU_str_index:
9439 case DW_FORM_rnglistx:
9440 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
9441 break;
9442 case DW_FORM_indirect:
9443 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9444 info_ptr += bytes_read;
9445 /* We need to continue parsing from here, so just go back to
9446 the top. */
9447 goto skip_attribute;
9448
9449 default:
9450 error (_("Dwarf Error: Cannot handle %s "
9451 "in DWARF reader [in module %s]"),
9452 dwarf_form_name (form),
9453 bfd_get_filename (abfd));
9454 }
9455 }
9456
9457 if (abbrev->has_children)
9458 return skip_children (reader, info_ptr);
9459 else
9460 return info_ptr;
9461 }
9462
9463 /* Locate ORIG_PDI's sibling.
9464 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
9465
9466 static const gdb_byte *
9467 locate_pdi_sibling (const struct die_reader_specs *reader,
9468 struct partial_die_info *orig_pdi,
9469 const gdb_byte *info_ptr)
9470 {
9471 /* Do we know the sibling already? */
9472
9473 if (orig_pdi->sibling)
9474 return orig_pdi->sibling;
9475
9476 /* Are there any children to deal with? */
9477
9478 if (!orig_pdi->has_children)
9479 return info_ptr;
9480
9481 /* Skip the children the long way. */
9482
9483 return skip_children (reader, info_ptr);
9484 }
9485
9486 /* Expand this partial symbol table into a full symbol table. SELF is
9487 not NULL. */
9488
9489 void
9490 dwarf2_psymtab::read_symtab (struct objfile *objfile)
9491 {
9492 struct dwarf2_per_objfile *dwarf2_per_objfile
9493 = get_dwarf2_per_objfile (objfile);
9494
9495 gdb_assert (!readin);
9496 /* If this psymtab is constructed from a debug-only objfile, the
9497 has_section_at_zero flag will not necessarily be correct. We
9498 can get the correct value for this flag by looking at the data
9499 associated with the (presumably stripped) associated objfile. */
9500 if (objfile->separate_debug_objfile_backlink)
9501 {
9502 struct dwarf2_per_objfile *dpo_backlink
9503 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
9504
9505 dwarf2_per_objfile->has_section_at_zero
9506 = dpo_backlink->has_section_at_zero;
9507 }
9508
9509 dwarf2_per_objfile->reading_partial_symbols = 0;
9510
9511 expand_psymtab (objfile);
9512
9513 process_cu_includes (dwarf2_per_objfile);
9514 }
9515 \f
9516 /* Reading in full CUs. */
9517
9518 /* Add PER_CU to the queue. */
9519
9520 static void
9521 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
9522 enum language pretend_language)
9523 {
9524 struct dwarf2_queue_item *item;
9525
9526 per_cu->queued = 1;
9527 item = XNEW (struct dwarf2_queue_item);
9528 item->per_cu = per_cu;
9529 item->pretend_language = pretend_language;
9530 item->next = NULL;
9531
9532 if (dwarf2_queue == NULL)
9533 dwarf2_queue = item;
9534 else
9535 dwarf2_queue_tail->next = item;
9536
9537 dwarf2_queue_tail = item;
9538 }
9539
9540 /* If PER_CU is not yet queued, add it to the queue.
9541 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9542 dependency.
9543 The result is non-zero if PER_CU was queued, otherwise the result is zero
9544 meaning either PER_CU is already queued or it is already loaded.
9545
9546 N.B. There is an invariant here that if a CU is queued then it is loaded.
9547 The caller is required to load PER_CU if we return non-zero. */
9548
9549 static int
9550 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
9551 struct dwarf2_per_cu_data *per_cu,
9552 enum language pretend_language)
9553 {
9554 /* We may arrive here during partial symbol reading, if we need full
9555 DIEs to process an unusual case (e.g. template arguments). Do
9556 not queue PER_CU, just tell our caller to load its DIEs. */
9557 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
9558 {
9559 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
9560 return 1;
9561 return 0;
9562 }
9563
9564 /* Mark the dependence relation so that we don't flush PER_CU
9565 too early. */
9566 if (dependent_cu != NULL)
9567 dwarf2_add_dependence (dependent_cu, per_cu);
9568
9569 /* If it's already on the queue, we have nothing to do. */
9570 if (per_cu->queued)
9571 return 0;
9572
9573 /* If the compilation unit is already loaded, just mark it as
9574 used. */
9575 if (per_cu->cu != NULL)
9576 {
9577 per_cu->cu->last_used = 0;
9578 return 0;
9579 }
9580
9581 /* Add it to the queue. */
9582 queue_comp_unit (per_cu, pretend_language);
9583
9584 return 1;
9585 }
9586
9587 /* Process the queue. */
9588
9589 static void
9590 process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
9591 {
9592 struct dwarf2_queue_item *item, *next_item;
9593
9594 if (dwarf_read_debug)
9595 {
9596 fprintf_unfiltered (gdb_stdlog,
9597 "Expanding one or more symtabs of objfile %s ...\n",
9598 objfile_name (dwarf2_per_objfile->objfile));
9599 }
9600
9601 /* The queue starts out with one item, but following a DIE reference
9602 may load a new CU, adding it to the end of the queue. */
9603 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
9604 {
9605 if ((dwarf2_per_objfile->using_index
9606 ? !item->per_cu->v.quick->compunit_symtab
9607 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
9608 /* Skip dummy CUs. */
9609 && item->per_cu->cu != NULL)
9610 {
9611 struct dwarf2_per_cu_data *per_cu = item->per_cu;
9612 unsigned int debug_print_threshold;
9613 char buf[100];
9614
9615 if (per_cu->is_debug_types)
9616 {
9617 struct signatured_type *sig_type =
9618 (struct signatured_type *) per_cu;
9619
9620 sprintf (buf, "TU %s at offset %s",
9621 hex_string (sig_type->signature),
9622 sect_offset_str (per_cu->sect_off));
9623 /* There can be 100s of TUs.
9624 Only print them in verbose mode. */
9625 debug_print_threshold = 2;
9626 }
9627 else
9628 {
9629 sprintf (buf, "CU at offset %s",
9630 sect_offset_str (per_cu->sect_off));
9631 debug_print_threshold = 1;
9632 }
9633
9634 if (dwarf_read_debug >= debug_print_threshold)
9635 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
9636
9637 if (per_cu->is_debug_types)
9638 process_full_type_unit (per_cu, item->pretend_language);
9639 else
9640 process_full_comp_unit (per_cu, item->pretend_language);
9641
9642 if (dwarf_read_debug >= debug_print_threshold)
9643 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
9644 }
9645
9646 item->per_cu->queued = 0;
9647 next_item = item->next;
9648 xfree (item);
9649 }
9650
9651 dwarf2_queue_tail = NULL;
9652
9653 if (dwarf_read_debug)
9654 {
9655 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
9656 objfile_name (dwarf2_per_objfile->objfile));
9657 }
9658 }
9659
9660 /* Read in full symbols for PST, and anything it depends on. */
9661
9662 void
9663 dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
9664 {
9665 struct dwarf2_per_cu_data *per_cu;
9666
9667 if (readin)
9668 return;
9669
9670 read_dependencies (objfile);
9671
9672 per_cu = per_cu_data;
9673
9674 if (per_cu == NULL)
9675 {
9676 /* It's an include file, no symbols to read for it.
9677 Everything is in the parent symtab. */
9678 readin = true;
9679 return;
9680 }
9681
9682 dw2_do_instantiate_symtab (per_cu, false);
9683 }
9684
9685 /* Trivial hash function for die_info: the hash value of a DIE
9686 is its offset in .debug_info for this objfile. */
9687
9688 static hashval_t
9689 die_hash (const void *item)
9690 {
9691 const struct die_info *die = (const struct die_info *) item;
9692
9693 return to_underlying (die->sect_off);
9694 }
9695
9696 /* Trivial comparison function for die_info structures: two DIEs
9697 are equal if they have the same offset. */
9698
9699 static int
9700 die_eq (const void *item_lhs, const void *item_rhs)
9701 {
9702 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9703 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
9704
9705 return die_lhs->sect_off == die_rhs->sect_off;
9706 }
9707
9708 /* Load the DIEs associated with PER_CU into memory. */
9709
9710 static void
9711 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
9712 bool skip_partial,
9713 enum language pretend_language)
9714 {
9715 gdb_assert (! this_cu->is_debug_types);
9716
9717 cutu_reader reader (this_cu, NULL, 1, 1, skip_partial);
9718 if (reader.dummy_p)
9719 return;
9720
9721 struct dwarf2_cu *cu = reader.cu;
9722 const gdb_byte *info_ptr = reader.info_ptr;
9723
9724 gdb_assert (cu->die_hash == NULL);
9725 cu->die_hash =
9726 htab_create_alloc_ex (cu->header.length / 12,
9727 die_hash,
9728 die_eq,
9729 NULL,
9730 &cu->comp_unit_obstack,
9731 hashtab_obstack_allocate,
9732 dummy_obstack_deallocate);
9733
9734 if (reader.has_children)
9735 reader.comp_unit_die->child
9736 = read_die_and_siblings (&reader, reader.info_ptr,
9737 &info_ptr, reader.comp_unit_die);
9738 cu->dies = reader.comp_unit_die;
9739 /* comp_unit_die is not stored in die_hash, no need. */
9740
9741 /* We try not to read any attributes in this function, because not
9742 all CUs needed for references have been loaded yet, and symbol
9743 table processing isn't initialized. But we have to set the CU language,
9744 or we won't be able to build types correctly.
9745 Similarly, if we do not read the producer, we can not apply
9746 producer-specific interpretation. */
9747 prepare_one_comp_unit (cu, cu->dies, pretend_language);
9748 }
9749
9750 /* Add a DIE to the delayed physname list. */
9751
9752 static void
9753 add_to_method_list (struct type *type, int fnfield_index, int index,
9754 const char *name, struct die_info *die,
9755 struct dwarf2_cu *cu)
9756 {
9757 struct delayed_method_info mi;
9758 mi.type = type;
9759 mi.fnfield_index = fnfield_index;
9760 mi.index = index;
9761 mi.name = name;
9762 mi.die = die;
9763 cu->method_list.push_back (mi);
9764 }
9765
9766 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9767 "const" / "volatile". If so, decrements LEN by the length of the
9768 modifier and return true. Otherwise return false. */
9769
9770 template<size_t N>
9771 static bool
9772 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9773 {
9774 size_t mod_len = sizeof (mod) - 1;
9775 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9776 {
9777 len -= mod_len;
9778 return true;
9779 }
9780 return false;
9781 }
9782
9783 /* Compute the physnames of any methods on the CU's method list.
9784
9785 The computation of method physnames is delayed in order to avoid the
9786 (bad) condition that one of the method's formal parameters is of an as yet
9787 incomplete type. */
9788
9789 static void
9790 compute_delayed_physnames (struct dwarf2_cu *cu)
9791 {
9792 /* Only C++ delays computing physnames. */
9793 if (cu->method_list.empty ())
9794 return;
9795 gdb_assert (cu->language == language_cplus);
9796
9797 for (const delayed_method_info &mi : cu->method_list)
9798 {
9799 const char *physname;
9800 struct fn_fieldlist *fn_flp
9801 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9802 physname = dwarf2_physname (mi.name, mi.die, cu);
9803 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
9804 = physname ? physname : "";
9805
9806 /* Since there's no tag to indicate whether a method is a
9807 const/volatile overload, extract that information out of the
9808 demangled name. */
9809 if (physname != NULL)
9810 {
9811 size_t len = strlen (physname);
9812
9813 while (1)
9814 {
9815 if (physname[len] == ')') /* shortcut */
9816 break;
9817 else if (check_modifier (physname, len, " const"))
9818 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
9819 else if (check_modifier (physname, len, " volatile"))
9820 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
9821 else
9822 break;
9823 }
9824 }
9825 }
9826
9827 /* The list is no longer needed. */
9828 cu->method_list.clear ();
9829 }
9830
9831 /* Go objects should be embedded in a DW_TAG_module DIE,
9832 and it's not clear if/how imported objects will appear.
9833 To keep Go support simple until that's worked out,
9834 go back through what we've read and create something usable.
9835 We could do this while processing each DIE, and feels kinda cleaner,
9836 but that way is more invasive.
9837 This is to, for example, allow the user to type "p var" or "b main"
9838 without having to specify the package name, and allow lookups
9839 of module.object to work in contexts that use the expression
9840 parser. */
9841
9842 static void
9843 fixup_go_packaging (struct dwarf2_cu *cu)
9844 {
9845 gdb::unique_xmalloc_ptr<char> package_name;
9846 struct pending *list;
9847 int i;
9848
9849 for (list = *cu->get_builder ()->get_global_symbols ();
9850 list != NULL;
9851 list = list->next)
9852 {
9853 for (i = 0; i < list->nsyms; ++i)
9854 {
9855 struct symbol *sym = list->symbol[i];
9856
9857 if (sym->language () == language_go
9858 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9859 {
9860 gdb::unique_xmalloc_ptr<char> this_package_name
9861 (go_symbol_package_name (sym));
9862
9863 if (this_package_name == NULL)
9864 continue;
9865 if (package_name == NULL)
9866 package_name = std::move (this_package_name);
9867 else
9868 {
9869 struct objfile *objfile
9870 = cu->per_cu->dwarf2_per_objfile->objfile;
9871 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
9872 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
9873 (symbol_symtab (sym) != NULL
9874 ? symtab_to_filename_for_display
9875 (symbol_symtab (sym))
9876 : objfile_name (objfile)),
9877 this_package_name.get (), package_name.get ());
9878 }
9879 }
9880 }
9881 }
9882
9883 if (package_name != NULL)
9884 {
9885 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9886 const char *saved_package_name
9887 = obstack_strdup (&objfile->per_bfd->storage_obstack, package_name.get ());
9888 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9889 saved_package_name);
9890 struct symbol *sym;
9891
9892 sym = allocate_symbol (objfile);
9893 sym->set_language (language_go, &objfile->objfile_obstack);
9894 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
9895 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9896 e.g., "main" finds the "main" module and not C's main(). */
9897 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9898 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9899 SYMBOL_TYPE (sym) = type;
9900
9901 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
9902 }
9903 }
9904
9905 /* Allocate a fully-qualified name consisting of the two parts on the
9906 obstack. */
9907
9908 static const char *
9909 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9910 {
9911 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9912 }
9913
9914 /* A helper that allocates a struct discriminant_info to attach to a
9915 union type. */
9916
9917 static struct discriminant_info *
9918 alloc_discriminant_info (struct type *type, int discriminant_index,
9919 int default_index)
9920 {
9921 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9922 gdb_assert (discriminant_index == -1
9923 || (discriminant_index >= 0
9924 && discriminant_index < TYPE_NFIELDS (type)));
9925 gdb_assert (default_index == -1
9926 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
9927
9928 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
9929
9930 struct discriminant_info *disc
9931 = ((struct discriminant_info *)
9932 TYPE_ZALLOC (type,
9933 offsetof (struct discriminant_info, discriminants)
9934 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
9935 disc->default_index = default_index;
9936 disc->discriminant_index = discriminant_index;
9937
9938 struct dynamic_prop prop;
9939 prop.kind = PROP_UNDEFINED;
9940 prop.data.baton = disc;
9941
9942 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
9943
9944 return disc;
9945 }
9946
9947 /* Some versions of rustc emitted enums in an unusual way.
9948
9949 Ordinary enums were emitted as unions. The first element of each
9950 structure in the union was named "RUST$ENUM$DISR". This element
9951 held the discriminant.
9952
9953 These versions of Rust also implemented the "non-zero"
9954 optimization. When the enum had two values, and one is empty and
9955 the other holds a pointer that cannot be zero, the pointer is used
9956 as the discriminant, with a zero value meaning the empty variant.
9957 Here, the union's first member is of the form
9958 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9959 where the fieldnos are the indices of the fields that should be
9960 traversed in order to find the field (which may be several fields deep)
9961 and the variantname is the name of the variant of the case when the
9962 field is zero.
9963
9964 This function recognizes whether TYPE is of one of these forms,
9965 and, if so, smashes it to be a variant type. */
9966
9967 static void
9968 quirk_rust_enum (struct type *type, struct objfile *objfile)
9969 {
9970 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9971
9972 /* We don't need to deal with empty enums. */
9973 if (TYPE_NFIELDS (type) == 0)
9974 return;
9975
9976 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9977 if (TYPE_NFIELDS (type) == 1
9978 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9979 {
9980 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9981
9982 /* Decode the field name to find the offset of the
9983 discriminant. */
9984 ULONGEST bit_offset = 0;
9985 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9986 while (name[0] >= '0' && name[0] <= '9')
9987 {
9988 char *tail;
9989 unsigned long index = strtoul (name, &tail, 10);
9990 name = tail;
9991 if (*name != '$'
9992 || index >= TYPE_NFIELDS (field_type)
9993 || (TYPE_FIELD_LOC_KIND (field_type, index)
9994 != FIELD_LOC_KIND_BITPOS))
9995 {
9996 complaint (_("Could not parse Rust enum encoding string \"%s\""
9997 "[in module %s]"),
9998 TYPE_FIELD_NAME (type, 0),
9999 objfile_name (objfile));
10000 return;
10001 }
10002 ++name;
10003
10004 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
10005 field_type = TYPE_FIELD_TYPE (field_type, index);
10006 }
10007
10008 /* Make a union to hold the variants. */
10009 struct type *union_type = alloc_type (objfile);
10010 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10011 TYPE_NFIELDS (union_type) = 3;
10012 TYPE_FIELDS (union_type)
10013 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
10014 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
10015 set_type_align (union_type, TYPE_RAW_ALIGN (type));
10016
10017 /* Put the discriminant must at index 0. */
10018 TYPE_FIELD_TYPE (union_type, 0) = field_type;
10019 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10020 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10021 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
10022
10023 /* The order of fields doesn't really matter, so put the real
10024 field at index 1 and the data-less field at index 2. */
10025 struct discriminant_info *disc
10026 = alloc_discriminant_info (union_type, 0, 1);
10027 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
10028 TYPE_FIELD_NAME (union_type, 1)
10029 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
10030 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
10031 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
10032 TYPE_FIELD_NAME (union_type, 1));
10033
10034 const char *dataless_name
10035 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
10036 name);
10037 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
10038 dataless_name);
10039 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
10040 /* NAME points into the original discriminant name, which
10041 already has the correct lifetime. */
10042 TYPE_FIELD_NAME (union_type, 2) = name;
10043 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
10044 disc->discriminants[2] = 0;
10045
10046 /* Smash this type to be a structure type. We have to do this
10047 because the type has already been recorded. */
10048 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10049 TYPE_NFIELDS (type) = 1;
10050 TYPE_FIELDS (type)
10051 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
10052
10053 /* Install the variant part. */
10054 TYPE_FIELD_TYPE (type, 0) = union_type;
10055 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10056 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10057 }
10058 /* A union with a single anonymous field is probably an old-style
10059 univariant enum. */
10060 else if (TYPE_NFIELDS (type) == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
10061 {
10062 /* Smash this type to be a structure type. We have to do this
10063 because the type has already been recorded. */
10064 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10065
10066 /* Make a union to hold the variants. */
10067 struct type *union_type = alloc_type (objfile);
10068 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10069 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
10070 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
10071 set_type_align (union_type, TYPE_RAW_ALIGN (type));
10072 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
10073
10074 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
10075 const char *variant_name
10076 = rust_last_path_segment (TYPE_NAME (field_type));
10077 TYPE_FIELD_NAME (union_type, 0) = variant_name;
10078 TYPE_NAME (field_type)
10079 = rust_fully_qualify (&objfile->objfile_obstack,
10080 TYPE_NAME (type), variant_name);
10081
10082 /* Install the union in the outer struct type. */
10083 TYPE_NFIELDS (type) = 1;
10084 TYPE_FIELDS (type)
10085 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
10086 TYPE_FIELD_TYPE (type, 0) = union_type;
10087 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10088 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10089
10090 alloc_discriminant_info (union_type, -1, 0);
10091 }
10092 else
10093 {
10094 struct type *disr_type = nullptr;
10095 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
10096 {
10097 disr_type = TYPE_FIELD_TYPE (type, i);
10098
10099 if (TYPE_CODE (disr_type) != TYPE_CODE_STRUCT)
10100 {
10101 /* All fields of a true enum will be structs. */
10102 return;
10103 }
10104 else if (TYPE_NFIELDS (disr_type) == 0)
10105 {
10106 /* Could be data-less variant, so keep going. */
10107 disr_type = nullptr;
10108 }
10109 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
10110 "RUST$ENUM$DISR") != 0)
10111 {
10112 /* Not a Rust enum. */
10113 return;
10114 }
10115 else
10116 {
10117 /* Found one. */
10118 break;
10119 }
10120 }
10121
10122 /* If we got here without a discriminant, then it's probably
10123 just a union. */
10124 if (disr_type == nullptr)
10125 return;
10126
10127 /* Smash this type to be a structure type. We have to do this
10128 because the type has already been recorded. */
10129 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10130
10131 /* Make a union to hold the variants. */
10132 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
10133 struct type *union_type = alloc_type (objfile);
10134 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10135 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
10136 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
10137 set_type_align (union_type, TYPE_RAW_ALIGN (type));
10138 TYPE_FIELDS (union_type)
10139 = (struct field *) TYPE_ZALLOC (union_type,
10140 (TYPE_NFIELDS (union_type)
10141 * sizeof (struct field)));
10142
10143 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
10144 TYPE_NFIELDS (type) * sizeof (struct field));
10145
10146 /* Install the discriminant at index 0 in the union. */
10147 TYPE_FIELD (union_type, 0) = *disr_field;
10148 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10149 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10150
10151 /* Install the union in the outer struct type. */
10152 TYPE_FIELD_TYPE (type, 0) = union_type;
10153 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10154 TYPE_NFIELDS (type) = 1;
10155
10156 /* Set the size and offset of the union type. */
10157 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10158
10159 /* We need a way to find the correct discriminant given a
10160 variant name. For convenience we build a map here. */
10161 struct type *enum_type = FIELD_TYPE (*disr_field);
10162 std::unordered_map<std::string, ULONGEST> discriminant_map;
10163 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
10164 {
10165 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
10166 {
10167 const char *name
10168 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
10169 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
10170 }
10171 }
10172
10173 int n_fields = TYPE_NFIELDS (union_type);
10174 struct discriminant_info *disc
10175 = alloc_discriminant_info (union_type, 0, -1);
10176 /* Skip the discriminant here. */
10177 for (int i = 1; i < n_fields; ++i)
10178 {
10179 /* Find the final word in the name of this variant's type.
10180 That name can be used to look up the correct
10181 discriminant. */
10182 const char *variant_name
10183 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
10184 i)));
10185
10186 auto iter = discriminant_map.find (variant_name);
10187 if (iter != discriminant_map.end ())
10188 disc->discriminants[i] = iter->second;
10189
10190 /* Remove the discriminant field, if it exists. */
10191 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
10192 if (TYPE_NFIELDS (sub_type) > 0)
10193 {
10194 --TYPE_NFIELDS (sub_type);
10195 ++TYPE_FIELDS (sub_type);
10196 }
10197 TYPE_FIELD_NAME (union_type, i) = variant_name;
10198 TYPE_NAME (sub_type)
10199 = rust_fully_qualify (&objfile->objfile_obstack,
10200 TYPE_NAME (type), variant_name);
10201 }
10202 }
10203 }
10204
10205 /* Rewrite some Rust unions to be structures with variants parts. */
10206
10207 static void
10208 rust_union_quirks (struct dwarf2_cu *cu)
10209 {
10210 gdb_assert (cu->language == language_rust);
10211 for (type *type_ : cu->rust_unions)
10212 quirk_rust_enum (type_, cu->per_cu->dwarf2_per_objfile->objfile);
10213 /* We don't need this any more. */
10214 cu->rust_unions.clear ();
10215 }
10216
10217 /* Return the symtab for PER_CU. This works properly regardless of
10218 whether we're using the index or psymtabs. */
10219
10220 static struct compunit_symtab *
10221 get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
10222 {
10223 return (per_cu->dwarf2_per_objfile->using_index
10224 ? per_cu->v.quick->compunit_symtab
10225 : per_cu->v.psymtab->compunit_symtab);
10226 }
10227
10228 /* A helper function for computing the list of all symbol tables
10229 included by PER_CU. */
10230
10231 static void
10232 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
10233 htab_t all_children, htab_t all_type_symtabs,
10234 struct dwarf2_per_cu_data *per_cu,
10235 struct compunit_symtab *immediate_parent)
10236 {
10237 void **slot;
10238 struct compunit_symtab *cust;
10239
10240 slot = htab_find_slot (all_children, per_cu, INSERT);
10241 if (*slot != NULL)
10242 {
10243 /* This inclusion and its children have been processed. */
10244 return;
10245 }
10246
10247 *slot = per_cu;
10248 /* Only add a CU if it has a symbol table. */
10249 cust = get_compunit_symtab (per_cu);
10250 if (cust != NULL)
10251 {
10252 /* If this is a type unit only add its symbol table if we haven't
10253 seen it yet (type unit per_cu's can share symtabs). */
10254 if (per_cu->is_debug_types)
10255 {
10256 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
10257 if (*slot == NULL)
10258 {
10259 *slot = cust;
10260 result->push_back (cust);
10261 if (cust->user == NULL)
10262 cust->user = immediate_parent;
10263 }
10264 }
10265 else
10266 {
10267 result->push_back (cust);
10268 if (cust->user == NULL)
10269 cust->user = immediate_parent;
10270 }
10271 }
10272
10273 if (!per_cu->imported_symtabs_empty ())
10274 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
10275 {
10276 recursively_compute_inclusions (result, all_children,
10277 all_type_symtabs, ptr, cust);
10278 }
10279 }
10280
10281 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
10282 PER_CU. */
10283
10284 static void
10285 compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
10286 {
10287 gdb_assert (! per_cu->is_debug_types);
10288
10289 if (!per_cu->imported_symtabs_empty ())
10290 {
10291 int len;
10292 std::vector<compunit_symtab *> result_symtabs;
10293 htab_t all_children, all_type_symtabs;
10294 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
10295
10296 /* If we don't have a symtab, we can just skip this case. */
10297 if (cust == NULL)
10298 return;
10299
10300 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10301 NULL, xcalloc, xfree);
10302 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10303 NULL, xcalloc, xfree);
10304
10305 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
10306 {
10307 recursively_compute_inclusions (&result_symtabs, all_children,
10308 all_type_symtabs, ptr, cust);
10309 }
10310
10311 /* Now we have a transitive closure of all the included symtabs. */
10312 len = result_symtabs.size ();
10313 cust->includes
10314 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
10315 struct compunit_symtab *, len + 1);
10316 memcpy (cust->includes, result_symtabs.data (),
10317 len * sizeof (compunit_symtab *));
10318 cust->includes[len] = NULL;
10319
10320 htab_delete (all_children);
10321 htab_delete (all_type_symtabs);
10322 }
10323 }
10324
10325 /* Compute the 'includes' field for the symtabs of all the CUs we just
10326 read. */
10327
10328 static void
10329 process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
10330 {
10331 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->just_read_cus)
10332 {
10333 if (! iter->is_debug_types)
10334 compute_compunit_symtab_includes (iter);
10335 }
10336
10337 dwarf2_per_objfile->just_read_cus.clear ();
10338 }
10339
10340 /* Generate full symbol information for PER_CU, whose DIEs have
10341 already been loaded into memory. */
10342
10343 static void
10344 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
10345 enum language pretend_language)
10346 {
10347 struct dwarf2_cu *cu = per_cu->cu;
10348 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10349 struct objfile *objfile = dwarf2_per_objfile->objfile;
10350 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10351 CORE_ADDR lowpc, highpc;
10352 struct compunit_symtab *cust;
10353 CORE_ADDR baseaddr;
10354 struct block *static_block;
10355 CORE_ADDR addr;
10356
10357 baseaddr = objfile->text_section_offset ();
10358
10359 /* Clear the list here in case something was left over. */
10360 cu->method_list.clear ();
10361
10362 cu->language = pretend_language;
10363 cu->language_defn = language_def (cu->language);
10364
10365 /* Do line number decoding in read_file_scope () */
10366 process_die (cu->dies, cu);
10367
10368 /* For now fudge the Go package. */
10369 if (cu->language == language_go)
10370 fixup_go_packaging (cu);
10371
10372 /* Now that we have processed all the DIEs in the CU, all the types
10373 should be complete, and it should now be safe to compute all of the
10374 physnames. */
10375 compute_delayed_physnames (cu);
10376
10377 if (cu->language == language_rust)
10378 rust_union_quirks (cu);
10379
10380 /* Some compilers don't define a DW_AT_high_pc attribute for the
10381 compilation unit. If the DW_AT_high_pc is missing, synthesize
10382 it, by scanning the DIE's below the compilation unit. */
10383 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
10384
10385 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
10386 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
10387
10388 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10389 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10390 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10391 addrmap to help ensure it has an accurate map of pc values belonging to
10392 this comp unit. */
10393 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10394
10395 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
10396 SECT_OFF_TEXT (objfile),
10397 0);
10398
10399 if (cust != NULL)
10400 {
10401 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
10402
10403 /* Set symtab language to language from DW_AT_language. If the
10404 compilation is from a C file generated by language preprocessors, do
10405 not set the language if it was already deduced by start_subfile. */
10406 if (!(cu->language == language_c
10407 && COMPUNIT_FILETABS (cust)->language != language_unknown))
10408 COMPUNIT_FILETABS (cust)->language = cu->language;
10409
10410 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10411 produce DW_AT_location with location lists but it can be possibly
10412 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10413 there were bugs in prologue debug info, fixed later in GCC-4.5
10414 by "unwind info for epilogues" patch (which is not directly related).
10415
10416 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10417 needed, it would be wrong due to missing DW_AT_producer there.
10418
10419 Still one can confuse GDB by using non-standard GCC compilation
10420 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
10421 */
10422 if (cu->has_loclist && gcc_4_minor >= 5)
10423 cust->locations_valid = 1;
10424
10425 if (gcc_4_minor >= 5)
10426 cust->epilogue_unwind_valid = 1;
10427
10428 cust->call_site_htab = cu->call_site_htab;
10429 }
10430
10431 if (dwarf2_per_objfile->using_index)
10432 per_cu->v.quick->compunit_symtab = cust;
10433 else
10434 {
10435 dwarf2_psymtab *pst = per_cu->v.psymtab;
10436 pst->compunit_symtab = cust;
10437 pst->readin = true;
10438 }
10439
10440 /* Push it for inclusion processing later. */
10441 dwarf2_per_objfile->just_read_cus.push_back (per_cu);
10442
10443 /* Not needed any more. */
10444 cu->reset_builder ();
10445 }
10446
10447 /* Generate full symbol information for type unit PER_CU, whose DIEs have
10448 already been loaded into memory. */
10449
10450 static void
10451 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
10452 enum language pretend_language)
10453 {
10454 struct dwarf2_cu *cu = per_cu->cu;
10455 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10456 struct objfile *objfile = dwarf2_per_objfile->objfile;
10457 struct compunit_symtab *cust;
10458 struct signatured_type *sig_type;
10459
10460 gdb_assert (per_cu->is_debug_types);
10461 sig_type = (struct signatured_type *) per_cu;
10462
10463 /* Clear the list here in case something was left over. */
10464 cu->method_list.clear ();
10465
10466 cu->language = pretend_language;
10467 cu->language_defn = language_def (cu->language);
10468
10469 /* The symbol tables are set up in read_type_unit_scope. */
10470 process_die (cu->dies, cu);
10471
10472 /* For now fudge the Go package. */
10473 if (cu->language == language_go)
10474 fixup_go_packaging (cu);
10475
10476 /* Now that we have processed all the DIEs in the CU, all the types
10477 should be complete, and it should now be safe to compute all of the
10478 physnames. */
10479 compute_delayed_physnames (cu);
10480
10481 if (cu->language == language_rust)
10482 rust_union_quirks (cu);
10483
10484 /* TUs share symbol tables.
10485 If this is the first TU to use this symtab, complete the construction
10486 of it with end_expandable_symtab. Otherwise, complete the addition of
10487 this TU's symbols to the existing symtab. */
10488 if (sig_type->type_unit_group->compunit_symtab == NULL)
10489 {
10490 buildsym_compunit *builder = cu->get_builder ();
10491 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
10492 sig_type->type_unit_group->compunit_symtab = cust;
10493
10494 if (cust != NULL)
10495 {
10496 /* Set symtab language to language from DW_AT_language. If the
10497 compilation is from a C file generated by language preprocessors,
10498 do not set the language if it was already deduced by
10499 start_subfile. */
10500 if (!(cu->language == language_c
10501 && COMPUNIT_FILETABS (cust)->language != language_c))
10502 COMPUNIT_FILETABS (cust)->language = cu->language;
10503 }
10504 }
10505 else
10506 {
10507 cu->get_builder ()->augment_type_symtab ();
10508 cust = sig_type->type_unit_group->compunit_symtab;
10509 }
10510
10511 if (dwarf2_per_objfile->using_index)
10512 per_cu->v.quick->compunit_symtab = cust;
10513 else
10514 {
10515 dwarf2_psymtab *pst = per_cu->v.psymtab;
10516 pst->compunit_symtab = cust;
10517 pst->readin = true;
10518 }
10519
10520 /* Not needed any more. */
10521 cu->reset_builder ();
10522 }
10523
10524 /* Process an imported unit DIE. */
10525
10526 static void
10527 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10528 {
10529 struct attribute *attr;
10530
10531 /* For now we don't handle imported units in type units. */
10532 if (cu->per_cu->is_debug_types)
10533 {
10534 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10535 " supported in type units [in module %s]"),
10536 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
10537 }
10538
10539 attr = dwarf2_attr (die, DW_AT_import, cu);
10540 if (attr != NULL)
10541 {
10542 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10543 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10544 dwarf2_per_cu_data *per_cu
10545 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
10546 cu->per_cu->dwarf2_per_objfile);
10547
10548 /* If necessary, add it to the queue and load its DIEs. */
10549 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
10550 load_full_comp_unit (per_cu, false, cu->language);
10551
10552 cu->per_cu->imported_symtabs_push (per_cu);
10553 }
10554 }
10555
10556 /* RAII object that represents a process_die scope: i.e.,
10557 starts/finishes processing a DIE. */
10558 class process_die_scope
10559 {
10560 public:
10561 process_die_scope (die_info *die, dwarf2_cu *cu)
10562 : m_die (die), m_cu (cu)
10563 {
10564 /* We should only be processing DIEs not already in process. */
10565 gdb_assert (!m_die->in_process);
10566 m_die->in_process = true;
10567 }
10568
10569 ~process_die_scope ()
10570 {
10571 m_die->in_process = false;
10572
10573 /* If we're done processing the DIE for the CU that owns the line
10574 header, we don't need the line header anymore. */
10575 if (m_cu->line_header_die_owner == m_die)
10576 {
10577 delete m_cu->line_header;
10578 m_cu->line_header = NULL;
10579 m_cu->line_header_die_owner = NULL;
10580 }
10581 }
10582
10583 private:
10584 die_info *m_die;
10585 dwarf2_cu *m_cu;
10586 };
10587
10588 /* Process a die and its children. */
10589
10590 static void
10591 process_die (struct die_info *die, struct dwarf2_cu *cu)
10592 {
10593 process_die_scope scope (die, cu);
10594
10595 switch (die->tag)
10596 {
10597 case DW_TAG_padding:
10598 break;
10599 case DW_TAG_compile_unit:
10600 case DW_TAG_partial_unit:
10601 read_file_scope (die, cu);
10602 break;
10603 case DW_TAG_type_unit:
10604 read_type_unit_scope (die, cu);
10605 break;
10606 case DW_TAG_subprogram:
10607 /* Nested subprograms in Fortran get a prefix. */
10608 if (cu->language == language_fortran
10609 && die->parent != NULL
10610 && die->parent->tag == DW_TAG_subprogram)
10611 cu->processing_has_namespace_info = true;
10612 /* Fall through. */
10613 case DW_TAG_inlined_subroutine:
10614 read_func_scope (die, cu);
10615 break;
10616 case DW_TAG_lexical_block:
10617 case DW_TAG_try_block:
10618 case DW_TAG_catch_block:
10619 read_lexical_block_scope (die, cu);
10620 break;
10621 case DW_TAG_call_site:
10622 case DW_TAG_GNU_call_site:
10623 read_call_site_scope (die, cu);
10624 break;
10625 case DW_TAG_class_type:
10626 case DW_TAG_interface_type:
10627 case DW_TAG_structure_type:
10628 case DW_TAG_union_type:
10629 process_structure_scope (die, cu);
10630 break;
10631 case DW_TAG_enumeration_type:
10632 process_enumeration_scope (die, cu);
10633 break;
10634
10635 /* These dies have a type, but processing them does not create
10636 a symbol or recurse to process the children. Therefore we can
10637 read them on-demand through read_type_die. */
10638 case DW_TAG_subroutine_type:
10639 case DW_TAG_set_type:
10640 case DW_TAG_array_type:
10641 case DW_TAG_pointer_type:
10642 case DW_TAG_ptr_to_member_type:
10643 case DW_TAG_reference_type:
10644 case DW_TAG_rvalue_reference_type:
10645 case DW_TAG_string_type:
10646 break;
10647
10648 case DW_TAG_base_type:
10649 case DW_TAG_subrange_type:
10650 case DW_TAG_typedef:
10651 /* Add a typedef symbol for the type definition, if it has a
10652 DW_AT_name. */
10653 new_symbol (die, read_type_die (die, cu), cu);
10654 break;
10655 case DW_TAG_common_block:
10656 read_common_block (die, cu);
10657 break;
10658 case DW_TAG_common_inclusion:
10659 break;
10660 case DW_TAG_namespace:
10661 cu->processing_has_namespace_info = true;
10662 read_namespace (die, cu);
10663 break;
10664 case DW_TAG_module:
10665 cu->processing_has_namespace_info = true;
10666 read_module (die, cu);
10667 break;
10668 case DW_TAG_imported_declaration:
10669 cu->processing_has_namespace_info = true;
10670 if (read_namespace_alias (die, cu))
10671 break;
10672 /* The declaration is not a global namespace alias. */
10673 /* Fall through. */
10674 case DW_TAG_imported_module:
10675 cu->processing_has_namespace_info = true;
10676 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10677 || cu->language != language_fortran))
10678 complaint (_("Tag '%s' has unexpected children"),
10679 dwarf_tag_name (die->tag));
10680 read_import_statement (die, cu);
10681 break;
10682
10683 case DW_TAG_imported_unit:
10684 process_imported_unit_die (die, cu);
10685 break;
10686
10687 case DW_TAG_variable:
10688 read_variable (die, cu);
10689 break;
10690
10691 default:
10692 new_symbol (die, NULL, cu);
10693 break;
10694 }
10695 }
10696 \f
10697 /* DWARF name computation. */
10698
10699 /* A helper function for dwarf2_compute_name which determines whether DIE
10700 needs to have the name of the scope prepended to the name listed in the
10701 die. */
10702
10703 static int
10704 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10705 {
10706 struct attribute *attr;
10707
10708 switch (die->tag)
10709 {
10710 case DW_TAG_namespace:
10711 case DW_TAG_typedef:
10712 case DW_TAG_class_type:
10713 case DW_TAG_interface_type:
10714 case DW_TAG_structure_type:
10715 case DW_TAG_union_type:
10716 case DW_TAG_enumeration_type:
10717 case DW_TAG_enumerator:
10718 case DW_TAG_subprogram:
10719 case DW_TAG_inlined_subroutine:
10720 case DW_TAG_member:
10721 case DW_TAG_imported_declaration:
10722 return 1;
10723
10724 case DW_TAG_variable:
10725 case DW_TAG_constant:
10726 /* We only need to prefix "globally" visible variables. These include
10727 any variable marked with DW_AT_external or any variable that
10728 lives in a namespace. [Variables in anonymous namespaces
10729 require prefixing, but they are not DW_AT_external.] */
10730
10731 if (dwarf2_attr (die, DW_AT_specification, cu))
10732 {
10733 struct dwarf2_cu *spec_cu = cu;
10734
10735 return die_needs_namespace (die_specification (die, &spec_cu),
10736 spec_cu);
10737 }
10738
10739 attr = dwarf2_attr (die, DW_AT_external, cu);
10740 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10741 && die->parent->tag != DW_TAG_module)
10742 return 0;
10743 /* A variable in a lexical block of some kind does not need a
10744 namespace, even though in C++ such variables may be external
10745 and have a mangled name. */
10746 if (die->parent->tag == DW_TAG_lexical_block
10747 || die->parent->tag == DW_TAG_try_block
10748 || die->parent->tag == DW_TAG_catch_block
10749 || die->parent->tag == DW_TAG_subprogram)
10750 return 0;
10751 return 1;
10752
10753 default:
10754 return 0;
10755 }
10756 }
10757
10758 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10759 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10760 defined for the given DIE. */
10761
10762 static struct attribute *
10763 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10764 {
10765 struct attribute *attr;
10766
10767 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10768 if (attr == NULL)
10769 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10770
10771 return attr;
10772 }
10773
10774 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10775 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10776 defined for the given DIE. */
10777
10778 static const char *
10779 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10780 {
10781 const char *linkage_name;
10782
10783 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10784 if (linkage_name == NULL)
10785 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10786
10787 return linkage_name;
10788 }
10789
10790 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
10791 compute the physname for the object, which include a method's:
10792 - formal parameters (C++),
10793 - receiver type (Go),
10794
10795 The term "physname" is a bit confusing.
10796 For C++, for example, it is the demangled name.
10797 For Go, for example, it's the mangled name.
10798
10799 For Ada, return the DIE's linkage name rather than the fully qualified
10800 name. PHYSNAME is ignored..
10801
10802 The result is allocated on the objfile_obstack and canonicalized. */
10803
10804 static const char *
10805 dwarf2_compute_name (const char *name,
10806 struct die_info *die, struct dwarf2_cu *cu,
10807 int physname)
10808 {
10809 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10810
10811 if (name == NULL)
10812 name = dwarf2_name (die, cu);
10813
10814 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10815 but otherwise compute it by typename_concat inside GDB.
10816 FIXME: Actually this is not really true, or at least not always true.
10817 It's all very confusing. compute_and_set_names doesn't try to demangle
10818 Fortran names because there is no mangling standard. So new_symbol
10819 will set the demangled name to the result of dwarf2_full_name, and it is
10820 the demangled name that GDB uses if it exists. */
10821 if (cu->language == language_ada
10822 || (cu->language == language_fortran && physname))
10823 {
10824 /* For Ada unit, we prefer the linkage name over the name, as
10825 the former contains the exported name, which the user expects
10826 to be able to reference. Ideally, we want the user to be able
10827 to reference this entity using either natural or linkage name,
10828 but we haven't started looking at this enhancement yet. */
10829 const char *linkage_name = dw2_linkage_name (die, cu);
10830
10831 if (linkage_name != NULL)
10832 return linkage_name;
10833 }
10834
10835 /* These are the only languages we know how to qualify names in. */
10836 if (name != NULL
10837 && (cu->language == language_cplus
10838 || cu->language == language_fortran || cu->language == language_d
10839 || cu->language == language_rust))
10840 {
10841 if (die_needs_namespace (die, cu))
10842 {
10843 const char *prefix;
10844 const char *canonical_name = NULL;
10845
10846 string_file buf;
10847
10848 prefix = determine_prefix (die, cu);
10849 if (*prefix != '\0')
10850 {
10851 gdb::unique_xmalloc_ptr<char> prefixed_name
10852 (typename_concat (NULL, prefix, name, physname, cu));
10853
10854 buf.puts (prefixed_name.get ());
10855 }
10856 else
10857 buf.puts (name);
10858
10859 /* Template parameters may be specified in the DIE's DW_AT_name, or
10860 as children with DW_TAG_template_type_param or
10861 DW_TAG_value_type_param. If the latter, add them to the name
10862 here. If the name already has template parameters, then
10863 skip this step; some versions of GCC emit both, and
10864 it is more efficient to use the pre-computed name.
10865
10866 Something to keep in mind about this process: it is very
10867 unlikely, or in some cases downright impossible, to produce
10868 something that will match the mangled name of a function.
10869 If the definition of the function has the same debug info,
10870 we should be able to match up with it anyway. But fallbacks
10871 using the minimal symbol, for instance to find a method
10872 implemented in a stripped copy of libstdc++, will not work.
10873 If we do not have debug info for the definition, we will have to
10874 match them up some other way.
10875
10876 When we do name matching there is a related problem with function
10877 templates; two instantiated function templates are allowed to
10878 differ only by their return types, which we do not add here. */
10879
10880 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10881 {
10882 struct attribute *attr;
10883 struct die_info *child;
10884 int first = 1;
10885
10886 die->building_fullname = 1;
10887
10888 for (child = die->child; child != NULL; child = child->sibling)
10889 {
10890 struct type *type;
10891 LONGEST value;
10892 const gdb_byte *bytes;
10893 struct dwarf2_locexpr_baton *baton;
10894 struct value *v;
10895
10896 if (child->tag != DW_TAG_template_type_param
10897 && child->tag != DW_TAG_template_value_param)
10898 continue;
10899
10900 if (first)
10901 {
10902 buf.puts ("<");
10903 first = 0;
10904 }
10905 else
10906 buf.puts (", ");
10907
10908 attr = dwarf2_attr (child, DW_AT_type, cu);
10909 if (attr == NULL)
10910 {
10911 complaint (_("template parameter missing DW_AT_type"));
10912 buf.puts ("UNKNOWN_TYPE");
10913 continue;
10914 }
10915 type = die_type (child, cu);
10916
10917 if (child->tag == DW_TAG_template_type_param)
10918 {
10919 c_print_type (type, "", &buf, -1, 0, cu->language,
10920 &type_print_raw_options);
10921 continue;
10922 }
10923
10924 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10925 if (attr == NULL)
10926 {
10927 complaint (_("template parameter missing "
10928 "DW_AT_const_value"));
10929 buf.puts ("UNKNOWN_VALUE");
10930 continue;
10931 }
10932
10933 dwarf2_const_value_attr (attr, type, name,
10934 &cu->comp_unit_obstack, cu,
10935 &value, &bytes, &baton);
10936
10937 if (TYPE_NOSIGN (type))
10938 /* GDB prints characters as NUMBER 'CHAR'. If that's
10939 changed, this can use value_print instead. */
10940 c_printchar (value, type, &buf);
10941 else
10942 {
10943 struct value_print_options opts;
10944
10945 if (baton != NULL)
10946 v = dwarf2_evaluate_loc_desc (type, NULL,
10947 baton->data,
10948 baton->size,
10949 baton->per_cu);
10950 else if (bytes != NULL)
10951 {
10952 v = allocate_value (type);
10953 memcpy (value_contents_writeable (v), bytes,
10954 TYPE_LENGTH (type));
10955 }
10956 else
10957 v = value_from_longest (type, value);
10958
10959 /* Specify decimal so that we do not depend on
10960 the radix. */
10961 get_formatted_print_options (&opts, 'd');
10962 opts.raw = 1;
10963 value_print (v, &buf, &opts);
10964 release_value (v);
10965 }
10966 }
10967
10968 die->building_fullname = 0;
10969
10970 if (!first)
10971 {
10972 /* Close the argument list, with a space if necessary
10973 (nested templates). */
10974 if (!buf.empty () && buf.string ().back () == '>')
10975 buf.puts (" >");
10976 else
10977 buf.puts (">");
10978 }
10979 }
10980
10981 /* For C++ methods, append formal parameter type
10982 information, if PHYSNAME. */
10983
10984 if (physname && die->tag == DW_TAG_subprogram
10985 && cu->language == language_cplus)
10986 {
10987 struct type *type = read_type_die (die, cu);
10988
10989 c_type_print_args (type, &buf, 1, cu->language,
10990 &type_print_raw_options);
10991
10992 if (cu->language == language_cplus)
10993 {
10994 /* Assume that an artificial first parameter is
10995 "this", but do not crash if it is not. RealView
10996 marks unnamed (and thus unused) parameters as
10997 artificial; there is no way to differentiate
10998 the two cases. */
10999 if (TYPE_NFIELDS (type) > 0
11000 && TYPE_FIELD_ARTIFICIAL (type, 0)
11001 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
11002 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
11003 0))))
11004 buf.puts (" const");
11005 }
11006 }
11007
11008 const std::string &intermediate_name = buf.string ();
11009
11010 if (cu->language == language_cplus)
11011 canonical_name
11012 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
11013 &objfile->per_bfd->storage_obstack);
11014
11015 /* If we only computed INTERMEDIATE_NAME, or if
11016 INTERMEDIATE_NAME is already canonical, then we need to
11017 copy it to the appropriate obstack. */
11018 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
11019 name = obstack_strdup (&objfile->per_bfd->storage_obstack,
11020 intermediate_name);
11021 else
11022 name = canonical_name;
11023 }
11024 }
11025
11026 return name;
11027 }
11028
11029 /* Return the fully qualified name of DIE, based on its DW_AT_name.
11030 If scope qualifiers are appropriate they will be added. The result
11031 will be allocated on the storage_obstack, or NULL if the DIE does
11032 not have a name. NAME may either be from a previous call to
11033 dwarf2_name or NULL.
11034
11035 The output string will be canonicalized (if C++). */
11036
11037 static const char *
11038 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
11039 {
11040 return dwarf2_compute_name (name, die, cu, 0);
11041 }
11042
11043 /* Construct a physname for the given DIE in CU. NAME may either be
11044 from a previous call to dwarf2_name or NULL. The result will be
11045 allocated on the objfile_objstack or NULL if the DIE does not have a
11046 name.
11047
11048 The output string will be canonicalized (if C++). */
11049
11050 static const char *
11051 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
11052 {
11053 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
11054 const char *retval, *mangled = NULL, *canon = NULL;
11055 int need_copy = 1;
11056
11057 /* In this case dwarf2_compute_name is just a shortcut not building anything
11058 on its own. */
11059 if (!die_needs_namespace (die, cu))
11060 return dwarf2_compute_name (name, die, cu, 1);
11061
11062 mangled = dw2_linkage_name (die, cu);
11063
11064 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
11065 See https://github.com/rust-lang/rust/issues/32925. */
11066 if (cu->language == language_rust && mangled != NULL
11067 && strchr (mangled, '{') != NULL)
11068 mangled = NULL;
11069
11070 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
11071 has computed. */
11072 gdb::unique_xmalloc_ptr<char> demangled;
11073 if (mangled != NULL)
11074 {
11075
11076 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
11077 {
11078 /* Do nothing (do not demangle the symbol name). */
11079 }
11080 else if (cu->language == language_go)
11081 {
11082 /* This is a lie, but we already lie to the caller new_symbol.
11083 new_symbol assumes we return the mangled name.
11084 This just undoes that lie until things are cleaned up. */
11085 }
11086 else
11087 {
11088 /* Use DMGL_RET_DROP for C++ template functions to suppress
11089 their return type. It is easier for GDB users to search
11090 for such functions as `name(params)' than `long name(params)'.
11091 In such case the minimal symbol names do not match the full
11092 symbol names but for template functions there is never a need
11093 to look up their definition from their declaration so
11094 the only disadvantage remains the minimal symbol variant
11095 `long name(params)' does not have the proper inferior type. */
11096 demangled.reset (gdb_demangle (mangled,
11097 (DMGL_PARAMS | DMGL_ANSI
11098 | DMGL_RET_DROP)));
11099 }
11100 if (demangled)
11101 canon = demangled.get ();
11102 else
11103 {
11104 canon = mangled;
11105 need_copy = 0;
11106 }
11107 }
11108
11109 if (canon == NULL || check_physname)
11110 {
11111 const char *physname = dwarf2_compute_name (name, die, cu, 1);
11112
11113 if (canon != NULL && strcmp (physname, canon) != 0)
11114 {
11115 /* It may not mean a bug in GDB. The compiler could also
11116 compute DW_AT_linkage_name incorrectly. But in such case
11117 GDB would need to be bug-to-bug compatible. */
11118
11119 complaint (_("Computed physname <%s> does not match demangled <%s> "
11120 "(from linkage <%s>) - DIE at %s [in module %s]"),
11121 physname, canon, mangled, sect_offset_str (die->sect_off),
11122 objfile_name (objfile));
11123
11124 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
11125 is available here - over computed PHYSNAME. It is safer
11126 against both buggy GDB and buggy compilers. */
11127
11128 retval = canon;
11129 }
11130 else
11131 {
11132 retval = physname;
11133 need_copy = 0;
11134 }
11135 }
11136 else
11137 retval = canon;
11138
11139 if (need_copy)
11140 retval = obstack_strdup (&objfile->per_bfd->storage_obstack, retval);
11141
11142 return retval;
11143 }
11144
11145 /* Inspect DIE in CU for a namespace alias. If one exists, record
11146 a new symbol for it.
11147
11148 Returns 1 if a namespace alias was recorded, 0 otherwise. */
11149
11150 static int
11151 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
11152 {
11153 struct attribute *attr;
11154
11155 /* If the die does not have a name, this is not a namespace
11156 alias. */
11157 attr = dwarf2_attr (die, DW_AT_name, cu);
11158 if (attr != NULL)
11159 {
11160 int num;
11161 struct die_info *d = die;
11162 struct dwarf2_cu *imported_cu = cu;
11163
11164 /* If the compiler has nested DW_AT_imported_declaration DIEs,
11165 keep inspecting DIEs until we hit the underlying import. */
11166 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
11167 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
11168 {
11169 attr = dwarf2_attr (d, DW_AT_import, cu);
11170 if (attr == NULL)
11171 break;
11172
11173 d = follow_die_ref (d, attr, &imported_cu);
11174 if (d->tag != DW_TAG_imported_declaration)
11175 break;
11176 }
11177
11178 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
11179 {
11180 complaint (_("DIE at %s has too many recursively imported "
11181 "declarations"), sect_offset_str (d->sect_off));
11182 return 0;
11183 }
11184
11185 if (attr != NULL)
11186 {
11187 struct type *type;
11188 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
11189
11190 type = get_die_type_at_offset (sect_off, cu->per_cu);
11191 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
11192 {
11193 /* This declaration is a global namespace alias. Add
11194 a symbol for it whose type is the aliased namespace. */
11195 new_symbol (die, type, cu);
11196 return 1;
11197 }
11198 }
11199 }
11200
11201 return 0;
11202 }
11203
11204 /* Return the using directives repository (global or local?) to use in the
11205 current context for CU.
11206
11207 For Ada, imported declarations can materialize renamings, which *may* be
11208 global. However it is impossible (for now?) in DWARF to distinguish
11209 "external" imported declarations and "static" ones. As all imported
11210 declarations seem to be static in all other languages, make them all CU-wide
11211 global only in Ada. */
11212
11213 static struct using_direct **
11214 using_directives (struct dwarf2_cu *cu)
11215 {
11216 if (cu->language == language_ada
11217 && cu->get_builder ()->outermost_context_p ())
11218 return cu->get_builder ()->get_global_using_directives ();
11219 else
11220 return cu->get_builder ()->get_local_using_directives ();
11221 }
11222
11223 /* Read the import statement specified by the given die and record it. */
11224
11225 static void
11226 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
11227 {
11228 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
11229 struct attribute *import_attr;
11230 struct die_info *imported_die, *child_die;
11231 struct dwarf2_cu *imported_cu;
11232 const char *imported_name;
11233 const char *imported_name_prefix;
11234 const char *canonical_name;
11235 const char *import_alias;
11236 const char *imported_declaration = NULL;
11237 const char *import_prefix;
11238 std::vector<const char *> excludes;
11239
11240 import_attr = dwarf2_attr (die, DW_AT_import, cu);
11241 if (import_attr == NULL)
11242 {
11243 complaint (_("Tag '%s' has no DW_AT_import"),
11244 dwarf_tag_name (die->tag));
11245 return;
11246 }
11247
11248 imported_cu = cu;
11249 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
11250 imported_name = dwarf2_name (imported_die, imported_cu);
11251 if (imported_name == NULL)
11252 {
11253 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
11254
11255 The import in the following code:
11256 namespace A
11257 {
11258 typedef int B;
11259 }
11260
11261 int main ()
11262 {
11263 using A::B;
11264 B b;
11265 return b;
11266 }
11267
11268 ...
11269 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
11270 <52> DW_AT_decl_file : 1
11271 <53> DW_AT_decl_line : 6
11272 <54> DW_AT_import : <0x75>
11273 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
11274 <59> DW_AT_name : B
11275 <5b> DW_AT_decl_file : 1
11276 <5c> DW_AT_decl_line : 2
11277 <5d> DW_AT_type : <0x6e>
11278 ...
11279 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
11280 <76> DW_AT_byte_size : 4
11281 <77> DW_AT_encoding : 5 (signed)
11282
11283 imports the wrong die ( 0x75 instead of 0x58 ).
11284 This case will be ignored until the gcc bug is fixed. */
11285 return;
11286 }
11287
11288 /* Figure out the local name after import. */
11289 import_alias = dwarf2_name (die, cu);
11290
11291 /* Figure out where the statement is being imported to. */
11292 import_prefix = determine_prefix (die, cu);
11293
11294 /* Figure out what the scope of the imported die is and prepend it
11295 to the name of the imported die. */
11296 imported_name_prefix = determine_prefix (imported_die, imported_cu);
11297
11298 if (imported_die->tag != DW_TAG_namespace
11299 && imported_die->tag != DW_TAG_module)
11300 {
11301 imported_declaration = imported_name;
11302 canonical_name = imported_name_prefix;
11303 }
11304 else if (strlen (imported_name_prefix) > 0)
11305 canonical_name = obconcat (&objfile->objfile_obstack,
11306 imported_name_prefix,
11307 (cu->language == language_d ? "." : "::"),
11308 imported_name, (char *) NULL);
11309 else
11310 canonical_name = imported_name;
11311
11312 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
11313 for (child_die = die->child; child_die && child_die->tag;
11314 child_die = sibling_die (child_die))
11315 {
11316 /* DWARF-4: A Fortran use statement with a “rename list” may be
11317 represented by an imported module entry with an import attribute
11318 referring to the module and owned entries corresponding to those
11319 entities that are renamed as part of being imported. */
11320
11321 if (child_die->tag != DW_TAG_imported_declaration)
11322 {
11323 complaint (_("child DW_TAG_imported_declaration expected "
11324 "- DIE at %s [in module %s]"),
11325 sect_offset_str (child_die->sect_off),
11326 objfile_name (objfile));
11327 continue;
11328 }
11329
11330 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
11331 if (import_attr == NULL)
11332 {
11333 complaint (_("Tag '%s' has no DW_AT_import"),
11334 dwarf_tag_name (child_die->tag));
11335 continue;
11336 }
11337
11338 imported_cu = cu;
11339 imported_die = follow_die_ref_or_sig (child_die, import_attr,
11340 &imported_cu);
11341 imported_name = dwarf2_name (imported_die, imported_cu);
11342 if (imported_name == NULL)
11343 {
11344 complaint (_("child DW_TAG_imported_declaration has unknown "
11345 "imported name - DIE at %s [in module %s]"),
11346 sect_offset_str (child_die->sect_off),
11347 objfile_name (objfile));
11348 continue;
11349 }
11350
11351 excludes.push_back (imported_name);
11352
11353 process_die (child_die, cu);
11354 }
11355
11356 add_using_directive (using_directives (cu),
11357 import_prefix,
11358 canonical_name,
11359 import_alias,
11360 imported_declaration,
11361 excludes,
11362 0,
11363 &objfile->objfile_obstack);
11364 }
11365
11366 /* ICC<14 does not output the required DW_AT_declaration on incomplete
11367 types, but gives them a size of zero. Starting with version 14,
11368 ICC is compatible with GCC. */
11369
11370 static bool
11371 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
11372 {
11373 if (!cu->checked_producer)
11374 check_producer (cu);
11375
11376 return cu->producer_is_icc_lt_14;
11377 }
11378
11379 /* ICC generates a DW_AT_type for C void functions. This was observed on
11380 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
11381 which says that void functions should not have a DW_AT_type. */
11382
11383 static bool
11384 producer_is_icc (struct dwarf2_cu *cu)
11385 {
11386 if (!cu->checked_producer)
11387 check_producer (cu);
11388
11389 return cu->producer_is_icc;
11390 }
11391
11392 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11393 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11394 this, it was first present in GCC release 4.3.0. */
11395
11396 static bool
11397 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11398 {
11399 if (!cu->checked_producer)
11400 check_producer (cu);
11401
11402 return cu->producer_is_gcc_lt_4_3;
11403 }
11404
11405 static file_and_directory
11406 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
11407 {
11408 file_and_directory res;
11409
11410 /* Find the filename. Do not use dwarf2_name here, since the filename
11411 is not a source language identifier. */
11412 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11413 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
11414
11415 if (res.comp_dir == NULL
11416 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11417 && IS_ABSOLUTE_PATH (res.name))
11418 {
11419 res.comp_dir_storage = ldirname (res.name);
11420 if (!res.comp_dir_storage.empty ())
11421 res.comp_dir = res.comp_dir_storage.c_str ();
11422 }
11423 if (res.comp_dir != NULL)
11424 {
11425 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11426 directory, get rid of it. */
11427 const char *cp = strchr (res.comp_dir, ':');
11428
11429 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11430 res.comp_dir = cp + 1;
11431 }
11432
11433 if (res.name == NULL)
11434 res.name = "<unknown>";
11435
11436 return res;
11437 }
11438
11439 /* Handle DW_AT_stmt_list for a compilation unit.
11440 DIE is the DW_TAG_compile_unit die for CU.
11441 COMP_DIR is the compilation directory. LOWPC is passed to
11442 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
11443
11444 static void
11445 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
11446 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
11447 {
11448 struct dwarf2_per_objfile *dwarf2_per_objfile
11449 = cu->per_cu->dwarf2_per_objfile;
11450 struct objfile *objfile = dwarf2_per_objfile->objfile;
11451 struct attribute *attr;
11452 struct line_header line_header_local;
11453 hashval_t line_header_local_hash;
11454 void **slot;
11455 int decode_mapping;
11456
11457 gdb_assert (! cu->per_cu->is_debug_types);
11458
11459 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
11460 if (attr == NULL)
11461 return;
11462
11463 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11464
11465 /* The line header hash table is only created if needed (it exists to
11466 prevent redundant reading of the line table for partial_units).
11467 If we're given a partial_unit, we'll need it. If we're given a
11468 compile_unit, then use the line header hash table if it's already
11469 created, but don't create one just yet. */
11470
11471 if (dwarf2_per_objfile->line_header_hash == NULL
11472 && die->tag == DW_TAG_partial_unit)
11473 {
11474 dwarf2_per_objfile->line_header_hash
11475 = htab_create_alloc_ex (127, line_header_hash_voidp,
11476 line_header_eq_voidp,
11477 free_line_header_voidp,
11478 &objfile->objfile_obstack,
11479 hashtab_obstack_allocate,
11480 dummy_obstack_deallocate);
11481 }
11482
11483 line_header_local.sect_off = line_offset;
11484 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11485 line_header_local_hash = line_header_hash (&line_header_local);
11486 if (dwarf2_per_objfile->line_header_hash != NULL)
11487 {
11488 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11489 &line_header_local,
11490 line_header_local_hash, NO_INSERT);
11491
11492 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11493 is not present in *SLOT (since if there is something in *SLOT then
11494 it will be for a partial_unit). */
11495 if (die->tag == DW_TAG_partial_unit && slot != NULL)
11496 {
11497 gdb_assert (*slot != NULL);
11498 cu->line_header = (struct line_header *) *slot;
11499 return;
11500 }
11501 }
11502
11503 /* dwarf_decode_line_header does not yet provide sufficient information.
11504 We always have to call also dwarf_decode_lines for it. */
11505 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11506 if (lh == NULL)
11507 return;
11508
11509 cu->line_header = lh.release ();
11510 cu->line_header_die_owner = die;
11511
11512 if (dwarf2_per_objfile->line_header_hash == NULL)
11513 slot = NULL;
11514 else
11515 {
11516 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11517 &line_header_local,
11518 line_header_local_hash, INSERT);
11519 gdb_assert (slot != NULL);
11520 }
11521 if (slot != NULL && *slot == NULL)
11522 {
11523 /* This newly decoded line number information unit will be owned
11524 by line_header_hash hash table. */
11525 *slot = cu->line_header;
11526 cu->line_header_die_owner = NULL;
11527 }
11528 else
11529 {
11530 /* We cannot free any current entry in (*slot) as that struct line_header
11531 may be already used by multiple CUs. Create only temporary decoded
11532 line_header for this CU - it may happen at most once for each line
11533 number information unit. And if we're not using line_header_hash
11534 then this is what we want as well. */
11535 gdb_assert (die->tag != DW_TAG_partial_unit);
11536 }
11537 decode_mapping = (die->tag != DW_TAG_partial_unit);
11538 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11539 decode_mapping);
11540
11541 }
11542
11543 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
11544
11545 static void
11546 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
11547 {
11548 struct dwarf2_per_objfile *dwarf2_per_objfile
11549 = cu->per_cu->dwarf2_per_objfile;
11550 struct objfile *objfile = dwarf2_per_objfile->objfile;
11551 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11552 CORE_ADDR lowpc = ((CORE_ADDR) -1);
11553 CORE_ADDR highpc = ((CORE_ADDR) 0);
11554 struct attribute *attr;
11555 struct die_info *child_die;
11556 CORE_ADDR baseaddr;
11557
11558 prepare_one_comp_unit (cu, die, cu->language);
11559 baseaddr = objfile->text_section_offset ();
11560
11561 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
11562
11563 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11564 from finish_block. */
11565 if (lowpc == ((CORE_ADDR) -1))
11566 lowpc = highpc;
11567 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11568
11569 file_and_directory fnd = find_file_and_directory (die, cu);
11570
11571 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11572 standardised yet. As a workaround for the language detection we fall
11573 back to the DW_AT_producer string. */
11574 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11575 cu->language = language_opencl;
11576
11577 /* Similar hack for Go. */
11578 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11579 set_cu_language (DW_LANG_Go, cu);
11580
11581 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
11582
11583 /* Decode line number information if present. We do this before
11584 processing child DIEs, so that the line header table is available
11585 for DW_AT_decl_file. */
11586 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
11587
11588 /* Process all dies in compilation unit. */
11589 if (die->child != NULL)
11590 {
11591 child_die = die->child;
11592 while (child_die && child_die->tag)
11593 {
11594 process_die (child_die, cu);
11595 child_die = sibling_die (child_die);
11596 }
11597 }
11598
11599 /* Decode macro information, if present. Dwarf 2 macro information
11600 refers to information in the line number info statement program
11601 header, so we can only read it if we've read the header
11602 successfully. */
11603 attr = dwarf2_attr (die, DW_AT_macros, cu);
11604 if (attr == NULL)
11605 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
11606 if (attr && cu->line_header)
11607 {
11608 if (dwarf2_attr (die, DW_AT_macro_info, cu))
11609 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
11610
11611 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
11612 }
11613 else
11614 {
11615 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11616 if (attr && cu->line_header)
11617 {
11618 unsigned int macro_offset = DW_UNSND (attr);
11619
11620 dwarf_decode_macros (cu, macro_offset, 0);
11621 }
11622 }
11623 }
11624
11625 void
11626 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
11627 {
11628 struct type_unit_group *tu_group;
11629 int first_time;
11630 struct attribute *attr;
11631 unsigned int i;
11632 struct signatured_type *sig_type;
11633
11634 gdb_assert (per_cu->is_debug_types);
11635 sig_type = (struct signatured_type *) per_cu;
11636
11637 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
11638
11639 /* If we're using .gdb_index (includes -readnow) then
11640 per_cu->type_unit_group may not have been set up yet. */
11641 if (sig_type->type_unit_group == NULL)
11642 sig_type->type_unit_group = get_type_unit_group (this, attr);
11643 tu_group = sig_type->type_unit_group;
11644
11645 /* If we've already processed this stmt_list there's no real need to
11646 do it again, we could fake it and just recreate the part we need
11647 (file name,index -> symtab mapping). If data shows this optimization
11648 is useful we can do it then. */
11649 first_time = tu_group->compunit_symtab == NULL;
11650
11651 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11652 debug info. */
11653 line_header_up lh;
11654 if (attr != NULL)
11655 {
11656 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11657 lh = dwarf_decode_line_header (line_offset, this);
11658 }
11659 if (lh == NULL)
11660 {
11661 if (first_time)
11662 start_symtab ("", NULL, 0);
11663 else
11664 {
11665 gdb_assert (tu_group->symtabs == NULL);
11666 gdb_assert (m_builder == nullptr);
11667 struct compunit_symtab *cust = tu_group->compunit_symtab;
11668 m_builder.reset (new struct buildsym_compunit
11669 (COMPUNIT_OBJFILE (cust), "",
11670 COMPUNIT_DIRNAME (cust),
11671 compunit_language (cust),
11672 0, cust));
11673 }
11674 return;
11675 }
11676
11677 line_header = lh.release ();
11678 line_header_die_owner = die;
11679
11680 if (first_time)
11681 {
11682 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
11683
11684 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11685 still initializing it, and our caller (a few levels up)
11686 process_full_type_unit still needs to know if this is the first
11687 time. */
11688
11689 tu_group->num_symtabs = line_header->file_names_size ();
11690 tu_group->symtabs = XNEWVEC (struct symtab *,
11691 line_header->file_names_size ());
11692
11693 auto &file_names = line_header->file_names ();
11694 for (i = 0; i < file_names.size (); ++i)
11695 {
11696 file_entry &fe = file_names[i];
11697 dwarf2_start_subfile (this, fe.name,
11698 fe.include_dir (line_header));
11699 buildsym_compunit *b = get_builder ();
11700 if (b->get_current_subfile ()->symtab == NULL)
11701 {
11702 /* NOTE: start_subfile will recognize when it's been
11703 passed a file it has already seen. So we can't
11704 assume there's a simple mapping from
11705 cu->line_header->file_names to subfiles, plus
11706 cu->line_header->file_names may contain dups. */
11707 b->get_current_subfile ()->symtab
11708 = allocate_symtab (cust, b->get_current_subfile ()->name);
11709 }
11710
11711 fe.symtab = b->get_current_subfile ()->symtab;
11712 tu_group->symtabs[i] = fe.symtab;
11713 }
11714 }
11715 else
11716 {
11717 gdb_assert (m_builder == nullptr);
11718 struct compunit_symtab *cust = tu_group->compunit_symtab;
11719 m_builder.reset (new struct buildsym_compunit
11720 (COMPUNIT_OBJFILE (cust), "",
11721 COMPUNIT_DIRNAME (cust),
11722 compunit_language (cust),
11723 0, cust));
11724
11725 auto &file_names = line_header->file_names ();
11726 for (i = 0; i < file_names.size (); ++i)
11727 {
11728 file_entry &fe = file_names[i];
11729 fe.symtab = tu_group->symtabs[i];
11730 }
11731 }
11732
11733 /* The main symtab is allocated last. Type units don't have DW_AT_name
11734 so they don't have a "real" (so to speak) symtab anyway.
11735 There is later code that will assign the main symtab to all symbols
11736 that don't have one. We need to handle the case of a symbol with a
11737 missing symtab (DW_AT_decl_file) anyway. */
11738 }
11739
11740 /* Process DW_TAG_type_unit.
11741 For TUs we want to skip the first top level sibling if it's not the
11742 actual type being defined by this TU. In this case the first top
11743 level sibling is there to provide context only. */
11744
11745 static void
11746 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11747 {
11748 struct die_info *child_die;
11749
11750 prepare_one_comp_unit (cu, die, language_minimal);
11751
11752 /* Initialize (or reinitialize) the machinery for building symtabs.
11753 We do this before processing child DIEs, so that the line header table
11754 is available for DW_AT_decl_file. */
11755 cu->setup_type_unit_groups (die);
11756
11757 if (die->child != NULL)
11758 {
11759 child_die = die->child;
11760 while (child_die && child_die->tag)
11761 {
11762 process_die (child_die, cu);
11763 child_die = sibling_die (child_die);
11764 }
11765 }
11766 }
11767 \f
11768 /* DWO/DWP files.
11769
11770 http://gcc.gnu.org/wiki/DebugFission
11771 http://gcc.gnu.org/wiki/DebugFissionDWP
11772
11773 To simplify handling of both DWO files ("object" files with the DWARF info)
11774 and DWP files (a file with the DWOs packaged up into one file), we treat
11775 DWP files as having a collection of virtual DWO files. */
11776
11777 static hashval_t
11778 hash_dwo_file (const void *item)
11779 {
11780 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
11781 hashval_t hash;
11782
11783 hash = htab_hash_string (dwo_file->dwo_name);
11784 if (dwo_file->comp_dir != NULL)
11785 hash += htab_hash_string (dwo_file->comp_dir);
11786 return hash;
11787 }
11788
11789 static int
11790 eq_dwo_file (const void *item_lhs, const void *item_rhs)
11791 {
11792 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11793 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
11794
11795 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11796 return 0;
11797 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11798 return lhs->comp_dir == rhs->comp_dir;
11799 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
11800 }
11801
11802 /* Allocate a hash table for DWO files. */
11803
11804 static htab_up
11805 allocate_dwo_file_hash_table (struct objfile *objfile)
11806 {
11807 auto delete_dwo_file = [] (void *item)
11808 {
11809 struct dwo_file *dwo_file = (struct dwo_file *) item;
11810
11811 delete dwo_file;
11812 };
11813
11814 return htab_up (htab_create_alloc_ex (41,
11815 hash_dwo_file,
11816 eq_dwo_file,
11817 delete_dwo_file,
11818 &objfile->objfile_obstack,
11819 hashtab_obstack_allocate,
11820 dummy_obstack_deallocate));
11821 }
11822
11823 /* Lookup DWO file DWO_NAME. */
11824
11825 static void **
11826 lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11827 const char *dwo_name,
11828 const char *comp_dir)
11829 {
11830 struct dwo_file find_entry;
11831 void **slot;
11832
11833 if (dwarf2_per_objfile->dwo_files == NULL)
11834 dwarf2_per_objfile->dwo_files
11835 = allocate_dwo_file_hash_table (dwarf2_per_objfile->objfile);
11836
11837 find_entry.dwo_name = dwo_name;
11838 find_entry.comp_dir = comp_dir;
11839 slot = htab_find_slot (dwarf2_per_objfile->dwo_files.get (), &find_entry,
11840 INSERT);
11841
11842 return slot;
11843 }
11844
11845 static hashval_t
11846 hash_dwo_unit (const void *item)
11847 {
11848 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11849
11850 /* This drops the top 32 bits of the id, but is ok for a hash. */
11851 return dwo_unit->signature;
11852 }
11853
11854 static int
11855 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11856 {
11857 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11858 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11859
11860 /* The signature is assumed to be unique within the DWO file.
11861 So while object file CU dwo_id's always have the value zero,
11862 that's OK, assuming each object file DWO file has only one CU,
11863 and that's the rule for now. */
11864 return lhs->signature == rhs->signature;
11865 }
11866
11867 /* Allocate a hash table for DWO CUs,TUs.
11868 There is one of these tables for each of CUs,TUs for each DWO file. */
11869
11870 static htab_t
11871 allocate_dwo_unit_table (struct objfile *objfile)
11872 {
11873 /* Start out with a pretty small number.
11874 Generally DWO files contain only one CU and maybe some TUs. */
11875 return htab_create_alloc_ex (3,
11876 hash_dwo_unit,
11877 eq_dwo_unit,
11878 NULL,
11879 &objfile->objfile_obstack,
11880 hashtab_obstack_allocate,
11881 dummy_obstack_deallocate);
11882 }
11883
11884 /* die_reader_func for create_dwo_cu. */
11885
11886 static void
11887 create_dwo_cu_reader (const struct die_reader_specs *reader,
11888 const gdb_byte *info_ptr,
11889 struct die_info *comp_unit_die,
11890 int has_children,
11891 struct dwo_file *dwo_file,
11892 struct dwo_unit *dwo_unit)
11893 {
11894 struct dwarf2_cu *cu = reader->cu;
11895 sect_offset sect_off = cu->per_cu->sect_off;
11896 struct dwarf2_section_info *section = cu->per_cu->section;
11897
11898 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11899 if (!signature.has_value ())
11900 {
11901 complaint (_("Dwarf Error: debug entry at offset %s is missing"
11902 " its dwo_id [in module %s]"),
11903 sect_offset_str (sect_off), dwo_file->dwo_name);
11904 return;
11905 }
11906
11907 dwo_unit->dwo_file = dwo_file;
11908 dwo_unit->signature = *signature;
11909 dwo_unit->section = section;
11910 dwo_unit->sect_off = sect_off;
11911 dwo_unit->length = cu->per_cu->length;
11912
11913 if (dwarf_read_debug)
11914 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11915 sect_offset_str (sect_off),
11916 hex_string (dwo_unit->signature));
11917 }
11918
11919 /* Create the dwo_units for the CUs in a DWO_FILE.
11920 Note: This function processes DWO files only, not DWP files. */
11921
11922 static void
11923 create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11924 dwarf2_cu *cu, struct dwo_file &dwo_file,
11925 dwarf2_section_info &section, htab_t &cus_htab)
11926 {
11927 struct objfile *objfile = dwarf2_per_objfile->objfile;
11928 const gdb_byte *info_ptr, *end_ptr;
11929
11930 dwarf2_read_section (objfile, &section);
11931 info_ptr = section.buffer;
11932
11933 if (info_ptr == NULL)
11934 return;
11935
11936 if (dwarf_read_debug)
11937 {
11938 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
11939 get_section_name (&section),
11940 get_section_file_name (&section));
11941 }
11942
11943 end_ptr = info_ptr + section.size;
11944 while (info_ptr < end_ptr)
11945 {
11946 struct dwarf2_per_cu_data per_cu;
11947 struct dwo_unit read_unit {};
11948 struct dwo_unit *dwo_unit;
11949 void **slot;
11950 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11951
11952 memset (&per_cu, 0, sizeof (per_cu));
11953 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
11954 per_cu.is_debug_types = 0;
11955 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11956 per_cu.section = &section;
11957
11958 cutu_reader reader (&per_cu, cu, &dwo_file);
11959 if (!reader.dummy_p)
11960 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
11961 reader.has_children, &dwo_file, &read_unit);
11962 info_ptr += per_cu.length;
11963
11964 // If the unit could not be parsed, skip it.
11965 if (read_unit.dwo_file == NULL)
11966 continue;
11967
11968 if (cus_htab == NULL)
11969 cus_htab = allocate_dwo_unit_table (objfile);
11970
11971 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11972 *dwo_unit = read_unit;
11973 slot = htab_find_slot (cus_htab, dwo_unit, INSERT);
11974 gdb_assert (slot != NULL);
11975 if (*slot != NULL)
11976 {
11977 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11978 sect_offset dup_sect_off = dup_cu->sect_off;
11979
11980 complaint (_("debug cu entry at offset %s is duplicate to"
11981 " the entry at offset %s, signature %s"),
11982 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
11983 hex_string (dwo_unit->signature));
11984 }
11985 *slot = (void *)dwo_unit;
11986 }
11987 }
11988
11989 /* DWP file .debug_{cu,tu}_index section format:
11990 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11991
11992 DWP Version 1:
11993
11994 Both index sections have the same format, and serve to map a 64-bit
11995 signature to a set of section numbers. Each section begins with a header,
11996 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11997 indexes, and a pool of 32-bit section numbers. The index sections will be
11998 aligned at 8-byte boundaries in the file.
11999
12000 The index section header consists of:
12001
12002 V, 32 bit version number
12003 -, 32 bits unused
12004 N, 32 bit number of compilation units or type units in the index
12005 M, 32 bit number of slots in the hash table
12006
12007 Numbers are recorded using the byte order of the application binary.
12008
12009 The hash table begins at offset 16 in the section, and consists of an array
12010 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
12011 order of the application binary). Unused slots in the hash table are 0.
12012 (We rely on the extreme unlikeliness of a signature being exactly 0.)
12013
12014 The parallel table begins immediately after the hash table
12015 (at offset 16 + 8 * M from the beginning of the section), and consists of an
12016 array of 32-bit indexes (using the byte order of the application binary),
12017 corresponding 1-1 with slots in the hash table. Each entry in the parallel
12018 table contains a 32-bit index into the pool of section numbers. For unused
12019 hash table slots, the corresponding entry in the parallel table will be 0.
12020
12021 The pool of section numbers begins immediately following the hash table
12022 (at offset 16 + 12 * M from the beginning of the section). The pool of
12023 section numbers consists of an array of 32-bit words (using the byte order
12024 of the application binary). Each item in the array is indexed starting
12025 from 0. The hash table entry provides the index of the first section
12026 number in the set. Additional section numbers in the set follow, and the
12027 set is terminated by a 0 entry (section number 0 is not used in ELF).
12028
12029 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
12030 section must be the first entry in the set, and the .debug_abbrev.dwo must
12031 be the second entry. Other members of the set may follow in any order.
12032
12033 ---
12034
12035 DWP Version 2:
12036
12037 DWP Version 2 combines all the .debug_info, etc. sections into one,
12038 and the entries in the index tables are now offsets into these sections.
12039 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
12040 section.
12041
12042 Index Section Contents:
12043 Header
12044 Hash Table of Signatures dwp_hash_table.hash_table
12045 Parallel Table of Indices dwp_hash_table.unit_table
12046 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
12047 Table of Section Sizes dwp_hash_table.v2.sizes
12048
12049 The index section header consists of:
12050
12051 V, 32 bit version number
12052 L, 32 bit number of columns in the table of section offsets
12053 N, 32 bit number of compilation units or type units in the index
12054 M, 32 bit number of slots in the hash table
12055
12056 Numbers are recorded using the byte order of the application binary.
12057
12058 The hash table has the same format as version 1.
12059 The parallel table of indices has the same format as version 1,
12060 except that the entries are origin-1 indices into the table of sections
12061 offsets and the table of section sizes.
12062
12063 The table of offsets begins immediately following the parallel table
12064 (at offset 16 + 12 * M from the beginning of the section). The table is
12065 a two-dimensional array of 32-bit words (using the byte order of the
12066 application binary), with L columns and N+1 rows, in row-major order.
12067 Each row in the array is indexed starting from 0. The first row provides
12068 a key to the remaining rows: each column in this row provides an identifier
12069 for a debug section, and the offsets in the same column of subsequent rows
12070 refer to that section. The section identifiers are:
12071
12072 DW_SECT_INFO 1 .debug_info.dwo
12073 DW_SECT_TYPES 2 .debug_types.dwo
12074 DW_SECT_ABBREV 3 .debug_abbrev.dwo
12075 DW_SECT_LINE 4 .debug_line.dwo
12076 DW_SECT_LOC 5 .debug_loc.dwo
12077 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
12078 DW_SECT_MACINFO 7 .debug_macinfo.dwo
12079 DW_SECT_MACRO 8 .debug_macro.dwo
12080
12081 The offsets provided by the CU and TU index sections are the base offsets
12082 for the contributions made by each CU or TU to the corresponding section
12083 in the package file. Each CU and TU header contains an abbrev_offset
12084 field, used to find the abbreviations table for that CU or TU within the
12085 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
12086 be interpreted as relative to the base offset given in the index section.
12087 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
12088 should be interpreted as relative to the base offset for .debug_line.dwo,
12089 and offsets into other debug sections obtained from DWARF attributes should
12090 also be interpreted as relative to the corresponding base offset.
12091
12092 The table of sizes begins immediately following the table of offsets.
12093 Like the table of offsets, it is a two-dimensional array of 32-bit words,
12094 with L columns and N rows, in row-major order. Each row in the array is
12095 indexed starting from 1 (row 0 is shared by the two tables).
12096
12097 ---
12098
12099 Hash table lookup is handled the same in version 1 and 2:
12100
12101 We assume that N and M will not exceed 2^32 - 1.
12102 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
12103
12104 Given a 64-bit compilation unit signature or a type signature S, an entry
12105 in the hash table is located as follows:
12106
12107 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
12108 the low-order k bits all set to 1.
12109
12110 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
12111
12112 3) If the hash table entry at index H matches the signature, use that
12113 entry. If the hash table entry at index H is unused (all zeroes),
12114 terminate the search: the signature is not present in the table.
12115
12116 4) Let H = (H + H') modulo M. Repeat at Step 3.
12117
12118 Because M > N and H' and M are relatively prime, the search is guaranteed
12119 to stop at an unused slot or find the match. */
12120
12121 /* Create a hash table to map DWO IDs to their CU/TU entry in
12122 .debug_{info,types}.dwo in DWP_FILE.
12123 Returns NULL if there isn't one.
12124 Note: This function processes DWP files only, not DWO files. */
12125
12126 static struct dwp_hash_table *
12127 create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
12128 struct dwp_file *dwp_file, int is_debug_types)
12129 {
12130 struct objfile *objfile = dwarf2_per_objfile->objfile;
12131 bfd *dbfd = dwp_file->dbfd.get ();
12132 const gdb_byte *index_ptr, *index_end;
12133 struct dwarf2_section_info *index;
12134 uint32_t version, nr_columns, nr_units, nr_slots;
12135 struct dwp_hash_table *htab;
12136
12137 if (is_debug_types)
12138 index = &dwp_file->sections.tu_index;
12139 else
12140 index = &dwp_file->sections.cu_index;
12141
12142 if (dwarf2_section_empty_p (index))
12143 return NULL;
12144 dwarf2_read_section (objfile, index);
12145
12146 index_ptr = index->buffer;
12147 index_end = index_ptr + index->size;
12148
12149 version = read_4_bytes (dbfd, index_ptr);
12150 index_ptr += 4;
12151 if (version == 2)
12152 nr_columns = read_4_bytes (dbfd, index_ptr);
12153 else
12154 nr_columns = 0;
12155 index_ptr += 4;
12156 nr_units = read_4_bytes (dbfd, index_ptr);
12157 index_ptr += 4;
12158 nr_slots = read_4_bytes (dbfd, index_ptr);
12159 index_ptr += 4;
12160
12161 if (version != 1 && version != 2)
12162 {
12163 error (_("Dwarf Error: unsupported DWP file version (%s)"
12164 " [in module %s]"),
12165 pulongest (version), dwp_file->name);
12166 }
12167 if (nr_slots != (nr_slots & -nr_slots))
12168 {
12169 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
12170 " is not power of 2 [in module %s]"),
12171 pulongest (nr_slots), dwp_file->name);
12172 }
12173
12174 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
12175 htab->version = version;
12176 htab->nr_columns = nr_columns;
12177 htab->nr_units = nr_units;
12178 htab->nr_slots = nr_slots;
12179 htab->hash_table = index_ptr;
12180 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
12181
12182 /* Exit early if the table is empty. */
12183 if (nr_slots == 0 || nr_units == 0
12184 || (version == 2 && nr_columns == 0))
12185 {
12186 /* All must be zero. */
12187 if (nr_slots != 0 || nr_units != 0
12188 || (version == 2 && nr_columns != 0))
12189 {
12190 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
12191 " all zero [in modules %s]"),
12192 dwp_file->name);
12193 }
12194 return htab;
12195 }
12196
12197 if (version == 1)
12198 {
12199 htab->section_pool.v1.indices =
12200 htab->unit_table + sizeof (uint32_t) * nr_slots;
12201 /* It's harder to decide whether the section is too small in v1.
12202 V1 is deprecated anyway so we punt. */
12203 }
12204 else
12205 {
12206 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
12207 int *ids = htab->section_pool.v2.section_ids;
12208 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
12209 /* Reverse map for error checking. */
12210 int ids_seen[DW_SECT_MAX + 1];
12211 int i;
12212
12213 if (nr_columns < 2)
12214 {
12215 error (_("Dwarf Error: bad DWP hash table, too few columns"
12216 " in section table [in module %s]"),
12217 dwp_file->name);
12218 }
12219 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
12220 {
12221 error (_("Dwarf Error: bad DWP hash table, too many columns"
12222 " in section table [in module %s]"),
12223 dwp_file->name);
12224 }
12225 memset (ids, 255, sizeof_ids);
12226 memset (ids_seen, 255, sizeof (ids_seen));
12227 for (i = 0; i < nr_columns; ++i)
12228 {
12229 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
12230
12231 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
12232 {
12233 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
12234 " in section table [in module %s]"),
12235 id, dwp_file->name);
12236 }
12237 if (ids_seen[id] != -1)
12238 {
12239 error (_("Dwarf Error: bad DWP hash table, duplicate section"
12240 " id %d in section table [in module %s]"),
12241 id, dwp_file->name);
12242 }
12243 ids_seen[id] = i;
12244 ids[i] = id;
12245 }
12246 /* Must have exactly one info or types section. */
12247 if (((ids_seen[DW_SECT_INFO] != -1)
12248 + (ids_seen[DW_SECT_TYPES] != -1))
12249 != 1)
12250 {
12251 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
12252 " DWO info/types section [in module %s]"),
12253 dwp_file->name);
12254 }
12255 /* Must have an abbrev section. */
12256 if (ids_seen[DW_SECT_ABBREV] == -1)
12257 {
12258 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
12259 " section [in module %s]"),
12260 dwp_file->name);
12261 }
12262 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
12263 htab->section_pool.v2.sizes =
12264 htab->section_pool.v2.offsets + (sizeof (uint32_t)
12265 * nr_units * nr_columns);
12266 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
12267 * nr_units * nr_columns))
12268 > index_end)
12269 {
12270 error (_("Dwarf Error: DWP index section is corrupt (too small)"
12271 " [in module %s]"),
12272 dwp_file->name);
12273 }
12274 }
12275
12276 return htab;
12277 }
12278
12279 /* Update SECTIONS with the data from SECTP.
12280
12281 This function is like the other "locate" section routines that are
12282 passed to bfd_map_over_sections, but in this context the sections to
12283 read comes from the DWP V1 hash table, not the full ELF section table.
12284
12285 The result is non-zero for success, or zero if an error was found. */
12286
12287 static int
12288 locate_v1_virtual_dwo_sections (asection *sectp,
12289 struct virtual_v1_dwo_sections *sections)
12290 {
12291 const struct dwop_section_names *names = &dwop_section_names;
12292
12293 if (section_is_p (sectp->name, &names->abbrev_dwo))
12294 {
12295 /* There can be only one. */
12296 if (sections->abbrev.s.section != NULL)
12297 return 0;
12298 sections->abbrev.s.section = sectp;
12299 sections->abbrev.size = bfd_section_size (sectp);
12300 }
12301 else if (section_is_p (sectp->name, &names->info_dwo)
12302 || section_is_p (sectp->name, &names->types_dwo))
12303 {
12304 /* There can be only one. */
12305 if (sections->info_or_types.s.section != NULL)
12306 return 0;
12307 sections->info_or_types.s.section = sectp;
12308 sections->info_or_types.size = bfd_section_size (sectp);
12309 }
12310 else if (section_is_p (sectp->name, &names->line_dwo))
12311 {
12312 /* There can be only one. */
12313 if (sections->line.s.section != NULL)
12314 return 0;
12315 sections->line.s.section = sectp;
12316 sections->line.size = bfd_section_size (sectp);
12317 }
12318 else if (section_is_p (sectp->name, &names->loc_dwo))
12319 {
12320 /* There can be only one. */
12321 if (sections->loc.s.section != NULL)
12322 return 0;
12323 sections->loc.s.section = sectp;
12324 sections->loc.size = bfd_section_size (sectp);
12325 }
12326 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12327 {
12328 /* There can be only one. */
12329 if (sections->macinfo.s.section != NULL)
12330 return 0;
12331 sections->macinfo.s.section = sectp;
12332 sections->macinfo.size = bfd_section_size (sectp);
12333 }
12334 else if (section_is_p (sectp->name, &names->macro_dwo))
12335 {
12336 /* There can be only one. */
12337 if (sections->macro.s.section != NULL)
12338 return 0;
12339 sections->macro.s.section = sectp;
12340 sections->macro.size = bfd_section_size (sectp);
12341 }
12342 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12343 {
12344 /* There can be only one. */
12345 if (sections->str_offsets.s.section != NULL)
12346 return 0;
12347 sections->str_offsets.s.section = sectp;
12348 sections->str_offsets.size = bfd_section_size (sectp);
12349 }
12350 else
12351 {
12352 /* No other kind of section is valid. */
12353 return 0;
12354 }
12355
12356 return 1;
12357 }
12358
12359 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12360 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12361 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12362 This is for DWP version 1 files. */
12363
12364 static struct dwo_unit *
12365 create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12366 struct dwp_file *dwp_file,
12367 uint32_t unit_index,
12368 const char *comp_dir,
12369 ULONGEST signature, int is_debug_types)
12370 {
12371 struct objfile *objfile = dwarf2_per_objfile->objfile;
12372 const struct dwp_hash_table *dwp_htab =
12373 is_debug_types ? dwp_file->tus : dwp_file->cus;
12374 bfd *dbfd = dwp_file->dbfd.get ();
12375 const char *kind = is_debug_types ? "TU" : "CU";
12376 struct dwo_file *dwo_file;
12377 struct dwo_unit *dwo_unit;
12378 struct virtual_v1_dwo_sections sections;
12379 void **dwo_file_slot;
12380 int i;
12381
12382 gdb_assert (dwp_file->version == 1);
12383
12384 if (dwarf_read_debug)
12385 {
12386 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
12387 kind,
12388 pulongest (unit_index), hex_string (signature),
12389 dwp_file->name);
12390 }
12391
12392 /* Fetch the sections of this DWO unit.
12393 Put a limit on the number of sections we look for so that bad data
12394 doesn't cause us to loop forever. */
12395
12396 #define MAX_NR_V1_DWO_SECTIONS \
12397 (1 /* .debug_info or .debug_types */ \
12398 + 1 /* .debug_abbrev */ \
12399 + 1 /* .debug_line */ \
12400 + 1 /* .debug_loc */ \
12401 + 1 /* .debug_str_offsets */ \
12402 + 1 /* .debug_macro or .debug_macinfo */ \
12403 + 1 /* trailing zero */)
12404
12405 memset (&sections, 0, sizeof (sections));
12406
12407 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
12408 {
12409 asection *sectp;
12410 uint32_t section_nr =
12411 read_4_bytes (dbfd,
12412 dwp_htab->section_pool.v1.indices
12413 + (unit_index + i) * sizeof (uint32_t));
12414
12415 if (section_nr == 0)
12416 break;
12417 if (section_nr >= dwp_file->num_sections)
12418 {
12419 error (_("Dwarf Error: bad DWP hash table, section number too large"
12420 " [in module %s]"),
12421 dwp_file->name);
12422 }
12423
12424 sectp = dwp_file->elf_sections[section_nr];
12425 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
12426 {
12427 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12428 " [in module %s]"),
12429 dwp_file->name);
12430 }
12431 }
12432
12433 if (i < 2
12434 || dwarf2_section_empty_p (&sections.info_or_types)
12435 || dwarf2_section_empty_p (&sections.abbrev))
12436 {
12437 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12438 " [in module %s]"),
12439 dwp_file->name);
12440 }
12441 if (i == MAX_NR_V1_DWO_SECTIONS)
12442 {
12443 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12444 " [in module %s]"),
12445 dwp_file->name);
12446 }
12447
12448 /* It's easier for the rest of the code if we fake a struct dwo_file and
12449 have dwo_unit "live" in that. At least for now.
12450
12451 The DWP file can be made up of a random collection of CUs and TUs.
12452 However, for each CU + set of TUs that came from the same original DWO
12453 file, we can combine them back into a virtual DWO file to save space
12454 (fewer struct dwo_file objects to allocate). Remember that for really
12455 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12456
12457 std::string virtual_dwo_name =
12458 string_printf ("virtual-dwo/%d-%d-%d-%d",
12459 get_section_id (&sections.abbrev),
12460 get_section_id (&sections.line),
12461 get_section_id (&sections.loc),
12462 get_section_id (&sections.str_offsets));
12463 /* Can we use an existing virtual DWO file? */
12464 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12465 virtual_dwo_name.c_str (),
12466 comp_dir);
12467 /* Create one if necessary. */
12468 if (*dwo_file_slot == NULL)
12469 {
12470 if (dwarf_read_debug)
12471 {
12472 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12473 virtual_dwo_name.c_str ());
12474 }
12475 dwo_file = new struct dwo_file;
12476 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
12477 virtual_dwo_name);
12478 dwo_file->comp_dir = comp_dir;
12479 dwo_file->sections.abbrev = sections.abbrev;
12480 dwo_file->sections.line = sections.line;
12481 dwo_file->sections.loc = sections.loc;
12482 dwo_file->sections.macinfo = sections.macinfo;
12483 dwo_file->sections.macro = sections.macro;
12484 dwo_file->sections.str_offsets = sections.str_offsets;
12485 /* The "str" section is global to the entire DWP file. */
12486 dwo_file->sections.str = dwp_file->sections.str;
12487 /* The info or types section is assigned below to dwo_unit,
12488 there's no need to record it in dwo_file.
12489 Also, we can't simply record type sections in dwo_file because
12490 we record a pointer into the vector in dwo_unit. As we collect more
12491 types we'll grow the vector and eventually have to reallocate space
12492 for it, invalidating all copies of pointers into the previous
12493 contents. */
12494 *dwo_file_slot = dwo_file;
12495 }
12496 else
12497 {
12498 if (dwarf_read_debug)
12499 {
12500 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12501 virtual_dwo_name.c_str ());
12502 }
12503 dwo_file = (struct dwo_file *) *dwo_file_slot;
12504 }
12505
12506 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12507 dwo_unit->dwo_file = dwo_file;
12508 dwo_unit->signature = signature;
12509 dwo_unit->section =
12510 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
12511 *dwo_unit->section = sections.info_or_types;
12512 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12513
12514 return dwo_unit;
12515 }
12516
12517 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
12518 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
12519 piece within that section used by a TU/CU, return a virtual section
12520 of just that piece. */
12521
12522 static struct dwarf2_section_info
12523 create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
12524 struct dwarf2_section_info *section,
12525 bfd_size_type offset, bfd_size_type size)
12526 {
12527 struct dwarf2_section_info result;
12528 asection *sectp;
12529
12530 gdb_assert (section != NULL);
12531 gdb_assert (!section->is_virtual);
12532
12533 memset (&result, 0, sizeof (result));
12534 result.s.containing_section = section;
12535 result.is_virtual = true;
12536
12537 if (size == 0)
12538 return result;
12539
12540 sectp = get_section_bfd_section (section);
12541
12542 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12543 bounds of the real section. This is a pretty-rare event, so just
12544 flag an error (easier) instead of a warning and trying to cope. */
12545 if (sectp == NULL
12546 || offset + size > bfd_section_size (sectp))
12547 {
12548 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
12549 " in section %s [in module %s]"),
12550 sectp ? bfd_section_name (sectp) : "<unknown>",
12551 objfile_name (dwarf2_per_objfile->objfile));
12552 }
12553
12554 result.virtual_offset = offset;
12555 result.size = size;
12556 return result;
12557 }
12558
12559 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12560 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12561 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12562 This is for DWP version 2 files. */
12563
12564 static struct dwo_unit *
12565 create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12566 struct dwp_file *dwp_file,
12567 uint32_t unit_index,
12568 const char *comp_dir,
12569 ULONGEST signature, int is_debug_types)
12570 {
12571 struct objfile *objfile = dwarf2_per_objfile->objfile;
12572 const struct dwp_hash_table *dwp_htab =
12573 is_debug_types ? dwp_file->tus : dwp_file->cus;
12574 bfd *dbfd = dwp_file->dbfd.get ();
12575 const char *kind = is_debug_types ? "TU" : "CU";
12576 struct dwo_file *dwo_file;
12577 struct dwo_unit *dwo_unit;
12578 struct virtual_v2_dwo_sections sections;
12579 void **dwo_file_slot;
12580 int i;
12581
12582 gdb_assert (dwp_file->version == 2);
12583
12584 if (dwarf_read_debug)
12585 {
12586 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12587 kind,
12588 pulongest (unit_index), hex_string (signature),
12589 dwp_file->name);
12590 }
12591
12592 /* Fetch the section offsets of this DWO unit. */
12593
12594 memset (&sections, 0, sizeof (sections));
12595
12596 for (i = 0; i < dwp_htab->nr_columns; ++i)
12597 {
12598 uint32_t offset = read_4_bytes (dbfd,
12599 dwp_htab->section_pool.v2.offsets
12600 + (((unit_index - 1) * dwp_htab->nr_columns
12601 + i)
12602 * sizeof (uint32_t)));
12603 uint32_t size = read_4_bytes (dbfd,
12604 dwp_htab->section_pool.v2.sizes
12605 + (((unit_index - 1) * dwp_htab->nr_columns
12606 + i)
12607 * sizeof (uint32_t)));
12608
12609 switch (dwp_htab->section_pool.v2.section_ids[i])
12610 {
12611 case DW_SECT_INFO:
12612 case DW_SECT_TYPES:
12613 sections.info_or_types_offset = offset;
12614 sections.info_or_types_size = size;
12615 break;
12616 case DW_SECT_ABBREV:
12617 sections.abbrev_offset = offset;
12618 sections.abbrev_size = size;
12619 break;
12620 case DW_SECT_LINE:
12621 sections.line_offset = offset;
12622 sections.line_size = size;
12623 break;
12624 case DW_SECT_LOC:
12625 sections.loc_offset = offset;
12626 sections.loc_size = size;
12627 break;
12628 case DW_SECT_STR_OFFSETS:
12629 sections.str_offsets_offset = offset;
12630 sections.str_offsets_size = size;
12631 break;
12632 case DW_SECT_MACINFO:
12633 sections.macinfo_offset = offset;
12634 sections.macinfo_size = size;
12635 break;
12636 case DW_SECT_MACRO:
12637 sections.macro_offset = offset;
12638 sections.macro_size = size;
12639 break;
12640 }
12641 }
12642
12643 /* It's easier for the rest of the code if we fake a struct dwo_file and
12644 have dwo_unit "live" in that. At least for now.
12645
12646 The DWP file can be made up of a random collection of CUs and TUs.
12647 However, for each CU + set of TUs that came from the same original DWO
12648 file, we can combine them back into a virtual DWO file to save space
12649 (fewer struct dwo_file objects to allocate). Remember that for really
12650 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12651
12652 std::string virtual_dwo_name =
12653 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12654 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12655 (long) (sections.line_size ? sections.line_offset : 0),
12656 (long) (sections.loc_size ? sections.loc_offset : 0),
12657 (long) (sections.str_offsets_size
12658 ? sections.str_offsets_offset : 0));
12659 /* Can we use an existing virtual DWO file? */
12660 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12661 virtual_dwo_name.c_str (),
12662 comp_dir);
12663 /* Create one if necessary. */
12664 if (*dwo_file_slot == NULL)
12665 {
12666 if (dwarf_read_debug)
12667 {
12668 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12669 virtual_dwo_name.c_str ());
12670 }
12671 dwo_file = new struct dwo_file;
12672 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
12673 virtual_dwo_name);
12674 dwo_file->comp_dir = comp_dir;
12675 dwo_file->sections.abbrev =
12676 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
12677 sections.abbrev_offset, sections.abbrev_size);
12678 dwo_file->sections.line =
12679 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
12680 sections.line_offset, sections.line_size);
12681 dwo_file->sections.loc =
12682 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
12683 sections.loc_offset, sections.loc_size);
12684 dwo_file->sections.macinfo =
12685 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
12686 sections.macinfo_offset, sections.macinfo_size);
12687 dwo_file->sections.macro =
12688 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
12689 sections.macro_offset, sections.macro_size);
12690 dwo_file->sections.str_offsets =
12691 create_dwp_v2_section (dwarf2_per_objfile,
12692 &dwp_file->sections.str_offsets,
12693 sections.str_offsets_offset,
12694 sections.str_offsets_size);
12695 /* The "str" section is global to the entire DWP file. */
12696 dwo_file->sections.str = dwp_file->sections.str;
12697 /* The info or types section is assigned below to dwo_unit,
12698 there's no need to record it in dwo_file.
12699 Also, we can't simply record type sections in dwo_file because
12700 we record a pointer into the vector in dwo_unit. As we collect more
12701 types we'll grow the vector and eventually have to reallocate space
12702 for it, invalidating all copies of pointers into the previous
12703 contents. */
12704 *dwo_file_slot = dwo_file;
12705 }
12706 else
12707 {
12708 if (dwarf_read_debug)
12709 {
12710 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12711 virtual_dwo_name.c_str ());
12712 }
12713 dwo_file = (struct dwo_file *) *dwo_file_slot;
12714 }
12715
12716 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12717 dwo_unit->dwo_file = dwo_file;
12718 dwo_unit->signature = signature;
12719 dwo_unit->section =
12720 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
12721 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12722 is_debug_types
12723 ? &dwp_file->sections.types
12724 : &dwp_file->sections.info,
12725 sections.info_or_types_offset,
12726 sections.info_or_types_size);
12727 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12728
12729 return dwo_unit;
12730 }
12731
12732 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12733 Returns NULL if the signature isn't found. */
12734
12735 static struct dwo_unit *
12736 lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12737 struct dwp_file *dwp_file, const char *comp_dir,
12738 ULONGEST signature, int is_debug_types)
12739 {
12740 const struct dwp_hash_table *dwp_htab =
12741 is_debug_types ? dwp_file->tus : dwp_file->cus;
12742 bfd *dbfd = dwp_file->dbfd.get ();
12743 uint32_t mask = dwp_htab->nr_slots - 1;
12744 uint32_t hash = signature & mask;
12745 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12746 unsigned int i;
12747 void **slot;
12748 struct dwo_unit find_dwo_cu;
12749
12750 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12751 find_dwo_cu.signature = signature;
12752 slot = htab_find_slot (is_debug_types
12753 ? dwp_file->loaded_tus
12754 : dwp_file->loaded_cus,
12755 &find_dwo_cu, INSERT);
12756
12757 if (*slot != NULL)
12758 return (struct dwo_unit *) *slot;
12759
12760 /* Use a for loop so that we don't loop forever on bad debug info. */
12761 for (i = 0; i < dwp_htab->nr_slots; ++i)
12762 {
12763 ULONGEST signature_in_table;
12764
12765 signature_in_table =
12766 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
12767 if (signature_in_table == signature)
12768 {
12769 uint32_t unit_index =
12770 read_4_bytes (dbfd,
12771 dwp_htab->unit_table + hash * sizeof (uint32_t));
12772
12773 if (dwp_file->version == 1)
12774 {
12775 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12776 dwp_file, unit_index,
12777 comp_dir, signature,
12778 is_debug_types);
12779 }
12780 else
12781 {
12782 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12783 dwp_file, unit_index,
12784 comp_dir, signature,
12785 is_debug_types);
12786 }
12787 return (struct dwo_unit *) *slot;
12788 }
12789 if (signature_in_table == 0)
12790 return NULL;
12791 hash = (hash + hash2) & mask;
12792 }
12793
12794 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12795 " [in module %s]"),
12796 dwp_file->name);
12797 }
12798
12799 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12800 Open the file specified by FILE_NAME and hand it off to BFD for
12801 preliminary analysis. Return a newly initialized bfd *, which
12802 includes a canonicalized copy of FILE_NAME.
12803 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12804 SEARCH_CWD is true if the current directory is to be searched.
12805 It will be searched before debug-file-directory.
12806 If successful, the file is added to the bfd include table of the
12807 objfile's bfd (see gdb_bfd_record_inclusion).
12808 If unable to find/open the file, return NULL.
12809 NOTE: This function is derived from symfile_bfd_open. */
12810
12811 static gdb_bfd_ref_ptr
12812 try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12813 const char *file_name, int is_dwp, int search_cwd)
12814 {
12815 int desc;
12816 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12817 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12818 to debug_file_directory. */
12819 const char *search_path;
12820 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12821
12822 gdb::unique_xmalloc_ptr<char> search_path_holder;
12823 if (search_cwd)
12824 {
12825 if (*debug_file_directory != '\0')
12826 {
12827 search_path_holder.reset (concat (".", dirname_separator_string,
12828 debug_file_directory,
12829 (char *) NULL));
12830 search_path = search_path_holder.get ();
12831 }
12832 else
12833 search_path = ".";
12834 }
12835 else
12836 search_path = debug_file_directory;
12837
12838 openp_flags flags = OPF_RETURN_REALPATH;
12839 if (is_dwp)
12840 flags |= OPF_SEARCH_IN_PATH;
12841
12842 gdb::unique_xmalloc_ptr<char> absolute_name;
12843 desc = openp (search_path, flags, file_name,
12844 O_RDONLY | O_BINARY, &absolute_name);
12845 if (desc < 0)
12846 return NULL;
12847
12848 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12849 gnutarget, desc));
12850 if (sym_bfd == NULL)
12851 return NULL;
12852 bfd_set_cacheable (sym_bfd.get (), 1);
12853
12854 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12855 return NULL;
12856
12857 /* Success. Record the bfd as having been included by the objfile's bfd.
12858 This is important because things like demangled_names_hash lives in the
12859 objfile's per_bfd space and may have references to things like symbol
12860 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12861 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
12862
12863 return sym_bfd;
12864 }
12865
12866 /* Try to open DWO file FILE_NAME.
12867 COMP_DIR is the DW_AT_comp_dir attribute.
12868 The result is the bfd handle of the file.
12869 If there is a problem finding or opening the file, return NULL.
12870 Upon success, the canonicalized path of the file is stored in the bfd,
12871 same as symfile_bfd_open. */
12872
12873 static gdb_bfd_ref_ptr
12874 open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12875 const char *file_name, const char *comp_dir)
12876 {
12877 if (IS_ABSOLUTE_PATH (file_name))
12878 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12879 0 /*is_dwp*/, 0 /*search_cwd*/);
12880
12881 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12882
12883 if (comp_dir != NULL)
12884 {
12885 gdb::unique_xmalloc_ptr<char> path_to_try
12886 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
12887
12888 /* NOTE: If comp_dir is a relative path, this will also try the
12889 search path, which seems useful. */
12890 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
12891 path_to_try.get (),
12892 0 /*is_dwp*/,
12893 1 /*search_cwd*/));
12894 if (abfd != NULL)
12895 return abfd;
12896 }
12897
12898 /* That didn't work, try debug-file-directory, which, despite its name,
12899 is a list of paths. */
12900
12901 if (*debug_file_directory == '\0')
12902 return NULL;
12903
12904 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12905 0 /*is_dwp*/, 1 /*search_cwd*/);
12906 }
12907
12908 /* This function is mapped across the sections and remembers the offset and
12909 size of each of the DWO debugging sections we are interested in. */
12910
12911 static void
12912 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12913 {
12914 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
12915 const struct dwop_section_names *names = &dwop_section_names;
12916
12917 if (section_is_p (sectp->name, &names->abbrev_dwo))
12918 {
12919 dwo_sections->abbrev.s.section = sectp;
12920 dwo_sections->abbrev.size = bfd_section_size (sectp);
12921 }
12922 else if (section_is_p (sectp->name, &names->info_dwo))
12923 {
12924 dwo_sections->info.s.section = sectp;
12925 dwo_sections->info.size = bfd_section_size (sectp);
12926 }
12927 else if (section_is_p (sectp->name, &names->line_dwo))
12928 {
12929 dwo_sections->line.s.section = sectp;
12930 dwo_sections->line.size = bfd_section_size (sectp);
12931 }
12932 else if (section_is_p (sectp->name, &names->loc_dwo))
12933 {
12934 dwo_sections->loc.s.section = sectp;
12935 dwo_sections->loc.size = bfd_section_size (sectp);
12936 }
12937 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12938 {
12939 dwo_sections->macinfo.s.section = sectp;
12940 dwo_sections->macinfo.size = bfd_section_size (sectp);
12941 }
12942 else if (section_is_p (sectp->name, &names->macro_dwo))
12943 {
12944 dwo_sections->macro.s.section = sectp;
12945 dwo_sections->macro.size = bfd_section_size (sectp);
12946 }
12947 else if (section_is_p (sectp->name, &names->str_dwo))
12948 {
12949 dwo_sections->str.s.section = sectp;
12950 dwo_sections->str.size = bfd_section_size (sectp);
12951 }
12952 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12953 {
12954 dwo_sections->str_offsets.s.section = sectp;
12955 dwo_sections->str_offsets.size = bfd_section_size (sectp);
12956 }
12957 else if (section_is_p (sectp->name, &names->types_dwo))
12958 {
12959 struct dwarf2_section_info type_section;
12960
12961 memset (&type_section, 0, sizeof (type_section));
12962 type_section.s.section = sectp;
12963 type_section.size = bfd_section_size (sectp);
12964 dwo_sections->types.push_back (type_section);
12965 }
12966 }
12967
12968 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12969 by PER_CU. This is for the non-DWP case.
12970 The result is NULL if DWO_NAME can't be found. */
12971
12972 static struct dwo_file *
12973 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
12974 const char *dwo_name, const char *comp_dir)
12975 {
12976 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
12977
12978 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
12979 if (dbfd == NULL)
12980 {
12981 if (dwarf_read_debug)
12982 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12983 return NULL;
12984 }
12985
12986 dwo_file_up dwo_file (new struct dwo_file);
12987 dwo_file->dwo_name = dwo_name;
12988 dwo_file->comp_dir = comp_dir;
12989 dwo_file->dbfd = std::move (dbfd);
12990
12991 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
12992 &dwo_file->sections);
12993
12994 create_cus_hash_table (dwarf2_per_objfile, per_cu->cu, *dwo_file,
12995 dwo_file->sections.info, dwo_file->cus);
12996
12997 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
12998 dwo_file->sections.types, dwo_file->tus);
12999
13000 if (dwarf_read_debug)
13001 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
13002
13003 return dwo_file.release ();
13004 }
13005
13006 /* This function is mapped across the sections and remembers the offset and
13007 size of each of the DWP debugging sections common to version 1 and 2 that
13008 we are interested in. */
13009
13010 static void
13011 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
13012 void *dwp_file_ptr)
13013 {
13014 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
13015 const struct dwop_section_names *names = &dwop_section_names;
13016 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13017
13018 /* Record the ELF section number for later lookup: this is what the
13019 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13020 gdb_assert (elf_section_nr < dwp_file->num_sections);
13021 dwp_file->elf_sections[elf_section_nr] = sectp;
13022
13023 /* Look for specific sections that we need. */
13024 if (section_is_p (sectp->name, &names->str_dwo))
13025 {
13026 dwp_file->sections.str.s.section = sectp;
13027 dwp_file->sections.str.size = bfd_section_size (sectp);
13028 }
13029 else if (section_is_p (sectp->name, &names->cu_index))
13030 {
13031 dwp_file->sections.cu_index.s.section = sectp;
13032 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
13033 }
13034 else if (section_is_p (sectp->name, &names->tu_index))
13035 {
13036 dwp_file->sections.tu_index.s.section = sectp;
13037 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
13038 }
13039 }
13040
13041 /* This function is mapped across the sections and remembers the offset and
13042 size of each of the DWP version 2 debugging sections that we are interested
13043 in. This is split into a separate function because we don't know if we
13044 have version 1 or 2 until we parse the cu_index/tu_index sections. */
13045
13046 static void
13047 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
13048 {
13049 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
13050 const struct dwop_section_names *names = &dwop_section_names;
13051 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13052
13053 /* Record the ELF section number for later lookup: this is what the
13054 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13055 gdb_assert (elf_section_nr < dwp_file->num_sections);
13056 dwp_file->elf_sections[elf_section_nr] = sectp;
13057
13058 /* Look for specific sections that we need. */
13059 if (section_is_p (sectp->name, &names->abbrev_dwo))
13060 {
13061 dwp_file->sections.abbrev.s.section = sectp;
13062 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
13063 }
13064 else if (section_is_p (sectp->name, &names->info_dwo))
13065 {
13066 dwp_file->sections.info.s.section = sectp;
13067 dwp_file->sections.info.size = bfd_section_size (sectp);
13068 }
13069 else if (section_is_p (sectp->name, &names->line_dwo))
13070 {
13071 dwp_file->sections.line.s.section = sectp;
13072 dwp_file->sections.line.size = bfd_section_size (sectp);
13073 }
13074 else if (section_is_p (sectp->name, &names->loc_dwo))
13075 {
13076 dwp_file->sections.loc.s.section = sectp;
13077 dwp_file->sections.loc.size = bfd_section_size (sectp);
13078 }
13079 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13080 {
13081 dwp_file->sections.macinfo.s.section = sectp;
13082 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
13083 }
13084 else if (section_is_p (sectp->name, &names->macro_dwo))
13085 {
13086 dwp_file->sections.macro.s.section = sectp;
13087 dwp_file->sections.macro.size = bfd_section_size (sectp);
13088 }
13089 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13090 {
13091 dwp_file->sections.str_offsets.s.section = sectp;
13092 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
13093 }
13094 else if (section_is_p (sectp->name, &names->types_dwo))
13095 {
13096 dwp_file->sections.types.s.section = sectp;
13097 dwp_file->sections.types.size = bfd_section_size (sectp);
13098 }
13099 }
13100
13101 /* Hash function for dwp_file loaded CUs/TUs. */
13102
13103 static hashval_t
13104 hash_dwp_loaded_cutus (const void *item)
13105 {
13106 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
13107
13108 /* This drops the top 32 bits of the signature, but is ok for a hash. */
13109 return dwo_unit->signature;
13110 }
13111
13112 /* Equality function for dwp_file loaded CUs/TUs. */
13113
13114 static int
13115 eq_dwp_loaded_cutus (const void *a, const void *b)
13116 {
13117 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13118 const struct dwo_unit *dub = (const struct dwo_unit *) b;
13119
13120 return dua->signature == dub->signature;
13121 }
13122
13123 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
13124
13125 static htab_t
13126 allocate_dwp_loaded_cutus_table (struct objfile *objfile)
13127 {
13128 return htab_create_alloc_ex (3,
13129 hash_dwp_loaded_cutus,
13130 eq_dwp_loaded_cutus,
13131 NULL,
13132 &objfile->objfile_obstack,
13133 hashtab_obstack_allocate,
13134 dummy_obstack_deallocate);
13135 }
13136
13137 /* Try to open DWP file FILE_NAME.
13138 The result is the bfd handle of the file.
13139 If there is a problem finding or opening the file, return NULL.
13140 Upon success, the canonicalized path of the file is stored in the bfd,
13141 same as symfile_bfd_open. */
13142
13143 static gdb_bfd_ref_ptr
13144 open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
13145 const char *file_name)
13146 {
13147 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
13148 1 /*is_dwp*/,
13149 1 /*search_cwd*/));
13150 if (abfd != NULL)
13151 return abfd;
13152
13153 /* Work around upstream bug 15652.
13154 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13155 [Whether that's a "bug" is debatable, but it is getting in our way.]
13156 We have no real idea where the dwp file is, because gdb's realpath-ing
13157 of the executable's path may have discarded the needed info.
13158 [IWBN if the dwp file name was recorded in the executable, akin to
13159 .gnu_debuglink, but that doesn't exist yet.]
13160 Strip the directory from FILE_NAME and search again. */
13161 if (*debug_file_directory != '\0')
13162 {
13163 /* Don't implicitly search the current directory here.
13164 If the user wants to search "." to handle this case,
13165 it must be added to debug-file-directory. */
13166 return try_open_dwop_file (dwarf2_per_objfile,
13167 lbasename (file_name), 1 /*is_dwp*/,
13168 0 /*search_cwd*/);
13169 }
13170
13171 return NULL;
13172 }
13173
13174 /* Initialize the use of the DWP file for the current objfile.
13175 By convention the name of the DWP file is ${objfile}.dwp.
13176 The result is NULL if it can't be found. */
13177
13178 static std::unique_ptr<struct dwp_file>
13179 open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
13180 {
13181 struct objfile *objfile = dwarf2_per_objfile->objfile;
13182
13183 /* Try to find first .dwp for the binary file before any symbolic links
13184 resolving. */
13185
13186 /* If the objfile is a debug file, find the name of the real binary
13187 file and get the name of dwp file from there. */
13188 std::string dwp_name;
13189 if (objfile->separate_debug_objfile_backlink != NULL)
13190 {
13191 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13192 const char *backlink_basename = lbasename (backlink->original_name);
13193
13194 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
13195 }
13196 else
13197 dwp_name = objfile->original_name;
13198
13199 dwp_name += ".dwp";
13200
13201 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
13202 if (dbfd == NULL
13203 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13204 {
13205 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
13206 dwp_name = objfile_name (objfile);
13207 dwp_name += ".dwp";
13208 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
13209 }
13210
13211 if (dbfd == NULL)
13212 {
13213 if (dwarf_read_debug)
13214 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
13215 return std::unique_ptr<dwp_file> ();
13216 }
13217
13218 const char *name = bfd_get_filename (dbfd.get ());
13219 std::unique_ptr<struct dwp_file> dwp_file
13220 (new struct dwp_file (name, std::move (dbfd)));
13221
13222 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
13223 dwp_file->elf_sections =
13224 OBSTACK_CALLOC (&objfile->objfile_obstack,
13225 dwp_file->num_sections, asection *);
13226
13227 bfd_map_over_sections (dwp_file->dbfd.get (),
13228 dwarf2_locate_common_dwp_sections,
13229 dwp_file.get ());
13230
13231 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
13232 0);
13233
13234 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
13235 1);
13236
13237 /* The DWP file version is stored in the hash table. Oh well. */
13238 if (dwp_file->cus && dwp_file->tus
13239 && dwp_file->cus->version != dwp_file->tus->version)
13240 {
13241 /* Technically speaking, we should try to limp along, but this is
13242 pretty bizarre. We use pulongest here because that's the established
13243 portability solution (e.g, we cannot use %u for uint32_t). */
13244 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13245 " TU version %s [in DWP file %s]"),
13246 pulongest (dwp_file->cus->version),
13247 pulongest (dwp_file->tus->version), dwp_name.c_str ());
13248 }
13249
13250 if (dwp_file->cus)
13251 dwp_file->version = dwp_file->cus->version;
13252 else if (dwp_file->tus)
13253 dwp_file->version = dwp_file->tus->version;
13254 else
13255 dwp_file->version = 2;
13256
13257 if (dwp_file->version == 2)
13258 bfd_map_over_sections (dwp_file->dbfd.get (),
13259 dwarf2_locate_v2_dwp_sections,
13260 dwp_file.get ());
13261
13262 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
13263 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
13264
13265 if (dwarf_read_debug)
13266 {
13267 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
13268 fprintf_unfiltered (gdb_stdlog,
13269 " %s CUs, %s TUs\n",
13270 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13271 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
13272 }
13273
13274 return dwp_file;
13275 }
13276
13277 /* Wrapper around open_and_init_dwp_file, only open it once. */
13278
13279 static struct dwp_file *
13280 get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
13281 {
13282 if (! dwarf2_per_objfile->dwp_checked)
13283 {
13284 dwarf2_per_objfile->dwp_file
13285 = open_and_init_dwp_file (dwarf2_per_objfile);
13286 dwarf2_per_objfile->dwp_checked = 1;
13287 }
13288 return dwarf2_per_objfile->dwp_file.get ();
13289 }
13290
13291 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13292 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13293 or in the DWP file for the objfile, referenced by THIS_UNIT.
13294 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
13295 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13296
13297 This is called, for example, when wanting to read a variable with a
13298 complex location. Therefore we don't want to do file i/o for every call.
13299 Therefore we don't want to look for a DWO file on every call.
13300 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13301 then we check if we've already seen DWO_NAME, and only THEN do we check
13302 for a DWO file.
13303
13304 The result is a pointer to the dwo_unit object or NULL if we didn't find it
13305 (dwo_id mismatch or couldn't find the DWO/DWP file). */
13306
13307 static struct dwo_unit *
13308 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
13309 const char *dwo_name, const char *comp_dir,
13310 ULONGEST signature, int is_debug_types)
13311 {
13312 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
13313 struct objfile *objfile = dwarf2_per_objfile->objfile;
13314 const char *kind = is_debug_types ? "TU" : "CU";
13315 void **dwo_file_slot;
13316 struct dwo_file *dwo_file;
13317 struct dwp_file *dwp_file;
13318
13319 /* First see if there's a DWP file.
13320 If we have a DWP file but didn't find the DWO inside it, don't
13321 look for the original DWO file. It makes gdb behave differently
13322 depending on whether one is debugging in the build tree. */
13323
13324 dwp_file = get_dwp_file (dwarf2_per_objfile);
13325 if (dwp_file != NULL)
13326 {
13327 const struct dwp_hash_table *dwp_htab =
13328 is_debug_types ? dwp_file->tus : dwp_file->cus;
13329
13330 if (dwp_htab != NULL)
13331 {
13332 struct dwo_unit *dwo_cutu =
13333 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
13334 signature, is_debug_types);
13335
13336 if (dwo_cutu != NULL)
13337 {
13338 if (dwarf_read_debug)
13339 {
13340 fprintf_unfiltered (gdb_stdlog,
13341 "Virtual DWO %s %s found: @%s\n",
13342 kind, hex_string (signature),
13343 host_address_to_string (dwo_cutu));
13344 }
13345 return dwo_cutu;
13346 }
13347 }
13348 }
13349 else
13350 {
13351 /* No DWP file, look for the DWO file. */
13352
13353 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
13354 dwo_name, comp_dir);
13355 if (*dwo_file_slot == NULL)
13356 {
13357 /* Read in the file and build a table of the CUs/TUs it contains. */
13358 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
13359 }
13360 /* NOTE: This will be NULL if unable to open the file. */
13361 dwo_file = (struct dwo_file *) *dwo_file_slot;
13362
13363 if (dwo_file != NULL)
13364 {
13365 struct dwo_unit *dwo_cutu = NULL;
13366
13367 if (is_debug_types && dwo_file->tus)
13368 {
13369 struct dwo_unit find_dwo_cutu;
13370
13371 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13372 find_dwo_cutu.signature = signature;
13373 dwo_cutu
13374 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
13375 }
13376 else if (!is_debug_types && dwo_file->cus)
13377 {
13378 struct dwo_unit find_dwo_cutu;
13379
13380 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13381 find_dwo_cutu.signature = signature;
13382 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus,
13383 &find_dwo_cutu);
13384 }
13385
13386 if (dwo_cutu != NULL)
13387 {
13388 if (dwarf_read_debug)
13389 {
13390 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
13391 kind, dwo_name, hex_string (signature),
13392 host_address_to_string (dwo_cutu));
13393 }
13394 return dwo_cutu;
13395 }
13396 }
13397 }
13398
13399 /* We didn't find it. This could mean a dwo_id mismatch, or
13400 someone deleted the DWO/DWP file, or the search path isn't set up
13401 correctly to find the file. */
13402
13403 if (dwarf_read_debug)
13404 {
13405 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
13406 kind, dwo_name, hex_string (signature));
13407 }
13408
13409 /* This is a warning and not a complaint because it can be caused by
13410 pilot error (e.g., user accidentally deleting the DWO). */
13411 {
13412 /* Print the name of the DWP file if we looked there, helps the user
13413 better diagnose the problem. */
13414 std::string dwp_text;
13415
13416 if (dwp_file != NULL)
13417 dwp_text = string_printf (" [in DWP file %s]",
13418 lbasename (dwp_file->name));
13419
13420 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
13421 " [in module %s]"),
13422 kind, dwo_name, hex_string (signature),
13423 dwp_text.c_str (),
13424 this_unit->is_debug_types ? "TU" : "CU",
13425 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
13426 }
13427 return NULL;
13428 }
13429
13430 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13431 See lookup_dwo_cutu_unit for details. */
13432
13433 static struct dwo_unit *
13434 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
13435 const char *dwo_name, const char *comp_dir,
13436 ULONGEST signature)
13437 {
13438 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
13439 }
13440
13441 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13442 See lookup_dwo_cutu_unit for details. */
13443
13444 static struct dwo_unit *
13445 lookup_dwo_type_unit (struct signatured_type *this_tu,
13446 const char *dwo_name, const char *comp_dir)
13447 {
13448 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
13449 }
13450
13451 /* Traversal function for queue_and_load_all_dwo_tus. */
13452
13453 static int
13454 queue_and_load_dwo_tu (void **slot, void *info)
13455 {
13456 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13457 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
13458 ULONGEST signature = dwo_unit->signature;
13459 struct signatured_type *sig_type =
13460 lookup_dwo_signatured_type (per_cu->cu, signature);
13461
13462 if (sig_type != NULL)
13463 {
13464 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13465
13466 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13467 a real dependency of PER_CU on SIG_TYPE. That is detected later
13468 while processing PER_CU. */
13469 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
13470 load_full_type_unit (sig_cu);
13471 per_cu->imported_symtabs_push (sig_cu);
13472 }
13473
13474 return 1;
13475 }
13476
13477 /* Queue all TUs contained in the DWO of PER_CU to be read in.
13478 The DWO may have the only definition of the type, though it may not be
13479 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13480 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13481
13482 static void
13483 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
13484 {
13485 struct dwo_unit *dwo_unit;
13486 struct dwo_file *dwo_file;
13487
13488 gdb_assert (!per_cu->is_debug_types);
13489 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
13490 gdb_assert (per_cu->cu != NULL);
13491
13492 dwo_unit = per_cu->cu->dwo_unit;
13493 gdb_assert (dwo_unit != NULL);
13494
13495 dwo_file = dwo_unit->dwo_file;
13496 if (dwo_file->tus != NULL)
13497 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
13498 }
13499
13500 /* Read in various DIEs. */
13501
13502 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
13503 Inherit only the children of the DW_AT_abstract_origin DIE not being
13504 already referenced by DW_AT_abstract_origin from the children of the
13505 current DIE. */
13506
13507 static void
13508 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13509 {
13510 struct die_info *child_die;
13511 sect_offset *offsetp;
13512 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13513 struct die_info *origin_die;
13514 /* Iterator of the ORIGIN_DIE children. */
13515 struct die_info *origin_child_die;
13516 struct attribute *attr;
13517 struct dwarf2_cu *origin_cu;
13518 struct pending **origin_previous_list_in_scope;
13519
13520 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13521 if (!attr)
13522 return;
13523
13524 /* Note that following die references may follow to a die in a
13525 different cu. */
13526
13527 origin_cu = cu;
13528 origin_die = follow_die_ref (die, attr, &origin_cu);
13529
13530 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13531 symbols in. */
13532 origin_previous_list_in_scope = origin_cu->list_in_scope;
13533 origin_cu->list_in_scope = cu->list_in_scope;
13534
13535 if (die->tag != origin_die->tag
13536 && !(die->tag == DW_TAG_inlined_subroutine
13537 && origin_die->tag == DW_TAG_subprogram))
13538 complaint (_("DIE %s and its abstract origin %s have different tags"),
13539 sect_offset_str (die->sect_off),
13540 sect_offset_str (origin_die->sect_off));
13541
13542 std::vector<sect_offset> offsets;
13543
13544 for (child_die = die->child;
13545 child_die && child_die->tag;
13546 child_die = sibling_die (child_die))
13547 {
13548 struct die_info *child_origin_die;
13549 struct dwarf2_cu *child_origin_cu;
13550
13551 /* We are trying to process concrete instance entries:
13552 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
13553 it's not relevant to our analysis here. i.e. detecting DIEs that are
13554 present in the abstract instance but not referenced in the concrete
13555 one. */
13556 if (child_die->tag == DW_TAG_call_site
13557 || child_die->tag == DW_TAG_GNU_call_site)
13558 continue;
13559
13560 /* For each CHILD_DIE, find the corresponding child of
13561 ORIGIN_DIE. If there is more than one layer of
13562 DW_AT_abstract_origin, follow them all; there shouldn't be,
13563 but GCC versions at least through 4.4 generate this (GCC PR
13564 40573). */
13565 child_origin_die = child_die;
13566 child_origin_cu = cu;
13567 while (1)
13568 {
13569 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13570 child_origin_cu);
13571 if (attr == NULL)
13572 break;
13573 child_origin_die = follow_die_ref (child_origin_die, attr,
13574 &child_origin_cu);
13575 }
13576
13577 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13578 counterpart may exist. */
13579 if (child_origin_die != child_die)
13580 {
13581 if (child_die->tag != child_origin_die->tag
13582 && !(child_die->tag == DW_TAG_inlined_subroutine
13583 && child_origin_die->tag == DW_TAG_subprogram))
13584 complaint (_("Child DIE %s and its abstract origin %s have "
13585 "different tags"),
13586 sect_offset_str (child_die->sect_off),
13587 sect_offset_str (child_origin_die->sect_off));
13588 if (child_origin_die->parent != origin_die)
13589 complaint (_("Child DIE %s and its abstract origin %s have "
13590 "different parents"),
13591 sect_offset_str (child_die->sect_off),
13592 sect_offset_str (child_origin_die->sect_off));
13593 else
13594 offsets.push_back (child_origin_die->sect_off);
13595 }
13596 }
13597 std::sort (offsets.begin (), offsets.end ());
13598 sect_offset *offsets_end = offsets.data () + offsets.size ();
13599 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
13600 if (offsetp[-1] == *offsetp)
13601 complaint (_("Multiple children of DIE %s refer "
13602 "to DIE %s as their abstract origin"),
13603 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
13604
13605 offsetp = offsets.data ();
13606 origin_child_die = origin_die->child;
13607 while (origin_child_die && origin_child_die->tag)
13608 {
13609 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
13610 while (offsetp < offsets_end
13611 && *offsetp < origin_child_die->sect_off)
13612 offsetp++;
13613 if (offsetp >= offsets_end
13614 || *offsetp > origin_child_die->sect_off)
13615 {
13616 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13617 Check whether we're already processing ORIGIN_CHILD_DIE.
13618 This can happen with mutually referenced abstract_origins.
13619 PR 16581. */
13620 if (!origin_child_die->in_process)
13621 process_die (origin_child_die, origin_cu);
13622 }
13623 origin_child_die = sibling_die (origin_child_die);
13624 }
13625 origin_cu->list_in_scope = origin_previous_list_in_scope;
13626
13627 if (cu != origin_cu)
13628 compute_delayed_physnames (origin_cu);
13629 }
13630
13631 static void
13632 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
13633 {
13634 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13635 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13636 struct context_stack *newobj;
13637 CORE_ADDR lowpc;
13638 CORE_ADDR highpc;
13639 struct die_info *child_die;
13640 struct attribute *attr, *call_line, *call_file;
13641 const char *name;
13642 CORE_ADDR baseaddr;
13643 struct block *block;
13644 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13645 std::vector<struct symbol *> template_args;
13646 struct template_symbol *templ_func = NULL;
13647
13648 if (inlined_func)
13649 {
13650 /* If we do not have call site information, we can't show the
13651 caller of this inlined function. That's too confusing, so
13652 only use the scope for local variables. */
13653 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13654 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13655 if (call_line == NULL || call_file == NULL)
13656 {
13657 read_lexical_block_scope (die, cu);
13658 return;
13659 }
13660 }
13661
13662 baseaddr = objfile->text_section_offset ();
13663
13664 name = dwarf2_name (die, cu);
13665
13666 /* Ignore functions with missing or empty names. These are actually
13667 illegal according to the DWARF standard. */
13668 if (name == NULL)
13669 {
13670 complaint (_("missing name for subprogram DIE at %s"),
13671 sect_offset_str (die->sect_off));
13672 return;
13673 }
13674
13675 /* Ignore functions with missing or invalid low and high pc attributes. */
13676 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
13677 <= PC_BOUNDS_INVALID)
13678 {
13679 attr = dwarf2_attr (die, DW_AT_external, cu);
13680 if (!attr || !DW_UNSND (attr))
13681 complaint (_("cannot get low and high bounds "
13682 "for subprogram DIE at %s"),
13683 sect_offset_str (die->sect_off));
13684 return;
13685 }
13686
13687 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13688 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13689
13690 /* If we have any template arguments, then we must allocate a
13691 different sort of symbol. */
13692 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
13693 {
13694 if (child_die->tag == DW_TAG_template_type_param
13695 || child_die->tag == DW_TAG_template_value_param)
13696 {
13697 templ_func = allocate_template_symbol (objfile);
13698 templ_func->subclass = SYMBOL_TEMPLATE;
13699 break;
13700 }
13701 }
13702
13703 newobj = cu->get_builder ()->push_context (0, lowpc);
13704 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13705 (struct symbol *) templ_func);
13706
13707 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
13708 set_objfile_main_name (objfile, newobj->name->linkage_name (),
13709 cu->language);
13710
13711 /* If there is a location expression for DW_AT_frame_base, record
13712 it. */
13713 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
13714 if (attr != nullptr)
13715 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
13716
13717 /* If there is a location for the static link, record it. */
13718 newobj->static_link = NULL;
13719 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13720 if (attr != nullptr)
13721 {
13722 newobj->static_link
13723 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
13724 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
13725 dwarf2_per_cu_addr_type (cu->per_cu));
13726 }
13727
13728 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
13729
13730 if (die->child != NULL)
13731 {
13732 child_die = die->child;
13733 while (child_die && child_die->tag)
13734 {
13735 if (child_die->tag == DW_TAG_template_type_param
13736 || child_die->tag == DW_TAG_template_value_param)
13737 {
13738 struct symbol *arg = new_symbol (child_die, NULL, cu);
13739
13740 if (arg != NULL)
13741 template_args.push_back (arg);
13742 }
13743 else
13744 process_die (child_die, cu);
13745 child_die = sibling_die (child_die);
13746 }
13747 }
13748
13749 inherit_abstract_dies (die, cu);
13750
13751 /* If we have a DW_AT_specification, we might need to import using
13752 directives from the context of the specification DIE. See the
13753 comment in determine_prefix. */
13754 if (cu->language == language_cplus
13755 && dwarf2_attr (die, DW_AT_specification, cu))
13756 {
13757 struct dwarf2_cu *spec_cu = cu;
13758 struct die_info *spec_die = die_specification (die, &spec_cu);
13759
13760 while (spec_die)
13761 {
13762 child_die = spec_die->child;
13763 while (child_die && child_die->tag)
13764 {
13765 if (child_die->tag == DW_TAG_imported_module)
13766 process_die (child_die, spec_cu);
13767 child_die = sibling_die (child_die);
13768 }
13769
13770 /* In some cases, GCC generates specification DIEs that
13771 themselves contain DW_AT_specification attributes. */
13772 spec_die = die_specification (spec_die, &spec_cu);
13773 }
13774 }
13775
13776 struct context_stack cstk = cu->get_builder ()->pop_context ();
13777 /* Make a block for the local symbols within. */
13778 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
13779 cstk.static_link, lowpc, highpc);
13780
13781 /* For C++, set the block's scope. */
13782 if ((cu->language == language_cplus
13783 || cu->language == language_fortran
13784 || cu->language == language_d
13785 || cu->language == language_rust)
13786 && cu->processing_has_namespace_info)
13787 block_set_scope (block, determine_prefix (die, cu),
13788 &objfile->objfile_obstack);
13789
13790 /* If we have address ranges, record them. */
13791 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13792
13793 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
13794
13795 /* Attach template arguments to function. */
13796 if (!template_args.empty ())
13797 {
13798 gdb_assert (templ_func != NULL);
13799
13800 templ_func->n_template_arguments = template_args.size ();
13801 templ_func->template_arguments
13802 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13803 templ_func->n_template_arguments);
13804 memcpy (templ_func->template_arguments,
13805 template_args.data (),
13806 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13807
13808 /* Make sure that the symtab is set on the new symbols. Even
13809 though they don't appear in this symtab directly, other parts
13810 of gdb assume that symbols do, and this is reasonably
13811 true. */
13812 for (symbol *sym : template_args)
13813 symbol_set_symtab (sym, symbol_symtab (templ_func));
13814 }
13815
13816 /* In C++, we can have functions nested inside functions (e.g., when
13817 a function declares a class that has methods). This means that
13818 when we finish processing a function scope, we may need to go
13819 back to building a containing block's symbol lists. */
13820 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13821 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13822
13823 /* If we've finished processing a top-level function, subsequent
13824 symbols go in the file symbol list. */
13825 if (cu->get_builder ()->outermost_context_p ())
13826 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
13827 }
13828
13829 /* Process all the DIES contained within a lexical block scope. Start
13830 a new scope, process the dies, and then close the scope. */
13831
13832 static void
13833 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13834 {
13835 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13836 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13837 CORE_ADDR lowpc, highpc;
13838 struct die_info *child_die;
13839 CORE_ADDR baseaddr;
13840
13841 baseaddr = objfile->text_section_offset ();
13842
13843 /* Ignore blocks with missing or invalid low and high pc attributes. */
13844 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13845 as multiple lexical blocks? Handling children in a sane way would
13846 be nasty. Might be easier to properly extend generic blocks to
13847 describe ranges. */
13848 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13849 {
13850 case PC_BOUNDS_NOT_PRESENT:
13851 /* DW_TAG_lexical_block has no attributes, process its children as if
13852 there was no wrapping by that DW_TAG_lexical_block.
13853 GCC does no longer produces such DWARF since GCC r224161. */
13854 for (child_die = die->child;
13855 child_die != NULL && child_die->tag;
13856 child_die = sibling_die (child_die))
13857 process_die (child_die, cu);
13858 return;
13859 case PC_BOUNDS_INVALID:
13860 return;
13861 }
13862 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13863 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13864
13865 cu->get_builder ()->push_context (0, lowpc);
13866 if (die->child != NULL)
13867 {
13868 child_die = die->child;
13869 while (child_die && child_die->tag)
13870 {
13871 process_die (child_die, cu);
13872 child_die = sibling_die (child_die);
13873 }
13874 }
13875 inherit_abstract_dies (die, cu);
13876 struct context_stack cstk = cu->get_builder ()->pop_context ();
13877
13878 if (*cu->get_builder ()->get_local_symbols () != NULL
13879 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13880 {
13881 struct block *block
13882 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13883 cstk.start_addr, highpc);
13884
13885 /* Note that recording ranges after traversing children, as we
13886 do here, means that recording a parent's ranges entails
13887 walking across all its children's ranges as they appear in
13888 the address map, which is quadratic behavior.
13889
13890 It would be nicer to record the parent's ranges before
13891 traversing its children, simply overriding whatever you find
13892 there. But since we don't even decide whether to create a
13893 block until after we've traversed its children, that's hard
13894 to do. */
13895 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13896 }
13897 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13898 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13899 }
13900
13901 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13902
13903 static void
13904 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13905 {
13906 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13907 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13908 CORE_ADDR pc, baseaddr;
13909 struct attribute *attr;
13910 struct call_site *call_site, call_site_local;
13911 void **slot;
13912 int nparams;
13913 struct die_info *child_die;
13914
13915 baseaddr = objfile->text_section_offset ();
13916
13917 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13918 if (attr == NULL)
13919 {
13920 /* This was a pre-DWARF-5 GNU extension alias
13921 for DW_AT_call_return_pc. */
13922 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13923 }
13924 if (!attr)
13925 {
13926 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13927 "DIE %s [in module %s]"),
13928 sect_offset_str (die->sect_off), objfile_name (objfile));
13929 return;
13930 }
13931 pc = attr_value_as_address (attr) + baseaddr;
13932 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13933
13934 if (cu->call_site_htab == NULL)
13935 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13936 NULL, &objfile->objfile_obstack,
13937 hashtab_obstack_allocate, NULL);
13938 call_site_local.pc = pc;
13939 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13940 if (*slot != NULL)
13941 {
13942 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13943 "DIE %s [in module %s]"),
13944 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13945 objfile_name (objfile));
13946 return;
13947 }
13948
13949 /* Count parameters at the caller. */
13950
13951 nparams = 0;
13952 for (child_die = die->child; child_die && child_die->tag;
13953 child_die = sibling_die (child_die))
13954 {
13955 if (child_die->tag != DW_TAG_call_site_parameter
13956 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13957 {
13958 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
13959 "DW_TAG_call_site child DIE %s [in module %s]"),
13960 child_die->tag, sect_offset_str (child_die->sect_off),
13961 objfile_name (objfile));
13962 continue;
13963 }
13964
13965 nparams++;
13966 }
13967
13968 call_site
13969 = ((struct call_site *)
13970 obstack_alloc (&objfile->objfile_obstack,
13971 sizeof (*call_site)
13972 + (sizeof (*call_site->parameter) * (nparams - 1))));
13973 *slot = call_site;
13974 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13975 call_site->pc = pc;
13976
13977 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13978 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
13979 {
13980 struct die_info *func_die;
13981
13982 /* Skip also over DW_TAG_inlined_subroutine. */
13983 for (func_die = die->parent;
13984 func_die && func_die->tag != DW_TAG_subprogram
13985 && func_die->tag != DW_TAG_subroutine_type;
13986 func_die = func_die->parent);
13987
13988 /* DW_AT_call_all_calls is a superset
13989 of DW_AT_call_all_tail_calls. */
13990 if (func_die
13991 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13992 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
13993 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
13994 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13995 {
13996 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13997 not complete. But keep CALL_SITE for look ups via call_site_htab,
13998 both the initial caller containing the real return address PC and
13999 the final callee containing the current PC of a chain of tail
14000 calls do not need to have the tail call list complete. But any
14001 function candidate for a virtual tail call frame searched via
14002 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
14003 determined unambiguously. */
14004 }
14005 else
14006 {
14007 struct type *func_type = NULL;
14008
14009 if (func_die)
14010 func_type = get_die_type (func_die, cu);
14011 if (func_type != NULL)
14012 {
14013 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
14014
14015 /* Enlist this call site to the function. */
14016 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
14017 TYPE_TAIL_CALL_LIST (func_type) = call_site;
14018 }
14019 else
14020 complaint (_("Cannot find function owning DW_TAG_call_site "
14021 "DIE %s [in module %s]"),
14022 sect_offset_str (die->sect_off), objfile_name (objfile));
14023 }
14024 }
14025
14026 attr = dwarf2_attr (die, DW_AT_call_target, cu);
14027 if (attr == NULL)
14028 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
14029 if (attr == NULL)
14030 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
14031 if (attr == NULL)
14032 {
14033 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
14034 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14035 }
14036 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
14037 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
14038 /* Keep NULL DWARF_BLOCK. */;
14039 else if (attr_form_is_block (attr))
14040 {
14041 struct dwarf2_locexpr_baton *dlbaton;
14042
14043 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
14044 dlbaton->data = DW_BLOCK (attr)->data;
14045 dlbaton->size = DW_BLOCK (attr)->size;
14046 dlbaton->per_cu = cu->per_cu;
14047
14048 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
14049 }
14050 else if (attr_form_is_ref (attr))
14051 {
14052 struct dwarf2_cu *target_cu = cu;
14053 struct die_info *target_die;
14054
14055 target_die = follow_die_ref (die, attr, &target_cu);
14056 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
14057 if (die_is_declaration (target_die, target_cu))
14058 {
14059 const char *target_physname;
14060
14061 /* Prefer the mangled name; otherwise compute the demangled one. */
14062 target_physname = dw2_linkage_name (target_die, target_cu);
14063 if (target_physname == NULL)
14064 target_physname = dwarf2_physname (NULL, target_die, target_cu);
14065 if (target_physname == NULL)
14066 complaint (_("DW_AT_call_target target DIE has invalid "
14067 "physname, for referencing DIE %s [in module %s]"),
14068 sect_offset_str (die->sect_off), objfile_name (objfile));
14069 else
14070 SET_FIELD_PHYSNAME (call_site->target, target_physname);
14071 }
14072 else
14073 {
14074 CORE_ADDR lowpc;
14075
14076 /* DW_AT_entry_pc should be preferred. */
14077 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
14078 <= PC_BOUNDS_INVALID)
14079 complaint (_("DW_AT_call_target target DIE has invalid "
14080 "low pc, for referencing DIE %s [in module %s]"),
14081 sect_offset_str (die->sect_off), objfile_name (objfile));
14082 else
14083 {
14084 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14085 SET_FIELD_PHYSADDR (call_site->target, lowpc);
14086 }
14087 }
14088 }
14089 else
14090 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
14091 "block nor reference, for DIE %s [in module %s]"),
14092 sect_offset_str (die->sect_off), objfile_name (objfile));
14093
14094 call_site->per_cu = cu->per_cu;
14095
14096 for (child_die = die->child;
14097 child_die && child_die->tag;
14098 child_die = sibling_die (child_die))
14099 {
14100 struct call_site_parameter *parameter;
14101 struct attribute *loc, *origin;
14102
14103 if (child_die->tag != DW_TAG_call_site_parameter
14104 && child_die->tag != DW_TAG_GNU_call_site_parameter)
14105 {
14106 /* Already printed the complaint above. */
14107 continue;
14108 }
14109
14110 gdb_assert (call_site->parameter_count < nparams);
14111 parameter = &call_site->parameter[call_site->parameter_count];
14112
14113 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14114 specifies DW_TAG_formal_parameter. Value of the data assumed for the
14115 register is contained in DW_AT_call_value. */
14116
14117 loc = dwarf2_attr (child_die, DW_AT_location, cu);
14118 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14119 if (origin == NULL)
14120 {
14121 /* This was a pre-DWARF-5 GNU extension alias
14122 for DW_AT_call_parameter. */
14123 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14124 }
14125 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
14126 {
14127 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
14128
14129 sect_offset sect_off
14130 = (sect_offset) dwarf2_get_ref_die_offset (origin);
14131 if (!offset_in_cu_p (&cu->header, sect_off))
14132 {
14133 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14134 binding can be done only inside one CU. Such referenced DIE
14135 therefore cannot be even moved to DW_TAG_partial_unit. */
14136 complaint (_("DW_AT_call_parameter offset is not in CU for "
14137 "DW_TAG_call_site child DIE %s [in module %s]"),
14138 sect_offset_str (child_die->sect_off),
14139 objfile_name (objfile));
14140 continue;
14141 }
14142 parameter->u.param_cu_off
14143 = (cu_offset) (sect_off - cu->header.sect_off);
14144 }
14145 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
14146 {
14147 complaint (_("No DW_FORM_block* DW_AT_location for "
14148 "DW_TAG_call_site child DIE %s [in module %s]"),
14149 sect_offset_str (child_die->sect_off), objfile_name (objfile));
14150 continue;
14151 }
14152 else
14153 {
14154 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
14155 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
14156 if (parameter->u.dwarf_reg != -1)
14157 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
14158 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
14159 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
14160 &parameter->u.fb_offset))
14161 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14162 else
14163 {
14164 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
14165 "for DW_FORM_block* DW_AT_location is supported for "
14166 "DW_TAG_call_site child DIE %s "
14167 "[in module %s]"),
14168 sect_offset_str (child_die->sect_off),
14169 objfile_name (objfile));
14170 continue;
14171 }
14172 }
14173
14174 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14175 if (attr == NULL)
14176 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
14177 if (!attr_form_is_block (attr))
14178 {
14179 complaint (_("No DW_FORM_block* DW_AT_call_value for "
14180 "DW_TAG_call_site child DIE %s [in module %s]"),
14181 sect_offset_str (child_die->sect_off),
14182 objfile_name (objfile));
14183 continue;
14184 }
14185 parameter->value = DW_BLOCK (attr)->data;
14186 parameter->value_size = DW_BLOCK (attr)->size;
14187
14188 /* Parameters are not pre-cleared by memset above. */
14189 parameter->data_value = NULL;
14190 parameter->data_value_size = 0;
14191 call_site->parameter_count++;
14192
14193 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14194 if (attr == NULL)
14195 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
14196 if (attr != nullptr)
14197 {
14198 if (!attr_form_is_block (attr))
14199 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
14200 "DW_TAG_call_site child DIE %s [in module %s]"),
14201 sect_offset_str (child_die->sect_off),
14202 objfile_name (objfile));
14203 else
14204 {
14205 parameter->data_value = DW_BLOCK (attr)->data;
14206 parameter->data_value_size = DW_BLOCK (attr)->size;
14207 }
14208 }
14209 }
14210 }
14211
14212 /* Helper function for read_variable. If DIE represents a virtual
14213 table, then return the type of the concrete object that is
14214 associated with the virtual table. Otherwise, return NULL. */
14215
14216 static struct type *
14217 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14218 {
14219 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14220 if (attr == NULL)
14221 return NULL;
14222
14223 /* Find the type DIE. */
14224 struct die_info *type_die = NULL;
14225 struct dwarf2_cu *type_cu = cu;
14226
14227 if (attr_form_is_ref (attr))
14228 type_die = follow_die_ref (die, attr, &type_cu);
14229 if (type_die == NULL)
14230 return NULL;
14231
14232 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14233 return NULL;
14234 return die_containing_type (type_die, type_cu);
14235 }
14236
14237 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14238
14239 static void
14240 read_variable (struct die_info *die, struct dwarf2_cu *cu)
14241 {
14242 struct rust_vtable_symbol *storage = NULL;
14243
14244 if (cu->language == language_rust)
14245 {
14246 struct type *containing_type = rust_containing_type (die, cu);
14247
14248 if (containing_type != NULL)
14249 {
14250 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14251
14252 storage = new (&objfile->objfile_obstack) rust_vtable_symbol ();
14253 initialize_objfile_symbol (storage);
14254 storage->concrete_type = containing_type;
14255 storage->subclass = SYMBOL_RUST_VTABLE;
14256 }
14257 }
14258
14259 struct symbol *res = new_symbol (die, NULL, cu, storage);
14260 struct attribute *abstract_origin
14261 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14262 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
14263 if (res == NULL && loc && abstract_origin)
14264 {
14265 /* We have a variable without a name, but with a location and an abstract
14266 origin. This may be a concrete instance of an abstract variable
14267 referenced from an DW_OP_GNU_variable_value, so save it to find it back
14268 later. */
14269 struct dwarf2_cu *origin_cu = cu;
14270 struct die_info *origin_die
14271 = follow_die_ref (die, abstract_origin, &origin_cu);
14272 dwarf2_per_objfile *dpo = cu->per_cu->dwarf2_per_objfile;
14273 dpo->abstract_to_concrete[origin_die->sect_off].push_back (die->sect_off);
14274 }
14275 }
14276
14277 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14278 reading .debug_rnglists.
14279 Callback's type should be:
14280 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14281 Return true if the attributes are present and valid, otherwise,
14282 return false. */
14283
14284 template <typename Callback>
14285 static bool
14286 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
14287 Callback &&callback)
14288 {
14289 struct dwarf2_per_objfile *dwarf2_per_objfile
14290 = cu->per_cu->dwarf2_per_objfile;
14291 struct objfile *objfile = dwarf2_per_objfile->objfile;
14292 bfd *obfd = objfile->obfd;
14293 /* Base address selection entry. */
14294 CORE_ADDR base;
14295 int found_base;
14296 const gdb_byte *buffer;
14297 CORE_ADDR baseaddr;
14298 bool overflow = false;
14299
14300 found_base = cu->base_known;
14301 base = cu->base_address;
14302
14303 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
14304 if (offset >= dwarf2_per_objfile->rnglists.size)
14305 {
14306 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
14307 offset);
14308 return false;
14309 }
14310 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
14311
14312 baseaddr = objfile->text_section_offset ();
14313
14314 while (1)
14315 {
14316 /* Initialize it due to a false compiler warning. */
14317 CORE_ADDR range_beginning = 0, range_end = 0;
14318 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
14319 + dwarf2_per_objfile->rnglists.size);
14320 unsigned int bytes_read;
14321
14322 if (buffer == buf_end)
14323 {
14324 overflow = true;
14325 break;
14326 }
14327 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14328 switch (rlet)
14329 {
14330 case DW_RLE_end_of_list:
14331 break;
14332 case DW_RLE_base_address:
14333 if (buffer + cu->header.addr_size > buf_end)
14334 {
14335 overflow = true;
14336 break;
14337 }
14338 base = read_address (obfd, buffer, cu, &bytes_read);
14339 found_base = 1;
14340 buffer += bytes_read;
14341 break;
14342 case DW_RLE_start_length:
14343 if (buffer + cu->header.addr_size > buf_end)
14344 {
14345 overflow = true;
14346 break;
14347 }
14348 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14349 buffer += bytes_read;
14350 range_end = (range_beginning
14351 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14352 buffer += bytes_read;
14353 if (buffer > buf_end)
14354 {
14355 overflow = true;
14356 break;
14357 }
14358 break;
14359 case DW_RLE_offset_pair:
14360 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14361 buffer += bytes_read;
14362 if (buffer > buf_end)
14363 {
14364 overflow = true;
14365 break;
14366 }
14367 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14368 buffer += bytes_read;
14369 if (buffer > buf_end)
14370 {
14371 overflow = true;
14372 break;
14373 }
14374 break;
14375 case DW_RLE_start_end:
14376 if (buffer + 2 * cu->header.addr_size > buf_end)
14377 {
14378 overflow = true;
14379 break;
14380 }
14381 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14382 buffer += bytes_read;
14383 range_end = read_address (obfd, buffer, cu, &bytes_read);
14384 buffer += bytes_read;
14385 break;
14386 default:
14387 complaint (_("Invalid .debug_rnglists data (no base address)"));
14388 return false;
14389 }
14390 if (rlet == DW_RLE_end_of_list || overflow)
14391 break;
14392 if (rlet == DW_RLE_base_address)
14393 continue;
14394
14395 if (!found_base)
14396 {
14397 /* We have no valid base address for the ranges
14398 data. */
14399 complaint (_("Invalid .debug_rnglists data (no base address)"));
14400 return false;
14401 }
14402
14403 if (range_beginning > range_end)
14404 {
14405 /* Inverted range entries are invalid. */
14406 complaint (_("Invalid .debug_rnglists data (inverted range)"));
14407 return false;
14408 }
14409
14410 /* Empty range entries have no effect. */
14411 if (range_beginning == range_end)
14412 continue;
14413
14414 range_beginning += base;
14415 range_end += base;
14416
14417 /* A not-uncommon case of bad debug info.
14418 Don't pollute the addrmap with bad data. */
14419 if (range_beginning + baseaddr == 0
14420 && !dwarf2_per_objfile->has_section_at_zero)
14421 {
14422 complaint (_(".debug_rnglists entry has start address of zero"
14423 " [in module %s]"), objfile_name (objfile));
14424 continue;
14425 }
14426
14427 callback (range_beginning, range_end);
14428 }
14429
14430 if (overflow)
14431 {
14432 complaint (_("Offset %d is not terminated "
14433 "for DW_AT_ranges attribute"),
14434 offset);
14435 return false;
14436 }
14437
14438 return true;
14439 }
14440
14441 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14442 Callback's type should be:
14443 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14444 Return 1 if the attributes are present and valid, otherwise, return 0. */
14445
14446 template <typename Callback>
14447 static int
14448 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
14449 Callback &&callback)
14450 {
14451 struct dwarf2_per_objfile *dwarf2_per_objfile
14452 = cu->per_cu->dwarf2_per_objfile;
14453 struct objfile *objfile = dwarf2_per_objfile->objfile;
14454 struct comp_unit_head *cu_header = &cu->header;
14455 bfd *obfd = objfile->obfd;
14456 unsigned int addr_size = cu_header->addr_size;
14457 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14458 /* Base address selection entry. */
14459 CORE_ADDR base;
14460 int found_base;
14461 unsigned int dummy;
14462 const gdb_byte *buffer;
14463 CORE_ADDR baseaddr;
14464
14465 if (cu_header->version >= 5)
14466 return dwarf2_rnglists_process (offset, cu, callback);
14467
14468 found_base = cu->base_known;
14469 base = cu->base_address;
14470
14471 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
14472 if (offset >= dwarf2_per_objfile->ranges.size)
14473 {
14474 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
14475 offset);
14476 return 0;
14477 }
14478 buffer = dwarf2_per_objfile->ranges.buffer + offset;
14479
14480 baseaddr = objfile->text_section_offset ();
14481
14482 while (1)
14483 {
14484 CORE_ADDR range_beginning, range_end;
14485
14486 range_beginning = read_address (obfd, buffer, cu, &dummy);
14487 buffer += addr_size;
14488 range_end = read_address (obfd, buffer, cu, &dummy);
14489 buffer += addr_size;
14490 offset += 2 * addr_size;
14491
14492 /* An end of list marker is a pair of zero addresses. */
14493 if (range_beginning == 0 && range_end == 0)
14494 /* Found the end of list entry. */
14495 break;
14496
14497 /* Each base address selection entry is a pair of 2 values.
14498 The first is the largest possible address, the second is
14499 the base address. Check for a base address here. */
14500 if ((range_beginning & mask) == mask)
14501 {
14502 /* If we found the largest possible address, then we already
14503 have the base address in range_end. */
14504 base = range_end;
14505 found_base = 1;
14506 continue;
14507 }
14508
14509 if (!found_base)
14510 {
14511 /* We have no valid base address for the ranges
14512 data. */
14513 complaint (_("Invalid .debug_ranges data (no base address)"));
14514 return 0;
14515 }
14516
14517 if (range_beginning > range_end)
14518 {
14519 /* Inverted range entries are invalid. */
14520 complaint (_("Invalid .debug_ranges data (inverted range)"));
14521 return 0;
14522 }
14523
14524 /* Empty range entries have no effect. */
14525 if (range_beginning == range_end)
14526 continue;
14527
14528 range_beginning += base;
14529 range_end += base;
14530
14531 /* A not-uncommon case of bad debug info.
14532 Don't pollute the addrmap with bad data. */
14533 if (range_beginning + baseaddr == 0
14534 && !dwarf2_per_objfile->has_section_at_zero)
14535 {
14536 complaint (_(".debug_ranges entry has start address of zero"
14537 " [in module %s]"), objfile_name (objfile));
14538 continue;
14539 }
14540
14541 callback (range_beginning, range_end);
14542 }
14543
14544 return 1;
14545 }
14546
14547 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14548 Return 1 if the attributes are present and valid, otherwise, return 0.
14549 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14550
14551 static int
14552 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14553 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14554 dwarf2_psymtab *ranges_pst)
14555 {
14556 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14557 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14558 const CORE_ADDR baseaddr = objfile->text_section_offset ();
14559 int low_set = 0;
14560 CORE_ADDR low = 0;
14561 CORE_ADDR high = 0;
14562 int retval;
14563
14564 retval = dwarf2_ranges_process (offset, cu,
14565 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14566 {
14567 if (ranges_pst != NULL)
14568 {
14569 CORE_ADDR lowpc;
14570 CORE_ADDR highpc;
14571
14572 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14573 range_beginning + baseaddr)
14574 - baseaddr);
14575 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14576 range_end + baseaddr)
14577 - baseaddr);
14578 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
14579 lowpc, highpc - 1, ranges_pst);
14580 }
14581
14582 /* FIXME: This is recording everything as a low-high
14583 segment of consecutive addresses. We should have a
14584 data structure for discontiguous block ranges
14585 instead. */
14586 if (! low_set)
14587 {
14588 low = range_beginning;
14589 high = range_end;
14590 low_set = 1;
14591 }
14592 else
14593 {
14594 if (range_beginning < low)
14595 low = range_beginning;
14596 if (range_end > high)
14597 high = range_end;
14598 }
14599 });
14600 if (!retval)
14601 return 0;
14602
14603 if (! low_set)
14604 /* If the first entry is an end-of-list marker, the range
14605 describes an empty scope, i.e. no instructions. */
14606 return 0;
14607
14608 if (low_return)
14609 *low_return = low;
14610 if (high_return)
14611 *high_return = high;
14612 return 1;
14613 }
14614
14615 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
14616 definition for the return value. *LOWPC and *HIGHPC are set iff
14617 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
14618
14619 static enum pc_bounds_kind
14620 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
14621 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14622 dwarf2_psymtab *pst)
14623 {
14624 struct dwarf2_per_objfile *dwarf2_per_objfile
14625 = cu->per_cu->dwarf2_per_objfile;
14626 struct attribute *attr;
14627 struct attribute *attr_high;
14628 CORE_ADDR low = 0;
14629 CORE_ADDR high = 0;
14630 enum pc_bounds_kind ret;
14631
14632 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14633 if (attr_high)
14634 {
14635 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14636 if (attr != nullptr)
14637 {
14638 low = attr_value_as_address (attr);
14639 high = attr_value_as_address (attr_high);
14640 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14641 high += low;
14642 }
14643 else
14644 /* Found high w/o low attribute. */
14645 return PC_BOUNDS_INVALID;
14646
14647 /* Found consecutive range of addresses. */
14648 ret = PC_BOUNDS_HIGH_LOW;
14649 }
14650 else
14651 {
14652 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14653 if (attr != NULL)
14654 {
14655 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
14656 We take advantage of the fact that DW_AT_ranges does not appear
14657 in DW_TAG_compile_unit of DWO files. */
14658 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14659 unsigned int ranges_offset = (DW_UNSND (attr)
14660 + (need_ranges_base
14661 ? cu->ranges_base
14662 : 0));
14663
14664 /* Value of the DW_AT_ranges attribute is the offset in the
14665 .debug_ranges section. */
14666 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
14667 return PC_BOUNDS_INVALID;
14668 /* Found discontinuous range of addresses. */
14669 ret = PC_BOUNDS_RANGES;
14670 }
14671 else
14672 return PC_BOUNDS_NOT_PRESENT;
14673 }
14674
14675 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
14676 if (high <= low)
14677 return PC_BOUNDS_INVALID;
14678
14679 /* When using the GNU linker, .gnu.linkonce. sections are used to
14680 eliminate duplicate copies of functions and vtables and such.
14681 The linker will arbitrarily choose one and discard the others.
14682 The AT_*_pc values for such functions refer to local labels in
14683 these sections. If the section from that file was discarded, the
14684 labels are not in the output, so the relocs get a value of 0.
14685 If this is a discarded function, mark the pc bounds as invalid,
14686 so that GDB will ignore it. */
14687 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
14688 return PC_BOUNDS_INVALID;
14689
14690 *lowpc = low;
14691 if (highpc)
14692 *highpc = high;
14693 return ret;
14694 }
14695
14696 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
14697 its low and high PC addresses. Do nothing if these addresses could not
14698 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14699 and HIGHPC to the high address if greater than HIGHPC. */
14700
14701 static void
14702 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14703 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14704 struct dwarf2_cu *cu)
14705 {
14706 CORE_ADDR low, high;
14707 struct die_info *child = die->child;
14708
14709 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
14710 {
14711 *lowpc = std::min (*lowpc, low);
14712 *highpc = std::max (*highpc, high);
14713 }
14714
14715 /* If the language does not allow nested subprograms (either inside
14716 subprograms or lexical blocks), we're done. */
14717 if (cu->language != language_ada)
14718 return;
14719
14720 /* Check all the children of the given DIE. If it contains nested
14721 subprograms, then check their pc bounds. Likewise, we need to
14722 check lexical blocks as well, as they may also contain subprogram
14723 definitions. */
14724 while (child && child->tag)
14725 {
14726 if (child->tag == DW_TAG_subprogram
14727 || child->tag == DW_TAG_lexical_block)
14728 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14729 child = sibling_die (child);
14730 }
14731 }
14732
14733 /* Get the low and high pc's represented by the scope DIE, and store
14734 them in *LOWPC and *HIGHPC. If the correct values can't be
14735 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14736
14737 static void
14738 get_scope_pc_bounds (struct die_info *die,
14739 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14740 struct dwarf2_cu *cu)
14741 {
14742 CORE_ADDR best_low = (CORE_ADDR) -1;
14743 CORE_ADDR best_high = (CORE_ADDR) 0;
14744 CORE_ADDR current_low, current_high;
14745
14746 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
14747 >= PC_BOUNDS_RANGES)
14748 {
14749 best_low = current_low;
14750 best_high = current_high;
14751 }
14752 else
14753 {
14754 struct die_info *child = die->child;
14755
14756 while (child && child->tag)
14757 {
14758 switch (child->tag) {
14759 case DW_TAG_subprogram:
14760 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14761 break;
14762 case DW_TAG_namespace:
14763 case DW_TAG_module:
14764 /* FIXME: carlton/2004-01-16: Should we do this for
14765 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14766 that current GCC's always emit the DIEs corresponding
14767 to definitions of methods of classes as children of a
14768 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14769 the DIEs giving the declarations, which could be
14770 anywhere). But I don't see any reason why the
14771 standards says that they have to be there. */
14772 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14773
14774 if (current_low != ((CORE_ADDR) -1))
14775 {
14776 best_low = std::min (best_low, current_low);
14777 best_high = std::max (best_high, current_high);
14778 }
14779 break;
14780 default:
14781 /* Ignore. */
14782 break;
14783 }
14784
14785 child = sibling_die (child);
14786 }
14787 }
14788
14789 *lowpc = best_low;
14790 *highpc = best_high;
14791 }
14792
14793 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14794 in DIE. */
14795
14796 static void
14797 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14798 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14799 {
14800 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14801 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14802 struct attribute *attr;
14803 struct attribute *attr_high;
14804
14805 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14806 if (attr_high)
14807 {
14808 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14809 if (attr != nullptr)
14810 {
14811 CORE_ADDR low = attr_value_as_address (attr);
14812 CORE_ADDR high = attr_value_as_address (attr_high);
14813
14814 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14815 high += low;
14816
14817 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14818 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14819 cu->get_builder ()->record_block_range (block, low, high - 1);
14820 }
14821 }
14822
14823 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14824 if (attr != nullptr)
14825 {
14826 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
14827 We take advantage of the fact that DW_AT_ranges does not appear
14828 in DW_TAG_compile_unit of DWO files. */
14829 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14830
14831 /* The value of the DW_AT_ranges attribute is the offset of the
14832 address range list in the .debug_ranges section. */
14833 unsigned long offset = (DW_UNSND (attr)
14834 + (need_ranges_base ? cu->ranges_base : 0));
14835
14836 std::vector<blockrange> blockvec;
14837 dwarf2_ranges_process (offset, cu,
14838 [&] (CORE_ADDR start, CORE_ADDR end)
14839 {
14840 start += baseaddr;
14841 end += baseaddr;
14842 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14843 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14844 cu->get_builder ()->record_block_range (block, start, end - 1);
14845 blockvec.emplace_back (start, end);
14846 });
14847
14848 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
14849 }
14850 }
14851
14852 /* Check whether the producer field indicates either of GCC < 4.6, or the
14853 Intel C/C++ compiler, and cache the result in CU. */
14854
14855 static void
14856 check_producer (struct dwarf2_cu *cu)
14857 {
14858 int major, minor;
14859
14860 if (cu->producer == NULL)
14861 {
14862 /* For unknown compilers expect their behavior is DWARF version
14863 compliant.
14864
14865 GCC started to support .debug_types sections by -gdwarf-4 since
14866 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14867 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14868 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14869 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14870 }
14871 else if (producer_is_gcc (cu->producer, &major, &minor))
14872 {
14873 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14874 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14875 }
14876 else if (producer_is_icc (cu->producer, &major, &minor))
14877 {
14878 cu->producer_is_icc = true;
14879 cu->producer_is_icc_lt_14 = major < 14;
14880 }
14881 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14882 cu->producer_is_codewarrior = true;
14883 else
14884 {
14885 /* For other non-GCC compilers, expect their behavior is DWARF version
14886 compliant. */
14887 }
14888
14889 cu->checked_producer = true;
14890 }
14891
14892 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14893 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14894 during 4.6.0 experimental. */
14895
14896 static bool
14897 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14898 {
14899 if (!cu->checked_producer)
14900 check_producer (cu);
14901
14902 return cu->producer_is_gxx_lt_4_6;
14903 }
14904
14905
14906 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14907 with incorrect is_stmt attributes. */
14908
14909 static bool
14910 producer_is_codewarrior (struct dwarf2_cu *cu)
14911 {
14912 if (!cu->checked_producer)
14913 check_producer (cu);
14914
14915 return cu->producer_is_codewarrior;
14916 }
14917
14918 /* Return the default accessibility type if it is not overridden by
14919 DW_AT_accessibility. */
14920
14921 static enum dwarf_access_attribute
14922 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14923 {
14924 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14925 {
14926 /* The default DWARF 2 accessibility for members is public, the default
14927 accessibility for inheritance is private. */
14928
14929 if (die->tag != DW_TAG_inheritance)
14930 return DW_ACCESS_public;
14931 else
14932 return DW_ACCESS_private;
14933 }
14934 else
14935 {
14936 /* DWARF 3+ defines the default accessibility a different way. The same
14937 rules apply now for DW_TAG_inheritance as for the members and it only
14938 depends on the container kind. */
14939
14940 if (die->parent->tag == DW_TAG_class_type)
14941 return DW_ACCESS_private;
14942 else
14943 return DW_ACCESS_public;
14944 }
14945 }
14946
14947 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14948 offset. If the attribute was not found return 0, otherwise return
14949 1. If it was found but could not properly be handled, set *OFFSET
14950 to 0. */
14951
14952 static int
14953 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14954 LONGEST *offset)
14955 {
14956 struct attribute *attr;
14957
14958 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14959 if (attr != NULL)
14960 {
14961 *offset = 0;
14962
14963 /* Note that we do not check for a section offset first here.
14964 This is because DW_AT_data_member_location is new in DWARF 4,
14965 so if we see it, we can assume that a constant form is really
14966 a constant and not a section offset. */
14967 if (attr_form_is_constant (attr))
14968 *offset = dwarf2_get_attr_constant_value (attr, 0);
14969 else if (attr_form_is_section_offset (attr))
14970 dwarf2_complex_location_expr_complaint ();
14971 else if (attr_form_is_block (attr))
14972 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14973 else
14974 dwarf2_complex_location_expr_complaint ();
14975
14976 return 1;
14977 }
14978
14979 return 0;
14980 }
14981
14982 /* Add an aggregate field to the field list. */
14983
14984 static void
14985 dwarf2_add_field (struct field_info *fip, struct die_info *die,
14986 struct dwarf2_cu *cu)
14987 {
14988 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14989 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14990 struct nextfield *new_field;
14991 struct attribute *attr;
14992 struct field *fp;
14993 const char *fieldname = "";
14994
14995 if (die->tag == DW_TAG_inheritance)
14996 {
14997 fip->baseclasses.emplace_back ();
14998 new_field = &fip->baseclasses.back ();
14999 }
15000 else
15001 {
15002 fip->fields.emplace_back ();
15003 new_field = &fip->fields.back ();
15004 }
15005
15006 fip->nfields++;
15007
15008 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15009 if (attr != nullptr)
15010 new_field->accessibility = DW_UNSND (attr);
15011 else
15012 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
15013 if (new_field->accessibility != DW_ACCESS_public)
15014 fip->non_public_fields = 1;
15015
15016 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15017 if (attr != nullptr)
15018 new_field->virtuality = DW_UNSND (attr);
15019 else
15020 new_field->virtuality = DW_VIRTUALITY_none;
15021
15022 fp = &new_field->field;
15023
15024 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
15025 {
15026 LONGEST offset;
15027
15028 /* Data member other than a C++ static data member. */
15029
15030 /* Get type of field. */
15031 fp->type = die_type (die, cu);
15032
15033 SET_FIELD_BITPOS (*fp, 0);
15034
15035 /* Get bit size of field (zero if none). */
15036 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
15037 if (attr != nullptr)
15038 {
15039 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
15040 }
15041 else
15042 {
15043 FIELD_BITSIZE (*fp) = 0;
15044 }
15045
15046 /* Get bit offset of field. */
15047 if (handle_data_member_location (die, cu, &offset))
15048 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
15049 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
15050 if (attr != nullptr)
15051 {
15052 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
15053 {
15054 /* For big endian bits, the DW_AT_bit_offset gives the
15055 additional bit offset from the MSB of the containing
15056 anonymous object to the MSB of the field. We don't
15057 have to do anything special since we don't need to
15058 know the size of the anonymous object. */
15059 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
15060 }
15061 else
15062 {
15063 /* For little endian bits, compute the bit offset to the
15064 MSB of the anonymous object, subtract off the number of
15065 bits from the MSB of the field to the MSB of the
15066 object, and then subtract off the number of bits of
15067 the field itself. The result is the bit offset of
15068 the LSB of the field. */
15069 int anonymous_size;
15070 int bit_offset = DW_UNSND (attr);
15071
15072 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15073 if (attr != nullptr)
15074 {
15075 /* The size of the anonymous object containing
15076 the bit field is explicit, so use the
15077 indicated size (in bytes). */
15078 anonymous_size = DW_UNSND (attr);
15079 }
15080 else
15081 {
15082 /* The size of the anonymous object containing
15083 the bit field must be inferred from the type
15084 attribute of the data member containing the
15085 bit field. */
15086 anonymous_size = TYPE_LENGTH (fp->type);
15087 }
15088 SET_FIELD_BITPOS (*fp,
15089 (FIELD_BITPOS (*fp)
15090 + anonymous_size * bits_per_byte
15091 - bit_offset - FIELD_BITSIZE (*fp)));
15092 }
15093 }
15094 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15095 if (attr != NULL)
15096 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15097 + dwarf2_get_attr_constant_value (attr, 0)));
15098
15099 /* Get name of field. */
15100 fieldname = dwarf2_name (die, cu);
15101 if (fieldname == NULL)
15102 fieldname = "";
15103
15104 /* The name is already allocated along with this objfile, so we don't
15105 need to duplicate it for the type. */
15106 fp->name = fieldname;
15107
15108 /* Change accessibility for artificial fields (e.g. virtual table
15109 pointer or virtual base class pointer) to private. */
15110 if (dwarf2_attr (die, DW_AT_artificial, cu))
15111 {
15112 FIELD_ARTIFICIAL (*fp) = 1;
15113 new_field->accessibility = DW_ACCESS_private;
15114 fip->non_public_fields = 1;
15115 }
15116 }
15117 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
15118 {
15119 /* C++ static member. */
15120
15121 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15122 is a declaration, but all versions of G++ as of this writing
15123 (so through at least 3.2.1) incorrectly generate
15124 DW_TAG_variable tags. */
15125
15126 const char *physname;
15127
15128 /* Get name of field. */
15129 fieldname = dwarf2_name (die, cu);
15130 if (fieldname == NULL)
15131 return;
15132
15133 attr = dwarf2_attr (die, DW_AT_const_value, cu);
15134 if (attr
15135 /* Only create a symbol if this is an external value.
15136 new_symbol checks this and puts the value in the global symbol
15137 table, which we want. If it is not external, new_symbol
15138 will try to put the value in cu->list_in_scope which is wrong. */
15139 && dwarf2_flag_true_p (die, DW_AT_external, cu))
15140 {
15141 /* A static const member, not much different than an enum as far as
15142 we're concerned, except that we can support more types. */
15143 new_symbol (die, NULL, cu);
15144 }
15145
15146 /* Get physical name. */
15147 physname = dwarf2_physname (fieldname, die, cu);
15148
15149 /* The name is already allocated along with this objfile, so we don't
15150 need to duplicate it for the type. */
15151 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
15152 FIELD_TYPE (*fp) = die_type (die, cu);
15153 FIELD_NAME (*fp) = fieldname;
15154 }
15155 else if (die->tag == DW_TAG_inheritance)
15156 {
15157 LONGEST offset;
15158
15159 /* C++ base class field. */
15160 if (handle_data_member_location (die, cu, &offset))
15161 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
15162 FIELD_BITSIZE (*fp) = 0;
15163 FIELD_TYPE (*fp) = die_type (die, cu);
15164 FIELD_NAME (*fp) = TYPE_NAME (fp->type);
15165 }
15166 else if (die->tag == DW_TAG_variant_part)
15167 {
15168 /* process_structure_scope will treat this DIE as a union. */
15169 process_structure_scope (die, cu);
15170
15171 /* The variant part is relative to the start of the enclosing
15172 structure. */
15173 SET_FIELD_BITPOS (*fp, 0);
15174 fp->type = get_die_type (die, cu);
15175 fp->artificial = 1;
15176 fp->name = "<<variant>>";
15177
15178 /* Normally a DW_TAG_variant_part won't have a size, but our
15179 representation requires one, so set it to the maximum of the
15180 child sizes, being sure to account for the offset at which
15181 each child is seen. */
15182 if (TYPE_LENGTH (fp->type) == 0)
15183 {
15184 unsigned max = 0;
15185 for (int i = 0; i < TYPE_NFIELDS (fp->type); ++i)
15186 {
15187 unsigned len = ((TYPE_FIELD_BITPOS (fp->type, i) + 7) / 8
15188 + TYPE_LENGTH (TYPE_FIELD_TYPE (fp->type, i)));
15189 if (len > max)
15190 max = len;
15191 }
15192 TYPE_LENGTH (fp->type) = max;
15193 }
15194 }
15195 else
15196 gdb_assert_not_reached ("missing case in dwarf2_add_field");
15197 }
15198
15199 /* Can the type given by DIE define another type? */
15200
15201 static bool
15202 type_can_define_types (const struct die_info *die)
15203 {
15204 switch (die->tag)
15205 {
15206 case DW_TAG_typedef:
15207 case DW_TAG_class_type:
15208 case DW_TAG_structure_type:
15209 case DW_TAG_union_type:
15210 case DW_TAG_enumeration_type:
15211 return true;
15212
15213 default:
15214 return false;
15215 }
15216 }
15217
15218 /* Add a type definition defined in the scope of the FIP's class. */
15219
15220 static void
15221 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15222 struct dwarf2_cu *cu)
15223 {
15224 struct decl_field fp;
15225 memset (&fp, 0, sizeof (fp));
15226
15227 gdb_assert (type_can_define_types (die));
15228
15229 /* Get name of field. NULL is okay here, meaning an anonymous type. */
15230 fp.name = dwarf2_name (die, cu);
15231 fp.type = read_type_die (die, cu);
15232
15233 /* Save accessibility. */
15234 enum dwarf_access_attribute accessibility;
15235 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15236 if (attr != NULL)
15237 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15238 else
15239 accessibility = dwarf2_default_access_attribute (die, cu);
15240 switch (accessibility)
15241 {
15242 case DW_ACCESS_public:
15243 /* The assumed value if neither private nor protected. */
15244 break;
15245 case DW_ACCESS_private:
15246 fp.is_private = 1;
15247 break;
15248 case DW_ACCESS_protected:
15249 fp.is_protected = 1;
15250 break;
15251 default:
15252 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
15253 }
15254
15255 if (die->tag == DW_TAG_typedef)
15256 fip->typedef_field_list.push_back (fp);
15257 else
15258 fip->nested_types_list.push_back (fp);
15259 }
15260
15261 /* Create the vector of fields, and attach it to the type. */
15262
15263 static void
15264 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
15265 struct dwarf2_cu *cu)
15266 {
15267 int nfields = fip->nfields;
15268
15269 /* Record the field count, allocate space for the array of fields,
15270 and create blank accessibility bitfields if necessary. */
15271 TYPE_NFIELDS (type) = nfields;
15272 TYPE_FIELDS (type) = (struct field *)
15273 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
15274
15275 if (fip->non_public_fields && cu->language != language_ada)
15276 {
15277 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15278
15279 TYPE_FIELD_PRIVATE_BITS (type) =
15280 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15281 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15282
15283 TYPE_FIELD_PROTECTED_BITS (type) =
15284 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15285 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15286
15287 TYPE_FIELD_IGNORE_BITS (type) =
15288 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15289 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
15290 }
15291
15292 /* If the type has baseclasses, allocate and clear a bit vector for
15293 TYPE_FIELD_VIRTUAL_BITS. */
15294 if (!fip->baseclasses.empty () && cu->language != language_ada)
15295 {
15296 int num_bytes = B_BYTES (fip->baseclasses.size ());
15297 unsigned char *pointer;
15298
15299 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15300 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
15301 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
15302 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15303 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
15304 }
15305
15306 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
15307 {
15308 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
15309
15310 for (int index = 0; index < nfields; ++index)
15311 {
15312 struct nextfield &field = fip->fields[index];
15313
15314 if (field.variant.is_discriminant)
15315 di->discriminant_index = index;
15316 else if (field.variant.default_branch)
15317 di->default_index = index;
15318 else
15319 di->discriminants[index] = field.variant.discriminant_value;
15320 }
15321 }
15322
15323 /* Copy the saved-up fields into the field vector. */
15324 for (int i = 0; i < nfields; ++i)
15325 {
15326 struct nextfield &field
15327 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15328 : fip->fields[i - fip->baseclasses.size ()]);
15329
15330 TYPE_FIELD (type, i) = field.field;
15331 switch (field.accessibility)
15332 {
15333 case DW_ACCESS_private:
15334 if (cu->language != language_ada)
15335 SET_TYPE_FIELD_PRIVATE (type, i);
15336 break;
15337
15338 case DW_ACCESS_protected:
15339 if (cu->language != language_ada)
15340 SET_TYPE_FIELD_PROTECTED (type, i);
15341 break;
15342
15343 case DW_ACCESS_public:
15344 break;
15345
15346 default:
15347 /* Unknown accessibility. Complain and treat it as public. */
15348 {
15349 complaint (_("unsupported accessibility %d"),
15350 field.accessibility);
15351 }
15352 break;
15353 }
15354 if (i < fip->baseclasses.size ())
15355 {
15356 switch (field.virtuality)
15357 {
15358 case DW_VIRTUALITY_virtual:
15359 case DW_VIRTUALITY_pure_virtual:
15360 if (cu->language == language_ada)
15361 error (_("unexpected virtuality in component of Ada type"));
15362 SET_TYPE_FIELD_VIRTUAL (type, i);
15363 break;
15364 }
15365 }
15366 }
15367 }
15368
15369 /* Return true if this member function is a constructor, false
15370 otherwise. */
15371
15372 static int
15373 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15374 {
15375 const char *fieldname;
15376 const char *type_name;
15377 int len;
15378
15379 if (die->parent == NULL)
15380 return 0;
15381
15382 if (die->parent->tag != DW_TAG_structure_type
15383 && die->parent->tag != DW_TAG_union_type
15384 && die->parent->tag != DW_TAG_class_type)
15385 return 0;
15386
15387 fieldname = dwarf2_name (die, cu);
15388 type_name = dwarf2_name (die->parent, cu);
15389 if (fieldname == NULL || type_name == NULL)
15390 return 0;
15391
15392 len = strlen (fieldname);
15393 return (strncmp (fieldname, type_name, len) == 0
15394 && (type_name[len] == '\0' || type_name[len] == '<'));
15395 }
15396
15397 /* Check if the given VALUE is a recognized enum
15398 dwarf_defaulted_attribute constant according to DWARF5 spec,
15399 Table 7.24. */
15400
15401 static bool
15402 is_valid_DW_AT_defaulted (ULONGEST value)
15403 {
15404 switch (value)
15405 {
15406 case DW_DEFAULTED_no:
15407 case DW_DEFAULTED_in_class:
15408 case DW_DEFAULTED_out_of_class:
15409 return true;
15410 }
15411
15412 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
15413 return false;
15414 }
15415
15416 /* Add a member function to the proper fieldlist. */
15417
15418 static void
15419 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
15420 struct type *type, struct dwarf2_cu *cu)
15421 {
15422 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15423 struct attribute *attr;
15424 int i;
15425 struct fnfieldlist *flp = nullptr;
15426 struct fn_field *fnp;
15427 const char *fieldname;
15428 struct type *this_type;
15429 enum dwarf_access_attribute accessibility;
15430
15431 if (cu->language == language_ada)
15432 error (_("unexpected member function in Ada type"));
15433
15434 /* Get name of member function. */
15435 fieldname = dwarf2_name (die, cu);
15436 if (fieldname == NULL)
15437 return;
15438
15439 /* Look up member function name in fieldlist. */
15440 for (i = 0; i < fip->fnfieldlists.size (); i++)
15441 {
15442 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
15443 {
15444 flp = &fip->fnfieldlists[i];
15445 break;
15446 }
15447 }
15448
15449 /* Create a new fnfieldlist if necessary. */
15450 if (flp == nullptr)
15451 {
15452 fip->fnfieldlists.emplace_back ();
15453 flp = &fip->fnfieldlists.back ();
15454 flp->name = fieldname;
15455 i = fip->fnfieldlists.size () - 1;
15456 }
15457
15458 /* Create a new member function field and add it to the vector of
15459 fnfieldlists. */
15460 flp->fnfields.emplace_back ();
15461 fnp = &flp->fnfields.back ();
15462
15463 /* Delay processing of the physname until later. */
15464 if (cu->language == language_cplus)
15465 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15466 die, cu);
15467 else
15468 {
15469 const char *physname = dwarf2_physname (fieldname, die, cu);
15470 fnp->physname = physname ? physname : "";
15471 }
15472
15473 fnp->type = alloc_type (objfile);
15474 this_type = read_type_die (die, cu);
15475 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
15476 {
15477 int nparams = TYPE_NFIELDS (this_type);
15478
15479 /* TYPE is the domain of this method, and THIS_TYPE is the type
15480 of the method itself (TYPE_CODE_METHOD). */
15481 smash_to_method_type (fnp->type, type,
15482 TYPE_TARGET_TYPE (this_type),
15483 TYPE_FIELDS (this_type),
15484 TYPE_NFIELDS (this_type),
15485 TYPE_VARARGS (this_type));
15486
15487 /* Handle static member functions.
15488 Dwarf2 has no clean way to discern C++ static and non-static
15489 member functions. G++ helps GDB by marking the first
15490 parameter for non-static member functions (which is the this
15491 pointer) as artificial. We obtain this information from
15492 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15493 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15494 fnp->voffset = VOFFSET_STATIC;
15495 }
15496 else
15497 complaint (_("member function type missing for '%s'"),
15498 dwarf2_full_name (fieldname, die, cu));
15499
15500 /* Get fcontext from DW_AT_containing_type if present. */
15501 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15502 fnp->fcontext = die_containing_type (die, cu);
15503
15504 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15505 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15506
15507 /* Get accessibility. */
15508 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15509 if (attr != nullptr)
15510 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15511 else
15512 accessibility = dwarf2_default_access_attribute (die, cu);
15513 switch (accessibility)
15514 {
15515 case DW_ACCESS_private:
15516 fnp->is_private = 1;
15517 break;
15518 case DW_ACCESS_protected:
15519 fnp->is_protected = 1;
15520 break;
15521 }
15522
15523 /* Check for artificial methods. */
15524 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15525 if (attr && DW_UNSND (attr) != 0)
15526 fnp->is_artificial = 1;
15527
15528 /* Check for defaulted methods. */
15529 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
15530 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
15531 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
15532
15533 /* Check for deleted methods. */
15534 attr = dwarf2_attr (die, DW_AT_deleted, cu);
15535 if (attr != nullptr && DW_UNSND (attr) != 0)
15536 fnp->is_deleted = 1;
15537
15538 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15539
15540 /* Get index in virtual function table if it is a virtual member
15541 function. For older versions of GCC, this is an offset in the
15542 appropriate virtual table, as specified by DW_AT_containing_type.
15543 For everyone else, it is an expression to be evaluated relative
15544 to the object address. */
15545
15546 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15547 if (attr != nullptr)
15548 {
15549 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
15550 {
15551 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15552 {
15553 /* Old-style GCC. */
15554 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15555 }
15556 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15557 || (DW_BLOCK (attr)->size > 1
15558 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15559 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15560 {
15561 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15562 if ((fnp->voffset % cu->header.addr_size) != 0)
15563 dwarf2_complex_location_expr_complaint ();
15564 else
15565 fnp->voffset /= cu->header.addr_size;
15566 fnp->voffset += 2;
15567 }
15568 else
15569 dwarf2_complex_location_expr_complaint ();
15570
15571 if (!fnp->fcontext)
15572 {
15573 /* If there is no `this' field and no DW_AT_containing_type,
15574 we cannot actually find a base class context for the
15575 vtable! */
15576 if (TYPE_NFIELDS (this_type) == 0
15577 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15578 {
15579 complaint (_("cannot determine context for virtual member "
15580 "function \"%s\" (offset %s)"),
15581 fieldname, sect_offset_str (die->sect_off));
15582 }
15583 else
15584 {
15585 fnp->fcontext
15586 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15587 }
15588 }
15589 }
15590 else if (attr_form_is_section_offset (attr))
15591 {
15592 dwarf2_complex_location_expr_complaint ();
15593 }
15594 else
15595 {
15596 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15597 fieldname);
15598 }
15599 }
15600 else
15601 {
15602 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15603 if (attr && DW_UNSND (attr))
15604 {
15605 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15606 complaint (_("Member function \"%s\" (offset %s) is virtual "
15607 "but the vtable offset is not specified"),
15608 fieldname, sect_offset_str (die->sect_off));
15609 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15610 TYPE_CPLUS_DYNAMIC (type) = 1;
15611 }
15612 }
15613 }
15614
15615 /* Create the vector of member function fields, and attach it to the type. */
15616
15617 static void
15618 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15619 struct dwarf2_cu *cu)
15620 {
15621 if (cu->language == language_ada)
15622 error (_("unexpected member functions in Ada type"));
15623
15624 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15625 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15626 TYPE_ALLOC (type,
15627 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
15628
15629 for (int i = 0; i < fip->fnfieldlists.size (); i++)
15630 {
15631 struct fnfieldlist &nf = fip->fnfieldlists[i];
15632 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15633
15634 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15635 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
15636 fn_flp->fn_fields = (struct fn_field *)
15637 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15638
15639 for (int k = 0; k < nf.fnfields.size (); ++k)
15640 fn_flp->fn_fields[k] = nf.fnfields[k];
15641 }
15642
15643 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
15644 }
15645
15646 /* Returns non-zero if NAME is the name of a vtable member in CU's
15647 language, zero otherwise. */
15648 static int
15649 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15650 {
15651 static const char vptr[] = "_vptr";
15652
15653 /* Look for the C++ form of the vtable. */
15654 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15655 return 1;
15656
15657 return 0;
15658 }
15659
15660 /* GCC outputs unnamed structures that are really pointers to member
15661 functions, with the ABI-specified layout. If TYPE describes
15662 such a structure, smash it into a member function type.
15663
15664 GCC shouldn't do this; it should just output pointer to member DIEs.
15665 This is GCC PR debug/28767. */
15666
15667 static void
15668 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15669 {
15670 struct type *pfn_type, *self_type, *new_type;
15671
15672 /* Check for a structure with no name and two children. */
15673 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
15674 return;
15675
15676 /* Check for __pfn and __delta members. */
15677 if (TYPE_FIELD_NAME (type, 0) == NULL
15678 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15679 || TYPE_FIELD_NAME (type, 1) == NULL
15680 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15681 return;
15682
15683 /* Find the type of the method. */
15684 pfn_type = TYPE_FIELD_TYPE (type, 0);
15685 if (pfn_type == NULL
15686 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
15687 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
15688 return;
15689
15690 /* Look for the "this" argument. */
15691 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15692 if (TYPE_NFIELDS (pfn_type) == 0
15693 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
15694 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
15695 return;
15696
15697 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
15698 new_type = alloc_type (objfile);
15699 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15700 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
15701 TYPE_VARARGS (pfn_type));
15702 smash_to_methodptr_type (type, new_type);
15703 }
15704
15705 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
15706 appropriate error checking and issuing complaints if there is a
15707 problem. */
15708
15709 static ULONGEST
15710 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15711 {
15712 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15713
15714 if (attr == nullptr)
15715 return 0;
15716
15717 if (!attr_form_is_constant (attr))
15718 {
15719 complaint (_("DW_AT_alignment must have constant form"
15720 " - DIE at %s [in module %s]"),
15721 sect_offset_str (die->sect_off),
15722 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15723 return 0;
15724 }
15725
15726 ULONGEST align;
15727 if (attr->form == DW_FORM_sdata)
15728 {
15729 LONGEST val = DW_SND (attr);
15730 if (val < 0)
15731 {
15732 complaint (_("DW_AT_alignment value must not be negative"
15733 " - DIE at %s [in module %s]"),
15734 sect_offset_str (die->sect_off),
15735 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15736 return 0;
15737 }
15738 align = val;
15739 }
15740 else
15741 align = DW_UNSND (attr);
15742
15743 if (align == 0)
15744 {
15745 complaint (_("DW_AT_alignment value must not be zero"
15746 " - DIE at %s [in module %s]"),
15747 sect_offset_str (die->sect_off),
15748 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15749 return 0;
15750 }
15751 if ((align & (align - 1)) != 0)
15752 {
15753 complaint (_("DW_AT_alignment value must be a power of 2"
15754 " - DIE at %s [in module %s]"),
15755 sect_offset_str (die->sect_off),
15756 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15757 return 0;
15758 }
15759
15760 return align;
15761 }
15762
15763 /* If the DIE has a DW_AT_alignment attribute, use its value to set
15764 the alignment for TYPE. */
15765
15766 static void
15767 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15768 struct type *type)
15769 {
15770 if (!set_type_align (type, get_alignment (cu, die)))
15771 complaint (_("DW_AT_alignment value too large"
15772 " - DIE at %s [in module %s]"),
15773 sect_offset_str (die->sect_off),
15774 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15775 }
15776
15777 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15778 constant for a type, according to DWARF5 spec, Table 5.5. */
15779
15780 static bool
15781 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15782 {
15783 switch (value)
15784 {
15785 case DW_CC_normal:
15786 case DW_CC_pass_by_reference:
15787 case DW_CC_pass_by_value:
15788 return true;
15789
15790 default:
15791 complaint (_("unrecognized DW_AT_calling_convention value "
15792 "(%s) for a type"), pulongest (value));
15793 return false;
15794 }
15795 }
15796
15797 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15798 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15799 also according to GNU-specific values (see include/dwarf2.h). */
15800
15801 static bool
15802 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15803 {
15804 switch (value)
15805 {
15806 case DW_CC_normal:
15807 case DW_CC_program:
15808 case DW_CC_nocall:
15809 return true;
15810
15811 case DW_CC_GNU_renesas_sh:
15812 case DW_CC_GNU_borland_fastcall_i386:
15813 case DW_CC_GDB_IBM_OpenCL:
15814 return true;
15815
15816 default:
15817 complaint (_("unrecognized DW_AT_calling_convention value "
15818 "(%s) for a subroutine"), pulongest (value));
15819 return false;
15820 }
15821 }
15822
15823 /* Called when we find the DIE that starts a structure or union scope
15824 (definition) to create a type for the structure or union. Fill in
15825 the type's name and general properties; the members will not be
15826 processed until process_structure_scope. A symbol table entry for
15827 the type will also not be done until process_structure_scope (assuming
15828 the type has a name).
15829
15830 NOTE: we need to call these functions regardless of whether or not the
15831 DIE has a DW_AT_name attribute, since it might be an anonymous
15832 structure or union. This gets the type entered into our set of
15833 user defined types. */
15834
15835 static struct type *
15836 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15837 {
15838 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15839 struct type *type;
15840 struct attribute *attr;
15841 const char *name;
15842
15843 /* If the definition of this type lives in .debug_types, read that type.
15844 Don't follow DW_AT_specification though, that will take us back up
15845 the chain and we want to go down. */
15846 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
15847 if (attr != nullptr)
15848 {
15849 type = get_DW_AT_signature_type (die, attr, cu);
15850
15851 /* The type's CU may not be the same as CU.
15852 Ensure TYPE is recorded with CU in die_type_hash. */
15853 return set_die_type (die, type, cu);
15854 }
15855
15856 type = alloc_type (objfile);
15857 INIT_CPLUS_SPECIFIC (type);
15858
15859 name = dwarf2_name (die, cu);
15860 if (name != NULL)
15861 {
15862 if (cu->language == language_cplus
15863 || cu->language == language_d
15864 || cu->language == language_rust)
15865 {
15866 const char *full_name = dwarf2_full_name (name, die, cu);
15867
15868 /* dwarf2_full_name might have already finished building the DIE's
15869 type. If so, there is no need to continue. */
15870 if (get_die_type (die, cu) != NULL)
15871 return get_die_type (die, cu);
15872
15873 TYPE_NAME (type) = full_name;
15874 }
15875 else
15876 {
15877 /* The name is already allocated along with this objfile, so
15878 we don't need to duplicate it for the type. */
15879 TYPE_NAME (type) = name;
15880 }
15881 }
15882
15883 if (die->tag == DW_TAG_structure_type)
15884 {
15885 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15886 }
15887 else if (die->tag == DW_TAG_union_type)
15888 {
15889 TYPE_CODE (type) = TYPE_CODE_UNION;
15890 }
15891 else if (die->tag == DW_TAG_variant_part)
15892 {
15893 TYPE_CODE (type) = TYPE_CODE_UNION;
15894 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
15895 }
15896 else
15897 {
15898 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15899 }
15900
15901 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15902 TYPE_DECLARED_CLASS (type) = 1;
15903
15904 /* Store the calling convention in the type if it's available in
15905 the die. Otherwise the calling convention remains set to
15906 the default value DW_CC_normal. */
15907 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15908 if (attr != nullptr
15909 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15910 {
15911 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15912 TYPE_CPLUS_CALLING_CONVENTION (type)
15913 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15914 }
15915
15916 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15917 if (attr != nullptr)
15918 {
15919 if (attr_form_is_constant (attr))
15920 TYPE_LENGTH (type) = DW_UNSND (attr);
15921 else
15922 {
15923 /* For the moment, dynamic type sizes are not supported
15924 by GDB's struct type. The actual size is determined
15925 on-demand when resolving the type of a given object,
15926 so set the type's length to zero for now. Otherwise,
15927 we record an expression as the length, and that expression
15928 could lead to a very large value, which could eventually
15929 lead to us trying to allocate that much memory when creating
15930 a value of that type. */
15931 TYPE_LENGTH (type) = 0;
15932 }
15933 }
15934 else
15935 {
15936 TYPE_LENGTH (type) = 0;
15937 }
15938
15939 maybe_set_alignment (cu, die, type);
15940
15941 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15942 {
15943 /* ICC<14 does not output the required DW_AT_declaration on
15944 incomplete types, but gives them a size of zero. */
15945 TYPE_STUB (type) = 1;
15946 }
15947 else
15948 TYPE_STUB_SUPPORTED (type) = 1;
15949
15950 if (die_is_declaration (die, cu))
15951 TYPE_STUB (type) = 1;
15952 else if (attr == NULL && die->child == NULL
15953 && producer_is_realview (cu->producer))
15954 /* RealView does not output the required DW_AT_declaration
15955 on incomplete types. */
15956 TYPE_STUB (type) = 1;
15957
15958 /* We need to add the type field to the die immediately so we don't
15959 infinitely recurse when dealing with pointers to the structure
15960 type within the structure itself. */
15961 set_die_type (die, type, cu);
15962
15963 /* set_die_type should be already done. */
15964 set_descriptive_type (type, die, cu);
15965
15966 return type;
15967 }
15968
15969 /* A helper for process_structure_scope that handles a single member
15970 DIE. */
15971
15972 static void
15973 handle_struct_member_die (struct die_info *child_die, struct type *type,
15974 struct field_info *fi,
15975 std::vector<struct symbol *> *template_args,
15976 struct dwarf2_cu *cu)
15977 {
15978 if (child_die->tag == DW_TAG_member
15979 || child_die->tag == DW_TAG_variable
15980 || child_die->tag == DW_TAG_variant_part)
15981 {
15982 /* NOTE: carlton/2002-11-05: A C++ static data member
15983 should be a DW_TAG_member that is a declaration, but
15984 all versions of G++ as of this writing (so through at
15985 least 3.2.1) incorrectly generate DW_TAG_variable
15986 tags for them instead. */
15987 dwarf2_add_field (fi, child_die, cu);
15988 }
15989 else if (child_die->tag == DW_TAG_subprogram)
15990 {
15991 /* Rust doesn't have member functions in the C++ sense.
15992 However, it does emit ordinary functions as children
15993 of a struct DIE. */
15994 if (cu->language == language_rust)
15995 read_func_scope (child_die, cu);
15996 else
15997 {
15998 /* C++ member function. */
15999 dwarf2_add_member_fn (fi, child_die, type, cu);
16000 }
16001 }
16002 else if (child_die->tag == DW_TAG_inheritance)
16003 {
16004 /* C++ base class field. */
16005 dwarf2_add_field (fi, child_die, cu);
16006 }
16007 else if (type_can_define_types (child_die))
16008 dwarf2_add_type_defn (fi, child_die, cu);
16009 else if (child_die->tag == DW_TAG_template_type_param
16010 || child_die->tag == DW_TAG_template_value_param)
16011 {
16012 struct symbol *arg = new_symbol (child_die, NULL, cu);
16013
16014 if (arg != NULL)
16015 template_args->push_back (arg);
16016 }
16017 else if (child_die->tag == DW_TAG_variant)
16018 {
16019 /* In a variant we want to get the discriminant and also add a
16020 field for our sole member child. */
16021 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
16022
16023 for (die_info *variant_child = child_die->child;
16024 variant_child != NULL;
16025 variant_child = sibling_die (variant_child))
16026 {
16027 if (variant_child->tag == DW_TAG_member)
16028 {
16029 handle_struct_member_die (variant_child, type, fi,
16030 template_args, cu);
16031 /* Only handle the one. */
16032 break;
16033 }
16034 }
16035
16036 /* We don't handle this but we might as well report it if we see
16037 it. */
16038 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
16039 complaint (_("DW_AT_discr_list is not supported yet"
16040 " - DIE at %s [in module %s]"),
16041 sect_offset_str (child_die->sect_off),
16042 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16043
16044 /* The first field was just added, so we can stash the
16045 discriminant there. */
16046 gdb_assert (!fi->fields.empty ());
16047 if (discr == NULL)
16048 fi->fields.back ().variant.default_branch = true;
16049 else
16050 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
16051 }
16052 }
16053
16054 /* Finish creating a structure or union type, including filling in
16055 its members and creating a symbol for it. */
16056
16057 static void
16058 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
16059 {
16060 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16061 struct die_info *child_die;
16062 struct type *type;
16063
16064 type = get_die_type (die, cu);
16065 if (type == NULL)
16066 type = read_structure_type (die, cu);
16067
16068 /* When reading a DW_TAG_variant_part, we need to notice when we
16069 read the discriminant member, so we can record it later in the
16070 discriminant_info. */
16071 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
16072 sect_offset discr_offset {};
16073 bool has_template_parameters = false;
16074
16075 if (is_variant_part)
16076 {
16077 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
16078 if (discr == NULL)
16079 {
16080 /* Maybe it's a univariant form, an extension we support.
16081 In this case arrange not to check the offset. */
16082 is_variant_part = false;
16083 }
16084 else if (attr_form_is_ref (discr))
16085 {
16086 struct dwarf2_cu *target_cu = cu;
16087 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
16088
16089 discr_offset = target_die->sect_off;
16090 }
16091 else
16092 {
16093 complaint (_("DW_AT_discr does not have DIE reference form"
16094 " - DIE at %s [in module %s]"),
16095 sect_offset_str (die->sect_off),
16096 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16097 is_variant_part = false;
16098 }
16099 }
16100
16101 if (die->child != NULL && ! die_is_declaration (die, cu))
16102 {
16103 struct field_info fi;
16104 std::vector<struct symbol *> template_args;
16105
16106 child_die = die->child;
16107
16108 while (child_die && child_die->tag)
16109 {
16110 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
16111
16112 if (is_variant_part && discr_offset == child_die->sect_off)
16113 fi.fields.back ().variant.is_discriminant = true;
16114
16115 child_die = sibling_die (child_die);
16116 }
16117
16118 /* Attach template arguments to type. */
16119 if (!template_args.empty ())
16120 {
16121 has_template_parameters = true;
16122 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16123 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
16124 TYPE_TEMPLATE_ARGUMENTS (type)
16125 = XOBNEWVEC (&objfile->objfile_obstack,
16126 struct symbol *,
16127 TYPE_N_TEMPLATE_ARGUMENTS (type));
16128 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
16129 template_args.data (),
16130 (TYPE_N_TEMPLATE_ARGUMENTS (type)
16131 * sizeof (struct symbol *)));
16132 }
16133
16134 /* Attach fields and member functions to the type. */
16135 if (fi.nfields)
16136 dwarf2_attach_fields_to_type (&fi, type, cu);
16137 if (!fi.fnfieldlists.empty ())
16138 {
16139 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
16140
16141 /* Get the type which refers to the base class (possibly this
16142 class itself) which contains the vtable pointer for the current
16143 class from the DW_AT_containing_type attribute. This use of
16144 DW_AT_containing_type is a GNU extension. */
16145
16146 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
16147 {
16148 struct type *t = die_containing_type (die, cu);
16149
16150 set_type_vptr_basetype (type, t);
16151 if (type == t)
16152 {
16153 int i;
16154
16155 /* Our own class provides vtbl ptr. */
16156 for (i = TYPE_NFIELDS (t) - 1;
16157 i >= TYPE_N_BASECLASSES (t);
16158 --i)
16159 {
16160 const char *fieldname = TYPE_FIELD_NAME (t, i);
16161
16162 if (is_vtable_name (fieldname, cu))
16163 {
16164 set_type_vptr_fieldno (type, i);
16165 break;
16166 }
16167 }
16168
16169 /* Complain if virtual function table field not found. */
16170 if (i < TYPE_N_BASECLASSES (t))
16171 complaint (_("virtual function table pointer "
16172 "not found when defining class '%s'"),
16173 TYPE_NAME (type) ? TYPE_NAME (type) : "");
16174 }
16175 else
16176 {
16177 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
16178 }
16179 }
16180 else if (cu->producer
16181 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
16182 {
16183 /* The IBM XLC compiler does not provide direct indication
16184 of the containing type, but the vtable pointer is
16185 always named __vfp. */
16186
16187 int i;
16188
16189 for (i = TYPE_NFIELDS (type) - 1;
16190 i >= TYPE_N_BASECLASSES (type);
16191 --i)
16192 {
16193 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16194 {
16195 set_type_vptr_fieldno (type, i);
16196 set_type_vptr_basetype (type, type);
16197 break;
16198 }
16199 }
16200 }
16201 }
16202
16203 /* Copy fi.typedef_field_list linked list elements content into the
16204 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
16205 if (!fi.typedef_field_list.empty ())
16206 {
16207 int count = fi.typedef_field_list.size ();
16208
16209 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16210 TYPE_TYPEDEF_FIELD_ARRAY (type)
16211 = ((struct decl_field *)
16212 TYPE_ALLOC (type,
16213 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
16214 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
16215
16216 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
16217 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
16218 }
16219
16220 /* Copy fi.nested_types_list linked list elements content into the
16221 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
16222 if (!fi.nested_types_list.empty () && cu->language != language_ada)
16223 {
16224 int count = fi.nested_types_list.size ();
16225
16226 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16227 TYPE_NESTED_TYPES_ARRAY (type)
16228 = ((struct decl_field *)
16229 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
16230 TYPE_NESTED_TYPES_COUNT (type) = count;
16231
16232 for (int i = 0; i < fi.nested_types_list.size (); ++i)
16233 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
16234 }
16235 }
16236
16237 quirk_gcc_member_function_pointer (type, objfile);
16238 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
16239 cu->rust_unions.push_back (type);
16240
16241 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16242 snapshots) has been known to create a die giving a declaration
16243 for a class that has, as a child, a die giving a definition for a
16244 nested class. So we have to process our children even if the
16245 current die is a declaration. Normally, of course, a declaration
16246 won't have any children at all. */
16247
16248 child_die = die->child;
16249
16250 while (child_die != NULL && child_die->tag)
16251 {
16252 if (child_die->tag == DW_TAG_member
16253 || child_die->tag == DW_TAG_variable
16254 || child_die->tag == DW_TAG_inheritance
16255 || child_die->tag == DW_TAG_template_value_param
16256 || child_die->tag == DW_TAG_template_type_param)
16257 {
16258 /* Do nothing. */
16259 }
16260 else
16261 process_die (child_die, cu);
16262
16263 child_die = sibling_die (child_die);
16264 }
16265
16266 /* Do not consider external references. According to the DWARF standard,
16267 these DIEs are identified by the fact that they have no byte_size
16268 attribute, and a declaration attribute. */
16269 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16270 || !die_is_declaration (die, cu))
16271 {
16272 struct symbol *sym = new_symbol (die, type, cu);
16273
16274 if (has_template_parameters)
16275 {
16276 struct symtab *symtab;
16277 if (sym != nullptr)
16278 symtab = symbol_symtab (sym);
16279 else if (cu->line_header != nullptr)
16280 {
16281 /* Any related symtab will do. */
16282 symtab
16283 = cu->line_header->file_names ()[0].symtab;
16284 }
16285 else
16286 {
16287 symtab = nullptr;
16288 complaint (_("could not find suitable "
16289 "symtab for template parameter"
16290 " - DIE at %s [in module %s]"),
16291 sect_offset_str (die->sect_off),
16292 objfile_name (objfile));
16293 }
16294
16295 if (symtab != nullptr)
16296 {
16297 /* Make sure that the symtab is set on the new symbols.
16298 Even though they don't appear in this symtab directly,
16299 other parts of gdb assume that symbols do, and this is
16300 reasonably true. */
16301 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
16302 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
16303 }
16304 }
16305 }
16306 }
16307
16308 /* Assuming DIE is an enumeration type, and TYPE is its associated type,
16309 update TYPE using some information only available in DIE's children. */
16310
16311 static void
16312 update_enumeration_type_from_children (struct die_info *die,
16313 struct type *type,
16314 struct dwarf2_cu *cu)
16315 {
16316 struct die_info *child_die;
16317 int unsigned_enum = 1;
16318 int flag_enum = 1;
16319 ULONGEST mask = 0;
16320
16321 auto_obstack obstack;
16322
16323 for (child_die = die->child;
16324 child_die != NULL && child_die->tag;
16325 child_die = sibling_die (child_die))
16326 {
16327 struct attribute *attr;
16328 LONGEST value;
16329 const gdb_byte *bytes;
16330 struct dwarf2_locexpr_baton *baton;
16331 const char *name;
16332
16333 if (child_die->tag != DW_TAG_enumerator)
16334 continue;
16335
16336 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16337 if (attr == NULL)
16338 continue;
16339
16340 name = dwarf2_name (child_die, cu);
16341 if (name == NULL)
16342 name = "<anonymous enumerator>";
16343
16344 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16345 &value, &bytes, &baton);
16346 if (value < 0)
16347 {
16348 unsigned_enum = 0;
16349 flag_enum = 0;
16350 }
16351 else if ((mask & value) != 0)
16352 flag_enum = 0;
16353 else
16354 mask |= value;
16355
16356 /* If we already know that the enum type is neither unsigned, nor
16357 a flag type, no need to look at the rest of the enumerates. */
16358 if (!unsigned_enum && !flag_enum)
16359 break;
16360 }
16361
16362 if (unsigned_enum)
16363 TYPE_UNSIGNED (type) = 1;
16364 if (flag_enum)
16365 TYPE_FLAG_ENUM (type) = 1;
16366 }
16367
16368 /* Given a DW_AT_enumeration_type die, set its type. We do not
16369 complete the type's fields yet, or create any symbols. */
16370
16371 static struct type *
16372 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
16373 {
16374 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16375 struct type *type;
16376 struct attribute *attr;
16377 const char *name;
16378
16379 /* If the definition of this type lives in .debug_types, read that type.
16380 Don't follow DW_AT_specification though, that will take us back up
16381 the chain and we want to go down. */
16382 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
16383 if (attr != nullptr)
16384 {
16385 type = get_DW_AT_signature_type (die, attr, cu);
16386
16387 /* The type's CU may not be the same as CU.
16388 Ensure TYPE is recorded with CU in die_type_hash. */
16389 return set_die_type (die, type, cu);
16390 }
16391
16392 type = alloc_type (objfile);
16393
16394 TYPE_CODE (type) = TYPE_CODE_ENUM;
16395 name = dwarf2_full_name (NULL, die, cu);
16396 if (name != NULL)
16397 TYPE_NAME (type) = name;
16398
16399 attr = dwarf2_attr (die, DW_AT_type, cu);
16400 if (attr != NULL)
16401 {
16402 struct type *underlying_type = die_type (die, cu);
16403
16404 TYPE_TARGET_TYPE (type) = underlying_type;
16405 }
16406
16407 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16408 if (attr != nullptr)
16409 {
16410 TYPE_LENGTH (type) = DW_UNSND (attr);
16411 }
16412 else
16413 {
16414 TYPE_LENGTH (type) = 0;
16415 }
16416
16417 maybe_set_alignment (cu, die, type);
16418
16419 /* The enumeration DIE can be incomplete. In Ada, any type can be
16420 declared as private in the package spec, and then defined only
16421 inside the package body. Such types are known as Taft Amendment
16422 Types. When another package uses such a type, an incomplete DIE
16423 may be generated by the compiler. */
16424 if (die_is_declaration (die, cu))
16425 TYPE_STUB (type) = 1;
16426
16427 /* Finish the creation of this type by using the enum's children.
16428 We must call this even when the underlying type has been provided
16429 so that we can determine if we're looking at a "flag" enum. */
16430 update_enumeration_type_from_children (die, type, cu);
16431
16432 /* If this type has an underlying type that is not a stub, then we
16433 may use its attributes. We always use the "unsigned" attribute
16434 in this situation, because ordinarily we guess whether the type
16435 is unsigned -- but the guess can be wrong and the underlying type
16436 can tell us the reality. However, we defer to a local size
16437 attribute if one exists, because this lets the compiler override
16438 the underlying type if needed. */
16439 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16440 {
16441 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
16442 if (TYPE_LENGTH (type) == 0)
16443 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
16444 if (TYPE_RAW_ALIGN (type) == 0
16445 && TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)) != 0)
16446 set_type_align (type, TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)));
16447 }
16448
16449 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16450
16451 return set_die_type (die, type, cu);
16452 }
16453
16454 /* Given a pointer to a die which begins an enumeration, process all
16455 the dies that define the members of the enumeration, and create the
16456 symbol for the enumeration type.
16457
16458 NOTE: We reverse the order of the element list. */
16459
16460 static void
16461 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16462 {
16463 struct type *this_type;
16464
16465 this_type = get_die_type (die, cu);
16466 if (this_type == NULL)
16467 this_type = read_enumeration_type (die, cu);
16468
16469 if (die->child != NULL)
16470 {
16471 struct die_info *child_die;
16472 struct symbol *sym;
16473 std::vector<struct field> fields;
16474 const char *name;
16475
16476 child_die = die->child;
16477 while (child_die && child_die->tag)
16478 {
16479 if (child_die->tag != DW_TAG_enumerator)
16480 {
16481 process_die (child_die, cu);
16482 }
16483 else
16484 {
16485 name = dwarf2_name (child_die, cu);
16486 if (name)
16487 {
16488 sym = new_symbol (child_die, this_type, cu);
16489
16490 fields.emplace_back ();
16491 struct field &field = fields.back ();
16492
16493 FIELD_NAME (field) = sym->linkage_name ();
16494 FIELD_TYPE (field) = NULL;
16495 SET_FIELD_ENUMVAL (field, SYMBOL_VALUE (sym));
16496 FIELD_BITSIZE (field) = 0;
16497 }
16498 }
16499
16500 child_die = sibling_die (child_die);
16501 }
16502
16503 if (!fields.empty ())
16504 {
16505 TYPE_NFIELDS (this_type) = fields.size ();
16506 TYPE_FIELDS (this_type) = (struct field *)
16507 TYPE_ALLOC (this_type, sizeof (struct field) * fields.size ());
16508 memcpy (TYPE_FIELDS (this_type), fields.data (),
16509 sizeof (struct field) * fields.size ());
16510 }
16511 }
16512
16513 /* If we are reading an enum from a .debug_types unit, and the enum
16514 is a declaration, and the enum is not the signatured type in the
16515 unit, then we do not want to add a symbol for it. Adding a
16516 symbol would in some cases obscure the true definition of the
16517 enum, giving users an incomplete type when the definition is
16518 actually available. Note that we do not want to do this for all
16519 enums which are just declarations, because C++0x allows forward
16520 enum declarations. */
16521 if (cu->per_cu->is_debug_types
16522 && die_is_declaration (die, cu))
16523 {
16524 struct signatured_type *sig_type;
16525
16526 sig_type = (struct signatured_type *) cu->per_cu;
16527 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16528 if (sig_type->type_offset_in_section != die->sect_off)
16529 return;
16530 }
16531
16532 new_symbol (die, this_type, cu);
16533 }
16534
16535 /* Extract all information from a DW_TAG_array_type DIE and put it in
16536 the DIE's type field. For now, this only handles one dimensional
16537 arrays. */
16538
16539 static struct type *
16540 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
16541 {
16542 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16543 struct die_info *child_die;
16544 struct type *type;
16545 struct type *element_type, *range_type, *index_type;
16546 struct attribute *attr;
16547 const char *name;
16548 struct dynamic_prop *byte_stride_prop = NULL;
16549 unsigned int bit_stride = 0;
16550
16551 element_type = die_type (die, cu);
16552
16553 /* The die_type call above may have already set the type for this DIE. */
16554 type = get_die_type (die, cu);
16555 if (type)
16556 return type;
16557
16558 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16559 if (attr != NULL)
16560 {
16561 int stride_ok;
16562 struct type *prop_type
16563 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
16564
16565 byte_stride_prop
16566 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16567 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16568 prop_type);
16569 if (!stride_ok)
16570 {
16571 complaint (_("unable to read array DW_AT_byte_stride "
16572 " - DIE at %s [in module %s]"),
16573 sect_offset_str (die->sect_off),
16574 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16575 /* Ignore this attribute. We will likely not be able to print
16576 arrays of this type correctly, but there is little we can do
16577 to help if we cannot read the attribute's value. */
16578 byte_stride_prop = NULL;
16579 }
16580 }
16581
16582 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16583 if (attr != NULL)
16584 bit_stride = DW_UNSND (attr);
16585
16586 /* Irix 6.2 native cc creates array types without children for
16587 arrays with unspecified length. */
16588 if (die->child == NULL)
16589 {
16590 index_type = objfile_type (objfile)->builtin_int;
16591 range_type = create_static_range_type (NULL, index_type, 0, -1);
16592 type = create_array_type_with_stride (NULL, element_type, range_type,
16593 byte_stride_prop, bit_stride);
16594 return set_die_type (die, type, cu);
16595 }
16596
16597 std::vector<struct type *> range_types;
16598 child_die = die->child;
16599 while (child_die && child_die->tag)
16600 {
16601 if (child_die->tag == DW_TAG_subrange_type)
16602 {
16603 struct type *child_type = read_type_die (child_die, cu);
16604
16605 if (child_type != NULL)
16606 {
16607 /* The range type was succesfully read. Save it for the
16608 array type creation. */
16609 range_types.push_back (child_type);
16610 }
16611 }
16612 child_die = sibling_die (child_die);
16613 }
16614
16615 /* Dwarf2 dimensions are output from left to right, create the
16616 necessary array types in backwards order. */
16617
16618 type = element_type;
16619
16620 if (read_array_order (die, cu) == DW_ORD_col_major)
16621 {
16622 int i = 0;
16623
16624 while (i < range_types.size ())
16625 type = create_array_type_with_stride (NULL, type, range_types[i++],
16626 byte_stride_prop, bit_stride);
16627 }
16628 else
16629 {
16630 size_t ndim = range_types.size ();
16631 while (ndim-- > 0)
16632 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16633 byte_stride_prop, bit_stride);
16634 }
16635
16636 /* Understand Dwarf2 support for vector types (like they occur on
16637 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16638 array type. This is not part of the Dwarf2/3 standard yet, but a
16639 custom vendor extension. The main difference between a regular
16640 array and the vector variant is that vectors are passed by value
16641 to functions. */
16642 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
16643 if (attr != nullptr)
16644 make_vector_type (type);
16645
16646 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16647 implementation may choose to implement triple vectors using this
16648 attribute. */
16649 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16650 if (attr != nullptr)
16651 {
16652 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16653 TYPE_LENGTH (type) = DW_UNSND (attr);
16654 else
16655 complaint (_("DW_AT_byte_size for array type smaller "
16656 "than the total size of elements"));
16657 }
16658
16659 name = dwarf2_name (die, cu);
16660 if (name)
16661 TYPE_NAME (type) = name;
16662
16663 maybe_set_alignment (cu, die, type);
16664
16665 /* Install the type in the die. */
16666 set_die_type (die, type, cu);
16667
16668 /* set_die_type should be already done. */
16669 set_descriptive_type (type, die, cu);
16670
16671 return type;
16672 }
16673
16674 static enum dwarf_array_dim_ordering
16675 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
16676 {
16677 struct attribute *attr;
16678
16679 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16680
16681 if (attr != nullptr)
16682 return (enum dwarf_array_dim_ordering) DW_SND (attr);
16683
16684 /* GNU F77 is a special case, as at 08/2004 array type info is the
16685 opposite order to the dwarf2 specification, but data is still
16686 laid out as per normal fortran.
16687
16688 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16689 version checking. */
16690
16691 if (cu->language == language_fortran
16692 && cu->producer && strstr (cu->producer, "GNU F77"))
16693 {
16694 return DW_ORD_row_major;
16695 }
16696
16697 switch (cu->language_defn->la_array_ordering)
16698 {
16699 case array_column_major:
16700 return DW_ORD_col_major;
16701 case array_row_major:
16702 default:
16703 return DW_ORD_row_major;
16704 };
16705 }
16706
16707 /* Extract all information from a DW_TAG_set_type DIE and put it in
16708 the DIE's type field. */
16709
16710 static struct type *
16711 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16712 {
16713 struct type *domain_type, *set_type;
16714 struct attribute *attr;
16715
16716 domain_type = die_type (die, cu);
16717
16718 /* The die_type call above may have already set the type for this DIE. */
16719 set_type = get_die_type (die, cu);
16720 if (set_type)
16721 return set_type;
16722
16723 set_type = create_set_type (NULL, domain_type);
16724
16725 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16726 if (attr != nullptr)
16727 TYPE_LENGTH (set_type) = DW_UNSND (attr);
16728
16729 maybe_set_alignment (cu, die, set_type);
16730
16731 return set_die_type (die, set_type, cu);
16732 }
16733
16734 /* A helper for read_common_block that creates a locexpr baton.
16735 SYM is the symbol which we are marking as computed.
16736 COMMON_DIE is the DIE for the common block.
16737 COMMON_LOC is the location expression attribute for the common
16738 block itself.
16739 MEMBER_LOC is the location expression attribute for the particular
16740 member of the common block that we are processing.
16741 CU is the CU from which the above come. */
16742
16743 static void
16744 mark_common_block_symbol_computed (struct symbol *sym,
16745 struct die_info *common_die,
16746 struct attribute *common_loc,
16747 struct attribute *member_loc,
16748 struct dwarf2_cu *cu)
16749 {
16750 struct dwarf2_per_objfile *dwarf2_per_objfile
16751 = cu->per_cu->dwarf2_per_objfile;
16752 struct objfile *objfile = dwarf2_per_objfile->objfile;
16753 struct dwarf2_locexpr_baton *baton;
16754 gdb_byte *ptr;
16755 unsigned int cu_off;
16756 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
16757 LONGEST offset = 0;
16758
16759 gdb_assert (common_loc && member_loc);
16760 gdb_assert (attr_form_is_block (common_loc));
16761 gdb_assert (attr_form_is_block (member_loc)
16762 || attr_form_is_constant (member_loc));
16763
16764 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
16765 baton->per_cu = cu->per_cu;
16766 gdb_assert (baton->per_cu);
16767
16768 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16769
16770 if (attr_form_is_constant (member_loc))
16771 {
16772 offset = dwarf2_get_attr_constant_value (member_loc, 0);
16773 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16774 }
16775 else
16776 baton->size += DW_BLOCK (member_loc)->size;
16777
16778 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
16779 baton->data = ptr;
16780
16781 *ptr++ = DW_OP_call4;
16782 cu_off = common_die->sect_off - cu->per_cu->sect_off;
16783 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16784 ptr += 4;
16785
16786 if (attr_form_is_constant (member_loc))
16787 {
16788 *ptr++ = DW_OP_addr;
16789 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16790 ptr += cu->header.addr_size;
16791 }
16792 else
16793 {
16794 /* We have to copy the data here, because DW_OP_call4 will only
16795 use a DW_AT_location attribute. */
16796 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16797 ptr += DW_BLOCK (member_loc)->size;
16798 }
16799
16800 *ptr++ = DW_OP_plus;
16801 gdb_assert (ptr - baton->data == baton->size);
16802
16803 SYMBOL_LOCATION_BATON (sym) = baton;
16804 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
16805 }
16806
16807 /* Create appropriate locally-scoped variables for all the
16808 DW_TAG_common_block entries. Also create a struct common_block
16809 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16810 is used to separate the common blocks name namespace from regular
16811 variable names. */
16812
16813 static void
16814 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
16815 {
16816 struct attribute *attr;
16817
16818 attr = dwarf2_attr (die, DW_AT_location, cu);
16819 if (attr != nullptr)
16820 {
16821 /* Support the .debug_loc offsets. */
16822 if (attr_form_is_block (attr))
16823 {
16824 /* Ok. */
16825 }
16826 else if (attr_form_is_section_offset (attr))
16827 {
16828 dwarf2_complex_location_expr_complaint ();
16829 attr = NULL;
16830 }
16831 else
16832 {
16833 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16834 "common block member");
16835 attr = NULL;
16836 }
16837 }
16838
16839 if (die->child != NULL)
16840 {
16841 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16842 struct die_info *child_die;
16843 size_t n_entries = 0, size;
16844 struct common_block *common_block;
16845 struct symbol *sym;
16846
16847 for (child_die = die->child;
16848 child_die && child_die->tag;
16849 child_die = sibling_die (child_die))
16850 ++n_entries;
16851
16852 size = (sizeof (struct common_block)
16853 + (n_entries - 1) * sizeof (struct symbol *));
16854 common_block
16855 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16856 size);
16857 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16858 common_block->n_entries = 0;
16859
16860 for (child_die = die->child;
16861 child_die && child_die->tag;
16862 child_die = sibling_die (child_die))
16863 {
16864 /* Create the symbol in the DW_TAG_common_block block in the current
16865 symbol scope. */
16866 sym = new_symbol (child_die, NULL, cu);
16867 if (sym != NULL)
16868 {
16869 struct attribute *member_loc;
16870
16871 common_block->contents[common_block->n_entries++] = sym;
16872
16873 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16874 cu);
16875 if (member_loc)
16876 {
16877 /* GDB has handled this for a long time, but it is
16878 not specified by DWARF. It seems to have been
16879 emitted by gfortran at least as recently as:
16880 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16881 complaint (_("Variable in common block has "
16882 "DW_AT_data_member_location "
16883 "- DIE at %s [in module %s]"),
16884 sect_offset_str (child_die->sect_off),
16885 objfile_name (objfile));
16886
16887 if (attr_form_is_section_offset (member_loc))
16888 dwarf2_complex_location_expr_complaint ();
16889 else if (attr_form_is_constant (member_loc)
16890 || attr_form_is_block (member_loc))
16891 {
16892 if (attr != nullptr)
16893 mark_common_block_symbol_computed (sym, die, attr,
16894 member_loc, cu);
16895 }
16896 else
16897 dwarf2_complex_location_expr_complaint ();
16898 }
16899 }
16900 }
16901
16902 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16903 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
16904 }
16905 }
16906
16907 /* Create a type for a C++ namespace. */
16908
16909 static struct type *
16910 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16911 {
16912 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16913 const char *previous_prefix, *name;
16914 int is_anonymous;
16915 struct type *type;
16916
16917 /* For extensions, reuse the type of the original namespace. */
16918 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16919 {
16920 struct die_info *ext_die;
16921 struct dwarf2_cu *ext_cu = cu;
16922
16923 ext_die = dwarf2_extension (die, &ext_cu);
16924 type = read_type_die (ext_die, ext_cu);
16925
16926 /* EXT_CU may not be the same as CU.
16927 Ensure TYPE is recorded with CU in die_type_hash. */
16928 return set_die_type (die, type, cu);
16929 }
16930
16931 name = namespace_name (die, &is_anonymous, cu);
16932
16933 /* Now build the name of the current namespace. */
16934
16935 previous_prefix = determine_prefix (die, cu);
16936 if (previous_prefix[0] != '\0')
16937 name = typename_concat (&objfile->objfile_obstack,
16938 previous_prefix, name, 0, cu);
16939
16940 /* Create the type. */
16941 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
16942
16943 return set_die_type (die, type, cu);
16944 }
16945
16946 /* Read a namespace scope. */
16947
16948 static void
16949 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16950 {
16951 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16952 int is_anonymous;
16953
16954 /* Add a symbol associated to this if we haven't seen the namespace
16955 before. Also, add a using directive if it's an anonymous
16956 namespace. */
16957
16958 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
16959 {
16960 struct type *type;
16961
16962 type = read_type_die (die, cu);
16963 new_symbol (die, type, cu);
16964
16965 namespace_name (die, &is_anonymous, cu);
16966 if (is_anonymous)
16967 {
16968 const char *previous_prefix = determine_prefix (die, cu);
16969
16970 std::vector<const char *> excludes;
16971 add_using_directive (using_directives (cu),
16972 previous_prefix, TYPE_NAME (type), NULL,
16973 NULL, excludes, 0, &objfile->objfile_obstack);
16974 }
16975 }
16976
16977 if (die->child != NULL)
16978 {
16979 struct die_info *child_die = die->child;
16980
16981 while (child_die && child_die->tag)
16982 {
16983 process_die (child_die, cu);
16984 child_die = sibling_die (child_die);
16985 }
16986 }
16987 }
16988
16989 /* Read a Fortran module as type. This DIE can be only a declaration used for
16990 imported module. Still we need that type as local Fortran "use ... only"
16991 declaration imports depend on the created type in determine_prefix. */
16992
16993 static struct type *
16994 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16995 {
16996 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16997 const char *module_name;
16998 struct type *type;
16999
17000 module_name = dwarf2_name (die, cu);
17001 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
17002
17003 return set_die_type (die, type, cu);
17004 }
17005
17006 /* Read a Fortran module. */
17007
17008 static void
17009 read_module (struct die_info *die, struct dwarf2_cu *cu)
17010 {
17011 struct die_info *child_die = die->child;
17012 struct type *type;
17013
17014 type = read_type_die (die, cu);
17015 new_symbol (die, type, cu);
17016
17017 while (child_die && child_die->tag)
17018 {
17019 process_die (child_die, cu);
17020 child_die = sibling_die (child_die);
17021 }
17022 }
17023
17024 /* Return the name of the namespace represented by DIE. Set
17025 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
17026 namespace. */
17027
17028 static const char *
17029 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
17030 {
17031 struct die_info *current_die;
17032 const char *name = NULL;
17033
17034 /* Loop through the extensions until we find a name. */
17035
17036 for (current_die = die;
17037 current_die != NULL;
17038 current_die = dwarf2_extension (die, &cu))
17039 {
17040 /* We don't use dwarf2_name here so that we can detect the absence
17041 of a name -> anonymous namespace. */
17042 name = dwarf2_string_attr (die, DW_AT_name, cu);
17043
17044 if (name != NULL)
17045 break;
17046 }
17047
17048 /* Is it an anonymous namespace? */
17049
17050 *is_anonymous = (name == NULL);
17051 if (*is_anonymous)
17052 name = CP_ANONYMOUS_NAMESPACE_STR;
17053
17054 return name;
17055 }
17056
17057 /* Extract all information from a DW_TAG_pointer_type DIE and add to
17058 the user defined type vector. */
17059
17060 static struct type *
17061 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
17062 {
17063 struct gdbarch *gdbarch
17064 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
17065 struct comp_unit_head *cu_header = &cu->header;
17066 struct type *type;
17067 struct attribute *attr_byte_size;
17068 struct attribute *attr_address_class;
17069 int byte_size, addr_class;
17070 struct type *target_type;
17071
17072 target_type = die_type (die, cu);
17073
17074 /* The die_type call above may have already set the type for this DIE. */
17075 type = get_die_type (die, cu);
17076 if (type)
17077 return type;
17078
17079 type = lookup_pointer_type (target_type);
17080
17081 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
17082 if (attr_byte_size)
17083 byte_size = DW_UNSND (attr_byte_size);
17084 else
17085 byte_size = cu_header->addr_size;
17086
17087 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
17088 if (attr_address_class)
17089 addr_class = DW_UNSND (attr_address_class);
17090 else
17091 addr_class = DW_ADDR_none;
17092
17093 ULONGEST alignment = get_alignment (cu, die);
17094
17095 /* If the pointer size, alignment, or address class is different
17096 than the default, create a type variant marked as such and set
17097 the length accordingly. */
17098 if (TYPE_LENGTH (type) != byte_size
17099 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
17100 && alignment != TYPE_RAW_ALIGN (type))
17101 || addr_class != DW_ADDR_none)
17102 {
17103 if (gdbarch_address_class_type_flags_p (gdbarch))
17104 {
17105 int type_flags;
17106
17107 type_flags = gdbarch_address_class_type_flags
17108 (gdbarch, byte_size, addr_class);
17109 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17110 == 0);
17111 type = make_type_with_address_space (type, type_flags);
17112 }
17113 else if (TYPE_LENGTH (type) != byte_size)
17114 {
17115 complaint (_("invalid pointer size %d"), byte_size);
17116 }
17117 else if (TYPE_RAW_ALIGN (type) != alignment)
17118 {
17119 complaint (_("Invalid DW_AT_alignment"
17120 " - DIE at %s [in module %s]"),
17121 sect_offset_str (die->sect_off),
17122 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17123 }
17124 else
17125 {
17126 /* Should we also complain about unhandled address classes? */
17127 }
17128 }
17129
17130 TYPE_LENGTH (type) = byte_size;
17131 set_type_align (type, alignment);
17132 return set_die_type (die, type, cu);
17133 }
17134
17135 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17136 the user defined type vector. */
17137
17138 static struct type *
17139 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
17140 {
17141 struct type *type;
17142 struct type *to_type;
17143 struct type *domain;
17144
17145 to_type = die_type (die, cu);
17146 domain = die_containing_type (die, cu);
17147
17148 /* The calls above may have already set the type for this DIE. */
17149 type = get_die_type (die, cu);
17150 if (type)
17151 return type;
17152
17153 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
17154 type = lookup_methodptr_type (to_type);
17155 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
17156 {
17157 struct type *new_type
17158 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
17159
17160 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
17161 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
17162 TYPE_VARARGS (to_type));
17163 type = lookup_methodptr_type (new_type);
17164 }
17165 else
17166 type = lookup_memberptr_type (to_type, domain);
17167
17168 return set_die_type (die, type, cu);
17169 }
17170
17171 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
17172 the user defined type vector. */
17173
17174 static struct type *
17175 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
17176 enum type_code refcode)
17177 {
17178 struct comp_unit_head *cu_header = &cu->header;
17179 struct type *type, *target_type;
17180 struct attribute *attr;
17181
17182 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17183
17184 target_type = die_type (die, cu);
17185
17186 /* The die_type call above may have already set the type for this DIE. */
17187 type = get_die_type (die, cu);
17188 if (type)
17189 return type;
17190
17191 type = lookup_reference_type (target_type, refcode);
17192 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17193 if (attr != nullptr)
17194 {
17195 TYPE_LENGTH (type) = DW_UNSND (attr);
17196 }
17197 else
17198 {
17199 TYPE_LENGTH (type) = cu_header->addr_size;
17200 }
17201 maybe_set_alignment (cu, die, type);
17202 return set_die_type (die, type, cu);
17203 }
17204
17205 /* Add the given cv-qualifiers to the element type of the array. GCC
17206 outputs DWARF type qualifiers that apply to an array, not the
17207 element type. But GDB relies on the array element type to carry
17208 the cv-qualifiers. This mimics section 6.7.3 of the C99
17209 specification. */
17210
17211 static struct type *
17212 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17213 struct type *base_type, int cnst, int voltl)
17214 {
17215 struct type *el_type, *inner_array;
17216
17217 base_type = copy_type (base_type);
17218 inner_array = base_type;
17219
17220 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
17221 {
17222 TYPE_TARGET_TYPE (inner_array) =
17223 copy_type (TYPE_TARGET_TYPE (inner_array));
17224 inner_array = TYPE_TARGET_TYPE (inner_array);
17225 }
17226
17227 el_type = TYPE_TARGET_TYPE (inner_array);
17228 cnst |= TYPE_CONST (el_type);
17229 voltl |= TYPE_VOLATILE (el_type);
17230 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17231
17232 return set_die_type (die, base_type, cu);
17233 }
17234
17235 static struct type *
17236 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
17237 {
17238 struct type *base_type, *cv_type;
17239
17240 base_type = die_type (die, cu);
17241
17242 /* The die_type call above may have already set the type for this DIE. */
17243 cv_type = get_die_type (die, cu);
17244 if (cv_type)
17245 return cv_type;
17246
17247 /* In case the const qualifier is applied to an array type, the element type
17248 is so qualified, not the array type (section 6.7.3 of C99). */
17249 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17250 return add_array_cv_type (die, cu, base_type, 1, 0);
17251
17252 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17253 return set_die_type (die, cv_type, cu);
17254 }
17255
17256 static struct type *
17257 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
17258 {
17259 struct type *base_type, *cv_type;
17260
17261 base_type = die_type (die, cu);
17262
17263 /* The die_type call above may have already set the type for this DIE. */
17264 cv_type = get_die_type (die, cu);
17265 if (cv_type)
17266 return cv_type;
17267
17268 /* In case the volatile qualifier is applied to an array type, the
17269 element type is so qualified, not the array type (section 6.7.3
17270 of C99). */
17271 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17272 return add_array_cv_type (die, cu, base_type, 0, 1);
17273
17274 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17275 return set_die_type (die, cv_type, cu);
17276 }
17277
17278 /* Handle DW_TAG_restrict_type. */
17279
17280 static struct type *
17281 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17282 {
17283 struct type *base_type, *cv_type;
17284
17285 base_type = die_type (die, cu);
17286
17287 /* The die_type call above may have already set the type for this DIE. */
17288 cv_type = get_die_type (die, cu);
17289 if (cv_type)
17290 return cv_type;
17291
17292 cv_type = make_restrict_type (base_type);
17293 return set_die_type (die, cv_type, cu);
17294 }
17295
17296 /* Handle DW_TAG_atomic_type. */
17297
17298 static struct type *
17299 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17300 {
17301 struct type *base_type, *cv_type;
17302
17303 base_type = die_type (die, cu);
17304
17305 /* The die_type call above may have already set the type for this DIE. */
17306 cv_type = get_die_type (die, cu);
17307 if (cv_type)
17308 return cv_type;
17309
17310 cv_type = make_atomic_type (base_type);
17311 return set_die_type (die, cv_type, cu);
17312 }
17313
17314 /* Extract all information from a DW_TAG_string_type DIE and add to
17315 the user defined type vector. It isn't really a user defined type,
17316 but it behaves like one, with other DIE's using an AT_user_def_type
17317 attribute to reference it. */
17318
17319 static struct type *
17320 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
17321 {
17322 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17323 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17324 struct type *type, *range_type, *index_type, *char_type;
17325 struct attribute *attr;
17326 struct dynamic_prop prop;
17327 bool length_is_constant = true;
17328 LONGEST length;
17329
17330 /* There are a couple of places where bit sizes might be made use of
17331 when parsing a DW_TAG_string_type, however, no producer that we know
17332 of make use of these. Handling bit sizes that are a multiple of the
17333 byte size is easy enough, but what about other bit sizes? Lets deal
17334 with that problem when we have to. Warn about these attributes being
17335 unsupported, then parse the type and ignore them like we always
17336 have. */
17337 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
17338 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
17339 {
17340 static bool warning_printed = false;
17341 if (!warning_printed)
17342 {
17343 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
17344 "currently supported on DW_TAG_string_type."));
17345 warning_printed = true;
17346 }
17347 }
17348
17349 attr = dwarf2_attr (die, DW_AT_string_length, cu);
17350 if (attr != nullptr && !attr_form_is_constant (attr))
17351 {
17352 /* The string length describes the location at which the length of
17353 the string can be found. The size of the length field can be
17354 specified with one of the attributes below. */
17355 struct type *prop_type;
17356 struct attribute *len
17357 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
17358 if (len == nullptr)
17359 len = dwarf2_attr (die, DW_AT_byte_size, cu);
17360 if (len != nullptr && attr_form_is_constant (len))
17361 {
17362 /* Pass 0 as the default as we know this attribute is constant
17363 and the default value will not be returned. */
17364 LONGEST sz = dwarf2_get_attr_constant_value (len, 0);
17365 prop_type = dwarf2_per_cu_int_type (cu->per_cu, sz, true);
17366 }
17367 else
17368 {
17369 /* If the size is not specified then we assume it is the size of
17370 an address on this target. */
17371 prop_type = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, true);
17372 }
17373
17374 /* Convert the attribute into a dynamic property. */
17375 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17376 length = 1;
17377 else
17378 length_is_constant = false;
17379 }
17380 else if (attr != nullptr)
17381 {
17382 /* This DW_AT_string_length just contains the length with no
17383 indirection. There's no need to create a dynamic property in this
17384 case. Pass 0 for the default value as we know it will not be
17385 returned in this case. */
17386 length = dwarf2_get_attr_constant_value (attr, 0);
17387 }
17388 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
17389 {
17390 /* We don't currently support non-constant byte sizes for strings. */
17391 length = dwarf2_get_attr_constant_value (attr, 1);
17392 }
17393 else
17394 {
17395 /* Use 1 as a fallback length if we have nothing else. */
17396 length = 1;
17397 }
17398
17399 index_type = objfile_type (objfile)->builtin_int;
17400 if (length_is_constant)
17401 range_type = create_static_range_type (NULL, index_type, 1, length);
17402 else
17403 {
17404 struct dynamic_prop low_bound;
17405
17406 low_bound.kind = PROP_CONST;
17407 low_bound.data.const_val = 1;
17408 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17409 }
17410 char_type = language_string_char_type (cu->language_defn, gdbarch);
17411 type = create_string_type (NULL, char_type, range_type);
17412
17413 return set_die_type (die, type, cu);
17414 }
17415
17416 /* Assuming that DIE corresponds to a function, returns nonzero
17417 if the function is prototyped. */
17418
17419 static int
17420 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17421 {
17422 struct attribute *attr;
17423
17424 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17425 if (attr && (DW_UNSND (attr) != 0))
17426 return 1;
17427
17428 /* The DWARF standard implies that the DW_AT_prototyped attribute
17429 is only meaningful for C, but the concept also extends to other
17430 languages that allow unprototyped functions (Eg: Objective C).
17431 For all other languages, assume that functions are always
17432 prototyped. */
17433 if (cu->language != language_c
17434 && cu->language != language_objc
17435 && cu->language != language_opencl)
17436 return 1;
17437
17438 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17439 prototyped and unprototyped functions; default to prototyped,
17440 since that is more common in modern code (and RealView warns
17441 about unprototyped functions). */
17442 if (producer_is_realview (cu->producer))
17443 return 1;
17444
17445 return 0;
17446 }
17447
17448 /* Handle DIES due to C code like:
17449
17450 struct foo
17451 {
17452 int (*funcp)(int a, long l);
17453 int b;
17454 };
17455
17456 ('funcp' generates a DW_TAG_subroutine_type DIE). */
17457
17458 static struct type *
17459 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
17460 {
17461 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17462 struct type *type; /* Type that this function returns. */
17463 struct type *ftype; /* Function that returns above type. */
17464 struct attribute *attr;
17465
17466 type = die_type (die, cu);
17467
17468 /* The die_type call above may have already set the type for this DIE. */
17469 ftype = get_die_type (die, cu);
17470 if (ftype)
17471 return ftype;
17472
17473 ftype = lookup_function_type (type);
17474
17475 if (prototyped_function_p (die, cu))
17476 TYPE_PROTOTYPED (ftype) = 1;
17477
17478 /* Store the calling convention in the type if it's available in
17479 the subroutine die. Otherwise set the calling convention to
17480 the default value DW_CC_normal. */
17481 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
17482 if (attr != nullptr
17483 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
17484 TYPE_CALLING_CONVENTION (ftype)
17485 = (enum dwarf_calling_convention) (DW_UNSND (attr));
17486 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17487 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17488 else
17489 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
17490
17491 /* Record whether the function returns normally to its caller or not
17492 if the DWARF producer set that information. */
17493 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17494 if (attr && (DW_UNSND (attr) != 0))
17495 TYPE_NO_RETURN (ftype) = 1;
17496
17497 /* We need to add the subroutine type to the die immediately so
17498 we don't infinitely recurse when dealing with parameters
17499 declared as the same subroutine type. */
17500 set_die_type (die, ftype, cu);
17501
17502 if (die->child != NULL)
17503 {
17504 struct type *void_type = objfile_type (objfile)->builtin_void;
17505 struct die_info *child_die;
17506 int nparams, iparams;
17507
17508 /* Count the number of parameters.
17509 FIXME: GDB currently ignores vararg functions, but knows about
17510 vararg member functions. */
17511 nparams = 0;
17512 child_die = die->child;
17513 while (child_die && child_die->tag)
17514 {
17515 if (child_die->tag == DW_TAG_formal_parameter)
17516 nparams++;
17517 else if (child_die->tag == DW_TAG_unspecified_parameters)
17518 TYPE_VARARGS (ftype) = 1;
17519 child_die = sibling_die (child_die);
17520 }
17521
17522 /* Allocate storage for parameters and fill them in. */
17523 TYPE_NFIELDS (ftype) = nparams;
17524 TYPE_FIELDS (ftype) = (struct field *)
17525 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
17526
17527 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17528 even if we error out during the parameters reading below. */
17529 for (iparams = 0; iparams < nparams; iparams++)
17530 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17531
17532 iparams = 0;
17533 child_die = die->child;
17534 while (child_die && child_die->tag)
17535 {
17536 if (child_die->tag == DW_TAG_formal_parameter)
17537 {
17538 struct type *arg_type;
17539
17540 /* DWARF version 2 has no clean way to discern C++
17541 static and non-static member functions. G++ helps
17542 GDB by marking the first parameter for non-static
17543 member functions (which is the this pointer) as
17544 artificial. We pass this information to
17545 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17546
17547 DWARF version 3 added DW_AT_object_pointer, which GCC
17548 4.5 does not yet generate. */
17549 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
17550 if (attr != nullptr)
17551 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17552 else
17553 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
17554 arg_type = die_type (child_die, cu);
17555
17556 /* RealView does not mark THIS as const, which the testsuite
17557 expects. GCC marks THIS as const in method definitions,
17558 but not in the class specifications (GCC PR 43053). */
17559 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17560 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17561 {
17562 int is_this = 0;
17563 struct dwarf2_cu *arg_cu = cu;
17564 const char *name = dwarf2_name (child_die, cu);
17565
17566 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17567 if (attr != nullptr)
17568 {
17569 /* If the compiler emits this, use it. */
17570 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17571 is_this = 1;
17572 }
17573 else if (name && strcmp (name, "this") == 0)
17574 /* Function definitions will have the argument names. */
17575 is_this = 1;
17576 else if (name == NULL && iparams == 0)
17577 /* Declarations may not have the names, so like
17578 elsewhere in GDB, assume an artificial first
17579 argument is "this". */
17580 is_this = 1;
17581
17582 if (is_this)
17583 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17584 arg_type, 0);
17585 }
17586
17587 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
17588 iparams++;
17589 }
17590 child_die = sibling_die (child_die);
17591 }
17592 }
17593
17594 return ftype;
17595 }
17596
17597 static struct type *
17598 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
17599 {
17600 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17601 const char *name = NULL;
17602 struct type *this_type, *target_type;
17603
17604 name = dwarf2_full_name (NULL, die, cu);
17605 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17606 TYPE_TARGET_STUB (this_type) = 1;
17607 set_die_type (die, this_type, cu);
17608 target_type = die_type (die, cu);
17609 if (target_type != this_type)
17610 TYPE_TARGET_TYPE (this_type) = target_type;
17611 else
17612 {
17613 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17614 spec and cause infinite loops in GDB. */
17615 complaint (_("Self-referential DW_TAG_typedef "
17616 "- DIE at %s [in module %s]"),
17617 sect_offset_str (die->sect_off), objfile_name (objfile));
17618 TYPE_TARGET_TYPE (this_type) = NULL;
17619 }
17620 return this_type;
17621 }
17622
17623 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17624 (which may be different from NAME) to the architecture back-end to allow
17625 it to guess the correct format if necessary. */
17626
17627 static struct type *
17628 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17629 const char *name_hint, enum bfd_endian byte_order)
17630 {
17631 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17632 const struct floatformat **format;
17633 struct type *type;
17634
17635 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17636 if (format)
17637 type = init_float_type (objfile, bits, name, format, byte_order);
17638 else
17639 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17640
17641 return type;
17642 }
17643
17644 /* Allocate an integer type of size BITS and name NAME. */
17645
17646 static struct type *
17647 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17648 int bits, int unsigned_p, const char *name)
17649 {
17650 struct type *type;
17651
17652 /* Versions of Intel's C Compiler generate an integer type called "void"
17653 instead of using DW_TAG_unspecified_type. This has been seen on
17654 at least versions 14, 17, and 18. */
17655 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17656 && strcmp (name, "void") == 0)
17657 type = objfile_type (objfile)->builtin_void;
17658 else
17659 type = init_integer_type (objfile, bits, unsigned_p, name);
17660
17661 return type;
17662 }
17663
17664 /* Initialise and return a floating point type of size BITS suitable for
17665 use as a component of a complex number. The NAME_HINT is passed through
17666 when initialising the floating point type and is the name of the complex
17667 type.
17668
17669 As DWARF doesn't currently provide an explicit name for the components
17670 of a complex number, but it can be helpful to have these components
17671 named, we try to select a suitable name based on the size of the
17672 component. */
17673 static struct type *
17674 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17675 struct objfile *objfile,
17676 int bits, const char *name_hint,
17677 enum bfd_endian byte_order)
17678 {
17679 gdbarch *gdbarch = get_objfile_arch (objfile);
17680 struct type *tt = nullptr;
17681
17682 /* Try to find a suitable floating point builtin type of size BITS.
17683 We're going to use the name of this type as the name for the complex
17684 target type that we are about to create. */
17685 switch (cu->language)
17686 {
17687 case language_fortran:
17688 switch (bits)
17689 {
17690 case 32:
17691 tt = builtin_f_type (gdbarch)->builtin_real;
17692 break;
17693 case 64:
17694 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17695 break;
17696 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17697 case 128:
17698 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17699 break;
17700 }
17701 break;
17702 default:
17703 switch (bits)
17704 {
17705 case 32:
17706 tt = builtin_type (gdbarch)->builtin_float;
17707 break;
17708 case 64:
17709 tt = builtin_type (gdbarch)->builtin_double;
17710 break;
17711 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17712 case 128:
17713 tt = builtin_type (gdbarch)->builtin_long_double;
17714 break;
17715 }
17716 break;
17717 }
17718
17719 /* If the type we found doesn't match the size we were looking for, then
17720 pretend we didn't find a type at all, the complex target type we
17721 create will then be nameless. */
17722 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
17723 tt = nullptr;
17724
17725 const char *name = (tt == nullptr) ? nullptr : TYPE_NAME (tt);
17726 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
17727 }
17728
17729 /* Find a representation of a given base type and install
17730 it in the TYPE field of the die. */
17731
17732 static struct type *
17733 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
17734 {
17735 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17736 struct type *type;
17737 struct attribute *attr;
17738 int encoding = 0, bits = 0;
17739 const char *name;
17740 gdbarch *arch;
17741
17742 attr = dwarf2_attr (die, DW_AT_encoding, cu);
17743 if (attr != nullptr)
17744 encoding = DW_UNSND (attr);
17745 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17746 if (attr != nullptr)
17747 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
17748 name = dwarf2_name (die, cu);
17749 if (!name)
17750 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
17751
17752 arch = get_objfile_arch (objfile);
17753 enum bfd_endian byte_order = gdbarch_byte_order (arch);
17754
17755 attr = dwarf2_attr (die, DW_AT_endianity, cu);
17756 if (attr)
17757 {
17758 int endianity = DW_UNSND (attr);
17759
17760 switch (endianity)
17761 {
17762 case DW_END_big:
17763 byte_order = BFD_ENDIAN_BIG;
17764 break;
17765 case DW_END_little:
17766 byte_order = BFD_ENDIAN_LITTLE;
17767 break;
17768 default:
17769 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
17770 break;
17771 }
17772 }
17773
17774 switch (encoding)
17775 {
17776 case DW_ATE_address:
17777 /* Turn DW_ATE_address into a void * pointer. */
17778 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
17779 type = init_pointer_type (objfile, bits, name, type);
17780 break;
17781 case DW_ATE_boolean:
17782 type = init_boolean_type (objfile, bits, 1, name);
17783 break;
17784 case DW_ATE_complex_float:
17785 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
17786 byte_order);
17787 type = init_complex_type (objfile, name, type);
17788 break;
17789 case DW_ATE_decimal_float:
17790 type = init_decfloat_type (objfile, bits, name);
17791 break;
17792 case DW_ATE_float:
17793 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
17794 break;
17795 case DW_ATE_signed:
17796 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17797 break;
17798 case DW_ATE_unsigned:
17799 if (cu->language == language_fortran
17800 && name
17801 && startswith (name, "character("))
17802 type = init_character_type (objfile, bits, 1, name);
17803 else
17804 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17805 break;
17806 case DW_ATE_signed_char:
17807 if (cu->language == language_ada || cu->language == language_m2
17808 || cu->language == language_pascal
17809 || cu->language == language_fortran)
17810 type = init_character_type (objfile, bits, 0, name);
17811 else
17812 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17813 break;
17814 case DW_ATE_unsigned_char:
17815 if (cu->language == language_ada || cu->language == language_m2
17816 || cu->language == language_pascal
17817 || cu->language == language_fortran
17818 || cu->language == language_rust)
17819 type = init_character_type (objfile, bits, 1, name);
17820 else
17821 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17822 break;
17823 case DW_ATE_UTF:
17824 {
17825 if (bits == 16)
17826 type = builtin_type (arch)->builtin_char16;
17827 else if (bits == 32)
17828 type = builtin_type (arch)->builtin_char32;
17829 else
17830 {
17831 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
17832 bits);
17833 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17834 }
17835 return set_die_type (die, type, cu);
17836 }
17837 break;
17838
17839 default:
17840 complaint (_("unsupported DW_AT_encoding: '%s'"),
17841 dwarf_type_encoding_name (encoding));
17842 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17843 break;
17844 }
17845
17846 if (name && strcmp (name, "char") == 0)
17847 TYPE_NOSIGN (type) = 1;
17848
17849 maybe_set_alignment (cu, die, type);
17850
17851 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
17852
17853 return set_die_type (die, type, cu);
17854 }
17855
17856 /* Parse dwarf attribute if it's a block, reference or constant and put the
17857 resulting value of the attribute into struct bound_prop.
17858 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17859
17860 static int
17861 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17862 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17863 struct type *default_type)
17864 {
17865 struct dwarf2_property_baton *baton;
17866 struct obstack *obstack
17867 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
17868
17869 gdb_assert (default_type != NULL);
17870
17871 if (attr == NULL || prop == NULL)
17872 return 0;
17873
17874 if (attr_form_is_block (attr))
17875 {
17876 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17877 baton->property_type = default_type;
17878 baton->locexpr.per_cu = cu->per_cu;
17879 baton->locexpr.size = DW_BLOCK (attr)->size;
17880 baton->locexpr.data = DW_BLOCK (attr)->data;
17881 switch (attr->name)
17882 {
17883 case DW_AT_string_length:
17884 baton->locexpr.is_reference = true;
17885 break;
17886 default:
17887 baton->locexpr.is_reference = false;
17888 break;
17889 }
17890 prop->data.baton = baton;
17891 prop->kind = PROP_LOCEXPR;
17892 gdb_assert (prop->data.baton != NULL);
17893 }
17894 else if (attr_form_is_ref (attr))
17895 {
17896 struct dwarf2_cu *target_cu = cu;
17897 struct die_info *target_die;
17898 struct attribute *target_attr;
17899
17900 target_die = follow_die_ref (die, attr, &target_cu);
17901 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17902 if (target_attr == NULL)
17903 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17904 target_cu);
17905 if (target_attr == NULL)
17906 return 0;
17907
17908 switch (target_attr->name)
17909 {
17910 case DW_AT_location:
17911 if (attr_form_is_section_offset (target_attr))
17912 {
17913 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17914 baton->property_type = die_type (target_die, target_cu);
17915 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17916 prop->data.baton = baton;
17917 prop->kind = PROP_LOCLIST;
17918 gdb_assert (prop->data.baton != NULL);
17919 }
17920 else if (attr_form_is_block (target_attr))
17921 {
17922 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17923 baton->property_type = die_type (target_die, target_cu);
17924 baton->locexpr.per_cu = cu->per_cu;
17925 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17926 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17927 baton->locexpr.is_reference = true;
17928 prop->data.baton = baton;
17929 prop->kind = PROP_LOCEXPR;
17930 gdb_assert (prop->data.baton != NULL);
17931 }
17932 else
17933 {
17934 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17935 "dynamic property");
17936 return 0;
17937 }
17938 break;
17939 case DW_AT_data_member_location:
17940 {
17941 LONGEST offset;
17942
17943 if (!handle_data_member_location (target_die, target_cu,
17944 &offset))
17945 return 0;
17946
17947 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17948 baton->property_type = read_type_die (target_die->parent,
17949 target_cu);
17950 baton->offset_info.offset = offset;
17951 baton->offset_info.type = die_type (target_die, target_cu);
17952 prop->data.baton = baton;
17953 prop->kind = PROP_ADDR_OFFSET;
17954 break;
17955 }
17956 }
17957 }
17958 else if (attr_form_is_constant (attr))
17959 {
17960 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
17961 prop->kind = PROP_CONST;
17962 }
17963 else
17964 {
17965 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17966 dwarf2_name (die, cu));
17967 return 0;
17968 }
17969
17970 return 1;
17971 }
17972
17973 /* Find an integer type SIZE_IN_BYTES bytes in size and return it.
17974 UNSIGNED_P controls if the integer is unsigned or not. */
17975
17976 static struct type *
17977 dwarf2_per_cu_int_type (struct dwarf2_per_cu_data *per_cu,
17978 int size_in_bytes, bool unsigned_p)
17979 {
17980 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
17981 struct type *int_type;
17982
17983 /* Helper macro to examine the various builtin types. */
17984 #define TRY_TYPE(F) \
17985 int_type = (unsigned_p \
17986 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17987 : objfile_type (objfile)->builtin_ ## F); \
17988 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
17989 return int_type
17990
17991 TRY_TYPE (char);
17992 TRY_TYPE (short);
17993 TRY_TYPE (int);
17994 TRY_TYPE (long);
17995 TRY_TYPE (long_long);
17996
17997 #undef TRY_TYPE
17998
17999 gdb_assert_not_reached ("unable to find suitable integer type");
18000 }
18001
18002 /* Find an integer type the same size as the address size given in the
18003 compilation unit header for PER_CU. UNSIGNED_P controls if the integer
18004 is unsigned or not. */
18005
18006 static struct type *
18007 dwarf2_per_cu_addr_sized_int_type (struct dwarf2_per_cu_data *per_cu,
18008 bool unsigned_p)
18009 {
18010 int addr_size = dwarf2_per_cu_addr_size (per_cu);
18011 return dwarf2_per_cu_int_type (per_cu, addr_size, unsigned_p);
18012 }
18013
18014 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
18015 present (which is valid) then compute the default type based on the
18016 compilation units address size. */
18017
18018 static struct type *
18019 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
18020 {
18021 struct type *index_type = die_type (die, cu);
18022
18023 /* Dwarf-2 specifications explicitly allows to create subrange types
18024 without specifying a base type.
18025 In that case, the base type must be set to the type of
18026 the lower bound, upper bound or count, in that order, if any of these
18027 three attributes references an object that has a type.
18028 If no base type is found, the Dwarf-2 specifications say that
18029 a signed integer type of size equal to the size of an address should
18030 be used.
18031 For the following C code: `extern char gdb_int [];'
18032 GCC produces an empty range DIE.
18033 FIXME: muller/2010-05-28: Possible references to object for low bound,
18034 high bound or count are not yet handled by this code. */
18035 if (TYPE_CODE (index_type) == TYPE_CODE_VOID)
18036 index_type = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
18037
18038 return index_type;
18039 }
18040
18041 /* Read the given DW_AT_subrange DIE. */
18042
18043 static struct type *
18044 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
18045 {
18046 struct type *base_type, *orig_base_type;
18047 struct type *range_type;
18048 struct attribute *attr;
18049 struct dynamic_prop low, high;
18050 int low_default_is_valid;
18051 int high_bound_is_count = 0;
18052 const char *name;
18053 ULONGEST negative_mask;
18054
18055 orig_base_type = read_subrange_index_type (die, cu);
18056
18057 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
18058 whereas the real type might be. So, we use ORIG_BASE_TYPE when
18059 creating the range type, but we use the result of check_typedef
18060 when examining properties of the type. */
18061 base_type = check_typedef (orig_base_type);
18062
18063 /* The die_type call above may have already set the type for this DIE. */
18064 range_type = get_die_type (die, cu);
18065 if (range_type)
18066 return range_type;
18067
18068 low.kind = PROP_CONST;
18069 high.kind = PROP_CONST;
18070 high.data.const_val = 0;
18071
18072 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
18073 omitting DW_AT_lower_bound. */
18074 switch (cu->language)
18075 {
18076 case language_c:
18077 case language_cplus:
18078 low.data.const_val = 0;
18079 low_default_is_valid = 1;
18080 break;
18081 case language_fortran:
18082 low.data.const_val = 1;
18083 low_default_is_valid = 1;
18084 break;
18085 case language_d:
18086 case language_objc:
18087 case language_rust:
18088 low.data.const_val = 0;
18089 low_default_is_valid = (cu->header.version >= 4);
18090 break;
18091 case language_ada:
18092 case language_m2:
18093 case language_pascal:
18094 low.data.const_val = 1;
18095 low_default_is_valid = (cu->header.version >= 4);
18096 break;
18097 default:
18098 low.data.const_val = 0;
18099 low_default_is_valid = 0;
18100 break;
18101 }
18102
18103 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
18104 if (attr != nullptr)
18105 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
18106 else if (!low_default_is_valid)
18107 complaint (_("Missing DW_AT_lower_bound "
18108 "- DIE at %s [in module %s]"),
18109 sect_offset_str (die->sect_off),
18110 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
18111
18112 struct attribute *attr_ub, *attr_count;
18113 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
18114 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
18115 {
18116 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
18117 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
18118 {
18119 /* If bounds are constant do the final calculation here. */
18120 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
18121 high.data.const_val = low.data.const_val + high.data.const_val - 1;
18122 else
18123 high_bound_is_count = 1;
18124 }
18125 else
18126 {
18127 if (attr_ub != NULL)
18128 complaint (_("Unresolved DW_AT_upper_bound "
18129 "- DIE at %s [in module %s]"),
18130 sect_offset_str (die->sect_off),
18131 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
18132 if (attr_count != NULL)
18133 complaint (_("Unresolved DW_AT_count "
18134 "- DIE at %s [in module %s]"),
18135 sect_offset_str (die->sect_off),
18136 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
18137 }
18138 }
18139
18140 LONGEST bias = 0;
18141 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
18142 if (bias_attr != nullptr && attr_form_is_constant (bias_attr))
18143 bias = dwarf2_get_attr_constant_value (bias_attr, 0);
18144
18145 /* Normally, the DWARF producers are expected to use a signed
18146 constant form (Eg. DW_FORM_sdata) to express negative bounds.
18147 But this is unfortunately not always the case, as witnessed
18148 with GCC, for instance, where the ambiguous DW_FORM_dataN form
18149 is used instead. To work around that ambiguity, we treat
18150 the bounds as signed, and thus sign-extend their values, when
18151 the base type is signed. */
18152 negative_mask =
18153 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
18154 if (low.kind == PROP_CONST
18155 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
18156 low.data.const_val |= negative_mask;
18157 if (high.kind == PROP_CONST
18158 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
18159 high.data.const_val |= negative_mask;
18160
18161 /* Check for bit and byte strides. */
18162 struct dynamic_prop byte_stride_prop;
18163 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
18164 if (attr_byte_stride != nullptr)
18165 {
18166 struct type *prop_type
18167 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
18168 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
18169 prop_type);
18170 }
18171
18172 struct dynamic_prop bit_stride_prop;
18173 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
18174 if (attr_bit_stride != nullptr)
18175 {
18176 /* It only makes sense to have either a bit or byte stride. */
18177 if (attr_byte_stride != nullptr)
18178 {
18179 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
18180 "- DIE at %s [in module %s]"),
18181 sect_offset_str (die->sect_off),
18182 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
18183 attr_bit_stride = nullptr;
18184 }
18185 else
18186 {
18187 struct type *prop_type
18188 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
18189 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
18190 prop_type);
18191 }
18192 }
18193
18194 if (attr_byte_stride != nullptr
18195 || attr_bit_stride != nullptr)
18196 {
18197 bool byte_stride_p = (attr_byte_stride != nullptr);
18198 struct dynamic_prop *stride
18199 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
18200
18201 range_type
18202 = create_range_type_with_stride (NULL, orig_base_type, &low,
18203 &high, bias, stride, byte_stride_p);
18204 }
18205 else
18206 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
18207
18208 if (high_bound_is_count)
18209 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
18210
18211 /* Ada expects an empty array on no boundary attributes. */
18212 if (attr == NULL && cu->language != language_ada)
18213 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
18214
18215 name = dwarf2_name (die, cu);
18216 if (name)
18217 TYPE_NAME (range_type) = name;
18218
18219 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
18220 if (attr != nullptr)
18221 TYPE_LENGTH (range_type) = DW_UNSND (attr);
18222
18223 maybe_set_alignment (cu, die, range_type);
18224
18225 set_die_type (die, range_type, cu);
18226
18227 /* set_die_type should be already done. */
18228 set_descriptive_type (range_type, die, cu);
18229
18230 return range_type;
18231 }
18232
18233 static struct type *
18234 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
18235 {
18236 struct type *type;
18237
18238 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
18239 NULL);
18240 TYPE_NAME (type) = dwarf2_name (die, cu);
18241
18242 /* In Ada, an unspecified type is typically used when the description
18243 of the type is deferred to a different unit. When encountering
18244 such a type, we treat it as a stub, and try to resolve it later on,
18245 when needed. */
18246 if (cu->language == language_ada)
18247 TYPE_STUB (type) = 1;
18248
18249 return set_die_type (die, type, cu);
18250 }
18251
18252 /* Read a single die and all its descendents. Set the die's sibling
18253 field to NULL; set other fields in the die correctly, and set all
18254 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
18255 location of the info_ptr after reading all of those dies. PARENT
18256 is the parent of the die in question. */
18257
18258 static struct die_info *
18259 read_die_and_children (const struct die_reader_specs *reader,
18260 const gdb_byte *info_ptr,
18261 const gdb_byte **new_info_ptr,
18262 struct die_info *parent)
18263 {
18264 struct die_info *die;
18265 const gdb_byte *cur_ptr;
18266 int has_children;
18267
18268 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
18269 if (die == NULL)
18270 {
18271 *new_info_ptr = cur_ptr;
18272 return NULL;
18273 }
18274 store_in_ref_table (die, reader->cu);
18275
18276 if (has_children)
18277 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
18278 else
18279 {
18280 die->child = NULL;
18281 *new_info_ptr = cur_ptr;
18282 }
18283
18284 die->sibling = NULL;
18285 die->parent = parent;
18286 return die;
18287 }
18288
18289 /* Read a die, all of its descendents, and all of its siblings; set
18290 all of the fields of all of the dies correctly. Arguments are as
18291 in read_die_and_children. */
18292
18293 static struct die_info *
18294 read_die_and_siblings_1 (const struct die_reader_specs *reader,
18295 const gdb_byte *info_ptr,
18296 const gdb_byte **new_info_ptr,
18297 struct die_info *parent)
18298 {
18299 struct die_info *first_die, *last_sibling;
18300 const gdb_byte *cur_ptr;
18301
18302 cur_ptr = info_ptr;
18303 first_die = last_sibling = NULL;
18304
18305 while (1)
18306 {
18307 struct die_info *die
18308 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
18309
18310 if (die == NULL)
18311 {
18312 *new_info_ptr = cur_ptr;
18313 return first_die;
18314 }
18315
18316 if (!first_die)
18317 first_die = die;
18318 else
18319 last_sibling->sibling = die;
18320
18321 last_sibling = die;
18322 }
18323 }
18324
18325 /* Read a die, all of its descendents, and all of its siblings; set
18326 all of the fields of all of the dies correctly. Arguments are as
18327 in read_die_and_children.
18328 This the main entry point for reading a DIE and all its children. */
18329
18330 static struct die_info *
18331 read_die_and_siblings (const struct die_reader_specs *reader,
18332 const gdb_byte *info_ptr,
18333 const gdb_byte **new_info_ptr,
18334 struct die_info *parent)
18335 {
18336 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
18337 new_info_ptr, parent);
18338
18339 if (dwarf_die_debug)
18340 {
18341 fprintf_unfiltered (gdb_stdlog,
18342 "Read die from %s@0x%x of %s:\n",
18343 get_section_name (reader->die_section),
18344 (unsigned) (info_ptr - reader->die_section->buffer),
18345 bfd_get_filename (reader->abfd));
18346 dump_die (die, dwarf_die_debug);
18347 }
18348
18349 return die;
18350 }
18351
18352 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
18353 attributes.
18354 The caller is responsible for filling in the extra attributes
18355 and updating (*DIEP)->num_attrs.
18356 Set DIEP to point to a newly allocated die with its information,
18357 except for its child, sibling, and parent fields.
18358 Set HAS_CHILDREN to tell whether the die has children or not. */
18359
18360 static const gdb_byte *
18361 read_full_die_1 (const struct die_reader_specs *reader,
18362 struct die_info **diep, const gdb_byte *info_ptr,
18363 int *has_children, int num_extra_attrs)
18364 {
18365 unsigned int abbrev_number, bytes_read, i;
18366 struct abbrev_info *abbrev;
18367 struct die_info *die;
18368 struct dwarf2_cu *cu = reader->cu;
18369 bfd *abfd = reader->abfd;
18370
18371 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
18372 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18373 info_ptr += bytes_read;
18374 if (!abbrev_number)
18375 {
18376 *diep = NULL;
18377 *has_children = 0;
18378 return info_ptr;
18379 }
18380
18381 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
18382 if (!abbrev)
18383 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18384 abbrev_number,
18385 bfd_get_filename (abfd));
18386
18387 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
18388 die->sect_off = sect_off;
18389 die->tag = abbrev->tag;
18390 die->abbrev = abbrev_number;
18391
18392 /* Make the result usable.
18393 The caller needs to update num_attrs after adding the extra
18394 attributes. */
18395 die->num_attrs = abbrev->num_attrs;
18396
18397 std::vector<int> indexes_that_need_reprocess;
18398 for (i = 0; i < abbrev->num_attrs; ++i)
18399 {
18400 bool need_reprocess;
18401 info_ptr =
18402 read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18403 info_ptr, &need_reprocess);
18404 if (need_reprocess)
18405 indexes_that_need_reprocess.push_back (i);
18406 }
18407
18408 struct attribute *attr = dwarf2_attr_no_follow (die, DW_AT_str_offsets_base);
18409 if (attr != nullptr)
18410 cu->str_offsets_base = DW_UNSND (attr);
18411
18412 auto maybe_addr_base = lookup_addr_base(die);
18413 if (maybe_addr_base.has_value ())
18414 cu->addr_base = *maybe_addr_base;
18415 for (int index : indexes_that_need_reprocess)
18416 read_attribute_reprocess (reader, &die->attrs[index]);
18417 *diep = die;
18418 *has_children = abbrev->has_children;
18419 return info_ptr;
18420 }
18421
18422 /* Read a die and all its attributes.
18423 Set DIEP to point to a newly allocated die with its information,
18424 except for its child, sibling, and parent fields.
18425 Set HAS_CHILDREN to tell whether the die has children or not. */
18426
18427 static const gdb_byte *
18428 read_full_die (const struct die_reader_specs *reader,
18429 struct die_info **diep, const gdb_byte *info_ptr,
18430 int *has_children)
18431 {
18432 const gdb_byte *result;
18433
18434 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
18435
18436 if (dwarf_die_debug)
18437 {
18438 fprintf_unfiltered (gdb_stdlog,
18439 "Read die from %s@0x%x of %s:\n",
18440 get_section_name (reader->die_section),
18441 (unsigned) (info_ptr - reader->die_section->buffer),
18442 bfd_get_filename (reader->abfd));
18443 dump_die (*diep, dwarf_die_debug);
18444 }
18445
18446 return result;
18447 }
18448 \f
18449 /* Abbreviation tables.
18450
18451 In DWARF version 2, the description of the debugging information is
18452 stored in a separate .debug_abbrev section. Before we read any
18453 dies from a section we read in all abbreviations and install them
18454 in a hash table. */
18455
18456 /* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
18457
18458 struct abbrev_info *
18459 abbrev_table::alloc_abbrev ()
18460 {
18461 struct abbrev_info *abbrev;
18462
18463 abbrev = XOBNEW (&abbrev_obstack, struct abbrev_info);
18464 memset (abbrev, 0, sizeof (struct abbrev_info));
18465
18466 return abbrev;
18467 }
18468
18469 /* Add an abbreviation to the table. */
18470
18471 void
18472 abbrev_table::add_abbrev (unsigned int abbrev_number,
18473 struct abbrev_info *abbrev)
18474 {
18475 unsigned int hash_number;
18476
18477 hash_number = abbrev_number % ABBREV_HASH_SIZE;
18478 abbrev->next = m_abbrevs[hash_number];
18479 m_abbrevs[hash_number] = abbrev;
18480 }
18481
18482 /* Look up an abbrev in the table.
18483 Returns NULL if the abbrev is not found. */
18484
18485 struct abbrev_info *
18486 abbrev_table::lookup_abbrev (unsigned int abbrev_number)
18487 {
18488 unsigned int hash_number;
18489 struct abbrev_info *abbrev;
18490
18491 hash_number = abbrev_number % ABBREV_HASH_SIZE;
18492 abbrev = m_abbrevs[hash_number];
18493
18494 while (abbrev)
18495 {
18496 if (abbrev->number == abbrev_number)
18497 return abbrev;
18498 abbrev = abbrev->next;
18499 }
18500 return NULL;
18501 }
18502
18503 /* Read in an abbrev table. */
18504
18505 static abbrev_table_up
18506 abbrev_table_read_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
18507 struct dwarf2_section_info *section,
18508 sect_offset sect_off)
18509 {
18510 struct objfile *objfile = dwarf2_per_objfile->objfile;
18511 bfd *abfd = get_section_bfd_owner (section);
18512 const gdb_byte *abbrev_ptr;
18513 struct abbrev_info *cur_abbrev;
18514 unsigned int abbrev_number, bytes_read, abbrev_name;
18515 unsigned int abbrev_form;
18516 std::vector<struct attr_abbrev> cur_attrs;
18517
18518 abbrev_table_up abbrev_table (new struct abbrev_table (sect_off));
18519
18520 dwarf2_read_section (objfile, section);
18521 abbrev_ptr = section->buffer + to_underlying (sect_off);
18522 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18523 abbrev_ptr += bytes_read;
18524
18525 /* Loop until we reach an abbrev number of 0. */
18526 while (abbrev_number)
18527 {
18528 cur_attrs.clear ();
18529 cur_abbrev = abbrev_table->alloc_abbrev ();
18530
18531 /* read in abbrev header */
18532 cur_abbrev->number = abbrev_number;
18533 cur_abbrev->tag
18534 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18535 abbrev_ptr += bytes_read;
18536 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
18537 abbrev_ptr += 1;
18538
18539 /* now read in declarations */
18540 for (;;)
18541 {
18542 LONGEST implicit_const;
18543
18544 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18545 abbrev_ptr += bytes_read;
18546 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18547 abbrev_ptr += bytes_read;
18548 if (abbrev_form == DW_FORM_implicit_const)
18549 {
18550 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
18551 &bytes_read);
18552 abbrev_ptr += bytes_read;
18553 }
18554 else
18555 {
18556 /* Initialize it due to a false compiler warning. */
18557 implicit_const = -1;
18558 }
18559
18560 if (abbrev_name == 0)
18561 break;
18562
18563 cur_attrs.emplace_back ();
18564 struct attr_abbrev &cur_attr = cur_attrs.back ();
18565 cur_attr.name = (enum dwarf_attribute) abbrev_name;
18566 cur_attr.form = (enum dwarf_form) abbrev_form;
18567 cur_attr.implicit_const = implicit_const;
18568 ++cur_abbrev->num_attrs;
18569 }
18570
18571 cur_abbrev->attrs =
18572 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
18573 cur_abbrev->num_attrs);
18574 memcpy (cur_abbrev->attrs, cur_attrs.data (),
18575 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
18576
18577 abbrev_table->add_abbrev (abbrev_number, cur_abbrev);
18578
18579 /* Get next abbreviation.
18580 Under Irix6 the abbreviations for a compilation unit are not
18581 always properly terminated with an abbrev number of 0.
18582 Exit loop if we encounter an abbreviation which we have
18583 already read (which means we are about to read the abbreviations
18584 for the next compile unit) or if the end of the abbreviation
18585 table is reached. */
18586 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
18587 break;
18588 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18589 abbrev_ptr += bytes_read;
18590 if (abbrev_table->lookup_abbrev (abbrev_number) != NULL)
18591 break;
18592 }
18593
18594 return abbrev_table;
18595 }
18596
18597 /* Returns nonzero if TAG represents a type that we might generate a partial
18598 symbol for. */
18599
18600 static int
18601 is_type_tag_for_partial (int tag)
18602 {
18603 switch (tag)
18604 {
18605 #if 0
18606 /* Some types that would be reasonable to generate partial symbols for,
18607 that we don't at present. */
18608 case DW_TAG_array_type:
18609 case DW_TAG_file_type:
18610 case DW_TAG_ptr_to_member_type:
18611 case DW_TAG_set_type:
18612 case DW_TAG_string_type:
18613 case DW_TAG_subroutine_type:
18614 #endif
18615 case DW_TAG_base_type:
18616 case DW_TAG_class_type:
18617 case DW_TAG_interface_type:
18618 case DW_TAG_enumeration_type:
18619 case DW_TAG_structure_type:
18620 case DW_TAG_subrange_type:
18621 case DW_TAG_typedef:
18622 case DW_TAG_union_type:
18623 return 1;
18624 default:
18625 return 0;
18626 }
18627 }
18628
18629 /* Load all DIEs that are interesting for partial symbols into memory. */
18630
18631 static struct partial_die_info *
18632 load_partial_dies (const struct die_reader_specs *reader,
18633 const gdb_byte *info_ptr, int building_psymtab)
18634 {
18635 struct dwarf2_cu *cu = reader->cu;
18636 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18637 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
18638 unsigned int bytes_read;
18639 unsigned int load_all = 0;
18640 int nesting_level = 1;
18641
18642 parent_die = NULL;
18643 last_die = NULL;
18644
18645 gdb_assert (cu->per_cu != NULL);
18646 if (cu->per_cu->load_all_dies)
18647 load_all = 1;
18648
18649 cu->partial_dies
18650 = htab_create_alloc_ex (cu->header.length / 12,
18651 partial_die_hash,
18652 partial_die_eq,
18653 NULL,
18654 &cu->comp_unit_obstack,
18655 hashtab_obstack_allocate,
18656 dummy_obstack_deallocate);
18657
18658 while (1)
18659 {
18660 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
18661
18662 /* A NULL abbrev means the end of a series of children. */
18663 if (abbrev == NULL)
18664 {
18665 if (--nesting_level == 0)
18666 return first_die;
18667
18668 info_ptr += bytes_read;
18669 last_die = parent_die;
18670 parent_die = parent_die->die_parent;
18671 continue;
18672 }
18673
18674 /* Check for template arguments. We never save these; if
18675 they're seen, we just mark the parent, and go on our way. */
18676 if (parent_die != NULL
18677 && cu->language == language_cplus
18678 && (abbrev->tag == DW_TAG_template_type_param
18679 || abbrev->tag == DW_TAG_template_value_param))
18680 {
18681 parent_die->has_template_arguments = 1;
18682
18683 if (!load_all)
18684 {
18685 /* We don't need a partial DIE for the template argument. */
18686 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18687 continue;
18688 }
18689 }
18690
18691 /* We only recurse into c++ subprograms looking for template arguments.
18692 Skip their other children. */
18693 if (!load_all
18694 && cu->language == language_cplus
18695 && parent_die != NULL
18696 && parent_die->tag == DW_TAG_subprogram)
18697 {
18698 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18699 continue;
18700 }
18701
18702 /* Check whether this DIE is interesting enough to save. Normally
18703 we would not be interested in members here, but there may be
18704 later variables referencing them via DW_AT_specification (for
18705 static members). */
18706 if (!load_all
18707 && !is_type_tag_for_partial (abbrev->tag)
18708 && abbrev->tag != DW_TAG_constant
18709 && abbrev->tag != DW_TAG_enumerator
18710 && abbrev->tag != DW_TAG_subprogram
18711 && abbrev->tag != DW_TAG_inlined_subroutine
18712 && abbrev->tag != DW_TAG_lexical_block
18713 && abbrev->tag != DW_TAG_variable
18714 && abbrev->tag != DW_TAG_namespace
18715 && abbrev->tag != DW_TAG_module
18716 && abbrev->tag != DW_TAG_member
18717 && abbrev->tag != DW_TAG_imported_unit
18718 && abbrev->tag != DW_TAG_imported_declaration)
18719 {
18720 /* Otherwise we skip to the next sibling, if any. */
18721 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18722 continue;
18723 }
18724
18725 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18726 abbrev);
18727
18728 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
18729
18730 /* This two-pass algorithm for processing partial symbols has a
18731 high cost in cache pressure. Thus, handle some simple cases
18732 here which cover the majority of C partial symbols. DIEs
18733 which neither have specification tags in them, nor could have
18734 specification tags elsewhere pointing at them, can simply be
18735 processed and discarded.
18736
18737 This segment is also optional; scan_partial_symbols and
18738 add_partial_symbol will handle these DIEs if we chain
18739 them in normally. When compilers which do not emit large
18740 quantities of duplicate debug information are more common,
18741 this code can probably be removed. */
18742
18743 /* Any complete simple types at the top level (pretty much all
18744 of them, for a language without namespaces), can be processed
18745 directly. */
18746 if (parent_die == NULL
18747 && pdi.has_specification == 0
18748 && pdi.is_declaration == 0
18749 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18750 || pdi.tag == DW_TAG_base_type
18751 || pdi.tag == DW_TAG_subrange_type))
18752 {
18753 if (building_psymtab && pdi.name != NULL)
18754 add_psymbol_to_list (pdi.name, false,
18755 VAR_DOMAIN, LOC_TYPEDEF, -1,
18756 psymbol_placement::STATIC,
18757 0, cu->language, objfile);
18758 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18759 continue;
18760 }
18761
18762 /* The exception for DW_TAG_typedef with has_children above is
18763 a workaround of GCC PR debug/47510. In the case of this complaint
18764 type_name_or_error will error on such types later.
18765
18766 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18767 it could not find the child DIEs referenced later, this is checked
18768 above. In correct DWARF DW_TAG_typedef should have no children. */
18769
18770 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
18771 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
18772 "- DIE at %s [in module %s]"),
18773 sect_offset_str (pdi.sect_off), objfile_name (objfile));
18774
18775 /* If we're at the second level, and we're an enumerator, and
18776 our parent has no specification (meaning possibly lives in a
18777 namespace elsewhere), then we can add the partial symbol now
18778 instead of queueing it. */
18779 if (pdi.tag == DW_TAG_enumerator
18780 && parent_die != NULL
18781 && parent_die->die_parent == NULL
18782 && parent_die->tag == DW_TAG_enumeration_type
18783 && parent_die->has_specification == 0)
18784 {
18785 if (pdi.name == NULL)
18786 complaint (_("malformed enumerator DIE ignored"));
18787 else if (building_psymtab)
18788 add_psymbol_to_list (pdi.name, false,
18789 VAR_DOMAIN, LOC_CONST, -1,
18790 cu->language == language_cplus
18791 ? psymbol_placement::GLOBAL
18792 : psymbol_placement::STATIC,
18793 0, cu->language, objfile);
18794
18795 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18796 continue;
18797 }
18798
18799 struct partial_die_info *part_die
18800 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
18801
18802 /* We'll save this DIE so link it in. */
18803 part_die->die_parent = parent_die;
18804 part_die->die_sibling = NULL;
18805 part_die->die_child = NULL;
18806
18807 if (last_die && last_die == parent_die)
18808 last_die->die_child = part_die;
18809 else if (last_die)
18810 last_die->die_sibling = part_die;
18811
18812 last_die = part_die;
18813
18814 if (first_die == NULL)
18815 first_die = part_die;
18816
18817 /* Maybe add the DIE to the hash table. Not all DIEs that we
18818 find interesting need to be in the hash table, because we
18819 also have the parent/sibling/child chains; only those that we
18820 might refer to by offset later during partial symbol reading.
18821
18822 For now this means things that might have be the target of a
18823 DW_AT_specification, DW_AT_abstract_origin, or
18824 DW_AT_extension. DW_AT_extension will refer only to
18825 namespaces; DW_AT_abstract_origin refers to functions (and
18826 many things under the function DIE, but we do not recurse
18827 into function DIEs during partial symbol reading) and
18828 possibly variables as well; DW_AT_specification refers to
18829 declarations. Declarations ought to have the DW_AT_declaration
18830 flag. It happens that GCC forgets to put it in sometimes, but
18831 only for functions, not for types.
18832
18833 Adding more things than necessary to the hash table is harmless
18834 except for the performance cost. Adding too few will result in
18835 wasted time in find_partial_die, when we reread the compilation
18836 unit with load_all_dies set. */
18837
18838 if (load_all
18839 || abbrev->tag == DW_TAG_constant
18840 || abbrev->tag == DW_TAG_subprogram
18841 || abbrev->tag == DW_TAG_variable
18842 || abbrev->tag == DW_TAG_namespace
18843 || part_die->is_declaration)
18844 {
18845 void **slot;
18846
18847 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
18848 to_underlying (part_die->sect_off),
18849 INSERT);
18850 *slot = part_die;
18851 }
18852
18853 /* For some DIEs we want to follow their children (if any). For C
18854 we have no reason to follow the children of structures; for other
18855 languages we have to, so that we can get at method physnames
18856 to infer fully qualified class names, for DW_AT_specification,
18857 and for C++ template arguments. For C++, we also look one level
18858 inside functions to find template arguments (if the name of the
18859 function does not already contain the template arguments).
18860
18861 For Ada and Fortran, we need to scan the children of subprograms
18862 and lexical blocks as well because these languages allow the
18863 definition of nested entities that could be interesting for the
18864 debugger, such as nested subprograms for instance. */
18865 if (last_die->has_children
18866 && (load_all
18867 || last_die->tag == DW_TAG_namespace
18868 || last_die->tag == DW_TAG_module
18869 || last_die->tag == DW_TAG_enumeration_type
18870 || (cu->language == language_cplus
18871 && last_die->tag == DW_TAG_subprogram
18872 && (last_die->name == NULL
18873 || strchr (last_die->name, '<') == NULL))
18874 || (cu->language != language_c
18875 && (last_die->tag == DW_TAG_class_type
18876 || last_die->tag == DW_TAG_interface_type
18877 || last_die->tag == DW_TAG_structure_type
18878 || last_die->tag == DW_TAG_union_type))
18879 || ((cu->language == language_ada
18880 || cu->language == language_fortran)
18881 && (last_die->tag == DW_TAG_subprogram
18882 || last_die->tag == DW_TAG_lexical_block))))
18883 {
18884 nesting_level++;
18885 parent_die = last_die;
18886 continue;
18887 }
18888
18889 /* Otherwise we skip to the next sibling, if any. */
18890 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
18891
18892 /* Back to the top, do it again. */
18893 }
18894 }
18895
18896 partial_die_info::partial_die_info (sect_offset sect_off_,
18897 struct abbrev_info *abbrev)
18898 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18899 {
18900 }
18901
18902 /* Read a minimal amount of information into the minimal die structure.
18903 INFO_PTR should point just after the initial uleb128 of a DIE. */
18904
18905 const gdb_byte *
18906 partial_die_info::read (const struct die_reader_specs *reader,
18907 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
18908 {
18909 struct dwarf2_cu *cu = reader->cu;
18910 struct dwarf2_per_objfile *dwarf2_per_objfile
18911 = cu->per_cu->dwarf2_per_objfile;
18912 unsigned int i;
18913 int has_low_pc_attr = 0;
18914 int has_high_pc_attr = 0;
18915 int high_pc_relative = 0;
18916
18917 std::vector<struct attribute> attr_vec (abbrev.num_attrs);
18918 for (i = 0; i < abbrev.num_attrs; ++i)
18919 {
18920 bool need_reprocess;
18921 info_ptr = read_attribute (reader, &attr_vec[i], &abbrev.attrs[i],
18922 info_ptr, &need_reprocess);
18923 /* String and address offsets that need to do the reprocessing have
18924 already been read at this point, so there is no need to wait until
18925 the loop terminates to do the reprocessing. */
18926 if (need_reprocess)
18927 read_attribute_reprocess (reader, &attr_vec[i]);
18928 attribute &attr = attr_vec[i];
18929 /* Store the data if it is of an attribute we want to keep in a
18930 partial symbol table. */
18931 switch (attr.name)
18932 {
18933 case DW_AT_name:
18934 switch (tag)
18935 {
18936 case DW_TAG_compile_unit:
18937 case DW_TAG_partial_unit:
18938 case DW_TAG_type_unit:
18939 /* Compilation units have a DW_AT_name that is a filename, not
18940 a source language identifier. */
18941 case DW_TAG_enumeration_type:
18942 case DW_TAG_enumerator:
18943 /* These tags always have simple identifiers already; no need
18944 to canonicalize them. */
18945 name = DW_STRING (&attr);
18946 break;
18947 default:
18948 {
18949 struct objfile *objfile = dwarf2_per_objfile->objfile;
18950
18951 name
18952 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
18953 &objfile->per_bfd->storage_obstack);
18954 }
18955 break;
18956 }
18957 break;
18958 case DW_AT_linkage_name:
18959 case DW_AT_MIPS_linkage_name:
18960 /* Note that both forms of linkage name might appear. We
18961 assume they will be the same, and we only store the last
18962 one we see. */
18963 linkage_name = DW_STRING (&attr);
18964 break;
18965 case DW_AT_low_pc:
18966 has_low_pc_attr = 1;
18967 lowpc = attr_value_as_address (&attr);
18968 break;
18969 case DW_AT_high_pc:
18970 has_high_pc_attr = 1;
18971 highpc = attr_value_as_address (&attr);
18972 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
18973 high_pc_relative = 1;
18974 break;
18975 case DW_AT_location:
18976 /* Support the .debug_loc offsets. */
18977 if (attr_form_is_block (&attr))
18978 {
18979 d.locdesc = DW_BLOCK (&attr);
18980 }
18981 else if (attr_form_is_section_offset (&attr))
18982 {
18983 dwarf2_complex_location_expr_complaint ();
18984 }
18985 else
18986 {
18987 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18988 "partial symbol information");
18989 }
18990 break;
18991 case DW_AT_external:
18992 is_external = DW_UNSND (&attr);
18993 break;
18994 case DW_AT_declaration:
18995 is_declaration = DW_UNSND (&attr);
18996 break;
18997 case DW_AT_type:
18998 has_type = 1;
18999 break;
19000 case DW_AT_abstract_origin:
19001 case DW_AT_specification:
19002 case DW_AT_extension:
19003 has_specification = 1;
19004 spec_offset = dwarf2_get_ref_die_offset (&attr);
19005 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
19006 || cu->per_cu->is_dwz);
19007 break;
19008 case DW_AT_sibling:
19009 /* Ignore absolute siblings, they might point outside of
19010 the current compile unit. */
19011 if (attr.form == DW_FORM_ref_addr)
19012 complaint (_("ignoring absolute DW_AT_sibling"));
19013 else
19014 {
19015 const gdb_byte *buffer = reader->buffer;
19016 sect_offset off = dwarf2_get_ref_die_offset (&attr);
19017 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
19018
19019 if (sibling_ptr < info_ptr)
19020 complaint (_("DW_AT_sibling points backwards"));
19021 else if (sibling_ptr > reader->buffer_end)
19022 dwarf2_section_buffer_overflow_complaint (reader->die_section);
19023 else
19024 sibling = sibling_ptr;
19025 }
19026 break;
19027 case DW_AT_byte_size:
19028 has_byte_size = 1;
19029 break;
19030 case DW_AT_const_value:
19031 has_const_value = 1;
19032 break;
19033 case DW_AT_calling_convention:
19034 /* DWARF doesn't provide a way to identify a program's source-level
19035 entry point. DW_AT_calling_convention attributes are only meant
19036 to describe functions' calling conventions.
19037
19038 However, because it's a necessary piece of information in
19039 Fortran, and before DWARF 4 DW_CC_program was the only
19040 piece of debugging information whose definition refers to
19041 a 'main program' at all, several compilers marked Fortran
19042 main programs with DW_CC_program --- even when those
19043 functions use the standard calling conventions.
19044
19045 Although DWARF now specifies a way to provide this
19046 information, we support this practice for backward
19047 compatibility. */
19048 if (DW_UNSND (&attr) == DW_CC_program
19049 && cu->language == language_fortran)
19050 main_subprogram = 1;
19051 break;
19052 case DW_AT_inline:
19053 if (DW_UNSND (&attr) == DW_INL_inlined
19054 || DW_UNSND (&attr) == DW_INL_declared_inlined)
19055 may_be_inlined = 1;
19056 break;
19057
19058 case DW_AT_import:
19059 if (tag == DW_TAG_imported_unit)
19060 {
19061 d.sect_off = dwarf2_get_ref_die_offset (&attr);
19062 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
19063 || cu->per_cu->is_dwz);
19064 }
19065 break;
19066
19067 case DW_AT_main_subprogram:
19068 main_subprogram = DW_UNSND (&attr);
19069 break;
19070
19071 case DW_AT_ranges:
19072 {
19073 /* It would be nice to reuse dwarf2_get_pc_bounds here,
19074 but that requires a full DIE, so instead we just
19075 reimplement it. */
19076 int need_ranges_base = tag != DW_TAG_compile_unit;
19077 unsigned int ranges_offset = (DW_UNSND (&attr)
19078 + (need_ranges_base
19079 ? cu->ranges_base
19080 : 0));
19081
19082 /* Value of the DW_AT_ranges attribute is the offset in the
19083 .debug_ranges section. */
19084 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
19085 nullptr))
19086 has_pc_info = 1;
19087 }
19088 break;
19089
19090 default:
19091 break;
19092 }
19093 }
19094
19095 /* For Ada, if both the name and the linkage name appear, we prefer
19096 the latter. This lets "catch exception" work better, regardless
19097 of the order in which the name and linkage name were emitted.
19098 Really, though, this is just a workaround for the fact that gdb
19099 doesn't store both the name and the linkage name. */
19100 if (cu->language == language_ada && linkage_name != nullptr)
19101 name = linkage_name;
19102
19103 if (high_pc_relative)
19104 highpc += lowpc;
19105
19106 if (has_low_pc_attr && has_high_pc_attr)
19107 {
19108 /* When using the GNU linker, .gnu.linkonce. sections are used to
19109 eliminate duplicate copies of functions and vtables and such.
19110 The linker will arbitrarily choose one and discard the others.
19111 The AT_*_pc values for such functions refer to local labels in
19112 these sections. If the section from that file was discarded, the
19113 labels are not in the output, so the relocs get a value of 0.
19114 If this is a discarded function, mark the pc bounds as invalid,
19115 so that GDB will ignore it. */
19116 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
19117 {
19118 struct objfile *objfile = dwarf2_per_objfile->objfile;
19119 struct gdbarch *gdbarch = get_objfile_arch (objfile);
19120
19121 complaint (_("DW_AT_low_pc %s is zero "
19122 "for DIE at %s [in module %s]"),
19123 paddress (gdbarch, lowpc),
19124 sect_offset_str (sect_off),
19125 objfile_name (objfile));
19126 }
19127 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
19128 else if (lowpc >= highpc)
19129 {
19130 struct objfile *objfile = dwarf2_per_objfile->objfile;
19131 struct gdbarch *gdbarch = get_objfile_arch (objfile);
19132
19133 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
19134 "for DIE at %s [in module %s]"),
19135 paddress (gdbarch, lowpc),
19136 paddress (gdbarch, highpc),
19137 sect_offset_str (sect_off),
19138 objfile_name (objfile));
19139 }
19140 else
19141 has_pc_info = 1;
19142 }
19143
19144 return info_ptr;
19145 }
19146
19147 /* Find a cached partial DIE at OFFSET in CU. */
19148
19149 struct partial_die_info *
19150 dwarf2_cu::find_partial_die (sect_offset sect_off)
19151 {
19152 struct partial_die_info *lookup_die = NULL;
19153 struct partial_die_info part_die (sect_off);
19154
19155 lookup_die = ((struct partial_die_info *)
19156 htab_find_with_hash (partial_dies, &part_die,
19157 to_underlying (sect_off)));
19158
19159 return lookup_die;
19160 }
19161
19162 /* Find a partial DIE at OFFSET, which may or may not be in CU,
19163 except in the case of .debug_types DIEs which do not reference
19164 outside their CU (they do however referencing other types via
19165 DW_FORM_ref_sig8). */
19166
19167 static const struct cu_partial_die_info
19168 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
19169 {
19170 struct dwarf2_per_objfile *dwarf2_per_objfile
19171 = cu->per_cu->dwarf2_per_objfile;
19172 struct objfile *objfile = dwarf2_per_objfile->objfile;
19173 struct dwarf2_per_cu_data *per_cu = NULL;
19174 struct partial_die_info *pd = NULL;
19175
19176 if (offset_in_dwz == cu->per_cu->is_dwz
19177 && offset_in_cu_p (&cu->header, sect_off))
19178 {
19179 pd = cu->find_partial_die (sect_off);
19180 if (pd != NULL)
19181 return { cu, pd };
19182 /* We missed recording what we needed.
19183 Load all dies and try again. */
19184 per_cu = cu->per_cu;
19185 }
19186 else
19187 {
19188 /* TUs don't reference other CUs/TUs (except via type signatures). */
19189 if (cu->per_cu->is_debug_types)
19190 {
19191 error (_("Dwarf Error: Type Unit at offset %s contains"
19192 " external reference to offset %s [in module %s].\n"),
19193 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
19194 bfd_get_filename (objfile->obfd));
19195 }
19196 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
19197 dwarf2_per_objfile);
19198
19199 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
19200 load_partial_comp_unit (per_cu);
19201
19202 per_cu->cu->last_used = 0;
19203 pd = per_cu->cu->find_partial_die (sect_off);
19204 }
19205
19206 /* If we didn't find it, and not all dies have been loaded,
19207 load them all and try again. */
19208
19209 if (pd == NULL && per_cu->load_all_dies == 0)
19210 {
19211 per_cu->load_all_dies = 1;
19212
19213 /* This is nasty. When we reread the DIEs, somewhere up the call chain
19214 THIS_CU->cu may already be in use. So we can't just free it and
19215 replace its DIEs with the ones we read in. Instead, we leave those
19216 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
19217 and clobber THIS_CU->cu->partial_dies with the hash table for the new
19218 set. */
19219 load_partial_comp_unit (per_cu);
19220
19221 pd = per_cu->cu->find_partial_die (sect_off);
19222 }
19223
19224 if (pd == NULL)
19225 internal_error (__FILE__, __LINE__,
19226 _("could not find partial DIE %s "
19227 "in cache [from module %s]\n"),
19228 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
19229 return { per_cu->cu, pd };
19230 }
19231
19232 /* See if we can figure out if the class lives in a namespace. We do
19233 this by looking for a member function; its demangled name will
19234 contain namespace info, if there is any. */
19235
19236 static void
19237 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
19238 struct dwarf2_cu *cu)
19239 {
19240 /* NOTE: carlton/2003-10-07: Getting the info this way changes
19241 what template types look like, because the demangler
19242 frequently doesn't give the same name as the debug info. We
19243 could fix this by only using the demangled name to get the
19244 prefix (but see comment in read_structure_type). */
19245
19246 struct partial_die_info *real_pdi;
19247 struct partial_die_info *child_pdi;
19248
19249 /* If this DIE (this DIE's specification, if any) has a parent, then
19250 we should not do this. We'll prepend the parent's fully qualified
19251 name when we create the partial symbol. */
19252
19253 real_pdi = struct_pdi;
19254 while (real_pdi->has_specification)
19255 {
19256 auto res = find_partial_die (real_pdi->spec_offset,
19257 real_pdi->spec_is_dwz, cu);
19258 real_pdi = res.pdi;
19259 cu = res.cu;
19260 }
19261
19262 if (real_pdi->die_parent != NULL)
19263 return;
19264
19265 for (child_pdi = struct_pdi->die_child;
19266 child_pdi != NULL;
19267 child_pdi = child_pdi->die_sibling)
19268 {
19269 if (child_pdi->tag == DW_TAG_subprogram
19270 && child_pdi->linkage_name != NULL)
19271 {
19272 gdb::unique_xmalloc_ptr<char> actual_class_name
19273 (language_class_name_from_physname (cu->language_defn,
19274 child_pdi->linkage_name));
19275 if (actual_class_name != NULL)
19276 {
19277 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19278 struct_pdi->name
19279 = obstack_strdup (&objfile->per_bfd->storage_obstack,
19280 actual_class_name.get ());
19281 }
19282 break;
19283 }
19284 }
19285 }
19286
19287 void
19288 partial_die_info::fixup (struct dwarf2_cu *cu)
19289 {
19290 /* Once we've fixed up a die, there's no point in doing so again.
19291 This also avoids a memory leak if we were to call
19292 guess_partial_die_structure_name multiple times. */
19293 if (fixup_called)
19294 return;
19295
19296 /* If we found a reference attribute and the DIE has no name, try
19297 to find a name in the referred to DIE. */
19298
19299 if (name == NULL && has_specification)
19300 {
19301 struct partial_die_info *spec_die;
19302
19303 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
19304 spec_die = res.pdi;
19305 cu = res.cu;
19306
19307 spec_die->fixup (cu);
19308
19309 if (spec_die->name)
19310 {
19311 name = spec_die->name;
19312
19313 /* Copy DW_AT_external attribute if it is set. */
19314 if (spec_die->is_external)
19315 is_external = spec_die->is_external;
19316 }
19317 }
19318
19319 /* Set default names for some unnamed DIEs. */
19320
19321 if (name == NULL && tag == DW_TAG_namespace)
19322 name = CP_ANONYMOUS_NAMESPACE_STR;
19323
19324 /* If there is no parent die to provide a namespace, and there are
19325 children, see if we can determine the namespace from their linkage
19326 name. */
19327 if (cu->language == language_cplus
19328 && !cu->per_cu->dwarf2_per_objfile->types.empty ()
19329 && die_parent == NULL
19330 && has_children
19331 && (tag == DW_TAG_class_type
19332 || tag == DW_TAG_structure_type
19333 || tag == DW_TAG_union_type))
19334 guess_partial_die_structure_name (this, cu);
19335
19336 /* GCC might emit a nameless struct or union that has a linkage
19337 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
19338 if (name == NULL
19339 && (tag == DW_TAG_class_type
19340 || tag == DW_TAG_interface_type
19341 || tag == DW_TAG_structure_type
19342 || tag == DW_TAG_union_type)
19343 && linkage_name != NULL)
19344 {
19345 gdb::unique_xmalloc_ptr<char> demangled
19346 (gdb_demangle (linkage_name, DMGL_TYPES));
19347 if (demangled != nullptr)
19348 {
19349 const char *base;
19350
19351 /* Strip any leading namespaces/classes, keep only the base name.
19352 DW_AT_name for named DIEs does not contain the prefixes. */
19353 base = strrchr (demangled.get (), ':');
19354 if (base && base > demangled.get () && base[-1] == ':')
19355 base++;
19356 else
19357 base = demangled.get ();
19358
19359 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19360 name = obstack_strdup (&objfile->per_bfd->storage_obstack, base);
19361 }
19362 }
19363
19364 fixup_called = 1;
19365 }
19366
19367 /* Process the attributes that had to be skipped in the first round. These
19368 attributes are the ones that need str_offsets_base or addr_base attributes.
19369 They could not have been processed in the first round, because at the time
19370 the values of str_offsets_base or addr_base may not have been known. */
19371 void read_attribute_reprocess (const struct die_reader_specs *reader,
19372 struct attribute *attr)
19373 {
19374 struct dwarf2_cu *cu = reader->cu;
19375 switch (attr->form)
19376 {
19377 case DW_FORM_addrx:
19378 case DW_FORM_GNU_addr_index:
19379 DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
19380 break;
19381 case DW_FORM_strx:
19382 case DW_FORM_strx1:
19383 case DW_FORM_strx2:
19384 case DW_FORM_strx3:
19385 case DW_FORM_strx4:
19386 case DW_FORM_GNU_str_index:
19387 {
19388 unsigned int str_index = DW_UNSND (attr);
19389 if (reader->dwo_file != NULL)
19390 {
19391 DW_STRING (attr) = read_dwo_str_index (reader, str_index);
19392 DW_STRING_IS_CANONICAL (attr) = 0;
19393 }
19394 else
19395 {
19396 DW_STRING (attr) = read_stub_str_index (cu, str_index);
19397 DW_STRING_IS_CANONICAL (attr) = 0;
19398 }
19399 break;
19400 }
19401 default:
19402 gdb_assert_not_reached (_("Unexpected DWARF form."));
19403 }
19404 }
19405
19406 /* Read an attribute value described by an attribute form. */
19407
19408 static const gdb_byte *
19409 read_attribute_value (const struct die_reader_specs *reader,
19410 struct attribute *attr, unsigned form,
19411 LONGEST implicit_const, const gdb_byte *info_ptr,
19412 bool *need_reprocess)
19413 {
19414 struct dwarf2_cu *cu = reader->cu;
19415 struct dwarf2_per_objfile *dwarf2_per_objfile
19416 = cu->per_cu->dwarf2_per_objfile;
19417 struct objfile *objfile = dwarf2_per_objfile->objfile;
19418 struct gdbarch *gdbarch = get_objfile_arch (objfile);
19419 bfd *abfd = reader->abfd;
19420 struct comp_unit_head *cu_header = &cu->header;
19421 unsigned int bytes_read;
19422 struct dwarf_block *blk;
19423 *need_reprocess = false;
19424
19425 attr->form = (enum dwarf_form) form;
19426 switch (form)
19427 {
19428 case DW_FORM_ref_addr:
19429 if (cu->header.version == 2)
19430 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
19431 else
19432 DW_UNSND (attr) = read_offset (abfd, info_ptr,
19433 &cu->header, &bytes_read);
19434 info_ptr += bytes_read;
19435 break;
19436 case DW_FORM_GNU_ref_alt:
19437 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19438 info_ptr += bytes_read;
19439 break;
19440 case DW_FORM_addr:
19441 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
19442 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
19443 info_ptr += bytes_read;
19444 break;
19445 case DW_FORM_block2:
19446 blk = dwarf_alloc_block (cu);
19447 blk->size = read_2_bytes (abfd, info_ptr);
19448 info_ptr += 2;
19449 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19450 info_ptr += blk->size;
19451 DW_BLOCK (attr) = blk;
19452 break;
19453 case DW_FORM_block4:
19454 blk = dwarf_alloc_block (cu);
19455 blk->size = read_4_bytes (abfd, info_ptr);
19456 info_ptr += 4;
19457 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19458 info_ptr += blk->size;
19459 DW_BLOCK (attr) = blk;
19460 break;
19461 case DW_FORM_data2:
19462 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
19463 info_ptr += 2;
19464 break;
19465 case DW_FORM_data4:
19466 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
19467 info_ptr += 4;
19468 break;
19469 case DW_FORM_data8:
19470 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
19471 info_ptr += 8;
19472 break;
19473 case DW_FORM_data16:
19474 blk = dwarf_alloc_block (cu);
19475 blk->size = 16;
19476 blk->data = read_n_bytes (abfd, info_ptr, 16);
19477 info_ptr += 16;
19478 DW_BLOCK (attr) = blk;
19479 break;
19480 case DW_FORM_sec_offset:
19481 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19482 info_ptr += bytes_read;
19483 break;
19484 case DW_FORM_string:
19485 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
19486 DW_STRING_IS_CANONICAL (attr) = 0;
19487 info_ptr += bytes_read;
19488 break;
19489 case DW_FORM_strp:
19490 if (!cu->per_cu->is_dwz)
19491 {
19492 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
19493 abfd, info_ptr, cu_header,
19494 &bytes_read);
19495 DW_STRING_IS_CANONICAL (attr) = 0;
19496 info_ptr += bytes_read;
19497 break;
19498 }
19499 /* FALLTHROUGH */
19500 case DW_FORM_line_strp:
19501 if (!cu->per_cu->is_dwz)
19502 {
19503 DW_STRING (attr) = read_indirect_line_string (dwarf2_per_objfile,
19504 abfd, info_ptr,
19505 cu_header, &bytes_read);
19506 DW_STRING_IS_CANONICAL (attr) = 0;
19507 info_ptr += bytes_read;
19508 break;
19509 }
19510 /* FALLTHROUGH */
19511 case DW_FORM_GNU_strp_alt:
19512 {
19513 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
19514 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
19515 &bytes_read);
19516
19517 DW_STRING (attr) = read_indirect_string_from_dwz (objfile,
19518 dwz, str_offset);
19519 DW_STRING_IS_CANONICAL (attr) = 0;
19520 info_ptr += bytes_read;
19521 }
19522 break;
19523 case DW_FORM_exprloc:
19524 case DW_FORM_block:
19525 blk = dwarf_alloc_block (cu);
19526 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19527 info_ptr += bytes_read;
19528 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19529 info_ptr += blk->size;
19530 DW_BLOCK (attr) = blk;
19531 break;
19532 case DW_FORM_block1:
19533 blk = dwarf_alloc_block (cu);
19534 blk->size = read_1_byte (abfd, info_ptr);
19535 info_ptr += 1;
19536 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19537 info_ptr += blk->size;
19538 DW_BLOCK (attr) = blk;
19539 break;
19540 case DW_FORM_data1:
19541 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19542 info_ptr += 1;
19543 break;
19544 case DW_FORM_flag:
19545 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19546 info_ptr += 1;
19547 break;
19548 case DW_FORM_flag_present:
19549 DW_UNSND (attr) = 1;
19550 break;
19551 case DW_FORM_sdata:
19552 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19553 info_ptr += bytes_read;
19554 break;
19555 case DW_FORM_udata:
19556 case DW_FORM_rnglistx:
19557 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19558 info_ptr += bytes_read;
19559 break;
19560 case DW_FORM_ref1:
19561 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19562 + read_1_byte (abfd, info_ptr));
19563 info_ptr += 1;
19564 break;
19565 case DW_FORM_ref2:
19566 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19567 + read_2_bytes (abfd, info_ptr));
19568 info_ptr += 2;
19569 break;
19570 case DW_FORM_ref4:
19571 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19572 + read_4_bytes (abfd, info_ptr));
19573 info_ptr += 4;
19574 break;
19575 case DW_FORM_ref8:
19576 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19577 + read_8_bytes (abfd, info_ptr));
19578 info_ptr += 8;
19579 break;
19580 case DW_FORM_ref_sig8:
19581 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
19582 info_ptr += 8;
19583 break;
19584 case DW_FORM_ref_udata:
19585 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19586 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
19587 info_ptr += bytes_read;
19588 break;
19589 case DW_FORM_indirect:
19590 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19591 info_ptr += bytes_read;
19592 if (form == DW_FORM_implicit_const)
19593 {
19594 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19595 info_ptr += bytes_read;
19596 }
19597 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19598 info_ptr, need_reprocess);
19599 break;
19600 case DW_FORM_implicit_const:
19601 DW_SND (attr) = implicit_const;
19602 break;
19603 case DW_FORM_addrx:
19604 case DW_FORM_GNU_addr_index:
19605 *need_reprocess = true;
19606 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19607 info_ptr += bytes_read;
19608 break;
19609 case DW_FORM_strx:
19610 case DW_FORM_strx1:
19611 case DW_FORM_strx2:
19612 case DW_FORM_strx3:
19613 case DW_FORM_strx4:
19614 case DW_FORM_GNU_str_index:
19615 {
19616 ULONGEST str_index;
19617 if (form == DW_FORM_strx1)
19618 {
19619 str_index = read_1_byte (abfd, info_ptr);
19620 info_ptr += 1;
19621 }
19622 else if (form == DW_FORM_strx2)
19623 {
19624 str_index = read_2_bytes (abfd, info_ptr);
19625 info_ptr += 2;
19626 }
19627 else if (form == DW_FORM_strx3)
19628 {
19629 str_index = read_3_bytes (abfd, info_ptr);
19630 info_ptr += 3;
19631 }
19632 else if (form == DW_FORM_strx4)
19633 {
19634 str_index = read_4_bytes (abfd, info_ptr);
19635 info_ptr += 4;
19636 }
19637 else
19638 {
19639 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19640 info_ptr += bytes_read;
19641 }
19642 *need_reprocess = true;
19643 DW_UNSND (attr) = str_index;
19644 }
19645 break;
19646 default:
19647 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
19648 dwarf_form_name (form),
19649 bfd_get_filename (abfd));
19650 }
19651
19652 /* Super hack. */
19653 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
19654 attr->form = DW_FORM_GNU_ref_alt;
19655
19656 /* We have seen instances where the compiler tried to emit a byte
19657 size attribute of -1 which ended up being encoded as an unsigned
19658 0xffffffff. Although 0xffffffff is technically a valid size value,
19659 an object of this size seems pretty unlikely so we can relatively
19660 safely treat these cases as if the size attribute was invalid and
19661 treat them as zero by default. */
19662 if (attr->name == DW_AT_byte_size
19663 && form == DW_FORM_data4
19664 && DW_UNSND (attr) >= 0xffffffff)
19665 {
19666 complaint
19667 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19668 hex_string (DW_UNSND (attr)));
19669 DW_UNSND (attr) = 0;
19670 }
19671
19672 return info_ptr;
19673 }
19674
19675 /* Read an attribute described by an abbreviated attribute. */
19676
19677 static const gdb_byte *
19678 read_attribute (const struct die_reader_specs *reader,
19679 struct attribute *attr, struct attr_abbrev *abbrev,
19680 const gdb_byte *info_ptr, bool *need_reprocess)
19681 {
19682 attr->name = abbrev->name;
19683 return read_attribute_value (reader, attr, abbrev->form,
19684 abbrev->implicit_const, info_ptr,
19685 need_reprocess);
19686 }
19687
19688 /* Read dwarf information from a buffer. */
19689
19690 static unsigned int
19691 read_1_byte (bfd *abfd, const gdb_byte *buf)
19692 {
19693 return bfd_get_8 (abfd, buf);
19694 }
19695
19696 static int
19697 read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
19698 {
19699 return bfd_get_signed_8 (abfd, buf);
19700 }
19701
19702 static unsigned int
19703 read_2_bytes (bfd *abfd, const gdb_byte *buf)
19704 {
19705 return bfd_get_16 (abfd, buf);
19706 }
19707
19708 static int
19709 read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
19710 {
19711 return bfd_get_signed_16 (abfd, buf);
19712 }
19713
19714 static unsigned int
19715 read_3_bytes (bfd *abfd, const gdb_byte *buf)
19716 {
19717 unsigned int result = 0;
19718 for (int i = 0; i < 3; ++i)
19719 {
19720 unsigned char byte = bfd_get_8 (abfd, buf);
19721 buf++;
19722 result |= ((unsigned int) byte << (i * 8));
19723 }
19724 return result;
19725 }
19726
19727 static unsigned int
19728 read_4_bytes (bfd *abfd, const gdb_byte *buf)
19729 {
19730 return bfd_get_32 (abfd, buf);
19731 }
19732
19733 static int
19734 read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
19735 {
19736 return bfd_get_signed_32 (abfd, buf);
19737 }
19738
19739 static ULONGEST
19740 read_8_bytes (bfd *abfd, const gdb_byte *buf)
19741 {
19742 return bfd_get_64 (abfd, buf);
19743 }
19744
19745 static CORE_ADDR
19746 read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
19747 unsigned int *bytes_read)
19748 {
19749 struct comp_unit_head *cu_header = &cu->header;
19750 CORE_ADDR retval = 0;
19751
19752 if (cu_header->signed_addr_p)
19753 {
19754 switch (cu_header->addr_size)
19755 {
19756 case 2:
19757 retval = bfd_get_signed_16 (abfd, buf);
19758 break;
19759 case 4:
19760 retval = bfd_get_signed_32 (abfd, buf);
19761 break;
19762 case 8:
19763 retval = bfd_get_signed_64 (abfd, buf);
19764 break;
19765 default:
19766 internal_error (__FILE__, __LINE__,
19767 _("read_address: bad switch, signed [in module %s]"),
19768 bfd_get_filename (abfd));
19769 }
19770 }
19771 else
19772 {
19773 switch (cu_header->addr_size)
19774 {
19775 case 2:
19776 retval = bfd_get_16 (abfd, buf);
19777 break;
19778 case 4:
19779 retval = bfd_get_32 (abfd, buf);
19780 break;
19781 case 8:
19782 retval = bfd_get_64 (abfd, buf);
19783 break;
19784 default:
19785 internal_error (__FILE__, __LINE__,
19786 _("read_address: bad switch, "
19787 "unsigned [in module %s]"),
19788 bfd_get_filename (abfd));
19789 }
19790 }
19791
19792 *bytes_read = cu_header->addr_size;
19793 return retval;
19794 }
19795
19796 /* Read the initial length from a section. The (draft) DWARF 3
19797 specification allows the initial length to take up either 4 bytes
19798 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
19799 bytes describe the length and all offsets will be 8 bytes in length
19800 instead of 4.
19801
19802 An older, non-standard 64-bit format is also handled by this
19803 function. The older format in question stores the initial length
19804 as an 8-byte quantity without an escape value. Lengths greater
19805 than 2^32 aren't very common which means that the initial 4 bytes
19806 is almost always zero. Since a length value of zero doesn't make
19807 sense for the 32-bit format, this initial zero can be considered to
19808 be an escape value which indicates the presence of the older 64-bit
19809 format. As written, the code can't detect (old format) lengths
19810 greater than 4GB. If it becomes necessary to handle lengths
19811 somewhat larger than 4GB, we could allow other small values (such
19812 as the non-sensical values of 1, 2, and 3) to also be used as
19813 escape values indicating the presence of the old format.
19814
19815 The value returned via bytes_read should be used to increment the
19816 relevant pointer after calling read_initial_length().
19817
19818 [ Note: read_initial_length() and read_offset() are based on the
19819 document entitled "DWARF Debugging Information Format", revision
19820 3, draft 8, dated November 19, 2001. This document was obtained
19821 from:
19822
19823 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
19824
19825 This document is only a draft and is subject to change. (So beware.)
19826
19827 Details regarding the older, non-standard 64-bit format were
19828 determined empirically by examining 64-bit ELF files produced by
19829 the SGI toolchain on an IRIX 6.5 machine.
19830
19831 - Kevin, July 16, 2002
19832 ] */
19833
19834 static LONGEST
19835 read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
19836 {
19837 LONGEST length = bfd_get_32 (abfd, buf);
19838
19839 if (length == 0xffffffff)
19840 {
19841 length = bfd_get_64 (abfd, buf + 4);
19842 *bytes_read = 12;
19843 }
19844 else if (length == 0)
19845 {
19846 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
19847 length = bfd_get_64 (abfd, buf);
19848 *bytes_read = 8;
19849 }
19850 else
19851 {
19852 *bytes_read = 4;
19853 }
19854
19855 return length;
19856 }
19857
19858 /* Cover function for read_initial_length.
19859 Returns the length of the object at BUF, and stores the size of the
19860 initial length in *BYTES_READ and stores the size that offsets will be in
19861 *OFFSET_SIZE.
19862 If the initial length size is not equivalent to that specified in
19863 CU_HEADER then issue a complaint.
19864 This is useful when reading non-comp-unit headers. */
19865
19866 static LONGEST
19867 read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
19868 const struct comp_unit_head *cu_header,
19869 unsigned int *bytes_read,
19870 unsigned int *offset_size)
19871 {
19872 LONGEST length = read_initial_length (abfd, buf, bytes_read);
19873
19874 gdb_assert (cu_header->initial_length_size == 4
19875 || cu_header->initial_length_size == 8
19876 || cu_header->initial_length_size == 12);
19877
19878 if (cu_header->initial_length_size != *bytes_read)
19879 complaint (_("intermixed 32-bit and 64-bit DWARF sections"));
19880
19881 *offset_size = (*bytes_read == 4) ? 4 : 8;
19882 return length;
19883 }
19884
19885 /* Read an offset from the data stream. The size of the offset is
19886 given by cu_header->offset_size. */
19887
19888 static LONGEST
19889 read_offset (bfd *abfd, const gdb_byte *buf,
19890 const struct comp_unit_head *cu_header,
19891 unsigned int *bytes_read)
19892 {
19893 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
19894
19895 *bytes_read = cu_header->offset_size;
19896 return offset;
19897 }
19898
19899 /* Read an offset from the data stream. */
19900
19901 static LONGEST
19902 read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
19903 {
19904 LONGEST retval = 0;
19905
19906 switch (offset_size)
19907 {
19908 case 4:
19909 retval = bfd_get_32 (abfd, buf);
19910 break;
19911 case 8:
19912 retval = bfd_get_64 (abfd, buf);
19913 break;
19914 default:
19915 internal_error (__FILE__, __LINE__,
19916 _("read_offset_1: bad switch [in module %s]"),
19917 bfd_get_filename (abfd));
19918 }
19919
19920 return retval;
19921 }
19922
19923 static const gdb_byte *
19924 read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
19925 {
19926 /* If the size of a host char is 8 bits, we can return a pointer
19927 to the buffer, otherwise we have to copy the data to a buffer
19928 allocated on the temporary obstack. */
19929 gdb_assert (HOST_CHAR_BIT == 8);
19930 return buf;
19931 }
19932
19933 static const char *
19934 read_direct_string (bfd *abfd, const gdb_byte *buf,
19935 unsigned int *bytes_read_ptr)
19936 {
19937 /* If the size of a host char is 8 bits, we can return a pointer
19938 to the string, otherwise we have to copy the string to a buffer
19939 allocated on the temporary obstack. */
19940 gdb_assert (HOST_CHAR_BIT == 8);
19941 if (*buf == '\0')
19942 {
19943 *bytes_read_ptr = 1;
19944 return NULL;
19945 }
19946 *bytes_read_ptr = strlen ((const char *) buf) + 1;
19947 return (const char *) buf;
19948 }
19949
19950 /* Return pointer to string at section SECT offset STR_OFFSET with error
19951 reporting strings FORM_NAME and SECT_NAME. */
19952
19953 static const char *
19954 read_indirect_string_at_offset_from (struct objfile *objfile,
19955 bfd *abfd, LONGEST str_offset,
19956 struct dwarf2_section_info *sect,
19957 const char *form_name,
19958 const char *sect_name)
19959 {
19960 dwarf2_read_section (objfile, sect);
19961 if (sect->buffer == NULL)
19962 error (_("%s used without %s section [in module %s]"),
19963 form_name, sect_name, bfd_get_filename (abfd));
19964 if (str_offset >= sect->size)
19965 error (_("%s pointing outside of %s section [in module %s]"),
19966 form_name, sect_name, bfd_get_filename (abfd));
19967 gdb_assert (HOST_CHAR_BIT == 8);
19968 if (sect->buffer[str_offset] == '\0')
19969 return NULL;
19970 return (const char *) (sect->buffer + str_offset);
19971 }
19972
19973 /* Return pointer to string at .debug_str offset STR_OFFSET. */
19974
19975 static const char *
19976 read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19977 bfd *abfd, LONGEST str_offset)
19978 {
19979 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19980 abfd, str_offset,
19981 &dwarf2_per_objfile->str,
19982 "DW_FORM_strp", ".debug_str");
19983 }
19984
19985 /* Return pointer to string at .debug_line_str offset STR_OFFSET. */
19986
19987 static const char *
19988 read_indirect_line_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19989 bfd *abfd, LONGEST str_offset)
19990 {
19991 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19992 abfd, str_offset,
19993 &dwarf2_per_objfile->line_str,
19994 "DW_FORM_line_strp",
19995 ".debug_line_str");
19996 }
19997
19998 /* Read a string at offset STR_OFFSET in the .debug_str section from
19999 the .dwz file DWZ. Throw an error if the offset is too large. If
20000 the string consists of a single NUL byte, return NULL; otherwise
20001 return a pointer to the string. */
20002
20003 static const char *
20004 read_indirect_string_from_dwz (struct objfile *objfile, struct dwz_file *dwz,
20005 LONGEST str_offset)
20006 {
20007 dwarf2_read_section (objfile, &dwz->str);
20008
20009 if (dwz->str.buffer == NULL)
20010 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
20011 "section [in module %s]"),
20012 bfd_get_filename (dwz->dwz_bfd.get ()));
20013 if (str_offset >= dwz->str.size)
20014 error (_("DW_FORM_GNU_strp_alt pointing outside of "
20015 ".debug_str section [in module %s]"),
20016 bfd_get_filename (dwz->dwz_bfd.get ()));
20017 gdb_assert (HOST_CHAR_BIT == 8);
20018 if (dwz->str.buffer[str_offset] == '\0')
20019 return NULL;
20020 return (const char *) (dwz->str.buffer + str_offset);
20021 }
20022
20023 /* Return pointer to string at .debug_str offset as read from BUF.
20024 BUF is assumed to be in a compilation unit described by CU_HEADER.
20025 Return *BYTES_READ_PTR count of bytes read from BUF. */
20026
20027 static const char *
20028 read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
20029 const gdb_byte *buf,
20030 const struct comp_unit_head *cu_header,
20031 unsigned int *bytes_read_ptr)
20032 {
20033 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
20034
20035 return read_indirect_string_at_offset (dwarf2_per_objfile, abfd, str_offset);
20036 }
20037
20038 /* Return pointer to string at .debug_line_str offset as read from BUF.
20039 BUF is assumed to be in a compilation unit described by CU_HEADER.
20040 Return *BYTES_READ_PTR count of bytes read from BUF. */
20041
20042 static const char *
20043 read_indirect_line_string (struct dwarf2_per_objfile *dwarf2_per_objfile,
20044 bfd *abfd, const gdb_byte *buf,
20045 const struct comp_unit_head *cu_header,
20046 unsigned int *bytes_read_ptr)
20047 {
20048 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
20049
20050 return read_indirect_line_string_at_offset (dwarf2_per_objfile, abfd,
20051 str_offset);
20052 }
20053
20054 ULONGEST
20055 read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
20056 unsigned int *bytes_read_ptr)
20057 {
20058 ULONGEST result;
20059 unsigned int num_read;
20060 int shift;
20061 unsigned char byte;
20062
20063 result = 0;
20064 shift = 0;
20065 num_read = 0;
20066 while (1)
20067 {
20068 byte = bfd_get_8 (abfd, buf);
20069 buf++;
20070 num_read++;
20071 result |= ((ULONGEST) (byte & 127) << shift);
20072 if ((byte & 128) == 0)
20073 {
20074 break;
20075 }
20076 shift += 7;
20077 }
20078 *bytes_read_ptr = num_read;
20079 return result;
20080 }
20081
20082 static LONGEST
20083 read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
20084 unsigned int *bytes_read_ptr)
20085 {
20086 ULONGEST result;
20087 int shift, num_read;
20088 unsigned char byte;
20089
20090 result = 0;
20091 shift = 0;
20092 num_read = 0;
20093 while (1)
20094 {
20095 byte = bfd_get_8 (abfd, buf);
20096 buf++;
20097 num_read++;
20098 result |= ((ULONGEST) (byte & 127) << shift);
20099 shift += 7;
20100 if ((byte & 128) == 0)
20101 {
20102 break;
20103 }
20104 }
20105 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
20106 result |= -(((ULONGEST) 1) << shift);
20107 *bytes_read_ptr = num_read;
20108 return result;
20109 }
20110
20111 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
20112 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
20113 ADDR_SIZE is the size of addresses from the CU header. */
20114
20115 static CORE_ADDR
20116 read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
20117 unsigned int addr_index, gdb::optional<ULONGEST> addr_base,
20118 int addr_size)
20119 {
20120 struct objfile *objfile = dwarf2_per_objfile->objfile;
20121 bfd *abfd = objfile->obfd;
20122 const gdb_byte *info_ptr;
20123 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
20124
20125 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
20126 if (dwarf2_per_objfile->addr.buffer == NULL)
20127 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
20128 objfile_name (objfile));
20129 if (addr_base_or_zero + addr_index * addr_size
20130 >= dwarf2_per_objfile->addr.size)
20131 error (_("DW_FORM_addr_index pointing outside of "
20132 ".debug_addr section [in module %s]"),
20133 objfile_name (objfile));
20134 info_ptr = (dwarf2_per_objfile->addr.buffer
20135 + addr_base_or_zero + addr_index * addr_size);
20136 if (addr_size == 4)
20137 return bfd_get_32 (abfd, info_ptr);
20138 else
20139 return bfd_get_64 (abfd, info_ptr);
20140 }
20141
20142 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
20143
20144 static CORE_ADDR
20145 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
20146 {
20147 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
20148 cu->addr_base, cu->header.addr_size);
20149 }
20150
20151 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
20152
20153 static CORE_ADDR
20154 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
20155 unsigned int *bytes_read)
20156 {
20157 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
20158 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
20159
20160 return read_addr_index (cu, addr_index);
20161 }
20162
20163 /* Given an index in .debug_addr, fetch the value.
20164 NOTE: This can be called during dwarf expression evaluation,
20165 long after the debug information has been read, and thus per_cu->cu
20166 may no longer exist. */
20167
20168 CORE_ADDR
20169 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
20170 unsigned int addr_index)
20171 {
20172 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
20173 struct dwarf2_cu *cu = per_cu->cu;
20174 gdb::optional<ULONGEST> addr_base;
20175 int addr_size;
20176
20177 /* We need addr_base and addr_size.
20178 If we don't have PER_CU->cu, we have to get it.
20179 Nasty, but the alternative is storing the needed info in PER_CU,
20180 which at this point doesn't seem justified: it's not clear how frequently
20181 it would get used and it would increase the size of every PER_CU.
20182 Entry points like dwarf2_per_cu_addr_size do a similar thing
20183 so we're not in uncharted territory here.
20184 Alas we need to be a bit more complicated as addr_base is contained
20185 in the DIE.
20186
20187 We don't need to read the entire CU(/TU).
20188 We just need the header and top level die.
20189
20190 IWBN to use the aging mechanism to let us lazily later discard the CU.
20191 For now we skip this optimization. */
20192
20193 if (cu != NULL)
20194 {
20195 addr_base = cu->addr_base;
20196 addr_size = cu->header.addr_size;
20197 }
20198 else
20199 {
20200 cutu_reader reader (per_cu, NULL, 0, 0, false);
20201 addr_base = reader.cu->addr_base;
20202 addr_size = reader.cu->header.addr_size;
20203 }
20204
20205 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
20206 addr_size);
20207 }
20208
20209 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
20210 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
20211 DWO file. */
20212
20213 static const char *
20214 read_str_index (struct dwarf2_cu *cu,
20215 struct dwarf2_section_info *str_section,
20216 struct dwarf2_section_info *str_offsets_section,
20217 ULONGEST str_offsets_base, ULONGEST str_index)
20218 {
20219 struct dwarf2_per_objfile *dwarf2_per_objfile
20220 = cu->per_cu->dwarf2_per_objfile;
20221 struct objfile *objfile = dwarf2_per_objfile->objfile;
20222 const char *objf_name = objfile_name (objfile);
20223 bfd *abfd = objfile->obfd;
20224 const gdb_byte *info_ptr;
20225 ULONGEST str_offset;
20226 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
20227
20228 dwarf2_read_section (objfile, str_section);
20229 dwarf2_read_section (objfile, str_offsets_section);
20230 if (str_section->buffer == NULL)
20231 error (_("%s used without %s section"
20232 " in CU at offset %s [in module %s]"),
20233 form_name, get_section_name (str_section),
20234 sect_offset_str (cu->header.sect_off), objf_name);
20235 if (str_offsets_section->buffer == NULL)
20236 error (_("%s used without %s section"
20237 " in CU at offset %s [in module %s]"),
20238 form_name, get_section_name (str_section),
20239 sect_offset_str (cu->header.sect_off), objf_name);
20240 info_ptr = (str_offsets_section->buffer
20241 + str_offsets_base
20242 + str_index * cu->header.offset_size);
20243 if (cu->header.offset_size == 4)
20244 str_offset = bfd_get_32 (abfd, info_ptr);
20245 else
20246 str_offset = bfd_get_64 (abfd, info_ptr);
20247 if (str_offset >= str_section->size)
20248 error (_("Offset from %s pointing outside of"
20249 " .debug_str.dwo section in CU at offset %s [in module %s]"),
20250 form_name, sect_offset_str (cu->header.sect_off), objf_name);
20251 return (const char *) (str_section->buffer + str_offset);
20252 }
20253
20254 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
20255
20256 static const char *
20257 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
20258 {
20259 ULONGEST str_offsets_base = reader->cu->header.version >= 5
20260 ? reader->cu->header.addr_size : 0;
20261 return read_str_index (reader->cu,
20262 &reader->dwo_file->sections.str,
20263 &reader->dwo_file->sections.str_offsets,
20264 str_offsets_base, str_index);
20265 }
20266
20267 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
20268
20269 static const char *
20270 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
20271 {
20272 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20273 const char *objf_name = objfile_name (objfile);
20274 static const char form_name[] = "DW_FORM_GNU_str_index";
20275 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
20276
20277 if (!cu->str_offsets_base.has_value ())
20278 error (_("%s used in Fission stub without %s"
20279 " in CU at offset 0x%lx [in module %s]"),
20280 form_name, str_offsets_attr_name,
20281 (long) cu->header.offset_size, objf_name);
20282
20283 return read_str_index (cu,
20284 &cu->per_cu->dwarf2_per_objfile->str,
20285 &cu->per_cu->dwarf2_per_objfile->str_offsets,
20286 *cu->str_offsets_base, str_index);
20287 }
20288
20289 /* Return the length of an LEB128 number in BUF. */
20290
20291 static int
20292 leb128_size (const gdb_byte *buf)
20293 {
20294 const gdb_byte *begin = buf;
20295 gdb_byte byte;
20296
20297 while (1)
20298 {
20299 byte = *buf++;
20300 if ((byte & 128) == 0)
20301 return buf - begin;
20302 }
20303 }
20304
20305 static void
20306 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
20307 {
20308 switch (lang)
20309 {
20310 case DW_LANG_C89:
20311 case DW_LANG_C99:
20312 case DW_LANG_C11:
20313 case DW_LANG_C:
20314 case DW_LANG_UPC:
20315 cu->language = language_c;
20316 break;
20317 case DW_LANG_Java:
20318 case DW_LANG_C_plus_plus:
20319 case DW_LANG_C_plus_plus_11:
20320 case DW_LANG_C_plus_plus_14:
20321 cu->language = language_cplus;
20322 break;
20323 case DW_LANG_D:
20324 cu->language = language_d;
20325 break;
20326 case DW_LANG_Fortran77:
20327 case DW_LANG_Fortran90:
20328 case DW_LANG_Fortran95:
20329 case DW_LANG_Fortran03:
20330 case DW_LANG_Fortran08:
20331 cu->language = language_fortran;
20332 break;
20333 case DW_LANG_Go:
20334 cu->language = language_go;
20335 break;
20336 case DW_LANG_Mips_Assembler:
20337 cu->language = language_asm;
20338 break;
20339 case DW_LANG_Ada83:
20340 case DW_LANG_Ada95:
20341 cu->language = language_ada;
20342 break;
20343 case DW_LANG_Modula2:
20344 cu->language = language_m2;
20345 break;
20346 case DW_LANG_Pascal83:
20347 cu->language = language_pascal;
20348 break;
20349 case DW_LANG_ObjC:
20350 cu->language = language_objc;
20351 break;
20352 case DW_LANG_Rust:
20353 case DW_LANG_Rust_old:
20354 cu->language = language_rust;
20355 break;
20356 case DW_LANG_Cobol74:
20357 case DW_LANG_Cobol85:
20358 default:
20359 cu->language = language_minimal;
20360 break;
20361 }
20362 cu->language_defn = language_def (cu->language);
20363 }
20364
20365 /* Return the named attribute or NULL if not there. */
20366
20367 static struct attribute *
20368 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20369 {
20370 for (;;)
20371 {
20372 unsigned int i;
20373 struct attribute *spec = NULL;
20374
20375 for (i = 0; i < die->num_attrs; ++i)
20376 {
20377 if (die->attrs[i].name == name)
20378 return &die->attrs[i];
20379 if (die->attrs[i].name == DW_AT_specification
20380 || die->attrs[i].name == DW_AT_abstract_origin)
20381 spec = &die->attrs[i];
20382 }
20383
20384 if (!spec)
20385 break;
20386
20387 die = follow_die_ref (die, spec, &cu);
20388 }
20389
20390 return NULL;
20391 }
20392
20393 /* Return the named attribute or NULL if not there,
20394 but do not follow DW_AT_specification, etc.
20395 This is for use in contexts where we're reading .debug_types dies.
20396 Following DW_AT_specification, DW_AT_abstract_origin will take us
20397 back up the chain, and we want to go down. */
20398
20399 static struct attribute *
20400 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
20401 {
20402 unsigned int i;
20403
20404 for (i = 0; i < die->num_attrs; ++i)
20405 if (die->attrs[i].name == name)
20406 return &die->attrs[i];
20407
20408 return NULL;
20409 }
20410
20411 /* Return the string associated with a string-typed attribute, or NULL if it
20412 is either not found or is of an incorrect type. */
20413
20414 static const char *
20415 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20416 {
20417 struct attribute *attr;
20418 const char *str = NULL;
20419
20420 attr = dwarf2_attr (die, name, cu);
20421
20422 if (attr != NULL)
20423 {
20424 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
20425 || attr->form == DW_FORM_string
20426 || attr->form == DW_FORM_strx
20427 || attr->form == DW_FORM_strx1
20428 || attr->form == DW_FORM_strx2
20429 || attr->form == DW_FORM_strx3
20430 || attr->form == DW_FORM_strx4
20431 || attr->form == DW_FORM_GNU_str_index
20432 || attr->form == DW_FORM_GNU_strp_alt)
20433 str = DW_STRING (attr);
20434 else
20435 complaint (_("string type expected for attribute %s for "
20436 "DIE at %s in module %s"),
20437 dwarf_attr_name (name), sect_offset_str (die->sect_off),
20438 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
20439 }
20440
20441 return str;
20442 }
20443
20444 /* Return the dwo name or NULL if not present. If present, it is in either
20445 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
20446 static const char *
20447 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
20448 {
20449 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
20450 if (dwo_name == nullptr)
20451 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
20452 return dwo_name;
20453 }
20454
20455 /* Return non-zero iff the attribute NAME is defined for the given DIE,
20456 and holds a non-zero value. This function should only be used for
20457 DW_FORM_flag or DW_FORM_flag_present attributes. */
20458
20459 static int
20460 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
20461 {
20462 struct attribute *attr = dwarf2_attr (die, name, cu);
20463
20464 return (attr && DW_UNSND (attr));
20465 }
20466
20467 static int
20468 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
20469 {
20470 /* A DIE is a declaration if it has a DW_AT_declaration attribute
20471 which value is non-zero. However, we have to be careful with
20472 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
20473 (via dwarf2_flag_true_p) follows this attribute. So we may
20474 end up accidently finding a declaration attribute that belongs
20475 to a different DIE referenced by the specification attribute,
20476 even though the given DIE does not have a declaration attribute. */
20477 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
20478 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
20479 }
20480
20481 /* Return the die giving the specification for DIE, if there is
20482 one. *SPEC_CU is the CU containing DIE on input, and the CU
20483 containing the return value on output. If there is no
20484 specification, but there is an abstract origin, that is
20485 returned. */
20486
20487 static struct die_info *
20488 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
20489 {
20490 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
20491 *spec_cu);
20492
20493 if (spec_attr == NULL)
20494 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
20495
20496 if (spec_attr == NULL)
20497 return NULL;
20498 else
20499 return follow_die_ref (die, spec_attr, spec_cu);
20500 }
20501
20502 /* Stub for free_line_header to match void * callback types. */
20503
20504 static void
20505 free_line_header_voidp (void *arg)
20506 {
20507 struct line_header *lh = (struct line_header *) arg;
20508
20509 delete lh;
20510 }
20511
20512 void
20513 line_header::add_include_dir (const char *include_dir)
20514 {
20515 if (dwarf_line_debug >= 2)
20516 {
20517 size_t new_size;
20518 if (version >= 5)
20519 new_size = m_include_dirs.size ();
20520 else
20521 new_size = m_include_dirs.size () + 1;
20522 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
20523 new_size, include_dir);
20524 }
20525 m_include_dirs.push_back (include_dir);
20526 }
20527
20528 void
20529 line_header::add_file_name (const char *name,
20530 dir_index d_index,
20531 unsigned int mod_time,
20532 unsigned int length)
20533 {
20534 if (dwarf_line_debug >= 2)
20535 {
20536 size_t new_size;
20537 if (version >= 5)
20538 new_size = file_names_size ();
20539 else
20540 new_size = file_names_size () + 1;
20541 fprintf_unfiltered (gdb_stdlog, "Adding file %zu: %s\n",
20542 new_size, name);
20543 }
20544 m_file_names.emplace_back (name, d_index, mod_time, length);
20545 }
20546
20547 /* A convenience function to find the proper .debug_line section for a CU. */
20548
20549 static struct dwarf2_section_info *
20550 get_debug_line_section (struct dwarf2_cu *cu)
20551 {
20552 struct dwarf2_section_info *section;
20553 struct dwarf2_per_objfile *dwarf2_per_objfile
20554 = cu->per_cu->dwarf2_per_objfile;
20555
20556 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
20557 DWO file. */
20558 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20559 section = &cu->dwo_unit->dwo_file->sections.line;
20560 else if (cu->per_cu->is_dwz)
20561 {
20562 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
20563
20564 section = &dwz->line;
20565 }
20566 else
20567 section = &dwarf2_per_objfile->line;
20568
20569 return section;
20570 }
20571
20572 /* Read directory or file name entry format, starting with byte of
20573 format count entries, ULEB128 pairs of entry formats, ULEB128 of
20574 entries count and the entries themselves in the described entry
20575 format. */
20576
20577 static void
20578 read_formatted_entries (struct dwarf2_per_objfile *dwarf2_per_objfile,
20579 bfd *abfd, const gdb_byte **bufp,
20580 struct line_header *lh,
20581 const struct comp_unit_head *cu_header,
20582 void (*callback) (struct line_header *lh,
20583 const char *name,
20584 dir_index d_index,
20585 unsigned int mod_time,
20586 unsigned int length))
20587 {
20588 gdb_byte format_count, formati;
20589 ULONGEST data_count, datai;
20590 const gdb_byte *buf = *bufp;
20591 const gdb_byte *format_header_data;
20592 unsigned int bytes_read;
20593
20594 format_count = read_1_byte (abfd, buf);
20595 buf += 1;
20596 format_header_data = buf;
20597 for (formati = 0; formati < format_count; formati++)
20598 {
20599 read_unsigned_leb128 (abfd, buf, &bytes_read);
20600 buf += bytes_read;
20601 read_unsigned_leb128 (abfd, buf, &bytes_read);
20602 buf += bytes_read;
20603 }
20604
20605 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
20606 buf += bytes_read;
20607 for (datai = 0; datai < data_count; datai++)
20608 {
20609 const gdb_byte *format = format_header_data;
20610 struct file_entry fe;
20611
20612 for (formati = 0; formati < format_count; formati++)
20613 {
20614 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
20615 format += bytes_read;
20616
20617 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
20618 format += bytes_read;
20619
20620 gdb::optional<const char *> string;
20621 gdb::optional<unsigned int> uint;
20622
20623 switch (form)
20624 {
20625 case DW_FORM_string:
20626 string.emplace (read_direct_string (abfd, buf, &bytes_read));
20627 buf += bytes_read;
20628 break;
20629
20630 case DW_FORM_line_strp:
20631 string.emplace (read_indirect_line_string (dwarf2_per_objfile,
20632 abfd, buf,
20633 cu_header,
20634 &bytes_read));
20635 buf += bytes_read;
20636 break;
20637
20638 case DW_FORM_data1:
20639 uint.emplace (read_1_byte (abfd, buf));
20640 buf += 1;
20641 break;
20642
20643 case DW_FORM_data2:
20644 uint.emplace (read_2_bytes (abfd, buf));
20645 buf += 2;
20646 break;
20647
20648 case DW_FORM_data4:
20649 uint.emplace (read_4_bytes (abfd, buf));
20650 buf += 4;
20651 break;
20652
20653 case DW_FORM_data8:
20654 uint.emplace (read_8_bytes (abfd, buf));
20655 buf += 8;
20656 break;
20657
20658 case DW_FORM_data16:
20659 /* This is used for MD5, but file_entry does not record MD5s. */
20660 buf += 16;
20661 break;
20662
20663 case DW_FORM_udata:
20664 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
20665 buf += bytes_read;
20666 break;
20667
20668 case DW_FORM_block:
20669 /* It is valid only for DW_LNCT_timestamp which is ignored by
20670 current GDB. */
20671 break;
20672 }
20673
20674 switch (content_type)
20675 {
20676 case DW_LNCT_path:
20677 if (string.has_value ())
20678 fe.name = *string;
20679 break;
20680 case DW_LNCT_directory_index:
20681 if (uint.has_value ())
20682 fe.d_index = (dir_index) *uint;
20683 break;
20684 case DW_LNCT_timestamp:
20685 if (uint.has_value ())
20686 fe.mod_time = *uint;
20687 break;
20688 case DW_LNCT_size:
20689 if (uint.has_value ())
20690 fe.length = *uint;
20691 break;
20692 case DW_LNCT_MD5:
20693 break;
20694 default:
20695 complaint (_("Unknown format content type %s"),
20696 pulongest (content_type));
20697 }
20698 }
20699
20700 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
20701 }
20702
20703 *bufp = buf;
20704 }
20705
20706 /* Read the statement program header starting at OFFSET in
20707 .debug_line, or .debug_line.dwo. Return a pointer
20708 to a struct line_header, allocated using xmalloc.
20709 Returns NULL if there is a problem reading the header, e.g., if it
20710 has a version we don't understand.
20711
20712 NOTE: the strings in the include directory and file name tables of
20713 the returned object point into the dwarf line section buffer,
20714 and must not be freed. */
20715
20716 static line_header_up
20717 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
20718 {
20719 const gdb_byte *line_ptr;
20720 unsigned int bytes_read, offset_size;
20721 int i;
20722 const char *cur_dir, *cur_file;
20723 struct dwarf2_section_info *section;
20724 bfd *abfd;
20725 struct dwarf2_per_objfile *dwarf2_per_objfile
20726 = cu->per_cu->dwarf2_per_objfile;
20727
20728 section = get_debug_line_section (cu);
20729 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
20730 if (section->buffer == NULL)
20731 {
20732 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20733 complaint (_("missing .debug_line.dwo section"));
20734 else
20735 complaint (_("missing .debug_line section"));
20736 return 0;
20737 }
20738
20739 /* We can't do this until we know the section is non-empty.
20740 Only then do we know we have such a section. */
20741 abfd = get_section_bfd_owner (section);
20742
20743 /* Make sure that at least there's room for the total_length field.
20744 That could be 12 bytes long, but we're just going to fudge that. */
20745 if (to_underlying (sect_off) + 4 >= section->size)
20746 {
20747 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20748 return 0;
20749 }
20750
20751 line_header_up lh (new line_header ());
20752
20753 lh->sect_off = sect_off;
20754 lh->offset_in_dwz = cu->per_cu->is_dwz;
20755
20756 line_ptr = section->buffer + to_underlying (sect_off);
20757
20758 /* Read in the header. */
20759 lh->total_length =
20760 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
20761 &bytes_read, &offset_size);
20762 line_ptr += bytes_read;
20763
20764 const gdb_byte *start_here = line_ptr;
20765
20766 if (line_ptr + lh->total_length > (section->buffer + section->size))
20767 {
20768 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20769 return 0;
20770 }
20771 lh->statement_program_end = start_here + lh->total_length;
20772 lh->version = read_2_bytes (abfd, line_ptr);
20773 line_ptr += 2;
20774 if (lh->version > 5)
20775 {
20776 /* This is a version we don't understand. The format could have
20777 changed in ways we don't handle properly so just punt. */
20778 complaint (_("unsupported version in .debug_line section"));
20779 return NULL;
20780 }
20781 if (lh->version >= 5)
20782 {
20783 gdb_byte segment_selector_size;
20784
20785 /* Skip address size. */
20786 read_1_byte (abfd, line_ptr);
20787 line_ptr += 1;
20788
20789 segment_selector_size = read_1_byte (abfd, line_ptr);
20790 line_ptr += 1;
20791 if (segment_selector_size != 0)
20792 {
20793 complaint (_("unsupported segment selector size %u "
20794 "in .debug_line section"),
20795 segment_selector_size);
20796 return NULL;
20797 }
20798 }
20799 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
20800 line_ptr += offset_size;
20801 lh->statement_program_start = line_ptr + lh->header_length;
20802 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
20803 line_ptr += 1;
20804 if (lh->version >= 4)
20805 {
20806 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
20807 line_ptr += 1;
20808 }
20809 else
20810 lh->maximum_ops_per_instruction = 1;
20811
20812 if (lh->maximum_ops_per_instruction == 0)
20813 {
20814 lh->maximum_ops_per_instruction = 1;
20815 complaint (_("invalid maximum_ops_per_instruction "
20816 "in `.debug_line' section"));
20817 }
20818
20819 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
20820 line_ptr += 1;
20821 lh->line_base = read_1_signed_byte (abfd, line_ptr);
20822 line_ptr += 1;
20823 lh->line_range = read_1_byte (abfd, line_ptr);
20824 line_ptr += 1;
20825 lh->opcode_base = read_1_byte (abfd, line_ptr);
20826 line_ptr += 1;
20827 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
20828
20829 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
20830 for (i = 1; i < lh->opcode_base; ++i)
20831 {
20832 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
20833 line_ptr += 1;
20834 }
20835
20836 if (lh->version >= 5)
20837 {
20838 /* Read directory table. */
20839 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20840 &cu->header,
20841 [] (struct line_header *header, const char *name,
20842 dir_index d_index, unsigned int mod_time,
20843 unsigned int length)
20844 {
20845 header->add_include_dir (name);
20846 });
20847
20848 /* Read file name table. */
20849 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20850 &cu->header,
20851 [] (struct line_header *header, const char *name,
20852 dir_index d_index, unsigned int mod_time,
20853 unsigned int length)
20854 {
20855 header->add_file_name (name, d_index, mod_time, length);
20856 });
20857 }
20858 else
20859 {
20860 /* Read directory table. */
20861 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20862 {
20863 line_ptr += bytes_read;
20864 lh->add_include_dir (cur_dir);
20865 }
20866 line_ptr += bytes_read;
20867
20868 /* Read file name table. */
20869 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20870 {
20871 unsigned int mod_time, length;
20872 dir_index d_index;
20873
20874 line_ptr += bytes_read;
20875 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20876 line_ptr += bytes_read;
20877 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20878 line_ptr += bytes_read;
20879 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20880 line_ptr += bytes_read;
20881
20882 lh->add_file_name (cur_file, d_index, mod_time, length);
20883 }
20884 line_ptr += bytes_read;
20885 }
20886
20887 if (line_ptr > (section->buffer + section->size))
20888 complaint (_("line number info header doesn't "
20889 "fit in `.debug_line' section"));
20890
20891 return lh;
20892 }
20893
20894 /* Subroutine of dwarf_decode_lines to simplify it.
20895 Return the file name of the psymtab for the given file_entry.
20896 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20897 If space for the result is malloc'd, *NAME_HOLDER will be set.
20898 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
20899
20900 static const char *
20901 psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
20902 const dwarf2_psymtab *pst,
20903 const char *comp_dir,
20904 gdb::unique_xmalloc_ptr<char> *name_holder)
20905 {
20906 const char *include_name = fe.name;
20907 const char *include_name_to_compare = include_name;
20908 const char *pst_filename;
20909 int file_is_pst;
20910
20911 const char *dir_name = fe.include_dir (lh);
20912
20913 gdb::unique_xmalloc_ptr<char> hold_compare;
20914 if (!IS_ABSOLUTE_PATH (include_name)
20915 && (dir_name != NULL || comp_dir != NULL))
20916 {
20917 /* Avoid creating a duplicate psymtab for PST.
20918 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20919 Before we do the comparison, however, we need to account
20920 for DIR_NAME and COMP_DIR.
20921 First prepend dir_name (if non-NULL). If we still don't
20922 have an absolute path prepend comp_dir (if non-NULL).
20923 However, the directory we record in the include-file's
20924 psymtab does not contain COMP_DIR (to match the
20925 corresponding symtab(s)).
20926
20927 Example:
20928
20929 bash$ cd /tmp
20930 bash$ gcc -g ./hello.c
20931 include_name = "hello.c"
20932 dir_name = "."
20933 DW_AT_comp_dir = comp_dir = "/tmp"
20934 DW_AT_name = "./hello.c"
20935
20936 */
20937
20938 if (dir_name != NULL)
20939 {
20940 name_holder->reset (concat (dir_name, SLASH_STRING,
20941 include_name, (char *) NULL));
20942 include_name = name_holder->get ();
20943 include_name_to_compare = include_name;
20944 }
20945 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20946 {
20947 hold_compare.reset (concat (comp_dir, SLASH_STRING,
20948 include_name, (char *) NULL));
20949 include_name_to_compare = hold_compare.get ();
20950 }
20951 }
20952
20953 pst_filename = pst->filename;
20954 gdb::unique_xmalloc_ptr<char> copied_name;
20955 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20956 {
20957 copied_name.reset (concat (pst->dirname, SLASH_STRING,
20958 pst_filename, (char *) NULL));
20959 pst_filename = copied_name.get ();
20960 }
20961
20962 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
20963
20964 if (file_is_pst)
20965 return NULL;
20966 return include_name;
20967 }
20968
20969 /* State machine to track the state of the line number program. */
20970
20971 class lnp_state_machine
20972 {
20973 public:
20974 /* Initialize a machine state for the start of a line number
20975 program. */
20976 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
20977 bool record_lines_p);
20978
20979 file_entry *current_file ()
20980 {
20981 /* lh->file_names is 0-based, but the file name numbers in the
20982 statement program are 1-based. */
20983 return m_line_header->file_name_at (m_file);
20984 }
20985
20986 /* Record the line in the state machine. END_SEQUENCE is true if
20987 we're processing the end of a sequence. */
20988 void record_line (bool end_sequence);
20989
20990 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
20991 nop-out rest of the lines in this sequence. */
20992 void check_line_address (struct dwarf2_cu *cu,
20993 const gdb_byte *line_ptr,
20994 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
20995
20996 void handle_set_discriminator (unsigned int discriminator)
20997 {
20998 m_discriminator = discriminator;
20999 m_line_has_non_zero_discriminator |= discriminator != 0;
21000 }
21001
21002 /* Handle DW_LNE_set_address. */
21003 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
21004 {
21005 m_op_index = 0;
21006 address += baseaddr;
21007 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
21008 }
21009
21010 /* Handle DW_LNS_advance_pc. */
21011 void handle_advance_pc (CORE_ADDR adjust);
21012
21013 /* Handle a special opcode. */
21014 void handle_special_opcode (unsigned char op_code);
21015
21016 /* Handle DW_LNS_advance_line. */
21017 void handle_advance_line (int line_delta)
21018 {
21019 advance_line (line_delta);
21020 }
21021
21022 /* Handle DW_LNS_set_file. */
21023 void handle_set_file (file_name_index file);
21024
21025 /* Handle DW_LNS_negate_stmt. */
21026 void handle_negate_stmt ()
21027 {
21028 m_is_stmt = !m_is_stmt;
21029 }
21030
21031 /* Handle DW_LNS_const_add_pc. */
21032 void handle_const_add_pc ();
21033
21034 /* Handle DW_LNS_fixed_advance_pc. */
21035 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
21036 {
21037 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21038 m_op_index = 0;
21039 }
21040
21041 /* Handle DW_LNS_copy. */
21042 void handle_copy ()
21043 {
21044 record_line (false);
21045 m_discriminator = 0;
21046 }
21047
21048 /* Handle DW_LNE_end_sequence. */
21049 void handle_end_sequence ()
21050 {
21051 m_currently_recording_lines = true;
21052 }
21053
21054 private:
21055 /* Advance the line by LINE_DELTA. */
21056 void advance_line (int line_delta)
21057 {
21058 m_line += line_delta;
21059
21060 if (line_delta != 0)
21061 m_line_has_non_zero_discriminator = m_discriminator != 0;
21062 }
21063
21064 struct dwarf2_cu *m_cu;
21065
21066 gdbarch *m_gdbarch;
21067
21068 /* True if we're recording lines.
21069 Otherwise we're building partial symtabs and are just interested in
21070 finding include files mentioned by the line number program. */
21071 bool m_record_lines_p;
21072
21073 /* The line number header. */
21074 line_header *m_line_header;
21075
21076 /* These are part of the standard DWARF line number state machine,
21077 and initialized according to the DWARF spec. */
21078
21079 unsigned char m_op_index = 0;
21080 /* The line table index of the current file. */
21081 file_name_index m_file = 1;
21082 unsigned int m_line = 1;
21083
21084 /* These are initialized in the constructor. */
21085
21086 CORE_ADDR m_address;
21087 bool m_is_stmt;
21088 unsigned int m_discriminator;
21089
21090 /* Additional bits of state we need to track. */
21091
21092 /* The last file that we called dwarf2_start_subfile for.
21093 This is only used for TLLs. */
21094 unsigned int m_last_file = 0;
21095 /* The last file a line number was recorded for. */
21096 struct subfile *m_last_subfile = NULL;
21097
21098 /* When true, record the lines we decode. */
21099 bool m_currently_recording_lines = false;
21100
21101 /* The last line number that was recorded, used to coalesce
21102 consecutive entries for the same line. This can happen, for
21103 example, when discriminators are present. PR 17276. */
21104 unsigned int m_last_line = 0;
21105 bool m_line_has_non_zero_discriminator = false;
21106 };
21107
21108 void
21109 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
21110 {
21111 CORE_ADDR addr_adj = (((m_op_index + adjust)
21112 / m_line_header->maximum_ops_per_instruction)
21113 * m_line_header->minimum_instruction_length);
21114 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21115 m_op_index = ((m_op_index + adjust)
21116 % m_line_header->maximum_ops_per_instruction);
21117 }
21118
21119 void
21120 lnp_state_machine::handle_special_opcode (unsigned char op_code)
21121 {
21122 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
21123 CORE_ADDR addr_adj = (((m_op_index
21124 + (adj_opcode / m_line_header->line_range))
21125 / m_line_header->maximum_ops_per_instruction)
21126 * m_line_header->minimum_instruction_length);
21127 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21128 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
21129 % m_line_header->maximum_ops_per_instruction);
21130
21131 int line_delta = (m_line_header->line_base
21132 + (adj_opcode % m_line_header->line_range));
21133 advance_line (line_delta);
21134 record_line (false);
21135 m_discriminator = 0;
21136 }
21137
21138 void
21139 lnp_state_machine::handle_set_file (file_name_index file)
21140 {
21141 m_file = file;
21142
21143 const file_entry *fe = current_file ();
21144 if (fe == NULL)
21145 dwarf2_debug_line_missing_file_complaint ();
21146 else if (m_record_lines_p)
21147 {
21148 const char *dir = fe->include_dir (m_line_header);
21149
21150 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
21151 m_line_has_non_zero_discriminator = m_discriminator != 0;
21152 dwarf2_start_subfile (m_cu, fe->name, dir);
21153 }
21154 }
21155
21156 void
21157 lnp_state_machine::handle_const_add_pc ()
21158 {
21159 CORE_ADDR adjust
21160 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
21161
21162 CORE_ADDR addr_adj
21163 = (((m_op_index + adjust)
21164 / m_line_header->maximum_ops_per_instruction)
21165 * m_line_header->minimum_instruction_length);
21166
21167 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21168 m_op_index = ((m_op_index + adjust)
21169 % m_line_header->maximum_ops_per_instruction);
21170 }
21171
21172 /* Return non-zero if we should add LINE to the line number table.
21173 LINE is the line to add, LAST_LINE is the last line that was added,
21174 LAST_SUBFILE is the subfile for LAST_LINE.
21175 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
21176 had a non-zero discriminator.
21177
21178 We have to be careful in the presence of discriminators.
21179 E.g., for this line:
21180
21181 for (i = 0; i < 100000; i++);
21182
21183 clang can emit four line number entries for that one line,
21184 each with a different discriminator.
21185 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
21186
21187 However, we want gdb to coalesce all four entries into one.
21188 Otherwise the user could stepi into the middle of the line and
21189 gdb would get confused about whether the pc really was in the
21190 middle of the line.
21191
21192 Things are further complicated by the fact that two consecutive
21193 line number entries for the same line is a heuristic used by gcc
21194 to denote the end of the prologue. So we can't just discard duplicate
21195 entries, we have to be selective about it. The heuristic we use is
21196 that we only collapse consecutive entries for the same line if at least
21197 one of those entries has a non-zero discriminator. PR 17276.
21198
21199 Note: Addresses in the line number state machine can never go backwards
21200 within one sequence, thus this coalescing is ok. */
21201
21202 static int
21203 dwarf_record_line_p (struct dwarf2_cu *cu,
21204 unsigned int line, unsigned int last_line,
21205 int line_has_non_zero_discriminator,
21206 struct subfile *last_subfile)
21207 {
21208 if (cu->get_builder ()->get_current_subfile () != last_subfile)
21209 return 1;
21210 if (line != last_line)
21211 return 1;
21212 /* Same line for the same file that we've seen already.
21213 As a last check, for pr 17276, only record the line if the line
21214 has never had a non-zero discriminator. */
21215 if (!line_has_non_zero_discriminator)
21216 return 1;
21217 return 0;
21218 }
21219
21220 /* Use the CU's builder to record line number LINE beginning at
21221 address ADDRESS in the line table of subfile SUBFILE. */
21222
21223 static void
21224 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
21225 unsigned int line, CORE_ADDR address,
21226 struct dwarf2_cu *cu)
21227 {
21228 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
21229
21230 if (dwarf_line_debug)
21231 {
21232 fprintf_unfiltered (gdb_stdlog,
21233 "Recording line %u, file %s, address %s\n",
21234 line, lbasename (subfile->name),
21235 paddress (gdbarch, address));
21236 }
21237
21238 if (cu != nullptr)
21239 cu->get_builder ()->record_line (subfile, line, addr);
21240 }
21241
21242 /* Subroutine of dwarf_decode_lines_1 to simplify it.
21243 Mark the end of a set of line number records.
21244 The arguments are the same as for dwarf_record_line_1.
21245 If SUBFILE is NULL the request is ignored. */
21246
21247 static void
21248 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
21249 CORE_ADDR address, struct dwarf2_cu *cu)
21250 {
21251 if (subfile == NULL)
21252 return;
21253
21254 if (dwarf_line_debug)
21255 {
21256 fprintf_unfiltered (gdb_stdlog,
21257 "Finishing current line, file %s, address %s\n",
21258 lbasename (subfile->name),
21259 paddress (gdbarch, address));
21260 }
21261
21262 dwarf_record_line_1 (gdbarch, subfile, 0, address, cu);
21263 }
21264
21265 void
21266 lnp_state_machine::record_line (bool end_sequence)
21267 {
21268 if (dwarf_line_debug)
21269 {
21270 fprintf_unfiltered (gdb_stdlog,
21271 "Processing actual line %u: file %u,"
21272 " address %s, is_stmt %u, discrim %u%s\n",
21273 m_line, m_file,
21274 paddress (m_gdbarch, m_address),
21275 m_is_stmt, m_discriminator,
21276 (end_sequence ? "\t(end sequence)" : ""));
21277 }
21278
21279 file_entry *fe = current_file ();
21280
21281 if (fe == NULL)
21282 dwarf2_debug_line_missing_file_complaint ();
21283 /* For now we ignore lines not starting on an instruction boundary.
21284 But not when processing end_sequence for compatibility with the
21285 previous version of the code. */
21286 else if (m_op_index == 0 || end_sequence)
21287 {
21288 fe->included_p = 1;
21289 if (m_record_lines_p
21290 && (producer_is_codewarrior (m_cu) || m_is_stmt || end_sequence))
21291 {
21292 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
21293 || end_sequence)
21294 {
21295 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
21296 m_currently_recording_lines ? m_cu : nullptr);
21297 }
21298
21299 if (!end_sequence)
21300 {
21301 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
21302 m_line_has_non_zero_discriminator,
21303 m_last_subfile))
21304 {
21305 buildsym_compunit *builder = m_cu->get_builder ();
21306 dwarf_record_line_1 (m_gdbarch,
21307 builder->get_current_subfile (),
21308 m_line, m_address,
21309 m_currently_recording_lines ? m_cu : nullptr);
21310 }
21311 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
21312 m_last_line = m_line;
21313 }
21314 }
21315 }
21316 }
21317
21318 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
21319 line_header *lh, bool record_lines_p)
21320 {
21321 m_cu = cu;
21322 m_gdbarch = arch;
21323 m_record_lines_p = record_lines_p;
21324 m_line_header = lh;
21325
21326 m_currently_recording_lines = true;
21327
21328 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
21329 was a line entry for it so that the backend has a chance to adjust it
21330 and also record it in case it needs it. This is currently used by MIPS
21331 code, cf. `mips_adjust_dwarf2_line'. */
21332 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
21333 m_is_stmt = lh->default_is_stmt;
21334 m_discriminator = 0;
21335 }
21336
21337 void
21338 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
21339 const gdb_byte *line_ptr,
21340 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
21341 {
21342 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
21343 the pc range of the CU. However, we restrict the test to only ADDRESS
21344 values of zero to preserve GDB's previous behaviour which is to handle
21345 the specific case of a function being GC'd by the linker. */
21346
21347 if (address == 0 && address < unrelocated_lowpc)
21348 {
21349 /* This line table is for a function which has been
21350 GCd by the linker. Ignore it. PR gdb/12528 */
21351
21352 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21353 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
21354
21355 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
21356 line_offset, objfile_name (objfile));
21357 m_currently_recording_lines = false;
21358 /* Note: m_currently_recording_lines is left as false until we see
21359 DW_LNE_end_sequence. */
21360 }
21361 }
21362
21363 /* Subroutine of dwarf_decode_lines to simplify it.
21364 Process the line number information in LH.
21365 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
21366 program in order to set included_p for every referenced header. */
21367
21368 static void
21369 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
21370 const int decode_for_pst_p, CORE_ADDR lowpc)
21371 {
21372 const gdb_byte *line_ptr, *extended_end;
21373 const gdb_byte *line_end;
21374 unsigned int bytes_read, extended_len;
21375 unsigned char op_code, extended_op;
21376 CORE_ADDR baseaddr;
21377 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21378 bfd *abfd = objfile->obfd;
21379 struct gdbarch *gdbarch = get_objfile_arch (objfile);
21380 /* True if we're recording line info (as opposed to building partial
21381 symtabs and just interested in finding include files mentioned by
21382 the line number program). */
21383 bool record_lines_p = !decode_for_pst_p;
21384
21385 baseaddr = objfile->text_section_offset ();
21386
21387 line_ptr = lh->statement_program_start;
21388 line_end = lh->statement_program_end;
21389
21390 /* Read the statement sequences until there's nothing left. */
21391 while (line_ptr < line_end)
21392 {
21393 /* The DWARF line number program state machine. Reset the state
21394 machine at the start of each sequence. */
21395 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
21396 bool end_sequence = false;
21397
21398 if (record_lines_p)
21399 {
21400 /* Start a subfile for the current file of the state
21401 machine. */
21402 const file_entry *fe = state_machine.current_file ();
21403
21404 if (fe != NULL)
21405 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
21406 }
21407
21408 /* Decode the table. */
21409 while (line_ptr < line_end && !end_sequence)
21410 {
21411 op_code = read_1_byte (abfd, line_ptr);
21412 line_ptr += 1;
21413
21414 if (op_code >= lh->opcode_base)
21415 {
21416 /* Special opcode. */
21417 state_machine.handle_special_opcode (op_code);
21418 }
21419 else switch (op_code)
21420 {
21421 case DW_LNS_extended_op:
21422 extended_len = read_unsigned_leb128 (abfd, line_ptr,
21423 &bytes_read);
21424 line_ptr += bytes_read;
21425 extended_end = line_ptr + extended_len;
21426 extended_op = read_1_byte (abfd, line_ptr);
21427 line_ptr += 1;
21428 switch (extended_op)
21429 {
21430 case DW_LNE_end_sequence:
21431 state_machine.handle_end_sequence ();
21432 end_sequence = true;
21433 break;
21434 case DW_LNE_set_address:
21435 {
21436 CORE_ADDR address
21437 = read_address (abfd, line_ptr, cu, &bytes_read);
21438 line_ptr += bytes_read;
21439
21440 state_machine.check_line_address (cu, line_ptr,
21441 lowpc - baseaddr, address);
21442 state_machine.handle_set_address (baseaddr, address);
21443 }
21444 break;
21445 case DW_LNE_define_file:
21446 {
21447 const char *cur_file;
21448 unsigned int mod_time, length;
21449 dir_index dindex;
21450
21451 cur_file = read_direct_string (abfd, line_ptr,
21452 &bytes_read);
21453 line_ptr += bytes_read;
21454 dindex = (dir_index)
21455 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21456 line_ptr += bytes_read;
21457 mod_time =
21458 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21459 line_ptr += bytes_read;
21460 length =
21461 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21462 line_ptr += bytes_read;
21463 lh->add_file_name (cur_file, dindex, mod_time, length);
21464 }
21465 break;
21466 case DW_LNE_set_discriminator:
21467 {
21468 /* The discriminator is not interesting to the
21469 debugger; just ignore it. We still need to
21470 check its value though:
21471 if there are consecutive entries for the same
21472 (non-prologue) line we want to coalesce them.
21473 PR 17276. */
21474 unsigned int discr
21475 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21476 line_ptr += bytes_read;
21477
21478 state_machine.handle_set_discriminator (discr);
21479 }
21480 break;
21481 default:
21482 complaint (_("mangled .debug_line section"));
21483 return;
21484 }
21485 /* Make sure that we parsed the extended op correctly. If e.g.
21486 we expected a different address size than the producer used,
21487 we may have read the wrong number of bytes. */
21488 if (line_ptr != extended_end)
21489 {
21490 complaint (_("mangled .debug_line section"));
21491 return;
21492 }
21493 break;
21494 case DW_LNS_copy:
21495 state_machine.handle_copy ();
21496 break;
21497 case DW_LNS_advance_pc:
21498 {
21499 CORE_ADDR adjust
21500 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21501 line_ptr += bytes_read;
21502
21503 state_machine.handle_advance_pc (adjust);
21504 }
21505 break;
21506 case DW_LNS_advance_line:
21507 {
21508 int line_delta
21509 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
21510 line_ptr += bytes_read;
21511
21512 state_machine.handle_advance_line (line_delta);
21513 }
21514 break;
21515 case DW_LNS_set_file:
21516 {
21517 file_name_index file
21518 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21519 &bytes_read);
21520 line_ptr += bytes_read;
21521
21522 state_machine.handle_set_file (file);
21523 }
21524 break;
21525 case DW_LNS_set_column:
21526 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21527 line_ptr += bytes_read;
21528 break;
21529 case DW_LNS_negate_stmt:
21530 state_machine.handle_negate_stmt ();
21531 break;
21532 case DW_LNS_set_basic_block:
21533 break;
21534 /* Add to the address register of the state machine the
21535 address increment value corresponding to special opcode
21536 255. I.e., this value is scaled by the minimum
21537 instruction length since special opcode 255 would have
21538 scaled the increment. */
21539 case DW_LNS_const_add_pc:
21540 state_machine.handle_const_add_pc ();
21541 break;
21542 case DW_LNS_fixed_advance_pc:
21543 {
21544 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
21545 line_ptr += 2;
21546
21547 state_machine.handle_fixed_advance_pc (addr_adj);
21548 }
21549 break;
21550 default:
21551 {
21552 /* Unknown standard opcode, ignore it. */
21553 int i;
21554
21555 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
21556 {
21557 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21558 line_ptr += bytes_read;
21559 }
21560 }
21561 }
21562 }
21563
21564 if (!end_sequence)
21565 dwarf2_debug_line_missing_end_sequence_complaint ();
21566
21567 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21568 in which case we still finish recording the last line). */
21569 state_machine.record_line (true);
21570 }
21571 }
21572
21573 /* Decode the Line Number Program (LNP) for the given line_header
21574 structure and CU. The actual information extracted and the type
21575 of structures created from the LNP depends on the value of PST.
21576
21577 1. If PST is NULL, then this procedure uses the data from the program
21578 to create all necessary symbol tables, and their linetables.
21579
21580 2. If PST is not NULL, this procedure reads the program to determine
21581 the list of files included by the unit represented by PST, and
21582 builds all the associated partial symbol tables.
21583
21584 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21585 It is used for relative paths in the line table.
21586 NOTE: When processing partial symtabs (pst != NULL),
21587 comp_dir == pst->dirname.
21588
21589 NOTE: It is important that psymtabs have the same file name (via strcmp)
21590 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21591 symtab we don't use it in the name of the psymtabs we create.
21592 E.g. expand_line_sal requires this when finding psymtabs to expand.
21593 A good testcase for this is mb-inline.exp.
21594
21595 LOWPC is the lowest address in CU (or 0 if not known).
21596
21597 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21598 for its PC<->lines mapping information. Otherwise only the filename
21599 table is read in. */
21600
21601 static void
21602 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
21603 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
21604 CORE_ADDR lowpc, int decode_mapping)
21605 {
21606 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21607 const int decode_for_pst_p = (pst != NULL);
21608
21609 if (decode_mapping)
21610 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
21611
21612 if (decode_for_pst_p)
21613 {
21614 /* Now that we're done scanning the Line Header Program, we can
21615 create the psymtab of each included file. */
21616 for (auto &file_entry : lh->file_names ())
21617 if (file_entry.included_p == 1)
21618 {
21619 gdb::unique_xmalloc_ptr<char> name_holder;
21620 const char *include_name =
21621 psymtab_include_file_name (lh, file_entry, pst,
21622 comp_dir, &name_holder);
21623 if (include_name != NULL)
21624 dwarf2_create_include_psymtab (include_name, pst, objfile);
21625 }
21626 }
21627 else
21628 {
21629 /* Make sure a symtab is created for every file, even files
21630 which contain only variables (i.e. no code with associated
21631 line numbers). */
21632 buildsym_compunit *builder = cu->get_builder ();
21633 struct compunit_symtab *cust = builder->get_compunit_symtab ();
21634
21635 for (auto &fe : lh->file_names ())
21636 {
21637 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
21638 if (builder->get_current_subfile ()->symtab == NULL)
21639 {
21640 builder->get_current_subfile ()->symtab
21641 = allocate_symtab (cust,
21642 builder->get_current_subfile ()->name);
21643 }
21644 fe.symtab = builder->get_current_subfile ()->symtab;
21645 }
21646 }
21647 }
21648
21649 /* Start a subfile for DWARF. FILENAME is the name of the file and
21650 DIRNAME the name of the source directory which contains FILENAME
21651 or NULL if not known.
21652 This routine tries to keep line numbers from identical absolute and
21653 relative file names in a common subfile.
21654
21655 Using the `list' example from the GDB testsuite, which resides in
21656 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21657 of /srcdir/list0.c yields the following debugging information for list0.c:
21658
21659 DW_AT_name: /srcdir/list0.c
21660 DW_AT_comp_dir: /compdir
21661 files.files[0].name: list0.h
21662 files.files[0].dir: /srcdir
21663 files.files[1].name: list0.c
21664 files.files[1].dir: /srcdir
21665
21666 The line number information for list0.c has to end up in a single
21667 subfile, so that `break /srcdir/list0.c:1' works as expected.
21668 start_subfile will ensure that this happens provided that we pass the
21669 concatenation of files.files[1].dir and files.files[1].name as the
21670 subfile's name. */
21671
21672 static void
21673 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
21674 const char *dirname)
21675 {
21676 gdb::unique_xmalloc_ptr<char> copy;
21677
21678 /* In order not to lose the line information directory,
21679 we concatenate it to the filename when it makes sense.
21680 Note that the Dwarf3 standard says (speaking of filenames in line
21681 information): ``The directory index is ignored for file names
21682 that represent full path names''. Thus ignoring dirname in the
21683 `else' branch below isn't an issue. */
21684
21685 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
21686 {
21687 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
21688 filename = copy.get ();
21689 }
21690
21691 cu->get_builder ()->start_subfile (filename);
21692 }
21693
21694 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
21695 buildsym_compunit constructor. */
21696
21697 struct compunit_symtab *
21698 dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
21699 CORE_ADDR low_pc)
21700 {
21701 gdb_assert (m_builder == nullptr);
21702
21703 m_builder.reset (new struct buildsym_compunit
21704 (per_cu->dwarf2_per_objfile->objfile,
21705 name, comp_dir, language, low_pc));
21706
21707 list_in_scope = get_builder ()->get_file_symbols ();
21708
21709 get_builder ()->record_debugformat ("DWARF 2");
21710 get_builder ()->record_producer (producer);
21711
21712 processing_has_namespace_info = false;
21713
21714 return get_builder ()->get_compunit_symtab ();
21715 }
21716
21717 static void
21718 var_decode_location (struct attribute *attr, struct symbol *sym,
21719 struct dwarf2_cu *cu)
21720 {
21721 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21722 struct comp_unit_head *cu_header = &cu->header;
21723
21724 /* NOTE drow/2003-01-30: There used to be a comment and some special
21725 code here to turn a symbol with DW_AT_external and a
21726 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21727 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21728 with some versions of binutils) where shared libraries could have
21729 relocations against symbols in their debug information - the
21730 minimal symbol would have the right address, but the debug info
21731 would not. It's no longer necessary, because we will explicitly
21732 apply relocations when we read in the debug information now. */
21733
21734 /* A DW_AT_location attribute with no contents indicates that a
21735 variable has been optimized away. */
21736 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
21737 {
21738 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21739 return;
21740 }
21741
21742 /* Handle one degenerate form of location expression specially, to
21743 preserve GDB's previous behavior when section offsets are
21744 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
21745 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
21746
21747 if (attr_form_is_block (attr)
21748 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
21749 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
21750 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
21751 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
21752 && (DW_BLOCK (attr)->size
21753 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
21754 {
21755 unsigned int dummy;
21756
21757 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
21758 SET_SYMBOL_VALUE_ADDRESS (sym,
21759 read_address (objfile->obfd,
21760 DW_BLOCK (attr)->data + 1,
21761 cu, &dummy));
21762 else
21763 SET_SYMBOL_VALUE_ADDRESS
21764 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
21765 &dummy));
21766 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
21767 fixup_symbol_section (sym, objfile);
21768 SET_SYMBOL_VALUE_ADDRESS
21769 (sym,
21770 SYMBOL_VALUE_ADDRESS (sym)
21771 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
21772 return;
21773 }
21774
21775 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21776 expression evaluator, and use LOC_COMPUTED only when necessary
21777 (i.e. when the value of a register or memory location is
21778 referenced, or a thread-local block, etc.). Then again, it might
21779 not be worthwhile. I'm assuming that it isn't unless performance
21780 or memory numbers show me otherwise. */
21781
21782 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
21783
21784 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
21785 cu->has_loclist = true;
21786 }
21787
21788 /* Given a pointer to a DWARF information entry, figure out if we need
21789 to make a symbol table entry for it, and if so, create a new entry
21790 and return a pointer to it.
21791 If TYPE is NULL, determine symbol type from the die, otherwise
21792 used the passed type.
21793 If SPACE is not NULL, use it to hold the new symbol. If it is
21794 NULL, allocate a new symbol on the objfile's obstack. */
21795
21796 static struct symbol *
21797 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21798 struct symbol *space)
21799 {
21800 struct dwarf2_per_objfile *dwarf2_per_objfile
21801 = cu->per_cu->dwarf2_per_objfile;
21802 struct objfile *objfile = dwarf2_per_objfile->objfile;
21803 struct gdbarch *gdbarch = get_objfile_arch (objfile);
21804 struct symbol *sym = NULL;
21805 const char *name;
21806 struct attribute *attr = NULL;
21807 struct attribute *attr2 = NULL;
21808 CORE_ADDR baseaddr;
21809 struct pending **list_to_add = NULL;
21810
21811 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
21812
21813 baseaddr = objfile->text_section_offset ();
21814
21815 name = dwarf2_name (die, cu);
21816 if (name)
21817 {
21818 const char *linkagename;
21819 int suppress_add = 0;
21820
21821 if (space)
21822 sym = space;
21823 else
21824 sym = allocate_symbol (objfile);
21825 OBJSTAT (objfile, n_syms++);
21826
21827 /* Cache this symbol's name and the name's demangled form (if any). */
21828 sym->set_language (cu->language, &objfile->objfile_obstack);
21829 linkagename = dwarf2_physname (name, die, cu);
21830 sym->compute_and_set_names (linkagename, false, objfile->per_bfd);
21831
21832 /* Fortran does not have mangling standard and the mangling does differ
21833 between gfortran, iFort etc. */
21834 if (cu->language == language_fortran
21835 && symbol_get_demangled_name (sym) == NULL)
21836 symbol_set_demangled_name (sym,
21837 dwarf2_full_name (name, die, cu),
21838 NULL);
21839
21840 /* Default assumptions.
21841 Use the passed type or decode it from the die. */
21842 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21843 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21844 if (type != NULL)
21845 SYMBOL_TYPE (sym) = type;
21846 else
21847 SYMBOL_TYPE (sym) = die_type (die, cu);
21848 attr = dwarf2_attr (die,
21849 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21850 cu);
21851 if (attr != nullptr)
21852 {
21853 SYMBOL_LINE (sym) = DW_UNSND (attr);
21854 }
21855
21856 attr = dwarf2_attr (die,
21857 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21858 cu);
21859 if (attr != nullptr)
21860 {
21861 file_name_index file_index = (file_name_index) DW_UNSND (attr);
21862 struct file_entry *fe;
21863
21864 if (cu->line_header != NULL)
21865 fe = cu->line_header->file_name_at (file_index);
21866 else
21867 fe = NULL;
21868
21869 if (fe == NULL)
21870 complaint (_("file index out of range"));
21871 else
21872 symbol_set_symtab (sym, fe->symtab);
21873 }
21874
21875 switch (die->tag)
21876 {
21877 case DW_TAG_label:
21878 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
21879 if (attr != nullptr)
21880 {
21881 CORE_ADDR addr;
21882
21883 addr = attr_value_as_address (attr);
21884 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
21885 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
21886 }
21887 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21888 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
21889 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
21890 add_symbol_to_list (sym, cu->list_in_scope);
21891 break;
21892 case DW_TAG_subprogram:
21893 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21894 finish_block. */
21895 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21896 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21897 if ((attr2 && (DW_UNSND (attr2) != 0))
21898 || cu->language == language_ada
21899 || cu->language == language_fortran)
21900 {
21901 /* Subprograms marked external are stored as a global symbol.
21902 Ada and Fortran subprograms, whether marked external or
21903 not, are always stored as a global symbol, because we want
21904 to be able to access them globally. For instance, we want
21905 to be able to break on a nested subprogram without having
21906 to specify the context. */
21907 list_to_add = cu->get_builder ()->get_global_symbols ();
21908 }
21909 else
21910 {
21911 list_to_add = cu->list_in_scope;
21912 }
21913 break;
21914 case DW_TAG_inlined_subroutine:
21915 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21916 finish_block. */
21917 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21918 SYMBOL_INLINED (sym) = 1;
21919 list_to_add = cu->list_in_scope;
21920 break;
21921 case DW_TAG_template_value_param:
21922 suppress_add = 1;
21923 /* Fall through. */
21924 case DW_TAG_constant:
21925 case DW_TAG_variable:
21926 case DW_TAG_member:
21927 /* Compilation with minimal debug info may result in
21928 variables with missing type entries. Change the
21929 misleading `void' type to something sensible. */
21930 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
21931 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
21932
21933 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21934 /* In the case of DW_TAG_member, we should only be called for
21935 static const members. */
21936 if (die->tag == DW_TAG_member)
21937 {
21938 /* dwarf2_add_field uses die_is_declaration,
21939 so we do the same. */
21940 gdb_assert (die_is_declaration (die, cu));
21941 gdb_assert (attr);
21942 }
21943 if (attr != nullptr)
21944 {
21945 dwarf2_const_value (attr, sym, cu);
21946 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21947 if (!suppress_add)
21948 {
21949 if (attr2 && (DW_UNSND (attr2) != 0))
21950 list_to_add = cu->get_builder ()->get_global_symbols ();
21951 else
21952 list_to_add = cu->list_in_scope;
21953 }
21954 break;
21955 }
21956 attr = dwarf2_attr (die, DW_AT_location, cu);
21957 if (attr != nullptr)
21958 {
21959 var_decode_location (attr, sym, cu);
21960 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21961
21962 /* Fortran explicitly imports any global symbols to the local
21963 scope by DW_TAG_common_block. */
21964 if (cu->language == language_fortran && die->parent
21965 && die->parent->tag == DW_TAG_common_block)
21966 attr2 = NULL;
21967
21968 if (SYMBOL_CLASS (sym) == LOC_STATIC
21969 && SYMBOL_VALUE_ADDRESS (sym) == 0
21970 && !dwarf2_per_objfile->has_section_at_zero)
21971 {
21972 /* When a static variable is eliminated by the linker,
21973 the corresponding debug information is not stripped
21974 out, but the variable address is set to null;
21975 do not add such variables into symbol table. */
21976 }
21977 else if (attr2 && (DW_UNSND (attr2) != 0))
21978 {
21979 if (SYMBOL_CLASS (sym) == LOC_STATIC
21980 && (objfile->flags & OBJF_MAINLINE) == 0
21981 && dwarf2_per_objfile->can_copy)
21982 {
21983 /* A global static variable might be subject to
21984 copy relocation. We first check for a local
21985 minsym, though, because maybe the symbol was
21986 marked hidden, in which case this would not
21987 apply. */
21988 bound_minimal_symbol found
21989 = (lookup_minimal_symbol_linkage
21990 (sym->linkage_name (), objfile));
21991 if (found.minsym != nullptr)
21992 sym->maybe_copied = 1;
21993 }
21994
21995 /* A variable with DW_AT_external is never static,
21996 but it may be block-scoped. */
21997 list_to_add
21998 = ((cu->list_in_scope
21999 == cu->get_builder ()->get_file_symbols ())
22000 ? cu->get_builder ()->get_global_symbols ()
22001 : cu->list_in_scope);
22002 }
22003 else
22004 list_to_add = cu->list_in_scope;
22005 }
22006 else
22007 {
22008 /* We do not know the address of this symbol.
22009 If it is an external symbol and we have type information
22010 for it, enter the symbol as a LOC_UNRESOLVED symbol.
22011 The address of the variable will then be determined from
22012 the minimal symbol table whenever the variable is
22013 referenced. */
22014 attr2 = dwarf2_attr (die, DW_AT_external, cu);
22015
22016 /* Fortran explicitly imports any global symbols to the local
22017 scope by DW_TAG_common_block. */
22018 if (cu->language == language_fortran && die->parent
22019 && die->parent->tag == DW_TAG_common_block)
22020 {
22021 /* SYMBOL_CLASS doesn't matter here because
22022 read_common_block is going to reset it. */
22023 if (!suppress_add)
22024 list_to_add = cu->list_in_scope;
22025 }
22026 else if (attr2 && (DW_UNSND (attr2) != 0)
22027 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
22028 {
22029 /* A variable with DW_AT_external is never static, but it
22030 may be block-scoped. */
22031 list_to_add
22032 = ((cu->list_in_scope
22033 == cu->get_builder ()->get_file_symbols ())
22034 ? cu->get_builder ()->get_global_symbols ()
22035 : cu->list_in_scope);
22036
22037 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
22038 }
22039 else if (!die_is_declaration (die, cu))
22040 {
22041 /* Use the default LOC_OPTIMIZED_OUT class. */
22042 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
22043 if (!suppress_add)
22044 list_to_add = cu->list_in_scope;
22045 }
22046 }
22047 break;
22048 case DW_TAG_formal_parameter:
22049 {
22050 /* If we are inside a function, mark this as an argument. If
22051 not, we might be looking at an argument to an inlined function
22052 when we do not have enough information to show inlined frames;
22053 pretend it's a local variable in that case so that the user can
22054 still see it. */
22055 struct context_stack *curr
22056 = cu->get_builder ()->get_current_context_stack ();
22057 if (curr != nullptr && curr->name != nullptr)
22058 SYMBOL_IS_ARGUMENT (sym) = 1;
22059 attr = dwarf2_attr (die, DW_AT_location, cu);
22060 if (attr != nullptr)
22061 {
22062 var_decode_location (attr, sym, cu);
22063 }
22064 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22065 if (attr != nullptr)
22066 {
22067 dwarf2_const_value (attr, sym, cu);
22068 }
22069
22070 list_to_add = cu->list_in_scope;
22071 }
22072 break;
22073 case DW_TAG_unspecified_parameters:
22074 /* From varargs functions; gdb doesn't seem to have any
22075 interest in this information, so just ignore it for now.
22076 (FIXME?) */
22077 break;
22078 case DW_TAG_template_type_param:
22079 suppress_add = 1;
22080 /* Fall through. */
22081 case DW_TAG_class_type:
22082 case DW_TAG_interface_type:
22083 case DW_TAG_structure_type:
22084 case DW_TAG_union_type:
22085 case DW_TAG_set_type:
22086 case DW_TAG_enumeration_type:
22087 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22088 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
22089
22090 {
22091 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
22092 really ever be static objects: otherwise, if you try
22093 to, say, break of a class's method and you're in a file
22094 which doesn't mention that class, it won't work unless
22095 the check for all static symbols in lookup_symbol_aux
22096 saves you. See the OtherFileClass tests in
22097 gdb.c++/namespace.exp. */
22098
22099 if (!suppress_add)
22100 {
22101 buildsym_compunit *builder = cu->get_builder ();
22102 list_to_add
22103 = (cu->list_in_scope == builder->get_file_symbols ()
22104 && cu->language == language_cplus
22105 ? builder->get_global_symbols ()
22106 : cu->list_in_scope);
22107
22108 /* The semantics of C++ state that "struct foo {
22109 ... }" also defines a typedef for "foo". */
22110 if (cu->language == language_cplus
22111 || cu->language == language_ada
22112 || cu->language == language_d
22113 || cu->language == language_rust)
22114 {
22115 /* The symbol's name is already allocated along
22116 with this objfile, so we don't need to
22117 duplicate it for the type. */
22118 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
22119 TYPE_NAME (SYMBOL_TYPE (sym)) = sym->search_name ();
22120 }
22121 }
22122 }
22123 break;
22124 case DW_TAG_typedef:
22125 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22126 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
22127 list_to_add = cu->list_in_scope;
22128 break;
22129 case DW_TAG_base_type:
22130 case DW_TAG_subrange_type:
22131 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22132 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
22133 list_to_add = cu->list_in_scope;
22134 break;
22135 case DW_TAG_enumerator:
22136 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22137 if (attr != nullptr)
22138 {
22139 dwarf2_const_value (attr, sym, cu);
22140 }
22141 {
22142 /* NOTE: carlton/2003-11-10: See comment above in the
22143 DW_TAG_class_type, etc. block. */
22144
22145 list_to_add
22146 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
22147 && cu->language == language_cplus
22148 ? cu->get_builder ()->get_global_symbols ()
22149 : cu->list_in_scope);
22150 }
22151 break;
22152 case DW_TAG_imported_declaration:
22153 case DW_TAG_namespace:
22154 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22155 list_to_add = cu->get_builder ()->get_global_symbols ();
22156 break;
22157 case DW_TAG_module:
22158 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22159 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
22160 list_to_add = cu->get_builder ()->get_global_symbols ();
22161 break;
22162 case DW_TAG_common_block:
22163 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
22164 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
22165 add_symbol_to_list (sym, cu->list_in_scope);
22166 break;
22167 default:
22168 /* Not a tag we recognize. Hopefully we aren't processing
22169 trash data, but since we must specifically ignore things
22170 we don't recognize, there is nothing else we should do at
22171 this point. */
22172 complaint (_("unsupported tag: '%s'"),
22173 dwarf_tag_name (die->tag));
22174 break;
22175 }
22176
22177 if (suppress_add)
22178 {
22179 sym->hash_next = objfile->template_symbols;
22180 objfile->template_symbols = sym;
22181 list_to_add = NULL;
22182 }
22183
22184 if (list_to_add != NULL)
22185 add_symbol_to_list (sym, list_to_add);
22186
22187 /* For the benefit of old versions of GCC, check for anonymous
22188 namespaces based on the demangled name. */
22189 if (!cu->processing_has_namespace_info
22190 && cu->language == language_cplus)
22191 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
22192 }
22193 return (sym);
22194 }
22195
22196 /* Given an attr with a DW_FORM_dataN value in host byte order,
22197 zero-extend it as appropriate for the symbol's type. The DWARF
22198 standard (v4) is not entirely clear about the meaning of using
22199 DW_FORM_dataN for a constant with a signed type, where the type is
22200 wider than the data. The conclusion of a discussion on the DWARF
22201 list was that this is unspecified. We choose to always zero-extend
22202 because that is the interpretation long in use by GCC. */
22203
22204 static gdb_byte *
22205 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
22206 struct dwarf2_cu *cu, LONGEST *value, int bits)
22207 {
22208 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22209 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
22210 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
22211 LONGEST l = DW_UNSND (attr);
22212
22213 if (bits < sizeof (*value) * 8)
22214 {
22215 l &= ((LONGEST) 1 << bits) - 1;
22216 *value = l;
22217 }
22218 else if (bits == sizeof (*value) * 8)
22219 *value = l;
22220 else
22221 {
22222 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
22223 store_unsigned_integer (bytes, bits / 8, byte_order, l);
22224 return bytes;
22225 }
22226
22227 return NULL;
22228 }
22229
22230 /* Read a constant value from an attribute. Either set *VALUE, or if
22231 the value does not fit in *VALUE, set *BYTES - either already
22232 allocated on the objfile obstack, or newly allocated on OBSTACK,
22233 or, set *BATON, if we translated the constant to a location
22234 expression. */
22235
22236 static void
22237 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
22238 const char *name, struct obstack *obstack,
22239 struct dwarf2_cu *cu,
22240 LONGEST *value, const gdb_byte **bytes,
22241 struct dwarf2_locexpr_baton **baton)
22242 {
22243 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22244 struct comp_unit_head *cu_header = &cu->header;
22245 struct dwarf_block *blk;
22246 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
22247 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22248
22249 *value = 0;
22250 *bytes = NULL;
22251 *baton = NULL;
22252
22253 switch (attr->form)
22254 {
22255 case DW_FORM_addr:
22256 case DW_FORM_addrx:
22257 case DW_FORM_GNU_addr_index:
22258 {
22259 gdb_byte *data;
22260
22261 if (TYPE_LENGTH (type) != cu_header->addr_size)
22262 dwarf2_const_value_length_mismatch_complaint (name,
22263 cu_header->addr_size,
22264 TYPE_LENGTH (type));
22265 /* Symbols of this form are reasonably rare, so we just
22266 piggyback on the existing location code rather than writing
22267 a new implementation of symbol_computed_ops. */
22268 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
22269 (*baton)->per_cu = cu->per_cu;
22270 gdb_assert ((*baton)->per_cu);
22271
22272 (*baton)->size = 2 + cu_header->addr_size;
22273 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
22274 (*baton)->data = data;
22275
22276 data[0] = DW_OP_addr;
22277 store_unsigned_integer (&data[1], cu_header->addr_size,
22278 byte_order, DW_ADDR (attr));
22279 data[cu_header->addr_size + 1] = DW_OP_stack_value;
22280 }
22281 break;
22282 case DW_FORM_string:
22283 case DW_FORM_strp:
22284 case DW_FORM_strx:
22285 case DW_FORM_GNU_str_index:
22286 case DW_FORM_GNU_strp_alt:
22287 /* DW_STRING is already allocated on the objfile obstack, point
22288 directly to it. */
22289 *bytes = (const gdb_byte *) DW_STRING (attr);
22290 break;
22291 case DW_FORM_block1:
22292 case DW_FORM_block2:
22293 case DW_FORM_block4:
22294 case DW_FORM_block:
22295 case DW_FORM_exprloc:
22296 case DW_FORM_data16:
22297 blk = DW_BLOCK (attr);
22298 if (TYPE_LENGTH (type) != blk->size)
22299 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
22300 TYPE_LENGTH (type));
22301 *bytes = blk->data;
22302 break;
22303
22304 /* The DW_AT_const_value attributes are supposed to carry the
22305 symbol's value "represented as it would be on the target
22306 architecture." By the time we get here, it's already been
22307 converted to host endianness, so we just need to sign- or
22308 zero-extend it as appropriate. */
22309 case DW_FORM_data1:
22310 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
22311 break;
22312 case DW_FORM_data2:
22313 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
22314 break;
22315 case DW_FORM_data4:
22316 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
22317 break;
22318 case DW_FORM_data8:
22319 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
22320 break;
22321
22322 case DW_FORM_sdata:
22323 case DW_FORM_implicit_const:
22324 *value = DW_SND (attr);
22325 break;
22326
22327 case DW_FORM_udata:
22328 *value = DW_UNSND (attr);
22329 break;
22330
22331 default:
22332 complaint (_("unsupported const value attribute form: '%s'"),
22333 dwarf_form_name (attr->form));
22334 *value = 0;
22335 break;
22336 }
22337 }
22338
22339
22340 /* Copy constant value from an attribute to a symbol. */
22341
22342 static void
22343 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
22344 struct dwarf2_cu *cu)
22345 {
22346 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22347 LONGEST value;
22348 const gdb_byte *bytes;
22349 struct dwarf2_locexpr_baton *baton;
22350
22351 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
22352 sym->print_name (),
22353 &objfile->objfile_obstack, cu,
22354 &value, &bytes, &baton);
22355
22356 if (baton != NULL)
22357 {
22358 SYMBOL_LOCATION_BATON (sym) = baton;
22359 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
22360 }
22361 else if (bytes != NULL)
22362 {
22363 SYMBOL_VALUE_BYTES (sym) = bytes;
22364 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
22365 }
22366 else
22367 {
22368 SYMBOL_VALUE (sym) = value;
22369 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
22370 }
22371 }
22372
22373 /* Return the type of the die in question using its DW_AT_type attribute. */
22374
22375 static struct type *
22376 die_type (struct die_info *die, struct dwarf2_cu *cu)
22377 {
22378 struct attribute *type_attr;
22379
22380 type_attr = dwarf2_attr (die, DW_AT_type, cu);
22381 if (!type_attr)
22382 {
22383 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22384 /* A missing DW_AT_type represents a void type. */
22385 return objfile_type (objfile)->builtin_void;
22386 }
22387
22388 return lookup_die_type (die, type_attr, cu);
22389 }
22390
22391 /* True iff CU's producer generates GNAT Ada auxiliary information
22392 that allows to find parallel types through that information instead
22393 of having to do expensive parallel lookups by type name. */
22394
22395 static int
22396 need_gnat_info (struct dwarf2_cu *cu)
22397 {
22398 /* Assume that the Ada compiler was GNAT, which always produces
22399 the auxiliary information. */
22400 return (cu->language == language_ada);
22401 }
22402
22403 /* Return the auxiliary type of the die in question using its
22404 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
22405 attribute is not present. */
22406
22407 static struct type *
22408 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
22409 {
22410 struct attribute *type_attr;
22411
22412 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
22413 if (!type_attr)
22414 return NULL;
22415
22416 return lookup_die_type (die, type_attr, cu);
22417 }
22418
22419 /* If DIE has a descriptive_type attribute, then set the TYPE's
22420 descriptive type accordingly. */
22421
22422 static void
22423 set_descriptive_type (struct type *type, struct die_info *die,
22424 struct dwarf2_cu *cu)
22425 {
22426 struct type *descriptive_type = die_descriptive_type (die, cu);
22427
22428 if (descriptive_type)
22429 {
22430 ALLOCATE_GNAT_AUX_TYPE (type);
22431 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
22432 }
22433 }
22434
22435 /* Return the containing type of the die in question using its
22436 DW_AT_containing_type attribute. */
22437
22438 static struct type *
22439 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
22440 {
22441 struct attribute *type_attr;
22442 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22443
22444 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
22445 if (!type_attr)
22446 error (_("Dwarf Error: Problem turning containing type into gdb type "
22447 "[in module %s]"), objfile_name (objfile));
22448
22449 return lookup_die_type (die, type_attr, cu);
22450 }
22451
22452 /* Return an error marker type to use for the ill formed type in DIE/CU. */
22453
22454 static struct type *
22455 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22456 {
22457 struct dwarf2_per_objfile *dwarf2_per_objfile
22458 = cu->per_cu->dwarf2_per_objfile;
22459 struct objfile *objfile = dwarf2_per_objfile->objfile;
22460 char *saved;
22461
22462 std::string message
22463 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
22464 objfile_name (objfile),
22465 sect_offset_str (cu->header.sect_off),
22466 sect_offset_str (die->sect_off));
22467 saved = obstack_strdup (&objfile->objfile_obstack, message);
22468
22469 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
22470 }
22471
22472 /* Look up the type of DIE in CU using its type attribute ATTR.
22473 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22474 DW_AT_containing_type.
22475 If there is no type substitute an error marker. */
22476
22477 static struct type *
22478 lookup_die_type (struct die_info *die, const struct attribute *attr,
22479 struct dwarf2_cu *cu)
22480 {
22481 struct dwarf2_per_objfile *dwarf2_per_objfile
22482 = cu->per_cu->dwarf2_per_objfile;
22483 struct objfile *objfile = dwarf2_per_objfile->objfile;
22484 struct type *this_type;
22485
22486 gdb_assert (attr->name == DW_AT_type
22487 || attr->name == DW_AT_GNAT_descriptive_type
22488 || attr->name == DW_AT_containing_type);
22489
22490 /* First see if we have it cached. */
22491
22492 if (attr->form == DW_FORM_GNU_ref_alt)
22493 {
22494 struct dwarf2_per_cu_data *per_cu;
22495 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
22496
22497 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
22498 dwarf2_per_objfile);
22499 this_type = get_die_type_at_offset (sect_off, per_cu);
22500 }
22501 else if (attr_form_is_ref (attr))
22502 {
22503 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
22504
22505 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
22506 }
22507 else if (attr->form == DW_FORM_ref_sig8)
22508 {
22509 ULONGEST signature = DW_SIGNATURE (attr);
22510
22511 return get_signatured_type (die, signature, cu);
22512 }
22513 else
22514 {
22515 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
22516 " at %s [in module %s]"),
22517 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
22518 objfile_name (objfile));
22519 return build_error_marker_type (cu, die);
22520 }
22521
22522 /* If not cached we need to read it in. */
22523
22524 if (this_type == NULL)
22525 {
22526 struct die_info *type_die = NULL;
22527 struct dwarf2_cu *type_cu = cu;
22528
22529 if (attr_form_is_ref (attr))
22530 type_die = follow_die_ref (die, attr, &type_cu);
22531 if (type_die == NULL)
22532 return build_error_marker_type (cu, die);
22533 /* If we find the type now, it's probably because the type came
22534 from an inter-CU reference and the type's CU got expanded before
22535 ours. */
22536 this_type = read_type_die (type_die, type_cu);
22537 }
22538
22539 /* If we still don't have a type use an error marker. */
22540
22541 if (this_type == NULL)
22542 return build_error_marker_type (cu, die);
22543
22544 return this_type;
22545 }
22546
22547 /* Return the type in DIE, CU.
22548 Returns NULL for invalid types.
22549
22550 This first does a lookup in die_type_hash,
22551 and only reads the die in if necessary.
22552
22553 NOTE: This can be called when reading in partial or full symbols. */
22554
22555 static struct type *
22556 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
22557 {
22558 struct type *this_type;
22559
22560 this_type = get_die_type (die, cu);
22561 if (this_type)
22562 return this_type;
22563
22564 return read_type_die_1 (die, cu);
22565 }
22566
22567 /* Read the type in DIE, CU.
22568 Returns NULL for invalid types. */
22569
22570 static struct type *
22571 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22572 {
22573 struct type *this_type = NULL;
22574
22575 switch (die->tag)
22576 {
22577 case DW_TAG_class_type:
22578 case DW_TAG_interface_type:
22579 case DW_TAG_structure_type:
22580 case DW_TAG_union_type:
22581 this_type = read_structure_type (die, cu);
22582 break;
22583 case DW_TAG_enumeration_type:
22584 this_type = read_enumeration_type (die, cu);
22585 break;
22586 case DW_TAG_subprogram:
22587 case DW_TAG_subroutine_type:
22588 case DW_TAG_inlined_subroutine:
22589 this_type = read_subroutine_type (die, cu);
22590 break;
22591 case DW_TAG_array_type:
22592 this_type = read_array_type (die, cu);
22593 break;
22594 case DW_TAG_set_type:
22595 this_type = read_set_type (die, cu);
22596 break;
22597 case DW_TAG_pointer_type:
22598 this_type = read_tag_pointer_type (die, cu);
22599 break;
22600 case DW_TAG_ptr_to_member_type:
22601 this_type = read_tag_ptr_to_member_type (die, cu);
22602 break;
22603 case DW_TAG_reference_type:
22604 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22605 break;
22606 case DW_TAG_rvalue_reference_type:
22607 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
22608 break;
22609 case DW_TAG_const_type:
22610 this_type = read_tag_const_type (die, cu);
22611 break;
22612 case DW_TAG_volatile_type:
22613 this_type = read_tag_volatile_type (die, cu);
22614 break;
22615 case DW_TAG_restrict_type:
22616 this_type = read_tag_restrict_type (die, cu);
22617 break;
22618 case DW_TAG_string_type:
22619 this_type = read_tag_string_type (die, cu);
22620 break;
22621 case DW_TAG_typedef:
22622 this_type = read_typedef (die, cu);
22623 break;
22624 case DW_TAG_subrange_type:
22625 this_type = read_subrange_type (die, cu);
22626 break;
22627 case DW_TAG_base_type:
22628 this_type = read_base_type (die, cu);
22629 break;
22630 case DW_TAG_unspecified_type:
22631 this_type = read_unspecified_type (die, cu);
22632 break;
22633 case DW_TAG_namespace:
22634 this_type = read_namespace_type (die, cu);
22635 break;
22636 case DW_TAG_module:
22637 this_type = read_module_type (die, cu);
22638 break;
22639 case DW_TAG_atomic_type:
22640 this_type = read_tag_atomic_type (die, cu);
22641 break;
22642 default:
22643 complaint (_("unexpected tag in read_type_die: '%s'"),
22644 dwarf_tag_name (die->tag));
22645 break;
22646 }
22647
22648 return this_type;
22649 }
22650
22651 /* See if we can figure out if the class lives in a namespace. We do
22652 this by looking for a member function; its demangled name will
22653 contain namespace info, if there is any.
22654 Return the computed name or NULL.
22655 Space for the result is allocated on the objfile's obstack.
22656 This is the full-die version of guess_partial_die_structure_name.
22657 In this case we know DIE has no useful parent. */
22658
22659 static const char *
22660 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22661 {
22662 struct die_info *spec_die;
22663 struct dwarf2_cu *spec_cu;
22664 struct die_info *child;
22665 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22666
22667 spec_cu = cu;
22668 spec_die = die_specification (die, &spec_cu);
22669 if (spec_die != NULL)
22670 {
22671 die = spec_die;
22672 cu = spec_cu;
22673 }
22674
22675 for (child = die->child;
22676 child != NULL;
22677 child = child->sibling)
22678 {
22679 if (child->tag == DW_TAG_subprogram)
22680 {
22681 const char *linkage_name = dw2_linkage_name (child, cu);
22682
22683 if (linkage_name != NULL)
22684 {
22685 gdb::unique_xmalloc_ptr<char> actual_name
22686 (language_class_name_from_physname (cu->language_defn,
22687 linkage_name));
22688 const char *name = NULL;
22689
22690 if (actual_name != NULL)
22691 {
22692 const char *die_name = dwarf2_name (die, cu);
22693
22694 if (die_name != NULL
22695 && strcmp (die_name, actual_name.get ()) != 0)
22696 {
22697 /* Strip off the class name from the full name.
22698 We want the prefix. */
22699 int die_name_len = strlen (die_name);
22700 int actual_name_len = strlen (actual_name.get ());
22701 const char *ptr = actual_name.get ();
22702
22703 /* Test for '::' as a sanity check. */
22704 if (actual_name_len > die_name_len + 2
22705 && ptr[actual_name_len - die_name_len - 1] == ':')
22706 name = obstack_strndup (
22707 &objfile->per_bfd->storage_obstack,
22708 ptr, actual_name_len - die_name_len - 2);
22709 }
22710 }
22711 return name;
22712 }
22713 }
22714 }
22715
22716 return NULL;
22717 }
22718
22719 /* GCC might emit a nameless typedef that has a linkage name. Determine the
22720 prefix part in such case. See
22721 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22722
22723 static const char *
22724 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22725 {
22726 struct attribute *attr;
22727 const char *base;
22728
22729 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22730 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22731 return NULL;
22732
22733 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
22734 return NULL;
22735
22736 attr = dw2_linkage_name_attr (die, cu);
22737 if (attr == NULL || DW_STRING (attr) == NULL)
22738 return NULL;
22739
22740 /* dwarf2_name had to be already called. */
22741 gdb_assert (DW_STRING_IS_CANONICAL (attr));
22742
22743 /* Strip the base name, keep any leading namespaces/classes. */
22744 base = strrchr (DW_STRING (attr), ':');
22745 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
22746 return "";
22747
22748 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22749 return obstack_strndup (&objfile->per_bfd->storage_obstack,
22750 DW_STRING (attr),
22751 &base[-1] - DW_STRING (attr));
22752 }
22753
22754 /* Return the name of the namespace/class that DIE is defined within,
22755 or "" if we can't tell. The caller should not xfree the result.
22756
22757 For example, if we're within the method foo() in the following
22758 code:
22759
22760 namespace N {
22761 class C {
22762 void foo () {
22763 }
22764 };
22765 }
22766
22767 then determine_prefix on foo's die will return "N::C". */
22768
22769 static const char *
22770 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
22771 {
22772 struct dwarf2_per_objfile *dwarf2_per_objfile
22773 = cu->per_cu->dwarf2_per_objfile;
22774 struct die_info *parent, *spec_die;
22775 struct dwarf2_cu *spec_cu;
22776 struct type *parent_type;
22777 const char *retval;
22778
22779 if (cu->language != language_cplus
22780 && cu->language != language_fortran && cu->language != language_d
22781 && cu->language != language_rust)
22782 return "";
22783
22784 retval = anonymous_struct_prefix (die, cu);
22785 if (retval)
22786 return retval;
22787
22788 /* We have to be careful in the presence of DW_AT_specification.
22789 For example, with GCC 3.4, given the code
22790
22791 namespace N {
22792 void foo() {
22793 // Definition of N::foo.
22794 }
22795 }
22796
22797 then we'll have a tree of DIEs like this:
22798
22799 1: DW_TAG_compile_unit
22800 2: DW_TAG_namespace // N
22801 3: DW_TAG_subprogram // declaration of N::foo
22802 4: DW_TAG_subprogram // definition of N::foo
22803 DW_AT_specification // refers to die #3
22804
22805 Thus, when processing die #4, we have to pretend that we're in
22806 the context of its DW_AT_specification, namely the contex of die
22807 #3. */
22808 spec_cu = cu;
22809 spec_die = die_specification (die, &spec_cu);
22810 if (spec_die == NULL)
22811 parent = die->parent;
22812 else
22813 {
22814 parent = spec_die->parent;
22815 cu = spec_cu;
22816 }
22817
22818 if (parent == NULL)
22819 return "";
22820 else if (parent->building_fullname)
22821 {
22822 const char *name;
22823 const char *parent_name;
22824
22825 /* It has been seen on RealView 2.2 built binaries,
22826 DW_TAG_template_type_param types actually _defined_ as
22827 children of the parent class:
22828
22829 enum E {};
22830 template class <class Enum> Class{};
22831 Class<enum E> class_e;
22832
22833 1: DW_TAG_class_type (Class)
22834 2: DW_TAG_enumeration_type (E)
22835 3: DW_TAG_enumerator (enum1:0)
22836 3: DW_TAG_enumerator (enum2:1)
22837 ...
22838 2: DW_TAG_template_type_param
22839 DW_AT_type DW_FORM_ref_udata (E)
22840
22841 Besides being broken debug info, it can put GDB into an
22842 infinite loop. Consider:
22843
22844 When we're building the full name for Class<E>, we'll start
22845 at Class, and go look over its template type parameters,
22846 finding E. We'll then try to build the full name of E, and
22847 reach here. We're now trying to build the full name of E,
22848 and look over the parent DIE for containing scope. In the
22849 broken case, if we followed the parent DIE of E, we'd again
22850 find Class, and once again go look at its template type
22851 arguments, etc., etc. Simply don't consider such parent die
22852 as source-level parent of this die (it can't be, the language
22853 doesn't allow it), and break the loop here. */
22854 name = dwarf2_name (die, cu);
22855 parent_name = dwarf2_name (parent, cu);
22856 complaint (_("template param type '%s' defined within parent '%s'"),
22857 name ? name : "<unknown>",
22858 parent_name ? parent_name : "<unknown>");
22859 return "";
22860 }
22861 else
22862 switch (parent->tag)
22863 {
22864 case DW_TAG_namespace:
22865 parent_type = read_type_die (parent, cu);
22866 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22867 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22868 Work around this problem here. */
22869 if (cu->language == language_cplus
22870 && strcmp (TYPE_NAME (parent_type), "::") == 0)
22871 return "";
22872 /* We give a name to even anonymous namespaces. */
22873 return TYPE_NAME (parent_type);
22874 case DW_TAG_class_type:
22875 case DW_TAG_interface_type:
22876 case DW_TAG_structure_type:
22877 case DW_TAG_union_type:
22878 case DW_TAG_module:
22879 parent_type = read_type_die (parent, cu);
22880 if (TYPE_NAME (parent_type) != NULL)
22881 return TYPE_NAME (parent_type);
22882 else
22883 /* An anonymous structure is only allowed non-static data
22884 members; no typedefs, no member functions, et cetera.
22885 So it does not need a prefix. */
22886 return "";
22887 case DW_TAG_compile_unit:
22888 case DW_TAG_partial_unit:
22889 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22890 if (cu->language == language_cplus
22891 && !dwarf2_per_objfile->types.empty ()
22892 && die->child != NULL
22893 && (die->tag == DW_TAG_class_type
22894 || die->tag == DW_TAG_structure_type
22895 || die->tag == DW_TAG_union_type))
22896 {
22897 const char *name = guess_full_die_structure_name (die, cu);
22898 if (name != NULL)
22899 return name;
22900 }
22901 return "";
22902 case DW_TAG_subprogram:
22903 /* Nested subroutines in Fortran get a prefix with the name
22904 of the parent's subroutine. */
22905 if (cu->language == language_fortran)
22906 {
22907 if ((die->tag == DW_TAG_subprogram)
22908 && (dwarf2_name (parent, cu) != NULL))
22909 return dwarf2_name (parent, cu);
22910 }
22911 return determine_prefix (parent, cu);
22912 case DW_TAG_enumeration_type:
22913 parent_type = read_type_die (parent, cu);
22914 if (TYPE_DECLARED_CLASS (parent_type))
22915 {
22916 if (TYPE_NAME (parent_type) != NULL)
22917 return TYPE_NAME (parent_type);
22918 return "";
22919 }
22920 /* Fall through. */
22921 default:
22922 return determine_prefix (parent, cu);
22923 }
22924 }
22925
22926 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22927 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22928 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22929 an obconcat, otherwise allocate storage for the result. The CU argument is
22930 used to determine the language and hence, the appropriate separator. */
22931
22932 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
22933
22934 static char *
22935 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22936 int physname, struct dwarf2_cu *cu)
22937 {
22938 const char *lead = "";
22939 const char *sep;
22940
22941 if (suffix == NULL || suffix[0] == '\0'
22942 || prefix == NULL || prefix[0] == '\0')
22943 sep = "";
22944 else if (cu->language == language_d)
22945 {
22946 /* For D, the 'main' function could be defined in any module, but it
22947 should never be prefixed. */
22948 if (strcmp (suffix, "D main") == 0)
22949 {
22950 prefix = "";
22951 sep = "";
22952 }
22953 else
22954 sep = ".";
22955 }
22956 else if (cu->language == language_fortran && physname)
22957 {
22958 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22959 DW_AT_MIPS_linkage_name is preferred and used instead. */
22960
22961 lead = "__";
22962 sep = "_MOD_";
22963 }
22964 else
22965 sep = "::";
22966
22967 if (prefix == NULL)
22968 prefix = "";
22969 if (suffix == NULL)
22970 suffix = "";
22971
22972 if (obs == NULL)
22973 {
22974 char *retval
22975 = ((char *)
22976 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
22977
22978 strcpy (retval, lead);
22979 strcat (retval, prefix);
22980 strcat (retval, sep);
22981 strcat (retval, suffix);
22982 return retval;
22983 }
22984 else
22985 {
22986 /* We have an obstack. */
22987 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
22988 }
22989 }
22990
22991 /* Return sibling of die, NULL if no sibling. */
22992
22993 static struct die_info *
22994 sibling_die (struct die_info *die)
22995 {
22996 return die->sibling;
22997 }
22998
22999 /* Get name of a die, return NULL if not found. */
23000
23001 static const char *
23002 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
23003 struct obstack *obstack)
23004 {
23005 if (name && cu->language == language_cplus)
23006 {
23007 std::string canon_name = cp_canonicalize_string (name);
23008
23009 if (!canon_name.empty ())
23010 {
23011 if (canon_name != name)
23012 name = obstack_strdup (obstack, canon_name);
23013 }
23014 }
23015
23016 return name;
23017 }
23018
23019 /* Get name of a die, return NULL if not found.
23020 Anonymous namespaces are converted to their magic string. */
23021
23022 static const char *
23023 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
23024 {
23025 struct attribute *attr;
23026 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
23027
23028 attr = dwarf2_attr (die, DW_AT_name, cu);
23029 if ((!attr || !DW_STRING (attr))
23030 && die->tag != DW_TAG_namespace
23031 && die->tag != DW_TAG_class_type
23032 && die->tag != DW_TAG_interface_type
23033 && die->tag != DW_TAG_structure_type
23034 && die->tag != DW_TAG_union_type)
23035 return NULL;
23036
23037 switch (die->tag)
23038 {
23039 case DW_TAG_compile_unit:
23040 case DW_TAG_partial_unit:
23041 /* Compilation units have a DW_AT_name that is a filename, not
23042 a source language identifier. */
23043 case DW_TAG_enumeration_type:
23044 case DW_TAG_enumerator:
23045 /* These tags always have simple identifiers already; no need
23046 to canonicalize them. */
23047 return DW_STRING (attr);
23048
23049 case DW_TAG_namespace:
23050 if (attr != NULL && DW_STRING (attr) != NULL)
23051 return DW_STRING (attr);
23052 return CP_ANONYMOUS_NAMESPACE_STR;
23053
23054 case DW_TAG_class_type:
23055 case DW_TAG_interface_type:
23056 case DW_TAG_structure_type:
23057 case DW_TAG_union_type:
23058 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
23059 structures or unions. These were of the form "._%d" in GCC 4.1,
23060 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
23061 and GCC 4.4. We work around this problem by ignoring these. */
23062 if (attr && DW_STRING (attr)
23063 && (startswith (DW_STRING (attr), "._")
23064 || startswith (DW_STRING (attr), "<anonymous")))
23065 return NULL;
23066
23067 /* GCC might emit a nameless typedef that has a linkage name. See
23068 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
23069 if (!attr || DW_STRING (attr) == NULL)
23070 {
23071 attr = dw2_linkage_name_attr (die, cu);
23072 if (attr == NULL || DW_STRING (attr) == NULL)
23073 return NULL;
23074
23075 /* Avoid demangling DW_STRING (attr) the second time on a second
23076 call for the same DIE. */
23077 if (!DW_STRING_IS_CANONICAL (attr))
23078 {
23079 gdb::unique_xmalloc_ptr<char> demangled
23080 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
23081
23082 const char *base;
23083
23084 /* FIXME: we already did this for the partial symbol... */
23085 DW_STRING (attr)
23086 = obstack_strdup (&objfile->per_bfd->storage_obstack,
23087 demangled.get ());
23088 DW_STRING_IS_CANONICAL (attr) = 1;
23089
23090 /* Strip any leading namespaces/classes, keep only the base name.
23091 DW_AT_name for named DIEs does not contain the prefixes. */
23092 base = strrchr (DW_STRING (attr), ':');
23093 if (base && base > DW_STRING (attr) && base[-1] == ':')
23094 return &base[1];
23095 else
23096 return DW_STRING (attr);
23097 }
23098 }
23099 break;
23100
23101 default:
23102 break;
23103 }
23104
23105 if (!DW_STRING_IS_CANONICAL (attr))
23106 {
23107 DW_STRING (attr)
23108 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
23109 &objfile->per_bfd->storage_obstack);
23110 DW_STRING_IS_CANONICAL (attr) = 1;
23111 }
23112 return DW_STRING (attr);
23113 }
23114
23115 /* Return the die that this die in an extension of, or NULL if there
23116 is none. *EXT_CU is the CU containing DIE on input, and the CU
23117 containing the return value on output. */
23118
23119 static struct die_info *
23120 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
23121 {
23122 struct attribute *attr;
23123
23124 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
23125 if (attr == NULL)
23126 return NULL;
23127
23128 return follow_die_ref (die, attr, ext_cu);
23129 }
23130
23131 /* A convenience function that returns an "unknown" DWARF name,
23132 including the value of V. STR is the name of the entity being
23133 printed, e.g., "TAG". */
23134
23135 static const char *
23136 dwarf_unknown (const char *str, unsigned v)
23137 {
23138 char *cell = get_print_cell ();
23139 xsnprintf (cell, PRINT_CELL_SIZE, "DW_%s_<unknown: %u>", str, v);
23140 return cell;
23141 }
23142
23143 /* Convert a DIE tag into its string name. */
23144
23145 static const char *
23146 dwarf_tag_name (unsigned tag)
23147 {
23148 const char *name = get_DW_TAG_name (tag);
23149
23150 if (name == NULL)
23151 return dwarf_unknown ("TAG", tag);
23152
23153 return name;
23154 }
23155
23156 /* Convert a DWARF attribute code into its string name. */
23157
23158 static const char *
23159 dwarf_attr_name (unsigned attr)
23160 {
23161 const char *name;
23162
23163 #ifdef MIPS /* collides with DW_AT_HP_block_index */
23164 if (attr == DW_AT_MIPS_fde)
23165 return "DW_AT_MIPS_fde";
23166 #else
23167 if (attr == DW_AT_HP_block_index)
23168 return "DW_AT_HP_block_index";
23169 #endif
23170
23171 name = get_DW_AT_name (attr);
23172
23173 if (name == NULL)
23174 return dwarf_unknown ("AT", attr);
23175
23176 return name;
23177 }
23178
23179 /* Convert a unit type to corresponding DW_UT name. */
23180
23181 static const char *
23182 dwarf_unit_type_name (int unit_type) {
23183 switch (unit_type)
23184 {
23185 case 0x01:
23186 return "DW_UT_compile (0x01)";
23187 case 0x02:
23188 return "DW_UT_type (0x02)";
23189 case 0x03:
23190 return "DW_UT_partial (0x03)";
23191 case 0x04:
23192 return "DW_UT_skeleton (0x04)";
23193 case 0x05:
23194 return "DW_UT_split_compile (0x05)";
23195 case 0x06:
23196 return "DW_UT_split_type (0x06)";
23197 case 0x80:
23198 return "DW_UT_lo_user (0x80)";
23199 case 0xff:
23200 return "DW_UT_hi_user (0xff)";
23201 default:
23202 return nullptr;
23203 }
23204 }
23205
23206 /* Convert a DWARF value form code into its string name. */
23207
23208 static const char *
23209 dwarf_form_name (unsigned form)
23210 {
23211 const char *name = get_DW_FORM_name (form);
23212
23213 if (name == NULL)
23214 return dwarf_unknown ("FORM", form);
23215
23216 return name;
23217 }
23218
23219 static const char *
23220 dwarf_bool_name (unsigned mybool)
23221 {
23222 if (mybool)
23223 return "TRUE";
23224 else
23225 return "FALSE";
23226 }
23227
23228 /* Convert a DWARF type code into its string name. */
23229
23230 static const char *
23231 dwarf_type_encoding_name (unsigned enc)
23232 {
23233 const char *name = get_DW_ATE_name (enc);
23234
23235 if (name == NULL)
23236 return dwarf_unknown ("ATE", enc);
23237
23238 return name;
23239 }
23240
23241 static void
23242 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
23243 {
23244 unsigned int i;
23245
23246 print_spaces (indent, f);
23247 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
23248 dwarf_tag_name (die->tag), die->abbrev,
23249 sect_offset_str (die->sect_off));
23250
23251 if (die->parent != NULL)
23252 {
23253 print_spaces (indent, f);
23254 fprintf_unfiltered (f, " parent at offset: %s\n",
23255 sect_offset_str (die->parent->sect_off));
23256 }
23257
23258 print_spaces (indent, f);
23259 fprintf_unfiltered (f, " has children: %s\n",
23260 dwarf_bool_name (die->child != NULL));
23261
23262 print_spaces (indent, f);
23263 fprintf_unfiltered (f, " attributes:\n");
23264
23265 for (i = 0; i < die->num_attrs; ++i)
23266 {
23267 print_spaces (indent, f);
23268 fprintf_unfiltered (f, " %s (%s) ",
23269 dwarf_attr_name (die->attrs[i].name),
23270 dwarf_form_name (die->attrs[i].form));
23271
23272 switch (die->attrs[i].form)
23273 {
23274 case DW_FORM_addr:
23275 case DW_FORM_addrx:
23276 case DW_FORM_GNU_addr_index:
23277 fprintf_unfiltered (f, "address: ");
23278 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
23279 break;
23280 case DW_FORM_block2:
23281 case DW_FORM_block4:
23282 case DW_FORM_block:
23283 case DW_FORM_block1:
23284 fprintf_unfiltered (f, "block: size %s",
23285 pulongest (DW_BLOCK (&die->attrs[i])->size));
23286 break;
23287 case DW_FORM_exprloc:
23288 fprintf_unfiltered (f, "expression: size %s",
23289 pulongest (DW_BLOCK (&die->attrs[i])->size));
23290 break;
23291 case DW_FORM_data16:
23292 fprintf_unfiltered (f, "constant of 16 bytes");
23293 break;
23294 case DW_FORM_ref_addr:
23295 fprintf_unfiltered (f, "ref address: ");
23296 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
23297 break;
23298 case DW_FORM_GNU_ref_alt:
23299 fprintf_unfiltered (f, "alt ref address: ");
23300 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
23301 break;
23302 case DW_FORM_ref1:
23303 case DW_FORM_ref2:
23304 case DW_FORM_ref4:
23305 case DW_FORM_ref8:
23306 case DW_FORM_ref_udata:
23307 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
23308 (long) (DW_UNSND (&die->attrs[i])));
23309 break;
23310 case DW_FORM_data1:
23311 case DW_FORM_data2:
23312 case DW_FORM_data4:
23313 case DW_FORM_data8:
23314 case DW_FORM_udata:
23315 case DW_FORM_sdata:
23316 fprintf_unfiltered (f, "constant: %s",
23317 pulongest (DW_UNSND (&die->attrs[i])));
23318 break;
23319 case DW_FORM_sec_offset:
23320 fprintf_unfiltered (f, "section offset: %s",
23321 pulongest (DW_UNSND (&die->attrs[i])));
23322 break;
23323 case DW_FORM_ref_sig8:
23324 fprintf_unfiltered (f, "signature: %s",
23325 hex_string (DW_SIGNATURE (&die->attrs[i])));
23326 break;
23327 case DW_FORM_string:
23328 case DW_FORM_strp:
23329 case DW_FORM_line_strp:
23330 case DW_FORM_strx:
23331 case DW_FORM_GNU_str_index:
23332 case DW_FORM_GNU_strp_alt:
23333 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
23334 DW_STRING (&die->attrs[i])
23335 ? DW_STRING (&die->attrs[i]) : "",
23336 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
23337 break;
23338 case DW_FORM_flag:
23339 if (DW_UNSND (&die->attrs[i]))
23340 fprintf_unfiltered (f, "flag: TRUE");
23341 else
23342 fprintf_unfiltered (f, "flag: FALSE");
23343 break;
23344 case DW_FORM_flag_present:
23345 fprintf_unfiltered (f, "flag: TRUE");
23346 break;
23347 case DW_FORM_indirect:
23348 /* The reader will have reduced the indirect form to
23349 the "base form" so this form should not occur. */
23350 fprintf_unfiltered (f,
23351 "unexpected attribute form: DW_FORM_indirect");
23352 break;
23353 case DW_FORM_implicit_const:
23354 fprintf_unfiltered (f, "constant: %s",
23355 plongest (DW_SND (&die->attrs[i])));
23356 break;
23357 default:
23358 fprintf_unfiltered (f, "unsupported attribute form: %d.",
23359 die->attrs[i].form);
23360 break;
23361 }
23362 fprintf_unfiltered (f, "\n");
23363 }
23364 }
23365
23366 static void
23367 dump_die_for_error (struct die_info *die)
23368 {
23369 dump_die_shallow (gdb_stderr, 0, die);
23370 }
23371
23372 static void
23373 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
23374 {
23375 int indent = level * 4;
23376
23377 gdb_assert (die != NULL);
23378
23379 if (level >= max_level)
23380 return;
23381
23382 dump_die_shallow (f, indent, die);
23383
23384 if (die->child != NULL)
23385 {
23386 print_spaces (indent, f);
23387 fprintf_unfiltered (f, " Children:");
23388 if (level + 1 < max_level)
23389 {
23390 fprintf_unfiltered (f, "\n");
23391 dump_die_1 (f, level + 1, max_level, die->child);
23392 }
23393 else
23394 {
23395 fprintf_unfiltered (f,
23396 " [not printed, max nesting level reached]\n");
23397 }
23398 }
23399
23400 if (die->sibling != NULL && level > 0)
23401 {
23402 dump_die_1 (f, level, max_level, die->sibling);
23403 }
23404 }
23405
23406 /* This is called from the pdie macro in gdbinit.in.
23407 It's not static so gcc will keep a copy callable from gdb. */
23408
23409 void
23410 dump_die (struct die_info *die, int max_level)
23411 {
23412 dump_die_1 (gdb_stdlog, 0, max_level, die);
23413 }
23414
23415 static void
23416 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
23417 {
23418 void **slot;
23419
23420 slot = htab_find_slot_with_hash (cu->die_hash, die,
23421 to_underlying (die->sect_off),
23422 INSERT);
23423
23424 *slot = die;
23425 }
23426
23427 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
23428 required kind. */
23429
23430 static sect_offset
23431 dwarf2_get_ref_die_offset (const struct attribute *attr)
23432 {
23433 if (attr_form_is_ref (attr))
23434 return (sect_offset) DW_UNSND (attr);
23435
23436 complaint (_("unsupported die ref attribute form: '%s'"),
23437 dwarf_form_name (attr->form));
23438 return {};
23439 }
23440
23441 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
23442 * the value held by the attribute is not constant. */
23443
23444 static LONGEST
23445 dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
23446 {
23447 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
23448 return DW_SND (attr);
23449 else if (attr->form == DW_FORM_udata
23450 || attr->form == DW_FORM_data1
23451 || attr->form == DW_FORM_data2
23452 || attr->form == DW_FORM_data4
23453 || attr->form == DW_FORM_data8)
23454 return DW_UNSND (attr);
23455 else
23456 {
23457 /* For DW_FORM_data16 see attr_form_is_constant. */
23458 complaint (_("Attribute value is not a constant (%s)"),
23459 dwarf_form_name (attr->form));
23460 return default_value;
23461 }
23462 }
23463
23464 /* Follow reference or signature attribute ATTR of SRC_DIE.
23465 On entry *REF_CU is the CU of SRC_DIE.
23466 On exit *REF_CU is the CU of the result. */
23467
23468 static struct die_info *
23469 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
23470 struct dwarf2_cu **ref_cu)
23471 {
23472 struct die_info *die;
23473
23474 if (attr_form_is_ref (attr))
23475 die = follow_die_ref (src_die, attr, ref_cu);
23476 else if (attr->form == DW_FORM_ref_sig8)
23477 die = follow_die_sig (src_die, attr, ref_cu);
23478 else
23479 {
23480 dump_die_for_error (src_die);
23481 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
23482 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23483 }
23484
23485 return die;
23486 }
23487
23488 /* Follow reference OFFSET.
23489 On entry *REF_CU is the CU of the source die referencing OFFSET.
23490 On exit *REF_CU is the CU of the result.
23491 Returns NULL if OFFSET is invalid. */
23492
23493 static struct die_info *
23494 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
23495 struct dwarf2_cu **ref_cu)
23496 {
23497 struct die_info temp_die;
23498 struct dwarf2_cu *target_cu, *cu = *ref_cu;
23499 struct dwarf2_per_objfile *dwarf2_per_objfile
23500 = cu->per_cu->dwarf2_per_objfile;
23501
23502 gdb_assert (cu->per_cu != NULL);
23503
23504 target_cu = cu;
23505
23506 if (cu->per_cu->is_debug_types)
23507 {
23508 /* .debug_types CUs cannot reference anything outside their CU.
23509 If they need to, they have to reference a signatured type via
23510 DW_FORM_ref_sig8. */
23511 if (!offset_in_cu_p (&cu->header, sect_off))
23512 return NULL;
23513 }
23514 else if (offset_in_dwz != cu->per_cu->is_dwz
23515 || !offset_in_cu_p (&cu->header, sect_off))
23516 {
23517 struct dwarf2_per_cu_data *per_cu;
23518
23519 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
23520 dwarf2_per_objfile);
23521
23522 /* If necessary, add it to the queue and load its DIEs. */
23523 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
23524 load_full_comp_unit (per_cu, false, cu->language);
23525
23526 target_cu = per_cu->cu;
23527 }
23528 else if (cu->dies == NULL)
23529 {
23530 /* We're loading full DIEs during partial symbol reading. */
23531 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
23532 load_full_comp_unit (cu->per_cu, false, language_minimal);
23533 }
23534
23535 *ref_cu = target_cu;
23536 temp_die.sect_off = sect_off;
23537
23538 if (target_cu != cu)
23539 target_cu->ancestor = cu;
23540
23541 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
23542 &temp_die,
23543 to_underlying (sect_off));
23544 }
23545
23546 /* Follow reference attribute ATTR of SRC_DIE.
23547 On entry *REF_CU is the CU of SRC_DIE.
23548 On exit *REF_CU is the CU of the result. */
23549
23550 static struct die_info *
23551 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
23552 struct dwarf2_cu **ref_cu)
23553 {
23554 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
23555 struct dwarf2_cu *cu = *ref_cu;
23556 struct die_info *die;
23557
23558 die = follow_die_offset (sect_off,
23559 (attr->form == DW_FORM_GNU_ref_alt
23560 || cu->per_cu->is_dwz),
23561 ref_cu);
23562 if (!die)
23563 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
23564 "at %s [in module %s]"),
23565 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
23566 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
23567
23568 return die;
23569 }
23570
23571 /* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
23572 Returned value is intended for DW_OP_call*. Returned
23573 dwarf2_locexpr_baton->data has lifetime of
23574 PER_CU->DWARF2_PER_OBJFILE->OBJFILE. */
23575
23576 struct dwarf2_locexpr_baton
23577 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
23578 struct dwarf2_per_cu_data *per_cu,
23579 CORE_ADDR (*get_frame_pc) (void *baton),
23580 void *baton, bool resolve_abstract_p)
23581 {
23582 struct dwarf2_cu *cu;
23583 struct die_info *die;
23584 struct attribute *attr;
23585 struct dwarf2_locexpr_baton retval;
23586 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
23587 struct objfile *objfile = dwarf2_per_objfile->objfile;
23588
23589 if (per_cu->cu == NULL)
23590 load_cu (per_cu, false);
23591 cu = per_cu->cu;
23592 if (cu == NULL)
23593 {
23594 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23595 Instead just throw an error, not much else we can do. */
23596 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23597 sect_offset_str (sect_off), objfile_name (objfile));
23598 }
23599
23600 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23601 if (!die)
23602 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23603 sect_offset_str (sect_off), objfile_name (objfile));
23604
23605 attr = dwarf2_attr (die, DW_AT_location, cu);
23606 if (!attr && resolve_abstract_p
23607 && (dwarf2_per_objfile->abstract_to_concrete.find (die->sect_off)
23608 != dwarf2_per_objfile->abstract_to_concrete.end ()))
23609 {
23610 CORE_ADDR pc = (*get_frame_pc) (baton);
23611 CORE_ADDR baseaddr = objfile->text_section_offset ();
23612 struct gdbarch *gdbarch = get_objfile_arch (objfile);
23613
23614 for (const auto &cand_off
23615 : dwarf2_per_objfile->abstract_to_concrete[die->sect_off])
23616 {
23617 struct dwarf2_cu *cand_cu = cu;
23618 struct die_info *cand
23619 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
23620 if (!cand
23621 || !cand->parent
23622 || cand->parent->tag != DW_TAG_subprogram)
23623 continue;
23624
23625 CORE_ADDR pc_low, pc_high;
23626 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
23627 if (pc_low == ((CORE_ADDR) -1))
23628 continue;
23629 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
23630 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
23631 if (!(pc_low <= pc && pc < pc_high))
23632 continue;
23633
23634 die = cand;
23635 attr = dwarf2_attr (die, DW_AT_location, cu);
23636 break;
23637 }
23638 }
23639
23640 if (!attr)
23641 {
23642 /* DWARF: "If there is no such attribute, then there is no effect.".
23643 DATA is ignored if SIZE is 0. */
23644
23645 retval.data = NULL;
23646 retval.size = 0;
23647 }
23648 else if (attr_form_is_section_offset (attr))
23649 {
23650 struct dwarf2_loclist_baton loclist_baton;
23651 CORE_ADDR pc = (*get_frame_pc) (baton);
23652 size_t size;
23653
23654 fill_in_loclist_baton (cu, &loclist_baton, attr);
23655
23656 retval.data = dwarf2_find_location_expression (&loclist_baton,
23657 &size, pc);
23658 retval.size = size;
23659 }
23660 else
23661 {
23662 if (!attr_form_is_block (attr))
23663 error (_("Dwarf Error: DIE at %s referenced in module %s "
23664 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
23665 sect_offset_str (sect_off), objfile_name (objfile));
23666
23667 retval.data = DW_BLOCK (attr)->data;
23668 retval.size = DW_BLOCK (attr)->size;
23669 }
23670 retval.per_cu = cu->per_cu;
23671
23672 age_cached_comp_units (dwarf2_per_objfile);
23673
23674 return retval;
23675 }
23676
23677 /* Like dwarf2_fetch_die_loc_sect_off, but take a CU
23678 offset. */
23679
23680 struct dwarf2_locexpr_baton
23681 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
23682 struct dwarf2_per_cu_data *per_cu,
23683 CORE_ADDR (*get_frame_pc) (void *baton),
23684 void *baton)
23685 {
23686 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
23687
23688 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
23689 }
23690
23691 /* Write a constant of a given type as target-ordered bytes into
23692 OBSTACK. */
23693
23694 static const gdb_byte *
23695 write_constant_as_bytes (struct obstack *obstack,
23696 enum bfd_endian byte_order,
23697 struct type *type,
23698 ULONGEST value,
23699 LONGEST *len)
23700 {
23701 gdb_byte *result;
23702
23703 *len = TYPE_LENGTH (type);
23704 result = (gdb_byte *) obstack_alloc (obstack, *len);
23705 store_unsigned_integer (result, *len, byte_order, value);
23706
23707 return result;
23708 }
23709
23710 /* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
23711 pointer to the constant bytes and set LEN to the length of the
23712 data. If memory is needed, allocate it on OBSTACK. If the DIE
23713 does not have a DW_AT_const_value, return NULL. */
23714
23715 const gdb_byte *
23716 dwarf2_fetch_constant_bytes (sect_offset sect_off,
23717 struct dwarf2_per_cu_data *per_cu,
23718 struct obstack *obstack,
23719 LONGEST *len)
23720 {
23721 struct dwarf2_cu *cu;
23722 struct die_info *die;
23723 struct attribute *attr;
23724 const gdb_byte *result = NULL;
23725 struct type *type;
23726 LONGEST value;
23727 enum bfd_endian byte_order;
23728 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
23729
23730 if (per_cu->cu == NULL)
23731 load_cu (per_cu, false);
23732 cu = per_cu->cu;
23733 if (cu == NULL)
23734 {
23735 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23736 Instead just throw an error, not much else we can do. */
23737 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23738 sect_offset_str (sect_off), objfile_name (objfile));
23739 }
23740
23741 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23742 if (!die)
23743 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23744 sect_offset_str (sect_off), objfile_name (objfile));
23745
23746 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23747 if (attr == NULL)
23748 return NULL;
23749
23750 byte_order = (bfd_big_endian (objfile->obfd)
23751 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23752
23753 switch (attr->form)
23754 {
23755 case DW_FORM_addr:
23756 case DW_FORM_addrx:
23757 case DW_FORM_GNU_addr_index:
23758 {
23759 gdb_byte *tem;
23760
23761 *len = cu->header.addr_size;
23762 tem = (gdb_byte *) obstack_alloc (obstack, *len);
23763 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
23764 result = tem;
23765 }
23766 break;
23767 case DW_FORM_string:
23768 case DW_FORM_strp:
23769 case DW_FORM_strx:
23770 case DW_FORM_GNU_str_index:
23771 case DW_FORM_GNU_strp_alt:
23772 /* DW_STRING is already allocated on the objfile obstack, point
23773 directly to it. */
23774 result = (const gdb_byte *) DW_STRING (attr);
23775 *len = strlen (DW_STRING (attr));
23776 break;
23777 case DW_FORM_block1:
23778 case DW_FORM_block2:
23779 case DW_FORM_block4:
23780 case DW_FORM_block:
23781 case DW_FORM_exprloc:
23782 case DW_FORM_data16:
23783 result = DW_BLOCK (attr)->data;
23784 *len = DW_BLOCK (attr)->size;
23785 break;
23786
23787 /* The DW_AT_const_value attributes are supposed to carry the
23788 symbol's value "represented as it would be on the target
23789 architecture." By the time we get here, it's already been
23790 converted to host endianness, so we just need to sign- or
23791 zero-extend it as appropriate. */
23792 case DW_FORM_data1:
23793 type = die_type (die, cu);
23794 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23795 if (result == NULL)
23796 result = write_constant_as_bytes (obstack, byte_order,
23797 type, value, len);
23798 break;
23799 case DW_FORM_data2:
23800 type = die_type (die, cu);
23801 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23802 if (result == NULL)
23803 result = write_constant_as_bytes (obstack, byte_order,
23804 type, value, len);
23805 break;
23806 case DW_FORM_data4:
23807 type = die_type (die, cu);
23808 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23809 if (result == NULL)
23810 result = write_constant_as_bytes (obstack, byte_order,
23811 type, value, len);
23812 break;
23813 case DW_FORM_data8:
23814 type = die_type (die, cu);
23815 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23816 if (result == NULL)
23817 result = write_constant_as_bytes (obstack, byte_order,
23818 type, value, len);
23819 break;
23820
23821 case DW_FORM_sdata:
23822 case DW_FORM_implicit_const:
23823 type = die_type (die, cu);
23824 result = write_constant_as_bytes (obstack, byte_order,
23825 type, DW_SND (attr), len);
23826 break;
23827
23828 case DW_FORM_udata:
23829 type = die_type (die, cu);
23830 result = write_constant_as_bytes (obstack, byte_order,
23831 type, DW_UNSND (attr), len);
23832 break;
23833
23834 default:
23835 complaint (_("unsupported const value attribute form: '%s'"),
23836 dwarf_form_name (attr->form));
23837 break;
23838 }
23839
23840 return result;
23841 }
23842
23843 /* Return the type of the die at OFFSET in PER_CU. Return NULL if no
23844 valid type for this die is found. */
23845
23846 struct type *
23847 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
23848 struct dwarf2_per_cu_data *per_cu)
23849 {
23850 struct dwarf2_cu *cu;
23851 struct die_info *die;
23852
23853 if (per_cu->cu == NULL)
23854 load_cu (per_cu, false);
23855 cu = per_cu->cu;
23856 if (!cu)
23857 return NULL;
23858
23859 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23860 if (!die)
23861 return NULL;
23862
23863 return die_type (die, cu);
23864 }
23865
23866 /* Return the type of the DIE at DIE_OFFSET in the CU named by
23867 PER_CU. */
23868
23869 struct type *
23870 dwarf2_get_die_type (cu_offset die_offset,
23871 struct dwarf2_per_cu_data *per_cu)
23872 {
23873 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
23874 return get_die_type_at_offset (die_offset_sect, per_cu);
23875 }
23876
23877 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
23878 On entry *REF_CU is the CU of SRC_DIE.
23879 On exit *REF_CU is the CU of the result.
23880 Returns NULL if the referenced DIE isn't found. */
23881
23882 static struct die_info *
23883 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23884 struct dwarf2_cu **ref_cu)
23885 {
23886 struct die_info temp_die;
23887 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
23888 struct die_info *die;
23889
23890 /* While it might be nice to assert sig_type->type == NULL here,
23891 we can get here for DW_AT_imported_declaration where we need
23892 the DIE not the type. */
23893
23894 /* If necessary, add it to the queue and load its DIEs. */
23895
23896 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
23897 read_signatured_type (sig_type);
23898
23899 sig_cu = sig_type->per_cu.cu;
23900 gdb_assert (sig_cu != NULL);
23901 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23902 temp_die.sect_off = sig_type->type_offset_in_section;
23903 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
23904 to_underlying (temp_die.sect_off));
23905 if (die)
23906 {
23907 struct dwarf2_per_objfile *dwarf2_per_objfile
23908 = (*ref_cu)->per_cu->dwarf2_per_objfile;
23909
23910 /* For .gdb_index version 7 keep track of included TUs.
23911 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23912 if (dwarf2_per_objfile->index_table != NULL
23913 && dwarf2_per_objfile->index_table->version <= 7)
23914 {
23915 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
23916 }
23917
23918 *ref_cu = sig_cu;
23919 if (sig_cu != cu)
23920 sig_cu->ancestor = cu;
23921
23922 return die;
23923 }
23924
23925 return NULL;
23926 }
23927
23928 /* Follow signatured type referenced by ATTR in SRC_DIE.
23929 On entry *REF_CU is the CU of SRC_DIE.
23930 On exit *REF_CU is the CU of the result.
23931 The result is the DIE of the type.
23932 If the referenced type cannot be found an error is thrown. */
23933
23934 static struct die_info *
23935 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
23936 struct dwarf2_cu **ref_cu)
23937 {
23938 ULONGEST signature = DW_SIGNATURE (attr);
23939 struct signatured_type *sig_type;
23940 struct die_info *die;
23941
23942 gdb_assert (attr->form == DW_FORM_ref_sig8);
23943
23944 sig_type = lookup_signatured_type (*ref_cu, signature);
23945 /* sig_type will be NULL if the signatured type is missing from
23946 the debug info. */
23947 if (sig_type == NULL)
23948 {
23949 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23950 " from DIE at %s [in module %s]"),
23951 hex_string (signature), sect_offset_str (src_die->sect_off),
23952 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23953 }
23954
23955 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23956 if (die == NULL)
23957 {
23958 dump_die_for_error (src_die);
23959 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23960 " from DIE at %s [in module %s]"),
23961 hex_string (signature), sect_offset_str (src_die->sect_off),
23962 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23963 }
23964
23965 return die;
23966 }
23967
23968 /* Get the type specified by SIGNATURE referenced in DIE/CU,
23969 reading in and processing the type unit if necessary. */
23970
23971 static struct type *
23972 get_signatured_type (struct die_info *die, ULONGEST signature,
23973 struct dwarf2_cu *cu)
23974 {
23975 struct dwarf2_per_objfile *dwarf2_per_objfile
23976 = cu->per_cu->dwarf2_per_objfile;
23977 struct signatured_type *sig_type;
23978 struct dwarf2_cu *type_cu;
23979 struct die_info *type_die;
23980 struct type *type;
23981
23982 sig_type = lookup_signatured_type (cu, signature);
23983 /* sig_type will be NULL if the signatured type is missing from
23984 the debug info. */
23985 if (sig_type == NULL)
23986 {
23987 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23988 " from DIE at %s [in module %s]"),
23989 hex_string (signature), sect_offset_str (die->sect_off),
23990 objfile_name (dwarf2_per_objfile->objfile));
23991 return build_error_marker_type (cu, die);
23992 }
23993
23994 /* If we already know the type we're done. */
23995 if (sig_type->type != NULL)
23996 return sig_type->type;
23997
23998 type_cu = cu;
23999 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
24000 if (type_die != NULL)
24001 {
24002 /* N.B. We need to call get_die_type to ensure only one type for this DIE
24003 is created. This is important, for example, because for c++ classes
24004 we need TYPE_NAME set which is only done by new_symbol. Blech. */
24005 type = read_type_die (type_die, type_cu);
24006 if (type == NULL)
24007 {
24008 complaint (_("Dwarf Error: Cannot build signatured type %s"
24009 " referenced from DIE at %s [in module %s]"),
24010 hex_string (signature), sect_offset_str (die->sect_off),
24011 objfile_name (dwarf2_per_objfile->objfile));
24012 type = build_error_marker_type (cu, die);
24013 }
24014 }
24015 else
24016 {
24017 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
24018 " from DIE at %s [in module %s]"),
24019 hex_string (signature), sect_offset_str (die->sect_off),
24020 objfile_name (dwarf2_per_objfile->objfile));
24021 type = build_error_marker_type (cu, die);
24022 }
24023 sig_type->type = type;
24024
24025 return type;
24026 }
24027
24028 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
24029 reading in and processing the type unit if necessary. */
24030
24031 static struct type *
24032 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
24033 struct dwarf2_cu *cu) /* ARI: editCase function */
24034 {
24035 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
24036 if (attr_form_is_ref (attr))
24037 {
24038 struct dwarf2_cu *type_cu = cu;
24039 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
24040
24041 return read_type_die (type_die, type_cu);
24042 }
24043 else if (attr->form == DW_FORM_ref_sig8)
24044 {
24045 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
24046 }
24047 else
24048 {
24049 struct dwarf2_per_objfile *dwarf2_per_objfile
24050 = cu->per_cu->dwarf2_per_objfile;
24051
24052 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
24053 " at %s [in module %s]"),
24054 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
24055 objfile_name (dwarf2_per_objfile->objfile));
24056 return build_error_marker_type (cu, die);
24057 }
24058 }
24059
24060 /* Load the DIEs associated with type unit PER_CU into memory. */
24061
24062 static void
24063 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
24064 {
24065 struct signatured_type *sig_type;
24066
24067 /* Caller is responsible for ensuring type_unit_groups don't get here. */
24068 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
24069
24070 /* We have the per_cu, but we need the signatured_type.
24071 Fortunately this is an easy translation. */
24072 gdb_assert (per_cu->is_debug_types);
24073 sig_type = (struct signatured_type *) per_cu;
24074
24075 gdb_assert (per_cu->cu == NULL);
24076
24077 read_signatured_type (sig_type);
24078
24079 gdb_assert (per_cu->cu != NULL);
24080 }
24081
24082 /* Read in a signatured type and build its CU and DIEs.
24083 If the type is a stub for the real type in a DWO file,
24084 read in the real type from the DWO file as well. */
24085
24086 static void
24087 read_signatured_type (struct signatured_type *sig_type)
24088 {
24089 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
24090
24091 gdb_assert (per_cu->is_debug_types);
24092 gdb_assert (per_cu->cu == NULL);
24093
24094 cutu_reader reader (per_cu, NULL, 0, 1, false);
24095
24096 if (!reader.dummy_p)
24097 {
24098 struct dwarf2_cu *cu = reader.cu;
24099 const gdb_byte *info_ptr = reader.info_ptr;
24100
24101 gdb_assert (cu->die_hash == NULL);
24102 cu->die_hash =
24103 htab_create_alloc_ex (cu->header.length / 12,
24104 die_hash,
24105 die_eq,
24106 NULL,
24107 &cu->comp_unit_obstack,
24108 hashtab_obstack_allocate,
24109 dummy_obstack_deallocate);
24110
24111 if (reader.has_children)
24112 reader.comp_unit_die->child
24113 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
24114 reader.comp_unit_die);
24115 cu->dies = reader.comp_unit_die;
24116 /* comp_unit_die is not stored in die_hash, no need. */
24117
24118 /* We try not to read any attributes in this function, because
24119 not all CUs needed for references have been loaded yet, and
24120 symbol table processing isn't initialized. But we have to
24121 set the CU language, or we won't be able to build types
24122 correctly. Similarly, if we do not read the producer, we can
24123 not apply producer-specific interpretation. */
24124 prepare_one_comp_unit (cu, cu->dies, language_minimal);
24125 }
24126
24127 sig_type->per_cu.tu_read = 1;
24128 }
24129
24130 /* Decode simple location descriptions.
24131 Given a pointer to a dwarf block that defines a location, compute
24132 the location and return the value.
24133
24134 NOTE drow/2003-11-18: This function is called in two situations
24135 now: for the address of static or global variables (partial symbols
24136 only) and for offsets into structures which are expected to be
24137 (more or less) constant. The partial symbol case should go away,
24138 and only the constant case should remain. That will let this
24139 function complain more accurately. A few special modes are allowed
24140 without complaint for global variables (for instance, global
24141 register values and thread-local values).
24142
24143 A location description containing no operations indicates that the
24144 object is optimized out. The return value is 0 for that case.
24145 FIXME drow/2003-11-16: No callers check for this case any more; soon all
24146 callers will only want a very basic result and this can become a
24147 complaint.
24148
24149 Note that stack[0] is unused except as a default error return. */
24150
24151 static CORE_ADDR
24152 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
24153 {
24154 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
24155 size_t i;
24156 size_t size = blk->size;
24157 const gdb_byte *data = blk->data;
24158 CORE_ADDR stack[64];
24159 int stacki;
24160 unsigned int bytes_read, unsnd;
24161 gdb_byte op;
24162
24163 i = 0;
24164 stacki = 0;
24165 stack[stacki] = 0;
24166 stack[++stacki] = 0;
24167
24168 while (i < size)
24169 {
24170 op = data[i++];
24171 switch (op)
24172 {
24173 case DW_OP_lit0:
24174 case DW_OP_lit1:
24175 case DW_OP_lit2:
24176 case DW_OP_lit3:
24177 case DW_OP_lit4:
24178 case DW_OP_lit5:
24179 case DW_OP_lit6:
24180 case DW_OP_lit7:
24181 case DW_OP_lit8:
24182 case DW_OP_lit9:
24183 case DW_OP_lit10:
24184 case DW_OP_lit11:
24185 case DW_OP_lit12:
24186 case DW_OP_lit13:
24187 case DW_OP_lit14:
24188 case DW_OP_lit15:
24189 case DW_OP_lit16:
24190 case DW_OP_lit17:
24191 case DW_OP_lit18:
24192 case DW_OP_lit19:
24193 case DW_OP_lit20:
24194 case DW_OP_lit21:
24195 case DW_OP_lit22:
24196 case DW_OP_lit23:
24197 case DW_OP_lit24:
24198 case DW_OP_lit25:
24199 case DW_OP_lit26:
24200 case DW_OP_lit27:
24201 case DW_OP_lit28:
24202 case DW_OP_lit29:
24203 case DW_OP_lit30:
24204 case DW_OP_lit31:
24205 stack[++stacki] = op - DW_OP_lit0;
24206 break;
24207
24208 case DW_OP_reg0:
24209 case DW_OP_reg1:
24210 case DW_OP_reg2:
24211 case DW_OP_reg3:
24212 case DW_OP_reg4:
24213 case DW_OP_reg5:
24214 case DW_OP_reg6:
24215 case DW_OP_reg7:
24216 case DW_OP_reg8:
24217 case DW_OP_reg9:
24218 case DW_OP_reg10:
24219 case DW_OP_reg11:
24220 case DW_OP_reg12:
24221 case DW_OP_reg13:
24222 case DW_OP_reg14:
24223 case DW_OP_reg15:
24224 case DW_OP_reg16:
24225 case DW_OP_reg17:
24226 case DW_OP_reg18:
24227 case DW_OP_reg19:
24228 case DW_OP_reg20:
24229 case DW_OP_reg21:
24230 case DW_OP_reg22:
24231 case DW_OP_reg23:
24232 case DW_OP_reg24:
24233 case DW_OP_reg25:
24234 case DW_OP_reg26:
24235 case DW_OP_reg27:
24236 case DW_OP_reg28:
24237 case DW_OP_reg29:
24238 case DW_OP_reg30:
24239 case DW_OP_reg31:
24240 stack[++stacki] = op - DW_OP_reg0;
24241 if (i < size)
24242 dwarf2_complex_location_expr_complaint ();
24243 break;
24244
24245 case DW_OP_regx:
24246 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
24247 i += bytes_read;
24248 stack[++stacki] = unsnd;
24249 if (i < size)
24250 dwarf2_complex_location_expr_complaint ();
24251 break;
24252
24253 case DW_OP_addr:
24254 stack[++stacki] = read_address (objfile->obfd, &data[i],
24255 cu, &bytes_read);
24256 i += bytes_read;
24257 break;
24258
24259 case DW_OP_const1u:
24260 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
24261 i += 1;
24262 break;
24263
24264 case DW_OP_const1s:
24265 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
24266 i += 1;
24267 break;
24268
24269 case DW_OP_const2u:
24270 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
24271 i += 2;
24272 break;
24273
24274 case DW_OP_const2s:
24275 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
24276 i += 2;
24277 break;
24278
24279 case DW_OP_const4u:
24280 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
24281 i += 4;
24282 break;
24283
24284 case DW_OP_const4s:
24285 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
24286 i += 4;
24287 break;
24288
24289 case DW_OP_const8u:
24290 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
24291 i += 8;
24292 break;
24293
24294 case DW_OP_constu:
24295 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
24296 &bytes_read);
24297 i += bytes_read;
24298 break;
24299
24300 case DW_OP_consts:
24301 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
24302 i += bytes_read;
24303 break;
24304
24305 case DW_OP_dup:
24306 stack[stacki + 1] = stack[stacki];
24307 stacki++;
24308 break;
24309
24310 case DW_OP_plus:
24311 stack[stacki - 1] += stack[stacki];
24312 stacki--;
24313 break;
24314
24315 case DW_OP_plus_uconst:
24316 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
24317 &bytes_read);
24318 i += bytes_read;
24319 break;
24320
24321 case DW_OP_minus:
24322 stack[stacki - 1] -= stack[stacki];
24323 stacki--;
24324 break;
24325
24326 case DW_OP_deref:
24327 /* If we're not the last op, then we definitely can't encode
24328 this using GDB's address_class enum. This is valid for partial
24329 global symbols, although the variable's address will be bogus
24330 in the psymtab. */
24331 if (i < size)
24332 dwarf2_complex_location_expr_complaint ();
24333 break;
24334
24335 case DW_OP_GNU_push_tls_address:
24336 case DW_OP_form_tls_address:
24337 /* The top of the stack has the offset from the beginning
24338 of the thread control block at which the variable is located. */
24339 /* Nothing should follow this operator, so the top of stack would
24340 be returned. */
24341 /* This is valid for partial global symbols, but the variable's
24342 address will be bogus in the psymtab. Make it always at least
24343 non-zero to not look as a variable garbage collected by linker
24344 which have DW_OP_addr 0. */
24345 if (i < size)
24346 dwarf2_complex_location_expr_complaint ();
24347 stack[stacki]++;
24348 break;
24349
24350 case DW_OP_GNU_uninit:
24351 break;
24352
24353 case DW_OP_addrx:
24354 case DW_OP_GNU_addr_index:
24355 case DW_OP_GNU_const_index:
24356 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
24357 &bytes_read);
24358 i += bytes_read;
24359 break;
24360
24361 default:
24362 {
24363 const char *name = get_DW_OP_name (op);
24364
24365 if (name)
24366 complaint (_("unsupported stack op: '%s'"),
24367 name);
24368 else
24369 complaint (_("unsupported stack op: '%02x'"),
24370 op);
24371 }
24372
24373 return (stack[stacki]);
24374 }
24375
24376 /* Enforce maximum stack depth of SIZE-1 to avoid writing
24377 outside of the allocated space. Also enforce minimum>0. */
24378 if (stacki >= ARRAY_SIZE (stack) - 1)
24379 {
24380 complaint (_("location description stack overflow"));
24381 return 0;
24382 }
24383
24384 if (stacki <= 0)
24385 {
24386 complaint (_("location description stack underflow"));
24387 return 0;
24388 }
24389 }
24390 return (stack[stacki]);
24391 }
24392
24393 /* memory allocation interface */
24394
24395 static struct dwarf_block *
24396 dwarf_alloc_block (struct dwarf2_cu *cu)
24397 {
24398 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
24399 }
24400
24401 static struct die_info *
24402 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
24403 {
24404 struct die_info *die;
24405 size_t size = sizeof (struct die_info);
24406
24407 if (num_attrs > 1)
24408 size += (num_attrs - 1) * sizeof (struct attribute);
24409
24410 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
24411 memset (die, 0, sizeof (struct die_info));
24412 return (die);
24413 }
24414
24415 \f
24416 /* Macro support. */
24417
24418 /* Return file name relative to the compilation directory of file number I in
24419 *LH's file name table. The result is allocated using xmalloc; the caller is
24420 responsible for freeing it. */
24421
24422 static char *
24423 file_file_name (int file, struct line_header *lh)
24424 {
24425 /* Is the file number a valid index into the line header's file name
24426 table? Remember that file numbers start with one, not zero. */
24427 if (lh->is_valid_file_index (file))
24428 {
24429 const file_entry *fe = lh->file_name_at (file);
24430
24431 if (!IS_ABSOLUTE_PATH (fe->name))
24432 {
24433 const char *dir = fe->include_dir (lh);
24434 if (dir != NULL)
24435 return concat (dir, SLASH_STRING, fe->name, (char *) NULL);
24436 }
24437 return xstrdup (fe->name);
24438 }
24439 else
24440 {
24441 /* The compiler produced a bogus file number. We can at least
24442 record the macro definitions made in the file, even if we
24443 won't be able to find the file by name. */
24444 char fake_name[80];
24445
24446 xsnprintf (fake_name, sizeof (fake_name),
24447 "<bad macro file number %d>", file);
24448
24449 complaint (_("bad file number in macro information (%d)"),
24450 file);
24451
24452 return xstrdup (fake_name);
24453 }
24454 }
24455
24456 /* Return the full name of file number I in *LH's file name table.
24457 Use COMP_DIR as the name of the current directory of the
24458 compilation. The result is allocated using xmalloc; the caller is
24459 responsible for freeing it. */
24460 static char *
24461 file_full_name (int file, struct line_header *lh, const char *comp_dir)
24462 {
24463 /* Is the file number a valid index into the line header's file name
24464 table? Remember that file numbers start with one, not zero. */
24465 if (lh->is_valid_file_index (file))
24466 {
24467 char *relative = file_file_name (file, lh);
24468
24469 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
24470 return relative;
24471 return reconcat (relative, comp_dir, SLASH_STRING,
24472 relative, (char *) NULL);
24473 }
24474 else
24475 return file_file_name (file, lh);
24476 }
24477
24478
24479 static struct macro_source_file *
24480 macro_start_file (struct dwarf2_cu *cu,
24481 int file, int line,
24482 struct macro_source_file *current_file,
24483 struct line_header *lh)
24484 {
24485 /* File name relative to the compilation directory of this source file. */
24486 char *file_name = file_file_name (file, lh);
24487
24488 if (! current_file)
24489 {
24490 /* Note: We don't create a macro table for this compilation unit
24491 at all until we actually get a filename. */
24492 struct macro_table *macro_table = cu->get_builder ()->get_macro_table ();
24493
24494 /* If we have no current file, then this must be the start_file
24495 directive for the compilation unit's main source file. */
24496 current_file = macro_set_main (macro_table, file_name);
24497 macro_define_special (macro_table);
24498 }
24499 else
24500 current_file = macro_include (current_file, line, file_name);
24501
24502 xfree (file_name);
24503
24504 return current_file;
24505 }
24506
24507 static const char *
24508 consume_improper_spaces (const char *p, const char *body)
24509 {
24510 if (*p == ' ')
24511 {
24512 complaint (_("macro definition contains spaces "
24513 "in formal argument list:\n`%s'"),
24514 body);
24515
24516 while (*p == ' ')
24517 p++;
24518 }
24519
24520 return p;
24521 }
24522
24523
24524 static void
24525 parse_macro_definition (struct macro_source_file *file, int line,
24526 const char *body)
24527 {
24528 const char *p;
24529
24530 /* The body string takes one of two forms. For object-like macro
24531 definitions, it should be:
24532
24533 <macro name> " " <definition>
24534
24535 For function-like macro definitions, it should be:
24536
24537 <macro name> "() " <definition>
24538 or
24539 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
24540
24541 Spaces may appear only where explicitly indicated, and in the
24542 <definition>.
24543
24544 The Dwarf 2 spec says that an object-like macro's name is always
24545 followed by a space, but versions of GCC around March 2002 omit
24546 the space when the macro's definition is the empty string.
24547
24548 The Dwarf 2 spec says that there should be no spaces between the
24549 formal arguments in a function-like macro's formal argument list,
24550 but versions of GCC around March 2002 include spaces after the
24551 commas. */
24552
24553
24554 /* Find the extent of the macro name. The macro name is terminated
24555 by either a space or null character (for an object-like macro) or
24556 an opening paren (for a function-like macro). */
24557 for (p = body; *p; p++)
24558 if (*p == ' ' || *p == '(')
24559 break;
24560
24561 if (*p == ' ' || *p == '\0')
24562 {
24563 /* It's an object-like macro. */
24564 int name_len = p - body;
24565 std::string name (body, name_len);
24566 const char *replacement;
24567
24568 if (*p == ' ')
24569 replacement = body + name_len + 1;
24570 else
24571 {
24572 dwarf2_macro_malformed_definition_complaint (body);
24573 replacement = body + name_len;
24574 }
24575
24576 macro_define_object (file, line, name.c_str (), replacement);
24577 }
24578 else if (*p == '(')
24579 {
24580 /* It's a function-like macro. */
24581 std::string name (body, p - body);
24582 int argc = 0;
24583 int argv_size = 1;
24584 char **argv = XNEWVEC (char *, argv_size);
24585
24586 p++;
24587
24588 p = consume_improper_spaces (p, body);
24589
24590 /* Parse the formal argument list. */
24591 while (*p && *p != ')')
24592 {
24593 /* Find the extent of the current argument name. */
24594 const char *arg_start = p;
24595
24596 while (*p && *p != ',' && *p != ')' && *p != ' ')
24597 p++;
24598
24599 if (! *p || p == arg_start)
24600 dwarf2_macro_malformed_definition_complaint (body);
24601 else
24602 {
24603 /* Make sure argv has room for the new argument. */
24604 if (argc >= argv_size)
24605 {
24606 argv_size *= 2;
24607 argv = XRESIZEVEC (char *, argv, argv_size);
24608 }
24609
24610 argv[argc++] = savestring (arg_start, p - arg_start);
24611 }
24612
24613 p = consume_improper_spaces (p, body);
24614
24615 /* Consume the comma, if present. */
24616 if (*p == ',')
24617 {
24618 p++;
24619
24620 p = consume_improper_spaces (p, body);
24621 }
24622 }
24623
24624 if (*p == ')')
24625 {
24626 p++;
24627
24628 if (*p == ' ')
24629 /* Perfectly formed definition, no complaints. */
24630 macro_define_function (file, line, name.c_str (),
24631 argc, (const char **) argv,
24632 p + 1);
24633 else if (*p == '\0')
24634 {
24635 /* Complain, but do define it. */
24636 dwarf2_macro_malformed_definition_complaint (body);
24637 macro_define_function (file, line, name.c_str (),
24638 argc, (const char **) argv,
24639 p);
24640 }
24641 else
24642 /* Just complain. */
24643 dwarf2_macro_malformed_definition_complaint (body);
24644 }
24645 else
24646 /* Just complain. */
24647 dwarf2_macro_malformed_definition_complaint (body);
24648
24649 {
24650 int i;
24651
24652 for (i = 0; i < argc; i++)
24653 xfree (argv[i]);
24654 }
24655 xfree (argv);
24656 }
24657 else
24658 dwarf2_macro_malformed_definition_complaint (body);
24659 }
24660
24661 /* Skip some bytes from BYTES according to the form given in FORM.
24662 Returns the new pointer. */
24663
24664 static const gdb_byte *
24665 skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
24666 enum dwarf_form form,
24667 unsigned int offset_size,
24668 struct dwarf2_section_info *section)
24669 {
24670 unsigned int bytes_read;
24671
24672 switch (form)
24673 {
24674 case DW_FORM_data1:
24675 case DW_FORM_flag:
24676 ++bytes;
24677 break;
24678
24679 case DW_FORM_data2:
24680 bytes += 2;
24681 break;
24682
24683 case DW_FORM_data4:
24684 bytes += 4;
24685 break;
24686
24687 case DW_FORM_data8:
24688 bytes += 8;
24689 break;
24690
24691 case DW_FORM_data16:
24692 bytes += 16;
24693 break;
24694
24695 case DW_FORM_string:
24696 read_direct_string (abfd, bytes, &bytes_read);
24697 bytes += bytes_read;
24698 break;
24699
24700 case DW_FORM_sec_offset:
24701 case DW_FORM_strp:
24702 case DW_FORM_GNU_strp_alt:
24703 bytes += offset_size;
24704 break;
24705
24706 case DW_FORM_block:
24707 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
24708 bytes += bytes_read;
24709 break;
24710
24711 case DW_FORM_block1:
24712 bytes += 1 + read_1_byte (abfd, bytes);
24713 break;
24714 case DW_FORM_block2:
24715 bytes += 2 + read_2_bytes (abfd, bytes);
24716 break;
24717 case DW_FORM_block4:
24718 bytes += 4 + read_4_bytes (abfd, bytes);
24719 break;
24720
24721 case DW_FORM_addrx:
24722 case DW_FORM_sdata:
24723 case DW_FORM_strx:
24724 case DW_FORM_udata:
24725 case DW_FORM_GNU_addr_index:
24726 case DW_FORM_GNU_str_index:
24727 bytes = gdb_skip_leb128 (bytes, buffer_end);
24728 if (bytes == NULL)
24729 {
24730 dwarf2_section_buffer_overflow_complaint (section);
24731 return NULL;
24732 }
24733 break;
24734
24735 case DW_FORM_implicit_const:
24736 break;
24737
24738 default:
24739 {
24740 complaint (_("invalid form 0x%x in `%s'"),
24741 form, get_section_name (section));
24742 return NULL;
24743 }
24744 }
24745
24746 return bytes;
24747 }
24748
24749 /* A helper for dwarf_decode_macros that handles skipping an unknown
24750 opcode. Returns an updated pointer to the macro data buffer; or,
24751 on error, issues a complaint and returns NULL. */
24752
24753 static const gdb_byte *
24754 skip_unknown_opcode (unsigned int opcode,
24755 const gdb_byte **opcode_definitions,
24756 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
24757 bfd *abfd,
24758 unsigned int offset_size,
24759 struct dwarf2_section_info *section)
24760 {
24761 unsigned int bytes_read, i;
24762 unsigned long arg;
24763 const gdb_byte *defn;
24764
24765 if (opcode_definitions[opcode] == NULL)
24766 {
24767 complaint (_("unrecognized DW_MACFINO opcode 0x%x"),
24768 opcode);
24769 return NULL;
24770 }
24771
24772 defn = opcode_definitions[opcode];
24773 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
24774 defn += bytes_read;
24775
24776 for (i = 0; i < arg; ++i)
24777 {
24778 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
24779 (enum dwarf_form) defn[i], offset_size,
24780 section);
24781 if (mac_ptr == NULL)
24782 {
24783 /* skip_form_bytes already issued the complaint. */
24784 return NULL;
24785 }
24786 }
24787
24788 return mac_ptr;
24789 }
24790
24791 /* A helper function which parses the header of a macro section.
24792 If the macro section is the extended (for now called "GNU") type,
24793 then this updates *OFFSET_SIZE. Returns a pointer to just after
24794 the header, or issues a complaint and returns NULL on error. */
24795
24796 static const gdb_byte *
24797 dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
24798 bfd *abfd,
24799 const gdb_byte *mac_ptr,
24800 unsigned int *offset_size,
24801 int section_is_gnu)
24802 {
24803 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
24804
24805 if (section_is_gnu)
24806 {
24807 unsigned int version, flags;
24808
24809 version = read_2_bytes (abfd, mac_ptr);
24810 if (version != 4 && version != 5)
24811 {
24812 complaint (_("unrecognized version `%d' in .debug_macro section"),
24813 version);
24814 return NULL;
24815 }
24816 mac_ptr += 2;
24817
24818 flags = read_1_byte (abfd, mac_ptr);
24819 ++mac_ptr;
24820 *offset_size = (flags & 1) ? 8 : 4;
24821
24822 if ((flags & 2) != 0)
24823 /* We don't need the line table offset. */
24824 mac_ptr += *offset_size;
24825
24826 /* Vendor opcode descriptions. */
24827 if ((flags & 4) != 0)
24828 {
24829 unsigned int i, count;
24830
24831 count = read_1_byte (abfd, mac_ptr);
24832 ++mac_ptr;
24833 for (i = 0; i < count; ++i)
24834 {
24835 unsigned int opcode, bytes_read;
24836 unsigned long arg;
24837
24838 opcode = read_1_byte (abfd, mac_ptr);
24839 ++mac_ptr;
24840 opcode_definitions[opcode] = mac_ptr;
24841 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24842 mac_ptr += bytes_read;
24843 mac_ptr += arg;
24844 }
24845 }
24846 }
24847
24848 return mac_ptr;
24849 }
24850
24851 /* A helper for dwarf_decode_macros that handles the GNU extensions,
24852 including DW_MACRO_import. */
24853
24854 static void
24855 dwarf_decode_macro_bytes (struct dwarf2_cu *cu,
24856 bfd *abfd,
24857 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
24858 struct macro_source_file *current_file,
24859 struct line_header *lh,
24860 struct dwarf2_section_info *section,
24861 int section_is_gnu, int section_is_dwz,
24862 unsigned int offset_size,
24863 htab_t include_hash)
24864 {
24865 struct dwarf2_per_objfile *dwarf2_per_objfile
24866 = cu->per_cu->dwarf2_per_objfile;
24867 struct objfile *objfile = dwarf2_per_objfile->objfile;
24868 enum dwarf_macro_record_type macinfo_type;
24869 int at_commandline;
24870 const gdb_byte *opcode_definitions[256];
24871
24872 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24873 &offset_size, section_is_gnu);
24874 if (mac_ptr == NULL)
24875 {
24876 /* We already issued a complaint. */
24877 return;
24878 }
24879
24880 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
24881 GDB is still reading the definitions from command line. First
24882 DW_MACINFO_start_file will need to be ignored as it was already executed
24883 to create CURRENT_FILE for the main source holding also the command line
24884 definitions. On first met DW_MACINFO_start_file this flag is reset to
24885 normally execute all the remaining DW_MACINFO_start_file macinfos. */
24886
24887 at_commandline = 1;
24888
24889 do
24890 {
24891 /* Do we at least have room for a macinfo type byte? */
24892 if (mac_ptr >= mac_end)
24893 {
24894 dwarf2_section_buffer_overflow_complaint (section);
24895 break;
24896 }
24897
24898 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
24899 mac_ptr++;
24900
24901 /* Note that we rely on the fact that the corresponding GNU and
24902 DWARF constants are the same. */
24903 DIAGNOSTIC_PUSH
24904 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
24905 switch (macinfo_type)
24906 {
24907 /* A zero macinfo type indicates the end of the macro
24908 information. */
24909 case 0:
24910 break;
24911
24912 case DW_MACRO_define:
24913 case DW_MACRO_undef:
24914 case DW_MACRO_define_strp:
24915 case DW_MACRO_undef_strp:
24916 case DW_MACRO_define_sup:
24917 case DW_MACRO_undef_sup:
24918 {
24919 unsigned int bytes_read;
24920 int line;
24921 const char *body;
24922 int is_define;
24923
24924 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24925 mac_ptr += bytes_read;
24926
24927 if (macinfo_type == DW_MACRO_define
24928 || macinfo_type == DW_MACRO_undef)
24929 {
24930 body = read_direct_string (abfd, mac_ptr, &bytes_read);
24931 mac_ptr += bytes_read;
24932 }
24933 else
24934 {
24935 LONGEST str_offset;
24936
24937 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
24938 mac_ptr += offset_size;
24939
24940 if (macinfo_type == DW_MACRO_define_sup
24941 || macinfo_type == DW_MACRO_undef_sup
24942 || section_is_dwz)
24943 {
24944 struct dwz_file *dwz
24945 = dwarf2_get_dwz_file (dwarf2_per_objfile);
24946
24947 body = read_indirect_string_from_dwz (objfile,
24948 dwz, str_offset);
24949 }
24950 else
24951 body = read_indirect_string_at_offset (dwarf2_per_objfile,
24952 abfd, str_offset);
24953 }
24954
24955 is_define = (macinfo_type == DW_MACRO_define
24956 || macinfo_type == DW_MACRO_define_strp
24957 || macinfo_type == DW_MACRO_define_sup);
24958 if (! current_file)
24959 {
24960 /* DWARF violation as no main source is present. */
24961 complaint (_("debug info with no main source gives macro %s "
24962 "on line %d: %s"),
24963 is_define ? _("definition") : _("undefinition"),
24964 line, body);
24965 break;
24966 }
24967 if ((line == 0 && !at_commandline)
24968 || (line != 0 && at_commandline))
24969 complaint (_("debug info gives %s macro %s with %s line %d: %s"),
24970 at_commandline ? _("command-line") : _("in-file"),
24971 is_define ? _("definition") : _("undefinition"),
24972 line == 0 ? _("zero") : _("non-zero"), line, body);
24973
24974 if (body == NULL)
24975 {
24976 /* Fedora's rpm-build's "debugedit" binary
24977 corrupted .debug_macro sections.
24978
24979 For more info, see
24980 https://bugzilla.redhat.com/show_bug.cgi?id=1708786 */
24981 complaint (_("debug info gives %s invalid macro %s "
24982 "without body (corrupted?) at line %d "
24983 "on file %s"),
24984 at_commandline ? _("command-line") : _("in-file"),
24985 is_define ? _("definition") : _("undefinition"),
24986 line, current_file->filename);
24987 }
24988 else if (is_define)
24989 parse_macro_definition (current_file, line, body);
24990 else
24991 {
24992 gdb_assert (macinfo_type == DW_MACRO_undef
24993 || macinfo_type == DW_MACRO_undef_strp
24994 || macinfo_type == DW_MACRO_undef_sup);
24995 macro_undef (current_file, line, body);
24996 }
24997 }
24998 break;
24999
25000 case DW_MACRO_start_file:
25001 {
25002 unsigned int bytes_read;
25003 int line, file;
25004
25005 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25006 mac_ptr += bytes_read;
25007 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25008 mac_ptr += bytes_read;
25009
25010 if ((line == 0 && !at_commandline)
25011 || (line != 0 && at_commandline))
25012 complaint (_("debug info gives source %d included "
25013 "from %s at %s line %d"),
25014 file, at_commandline ? _("command-line") : _("file"),
25015 line == 0 ? _("zero") : _("non-zero"), line);
25016
25017 if (at_commandline)
25018 {
25019 /* This DW_MACRO_start_file was executed in the
25020 pass one. */
25021 at_commandline = 0;
25022 }
25023 else
25024 current_file = macro_start_file (cu, file, line, current_file,
25025 lh);
25026 }
25027 break;
25028
25029 case DW_MACRO_end_file:
25030 if (! current_file)
25031 complaint (_("macro debug info has an unmatched "
25032 "`close_file' directive"));
25033 else
25034 {
25035 current_file = current_file->included_by;
25036 if (! current_file)
25037 {
25038 enum dwarf_macro_record_type next_type;
25039
25040 /* GCC circa March 2002 doesn't produce the zero
25041 type byte marking the end of the compilation
25042 unit. Complain if it's not there, but exit no
25043 matter what. */
25044
25045 /* Do we at least have room for a macinfo type byte? */
25046 if (mac_ptr >= mac_end)
25047 {
25048 dwarf2_section_buffer_overflow_complaint (section);
25049 return;
25050 }
25051
25052 /* We don't increment mac_ptr here, so this is just
25053 a look-ahead. */
25054 next_type
25055 = (enum dwarf_macro_record_type) read_1_byte (abfd,
25056 mac_ptr);
25057 if (next_type != 0)
25058 complaint (_("no terminating 0-type entry for "
25059 "macros in `.debug_macinfo' section"));
25060
25061 return;
25062 }
25063 }
25064 break;
25065
25066 case DW_MACRO_import:
25067 case DW_MACRO_import_sup:
25068 {
25069 LONGEST offset;
25070 void **slot;
25071 bfd *include_bfd = abfd;
25072 struct dwarf2_section_info *include_section = section;
25073 const gdb_byte *include_mac_end = mac_end;
25074 int is_dwz = section_is_dwz;
25075 const gdb_byte *new_mac_ptr;
25076
25077 offset = read_offset_1 (abfd, mac_ptr, offset_size);
25078 mac_ptr += offset_size;
25079
25080 if (macinfo_type == DW_MACRO_import_sup)
25081 {
25082 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
25083
25084 dwarf2_read_section (objfile, &dwz->macro);
25085
25086 include_section = &dwz->macro;
25087 include_bfd = get_section_bfd_owner (include_section);
25088 include_mac_end = dwz->macro.buffer + dwz->macro.size;
25089 is_dwz = 1;
25090 }
25091
25092 new_mac_ptr = include_section->buffer + offset;
25093 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
25094
25095 if (*slot != NULL)
25096 {
25097 /* This has actually happened; see
25098 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
25099 complaint (_("recursive DW_MACRO_import in "
25100 ".debug_macro section"));
25101 }
25102 else
25103 {
25104 *slot = (void *) new_mac_ptr;
25105
25106 dwarf_decode_macro_bytes (cu, include_bfd, new_mac_ptr,
25107 include_mac_end, current_file, lh,
25108 section, section_is_gnu, is_dwz,
25109 offset_size, include_hash);
25110
25111 htab_remove_elt (include_hash, (void *) new_mac_ptr);
25112 }
25113 }
25114 break;
25115
25116 case DW_MACINFO_vendor_ext:
25117 if (!section_is_gnu)
25118 {
25119 unsigned int bytes_read;
25120
25121 /* This reads the constant, but since we don't recognize
25122 any vendor extensions, we ignore it. */
25123 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25124 mac_ptr += bytes_read;
25125 read_direct_string (abfd, mac_ptr, &bytes_read);
25126 mac_ptr += bytes_read;
25127
25128 /* We don't recognize any vendor extensions. */
25129 break;
25130 }
25131 /* FALLTHROUGH */
25132
25133 default:
25134 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
25135 mac_ptr, mac_end, abfd, offset_size,
25136 section);
25137 if (mac_ptr == NULL)
25138 return;
25139 break;
25140 }
25141 DIAGNOSTIC_POP
25142 } while (macinfo_type != 0);
25143 }
25144
25145 static void
25146 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
25147 int section_is_gnu)
25148 {
25149 struct dwarf2_per_objfile *dwarf2_per_objfile
25150 = cu->per_cu->dwarf2_per_objfile;
25151 struct objfile *objfile = dwarf2_per_objfile->objfile;
25152 struct line_header *lh = cu->line_header;
25153 bfd *abfd;
25154 const gdb_byte *mac_ptr, *mac_end;
25155 struct macro_source_file *current_file = 0;
25156 enum dwarf_macro_record_type macinfo_type;
25157 unsigned int offset_size = cu->header.offset_size;
25158 const gdb_byte *opcode_definitions[256];
25159 void **slot;
25160 struct dwarf2_section_info *section;
25161 const char *section_name;
25162
25163 if (cu->dwo_unit != NULL)
25164 {
25165 if (section_is_gnu)
25166 {
25167 section = &cu->dwo_unit->dwo_file->sections.macro;
25168 section_name = ".debug_macro.dwo";
25169 }
25170 else
25171 {
25172 section = &cu->dwo_unit->dwo_file->sections.macinfo;
25173 section_name = ".debug_macinfo.dwo";
25174 }
25175 }
25176 else
25177 {
25178 if (section_is_gnu)
25179 {
25180 section = &dwarf2_per_objfile->macro;
25181 section_name = ".debug_macro";
25182 }
25183 else
25184 {
25185 section = &dwarf2_per_objfile->macinfo;
25186 section_name = ".debug_macinfo";
25187 }
25188 }
25189
25190 dwarf2_read_section (objfile, section);
25191 if (section->buffer == NULL)
25192 {
25193 complaint (_("missing %s section"), section_name);
25194 return;
25195 }
25196 abfd = get_section_bfd_owner (section);
25197
25198 /* First pass: Find the name of the base filename.
25199 This filename is needed in order to process all macros whose definition
25200 (or undefinition) comes from the command line. These macros are defined
25201 before the first DW_MACINFO_start_file entry, and yet still need to be
25202 associated to the base file.
25203
25204 To determine the base file name, we scan the macro definitions until we
25205 reach the first DW_MACINFO_start_file entry. We then initialize
25206 CURRENT_FILE accordingly so that any macro definition found before the
25207 first DW_MACINFO_start_file can still be associated to the base file. */
25208
25209 mac_ptr = section->buffer + offset;
25210 mac_end = section->buffer + section->size;
25211
25212 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
25213 &offset_size, section_is_gnu);
25214 if (mac_ptr == NULL)
25215 {
25216 /* We already issued a complaint. */
25217 return;
25218 }
25219
25220 do
25221 {
25222 /* Do we at least have room for a macinfo type byte? */
25223 if (mac_ptr >= mac_end)
25224 {
25225 /* Complaint is printed during the second pass as GDB will probably
25226 stop the first pass earlier upon finding
25227 DW_MACINFO_start_file. */
25228 break;
25229 }
25230
25231 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
25232 mac_ptr++;
25233
25234 /* Note that we rely on the fact that the corresponding GNU and
25235 DWARF constants are the same. */
25236 DIAGNOSTIC_PUSH
25237 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
25238 switch (macinfo_type)
25239 {
25240 /* A zero macinfo type indicates the end of the macro
25241 information. */
25242 case 0:
25243 break;
25244
25245 case DW_MACRO_define:
25246 case DW_MACRO_undef:
25247 /* Only skip the data by MAC_PTR. */
25248 {
25249 unsigned int bytes_read;
25250
25251 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25252 mac_ptr += bytes_read;
25253 read_direct_string (abfd, mac_ptr, &bytes_read);
25254 mac_ptr += bytes_read;
25255 }
25256 break;
25257
25258 case DW_MACRO_start_file:
25259 {
25260 unsigned int bytes_read;
25261 int line, file;
25262
25263 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25264 mac_ptr += bytes_read;
25265 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25266 mac_ptr += bytes_read;
25267
25268 current_file = macro_start_file (cu, file, line, current_file, lh);
25269 }
25270 break;
25271
25272 case DW_MACRO_end_file:
25273 /* No data to skip by MAC_PTR. */
25274 break;
25275
25276 case DW_MACRO_define_strp:
25277 case DW_MACRO_undef_strp:
25278 case DW_MACRO_define_sup:
25279 case DW_MACRO_undef_sup:
25280 {
25281 unsigned int bytes_read;
25282
25283 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25284 mac_ptr += bytes_read;
25285 mac_ptr += offset_size;
25286 }
25287 break;
25288
25289 case DW_MACRO_import:
25290 case DW_MACRO_import_sup:
25291 /* Note that, according to the spec, a transparent include
25292 chain cannot call DW_MACRO_start_file. So, we can just
25293 skip this opcode. */
25294 mac_ptr += offset_size;
25295 break;
25296
25297 case DW_MACINFO_vendor_ext:
25298 /* Only skip the data by MAC_PTR. */
25299 if (!section_is_gnu)
25300 {
25301 unsigned int bytes_read;
25302
25303 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25304 mac_ptr += bytes_read;
25305 read_direct_string (abfd, mac_ptr, &bytes_read);
25306 mac_ptr += bytes_read;
25307 }
25308 /* FALLTHROUGH */
25309
25310 default:
25311 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
25312 mac_ptr, mac_end, abfd, offset_size,
25313 section);
25314 if (mac_ptr == NULL)
25315 return;
25316 break;
25317 }
25318 DIAGNOSTIC_POP
25319 } while (macinfo_type != 0 && current_file == NULL);
25320
25321 /* Second pass: Process all entries.
25322
25323 Use the AT_COMMAND_LINE flag to determine whether we are still processing
25324 command-line macro definitions/undefinitions. This flag is unset when we
25325 reach the first DW_MACINFO_start_file entry. */
25326
25327 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
25328 htab_eq_pointer,
25329 NULL, xcalloc, xfree));
25330 mac_ptr = section->buffer + offset;
25331 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
25332 *slot = (void *) mac_ptr;
25333 dwarf_decode_macro_bytes (cu, abfd, mac_ptr, mac_end,
25334 current_file, lh, section,
25335 section_is_gnu, 0, offset_size,
25336 include_hash.get ());
25337 }
25338
25339 /* Check if the attribute's form is a DW_FORM_block*
25340 if so return true else false. */
25341
25342 static int
25343 attr_form_is_block (const struct attribute *attr)
25344 {
25345 return (attr == NULL ? 0 :
25346 attr->form == DW_FORM_block1
25347 || attr->form == DW_FORM_block2
25348 || attr->form == DW_FORM_block4
25349 || attr->form == DW_FORM_block
25350 || attr->form == DW_FORM_exprloc);
25351 }
25352
25353 /* Return non-zero if ATTR's value is a section offset --- classes
25354 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
25355 You may use DW_UNSND (attr) to retrieve such offsets.
25356
25357 Section 7.5.4, "Attribute Encodings", explains that no attribute
25358 may have a value that belongs to more than one of these classes; it
25359 would be ambiguous if we did, because we use the same forms for all
25360 of them. */
25361
25362 static int
25363 attr_form_is_section_offset (const struct attribute *attr)
25364 {
25365 return (attr->form == DW_FORM_data4
25366 || attr->form == DW_FORM_data8
25367 || attr->form == DW_FORM_sec_offset);
25368 }
25369
25370 /* Return non-zero if ATTR's value falls in the 'constant' class, or
25371 zero otherwise. When this function returns true, you can apply
25372 dwarf2_get_attr_constant_value to it.
25373
25374 However, note that for some attributes you must check
25375 attr_form_is_section_offset before using this test. DW_FORM_data4
25376 and DW_FORM_data8 are members of both the constant class, and of
25377 the classes that contain offsets into other debug sections
25378 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
25379 that, if an attribute's can be either a constant or one of the
25380 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
25381 taken as section offsets, not constants.
25382
25383 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
25384 cannot handle that. */
25385
25386 static int
25387 attr_form_is_constant (const struct attribute *attr)
25388 {
25389 switch (attr->form)
25390 {
25391 case DW_FORM_sdata:
25392 case DW_FORM_udata:
25393 case DW_FORM_data1:
25394 case DW_FORM_data2:
25395 case DW_FORM_data4:
25396 case DW_FORM_data8:
25397 case DW_FORM_implicit_const:
25398 return 1;
25399 default:
25400 return 0;
25401 }
25402 }
25403
25404
25405 /* DW_ADDR is always stored already as sect_offset; despite for the forms
25406 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
25407
25408 static int
25409 attr_form_is_ref (const struct attribute *attr)
25410 {
25411 switch (attr->form)
25412 {
25413 case DW_FORM_ref_addr:
25414 case DW_FORM_ref1:
25415 case DW_FORM_ref2:
25416 case DW_FORM_ref4:
25417 case DW_FORM_ref8:
25418 case DW_FORM_ref_udata:
25419 case DW_FORM_GNU_ref_alt:
25420 return 1;
25421 default:
25422 return 0;
25423 }
25424 }
25425
25426 /* Return the .debug_loc section to use for CU.
25427 For DWO files use .debug_loc.dwo. */
25428
25429 static struct dwarf2_section_info *
25430 cu_debug_loc_section (struct dwarf2_cu *cu)
25431 {
25432 struct dwarf2_per_objfile *dwarf2_per_objfile
25433 = cu->per_cu->dwarf2_per_objfile;
25434
25435 if (cu->dwo_unit)
25436 {
25437 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
25438
25439 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
25440 }
25441 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
25442 : &dwarf2_per_objfile->loc);
25443 }
25444
25445 /* A helper function that fills in a dwarf2_loclist_baton. */
25446
25447 static void
25448 fill_in_loclist_baton (struct dwarf2_cu *cu,
25449 struct dwarf2_loclist_baton *baton,
25450 const struct attribute *attr)
25451 {
25452 struct dwarf2_per_objfile *dwarf2_per_objfile
25453 = cu->per_cu->dwarf2_per_objfile;
25454 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
25455
25456 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
25457
25458 baton->per_cu = cu->per_cu;
25459 gdb_assert (baton->per_cu);
25460 /* We don't know how long the location list is, but make sure we
25461 don't run off the edge of the section. */
25462 baton->size = section->size - DW_UNSND (attr);
25463 baton->data = section->buffer + DW_UNSND (attr);
25464 baton->base_address = cu->base_address;
25465 baton->from_dwo = cu->dwo_unit != NULL;
25466 }
25467
25468 static void
25469 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
25470 struct dwarf2_cu *cu, int is_block)
25471 {
25472 struct dwarf2_per_objfile *dwarf2_per_objfile
25473 = cu->per_cu->dwarf2_per_objfile;
25474 struct objfile *objfile = dwarf2_per_objfile->objfile;
25475 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
25476
25477 if (attr_form_is_section_offset (attr)
25478 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
25479 the section. If so, fall through to the complaint in the
25480 other branch. */
25481 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
25482 {
25483 struct dwarf2_loclist_baton *baton;
25484
25485 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
25486
25487 fill_in_loclist_baton (cu, baton, attr);
25488
25489 if (cu->base_known == 0)
25490 complaint (_("Location list used without "
25491 "specifying the CU base address."));
25492
25493 SYMBOL_ACLASS_INDEX (sym) = (is_block
25494 ? dwarf2_loclist_block_index
25495 : dwarf2_loclist_index);
25496 SYMBOL_LOCATION_BATON (sym) = baton;
25497 }
25498 else
25499 {
25500 struct dwarf2_locexpr_baton *baton;
25501
25502 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
25503 baton->per_cu = cu->per_cu;
25504 gdb_assert (baton->per_cu);
25505
25506 if (attr_form_is_block (attr))
25507 {
25508 /* Note that we're just copying the block's data pointer
25509 here, not the actual data. We're still pointing into the
25510 info_buffer for SYM's objfile; right now we never release
25511 that buffer, but when we do clean up properly this may
25512 need to change. */
25513 baton->size = DW_BLOCK (attr)->size;
25514 baton->data = DW_BLOCK (attr)->data;
25515 }
25516 else
25517 {
25518 dwarf2_invalid_attrib_class_complaint ("location description",
25519 sym->natural_name ());
25520 baton->size = 0;
25521 }
25522
25523 SYMBOL_ACLASS_INDEX (sym) = (is_block
25524 ? dwarf2_locexpr_block_index
25525 : dwarf2_locexpr_index);
25526 SYMBOL_LOCATION_BATON (sym) = baton;
25527 }
25528 }
25529
25530 /* Return the OBJFILE associated with the compilation unit CU. If CU
25531 came from a separate debuginfo file, then the master objfile is
25532 returned. */
25533
25534 struct objfile *
25535 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
25536 {
25537 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
25538
25539 /* Return the master objfile, so that we can report and look up the
25540 correct file containing this variable. */
25541 if (objfile->separate_debug_objfile_backlink)
25542 objfile = objfile->separate_debug_objfile_backlink;
25543
25544 return objfile;
25545 }
25546
25547 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
25548 (CU_HEADERP is unused in such case) or prepare a temporary copy at
25549 CU_HEADERP first. */
25550
25551 static const struct comp_unit_head *
25552 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
25553 struct dwarf2_per_cu_data *per_cu)
25554 {
25555 const gdb_byte *info_ptr;
25556
25557 if (per_cu->cu)
25558 return &per_cu->cu->header;
25559
25560 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
25561
25562 memset (cu_headerp, 0, sizeof (*cu_headerp));
25563 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
25564 rcuh_kind::COMPILE);
25565
25566 return cu_headerp;
25567 }
25568
25569 /* Return the address size given in the compilation unit header for CU. */
25570
25571 int
25572 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
25573 {
25574 struct comp_unit_head cu_header_local;
25575 const struct comp_unit_head *cu_headerp;
25576
25577 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25578
25579 return cu_headerp->addr_size;
25580 }
25581
25582 /* Return the offset size given in the compilation unit header for CU. */
25583
25584 int
25585 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
25586 {
25587 struct comp_unit_head cu_header_local;
25588 const struct comp_unit_head *cu_headerp;
25589
25590 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25591
25592 return cu_headerp->offset_size;
25593 }
25594
25595 /* See its dwarf2loc.h declaration. */
25596
25597 int
25598 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
25599 {
25600 struct comp_unit_head cu_header_local;
25601 const struct comp_unit_head *cu_headerp;
25602
25603 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25604
25605 if (cu_headerp->version == 2)
25606 return cu_headerp->addr_size;
25607 else
25608 return cu_headerp->offset_size;
25609 }
25610
25611 /* Return the text offset of the CU. The returned offset comes from
25612 this CU's objfile. If this objfile came from a separate debuginfo
25613 file, then the offset may be different from the corresponding
25614 offset in the parent objfile. */
25615
25616 CORE_ADDR
25617 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
25618 {
25619 return per_cu->dwarf2_per_objfile->objfile->text_section_offset ();
25620 }
25621
25622 /* Return a type that is a generic pointer type, the size of which matches
25623 the address size given in the compilation unit header for PER_CU. */
25624 static struct type *
25625 dwarf2_per_cu_addr_type (struct dwarf2_per_cu_data *per_cu)
25626 {
25627 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
25628 struct type *void_type = objfile_type (objfile)->builtin_void;
25629 struct type *addr_type = lookup_pointer_type (void_type);
25630 int addr_size = dwarf2_per_cu_addr_size (per_cu);
25631
25632 if (TYPE_LENGTH (addr_type) == addr_size)
25633 return addr_type;
25634
25635 addr_type
25636 = dwarf2_per_cu_addr_sized_int_type (per_cu, TYPE_UNSIGNED (addr_type));
25637 return addr_type;
25638 }
25639
25640 /* Return DWARF version number of PER_CU. */
25641
25642 short
25643 dwarf2_version (struct dwarf2_per_cu_data *per_cu)
25644 {
25645 return per_cu->dwarf_version;
25646 }
25647
25648 /* Locate the .debug_info compilation unit from CU's objfile which contains
25649 the DIE at OFFSET. Raises an error on failure. */
25650
25651 static struct dwarf2_per_cu_data *
25652 dwarf2_find_containing_comp_unit (sect_offset sect_off,
25653 unsigned int offset_in_dwz,
25654 struct dwarf2_per_objfile *dwarf2_per_objfile)
25655 {
25656 struct dwarf2_per_cu_data *this_cu;
25657 int low, high;
25658
25659 low = 0;
25660 high = dwarf2_per_objfile->all_comp_units.size () - 1;
25661 while (high > low)
25662 {
25663 struct dwarf2_per_cu_data *mid_cu;
25664 int mid = low + (high - low) / 2;
25665
25666 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
25667 if (mid_cu->is_dwz > offset_in_dwz
25668 || (mid_cu->is_dwz == offset_in_dwz
25669 && mid_cu->sect_off + mid_cu->length >= sect_off))
25670 high = mid;
25671 else
25672 low = mid + 1;
25673 }
25674 gdb_assert (low == high);
25675 this_cu = dwarf2_per_objfile->all_comp_units[low];
25676 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
25677 {
25678 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
25679 error (_("Dwarf Error: could not find partial DIE containing "
25680 "offset %s [in module %s]"),
25681 sect_offset_str (sect_off),
25682 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
25683
25684 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
25685 <= sect_off);
25686 return dwarf2_per_objfile->all_comp_units[low-1];
25687 }
25688 else
25689 {
25690 if (low == dwarf2_per_objfile->all_comp_units.size () - 1
25691 && sect_off >= this_cu->sect_off + this_cu->length)
25692 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
25693 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
25694 return this_cu;
25695 }
25696 }
25697
25698 /* Initialize dwarf2_cu CU, owned by PER_CU. */
25699
25700 dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
25701 : per_cu (per_cu_),
25702 mark (false),
25703 has_loclist (false),
25704 checked_producer (false),
25705 producer_is_gxx_lt_4_6 (false),
25706 producer_is_gcc_lt_4_3 (false),
25707 producer_is_icc (false),
25708 producer_is_icc_lt_14 (false),
25709 producer_is_codewarrior (false),
25710 processing_has_namespace_info (false)
25711 {
25712 per_cu->cu = this;
25713 }
25714
25715 /* Destroy a dwarf2_cu. */
25716
25717 dwarf2_cu::~dwarf2_cu ()
25718 {
25719 per_cu->cu = NULL;
25720 }
25721
25722 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
25723
25724 static void
25725 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
25726 enum language pretend_language)
25727 {
25728 struct attribute *attr;
25729
25730 /* Set the language we're debugging. */
25731 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
25732 if (attr != nullptr)
25733 set_cu_language (DW_UNSND (attr), cu);
25734 else
25735 {
25736 cu->language = pretend_language;
25737 cu->language_defn = language_def (cu->language);
25738 }
25739
25740 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
25741 }
25742
25743 /* Increase the age counter on each cached compilation unit, and free
25744 any that are too old. */
25745
25746 static void
25747 age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
25748 {
25749 struct dwarf2_per_cu_data *per_cu, **last_chain;
25750
25751 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
25752 per_cu = dwarf2_per_objfile->read_in_chain;
25753 while (per_cu != NULL)
25754 {
25755 per_cu->cu->last_used ++;
25756 if (per_cu->cu->last_used <= dwarf_max_cache_age)
25757 dwarf2_mark (per_cu->cu);
25758 per_cu = per_cu->cu->read_in_chain;
25759 }
25760
25761 per_cu = dwarf2_per_objfile->read_in_chain;
25762 last_chain = &dwarf2_per_objfile->read_in_chain;
25763 while (per_cu != NULL)
25764 {
25765 struct dwarf2_per_cu_data *next_cu;
25766
25767 next_cu = per_cu->cu->read_in_chain;
25768
25769 if (!per_cu->cu->mark)
25770 {
25771 delete per_cu->cu;
25772 *last_chain = next_cu;
25773 }
25774 else
25775 last_chain = &per_cu->cu->read_in_chain;
25776
25777 per_cu = next_cu;
25778 }
25779 }
25780
25781 /* Remove a single compilation unit from the cache. */
25782
25783 static void
25784 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
25785 {
25786 struct dwarf2_per_cu_data *per_cu, **last_chain;
25787 struct dwarf2_per_objfile *dwarf2_per_objfile
25788 = target_per_cu->dwarf2_per_objfile;
25789
25790 per_cu = dwarf2_per_objfile->read_in_chain;
25791 last_chain = &dwarf2_per_objfile->read_in_chain;
25792 while (per_cu != NULL)
25793 {
25794 struct dwarf2_per_cu_data *next_cu;
25795
25796 next_cu = per_cu->cu->read_in_chain;
25797
25798 if (per_cu == target_per_cu)
25799 {
25800 delete per_cu->cu;
25801 per_cu->cu = NULL;
25802 *last_chain = next_cu;
25803 break;
25804 }
25805 else
25806 last_chain = &per_cu->cu->read_in_chain;
25807
25808 per_cu = next_cu;
25809 }
25810 }
25811
25812 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25813 We store these in a hash table separate from the DIEs, and preserve them
25814 when the DIEs are flushed out of cache.
25815
25816 The CU "per_cu" pointer is needed because offset alone is not enough to
25817 uniquely identify the type. A file may have multiple .debug_types sections,
25818 or the type may come from a DWO file. Furthermore, while it's more logical
25819 to use per_cu->section+offset, with Fission the section with the data is in
25820 the DWO file but we don't know that section at the point we need it.
25821 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25822 because we can enter the lookup routine, get_die_type_at_offset, from
25823 outside this file, and thus won't necessarily have PER_CU->cu.
25824 Fortunately, PER_CU is stable for the life of the objfile. */
25825
25826 struct dwarf2_per_cu_offset_and_type
25827 {
25828 const struct dwarf2_per_cu_data *per_cu;
25829 sect_offset sect_off;
25830 struct type *type;
25831 };
25832
25833 /* Hash function for a dwarf2_per_cu_offset_and_type. */
25834
25835 static hashval_t
25836 per_cu_offset_and_type_hash (const void *item)
25837 {
25838 const struct dwarf2_per_cu_offset_and_type *ofs
25839 = (const struct dwarf2_per_cu_offset_and_type *) item;
25840
25841 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
25842 }
25843
25844 /* Equality function for a dwarf2_per_cu_offset_and_type. */
25845
25846 static int
25847 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
25848 {
25849 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25850 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25851 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25852 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
25853
25854 return (ofs_lhs->per_cu == ofs_rhs->per_cu
25855 && ofs_lhs->sect_off == ofs_rhs->sect_off);
25856 }
25857
25858 /* Set the type associated with DIE to TYPE. Save it in CU's hash
25859 table if necessary. For convenience, return TYPE.
25860
25861 The DIEs reading must have careful ordering to:
25862 * Not cause infinite loops trying to read in DIEs as a prerequisite for
25863 reading current DIE.
25864 * Not trying to dereference contents of still incompletely read in types
25865 while reading in other DIEs.
25866 * Enable referencing still incompletely read in types just by a pointer to
25867 the type without accessing its fields.
25868
25869 Therefore caller should follow these rules:
25870 * Try to fetch any prerequisite types we may need to build this DIE type
25871 before building the type and calling set_die_type.
25872 * After building type call set_die_type for current DIE as soon as
25873 possible before fetching more types to complete the current type.
25874 * Make the type as complete as possible before fetching more types. */
25875
25876 static struct type *
25877 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
25878 {
25879 struct dwarf2_per_objfile *dwarf2_per_objfile
25880 = cu->per_cu->dwarf2_per_objfile;
25881 struct dwarf2_per_cu_offset_and_type **slot, ofs;
25882 struct objfile *objfile = dwarf2_per_objfile->objfile;
25883 struct attribute *attr;
25884 struct dynamic_prop prop;
25885
25886 /* For Ada types, make sure that the gnat-specific data is always
25887 initialized (if not already set). There are a few types where
25888 we should not be doing so, because the type-specific area is
25889 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25890 where the type-specific area is used to store the floatformat).
25891 But this is not a problem, because the gnat-specific information
25892 is actually not needed for these types. */
25893 if (need_gnat_info (cu)
25894 && TYPE_CODE (type) != TYPE_CODE_FUNC
25895 && TYPE_CODE (type) != TYPE_CODE_FLT
25896 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
25897 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
25898 && TYPE_CODE (type) != TYPE_CODE_METHOD
25899 && !HAVE_GNAT_AUX_INFO (type))
25900 INIT_GNAT_SPECIFIC (type);
25901
25902 /* Read DW_AT_allocated and set in type. */
25903 attr = dwarf2_attr (die, DW_AT_allocated, cu);
25904 if (attr_form_is_block (attr))
25905 {
25906 struct type *prop_type
25907 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
25908 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
25909 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
25910 }
25911 else if (attr != NULL)
25912 {
25913 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
25914 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25915 sect_offset_str (die->sect_off));
25916 }
25917
25918 /* Read DW_AT_associated and set in type. */
25919 attr = dwarf2_attr (die, DW_AT_associated, cu);
25920 if (attr_form_is_block (attr))
25921 {
25922 struct type *prop_type
25923 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
25924 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
25925 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
25926 }
25927 else if (attr != NULL)
25928 {
25929 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
25930 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25931 sect_offset_str (die->sect_off));
25932 }
25933
25934 /* Read DW_AT_data_location and set in type. */
25935 attr = dwarf2_attr (die, DW_AT_data_location, cu);
25936 if (attr_to_dynamic_prop (attr, die, cu, &prop,
25937 dwarf2_per_cu_addr_type (cu->per_cu)))
25938 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
25939
25940 if (dwarf2_per_objfile->die_type_hash == NULL)
25941 {
25942 dwarf2_per_objfile->die_type_hash =
25943 htab_create_alloc_ex (127,
25944 per_cu_offset_and_type_hash,
25945 per_cu_offset_and_type_eq,
25946 NULL,
25947 &objfile->objfile_obstack,
25948 hashtab_obstack_allocate,
25949 dummy_obstack_deallocate);
25950 }
25951
25952 ofs.per_cu = cu->per_cu;
25953 ofs.sect_off = die->sect_off;
25954 ofs.type = type;
25955 slot = (struct dwarf2_per_cu_offset_and_type **)
25956 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
25957 if (*slot)
25958 complaint (_("A problem internal to GDB: DIE %s has type already set"),
25959 sect_offset_str (die->sect_off));
25960 *slot = XOBNEW (&objfile->objfile_obstack,
25961 struct dwarf2_per_cu_offset_and_type);
25962 **slot = ofs;
25963 return type;
25964 }
25965
25966 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
25967 or return NULL if the die does not have a saved type. */
25968
25969 static struct type *
25970 get_die_type_at_offset (sect_offset sect_off,
25971 struct dwarf2_per_cu_data *per_cu)
25972 {
25973 struct dwarf2_per_cu_offset_and_type *slot, ofs;
25974 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
25975
25976 if (dwarf2_per_objfile->die_type_hash == NULL)
25977 return NULL;
25978
25979 ofs.per_cu = per_cu;
25980 ofs.sect_off = sect_off;
25981 slot = ((struct dwarf2_per_cu_offset_and_type *)
25982 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
25983 if (slot)
25984 return slot->type;
25985 else
25986 return NULL;
25987 }
25988
25989 /* Look up the type for DIE in CU in die_type_hash,
25990 or return NULL if DIE does not have a saved type. */
25991
25992 static struct type *
25993 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
25994 {
25995 return get_die_type_at_offset (die->sect_off, cu->per_cu);
25996 }
25997
25998 /* Add a dependence relationship from CU to REF_PER_CU. */
25999
26000 static void
26001 dwarf2_add_dependence (struct dwarf2_cu *cu,
26002 struct dwarf2_per_cu_data *ref_per_cu)
26003 {
26004 void **slot;
26005
26006 if (cu->dependencies == NULL)
26007 cu->dependencies
26008 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
26009 NULL, &cu->comp_unit_obstack,
26010 hashtab_obstack_allocate,
26011 dummy_obstack_deallocate);
26012
26013 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
26014 if (*slot == NULL)
26015 *slot = ref_per_cu;
26016 }
26017
26018 /* Subroutine of dwarf2_mark to pass to htab_traverse.
26019 Set the mark field in every compilation unit in the
26020 cache that we must keep because we are keeping CU. */
26021
26022 static int
26023 dwarf2_mark_helper (void **slot, void *data)
26024 {
26025 struct dwarf2_per_cu_data *per_cu;
26026
26027 per_cu = (struct dwarf2_per_cu_data *) *slot;
26028
26029 /* cu->dependencies references may not yet have been ever read if QUIT aborts
26030 reading of the chain. As such dependencies remain valid it is not much
26031 useful to track and undo them during QUIT cleanups. */
26032 if (per_cu->cu == NULL)
26033 return 1;
26034
26035 if (per_cu->cu->mark)
26036 return 1;
26037 per_cu->cu->mark = true;
26038
26039 if (per_cu->cu->dependencies != NULL)
26040 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
26041
26042 return 1;
26043 }
26044
26045 /* Set the mark field in CU and in every other compilation unit in the
26046 cache that we must keep because we are keeping CU. */
26047
26048 static void
26049 dwarf2_mark (struct dwarf2_cu *cu)
26050 {
26051 if (cu->mark)
26052 return;
26053 cu->mark = true;
26054 if (cu->dependencies != NULL)
26055 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
26056 }
26057
26058 static void
26059 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
26060 {
26061 while (per_cu)
26062 {
26063 per_cu->cu->mark = false;
26064 per_cu = per_cu->cu->read_in_chain;
26065 }
26066 }
26067
26068 /* Trivial hash function for partial_die_info: the hash value of a DIE
26069 is its offset in .debug_info for this objfile. */
26070
26071 static hashval_t
26072 partial_die_hash (const void *item)
26073 {
26074 const struct partial_die_info *part_die
26075 = (const struct partial_die_info *) item;
26076
26077 return to_underlying (part_die->sect_off);
26078 }
26079
26080 /* Trivial comparison function for partial_die_info structures: two DIEs
26081 are equal if they have the same offset. */
26082
26083 static int
26084 partial_die_eq (const void *item_lhs, const void *item_rhs)
26085 {
26086 const struct partial_die_info *part_die_lhs
26087 = (const struct partial_die_info *) item_lhs;
26088 const struct partial_die_info *part_die_rhs
26089 = (const struct partial_die_info *) item_rhs;
26090
26091 return part_die_lhs->sect_off == part_die_rhs->sect_off;
26092 }
26093
26094 struct cmd_list_element *set_dwarf_cmdlist;
26095 struct cmd_list_element *show_dwarf_cmdlist;
26096
26097 static void
26098 set_dwarf_cmd (const char *args, int from_tty)
26099 {
26100 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
26101 gdb_stdout);
26102 }
26103
26104 static void
26105 show_dwarf_cmd (const char *args, int from_tty)
26106 {
26107 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
26108 }
26109
26110 bool dwarf_always_disassemble;
26111
26112 static void
26113 show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
26114 struct cmd_list_element *c, const char *value)
26115 {
26116 fprintf_filtered (file,
26117 _("Whether to always disassemble "
26118 "DWARF expressions is %s.\n"),
26119 value);
26120 }
26121
26122 static void
26123 show_check_physname (struct ui_file *file, int from_tty,
26124 struct cmd_list_element *c, const char *value)
26125 {
26126 fprintf_filtered (file,
26127 _("Whether to check \"physname\" is %s.\n"),
26128 value);
26129 }
26130
26131 void _initialize_dwarf2_read ();
26132 void
26133 _initialize_dwarf2_read ()
26134 {
26135 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
26136 Set DWARF specific variables.\n\
26137 Configure DWARF variables such as the cache size."),
26138 &set_dwarf_cmdlist, "maintenance set dwarf ",
26139 0/*allow-unknown*/, &maintenance_set_cmdlist);
26140
26141 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
26142 Show DWARF specific variables.\n\
26143 Show DWARF variables such as the cache size."),
26144 &show_dwarf_cmdlist, "maintenance show dwarf ",
26145 0/*allow-unknown*/, &maintenance_show_cmdlist);
26146
26147 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
26148 &dwarf_max_cache_age, _("\
26149 Set the upper bound on the age of cached DWARF compilation units."), _("\
26150 Show the upper bound on the age of cached DWARF compilation units."), _("\
26151 A higher limit means that cached compilation units will be stored\n\
26152 in memory longer, and more total memory will be used. Zero disables\n\
26153 caching, which can slow down startup."),
26154 NULL,
26155 show_dwarf_max_cache_age,
26156 &set_dwarf_cmdlist,
26157 &show_dwarf_cmdlist);
26158
26159 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
26160 &dwarf_always_disassemble, _("\
26161 Set whether `info address' always disassembles DWARF expressions."), _("\
26162 Show whether `info address' always disassembles DWARF expressions."), _("\
26163 When enabled, DWARF expressions are always printed in an assembly-like\n\
26164 syntax. When disabled, expressions will be printed in a more\n\
26165 conversational style, when possible."),
26166 NULL,
26167 show_dwarf_always_disassemble,
26168 &set_dwarf_cmdlist,
26169 &show_dwarf_cmdlist);
26170
26171 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
26172 Set debugging of the DWARF reader."), _("\
26173 Show debugging of the DWARF reader."), _("\
26174 When enabled (non-zero), debugging messages are printed during DWARF\n\
26175 reading and symtab expansion. A value of 1 (one) provides basic\n\
26176 information. A value greater than 1 provides more verbose information."),
26177 NULL,
26178 NULL,
26179 &setdebuglist, &showdebuglist);
26180
26181 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
26182 Set debugging of the DWARF DIE reader."), _("\
26183 Show debugging of the DWARF DIE reader."), _("\
26184 When enabled (non-zero), DIEs are dumped after they are read in.\n\
26185 The value is the maximum depth to print."),
26186 NULL,
26187 NULL,
26188 &setdebuglist, &showdebuglist);
26189
26190 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
26191 Set debugging of the dwarf line reader."), _("\
26192 Show debugging of the dwarf line reader."), _("\
26193 When enabled (non-zero), line number entries are dumped as they are read in.\n\
26194 A value of 1 (one) provides basic information.\n\
26195 A value greater than 1 provides more verbose information."),
26196 NULL,
26197 NULL,
26198 &setdebuglist, &showdebuglist);
26199
26200 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
26201 Set cross-checking of \"physname\" code against demangler."), _("\
26202 Show cross-checking of \"physname\" code against demangler."), _("\
26203 When enabled, GDB's internal \"physname\" code is checked against\n\
26204 the demangler."),
26205 NULL, show_check_physname,
26206 &setdebuglist, &showdebuglist);
26207
26208 add_setshow_boolean_cmd ("use-deprecated-index-sections",
26209 no_class, &use_deprecated_index_sections, _("\
26210 Set whether to use deprecated gdb_index sections."), _("\
26211 Show whether to use deprecated gdb_index sections."), _("\
26212 When enabled, deprecated .gdb_index sections are used anyway.\n\
26213 Normally they are ignored either because of a missing feature or\n\
26214 performance issue.\n\
26215 Warning: This option must be enabled before gdb reads the file."),
26216 NULL,
26217 NULL,
26218 &setlist, &showlist);
26219
26220 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
26221 &dwarf2_locexpr_funcs);
26222 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
26223 &dwarf2_loclist_funcs);
26224
26225 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
26226 &dwarf2_block_frame_base_locexpr_funcs);
26227 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
26228 &dwarf2_block_frame_base_loclist_funcs);
26229
26230 #if GDB_SELF_TEST
26231 selftests::register_test ("dw2_expand_symtabs_matching",
26232 selftests::dw2_expand_symtabs_matching::run_test);
26233 #endif
26234 }
This page took 0.623409 seconds and 3 git commands to generate.