Introduce partial_symtab::expand_psymtab method
[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 int i;
9667
9668 if (readin)
9669 return;
9670
9671 for (i = 0; i < number_of_dependencies; i++)
9672 if (!dependencies[i]->readin
9673 && dependencies[i]->user == NULL)
9674 {
9675 /* Inform about additional files that need to be read in. */
9676 if (info_verbose)
9677 {
9678 /* FIXME: i18n: Need to make this a single string. */
9679 fputs_filtered (" ", gdb_stdout);
9680 wrap_here ("");
9681 fputs_filtered ("and ", gdb_stdout);
9682 wrap_here ("");
9683 printf_filtered ("%s...", dependencies[i]->filename);
9684 wrap_here (""); /* Flush output. */
9685 gdb_flush (gdb_stdout);
9686 }
9687 dependencies[i]->expand_psymtab (objfile);
9688 }
9689
9690 per_cu = per_cu_data;
9691
9692 if (per_cu == NULL)
9693 {
9694 /* It's an include file, no symbols to read for it.
9695 Everything is in the parent symtab. */
9696 readin = true;
9697 return;
9698 }
9699
9700 dw2_do_instantiate_symtab (per_cu, false);
9701 }
9702
9703 /* Trivial hash function for die_info: the hash value of a DIE
9704 is its offset in .debug_info for this objfile. */
9705
9706 static hashval_t
9707 die_hash (const void *item)
9708 {
9709 const struct die_info *die = (const struct die_info *) item;
9710
9711 return to_underlying (die->sect_off);
9712 }
9713
9714 /* Trivial comparison function for die_info structures: two DIEs
9715 are equal if they have the same offset. */
9716
9717 static int
9718 die_eq (const void *item_lhs, const void *item_rhs)
9719 {
9720 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9721 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
9722
9723 return die_lhs->sect_off == die_rhs->sect_off;
9724 }
9725
9726 /* Load the DIEs associated with PER_CU into memory. */
9727
9728 static void
9729 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
9730 bool skip_partial,
9731 enum language pretend_language)
9732 {
9733 gdb_assert (! this_cu->is_debug_types);
9734
9735 cutu_reader reader (this_cu, NULL, 1, 1, skip_partial);
9736 if (reader.dummy_p)
9737 return;
9738
9739 struct dwarf2_cu *cu = reader.cu;
9740 const gdb_byte *info_ptr = reader.info_ptr;
9741
9742 gdb_assert (cu->die_hash == NULL);
9743 cu->die_hash =
9744 htab_create_alloc_ex (cu->header.length / 12,
9745 die_hash,
9746 die_eq,
9747 NULL,
9748 &cu->comp_unit_obstack,
9749 hashtab_obstack_allocate,
9750 dummy_obstack_deallocate);
9751
9752 if (reader.has_children)
9753 reader.comp_unit_die->child
9754 = read_die_and_siblings (&reader, reader.info_ptr,
9755 &info_ptr, reader.comp_unit_die);
9756 cu->dies = reader.comp_unit_die;
9757 /* comp_unit_die is not stored in die_hash, no need. */
9758
9759 /* We try not to read any attributes in this function, because not
9760 all CUs needed for references have been loaded yet, and symbol
9761 table processing isn't initialized. But we have to set the CU language,
9762 or we won't be able to build types correctly.
9763 Similarly, if we do not read the producer, we can not apply
9764 producer-specific interpretation. */
9765 prepare_one_comp_unit (cu, cu->dies, pretend_language);
9766 }
9767
9768 /* Add a DIE to the delayed physname list. */
9769
9770 static void
9771 add_to_method_list (struct type *type, int fnfield_index, int index,
9772 const char *name, struct die_info *die,
9773 struct dwarf2_cu *cu)
9774 {
9775 struct delayed_method_info mi;
9776 mi.type = type;
9777 mi.fnfield_index = fnfield_index;
9778 mi.index = index;
9779 mi.name = name;
9780 mi.die = die;
9781 cu->method_list.push_back (mi);
9782 }
9783
9784 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9785 "const" / "volatile". If so, decrements LEN by the length of the
9786 modifier and return true. Otherwise return false. */
9787
9788 template<size_t N>
9789 static bool
9790 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9791 {
9792 size_t mod_len = sizeof (mod) - 1;
9793 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9794 {
9795 len -= mod_len;
9796 return true;
9797 }
9798 return false;
9799 }
9800
9801 /* Compute the physnames of any methods on the CU's method list.
9802
9803 The computation of method physnames is delayed in order to avoid the
9804 (bad) condition that one of the method's formal parameters is of an as yet
9805 incomplete type. */
9806
9807 static void
9808 compute_delayed_physnames (struct dwarf2_cu *cu)
9809 {
9810 /* Only C++ delays computing physnames. */
9811 if (cu->method_list.empty ())
9812 return;
9813 gdb_assert (cu->language == language_cplus);
9814
9815 for (const delayed_method_info &mi : cu->method_list)
9816 {
9817 const char *physname;
9818 struct fn_fieldlist *fn_flp
9819 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9820 physname = dwarf2_physname (mi.name, mi.die, cu);
9821 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
9822 = physname ? physname : "";
9823
9824 /* Since there's no tag to indicate whether a method is a
9825 const/volatile overload, extract that information out of the
9826 demangled name. */
9827 if (physname != NULL)
9828 {
9829 size_t len = strlen (physname);
9830
9831 while (1)
9832 {
9833 if (physname[len] == ')') /* shortcut */
9834 break;
9835 else if (check_modifier (physname, len, " const"))
9836 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
9837 else if (check_modifier (physname, len, " volatile"))
9838 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
9839 else
9840 break;
9841 }
9842 }
9843 }
9844
9845 /* The list is no longer needed. */
9846 cu->method_list.clear ();
9847 }
9848
9849 /* Go objects should be embedded in a DW_TAG_module DIE,
9850 and it's not clear if/how imported objects will appear.
9851 To keep Go support simple until that's worked out,
9852 go back through what we've read and create something usable.
9853 We could do this while processing each DIE, and feels kinda cleaner,
9854 but that way is more invasive.
9855 This is to, for example, allow the user to type "p var" or "b main"
9856 without having to specify the package name, and allow lookups
9857 of module.object to work in contexts that use the expression
9858 parser. */
9859
9860 static void
9861 fixup_go_packaging (struct dwarf2_cu *cu)
9862 {
9863 gdb::unique_xmalloc_ptr<char> package_name;
9864 struct pending *list;
9865 int i;
9866
9867 for (list = *cu->get_builder ()->get_global_symbols ();
9868 list != NULL;
9869 list = list->next)
9870 {
9871 for (i = 0; i < list->nsyms; ++i)
9872 {
9873 struct symbol *sym = list->symbol[i];
9874
9875 if (sym->language () == language_go
9876 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9877 {
9878 gdb::unique_xmalloc_ptr<char> this_package_name
9879 (go_symbol_package_name (sym));
9880
9881 if (this_package_name == NULL)
9882 continue;
9883 if (package_name == NULL)
9884 package_name = std::move (this_package_name);
9885 else
9886 {
9887 struct objfile *objfile
9888 = cu->per_cu->dwarf2_per_objfile->objfile;
9889 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
9890 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
9891 (symbol_symtab (sym) != NULL
9892 ? symtab_to_filename_for_display
9893 (symbol_symtab (sym))
9894 : objfile_name (objfile)),
9895 this_package_name.get (), package_name.get ());
9896 }
9897 }
9898 }
9899 }
9900
9901 if (package_name != NULL)
9902 {
9903 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9904 const char *saved_package_name
9905 = obstack_strdup (&objfile->per_bfd->storage_obstack, package_name.get ());
9906 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9907 saved_package_name);
9908 struct symbol *sym;
9909
9910 sym = allocate_symbol (objfile);
9911 sym->set_language (language_go, &objfile->objfile_obstack);
9912 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
9913 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9914 e.g., "main" finds the "main" module and not C's main(). */
9915 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9916 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9917 SYMBOL_TYPE (sym) = type;
9918
9919 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
9920 }
9921 }
9922
9923 /* Allocate a fully-qualified name consisting of the two parts on the
9924 obstack. */
9925
9926 static const char *
9927 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9928 {
9929 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9930 }
9931
9932 /* A helper that allocates a struct discriminant_info to attach to a
9933 union type. */
9934
9935 static struct discriminant_info *
9936 alloc_discriminant_info (struct type *type, int discriminant_index,
9937 int default_index)
9938 {
9939 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9940 gdb_assert (discriminant_index == -1
9941 || (discriminant_index >= 0
9942 && discriminant_index < TYPE_NFIELDS (type)));
9943 gdb_assert (default_index == -1
9944 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
9945
9946 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
9947
9948 struct discriminant_info *disc
9949 = ((struct discriminant_info *)
9950 TYPE_ZALLOC (type,
9951 offsetof (struct discriminant_info, discriminants)
9952 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
9953 disc->default_index = default_index;
9954 disc->discriminant_index = discriminant_index;
9955
9956 struct dynamic_prop prop;
9957 prop.kind = PROP_UNDEFINED;
9958 prop.data.baton = disc;
9959
9960 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
9961
9962 return disc;
9963 }
9964
9965 /* Some versions of rustc emitted enums in an unusual way.
9966
9967 Ordinary enums were emitted as unions. The first element of each
9968 structure in the union was named "RUST$ENUM$DISR". This element
9969 held the discriminant.
9970
9971 These versions of Rust also implemented the "non-zero"
9972 optimization. When the enum had two values, and one is empty and
9973 the other holds a pointer that cannot be zero, the pointer is used
9974 as the discriminant, with a zero value meaning the empty variant.
9975 Here, the union's first member is of the form
9976 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9977 where the fieldnos are the indices of the fields that should be
9978 traversed in order to find the field (which may be several fields deep)
9979 and the variantname is the name of the variant of the case when the
9980 field is zero.
9981
9982 This function recognizes whether TYPE is of one of these forms,
9983 and, if so, smashes it to be a variant type. */
9984
9985 static void
9986 quirk_rust_enum (struct type *type, struct objfile *objfile)
9987 {
9988 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9989
9990 /* We don't need to deal with empty enums. */
9991 if (TYPE_NFIELDS (type) == 0)
9992 return;
9993
9994 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9995 if (TYPE_NFIELDS (type) == 1
9996 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9997 {
9998 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9999
10000 /* Decode the field name to find the offset of the
10001 discriminant. */
10002 ULONGEST bit_offset = 0;
10003 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
10004 while (name[0] >= '0' && name[0] <= '9')
10005 {
10006 char *tail;
10007 unsigned long index = strtoul (name, &tail, 10);
10008 name = tail;
10009 if (*name != '$'
10010 || index >= TYPE_NFIELDS (field_type)
10011 || (TYPE_FIELD_LOC_KIND (field_type, index)
10012 != FIELD_LOC_KIND_BITPOS))
10013 {
10014 complaint (_("Could not parse Rust enum encoding string \"%s\""
10015 "[in module %s]"),
10016 TYPE_FIELD_NAME (type, 0),
10017 objfile_name (objfile));
10018 return;
10019 }
10020 ++name;
10021
10022 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
10023 field_type = TYPE_FIELD_TYPE (field_type, index);
10024 }
10025
10026 /* Make a union to hold the variants. */
10027 struct type *union_type = alloc_type (objfile);
10028 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10029 TYPE_NFIELDS (union_type) = 3;
10030 TYPE_FIELDS (union_type)
10031 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
10032 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
10033 set_type_align (union_type, TYPE_RAW_ALIGN (type));
10034
10035 /* Put the discriminant must at index 0. */
10036 TYPE_FIELD_TYPE (union_type, 0) = field_type;
10037 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10038 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10039 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
10040
10041 /* The order of fields doesn't really matter, so put the real
10042 field at index 1 and the data-less field at index 2. */
10043 struct discriminant_info *disc
10044 = alloc_discriminant_info (union_type, 0, 1);
10045 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
10046 TYPE_FIELD_NAME (union_type, 1)
10047 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
10048 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
10049 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
10050 TYPE_FIELD_NAME (union_type, 1));
10051
10052 const char *dataless_name
10053 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
10054 name);
10055 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
10056 dataless_name);
10057 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
10058 /* NAME points into the original discriminant name, which
10059 already has the correct lifetime. */
10060 TYPE_FIELD_NAME (union_type, 2) = name;
10061 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
10062 disc->discriminants[2] = 0;
10063
10064 /* Smash this type to be a structure type. We have to do this
10065 because the type has already been recorded. */
10066 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10067 TYPE_NFIELDS (type) = 1;
10068 TYPE_FIELDS (type)
10069 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
10070
10071 /* Install the variant part. */
10072 TYPE_FIELD_TYPE (type, 0) = union_type;
10073 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10074 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10075 }
10076 /* A union with a single anonymous field is probably an old-style
10077 univariant enum. */
10078 else if (TYPE_NFIELDS (type) == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
10079 {
10080 /* Smash this type to be a structure type. We have to do this
10081 because the type has already been recorded. */
10082 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10083
10084 /* Make a union to hold the variants. */
10085 struct type *union_type = alloc_type (objfile);
10086 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10087 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
10088 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
10089 set_type_align (union_type, TYPE_RAW_ALIGN (type));
10090 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
10091
10092 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
10093 const char *variant_name
10094 = rust_last_path_segment (TYPE_NAME (field_type));
10095 TYPE_FIELD_NAME (union_type, 0) = variant_name;
10096 TYPE_NAME (field_type)
10097 = rust_fully_qualify (&objfile->objfile_obstack,
10098 TYPE_NAME (type), variant_name);
10099
10100 /* Install the union in the outer struct type. */
10101 TYPE_NFIELDS (type) = 1;
10102 TYPE_FIELDS (type)
10103 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
10104 TYPE_FIELD_TYPE (type, 0) = union_type;
10105 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10106 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10107
10108 alloc_discriminant_info (union_type, -1, 0);
10109 }
10110 else
10111 {
10112 struct type *disr_type = nullptr;
10113 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
10114 {
10115 disr_type = TYPE_FIELD_TYPE (type, i);
10116
10117 if (TYPE_CODE (disr_type) != TYPE_CODE_STRUCT)
10118 {
10119 /* All fields of a true enum will be structs. */
10120 return;
10121 }
10122 else if (TYPE_NFIELDS (disr_type) == 0)
10123 {
10124 /* Could be data-less variant, so keep going. */
10125 disr_type = nullptr;
10126 }
10127 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
10128 "RUST$ENUM$DISR") != 0)
10129 {
10130 /* Not a Rust enum. */
10131 return;
10132 }
10133 else
10134 {
10135 /* Found one. */
10136 break;
10137 }
10138 }
10139
10140 /* If we got here without a discriminant, then it's probably
10141 just a union. */
10142 if (disr_type == nullptr)
10143 return;
10144
10145 /* Smash this type to be a structure type. We have to do this
10146 because the type has already been recorded. */
10147 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10148
10149 /* Make a union to hold the variants. */
10150 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
10151 struct type *union_type = alloc_type (objfile);
10152 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10153 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
10154 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
10155 set_type_align (union_type, TYPE_RAW_ALIGN (type));
10156 TYPE_FIELDS (union_type)
10157 = (struct field *) TYPE_ZALLOC (union_type,
10158 (TYPE_NFIELDS (union_type)
10159 * sizeof (struct field)));
10160
10161 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
10162 TYPE_NFIELDS (type) * sizeof (struct field));
10163
10164 /* Install the discriminant at index 0 in the union. */
10165 TYPE_FIELD (union_type, 0) = *disr_field;
10166 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10167 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10168
10169 /* Install the union in the outer struct type. */
10170 TYPE_FIELD_TYPE (type, 0) = union_type;
10171 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10172 TYPE_NFIELDS (type) = 1;
10173
10174 /* Set the size and offset of the union type. */
10175 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10176
10177 /* We need a way to find the correct discriminant given a
10178 variant name. For convenience we build a map here. */
10179 struct type *enum_type = FIELD_TYPE (*disr_field);
10180 std::unordered_map<std::string, ULONGEST> discriminant_map;
10181 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
10182 {
10183 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
10184 {
10185 const char *name
10186 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
10187 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
10188 }
10189 }
10190
10191 int n_fields = TYPE_NFIELDS (union_type);
10192 struct discriminant_info *disc
10193 = alloc_discriminant_info (union_type, 0, -1);
10194 /* Skip the discriminant here. */
10195 for (int i = 1; i < n_fields; ++i)
10196 {
10197 /* Find the final word in the name of this variant's type.
10198 That name can be used to look up the correct
10199 discriminant. */
10200 const char *variant_name
10201 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
10202 i)));
10203
10204 auto iter = discriminant_map.find (variant_name);
10205 if (iter != discriminant_map.end ())
10206 disc->discriminants[i] = iter->second;
10207
10208 /* Remove the discriminant field, if it exists. */
10209 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
10210 if (TYPE_NFIELDS (sub_type) > 0)
10211 {
10212 --TYPE_NFIELDS (sub_type);
10213 ++TYPE_FIELDS (sub_type);
10214 }
10215 TYPE_FIELD_NAME (union_type, i) = variant_name;
10216 TYPE_NAME (sub_type)
10217 = rust_fully_qualify (&objfile->objfile_obstack,
10218 TYPE_NAME (type), variant_name);
10219 }
10220 }
10221 }
10222
10223 /* Rewrite some Rust unions to be structures with variants parts. */
10224
10225 static void
10226 rust_union_quirks (struct dwarf2_cu *cu)
10227 {
10228 gdb_assert (cu->language == language_rust);
10229 for (type *type_ : cu->rust_unions)
10230 quirk_rust_enum (type_, cu->per_cu->dwarf2_per_objfile->objfile);
10231 /* We don't need this any more. */
10232 cu->rust_unions.clear ();
10233 }
10234
10235 /* Return the symtab for PER_CU. This works properly regardless of
10236 whether we're using the index or psymtabs. */
10237
10238 static struct compunit_symtab *
10239 get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
10240 {
10241 return (per_cu->dwarf2_per_objfile->using_index
10242 ? per_cu->v.quick->compunit_symtab
10243 : per_cu->v.psymtab->compunit_symtab);
10244 }
10245
10246 /* A helper function for computing the list of all symbol tables
10247 included by PER_CU. */
10248
10249 static void
10250 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
10251 htab_t all_children, htab_t all_type_symtabs,
10252 struct dwarf2_per_cu_data *per_cu,
10253 struct compunit_symtab *immediate_parent)
10254 {
10255 void **slot;
10256 struct compunit_symtab *cust;
10257
10258 slot = htab_find_slot (all_children, per_cu, INSERT);
10259 if (*slot != NULL)
10260 {
10261 /* This inclusion and its children have been processed. */
10262 return;
10263 }
10264
10265 *slot = per_cu;
10266 /* Only add a CU if it has a symbol table. */
10267 cust = get_compunit_symtab (per_cu);
10268 if (cust != NULL)
10269 {
10270 /* If this is a type unit only add its symbol table if we haven't
10271 seen it yet (type unit per_cu's can share symtabs). */
10272 if (per_cu->is_debug_types)
10273 {
10274 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
10275 if (*slot == NULL)
10276 {
10277 *slot = cust;
10278 result->push_back (cust);
10279 if (cust->user == NULL)
10280 cust->user = immediate_parent;
10281 }
10282 }
10283 else
10284 {
10285 result->push_back (cust);
10286 if (cust->user == NULL)
10287 cust->user = immediate_parent;
10288 }
10289 }
10290
10291 if (!per_cu->imported_symtabs_empty ())
10292 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
10293 {
10294 recursively_compute_inclusions (result, all_children,
10295 all_type_symtabs, ptr, cust);
10296 }
10297 }
10298
10299 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
10300 PER_CU. */
10301
10302 static void
10303 compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
10304 {
10305 gdb_assert (! per_cu->is_debug_types);
10306
10307 if (!per_cu->imported_symtabs_empty ())
10308 {
10309 int len;
10310 std::vector<compunit_symtab *> result_symtabs;
10311 htab_t all_children, all_type_symtabs;
10312 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
10313
10314 /* If we don't have a symtab, we can just skip this case. */
10315 if (cust == NULL)
10316 return;
10317
10318 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10319 NULL, xcalloc, xfree);
10320 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10321 NULL, xcalloc, xfree);
10322
10323 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
10324 {
10325 recursively_compute_inclusions (&result_symtabs, all_children,
10326 all_type_symtabs, ptr, cust);
10327 }
10328
10329 /* Now we have a transitive closure of all the included symtabs. */
10330 len = result_symtabs.size ();
10331 cust->includes
10332 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
10333 struct compunit_symtab *, len + 1);
10334 memcpy (cust->includes, result_symtabs.data (),
10335 len * sizeof (compunit_symtab *));
10336 cust->includes[len] = NULL;
10337
10338 htab_delete (all_children);
10339 htab_delete (all_type_symtabs);
10340 }
10341 }
10342
10343 /* Compute the 'includes' field for the symtabs of all the CUs we just
10344 read. */
10345
10346 static void
10347 process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
10348 {
10349 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->just_read_cus)
10350 {
10351 if (! iter->is_debug_types)
10352 compute_compunit_symtab_includes (iter);
10353 }
10354
10355 dwarf2_per_objfile->just_read_cus.clear ();
10356 }
10357
10358 /* Generate full symbol information for PER_CU, whose DIEs have
10359 already been loaded into memory. */
10360
10361 static void
10362 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
10363 enum language pretend_language)
10364 {
10365 struct dwarf2_cu *cu = per_cu->cu;
10366 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10367 struct objfile *objfile = dwarf2_per_objfile->objfile;
10368 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10369 CORE_ADDR lowpc, highpc;
10370 struct compunit_symtab *cust;
10371 CORE_ADDR baseaddr;
10372 struct block *static_block;
10373 CORE_ADDR addr;
10374
10375 baseaddr = objfile->text_section_offset ();
10376
10377 /* Clear the list here in case something was left over. */
10378 cu->method_list.clear ();
10379
10380 cu->language = pretend_language;
10381 cu->language_defn = language_def (cu->language);
10382
10383 /* Do line number decoding in read_file_scope () */
10384 process_die (cu->dies, cu);
10385
10386 /* For now fudge the Go package. */
10387 if (cu->language == language_go)
10388 fixup_go_packaging (cu);
10389
10390 /* Now that we have processed all the DIEs in the CU, all the types
10391 should be complete, and it should now be safe to compute all of the
10392 physnames. */
10393 compute_delayed_physnames (cu);
10394
10395 if (cu->language == language_rust)
10396 rust_union_quirks (cu);
10397
10398 /* Some compilers don't define a DW_AT_high_pc attribute for the
10399 compilation unit. If the DW_AT_high_pc is missing, synthesize
10400 it, by scanning the DIE's below the compilation unit. */
10401 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
10402
10403 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
10404 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
10405
10406 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10407 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10408 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10409 addrmap to help ensure it has an accurate map of pc values belonging to
10410 this comp unit. */
10411 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10412
10413 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
10414 SECT_OFF_TEXT (objfile),
10415 0);
10416
10417 if (cust != NULL)
10418 {
10419 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
10420
10421 /* Set symtab language to language from DW_AT_language. If the
10422 compilation is from a C file generated by language preprocessors, do
10423 not set the language if it was already deduced by start_subfile. */
10424 if (!(cu->language == language_c
10425 && COMPUNIT_FILETABS (cust)->language != language_unknown))
10426 COMPUNIT_FILETABS (cust)->language = cu->language;
10427
10428 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10429 produce DW_AT_location with location lists but it can be possibly
10430 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10431 there were bugs in prologue debug info, fixed later in GCC-4.5
10432 by "unwind info for epilogues" patch (which is not directly related).
10433
10434 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10435 needed, it would be wrong due to missing DW_AT_producer there.
10436
10437 Still one can confuse GDB by using non-standard GCC compilation
10438 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
10439 */
10440 if (cu->has_loclist && gcc_4_minor >= 5)
10441 cust->locations_valid = 1;
10442
10443 if (gcc_4_minor >= 5)
10444 cust->epilogue_unwind_valid = 1;
10445
10446 cust->call_site_htab = cu->call_site_htab;
10447 }
10448
10449 if (dwarf2_per_objfile->using_index)
10450 per_cu->v.quick->compunit_symtab = cust;
10451 else
10452 {
10453 dwarf2_psymtab *pst = per_cu->v.psymtab;
10454 pst->compunit_symtab = cust;
10455 pst->readin = true;
10456 }
10457
10458 /* Push it for inclusion processing later. */
10459 dwarf2_per_objfile->just_read_cus.push_back (per_cu);
10460
10461 /* Not needed any more. */
10462 cu->reset_builder ();
10463 }
10464
10465 /* Generate full symbol information for type unit PER_CU, whose DIEs have
10466 already been loaded into memory. */
10467
10468 static void
10469 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
10470 enum language pretend_language)
10471 {
10472 struct dwarf2_cu *cu = per_cu->cu;
10473 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10474 struct objfile *objfile = dwarf2_per_objfile->objfile;
10475 struct compunit_symtab *cust;
10476 struct signatured_type *sig_type;
10477
10478 gdb_assert (per_cu->is_debug_types);
10479 sig_type = (struct signatured_type *) per_cu;
10480
10481 /* Clear the list here in case something was left over. */
10482 cu->method_list.clear ();
10483
10484 cu->language = pretend_language;
10485 cu->language_defn = language_def (cu->language);
10486
10487 /* The symbol tables are set up in read_type_unit_scope. */
10488 process_die (cu->dies, cu);
10489
10490 /* For now fudge the Go package. */
10491 if (cu->language == language_go)
10492 fixup_go_packaging (cu);
10493
10494 /* Now that we have processed all the DIEs in the CU, all the types
10495 should be complete, and it should now be safe to compute all of the
10496 physnames. */
10497 compute_delayed_physnames (cu);
10498
10499 if (cu->language == language_rust)
10500 rust_union_quirks (cu);
10501
10502 /* TUs share symbol tables.
10503 If this is the first TU to use this symtab, complete the construction
10504 of it with end_expandable_symtab. Otherwise, complete the addition of
10505 this TU's symbols to the existing symtab. */
10506 if (sig_type->type_unit_group->compunit_symtab == NULL)
10507 {
10508 buildsym_compunit *builder = cu->get_builder ();
10509 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
10510 sig_type->type_unit_group->compunit_symtab = cust;
10511
10512 if (cust != NULL)
10513 {
10514 /* Set symtab language to language from DW_AT_language. If the
10515 compilation is from a C file generated by language preprocessors,
10516 do not set the language if it was already deduced by
10517 start_subfile. */
10518 if (!(cu->language == language_c
10519 && COMPUNIT_FILETABS (cust)->language != language_c))
10520 COMPUNIT_FILETABS (cust)->language = cu->language;
10521 }
10522 }
10523 else
10524 {
10525 cu->get_builder ()->augment_type_symtab ();
10526 cust = sig_type->type_unit_group->compunit_symtab;
10527 }
10528
10529 if (dwarf2_per_objfile->using_index)
10530 per_cu->v.quick->compunit_symtab = cust;
10531 else
10532 {
10533 dwarf2_psymtab *pst = per_cu->v.psymtab;
10534 pst->compunit_symtab = cust;
10535 pst->readin = true;
10536 }
10537
10538 /* Not needed any more. */
10539 cu->reset_builder ();
10540 }
10541
10542 /* Process an imported unit DIE. */
10543
10544 static void
10545 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10546 {
10547 struct attribute *attr;
10548
10549 /* For now we don't handle imported units in type units. */
10550 if (cu->per_cu->is_debug_types)
10551 {
10552 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10553 " supported in type units [in module %s]"),
10554 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
10555 }
10556
10557 attr = dwarf2_attr (die, DW_AT_import, cu);
10558 if (attr != NULL)
10559 {
10560 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10561 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10562 dwarf2_per_cu_data *per_cu
10563 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
10564 cu->per_cu->dwarf2_per_objfile);
10565
10566 /* If necessary, add it to the queue and load its DIEs. */
10567 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
10568 load_full_comp_unit (per_cu, false, cu->language);
10569
10570 cu->per_cu->imported_symtabs_push (per_cu);
10571 }
10572 }
10573
10574 /* RAII object that represents a process_die scope: i.e.,
10575 starts/finishes processing a DIE. */
10576 class process_die_scope
10577 {
10578 public:
10579 process_die_scope (die_info *die, dwarf2_cu *cu)
10580 : m_die (die), m_cu (cu)
10581 {
10582 /* We should only be processing DIEs not already in process. */
10583 gdb_assert (!m_die->in_process);
10584 m_die->in_process = true;
10585 }
10586
10587 ~process_die_scope ()
10588 {
10589 m_die->in_process = false;
10590
10591 /* If we're done processing the DIE for the CU that owns the line
10592 header, we don't need the line header anymore. */
10593 if (m_cu->line_header_die_owner == m_die)
10594 {
10595 delete m_cu->line_header;
10596 m_cu->line_header = NULL;
10597 m_cu->line_header_die_owner = NULL;
10598 }
10599 }
10600
10601 private:
10602 die_info *m_die;
10603 dwarf2_cu *m_cu;
10604 };
10605
10606 /* Process a die and its children. */
10607
10608 static void
10609 process_die (struct die_info *die, struct dwarf2_cu *cu)
10610 {
10611 process_die_scope scope (die, cu);
10612
10613 switch (die->tag)
10614 {
10615 case DW_TAG_padding:
10616 break;
10617 case DW_TAG_compile_unit:
10618 case DW_TAG_partial_unit:
10619 read_file_scope (die, cu);
10620 break;
10621 case DW_TAG_type_unit:
10622 read_type_unit_scope (die, cu);
10623 break;
10624 case DW_TAG_subprogram:
10625 /* Nested subprograms in Fortran get a prefix. */
10626 if (cu->language == language_fortran
10627 && die->parent != NULL
10628 && die->parent->tag == DW_TAG_subprogram)
10629 cu->processing_has_namespace_info = true;
10630 /* Fall through. */
10631 case DW_TAG_inlined_subroutine:
10632 read_func_scope (die, cu);
10633 break;
10634 case DW_TAG_lexical_block:
10635 case DW_TAG_try_block:
10636 case DW_TAG_catch_block:
10637 read_lexical_block_scope (die, cu);
10638 break;
10639 case DW_TAG_call_site:
10640 case DW_TAG_GNU_call_site:
10641 read_call_site_scope (die, cu);
10642 break;
10643 case DW_TAG_class_type:
10644 case DW_TAG_interface_type:
10645 case DW_TAG_structure_type:
10646 case DW_TAG_union_type:
10647 process_structure_scope (die, cu);
10648 break;
10649 case DW_TAG_enumeration_type:
10650 process_enumeration_scope (die, cu);
10651 break;
10652
10653 /* These dies have a type, but processing them does not create
10654 a symbol or recurse to process the children. Therefore we can
10655 read them on-demand through read_type_die. */
10656 case DW_TAG_subroutine_type:
10657 case DW_TAG_set_type:
10658 case DW_TAG_array_type:
10659 case DW_TAG_pointer_type:
10660 case DW_TAG_ptr_to_member_type:
10661 case DW_TAG_reference_type:
10662 case DW_TAG_rvalue_reference_type:
10663 case DW_TAG_string_type:
10664 break;
10665
10666 case DW_TAG_base_type:
10667 case DW_TAG_subrange_type:
10668 case DW_TAG_typedef:
10669 /* Add a typedef symbol for the type definition, if it has a
10670 DW_AT_name. */
10671 new_symbol (die, read_type_die (die, cu), cu);
10672 break;
10673 case DW_TAG_common_block:
10674 read_common_block (die, cu);
10675 break;
10676 case DW_TAG_common_inclusion:
10677 break;
10678 case DW_TAG_namespace:
10679 cu->processing_has_namespace_info = true;
10680 read_namespace (die, cu);
10681 break;
10682 case DW_TAG_module:
10683 cu->processing_has_namespace_info = true;
10684 read_module (die, cu);
10685 break;
10686 case DW_TAG_imported_declaration:
10687 cu->processing_has_namespace_info = true;
10688 if (read_namespace_alias (die, cu))
10689 break;
10690 /* The declaration is not a global namespace alias. */
10691 /* Fall through. */
10692 case DW_TAG_imported_module:
10693 cu->processing_has_namespace_info = true;
10694 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10695 || cu->language != language_fortran))
10696 complaint (_("Tag '%s' has unexpected children"),
10697 dwarf_tag_name (die->tag));
10698 read_import_statement (die, cu);
10699 break;
10700
10701 case DW_TAG_imported_unit:
10702 process_imported_unit_die (die, cu);
10703 break;
10704
10705 case DW_TAG_variable:
10706 read_variable (die, cu);
10707 break;
10708
10709 default:
10710 new_symbol (die, NULL, cu);
10711 break;
10712 }
10713 }
10714 \f
10715 /* DWARF name computation. */
10716
10717 /* A helper function for dwarf2_compute_name which determines whether DIE
10718 needs to have the name of the scope prepended to the name listed in the
10719 die. */
10720
10721 static int
10722 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10723 {
10724 struct attribute *attr;
10725
10726 switch (die->tag)
10727 {
10728 case DW_TAG_namespace:
10729 case DW_TAG_typedef:
10730 case DW_TAG_class_type:
10731 case DW_TAG_interface_type:
10732 case DW_TAG_structure_type:
10733 case DW_TAG_union_type:
10734 case DW_TAG_enumeration_type:
10735 case DW_TAG_enumerator:
10736 case DW_TAG_subprogram:
10737 case DW_TAG_inlined_subroutine:
10738 case DW_TAG_member:
10739 case DW_TAG_imported_declaration:
10740 return 1;
10741
10742 case DW_TAG_variable:
10743 case DW_TAG_constant:
10744 /* We only need to prefix "globally" visible variables. These include
10745 any variable marked with DW_AT_external or any variable that
10746 lives in a namespace. [Variables in anonymous namespaces
10747 require prefixing, but they are not DW_AT_external.] */
10748
10749 if (dwarf2_attr (die, DW_AT_specification, cu))
10750 {
10751 struct dwarf2_cu *spec_cu = cu;
10752
10753 return die_needs_namespace (die_specification (die, &spec_cu),
10754 spec_cu);
10755 }
10756
10757 attr = dwarf2_attr (die, DW_AT_external, cu);
10758 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10759 && die->parent->tag != DW_TAG_module)
10760 return 0;
10761 /* A variable in a lexical block of some kind does not need a
10762 namespace, even though in C++ such variables may be external
10763 and have a mangled name. */
10764 if (die->parent->tag == DW_TAG_lexical_block
10765 || die->parent->tag == DW_TAG_try_block
10766 || die->parent->tag == DW_TAG_catch_block
10767 || die->parent->tag == DW_TAG_subprogram)
10768 return 0;
10769 return 1;
10770
10771 default:
10772 return 0;
10773 }
10774 }
10775
10776 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10777 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10778 defined for the given DIE. */
10779
10780 static struct attribute *
10781 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10782 {
10783 struct attribute *attr;
10784
10785 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10786 if (attr == NULL)
10787 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10788
10789 return attr;
10790 }
10791
10792 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10793 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10794 defined for the given DIE. */
10795
10796 static const char *
10797 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10798 {
10799 const char *linkage_name;
10800
10801 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10802 if (linkage_name == NULL)
10803 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10804
10805 return linkage_name;
10806 }
10807
10808 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
10809 compute the physname for the object, which include a method's:
10810 - formal parameters (C++),
10811 - receiver type (Go),
10812
10813 The term "physname" is a bit confusing.
10814 For C++, for example, it is the demangled name.
10815 For Go, for example, it's the mangled name.
10816
10817 For Ada, return the DIE's linkage name rather than the fully qualified
10818 name. PHYSNAME is ignored..
10819
10820 The result is allocated on the objfile_obstack and canonicalized. */
10821
10822 static const char *
10823 dwarf2_compute_name (const char *name,
10824 struct die_info *die, struct dwarf2_cu *cu,
10825 int physname)
10826 {
10827 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10828
10829 if (name == NULL)
10830 name = dwarf2_name (die, cu);
10831
10832 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10833 but otherwise compute it by typename_concat inside GDB.
10834 FIXME: Actually this is not really true, or at least not always true.
10835 It's all very confusing. compute_and_set_names doesn't try to demangle
10836 Fortran names because there is no mangling standard. So new_symbol
10837 will set the demangled name to the result of dwarf2_full_name, and it is
10838 the demangled name that GDB uses if it exists. */
10839 if (cu->language == language_ada
10840 || (cu->language == language_fortran && physname))
10841 {
10842 /* For Ada unit, we prefer the linkage name over the name, as
10843 the former contains the exported name, which the user expects
10844 to be able to reference. Ideally, we want the user to be able
10845 to reference this entity using either natural or linkage name,
10846 but we haven't started looking at this enhancement yet. */
10847 const char *linkage_name = dw2_linkage_name (die, cu);
10848
10849 if (linkage_name != NULL)
10850 return linkage_name;
10851 }
10852
10853 /* These are the only languages we know how to qualify names in. */
10854 if (name != NULL
10855 && (cu->language == language_cplus
10856 || cu->language == language_fortran || cu->language == language_d
10857 || cu->language == language_rust))
10858 {
10859 if (die_needs_namespace (die, cu))
10860 {
10861 const char *prefix;
10862 const char *canonical_name = NULL;
10863
10864 string_file buf;
10865
10866 prefix = determine_prefix (die, cu);
10867 if (*prefix != '\0')
10868 {
10869 gdb::unique_xmalloc_ptr<char> prefixed_name
10870 (typename_concat (NULL, prefix, name, physname, cu));
10871
10872 buf.puts (prefixed_name.get ());
10873 }
10874 else
10875 buf.puts (name);
10876
10877 /* Template parameters may be specified in the DIE's DW_AT_name, or
10878 as children with DW_TAG_template_type_param or
10879 DW_TAG_value_type_param. If the latter, add them to the name
10880 here. If the name already has template parameters, then
10881 skip this step; some versions of GCC emit both, and
10882 it is more efficient to use the pre-computed name.
10883
10884 Something to keep in mind about this process: it is very
10885 unlikely, or in some cases downright impossible, to produce
10886 something that will match the mangled name of a function.
10887 If the definition of the function has the same debug info,
10888 we should be able to match up with it anyway. But fallbacks
10889 using the minimal symbol, for instance to find a method
10890 implemented in a stripped copy of libstdc++, will not work.
10891 If we do not have debug info for the definition, we will have to
10892 match them up some other way.
10893
10894 When we do name matching there is a related problem with function
10895 templates; two instantiated function templates are allowed to
10896 differ only by their return types, which we do not add here. */
10897
10898 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10899 {
10900 struct attribute *attr;
10901 struct die_info *child;
10902 int first = 1;
10903
10904 die->building_fullname = 1;
10905
10906 for (child = die->child; child != NULL; child = child->sibling)
10907 {
10908 struct type *type;
10909 LONGEST value;
10910 const gdb_byte *bytes;
10911 struct dwarf2_locexpr_baton *baton;
10912 struct value *v;
10913
10914 if (child->tag != DW_TAG_template_type_param
10915 && child->tag != DW_TAG_template_value_param)
10916 continue;
10917
10918 if (first)
10919 {
10920 buf.puts ("<");
10921 first = 0;
10922 }
10923 else
10924 buf.puts (", ");
10925
10926 attr = dwarf2_attr (child, DW_AT_type, cu);
10927 if (attr == NULL)
10928 {
10929 complaint (_("template parameter missing DW_AT_type"));
10930 buf.puts ("UNKNOWN_TYPE");
10931 continue;
10932 }
10933 type = die_type (child, cu);
10934
10935 if (child->tag == DW_TAG_template_type_param)
10936 {
10937 c_print_type (type, "", &buf, -1, 0, cu->language,
10938 &type_print_raw_options);
10939 continue;
10940 }
10941
10942 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10943 if (attr == NULL)
10944 {
10945 complaint (_("template parameter missing "
10946 "DW_AT_const_value"));
10947 buf.puts ("UNKNOWN_VALUE");
10948 continue;
10949 }
10950
10951 dwarf2_const_value_attr (attr, type, name,
10952 &cu->comp_unit_obstack, cu,
10953 &value, &bytes, &baton);
10954
10955 if (TYPE_NOSIGN (type))
10956 /* GDB prints characters as NUMBER 'CHAR'. If that's
10957 changed, this can use value_print instead. */
10958 c_printchar (value, type, &buf);
10959 else
10960 {
10961 struct value_print_options opts;
10962
10963 if (baton != NULL)
10964 v = dwarf2_evaluate_loc_desc (type, NULL,
10965 baton->data,
10966 baton->size,
10967 baton->per_cu);
10968 else if (bytes != NULL)
10969 {
10970 v = allocate_value (type);
10971 memcpy (value_contents_writeable (v), bytes,
10972 TYPE_LENGTH (type));
10973 }
10974 else
10975 v = value_from_longest (type, value);
10976
10977 /* Specify decimal so that we do not depend on
10978 the radix. */
10979 get_formatted_print_options (&opts, 'd');
10980 opts.raw = 1;
10981 value_print (v, &buf, &opts);
10982 release_value (v);
10983 }
10984 }
10985
10986 die->building_fullname = 0;
10987
10988 if (!first)
10989 {
10990 /* Close the argument list, with a space if necessary
10991 (nested templates). */
10992 if (!buf.empty () && buf.string ().back () == '>')
10993 buf.puts (" >");
10994 else
10995 buf.puts (">");
10996 }
10997 }
10998
10999 /* For C++ methods, append formal parameter type
11000 information, if PHYSNAME. */
11001
11002 if (physname && die->tag == DW_TAG_subprogram
11003 && cu->language == language_cplus)
11004 {
11005 struct type *type = read_type_die (die, cu);
11006
11007 c_type_print_args (type, &buf, 1, cu->language,
11008 &type_print_raw_options);
11009
11010 if (cu->language == language_cplus)
11011 {
11012 /* Assume that an artificial first parameter is
11013 "this", but do not crash if it is not. RealView
11014 marks unnamed (and thus unused) parameters as
11015 artificial; there is no way to differentiate
11016 the two cases. */
11017 if (TYPE_NFIELDS (type) > 0
11018 && TYPE_FIELD_ARTIFICIAL (type, 0)
11019 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
11020 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
11021 0))))
11022 buf.puts (" const");
11023 }
11024 }
11025
11026 const std::string &intermediate_name = buf.string ();
11027
11028 if (cu->language == language_cplus)
11029 canonical_name
11030 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
11031 &objfile->per_bfd->storage_obstack);
11032
11033 /* If we only computed INTERMEDIATE_NAME, or if
11034 INTERMEDIATE_NAME is already canonical, then we need to
11035 copy it to the appropriate obstack. */
11036 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
11037 name = obstack_strdup (&objfile->per_bfd->storage_obstack,
11038 intermediate_name);
11039 else
11040 name = canonical_name;
11041 }
11042 }
11043
11044 return name;
11045 }
11046
11047 /* Return the fully qualified name of DIE, based on its DW_AT_name.
11048 If scope qualifiers are appropriate they will be added. The result
11049 will be allocated on the storage_obstack, or NULL if the DIE does
11050 not have a name. NAME may either be from a previous call to
11051 dwarf2_name or NULL.
11052
11053 The output string will be canonicalized (if C++). */
11054
11055 static const char *
11056 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
11057 {
11058 return dwarf2_compute_name (name, die, cu, 0);
11059 }
11060
11061 /* Construct a physname for the given DIE in CU. NAME may either be
11062 from a previous call to dwarf2_name or NULL. The result will be
11063 allocated on the objfile_objstack or NULL if the DIE does not have a
11064 name.
11065
11066 The output string will be canonicalized (if C++). */
11067
11068 static const char *
11069 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
11070 {
11071 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
11072 const char *retval, *mangled = NULL, *canon = NULL;
11073 int need_copy = 1;
11074
11075 /* In this case dwarf2_compute_name is just a shortcut not building anything
11076 on its own. */
11077 if (!die_needs_namespace (die, cu))
11078 return dwarf2_compute_name (name, die, cu, 1);
11079
11080 mangled = dw2_linkage_name (die, cu);
11081
11082 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
11083 See https://github.com/rust-lang/rust/issues/32925. */
11084 if (cu->language == language_rust && mangled != NULL
11085 && strchr (mangled, '{') != NULL)
11086 mangled = NULL;
11087
11088 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
11089 has computed. */
11090 gdb::unique_xmalloc_ptr<char> demangled;
11091 if (mangled != NULL)
11092 {
11093
11094 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
11095 {
11096 /* Do nothing (do not demangle the symbol name). */
11097 }
11098 else if (cu->language == language_go)
11099 {
11100 /* This is a lie, but we already lie to the caller new_symbol.
11101 new_symbol assumes we return the mangled name.
11102 This just undoes that lie until things are cleaned up. */
11103 }
11104 else
11105 {
11106 /* Use DMGL_RET_DROP for C++ template functions to suppress
11107 their return type. It is easier for GDB users to search
11108 for such functions as `name(params)' than `long name(params)'.
11109 In such case the minimal symbol names do not match the full
11110 symbol names but for template functions there is never a need
11111 to look up their definition from their declaration so
11112 the only disadvantage remains the minimal symbol variant
11113 `long name(params)' does not have the proper inferior type. */
11114 demangled.reset (gdb_demangle (mangled,
11115 (DMGL_PARAMS | DMGL_ANSI
11116 | DMGL_RET_DROP)));
11117 }
11118 if (demangled)
11119 canon = demangled.get ();
11120 else
11121 {
11122 canon = mangled;
11123 need_copy = 0;
11124 }
11125 }
11126
11127 if (canon == NULL || check_physname)
11128 {
11129 const char *physname = dwarf2_compute_name (name, die, cu, 1);
11130
11131 if (canon != NULL && strcmp (physname, canon) != 0)
11132 {
11133 /* It may not mean a bug in GDB. The compiler could also
11134 compute DW_AT_linkage_name incorrectly. But in such case
11135 GDB would need to be bug-to-bug compatible. */
11136
11137 complaint (_("Computed physname <%s> does not match demangled <%s> "
11138 "(from linkage <%s>) - DIE at %s [in module %s]"),
11139 physname, canon, mangled, sect_offset_str (die->sect_off),
11140 objfile_name (objfile));
11141
11142 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
11143 is available here - over computed PHYSNAME. It is safer
11144 against both buggy GDB and buggy compilers. */
11145
11146 retval = canon;
11147 }
11148 else
11149 {
11150 retval = physname;
11151 need_copy = 0;
11152 }
11153 }
11154 else
11155 retval = canon;
11156
11157 if (need_copy)
11158 retval = obstack_strdup (&objfile->per_bfd->storage_obstack, retval);
11159
11160 return retval;
11161 }
11162
11163 /* Inspect DIE in CU for a namespace alias. If one exists, record
11164 a new symbol for it.
11165
11166 Returns 1 if a namespace alias was recorded, 0 otherwise. */
11167
11168 static int
11169 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
11170 {
11171 struct attribute *attr;
11172
11173 /* If the die does not have a name, this is not a namespace
11174 alias. */
11175 attr = dwarf2_attr (die, DW_AT_name, cu);
11176 if (attr != NULL)
11177 {
11178 int num;
11179 struct die_info *d = die;
11180 struct dwarf2_cu *imported_cu = cu;
11181
11182 /* If the compiler has nested DW_AT_imported_declaration DIEs,
11183 keep inspecting DIEs until we hit the underlying import. */
11184 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
11185 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
11186 {
11187 attr = dwarf2_attr (d, DW_AT_import, cu);
11188 if (attr == NULL)
11189 break;
11190
11191 d = follow_die_ref (d, attr, &imported_cu);
11192 if (d->tag != DW_TAG_imported_declaration)
11193 break;
11194 }
11195
11196 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
11197 {
11198 complaint (_("DIE at %s has too many recursively imported "
11199 "declarations"), sect_offset_str (d->sect_off));
11200 return 0;
11201 }
11202
11203 if (attr != NULL)
11204 {
11205 struct type *type;
11206 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
11207
11208 type = get_die_type_at_offset (sect_off, cu->per_cu);
11209 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
11210 {
11211 /* This declaration is a global namespace alias. Add
11212 a symbol for it whose type is the aliased namespace. */
11213 new_symbol (die, type, cu);
11214 return 1;
11215 }
11216 }
11217 }
11218
11219 return 0;
11220 }
11221
11222 /* Return the using directives repository (global or local?) to use in the
11223 current context for CU.
11224
11225 For Ada, imported declarations can materialize renamings, which *may* be
11226 global. However it is impossible (for now?) in DWARF to distinguish
11227 "external" imported declarations and "static" ones. As all imported
11228 declarations seem to be static in all other languages, make them all CU-wide
11229 global only in Ada. */
11230
11231 static struct using_direct **
11232 using_directives (struct dwarf2_cu *cu)
11233 {
11234 if (cu->language == language_ada
11235 && cu->get_builder ()->outermost_context_p ())
11236 return cu->get_builder ()->get_global_using_directives ();
11237 else
11238 return cu->get_builder ()->get_local_using_directives ();
11239 }
11240
11241 /* Read the import statement specified by the given die and record it. */
11242
11243 static void
11244 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
11245 {
11246 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
11247 struct attribute *import_attr;
11248 struct die_info *imported_die, *child_die;
11249 struct dwarf2_cu *imported_cu;
11250 const char *imported_name;
11251 const char *imported_name_prefix;
11252 const char *canonical_name;
11253 const char *import_alias;
11254 const char *imported_declaration = NULL;
11255 const char *import_prefix;
11256 std::vector<const char *> excludes;
11257
11258 import_attr = dwarf2_attr (die, DW_AT_import, cu);
11259 if (import_attr == NULL)
11260 {
11261 complaint (_("Tag '%s' has no DW_AT_import"),
11262 dwarf_tag_name (die->tag));
11263 return;
11264 }
11265
11266 imported_cu = cu;
11267 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
11268 imported_name = dwarf2_name (imported_die, imported_cu);
11269 if (imported_name == NULL)
11270 {
11271 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
11272
11273 The import in the following code:
11274 namespace A
11275 {
11276 typedef int B;
11277 }
11278
11279 int main ()
11280 {
11281 using A::B;
11282 B b;
11283 return b;
11284 }
11285
11286 ...
11287 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
11288 <52> DW_AT_decl_file : 1
11289 <53> DW_AT_decl_line : 6
11290 <54> DW_AT_import : <0x75>
11291 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
11292 <59> DW_AT_name : B
11293 <5b> DW_AT_decl_file : 1
11294 <5c> DW_AT_decl_line : 2
11295 <5d> DW_AT_type : <0x6e>
11296 ...
11297 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
11298 <76> DW_AT_byte_size : 4
11299 <77> DW_AT_encoding : 5 (signed)
11300
11301 imports the wrong die ( 0x75 instead of 0x58 ).
11302 This case will be ignored until the gcc bug is fixed. */
11303 return;
11304 }
11305
11306 /* Figure out the local name after import. */
11307 import_alias = dwarf2_name (die, cu);
11308
11309 /* Figure out where the statement is being imported to. */
11310 import_prefix = determine_prefix (die, cu);
11311
11312 /* Figure out what the scope of the imported die is and prepend it
11313 to the name of the imported die. */
11314 imported_name_prefix = determine_prefix (imported_die, imported_cu);
11315
11316 if (imported_die->tag != DW_TAG_namespace
11317 && imported_die->tag != DW_TAG_module)
11318 {
11319 imported_declaration = imported_name;
11320 canonical_name = imported_name_prefix;
11321 }
11322 else if (strlen (imported_name_prefix) > 0)
11323 canonical_name = obconcat (&objfile->objfile_obstack,
11324 imported_name_prefix,
11325 (cu->language == language_d ? "." : "::"),
11326 imported_name, (char *) NULL);
11327 else
11328 canonical_name = imported_name;
11329
11330 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
11331 for (child_die = die->child; child_die && child_die->tag;
11332 child_die = sibling_die (child_die))
11333 {
11334 /* DWARF-4: A Fortran use statement with a “rename list” may be
11335 represented by an imported module entry with an import attribute
11336 referring to the module and owned entries corresponding to those
11337 entities that are renamed as part of being imported. */
11338
11339 if (child_die->tag != DW_TAG_imported_declaration)
11340 {
11341 complaint (_("child DW_TAG_imported_declaration expected "
11342 "- DIE at %s [in module %s]"),
11343 sect_offset_str (child_die->sect_off),
11344 objfile_name (objfile));
11345 continue;
11346 }
11347
11348 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
11349 if (import_attr == NULL)
11350 {
11351 complaint (_("Tag '%s' has no DW_AT_import"),
11352 dwarf_tag_name (child_die->tag));
11353 continue;
11354 }
11355
11356 imported_cu = cu;
11357 imported_die = follow_die_ref_or_sig (child_die, import_attr,
11358 &imported_cu);
11359 imported_name = dwarf2_name (imported_die, imported_cu);
11360 if (imported_name == NULL)
11361 {
11362 complaint (_("child DW_TAG_imported_declaration has unknown "
11363 "imported name - DIE at %s [in module %s]"),
11364 sect_offset_str (child_die->sect_off),
11365 objfile_name (objfile));
11366 continue;
11367 }
11368
11369 excludes.push_back (imported_name);
11370
11371 process_die (child_die, cu);
11372 }
11373
11374 add_using_directive (using_directives (cu),
11375 import_prefix,
11376 canonical_name,
11377 import_alias,
11378 imported_declaration,
11379 excludes,
11380 0,
11381 &objfile->objfile_obstack);
11382 }
11383
11384 /* ICC<14 does not output the required DW_AT_declaration on incomplete
11385 types, but gives them a size of zero. Starting with version 14,
11386 ICC is compatible with GCC. */
11387
11388 static bool
11389 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
11390 {
11391 if (!cu->checked_producer)
11392 check_producer (cu);
11393
11394 return cu->producer_is_icc_lt_14;
11395 }
11396
11397 /* ICC generates a DW_AT_type for C void functions. This was observed on
11398 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
11399 which says that void functions should not have a DW_AT_type. */
11400
11401 static bool
11402 producer_is_icc (struct dwarf2_cu *cu)
11403 {
11404 if (!cu->checked_producer)
11405 check_producer (cu);
11406
11407 return cu->producer_is_icc;
11408 }
11409
11410 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11411 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11412 this, it was first present in GCC release 4.3.0. */
11413
11414 static bool
11415 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11416 {
11417 if (!cu->checked_producer)
11418 check_producer (cu);
11419
11420 return cu->producer_is_gcc_lt_4_3;
11421 }
11422
11423 static file_and_directory
11424 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
11425 {
11426 file_and_directory res;
11427
11428 /* Find the filename. Do not use dwarf2_name here, since the filename
11429 is not a source language identifier. */
11430 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11431 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
11432
11433 if (res.comp_dir == NULL
11434 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11435 && IS_ABSOLUTE_PATH (res.name))
11436 {
11437 res.comp_dir_storage = ldirname (res.name);
11438 if (!res.comp_dir_storage.empty ())
11439 res.comp_dir = res.comp_dir_storage.c_str ();
11440 }
11441 if (res.comp_dir != NULL)
11442 {
11443 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11444 directory, get rid of it. */
11445 const char *cp = strchr (res.comp_dir, ':');
11446
11447 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11448 res.comp_dir = cp + 1;
11449 }
11450
11451 if (res.name == NULL)
11452 res.name = "<unknown>";
11453
11454 return res;
11455 }
11456
11457 /* Handle DW_AT_stmt_list for a compilation unit.
11458 DIE is the DW_TAG_compile_unit die for CU.
11459 COMP_DIR is the compilation directory. LOWPC is passed to
11460 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
11461
11462 static void
11463 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
11464 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
11465 {
11466 struct dwarf2_per_objfile *dwarf2_per_objfile
11467 = cu->per_cu->dwarf2_per_objfile;
11468 struct objfile *objfile = dwarf2_per_objfile->objfile;
11469 struct attribute *attr;
11470 struct line_header line_header_local;
11471 hashval_t line_header_local_hash;
11472 void **slot;
11473 int decode_mapping;
11474
11475 gdb_assert (! cu->per_cu->is_debug_types);
11476
11477 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
11478 if (attr == NULL)
11479 return;
11480
11481 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11482
11483 /* The line header hash table is only created if needed (it exists to
11484 prevent redundant reading of the line table for partial_units).
11485 If we're given a partial_unit, we'll need it. If we're given a
11486 compile_unit, then use the line header hash table if it's already
11487 created, but don't create one just yet. */
11488
11489 if (dwarf2_per_objfile->line_header_hash == NULL
11490 && die->tag == DW_TAG_partial_unit)
11491 {
11492 dwarf2_per_objfile->line_header_hash
11493 = htab_create_alloc_ex (127, line_header_hash_voidp,
11494 line_header_eq_voidp,
11495 free_line_header_voidp,
11496 &objfile->objfile_obstack,
11497 hashtab_obstack_allocate,
11498 dummy_obstack_deallocate);
11499 }
11500
11501 line_header_local.sect_off = line_offset;
11502 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11503 line_header_local_hash = line_header_hash (&line_header_local);
11504 if (dwarf2_per_objfile->line_header_hash != NULL)
11505 {
11506 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11507 &line_header_local,
11508 line_header_local_hash, NO_INSERT);
11509
11510 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11511 is not present in *SLOT (since if there is something in *SLOT then
11512 it will be for a partial_unit). */
11513 if (die->tag == DW_TAG_partial_unit && slot != NULL)
11514 {
11515 gdb_assert (*slot != NULL);
11516 cu->line_header = (struct line_header *) *slot;
11517 return;
11518 }
11519 }
11520
11521 /* dwarf_decode_line_header does not yet provide sufficient information.
11522 We always have to call also dwarf_decode_lines for it. */
11523 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11524 if (lh == NULL)
11525 return;
11526
11527 cu->line_header = lh.release ();
11528 cu->line_header_die_owner = die;
11529
11530 if (dwarf2_per_objfile->line_header_hash == NULL)
11531 slot = NULL;
11532 else
11533 {
11534 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11535 &line_header_local,
11536 line_header_local_hash, INSERT);
11537 gdb_assert (slot != NULL);
11538 }
11539 if (slot != NULL && *slot == NULL)
11540 {
11541 /* This newly decoded line number information unit will be owned
11542 by line_header_hash hash table. */
11543 *slot = cu->line_header;
11544 cu->line_header_die_owner = NULL;
11545 }
11546 else
11547 {
11548 /* We cannot free any current entry in (*slot) as that struct line_header
11549 may be already used by multiple CUs. Create only temporary decoded
11550 line_header for this CU - it may happen at most once for each line
11551 number information unit. And if we're not using line_header_hash
11552 then this is what we want as well. */
11553 gdb_assert (die->tag != DW_TAG_partial_unit);
11554 }
11555 decode_mapping = (die->tag != DW_TAG_partial_unit);
11556 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11557 decode_mapping);
11558
11559 }
11560
11561 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
11562
11563 static void
11564 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
11565 {
11566 struct dwarf2_per_objfile *dwarf2_per_objfile
11567 = cu->per_cu->dwarf2_per_objfile;
11568 struct objfile *objfile = dwarf2_per_objfile->objfile;
11569 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11570 CORE_ADDR lowpc = ((CORE_ADDR) -1);
11571 CORE_ADDR highpc = ((CORE_ADDR) 0);
11572 struct attribute *attr;
11573 struct die_info *child_die;
11574 CORE_ADDR baseaddr;
11575
11576 prepare_one_comp_unit (cu, die, cu->language);
11577 baseaddr = objfile->text_section_offset ();
11578
11579 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
11580
11581 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11582 from finish_block. */
11583 if (lowpc == ((CORE_ADDR) -1))
11584 lowpc = highpc;
11585 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11586
11587 file_and_directory fnd = find_file_and_directory (die, cu);
11588
11589 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11590 standardised yet. As a workaround for the language detection we fall
11591 back to the DW_AT_producer string. */
11592 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11593 cu->language = language_opencl;
11594
11595 /* Similar hack for Go. */
11596 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11597 set_cu_language (DW_LANG_Go, cu);
11598
11599 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
11600
11601 /* Decode line number information if present. We do this before
11602 processing child DIEs, so that the line header table is available
11603 for DW_AT_decl_file. */
11604 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
11605
11606 /* Process all dies in compilation unit. */
11607 if (die->child != NULL)
11608 {
11609 child_die = die->child;
11610 while (child_die && child_die->tag)
11611 {
11612 process_die (child_die, cu);
11613 child_die = sibling_die (child_die);
11614 }
11615 }
11616
11617 /* Decode macro information, if present. Dwarf 2 macro information
11618 refers to information in the line number info statement program
11619 header, so we can only read it if we've read the header
11620 successfully. */
11621 attr = dwarf2_attr (die, DW_AT_macros, cu);
11622 if (attr == NULL)
11623 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
11624 if (attr && cu->line_header)
11625 {
11626 if (dwarf2_attr (die, DW_AT_macro_info, cu))
11627 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
11628
11629 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
11630 }
11631 else
11632 {
11633 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11634 if (attr && cu->line_header)
11635 {
11636 unsigned int macro_offset = DW_UNSND (attr);
11637
11638 dwarf_decode_macros (cu, macro_offset, 0);
11639 }
11640 }
11641 }
11642
11643 void
11644 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
11645 {
11646 struct type_unit_group *tu_group;
11647 int first_time;
11648 struct attribute *attr;
11649 unsigned int i;
11650 struct signatured_type *sig_type;
11651
11652 gdb_assert (per_cu->is_debug_types);
11653 sig_type = (struct signatured_type *) per_cu;
11654
11655 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
11656
11657 /* If we're using .gdb_index (includes -readnow) then
11658 per_cu->type_unit_group may not have been set up yet. */
11659 if (sig_type->type_unit_group == NULL)
11660 sig_type->type_unit_group = get_type_unit_group (this, attr);
11661 tu_group = sig_type->type_unit_group;
11662
11663 /* If we've already processed this stmt_list there's no real need to
11664 do it again, we could fake it and just recreate the part we need
11665 (file name,index -> symtab mapping). If data shows this optimization
11666 is useful we can do it then. */
11667 first_time = tu_group->compunit_symtab == NULL;
11668
11669 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11670 debug info. */
11671 line_header_up lh;
11672 if (attr != NULL)
11673 {
11674 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11675 lh = dwarf_decode_line_header (line_offset, this);
11676 }
11677 if (lh == NULL)
11678 {
11679 if (first_time)
11680 start_symtab ("", NULL, 0);
11681 else
11682 {
11683 gdb_assert (tu_group->symtabs == NULL);
11684 gdb_assert (m_builder == nullptr);
11685 struct compunit_symtab *cust = tu_group->compunit_symtab;
11686 m_builder.reset (new struct buildsym_compunit
11687 (COMPUNIT_OBJFILE (cust), "",
11688 COMPUNIT_DIRNAME (cust),
11689 compunit_language (cust),
11690 0, cust));
11691 }
11692 return;
11693 }
11694
11695 line_header = lh.release ();
11696 line_header_die_owner = die;
11697
11698 if (first_time)
11699 {
11700 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
11701
11702 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11703 still initializing it, and our caller (a few levels up)
11704 process_full_type_unit still needs to know if this is the first
11705 time. */
11706
11707 tu_group->num_symtabs = line_header->file_names_size ();
11708 tu_group->symtabs = XNEWVEC (struct symtab *,
11709 line_header->file_names_size ());
11710
11711 auto &file_names = line_header->file_names ();
11712 for (i = 0; i < file_names.size (); ++i)
11713 {
11714 file_entry &fe = file_names[i];
11715 dwarf2_start_subfile (this, fe.name,
11716 fe.include_dir (line_header));
11717 buildsym_compunit *b = get_builder ();
11718 if (b->get_current_subfile ()->symtab == NULL)
11719 {
11720 /* NOTE: start_subfile will recognize when it's been
11721 passed a file it has already seen. So we can't
11722 assume there's a simple mapping from
11723 cu->line_header->file_names to subfiles, plus
11724 cu->line_header->file_names may contain dups. */
11725 b->get_current_subfile ()->symtab
11726 = allocate_symtab (cust, b->get_current_subfile ()->name);
11727 }
11728
11729 fe.symtab = b->get_current_subfile ()->symtab;
11730 tu_group->symtabs[i] = fe.symtab;
11731 }
11732 }
11733 else
11734 {
11735 gdb_assert (m_builder == nullptr);
11736 struct compunit_symtab *cust = tu_group->compunit_symtab;
11737 m_builder.reset (new struct buildsym_compunit
11738 (COMPUNIT_OBJFILE (cust), "",
11739 COMPUNIT_DIRNAME (cust),
11740 compunit_language (cust),
11741 0, cust));
11742
11743 auto &file_names = line_header->file_names ();
11744 for (i = 0; i < file_names.size (); ++i)
11745 {
11746 file_entry &fe = file_names[i];
11747 fe.symtab = tu_group->symtabs[i];
11748 }
11749 }
11750
11751 /* The main symtab is allocated last. Type units don't have DW_AT_name
11752 so they don't have a "real" (so to speak) symtab anyway.
11753 There is later code that will assign the main symtab to all symbols
11754 that don't have one. We need to handle the case of a symbol with a
11755 missing symtab (DW_AT_decl_file) anyway. */
11756 }
11757
11758 /* Process DW_TAG_type_unit.
11759 For TUs we want to skip the first top level sibling if it's not the
11760 actual type being defined by this TU. In this case the first top
11761 level sibling is there to provide context only. */
11762
11763 static void
11764 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11765 {
11766 struct die_info *child_die;
11767
11768 prepare_one_comp_unit (cu, die, language_minimal);
11769
11770 /* Initialize (or reinitialize) the machinery for building symtabs.
11771 We do this before processing child DIEs, so that the line header table
11772 is available for DW_AT_decl_file. */
11773 cu->setup_type_unit_groups (die);
11774
11775 if (die->child != NULL)
11776 {
11777 child_die = die->child;
11778 while (child_die && child_die->tag)
11779 {
11780 process_die (child_die, cu);
11781 child_die = sibling_die (child_die);
11782 }
11783 }
11784 }
11785 \f
11786 /* DWO/DWP files.
11787
11788 http://gcc.gnu.org/wiki/DebugFission
11789 http://gcc.gnu.org/wiki/DebugFissionDWP
11790
11791 To simplify handling of both DWO files ("object" files with the DWARF info)
11792 and DWP files (a file with the DWOs packaged up into one file), we treat
11793 DWP files as having a collection of virtual DWO files. */
11794
11795 static hashval_t
11796 hash_dwo_file (const void *item)
11797 {
11798 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
11799 hashval_t hash;
11800
11801 hash = htab_hash_string (dwo_file->dwo_name);
11802 if (dwo_file->comp_dir != NULL)
11803 hash += htab_hash_string (dwo_file->comp_dir);
11804 return hash;
11805 }
11806
11807 static int
11808 eq_dwo_file (const void *item_lhs, const void *item_rhs)
11809 {
11810 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11811 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
11812
11813 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11814 return 0;
11815 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11816 return lhs->comp_dir == rhs->comp_dir;
11817 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
11818 }
11819
11820 /* Allocate a hash table for DWO files. */
11821
11822 static htab_up
11823 allocate_dwo_file_hash_table (struct objfile *objfile)
11824 {
11825 auto delete_dwo_file = [] (void *item)
11826 {
11827 struct dwo_file *dwo_file = (struct dwo_file *) item;
11828
11829 delete dwo_file;
11830 };
11831
11832 return htab_up (htab_create_alloc_ex (41,
11833 hash_dwo_file,
11834 eq_dwo_file,
11835 delete_dwo_file,
11836 &objfile->objfile_obstack,
11837 hashtab_obstack_allocate,
11838 dummy_obstack_deallocate));
11839 }
11840
11841 /* Lookup DWO file DWO_NAME. */
11842
11843 static void **
11844 lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11845 const char *dwo_name,
11846 const char *comp_dir)
11847 {
11848 struct dwo_file find_entry;
11849 void **slot;
11850
11851 if (dwarf2_per_objfile->dwo_files == NULL)
11852 dwarf2_per_objfile->dwo_files
11853 = allocate_dwo_file_hash_table (dwarf2_per_objfile->objfile);
11854
11855 find_entry.dwo_name = dwo_name;
11856 find_entry.comp_dir = comp_dir;
11857 slot = htab_find_slot (dwarf2_per_objfile->dwo_files.get (), &find_entry,
11858 INSERT);
11859
11860 return slot;
11861 }
11862
11863 static hashval_t
11864 hash_dwo_unit (const void *item)
11865 {
11866 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11867
11868 /* This drops the top 32 bits of the id, but is ok for a hash. */
11869 return dwo_unit->signature;
11870 }
11871
11872 static int
11873 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11874 {
11875 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11876 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11877
11878 /* The signature is assumed to be unique within the DWO file.
11879 So while object file CU dwo_id's always have the value zero,
11880 that's OK, assuming each object file DWO file has only one CU,
11881 and that's the rule for now. */
11882 return lhs->signature == rhs->signature;
11883 }
11884
11885 /* Allocate a hash table for DWO CUs,TUs.
11886 There is one of these tables for each of CUs,TUs for each DWO file. */
11887
11888 static htab_t
11889 allocate_dwo_unit_table (struct objfile *objfile)
11890 {
11891 /* Start out with a pretty small number.
11892 Generally DWO files contain only one CU and maybe some TUs. */
11893 return htab_create_alloc_ex (3,
11894 hash_dwo_unit,
11895 eq_dwo_unit,
11896 NULL,
11897 &objfile->objfile_obstack,
11898 hashtab_obstack_allocate,
11899 dummy_obstack_deallocate);
11900 }
11901
11902 /* die_reader_func for create_dwo_cu. */
11903
11904 static void
11905 create_dwo_cu_reader (const struct die_reader_specs *reader,
11906 const gdb_byte *info_ptr,
11907 struct die_info *comp_unit_die,
11908 int has_children,
11909 struct dwo_file *dwo_file,
11910 struct dwo_unit *dwo_unit)
11911 {
11912 struct dwarf2_cu *cu = reader->cu;
11913 sect_offset sect_off = cu->per_cu->sect_off;
11914 struct dwarf2_section_info *section = cu->per_cu->section;
11915
11916 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11917 if (!signature.has_value ())
11918 {
11919 complaint (_("Dwarf Error: debug entry at offset %s is missing"
11920 " its dwo_id [in module %s]"),
11921 sect_offset_str (sect_off), dwo_file->dwo_name);
11922 return;
11923 }
11924
11925 dwo_unit->dwo_file = dwo_file;
11926 dwo_unit->signature = *signature;
11927 dwo_unit->section = section;
11928 dwo_unit->sect_off = sect_off;
11929 dwo_unit->length = cu->per_cu->length;
11930
11931 if (dwarf_read_debug)
11932 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11933 sect_offset_str (sect_off),
11934 hex_string (dwo_unit->signature));
11935 }
11936
11937 /* Create the dwo_units for the CUs in a DWO_FILE.
11938 Note: This function processes DWO files only, not DWP files. */
11939
11940 static void
11941 create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11942 dwarf2_cu *cu, struct dwo_file &dwo_file,
11943 dwarf2_section_info &section, htab_t &cus_htab)
11944 {
11945 struct objfile *objfile = dwarf2_per_objfile->objfile;
11946 const gdb_byte *info_ptr, *end_ptr;
11947
11948 dwarf2_read_section (objfile, &section);
11949 info_ptr = section.buffer;
11950
11951 if (info_ptr == NULL)
11952 return;
11953
11954 if (dwarf_read_debug)
11955 {
11956 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
11957 get_section_name (&section),
11958 get_section_file_name (&section));
11959 }
11960
11961 end_ptr = info_ptr + section.size;
11962 while (info_ptr < end_ptr)
11963 {
11964 struct dwarf2_per_cu_data per_cu;
11965 struct dwo_unit read_unit {};
11966 struct dwo_unit *dwo_unit;
11967 void **slot;
11968 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11969
11970 memset (&per_cu, 0, sizeof (per_cu));
11971 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
11972 per_cu.is_debug_types = 0;
11973 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11974 per_cu.section = &section;
11975
11976 cutu_reader reader (&per_cu, cu, &dwo_file);
11977 if (!reader.dummy_p)
11978 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
11979 reader.has_children, &dwo_file, &read_unit);
11980 info_ptr += per_cu.length;
11981
11982 // If the unit could not be parsed, skip it.
11983 if (read_unit.dwo_file == NULL)
11984 continue;
11985
11986 if (cus_htab == NULL)
11987 cus_htab = allocate_dwo_unit_table (objfile);
11988
11989 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11990 *dwo_unit = read_unit;
11991 slot = htab_find_slot (cus_htab, dwo_unit, INSERT);
11992 gdb_assert (slot != NULL);
11993 if (*slot != NULL)
11994 {
11995 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11996 sect_offset dup_sect_off = dup_cu->sect_off;
11997
11998 complaint (_("debug cu entry at offset %s is duplicate to"
11999 " the entry at offset %s, signature %s"),
12000 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
12001 hex_string (dwo_unit->signature));
12002 }
12003 *slot = (void *)dwo_unit;
12004 }
12005 }
12006
12007 /* DWP file .debug_{cu,tu}_index section format:
12008 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
12009
12010 DWP Version 1:
12011
12012 Both index sections have the same format, and serve to map a 64-bit
12013 signature to a set of section numbers. Each section begins with a header,
12014 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
12015 indexes, and a pool of 32-bit section numbers. The index sections will be
12016 aligned at 8-byte boundaries in the file.
12017
12018 The index section header consists of:
12019
12020 V, 32 bit version number
12021 -, 32 bits unused
12022 N, 32 bit number of compilation units or type units in the index
12023 M, 32 bit number of slots in the hash table
12024
12025 Numbers are recorded using the byte order of the application binary.
12026
12027 The hash table begins at offset 16 in the section, and consists of an array
12028 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
12029 order of the application binary). Unused slots in the hash table are 0.
12030 (We rely on the extreme unlikeliness of a signature being exactly 0.)
12031
12032 The parallel table begins immediately after the hash table
12033 (at offset 16 + 8 * M from the beginning of the section), and consists of an
12034 array of 32-bit indexes (using the byte order of the application binary),
12035 corresponding 1-1 with slots in the hash table. Each entry in the parallel
12036 table contains a 32-bit index into the pool of section numbers. For unused
12037 hash table slots, the corresponding entry in the parallel table will be 0.
12038
12039 The pool of section numbers begins immediately following the hash table
12040 (at offset 16 + 12 * M from the beginning of the section). The pool of
12041 section numbers consists of an array of 32-bit words (using the byte order
12042 of the application binary). Each item in the array is indexed starting
12043 from 0. The hash table entry provides the index of the first section
12044 number in the set. Additional section numbers in the set follow, and the
12045 set is terminated by a 0 entry (section number 0 is not used in ELF).
12046
12047 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
12048 section must be the first entry in the set, and the .debug_abbrev.dwo must
12049 be the second entry. Other members of the set may follow in any order.
12050
12051 ---
12052
12053 DWP Version 2:
12054
12055 DWP Version 2 combines all the .debug_info, etc. sections into one,
12056 and the entries in the index tables are now offsets into these sections.
12057 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
12058 section.
12059
12060 Index Section Contents:
12061 Header
12062 Hash Table of Signatures dwp_hash_table.hash_table
12063 Parallel Table of Indices dwp_hash_table.unit_table
12064 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
12065 Table of Section Sizes dwp_hash_table.v2.sizes
12066
12067 The index section header consists of:
12068
12069 V, 32 bit version number
12070 L, 32 bit number of columns in the table of section offsets
12071 N, 32 bit number of compilation units or type units in the index
12072 M, 32 bit number of slots in the hash table
12073
12074 Numbers are recorded using the byte order of the application binary.
12075
12076 The hash table has the same format as version 1.
12077 The parallel table of indices has the same format as version 1,
12078 except that the entries are origin-1 indices into the table of sections
12079 offsets and the table of section sizes.
12080
12081 The table of offsets begins immediately following the parallel table
12082 (at offset 16 + 12 * M from the beginning of the section). The table is
12083 a two-dimensional array of 32-bit words (using the byte order of the
12084 application binary), with L columns and N+1 rows, in row-major order.
12085 Each row in the array is indexed starting from 0. The first row provides
12086 a key to the remaining rows: each column in this row provides an identifier
12087 for a debug section, and the offsets in the same column of subsequent rows
12088 refer to that section. The section identifiers are:
12089
12090 DW_SECT_INFO 1 .debug_info.dwo
12091 DW_SECT_TYPES 2 .debug_types.dwo
12092 DW_SECT_ABBREV 3 .debug_abbrev.dwo
12093 DW_SECT_LINE 4 .debug_line.dwo
12094 DW_SECT_LOC 5 .debug_loc.dwo
12095 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
12096 DW_SECT_MACINFO 7 .debug_macinfo.dwo
12097 DW_SECT_MACRO 8 .debug_macro.dwo
12098
12099 The offsets provided by the CU and TU index sections are the base offsets
12100 for the contributions made by each CU or TU to the corresponding section
12101 in the package file. Each CU and TU header contains an abbrev_offset
12102 field, used to find the abbreviations table for that CU or TU within the
12103 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
12104 be interpreted as relative to the base offset given in the index section.
12105 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
12106 should be interpreted as relative to the base offset for .debug_line.dwo,
12107 and offsets into other debug sections obtained from DWARF attributes should
12108 also be interpreted as relative to the corresponding base offset.
12109
12110 The table of sizes begins immediately following the table of offsets.
12111 Like the table of offsets, it is a two-dimensional array of 32-bit words,
12112 with L columns and N rows, in row-major order. Each row in the array is
12113 indexed starting from 1 (row 0 is shared by the two tables).
12114
12115 ---
12116
12117 Hash table lookup is handled the same in version 1 and 2:
12118
12119 We assume that N and M will not exceed 2^32 - 1.
12120 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
12121
12122 Given a 64-bit compilation unit signature or a type signature S, an entry
12123 in the hash table is located as follows:
12124
12125 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
12126 the low-order k bits all set to 1.
12127
12128 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
12129
12130 3) If the hash table entry at index H matches the signature, use that
12131 entry. If the hash table entry at index H is unused (all zeroes),
12132 terminate the search: the signature is not present in the table.
12133
12134 4) Let H = (H + H') modulo M. Repeat at Step 3.
12135
12136 Because M > N and H' and M are relatively prime, the search is guaranteed
12137 to stop at an unused slot or find the match. */
12138
12139 /* Create a hash table to map DWO IDs to their CU/TU entry in
12140 .debug_{info,types}.dwo in DWP_FILE.
12141 Returns NULL if there isn't one.
12142 Note: This function processes DWP files only, not DWO files. */
12143
12144 static struct dwp_hash_table *
12145 create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
12146 struct dwp_file *dwp_file, int is_debug_types)
12147 {
12148 struct objfile *objfile = dwarf2_per_objfile->objfile;
12149 bfd *dbfd = dwp_file->dbfd.get ();
12150 const gdb_byte *index_ptr, *index_end;
12151 struct dwarf2_section_info *index;
12152 uint32_t version, nr_columns, nr_units, nr_slots;
12153 struct dwp_hash_table *htab;
12154
12155 if (is_debug_types)
12156 index = &dwp_file->sections.tu_index;
12157 else
12158 index = &dwp_file->sections.cu_index;
12159
12160 if (dwarf2_section_empty_p (index))
12161 return NULL;
12162 dwarf2_read_section (objfile, index);
12163
12164 index_ptr = index->buffer;
12165 index_end = index_ptr + index->size;
12166
12167 version = read_4_bytes (dbfd, index_ptr);
12168 index_ptr += 4;
12169 if (version == 2)
12170 nr_columns = read_4_bytes (dbfd, index_ptr);
12171 else
12172 nr_columns = 0;
12173 index_ptr += 4;
12174 nr_units = read_4_bytes (dbfd, index_ptr);
12175 index_ptr += 4;
12176 nr_slots = read_4_bytes (dbfd, index_ptr);
12177 index_ptr += 4;
12178
12179 if (version != 1 && version != 2)
12180 {
12181 error (_("Dwarf Error: unsupported DWP file version (%s)"
12182 " [in module %s]"),
12183 pulongest (version), dwp_file->name);
12184 }
12185 if (nr_slots != (nr_slots & -nr_slots))
12186 {
12187 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
12188 " is not power of 2 [in module %s]"),
12189 pulongest (nr_slots), dwp_file->name);
12190 }
12191
12192 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
12193 htab->version = version;
12194 htab->nr_columns = nr_columns;
12195 htab->nr_units = nr_units;
12196 htab->nr_slots = nr_slots;
12197 htab->hash_table = index_ptr;
12198 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
12199
12200 /* Exit early if the table is empty. */
12201 if (nr_slots == 0 || nr_units == 0
12202 || (version == 2 && nr_columns == 0))
12203 {
12204 /* All must be zero. */
12205 if (nr_slots != 0 || nr_units != 0
12206 || (version == 2 && nr_columns != 0))
12207 {
12208 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
12209 " all zero [in modules %s]"),
12210 dwp_file->name);
12211 }
12212 return htab;
12213 }
12214
12215 if (version == 1)
12216 {
12217 htab->section_pool.v1.indices =
12218 htab->unit_table + sizeof (uint32_t) * nr_slots;
12219 /* It's harder to decide whether the section is too small in v1.
12220 V1 is deprecated anyway so we punt. */
12221 }
12222 else
12223 {
12224 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
12225 int *ids = htab->section_pool.v2.section_ids;
12226 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
12227 /* Reverse map for error checking. */
12228 int ids_seen[DW_SECT_MAX + 1];
12229 int i;
12230
12231 if (nr_columns < 2)
12232 {
12233 error (_("Dwarf Error: bad DWP hash table, too few columns"
12234 " in section table [in module %s]"),
12235 dwp_file->name);
12236 }
12237 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
12238 {
12239 error (_("Dwarf Error: bad DWP hash table, too many columns"
12240 " in section table [in module %s]"),
12241 dwp_file->name);
12242 }
12243 memset (ids, 255, sizeof_ids);
12244 memset (ids_seen, 255, sizeof (ids_seen));
12245 for (i = 0; i < nr_columns; ++i)
12246 {
12247 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
12248
12249 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
12250 {
12251 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
12252 " in section table [in module %s]"),
12253 id, dwp_file->name);
12254 }
12255 if (ids_seen[id] != -1)
12256 {
12257 error (_("Dwarf Error: bad DWP hash table, duplicate section"
12258 " id %d in section table [in module %s]"),
12259 id, dwp_file->name);
12260 }
12261 ids_seen[id] = i;
12262 ids[i] = id;
12263 }
12264 /* Must have exactly one info or types section. */
12265 if (((ids_seen[DW_SECT_INFO] != -1)
12266 + (ids_seen[DW_SECT_TYPES] != -1))
12267 != 1)
12268 {
12269 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
12270 " DWO info/types section [in module %s]"),
12271 dwp_file->name);
12272 }
12273 /* Must have an abbrev section. */
12274 if (ids_seen[DW_SECT_ABBREV] == -1)
12275 {
12276 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
12277 " section [in module %s]"),
12278 dwp_file->name);
12279 }
12280 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
12281 htab->section_pool.v2.sizes =
12282 htab->section_pool.v2.offsets + (sizeof (uint32_t)
12283 * nr_units * nr_columns);
12284 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
12285 * nr_units * nr_columns))
12286 > index_end)
12287 {
12288 error (_("Dwarf Error: DWP index section is corrupt (too small)"
12289 " [in module %s]"),
12290 dwp_file->name);
12291 }
12292 }
12293
12294 return htab;
12295 }
12296
12297 /* Update SECTIONS with the data from SECTP.
12298
12299 This function is like the other "locate" section routines that are
12300 passed to bfd_map_over_sections, but in this context the sections to
12301 read comes from the DWP V1 hash table, not the full ELF section table.
12302
12303 The result is non-zero for success, or zero if an error was found. */
12304
12305 static int
12306 locate_v1_virtual_dwo_sections (asection *sectp,
12307 struct virtual_v1_dwo_sections *sections)
12308 {
12309 const struct dwop_section_names *names = &dwop_section_names;
12310
12311 if (section_is_p (sectp->name, &names->abbrev_dwo))
12312 {
12313 /* There can be only one. */
12314 if (sections->abbrev.s.section != NULL)
12315 return 0;
12316 sections->abbrev.s.section = sectp;
12317 sections->abbrev.size = bfd_section_size (sectp);
12318 }
12319 else if (section_is_p (sectp->name, &names->info_dwo)
12320 || section_is_p (sectp->name, &names->types_dwo))
12321 {
12322 /* There can be only one. */
12323 if (sections->info_or_types.s.section != NULL)
12324 return 0;
12325 sections->info_or_types.s.section = sectp;
12326 sections->info_or_types.size = bfd_section_size (sectp);
12327 }
12328 else if (section_is_p (sectp->name, &names->line_dwo))
12329 {
12330 /* There can be only one. */
12331 if (sections->line.s.section != NULL)
12332 return 0;
12333 sections->line.s.section = sectp;
12334 sections->line.size = bfd_section_size (sectp);
12335 }
12336 else if (section_is_p (sectp->name, &names->loc_dwo))
12337 {
12338 /* There can be only one. */
12339 if (sections->loc.s.section != NULL)
12340 return 0;
12341 sections->loc.s.section = sectp;
12342 sections->loc.size = bfd_section_size (sectp);
12343 }
12344 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12345 {
12346 /* There can be only one. */
12347 if (sections->macinfo.s.section != NULL)
12348 return 0;
12349 sections->macinfo.s.section = sectp;
12350 sections->macinfo.size = bfd_section_size (sectp);
12351 }
12352 else if (section_is_p (sectp->name, &names->macro_dwo))
12353 {
12354 /* There can be only one. */
12355 if (sections->macro.s.section != NULL)
12356 return 0;
12357 sections->macro.s.section = sectp;
12358 sections->macro.size = bfd_section_size (sectp);
12359 }
12360 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12361 {
12362 /* There can be only one. */
12363 if (sections->str_offsets.s.section != NULL)
12364 return 0;
12365 sections->str_offsets.s.section = sectp;
12366 sections->str_offsets.size = bfd_section_size (sectp);
12367 }
12368 else
12369 {
12370 /* No other kind of section is valid. */
12371 return 0;
12372 }
12373
12374 return 1;
12375 }
12376
12377 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12378 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12379 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12380 This is for DWP version 1 files. */
12381
12382 static struct dwo_unit *
12383 create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12384 struct dwp_file *dwp_file,
12385 uint32_t unit_index,
12386 const char *comp_dir,
12387 ULONGEST signature, int is_debug_types)
12388 {
12389 struct objfile *objfile = dwarf2_per_objfile->objfile;
12390 const struct dwp_hash_table *dwp_htab =
12391 is_debug_types ? dwp_file->tus : dwp_file->cus;
12392 bfd *dbfd = dwp_file->dbfd.get ();
12393 const char *kind = is_debug_types ? "TU" : "CU";
12394 struct dwo_file *dwo_file;
12395 struct dwo_unit *dwo_unit;
12396 struct virtual_v1_dwo_sections sections;
12397 void **dwo_file_slot;
12398 int i;
12399
12400 gdb_assert (dwp_file->version == 1);
12401
12402 if (dwarf_read_debug)
12403 {
12404 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
12405 kind,
12406 pulongest (unit_index), hex_string (signature),
12407 dwp_file->name);
12408 }
12409
12410 /* Fetch the sections of this DWO unit.
12411 Put a limit on the number of sections we look for so that bad data
12412 doesn't cause us to loop forever. */
12413
12414 #define MAX_NR_V1_DWO_SECTIONS \
12415 (1 /* .debug_info or .debug_types */ \
12416 + 1 /* .debug_abbrev */ \
12417 + 1 /* .debug_line */ \
12418 + 1 /* .debug_loc */ \
12419 + 1 /* .debug_str_offsets */ \
12420 + 1 /* .debug_macro or .debug_macinfo */ \
12421 + 1 /* trailing zero */)
12422
12423 memset (&sections, 0, sizeof (sections));
12424
12425 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
12426 {
12427 asection *sectp;
12428 uint32_t section_nr =
12429 read_4_bytes (dbfd,
12430 dwp_htab->section_pool.v1.indices
12431 + (unit_index + i) * sizeof (uint32_t));
12432
12433 if (section_nr == 0)
12434 break;
12435 if (section_nr >= dwp_file->num_sections)
12436 {
12437 error (_("Dwarf Error: bad DWP hash table, section number too large"
12438 " [in module %s]"),
12439 dwp_file->name);
12440 }
12441
12442 sectp = dwp_file->elf_sections[section_nr];
12443 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
12444 {
12445 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12446 " [in module %s]"),
12447 dwp_file->name);
12448 }
12449 }
12450
12451 if (i < 2
12452 || dwarf2_section_empty_p (&sections.info_or_types)
12453 || dwarf2_section_empty_p (&sections.abbrev))
12454 {
12455 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12456 " [in module %s]"),
12457 dwp_file->name);
12458 }
12459 if (i == MAX_NR_V1_DWO_SECTIONS)
12460 {
12461 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12462 " [in module %s]"),
12463 dwp_file->name);
12464 }
12465
12466 /* It's easier for the rest of the code if we fake a struct dwo_file and
12467 have dwo_unit "live" in that. At least for now.
12468
12469 The DWP file can be made up of a random collection of CUs and TUs.
12470 However, for each CU + set of TUs that came from the same original DWO
12471 file, we can combine them back into a virtual DWO file to save space
12472 (fewer struct dwo_file objects to allocate). Remember that for really
12473 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12474
12475 std::string virtual_dwo_name =
12476 string_printf ("virtual-dwo/%d-%d-%d-%d",
12477 get_section_id (&sections.abbrev),
12478 get_section_id (&sections.line),
12479 get_section_id (&sections.loc),
12480 get_section_id (&sections.str_offsets));
12481 /* Can we use an existing virtual DWO file? */
12482 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12483 virtual_dwo_name.c_str (),
12484 comp_dir);
12485 /* Create one if necessary. */
12486 if (*dwo_file_slot == NULL)
12487 {
12488 if (dwarf_read_debug)
12489 {
12490 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12491 virtual_dwo_name.c_str ());
12492 }
12493 dwo_file = new struct dwo_file;
12494 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
12495 virtual_dwo_name);
12496 dwo_file->comp_dir = comp_dir;
12497 dwo_file->sections.abbrev = sections.abbrev;
12498 dwo_file->sections.line = sections.line;
12499 dwo_file->sections.loc = sections.loc;
12500 dwo_file->sections.macinfo = sections.macinfo;
12501 dwo_file->sections.macro = sections.macro;
12502 dwo_file->sections.str_offsets = sections.str_offsets;
12503 /* The "str" section is global to the entire DWP file. */
12504 dwo_file->sections.str = dwp_file->sections.str;
12505 /* The info or types section is assigned below to dwo_unit,
12506 there's no need to record it in dwo_file.
12507 Also, we can't simply record type sections in dwo_file because
12508 we record a pointer into the vector in dwo_unit. As we collect more
12509 types we'll grow the vector and eventually have to reallocate space
12510 for it, invalidating all copies of pointers into the previous
12511 contents. */
12512 *dwo_file_slot = dwo_file;
12513 }
12514 else
12515 {
12516 if (dwarf_read_debug)
12517 {
12518 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12519 virtual_dwo_name.c_str ());
12520 }
12521 dwo_file = (struct dwo_file *) *dwo_file_slot;
12522 }
12523
12524 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12525 dwo_unit->dwo_file = dwo_file;
12526 dwo_unit->signature = signature;
12527 dwo_unit->section =
12528 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
12529 *dwo_unit->section = sections.info_or_types;
12530 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12531
12532 return dwo_unit;
12533 }
12534
12535 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
12536 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
12537 piece within that section used by a TU/CU, return a virtual section
12538 of just that piece. */
12539
12540 static struct dwarf2_section_info
12541 create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
12542 struct dwarf2_section_info *section,
12543 bfd_size_type offset, bfd_size_type size)
12544 {
12545 struct dwarf2_section_info result;
12546 asection *sectp;
12547
12548 gdb_assert (section != NULL);
12549 gdb_assert (!section->is_virtual);
12550
12551 memset (&result, 0, sizeof (result));
12552 result.s.containing_section = section;
12553 result.is_virtual = true;
12554
12555 if (size == 0)
12556 return result;
12557
12558 sectp = get_section_bfd_section (section);
12559
12560 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12561 bounds of the real section. This is a pretty-rare event, so just
12562 flag an error (easier) instead of a warning and trying to cope. */
12563 if (sectp == NULL
12564 || offset + size > bfd_section_size (sectp))
12565 {
12566 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
12567 " in section %s [in module %s]"),
12568 sectp ? bfd_section_name (sectp) : "<unknown>",
12569 objfile_name (dwarf2_per_objfile->objfile));
12570 }
12571
12572 result.virtual_offset = offset;
12573 result.size = size;
12574 return result;
12575 }
12576
12577 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12578 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12579 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12580 This is for DWP version 2 files. */
12581
12582 static struct dwo_unit *
12583 create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12584 struct dwp_file *dwp_file,
12585 uint32_t unit_index,
12586 const char *comp_dir,
12587 ULONGEST signature, int is_debug_types)
12588 {
12589 struct objfile *objfile = dwarf2_per_objfile->objfile;
12590 const struct dwp_hash_table *dwp_htab =
12591 is_debug_types ? dwp_file->tus : dwp_file->cus;
12592 bfd *dbfd = dwp_file->dbfd.get ();
12593 const char *kind = is_debug_types ? "TU" : "CU";
12594 struct dwo_file *dwo_file;
12595 struct dwo_unit *dwo_unit;
12596 struct virtual_v2_dwo_sections sections;
12597 void **dwo_file_slot;
12598 int i;
12599
12600 gdb_assert (dwp_file->version == 2);
12601
12602 if (dwarf_read_debug)
12603 {
12604 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12605 kind,
12606 pulongest (unit_index), hex_string (signature),
12607 dwp_file->name);
12608 }
12609
12610 /* Fetch the section offsets of this DWO unit. */
12611
12612 memset (&sections, 0, sizeof (sections));
12613
12614 for (i = 0; i < dwp_htab->nr_columns; ++i)
12615 {
12616 uint32_t offset = read_4_bytes (dbfd,
12617 dwp_htab->section_pool.v2.offsets
12618 + (((unit_index - 1) * dwp_htab->nr_columns
12619 + i)
12620 * sizeof (uint32_t)));
12621 uint32_t size = read_4_bytes (dbfd,
12622 dwp_htab->section_pool.v2.sizes
12623 + (((unit_index - 1) * dwp_htab->nr_columns
12624 + i)
12625 * sizeof (uint32_t)));
12626
12627 switch (dwp_htab->section_pool.v2.section_ids[i])
12628 {
12629 case DW_SECT_INFO:
12630 case DW_SECT_TYPES:
12631 sections.info_or_types_offset = offset;
12632 sections.info_or_types_size = size;
12633 break;
12634 case DW_SECT_ABBREV:
12635 sections.abbrev_offset = offset;
12636 sections.abbrev_size = size;
12637 break;
12638 case DW_SECT_LINE:
12639 sections.line_offset = offset;
12640 sections.line_size = size;
12641 break;
12642 case DW_SECT_LOC:
12643 sections.loc_offset = offset;
12644 sections.loc_size = size;
12645 break;
12646 case DW_SECT_STR_OFFSETS:
12647 sections.str_offsets_offset = offset;
12648 sections.str_offsets_size = size;
12649 break;
12650 case DW_SECT_MACINFO:
12651 sections.macinfo_offset = offset;
12652 sections.macinfo_size = size;
12653 break;
12654 case DW_SECT_MACRO:
12655 sections.macro_offset = offset;
12656 sections.macro_size = size;
12657 break;
12658 }
12659 }
12660
12661 /* It's easier for the rest of the code if we fake a struct dwo_file and
12662 have dwo_unit "live" in that. At least for now.
12663
12664 The DWP file can be made up of a random collection of CUs and TUs.
12665 However, for each CU + set of TUs that came from the same original DWO
12666 file, we can combine them back into a virtual DWO file to save space
12667 (fewer struct dwo_file objects to allocate). Remember that for really
12668 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12669
12670 std::string virtual_dwo_name =
12671 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12672 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12673 (long) (sections.line_size ? sections.line_offset : 0),
12674 (long) (sections.loc_size ? sections.loc_offset : 0),
12675 (long) (sections.str_offsets_size
12676 ? sections.str_offsets_offset : 0));
12677 /* Can we use an existing virtual DWO file? */
12678 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12679 virtual_dwo_name.c_str (),
12680 comp_dir);
12681 /* Create one if necessary. */
12682 if (*dwo_file_slot == NULL)
12683 {
12684 if (dwarf_read_debug)
12685 {
12686 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12687 virtual_dwo_name.c_str ());
12688 }
12689 dwo_file = new struct dwo_file;
12690 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
12691 virtual_dwo_name);
12692 dwo_file->comp_dir = comp_dir;
12693 dwo_file->sections.abbrev =
12694 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
12695 sections.abbrev_offset, sections.abbrev_size);
12696 dwo_file->sections.line =
12697 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
12698 sections.line_offset, sections.line_size);
12699 dwo_file->sections.loc =
12700 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
12701 sections.loc_offset, sections.loc_size);
12702 dwo_file->sections.macinfo =
12703 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
12704 sections.macinfo_offset, sections.macinfo_size);
12705 dwo_file->sections.macro =
12706 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
12707 sections.macro_offset, sections.macro_size);
12708 dwo_file->sections.str_offsets =
12709 create_dwp_v2_section (dwarf2_per_objfile,
12710 &dwp_file->sections.str_offsets,
12711 sections.str_offsets_offset,
12712 sections.str_offsets_size);
12713 /* The "str" section is global to the entire DWP file. */
12714 dwo_file->sections.str = dwp_file->sections.str;
12715 /* The info or types section is assigned below to dwo_unit,
12716 there's no need to record it in dwo_file.
12717 Also, we can't simply record type sections in dwo_file because
12718 we record a pointer into the vector in dwo_unit. As we collect more
12719 types we'll grow the vector and eventually have to reallocate space
12720 for it, invalidating all copies of pointers into the previous
12721 contents. */
12722 *dwo_file_slot = dwo_file;
12723 }
12724 else
12725 {
12726 if (dwarf_read_debug)
12727 {
12728 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12729 virtual_dwo_name.c_str ());
12730 }
12731 dwo_file = (struct dwo_file *) *dwo_file_slot;
12732 }
12733
12734 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12735 dwo_unit->dwo_file = dwo_file;
12736 dwo_unit->signature = signature;
12737 dwo_unit->section =
12738 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
12739 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12740 is_debug_types
12741 ? &dwp_file->sections.types
12742 : &dwp_file->sections.info,
12743 sections.info_or_types_offset,
12744 sections.info_or_types_size);
12745 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12746
12747 return dwo_unit;
12748 }
12749
12750 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12751 Returns NULL if the signature isn't found. */
12752
12753 static struct dwo_unit *
12754 lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12755 struct dwp_file *dwp_file, const char *comp_dir,
12756 ULONGEST signature, int is_debug_types)
12757 {
12758 const struct dwp_hash_table *dwp_htab =
12759 is_debug_types ? dwp_file->tus : dwp_file->cus;
12760 bfd *dbfd = dwp_file->dbfd.get ();
12761 uint32_t mask = dwp_htab->nr_slots - 1;
12762 uint32_t hash = signature & mask;
12763 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12764 unsigned int i;
12765 void **slot;
12766 struct dwo_unit find_dwo_cu;
12767
12768 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12769 find_dwo_cu.signature = signature;
12770 slot = htab_find_slot (is_debug_types
12771 ? dwp_file->loaded_tus
12772 : dwp_file->loaded_cus,
12773 &find_dwo_cu, INSERT);
12774
12775 if (*slot != NULL)
12776 return (struct dwo_unit *) *slot;
12777
12778 /* Use a for loop so that we don't loop forever on bad debug info. */
12779 for (i = 0; i < dwp_htab->nr_slots; ++i)
12780 {
12781 ULONGEST signature_in_table;
12782
12783 signature_in_table =
12784 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
12785 if (signature_in_table == signature)
12786 {
12787 uint32_t unit_index =
12788 read_4_bytes (dbfd,
12789 dwp_htab->unit_table + hash * sizeof (uint32_t));
12790
12791 if (dwp_file->version == 1)
12792 {
12793 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12794 dwp_file, unit_index,
12795 comp_dir, signature,
12796 is_debug_types);
12797 }
12798 else
12799 {
12800 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12801 dwp_file, unit_index,
12802 comp_dir, signature,
12803 is_debug_types);
12804 }
12805 return (struct dwo_unit *) *slot;
12806 }
12807 if (signature_in_table == 0)
12808 return NULL;
12809 hash = (hash + hash2) & mask;
12810 }
12811
12812 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12813 " [in module %s]"),
12814 dwp_file->name);
12815 }
12816
12817 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12818 Open the file specified by FILE_NAME and hand it off to BFD for
12819 preliminary analysis. Return a newly initialized bfd *, which
12820 includes a canonicalized copy of FILE_NAME.
12821 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12822 SEARCH_CWD is true if the current directory is to be searched.
12823 It will be searched before debug-file-directory.
12824 If successful, the file is added to the bfd include table of the
12825 objfile's bfd (see gdb_bfd_record_inclusion).
12826 If unable to find/open the file, return NULL.
12827 NOTE: This function is derived from symfile_bfd_open. */
12828
12829 static gdb_bfd_ref_ptr
12830 try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12831 const char *file_name, int is_dwp, int search_cwd)
12832 {
12833 int desc;
12834 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12835 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12836 to debug_file_directory. */
12837 const char *search_path;
12838 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12839
12840 gdb::unique_xmalloc_ptr<char> search_path_holder;
12841 if (search_cwd)
12842 {
12843 if (*debug_file_directory != '\0')
12844 {
12845 search_path_holder.reset (concat (".", dirname_separator_string,
12846 debug_file_directory,
12847 (char *) NULL));
12848 search_path = search_path_holder.get ();
12849 }
12850 else
12851 search_path = ".";
12852 }
12853 else
12854 search_path = debug_file_directory;
12855
12856 openp_flags flags = OPF_RETURN_REALPATH;
12857 if (is_dwp)
12858 flags |= OPF_SEARCH_IN_PATH;
12859
12860 gdb::unique_xmalloc_ptr<char> absolute_name;
12861 desc = openp (search_path, flags, file_name,
12862 O_RDONLY | O_BINARY, &absolute_name);
12863 if (desc < 0)
12864 return NULL;
12865
12866 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12867 gnutarget, desc));
12868 if (sym_bfd == NULL)
12869 return NULL;
12870 bfd_set_cacheable (sym_bfd.get (), 1);
12871
12872 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12873 return NULL;
12874
12875 /* Success. Record the bfd as having been included by the objfile's bfd.
12876 This is important because things like demangled_names_hash lives in the
12877 objfile's per_bfd space and may have references to things like symbol
12878 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12879 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
12880
12881 return sym_bfd;
12882 }
12883
12884 /* Try to open DWO file FILE_NAME.
12885 COMP_DIR is the DW_AT_comp_dir attribute.
12886 The result is the bfd handle of the file.
12887 If there is a problem finding or opening the file, return NULL.
12888 Upon success, the canonicalized path of the file is stored in the bfd,
12889 same as symfile_bfd_open. */
12890
12891 static gdb_bfd_ref_ptr
12892 open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12893 const char *file_name, const char *comp_dir)
12894 {
12895 if (IS_ABSOLUTE_PATH (file_name))
12896 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12897 0 /*is_dwp*/, 0 /*search_cwd*/);
12898
12899 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12900
12901 if (comp_dir != NULL)
12902 {
12903 gdb::unique_xmalloc_ptr<char> path_to_try
12904 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
12905
12906 /* NOTE: If comp_dir is a relative path, this will also try the
12907 search path, which seems useful. */
12908 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
12909 path_to_try.get (),
12910 0 /*is_dwp*/,
12911 1 /*search_cwd*/));
12912 if (abfd != NULL)
12913 return abfd;
12914 }
12915
12916 /* That didn't work, try debug-file-directory, which, despite its name,
12917 is a list of paths. */
12918
12919 if (*debug_file_directory == '\0')
12920 return NULL;
12921
12922 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12923 0 /*is_dwp*/, 1 /*search_cwd*/);
12924 }
12925
12926 /* This function is mapped across the sections and remembers the offset and
12927 size of each of the DWO debugging sections we are interested in. */
12928
12929 static void
12930 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12931 {
12932 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
12933 const struct dwop_section_names *names = &dwop_section_names;
12934
12935 if (section_is_p (sectp->name, &names->abbrev_dwo))
12936 {
12937 dwo_sections->abbrev.s.section = sectp;
12938 dwo_sections->abbrev.size = bfd_section_size (sectp);
12939 }
12940 else if (section_is_p (sectp->name, &names->info_dwo))
12941 {
12942 dwo_sections->info.s.section = sectp;
12943 dwo_sections->info.size = bfd_section_size (sectp);
12944 }
12945 else if (section_is_p (sectp->name, &names->line_dwo))
12946 {
12947 dwo_sections->line.s.section = sectp;
12948 dwo_sections->line.size = bfd_section_size (sectp);
12949 }
12950 else if (section_is_p (sectp->name, &names->loc_dwo))
12951 {
12952 dwo_sections->loc.s.section = sectp;
12953 dwo_sections->loc.size = bfd_section_size (sectp);
12954 }
12955 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12956 {
12957 dwo_sections->macinfo.s.section = sectp;
12958 dwo_sections->macinfo.size = bfd_section_size (sectp);
12959 }
12960 else if (section_is_p (sectp->name, &names->macro_dwo))
12961 {
12962 dwo_sections->macro.s.section = sectp;
12963 dwo_sections->macro.size = bfd_section_size (sectp);
12964 }
12965 else if (section_is_p (sectp->name, &names->str_dwo))
12966 {
12967 dwo_sections->str.s.section = sectp;
12968 dwo_sections->str.size = bfd_section_size (sectp);
12969 }
12970 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12971 {
12972 dwo_sections->str_offsets.s.section = sectp;
12973 dwo_sections->str_offsets.size = bfd_section_size (sectp);
12974 }
12975 else if (section_is_p (sectp->name, &names->types_dwo))
12976 {
12977 struct dwarf2_section_info type_section;
12978
12979 memset (&type_section, 0, sizeof (type_section));
12980 type_section.s.section = sectp;
12981 type_section.size = bfd_section_size (sectp);
12982 dwo_sections->types.push_back (type_section);
12983 }
12984 }
12985
12986 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12987 by PER_CU. This is for the non-DWP case.
12988 The result is NULL if DWO_NAME can't be found. */
12989
12990 static struct dwo_file *
12991 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
12992 const char *dwo_name, const char *comp_dir)
12993 {
12994 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
12995
12996 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
12997 if (dbfd == NULL)
12998 {
12999 if (dwarf_read_debug)
13000 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
13001 return NULL;
13002 }
13003
13004 dwo_file_up dwo_file (new struct dwo_file);
13005 dwo_file->dwo_name = dwo_name;
13006 dwo_file->comp_dir = comp_dir;
13007 dwo_file->dbfd = std::move (dbfd);
13008
13009 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
13010 &dwo_file->sections);
13011
13012 create_cus_hash_table (dwarf2_per_objfile, per_cu->cu, *dwo_file,
13013 dwo_file->sections.info, dwo_file->cus);
13014
13015 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
13016 dwo_file->sections.types, dwo_file->tus);
13017
13018 if (dwarf_read_debug)
13019 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
13020
13021 return dwo_file.release ();
13022 }
13023
13024 /* This function is mapped across the sections and remembers the offset and
13025 size of each of the DWP debugging sections common to version 1 and 2 that
13026 we are interested in. */
13027
13028 static void
13029 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
13030 void *dwp_file_ptr)
13031 {
13032 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
13033 const struct dwop_section_names *names = &dwop_section_names;
13034 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13035
13036 /* Record the ELF section number for later lookup: this is what the
13037 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13038 gdb_assert (elf_section_nr < dwp_file->num_sections);
13039 dwp_file->elf_sections[elf_section_nr] = sectp;
13040
13041 /* Look for specific sections that we need. */
13042 if (section_is_p (sectp->name, &names->str_dwo))
13043 {
13044 dwp_file->sections.str.s.section = sectp;
13045 dwp_file->sections.str.size = bfd_section_size (sectp);
13046 }
13047 else if (section_is_p (sectp->name, &names->cu_index))
13048 {
13049 dwp_file->sections.cu_index.s.section = sectp;
13050 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
13051 }
13052 else if (section_is_p (sectp->name, &names->tu_index))
13053 {
13054 dwp_file->sections.tu_index.s.section = sectp;
13055 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
13056 }
13057 }
13058
13059 /* This function is mapped across the sections and remembers the offset and
13060 size of each of the DWP version 2 debugging sections that we are interested
13061 in. This is split into a separate function because we don't know if we
13062 have version 1 or 2 until we parse the cu_index/tu_index sections. */
13063
13064 static void
13065 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
13066 {
13067 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
13068 const struct dwop_section_names *names = &dwop_section_names;
13069 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13070
13071 /* Record the ELF section number for later lookup: this is what the
13072 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13073 gdb_assert (elf_section_nr < dwp_file->num_sections);
13074 dwp_file->elf_sections[elf_section_nr] = sectp;
13075
13076 /* Look for specific sections that we need. */
13077 if (section_is_p (sectp->name, &names->abbrev_dwo))
13078 {
13079 dwp_file->sections.abbrev.s.section = sectp;
13080 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
13081 }
13082 else if (section_is_p (sectp->name, &names->info_dwo))
13083 {
13084 dwp_file->sections.info.s.section = sectp;
13085 dwp_file->sections.info.size = bfd_section_size (sectp);
13086 }
13087 else if (section_is_p (sectp->name, &names->line_dwo))
13088 {
13089 dwp_file->sections.line.s.section = sectp;
13090 dwp_file->sections.line.size = bfd_section_size (sectp);
13091 }
13092 else if (section_is_p (sectp->name, &names->loc_dwo))
13093 {
13094 dwp_file->sections.loc.s.section = sectp;
13095 dwp_file->sections.loc.size = bfd_section_size (sectp);
13096 }
13097 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13098 {
13099 dwp_file->sections.macinfo.s.section = sectp;
13100 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
13101 }
13102 else if (section_is_p (sectp->name, &names->macro_dwo))
13103 {
13104 dwp_file->sections.macro.s.section = sectp;
13105 dwp_file->sections.macro.size = bfd_section_size (sectp);
13106 }
13107 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13108 {
13109 dwp_file->sections.str_offsets.s.section = sectp;
13110 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
13111 }
13112 else if (section_is_p (sectp->name, &names->types_dwo))
13113 {
13114 dwp_file->sections.types.s.section = sectp;
13115 dwp_file->sections.types.size = bfd_section_size (sectp);
13116 }
13117 }
13118
13119 /* Hash function for dwp_file loaded CUs/TUs. */
13120
13121 static hashval_t
13122 hash_dwp_loaded_cutus (const void *item)
13123 {
13124 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
13125
13126 /* This drops the top 32 bits of the signature, but is ok for a hash. */
13127 return dwo_unit->signature;
13128 }
13129
13130 /* Equality function for dwp_file loaded CUs/TUs. */
13131
13132 static int
13133 eq_dwp_loaded_cutus (const void *a, const void *b)
13134 {
13135 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13136 const struct dwo_unit *dub = (const struct dwo_unit *) b;
13137
13138 return dua->signature == dub->signature;
13139 }
13140
13141 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
13142
13143 static htab_t
13144 allocate_dwp_loaded_cutus_table (struct objfile *objfile)
13145 {
13146 return htab_create_alloc_ex (3,
13147 hash_dwp_loaded_cutus,
13148 eq_dwp_loaded_cutus,
13149 NULL,
13150 &objfile->objfile_obstack,
13151 hashtab_obstack_allocate,
13152 dummy_obstack_deallocate);
13153 }
13154
13155 /* Try to open DWP file FILE_NAME.
13156 The result is the bfd handle of the file.
13157 If there is a problem finding or opening the file, return NULL.
13158 Upon success, the canonicalized path of the file is stored in the bfd,
13159 same as symfile_bfd_open. */
13160
13161 static gdb_bfd_ref_ptr
13162 open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
13163 const char *file_name)
13164 {
13165 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
13166 1 /*is_dwp*/,
13167 1 /*search_cwd*/));
13168 if (abfd != NULL)
13169 return abfd;
13170
13171 /* Work around upstream bug 15652.
13172 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13173 [Whether that's a "bug" is debatable, but it is getting in our way.]
13174 We have no real idea where the dwp file is, because gdb's realpath-ing
13175 of the executable's path may have discarded the needed info.
13176 [IWBN if the dwp file name was recorded in the executable, akin to
13177 .gnu_debuglink, but that doesn't exist yet.]
13178 Strip the directory from FILE_NAME and search again. */
13179 if (*debug_file_directory != '\0')
13180 {
13181 /* Don't implicitly search the current directory here.
13182 If the user wants to search "." to handle this case,
13183 it must be added to debug-file-directory. */
13184 return try_open_dwop_file (dwarf2_per_objfile,
13185 lbasename (file_name), 1 /*is_dwp*/,
13186 0 /*search_cwd*/);
13187 }
13188
13189 return NULL;
13190 }
13191
13192 /* Initialize the use of the DWP file for the current objfile.
13193 By convention the name of the DWP file is ${objfile}.dwp.
13194 The result is NULL if it can't be found. */
13195
13196 static std::unique_ptr<struct dwp_file>
13197 open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
13198 {
13199 struct objfile *objfile = dwarf2_per_objfile->objfile;
13200
13201 /* Try to find first .dwp for the binary file before any symbolic links
13202 resolving. */
13203
13204 /* If the objfile is a debug file, find the name of the real binary
13205 file and get the name of dwp file from there. */
13206 std::string dwp_name;
13207 if (objfile->separate_debug_objfile_backlink != NULL)
13208 {
13209 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13210 const char *backlink_basename = lbasename (backlink->original_name);
13211
13212 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
13213 }
13214 else
13215 dwp_name = objfile->original_name;
13216
13217 dwp_name += ".dwp";
13218
13219 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
13220 if (dbfd == NULL
13221 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13222 {
13223 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
13224 dwp_name = objfile_name (objfile);
13225 dwp_name += ".dwp";
13226 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
13227 }
13228
13229 if (dbfd == NULL)
13230 {
13231 if (dwarf_read_debug)
13232 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
13233 return std::unique_ptr<dwp_file> ();
13234 }
13235
13236 const char *name = bfd_get_filename (dbfd.get ());
13237 std::unique_ptr<struct dwp_file> dwp_file
13238 (new struct dwp_file (name, std::move (dbfd)));
13239
13240 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
13241 dwp_file->elf_sections =
13242 OBSTACK_CALLOC (&objfile->objfile_obstack,
13243 dwp_file->num_sections, asection *);
13244
13245 bfd_map_over_sections (dwp_file->dbfd.get (),
13246 dwarf2_locate_common_dwp_sections,
13247 dwp_file.get ());
13248
13249 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
13250 0);
13251
13252 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
13253 1);
13254
13255 /* The DWP file version is stored in the hash table. Oh well. */
13256 if (dwp_file->cus && dwp_file->tus
13257 && dwp_file->cus->version != dwp_file->tus->version)
13258 {
13259 /* Technically speaking, we should try to limp along, but this is
13260 pretty bizarre. We use pulongest here because that's the established
13261 portability solution (e.g, we cannot use %u for uint32_t). */
13262 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13263 " TU version %s [in DWP file %s]"),
13264 pulongest (dwp_file->cus->version),
13265 pulongest (dwp_file->tus->version), dwp_name.c_str ());
13266 }
13267
13268 if (dwp_file->cus)
13269 dwp_file->version = dwp_file->cus->version;
13270 else if (dwp_file->tus)
13271 dwp_file->version = dwp_file->tus->version;
13272 else
13273 dwp_file->version = 2;
13274
13275 if (dwp_file->version == 2)
13276 bfd_map_over_sections (dwp_file->dbfd.get (),
13277 dwarf2_locate_v2_dwp_sections,
13278 dwp_file.get ());
13279
13280 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
13281 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
13282
13283 if (dwarf_read_debug)
13284 {
13285 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
13286 fprintf_unfiltered (gdb_stdlog,
13287 " %s CUs, %s TUs\n",
13288 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13289 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
13290 }
13291
13292 return dwp_file;
13293 }
13294
13295 /* Wrapper around open_and_init_dwp_file, only open it once. */
13296
13297 static struct dwp_file *
13298 get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
13299 {
13300 if (! dwarf2_per_objfile->dwp_checked)
13301 {
13302 dwarf2_per_objfile->dwp_file
13303 = open_and_init_dwp_file (dwarf2_per_objfile);
13304 dwarf2_per_objfile->dwp_checked = 1;
13305 }
13306 return dwarf2_per_objfile->dwp_file.get ();
13307 }
13308
13309 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13310 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13311 or in the DWP file for the objfile, referenced by THIS_UNIT.
13312 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
13313 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13314
13315 This is called, for example, when wanting to read a variable with a
13316 complex location. Therefore we don't want to do file i/o for every call.
13317 Therefore we don't want to look for a DWO file on every call.
13318 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13319 then we check if we've already seen DWO_NAME, and only THEN do we check
13320 for a DWO file.
13321
13322 The result is a pointer to the dwo_unit object or NULL if we didn't find it
13323 (dwo_id mismatch or couldn't find the DWO/DWP file). */
13324
13325 static struct dwo_unit *
13326 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
13327 const char *dwo_name, const char *comp_dir,
13328 ULONGEST signature, int is_debug_types)
13329 {
13330 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
13331 struct objfile *objfile = dwarf2_per_objfile->objfile;
13332 const char *kind = is_debug_types ? "TU" : "CU";
13333 void **dwo_file_slot;
13334 struct dwo_file *dwo_file;
13335 struct dwp_file *dwp_file;
13336
13337 /* First see if there's a DWP file.
13338 If we have a DWP file but didn't find the DWO inside it, don't
13339 look for the original DWO file. It makes gdb behave differently
13340 depending on whether one is debugging in the build tree. */
13341
13342 dwp_file = get_dwp_file (dwarf2_per_objfile);
13343 if (dwp_file != NULL)
13344 {
13345 const struct dwp_hash_table *dwp_htab =
13346 is_debug_types ? dwp_file->tus : dwp_file->cus;
13347
13348 if (dwp_htab != NULL)
13349 {
13350 struct dwo_unit *dwo_cutu =
13351 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
13352 signature, is_debug_types);
13353
13354 if (dwo_cutu != NULL)
13355 {
13356 if (dwarf_read_debug)
13357 {
13358 fprintf_unfiltered (gdb_stdlog,
13359 "Virtual DWO %s %s found: @%s\n",
13360 kind, hex_string (signature),
13361 host_address_to_string (dwo_cutu));
13362 }
13363 return dwo_cutu;
13364 }
13365 }
13366 }
13367 else
13368 {
13369 /* No DWP file, look for the DWO file. */
13370
13371 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
13372 dwo_name, comp_dir);
13373 if (*dwo_file_slot == NULL)
13374 {
13375 /* Read in the file and build a table of the CUs/TUs it contains. */
13376 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
13377 }
13378 /* NOTE: This will be NULL if unable to open the file. */
13379 dwo_file = (struct dwo_file *) *dwo_file_slot;
13380
13381 if (dwo_file != NULL)
13382 {
13383 struct dwo_unit *dwo_cutu = NULL;
13384
13385 if (is_debug_types && dwo_file->tus)
13386 {
13387 struct dwo_unit find_dwo_cutu;
13388
13389 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13390 find_dwo_cutu.signature = signature;
13391 dwo_cutu
13392 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
13393 }
13394 else if (!is_debug_types && dwo_file->cus)
13395 {
13396 struct dwo_unit find_dwo_cutu;
13397
13398 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13399 find_dwo_cutu.signature = signature;
13400 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus,
13401 &find_dwo_cutu);
13402 }
13403
13404 if (dwo_cutu != NULL)
13405 {
13406 if (dwarf_read_debug)
13407 {
13408 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
13409 kind, dwo_name, hex_string (signature),
13410 host_address_to_string (dwo_cutu));
13411 }
13412 return dwo_cutu;
13413 }
13414 }
13415 }
13416
13417 /* We didn't find it. This could mean a dwo_id mismatch, or
13418 someone deleted the DWO/DWP file, or the search path isn't set up
13419 correctly to find the file. */
13420
13421 if (dwarf_read_debug)
13422 {
13423 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
13424 kind, dwo_name, hex_string (signature));
13425 }
13426
13427 /* This is a warning and not a complaint because it can be caused by
13428 pilot error (e.g., user accidentally deleting the DWO). */
13429 {
13430 /* Print the name of the DWP file if we looked there, helps the user
13431 better diagnose the problem. */
13432 std::string dwp_text;
13433
13434 if (dwp_file != NULL)
13435 dwp_text = string_printf (" [in DWP file %s]",
13436 lbasename (dwp_file->name));
13437
13438 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
13439 " [in module %s]"),
13440 kind, dwo_name, hex_string (signature),
13441 dwp_text.c_str (),
13442 this_unit->is_debug_types ? "TU" : "CU",
13443 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
13444 }
13445 return NULL;
13446 }
13447
13448 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13449 See lookup_dwo_cutu_unit for details. */
13450
13451 static struct dwo_unit *
13452 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
13453 const char *dwo_name, const char *comp_dir,
13454 ULONGEST signature)
13455 {
13456 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
13457 }
13458
13459 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13460 See lookup_dwo_cutu_unit for details. */
13461
13462 static struct dwo_unit *
13463 lookup_dwo_type_unit (struct signatured_type *this_tu,
13464 const char *dwo_name, const char *comp_dir)
13465 {
13466 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
13467 }
13468
13469 /* Traversal function for queue_and_load_all_dwo_tus. */
13470
13471 static int
13472 queue_and_load_dwo_tu (void **slot, void *info)
13473 {
13474 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13475 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
13476 ULONGEST signature = dwo_unit->signature;
13477 struct signatured_type *sig_type =
13478 lookup_dwo_signatured_type (per_cu->cu, signature);
13479
13480 if (sig_type != NULL)
13481 {
13482 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13483
13484 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13485 a real dependency of PER_CU on SIG_TYPE. That is detected later
13486 while processing PER_CU. */
13487 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
13488 load_full_type_unit (sig_cu);
13489 per_cu->imported_symtabs_push (sig_cu);
13490 }
13491
13492 return 1;
13493 }
13494
13495 /* Queue all TUs contained in the DWO of PER_CU to be read in.
13496 The DWO may have the only definition of the type, though it may not be
13497 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13498 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13499
13500 static void
13501 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
13502 {
13503 struct dwo_unit *dwo_unit;
13504 struct dwo_file *dwo_file;
13505
13506 gdb_assert (!per_cu->is_debug_types);
13507 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
13508 gdb_assert (per_cu->cu != NULL);
13509
13510 dwo_unit = per_cu->cu->dwo_unit;
13511 gdb_assert (dwo_unit != NULL);
13512
13513 dwo_file = dwo_unit->dwo_file;
13514 if (dwo_file->tus != NULL)
13515 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
13516 }
13517
13518 /* Read in various DIEs. */
13519
13520 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
13521 Inherit only the children of the DW_AT_abstract_origin DIE not being
13522 already referenced by DW_AT_abstract_origin from the children of the
13523 current DIE. */
13524
13525 static void
13526 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13527 {
13528 struct die_info *child_die;
13529 sect_offset *offsetp;
13530 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13531 struct die_info *origin_die;
13532 /* Iterator of the ORIGIN_DIE children. */
13533 struct die_info *origin_child_die;
13534 struct attribute *attr;
13535 struct dwarf2_cu *origin_cu;
13536 struct pending **origin_previous_list_in_scope;
13537
13538 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13539 if (!attr)
13540 return;
13541
13542 /* Note that following die references may follow to a die in a
13543 different cu. */
13544
13545 origin_cu = cu;
13546 origin_die = follow_die_ref (die, attr, &origin_cu);
13547
13548 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13549 symbols in. */
13550 origin_previous_list_in_scope = origin_cu->list_in_scope;
13551 origin_cu->list_in_scope = cu->list_in_scope;
13552
13553 if (die->tag != origin_die->tag
13554 && !(die->tag == DW_TAG_inlined_subroutine
13555 && origin_die->tag == DW_TAG_subprogram))
13556 complaint (_("DIE %s and its abstract origin %s have different tags"),
13557 sect_offset_str (die->sect_off),
13558 sect_offset_str (origin_die->sect_off));
13559
13560 std::vector<sect_offset> offsets;
13561
13562 for (child_die = die->child;
13563 child_die && child_die->tag;
13564 child_die = sibling_die (child_die))
13565 {
13566 struct die_info *child_origin_die;
13567 struct dwarf2_cu *child_origin_cu;
13568
13569 /* We are trying to process concrete instance entries:
13570 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
13571 it's not relevant to our analysis here. i.e. detecting DIEs that are
13572 present in the abstract instance but not referenced in the concrete
13573 one. */
13574 if (child_die->tag == DW_TAG_call_site
13575 || child_die->tag == DW_TAG_GNU_call_site)
13576 continue;
13577
13578 /* For each CHILD_DIE, find the corresponding child of
13579 ORIGIN_DIE. If there is more than one layer of
13580 DW_AT_abstract_origin, follow them all; there shouldn't be,
13581 but GCC versions at least through 4.4 generate this (GCC PR
13582 40573). */
13583 child_origin_die = child_die;
13584 child_origin_cu = cu;
13585 while (1)
13586 {
13587 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13588 child_origin_cu);
13589 if (attr == NULL)
13590 break;
13591 child_origin_die = follow_die_ref (child_origin_die, attr,
13592 &child_origin_cu);
13593 }
13594
13595 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13596 counterpart may exist. */
13597 if (child_origin_die != child_die)
13598 {
13599 if (child_die->tag != child_origin_die->tag
13600 && !(child_die->tag == DW_TAG_inlined_subroutine
13601 && child_origin_die->tag == DW_TAG_subprogram))
13602 complaint (_("Child DIE %s and its abstract origin %s have "
13603 "different tags"),
13604 sect_offset_str (child_die->sect_off),
13605 sect_offset_str (child_origin_die->sect_off));
13606 if (child_origin_die->parent != origin_die)
13607 complaint (_("Child DIE %s and its abstract origin %s have "
13608 "different parents"),
13609 sect_offset_str (child_die->sect_off),
13610 sect_offset_str (child_origin_die->sect_off));
13611 else
13612 offsets.push_back (child_origin_die->sect_off);
13613 }
13614 }
13615 std::sort (offsets.begin (), offsets.end ());
13616 sect_offset *offsets_end = offsets.data () + offsets.size ();
13617 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
13618 if (offsetp[-1] == *offsetp)
13619 complaint (_("Multiple children of DIE %s refer "
13620 "to DIE %s as their abstract origin"),
13621 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
13622
13623 offsetp = offsets.data ();
13624 origin_child_die = origin_die->child;
13625 while (origin_child_die && origin_child_die->tag)
13626 {
13627 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
13628 while (offsetp < offsets_end
13629 && *offsetp < origin_child_die->sect_off)
13630 offsetp++;
13631 if (offsetp >= offsets_end
13632 || *offsetp > origin_child_die->sect_off)
13633 {
13634 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13635 Check whether we're already processing ORIGIN_CHILD_DIE.
13636 This can happen with mutually referenced abstract_origins.
13637 PR 16581. */
13638 if (!origin_child_die->in_process)
13639 process_die (origin_child_die, origin_cu);
13640 }
13641 origin_child_die = sibling_die (origin_child_die);
13642 }
13643 origin_cu->list_in_scope = origin_previous_list_in_scope;
13644
13645 if (cu != origin_cu)
13646 compute_delayed_physnames (origin_cu);
13647 }
13648
13649 static void
13650 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
13651 {
13652 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13653 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13654 struct context_stack *newobj;
13655 CORE_ADDR lowpc;
13656 CORE_ADDR highpc;
13657 struct die_info *child_die;
13658 struct attribute *attr, *call_line, *call_file;
13659 const char *name;
13660 CORE_ADDR baseaddr;
13661 struct block *block;
13662 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13663 std::vector<struct symbol *> template_args;
13664 struct template_symbol *templ_func = NULL;
13665
13666 if (inlined_func)
13667 {
13668 /* If we do not have call site information, we can't show the
13669 caller of this inlined function. That's too confusing, so
13670 only use the scope for local variables. */
13671 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13672 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13673 if (call_line == NULL || call_file == NULL)
13674 {
13675 read_lexical_block_scope (die, cu);
13676 return;
13677 }
13678 }
13679
13680 baseaddr = objfile->text_section_offset ();
13681
13682 name = dwarf2_name (die, cu);
13683
13684 /* Ignore functions with missing or empty names. These are actually
13685 illegal according to the DWARF standard. */
13686 if (name == NULL)
13687 {
13688 complaint (_("missing name for subprogram DIE at %s"),
13689 sect_offset_str (die->sect_off));
13690 return;
13691 }
13692
13693 /* Ignore functions with missing or invalid low and high pc attributes. */
13694 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
13695 <= PC_BOUNDS_INVALID)
13696 {
13697 attr = dwarf2_attr (die, DW_AT_external, cu);
13698 if (!attr || !DW_UNSND (attr))
13699 complaint (_("cannot get low and high bounds "
13700 "for subprogram DIE at %s"),
13701 sect_offset_str (die->sect_off));
13702 return;
13703 }
13704
13705 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13706 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13707
13708 /* If we have any template arguments, then we must allocate a
13709 different sort of symbol. */
13710 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
13711 {
13712 if (child_die->tag == DW_TAG_template_type_param
13713 || child_die->tag == DW_TAG_template_value_param)
13714 {
13715 templ_func = allocate_template_symbol (objfile);
13716 templ_func->subclass = SYMBOL_TEMPLATE;
13717 break;
13718 }
13719 }
13720
13721 newobj = cu->get_builder ()->push_context (0, lowpc);
13722 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13723 (struct symbol *) templ_func);
13724
13725 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
13726 set_objfile_main_name (objfile, newobj->name->linkage_name (),
13727 cu->language);
13728
13729 /* If there is a location expression for DW_AT_frame_base, record
13730 it. */
13731 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
13732 if (attr != nullptr)
13733 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
13734
13735 /* If there is a location for the static link, record it. */
13736 newobj->static_link = NULL;
13737 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13738 if (attr != nullptr)
13739 {
13740 newobj->static_link
13741 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
13742 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
13743 dwarf2_per_cu_addr_type (cu->per_cu));
13744 }
13745
13746 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
13747
13748 if (die->child != NULL)
13749 {
13750 child_die = die->child;
13751 while (child_die && child_die->tag)
13752 {
13753 if (child_die->tag == DW_TAG_template_type_param
13754 || child_die->tag == DW_TAG_template_value_param)
13755 {
13756 struct symbol *arg = new_symbol (child_die, NULL, cu);
13757
13758 if (arg != NULL)
13759 template_args.push_back (arg);
13760 }
13761 else
13762 process_die (child_die, cu);
13763 child_die = sibling_die (child_die);
13764 }
13765 }
13766
13767 inherit_abstract_dies (die, cu);
13768
13769 /* If we have a DW_AT_specification, we might need to import using
13770 directives from the context of the specification DIE. See the
13771 comment in determine_prefix. */
13772 if (cu->language == language_cplus
13773 && dwarf2_attr (die, DW_AT_specification, cu))
13774 {
13775 struct dwarf2_cu *spec_cu = cu;
13776 struct die_info *spec_die = die_specification (die, &spec_cu);
13777
13778 while (spec_die)
13779 {
13780 child_die = spec_die->child;
13781 while (child_die && child_die->tag)
13782 {
13783 if (child_die->tag == DW_TAG_imported_module)
13784 process_die (child_die, spec_cu);
13785 child_die = sibling_die (child_die);
13786 }
13787
13788 /* In some cases, GCC generates specification DIEs that
13789 themselves contain DW_AT_specification attributes. */
13790 spec_die = die_specification (spec_die, &spec_cu);
13791 }
13792 }
13793
13794 struct context_stack cstk = cu->get_builder ()->pop_context ();
13795 /* Make a block for the local symbols within. */
13796 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
13797 cstk.static_link, lowpc, highpc);
13798
13799 /* For C++, set the block's scope. */
13800 if ((cu->language == language_cplus
13801 || cu->language == language_fortran
13802 || cu->language == language_d
13803 || cu->language == language_rust)
13804 && cu->processing_has_namespace_info)
13805 block_set_scope (block, determine_prefix (die, cu),
13806 &objfile->objfile_obstack);
13807
13808 /* If we have address ranges, record them. */
13809 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13810
13811 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
13812
13813 /* Attach template arguments to function. */
13814 if (!template_args.empty ())
13815 {
13816 gdb_assert (templ_func != NULL);
13817
13818 templ_func->n_template_arguments = template_args.size ();
13819 templ_func->template_arguments
13820 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13821 templ_func->n_template_arguments);
13822 memcpy (templ_func->template_arguments,
13823 template_args.data (),
13824 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13825
13826 /* Make sure that the symtab is set on the new symbols. Even
13827 though they don't appear in this symtab directly, other parts
13828 of gdb assume that symbols do, and this is reasonably
13829 true. */
13830 for (symbol *sym : template_args)
13831 symbol_set_symtab (sym, symbol_symtab (templ_func));
13832 }
13833
13834 /* In C++, we can have functions nested inside functions (e.g., when
13835 a function declares a class that has methods). This means that
13836 when we finish processing a function scope, we may need to go
13837 back to building a containing block's symbol lists. */
13838 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13839 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13840
13841 /* If we've finished processing a top-level function, subsequent
13842 symbols go in the file symbol list. */
13843 if (cu->get_builder ()->outermost_context_p ())
13844 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
13845 }
13846
13847 /* Process all the DIES contained within a lexical block scope. Start
13848 a new scope, process the dies, and then close the scope. */
13849
13850 static void
13851 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13852 {
13853 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13854 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13855 CORE_ADDR lowpc, highpc;
13856 struct die_info *child_die;
13857 CORE_ADDR baseaddr;
13858
13859 baseaddr = objfile->text_section_offset ();
13860
13861 /* Ignore blocks with missing or invalid low and high pc attributes. */
13862 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13863 as multiple lexical blocks? Handling children in a sane way would
13864 be nasty. Might be easier to properly extend generic blocks to
13865 describe ranges. */
13866 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13867 {
13868 case PC_BOUNDS_NOT_PRESENT:
13869 /* DW_TAG_lexical_block has no attributes, process its children as if
13870 there was no wrapping by that DW_TAG_lexical_block.
13871 GCC does no longer produces such DWARF since GCC r224161. */
13872 for (child_die = die->child;
13873 child_die != NULL && child_die->tag;
13874 child_die = sibling_die (child_die))
13875 process_die (child_die, cu);
13876 return;
13877 case PC_BOUNDS_INVALID:
13878 return;
13879 }
13880 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13881 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13882
13883 cu->get_builder ()->push_context (0, lowpc);
13884 if (die->child != NULL)
13885 {
13886 child_die = die->child;
13887 while (child_die && child_die->tag)
13888 {
13889 process_die (child_die, cu);
13890 child_die = sibling_die (child_die);
13891 }
13892 }
13893 inherit_abstract_dies (die, cu);
13894 struct context_stack cstk = cu->get_builder ()->pop_context ();
13895
13896 if (*cu->get_builder ()->get_local_symbols () != NULL
13897 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13898 {
13899 struct block *block
13900 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13901 cstk.start_addr, highpc);
13902
13903 /* Note that recording ranges after traversing children, as we
13904 do here, means that recording a parent's ranges entails
13905 walking across all its children's ranges as they appear in
13906 the address map, which is quadratic behavior.
13907
13908 It would be nicer to record the parent's ranges before
13909 traversing its children, simply overriding whatever you find
13910 there. But since we don't even decide whether to create a
13911 block until after we've traversed its children, that's hard
13912 to do. */
13913 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13914 }
13915 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13916 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13917 }
13918
13919 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13920
13921 static void
13922 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13923 {
13924 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13925 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13926 CORE_ADDR pc, baseaddr;
13927 struct attribute *attr;
13928 struct call_site *call_site, call_site_local;
13929 void **slot;
13930 int nparams;
13931 struct die_info *child_die;
13932
13933 baseaddr = objfile->text_section_offset ();
13934
13935 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13936 if (attr == NULL)
13937 {
13938 /* This was a pre-DWARF-5 GNU extension alias
13939 for DW_AT_call_return_pc. */
13940 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13941 }
13942 if (!attr)
13943 {
13944 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13945 "DIE %s [in module %s]"),
13946 sect_offset_str (die->sect_off), objfile_name (objfile));
13947 return;
13948 }
13949 pc = attr_value_as_address (attr) + baseaddr;
13950 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13951
13952 if (cu->call_site_htab == NULL)
13953 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13954 NULL, &objfile->objfile_obstack,
13955 hashtab_obstack_allocate, NULL);
13956 call_site_local.pc = pc;
13957 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13958 if (*slot != NULL)
13959 {
13960 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13961 "DIE %s [in module %s]"),
13962 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13963 objfile_name (objfile));
13964 return;
13965 }
13966
13967 /* Count parameters at the caller. */
13968
13969 nparams = 0;
13970 for (child_die = die->child; child_die && child_die->tag;
13971 child_die = sibling_die (child_die))
13972 {
13973 if (child_die->tag != DW_TAG_call_site_parameter
13974 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13975 {
13976 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
13977 "DW_TAG_call_site child DIE %s [in module %s]"),
13978 child_die->tag, sect_offset_str (child_die->sect_off),
13979 objfile_name (objfile));
13980 continue;
13981 }
13982
13983 nparams++;
13984 }
13985
13986 call_site
13987 = ((struct call_site *)
13988 obstack_alloc (&objfile->objfile_obstack,
13989 sizeof (*call_site)
13990 + (sizeof (*call_site->parameter) * (nparams - 1))));
13991 *slot = call_site;
13992 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13993 call_site->pc = pc;
13994
13995 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13996 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
13997 {
13998 struct die_info *func_die;
13999
14000 /* Skip also over DW_TAG_inlined_subroutine. */
14001 for (func_die = die->parent;
14002 func_die && func_die->tag != DW_TAG_subprogram
14003 && func_die->tag != DW_TAG_subroutine_type;
14004 func_die = func_die->parent);
14005
14006 /* DW_AT_call_all_calls is a superset
14007 of DW_AT_call_all_tail_calls. */
14008 if (func_die
14009 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
14010 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
14011 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
14012 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
14013 {
14014 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
14015 not complete. But keep CALL_SITE for look ups via call_site_htab,
14016 both the initial caller containing the real return address PC and
14017 the final callee containing the current PC of a chain of tail
14018 calls do not need to have the tail call list complete. But any
14019 function candidate for a virtual tail call frame searched via
14020 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
14021 determined unambiguously. */
14022 }
14023 else
14024 {
14025 struct type *func_type = NULL;
14026
14027 if (func_die)
14028 func_type = get_die_type (func_die, cu);
14029 if (func_type != NULL)
14030 {
14031 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
14032
14033 /* Enlist this call site to the function. */
14034 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
14035 TYPE_TAIL_CALL_LIST (func_type) = call_site;
14036 }
14037 else
14038 complaint (_("Cannot find function owning DW_TAG_call_site "
14039 "DIE %s [in module %s]"),
14040 sect_offset_str (die->sect_off), objfile_name (objfile));
14041 }
14042 }
14043
14044 attr = dwarf2_attr (die, DW_AT_call_target, cu);
14045 if (attr == NULL)
14046 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
14047 if (attr == NULL)
14048 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
14049 if (attr == NULL)
14050 {
14051 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
14052 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14053 }
14054 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
14055 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
14056 /* Keep NULL DWARF_BLOCK. */;
14057 else if (attr_form_is_block (attr))
14058 {
14059 struct dwarf2_locexpr_baton *dlbaton;
14060
14061 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
14062 dlbaton->data = DW_BLOCK (attr)->data;
14063 dlbaton->size = DW_BLOCK (attr)->size;
14064 dlbaton->per_cu = cu->per_cu;
14065
14066 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
14067 }
14068 else if (attr_form_is_ref (attr))
14069 {
14070 struct dwarf2_cu *target_cu = cu;
14071 struct die_info *target_die;
14072
14073 target_die = follow_die_ref (die, attr, &target_cu);
14074 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
14075 if (die_is_declaration (target_die, target_cu))
14076 {
14077 const char *target_physname;
14078
14079 /* Prefer the mangled name; otherwise compute the demangled one. */
14080 target_physname = dw2_linkage_name (target_die, target_cu);
14081 if (target_physname == NULL)
14082 target_physname = dwarf2_physname (NULL, target_die, target_cu);
14083 if (target_physname == NULL)
14084 complaint (_("DW_AT_call_target target DIE has invalid "
14085 "physname, for referencing DIE %s [in module %s]"),
14086 sect_offset_str (die->sect_off), objfile_name (objfile));
14087 else
14088 SET_FIELD_PHYSNAME (call_site->target, target_physname);
14089 }
14090 else
14091 {
14092 CORE_ADDR lowpc;
14093
14094 /* DW_AT_entry_pc should be preferred. */
14095 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
14096 <= PC_BOUNDS_INVALID)
14097 complaint (_("DW_AT_call_target target DIE has invalid "
14098 "low pc, for referencing DIE %s [in module %s]"),
14099 sect_offset_str (die->sect_off), objfile_name (objfile));
14100 else
14101 {
14102 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14103 SET_FIELD_PHYSADDR (call_site->target, lowpc);
14104 }
14105 }
14106 }
14107 else
14108 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
14109 "block nor reference, for DIE %s [in module %s]"),
14110 sect_offset_str (die->sect_off), objfile_name (objfile));
14111
14112 call_site->per_cu = cu->per_cu;
14113
14114 for (child_die = die->child;
14115 child_die && child_die->tag;
14116 child_die = sibling_die (child_die))
14117 {
14118 struct call_site_parameter *parameter;
14119 struct attribute *loc, *origin;
14120
14121 if (child_die->tag != DW_TAG_call_site_parameter
14122 && child_die->tag != DW_TAG_GNU_call_site_parameter)
14123 {
14124 /* Already printed the complaint above. */
14125 continue;
14126 }
14127
14128 gdb_assert (call_site->parameter_count < nparams);
14129 parameter = &call_site->parameter[call_site->parameter_count];
14130
14131 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14132 specifies DW_TAG_formal_parameter. Value of the data assumed for the
14133 register is contained in DW_AT_call_value. */
14134
14135 loc = dwarf2_attr (child_die, DW_AT_location, cu);
14136 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14137 if (origin == NULL)
14138 {
14139 /* This was a pre-DWARF-5 GNU extension alias
14140 for DW_AT_call_parameter. */
14141 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14142 }
14143 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
14144 {
14145 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
14146
14147 sect_offset sect_off
14148 = (sect_offset) dwarf2_get_ref_die_offset (origin);
14149 if (!offset_in_cu_p (&cu->header, sect_off))
14150 {
14151 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14152 binding can be done only inside one CU. Such referenced DIE
14153 therefore cannot be even moved to DW_TAG_partial_unit. */
14154 complaint (_("DW_AT_call_parameter offset is not in CU for "
14155 "DW_TAG_call_site child DIE %s [in module %s]"),
14156 sect_offset_str (child_die->sect_off),
14157 objfile_name (objfile));
14158 continue;
14159 }
14160 parameter->u.param_cu_off
14161 = (cu_offset) (sect_off - cu->header.sect_off);
14162 }
14163 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
14164 {
14165 complaint (_("No DW_FORM_block* DW_AT_location for "
14166 "DW_TAG_call_site child DIE %s [in module %s]"),
14167 sect_offset_str (child_die->sect_off), objfile_name (objfile));
14168 continue;
14169 }
14170 else
14171 {
14172 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
14173 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
14174 if (parameter->u.dwarf_reg != -1)
14175 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
14176 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
14177 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
14178 &parameter->u.fb_offset))
14179 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14180 else
14181 {
14182 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
14183 "for DW_FORM_block* DW_AT_location is supported for "
14184 "DW_TAG_call_site child DIE %s "
14185 "[in module %s]"),
14186 sect_offset_str (child_die->sect_off),
14187 objfile_name (objfile));
14188 continue;
14189 }
14190 }
14191
14192 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14193 if (attr == NULL)
14194 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
14195 if (!attr_form_is_block (attr))
14196 {
14197 complaint (_("No DW_FORM_block* DW_AT_call_value for "
14198 "DW_TAG_call_site child DIE %s [in module %s]"),
14199 sect_offset_str (child_die->sect_off),
14200 objfile_name (objfile));
14201 continue;
14202 }
14203 parameter->value = DW_BLOCK (attr)->data;
14204 parameter->value_size = DW_BLOCK (attr)->size;
14205
14206 /* Parameters are not pre-cleared by memset above. */
14207 parameter->data_value = NULL;
14208 parameter->data_value_size = 0;
14209 call_site->parameter_count++;
14210
14211 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14212 if (attr == NULL)
14213 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
14214 if (attr != nullptr)
14215 {
14216 if (!attr_form_is_block (attr))
14217 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
14218 "DW_TAG_call_site child DIE %s [in module %s]"),
14219 sect_offset_str (child_die->sect_off),
14220 objfile_name (objfile));
14221 else
14222 {
14223 parameter->data_value = DW_BLOCK (attr)->data;
14224 parameter->data_value_size = DW_BLOCK (attr)->size;
14225 }
14226 }
14227 }
14228 }
14229
14230 /* Helper function for read_variable. If DIE represents a virtual
14231 table, then return the type of the concrete object that is
14232 associated with the virtual table. Otherwise, return NULL. */
14233
14234 static struct type *
14235 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14236 {
14237 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14238 if (attr == NULL)
14239 return NULL;
14240
14241 /* Find the type DIE. */
14242 struct die_info *type_die = NULL;
14243 struct dwarf2_cu *type_cu = cu;
14244
14245 if (attr_form_is_ref (attr))
14246 type_die = follow_die_ref (die, attr, &type_cu);
14247 if (type_die == NULL)
14248 return NULL;
14249
14250 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14251 return NULL;
14252 return die_containing_type (type_die, type_cu);
14253 }
14254
14255 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14256
14257 static void
14258 read_variable (struct die_info *die, struct dwarf2_cu *cu)
14259 {
14260 struct rust_vtable_symbol *storage = NULL;
14261
14262 if (cu->language == language_rust)
14263 {
14264 struct type *containing_type = rust_containing_type (die, cu);
14265
14266 if (containing_type != NULL)
14267 {
14268 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14269
14270 storage = new (&objfile->objfile_obstack) rust_vtable_symbol ();
14271 initialize_objfile_symbol (storage);
14272 storage->concrete_type = containing_type;
14273 storage->subclass = SYMBOL_RUST_VTABLE;
14274 }
14275 }
14276
14277 struct symbol *res = new_symbol (die, NULL, cu, storage);
14278 struct attribute *abstract_origin
14279 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14280 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
14281 if (res == NULL && loc && abstract_origin)
14282 {
14283 /* We have a variable without a name, but with a location and an abstract
14284 origin. This may be a concrete instance of an abstract variable
14285 referenced from an DW_OP_GNU_variable_value, so save it to find it back
14286 later. */
14287 struct dwarf2_cu *origin_cu = cu;
14288 struct die_info *origin_die
14289 = follow_die_ref (die, abstract_origin, &origin_cu);
14290 dwarf2_per_objfile *dpo = cu->per_cu->dwarf2_per_objfile;
14291 dpo->abstract_to_concrete[origin_die->sect_off].push_back (die->sect_off);
14292 }
14293 }
14294
14295 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14296 reading .debug_rnglists.
14297 Callback's type should be:
14298 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14299 Return true if the attributes are present and valid, otherwise,
14300 return false. */
14301
14302 template <typename Callback>
14303 static bool
14304 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
14305 Callback &&callback)
14306 {
14307 struct dwarf2_per_objfile *dwarf2_per_objfile
14308 = cu->per_cu->dwarf2_per_objfile;
14309 struct objfile *objfile = dwarf2_per_objfile->objfile;
14310 bfd *obfd = objfile->obfd;
14311 /* Base address selection entry. */
14312 CORE_ADDR base;
14313 int found_base;
14314 const gdb_byte *buffer;
14315 CORE_ADDR baseaddr;
14316 bool overflow = false;
14317
14318 found_base = cu->base_known;
14319 base = cu->base_address;
14320
14321 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
14322 if (offset >= dwarf2_per_objfile->rnglists.size)
14323 {
14324 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
14325 offset);
14326 return false;
14327 }
14328 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
14329
14330 baseaddr = objfile->text_section_offset ();
14331
14332 while (1)
14333 {
14334 /* Initialize it due to a false compiler warning. */
14335 CORE_ADDR range_beginning = 0, range_end = 0;
14336 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
14337 + dwarf2_per_objfile->rnglists.size);
14338 unsigned int bytes_read;
14339
14340 if (buffer == buf_end)
14341 {
14342 overflow = true;
14343 break;
14344 }
14345 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14346 switch (rlet)
14347 {
14348 case DW_RLE_end_of_list:
14349 break;
14350 case DW_RLE_base_address:
14351 if (buffer + cu->header.addr_size > buf_end)
14352 {
14353 overflow = true;
14354 break;
14355 }
14356 base = read_address (obfd, buffer, cu, &bytes_read);
14357 found_base = 1;
14358 buffer += bytes_read;
14359 break;
14360 case DW_RLE_start_length:
14361 if (buffer + cu->header.addr_size > buf_end)
14362 {
14363 overflow = true;
14364 break;
14365 }
14366 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14367 buffer += bytes_read;
14368 range_end = (range_beginning
14369 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14370 buffer += bytes_read;
14371 if (buffer > buf_end)
14372 {
14373 overflow = true;
14374 break;
14375 }
14376 break;
14377 case DW_RLE_offset_pair:
14378 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14379 buffer += bytes_read;
14380 if (buffer > buf_end)
14381 {
14382 overflow = true;
14383 break;
14384 }
14385 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14386 buffer += bytes_read;
14387 if (buffer > buf_end)
14388 {
14389 overflow = true;
14390 break;
14391 }
14392 break;
14393 case DW_RLE_start_end:
14394 if (buffer + 2 * cu->header.addr_size > buf_end)
14395 {
14396 overflow = true;
14397 break;
14398 }
14399 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14400 buffer += bytes_read;
14401 range_end = read_address (obfd, buffer, cu, &bytes_read);
14402 buffer += bytes_read;
14403 break;
14404 default:
14405 complaint (_("Invalid .debug_rnglists data (no base address)"));
14406 return false;
14407 }
14408 if (rlet == DW_RLE_end_of_list || overflow)
14409 break;
14410 if (rlet == DW_RLE_base_address)
14411 continue;
14412
14413 if (!found_base)
14414 {
14415 /* We have no valid base address for the ranges
14416 data. */
14417 complaint (_("Invalid .debug_rnglists data (no base address)"));
14418 return false;
14419 }
14420
14421 if (range_beginning > range_end)
14422 {
14423 /* Inverted range entries are invalid. */
14424 complaint (_("Invalid .debug_rnglists data (inverted range)"));
14425 return false;
14426 }
14427
14428 /* Empty range entries have no effect. */
14429 if (range_beginning == range_end)
14430 continue;
14431
14432 range_beginning += base;
14433 range_end += base;
14434
14435 /* A not-uncommon case of bad debug info.
14436 Don't pollute the addrmap with bad data. */
14437 if (range_beginning + baseaddr == 0
14438 && !dwarf2_per_objfile->has_section_at_zero)
14439 {
14440 complaint (_(".debug_rnglists entry has start address of zero"
14441 " [in module %s]"), objfile_name (objfile));
14442 continue;
14443 }
14444
14445 callback (range_beginning, range_end);
14446 }
14447
14448 if (overflow)
14449 {
14450 complaint (_("Offset %d is not terminated "
14451 "for DW_AT_ranges attribute"),
14452 offset);
14453 return false;
14454 }
14455
14456 return true;
14457 }
14458
14459 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14460 Callback's type should be:
14461 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14462 Return 1 if the attributes are present and valid, otherwise, return 0. */
14463
14464 template <typename Callback>
14465 static int
14466 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
14467 Callback &&callback)
14468 {
14469 struct dwarf2_per_objfile *dwarf2_per_objfile
14470 = cu->per_cu->dwarf2_per_objfile;
14471 struct objfile *objfile = dwarf2_per_objfile->objfile;
14472 struct comp_unit_head *cu_header = &cu->header;
14473 bfd *obfd = objfile->obfd;
14474 unsigned int addr_size = cu_header->addr_size;
14475 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14476 /* Base address selection entry. */
14477 CORE_ADDR base;
14478 int found_base;
14479 unsigned int dummy;
14480 const gdb_byte *buffer;
14481 CORE_ADDR baseaddr;
14482
14483 if (cu_header->version >= 5)
14484 return dwarf2_rnglists_process (offset, cu, callback);
14485
14486 found_base = cu->base_known;
14487 base = cu->base_address;
14488
14489 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
14490 if (offset >= dwarf2_per_objfile->ranges.size)
14491 {
14492 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
14493 offset);
14494 return 0;
14495 }
14496 buffer = dwarf2_per_objfile->ranges.buffer + offset;
14497
14498 baseaddr = objfile->text_section_offset ();
14499
14500 while (1)
14501 {
14502 CORE_ADDR range_beginning, range_end;
14503
14504 range_beginning = read_address (obfd, buffer, cu, &dummy);
14505 buffer += addr_size;
14506 range_end = read_address (obfd, buffer, cu, &dummy);
14507 buffer += addr_size;
14508 offset += 2 * addr_size;
14509
14510 /* An end of list marker is a pair of zero addresses. */
14511 if (range_beginning == 0 && range_end == 0)
14512 /* Found the end of list entry. */
14513 break;
14514
14515 /* Each base address selection entry is a pair of 2 values.
14516 The first is the largest possible address, the second is
14517 the base address. Check for a base address here. */
14518 if ((range_beginning & mask) == mask)
14519 {
14520 /* If we found the largest possible address, then we already
14521 have the base address in range_end. */
14522 base = range_end;
14523 found_base = 1;
14524 continue;
14525 }
14526
14527 if (!found_base)
14528 {
14529 /* We have no valid base address for the ranges
14530 data. */
14531 complaint (_("Invalid .debug_ranges data (no base address)"));
14532 return 0;
14533 }
14534
14535 if (range_beginning > range_end)
14536 {
14537 /* Inverted range entries are invalid. */
14538 complaint (_("Invalid .debug_ranges data (inverted range)"));
14539 return 0;
14540 }
14541
14542 /* Empty range entries have no effect. */
14543 if (range_beginning == range_end)
14544 continue;
14545
14546 range_beginning += base;
14547 range_end += base;
14548
14549 /* A not-uncommon case of bad debug info.
14550 Don't pollute the addrmap with bad data. */
14551 if (range_beginning + baseaddr == 0
14552 && !dwarf2_per_objfile->has_section_at_zero)
14553 {
14554 complaint (_(".debug_ranges entry has start address of zero"
14555 " [in module %s]"), objfile_name (objfile));
14556 continue;
14557 }
14558
14559 callback (range_beginning, range_end);
14560 }
14561
14562 return 1;
14563 }
14564
14565 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14566 Return 1 if the attributes are present and valid, otherwise, return 0.
14567 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14568
14569 static int
14570 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14571 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14572 dwarf2_psymtab *ranges_pst)
14573 {
14574 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14575 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14576 const CORE_ADDR baseaddr = objfile->text_section_offset ();
14577 int low_set = 0;
14578 CORE_ADDR low = 0;
14579 CORE_ADDR high = 0;
14580 int retval;
14581
14582 retval = dwarf2_ranges_process (offset, cu,
14583 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14584 {
14585 if (ranges_pst != NULL)
14586 {
14587 CORE_ADDR lowpc;
14588 CORE_ADDR highpc;
14589
14590 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14591 range_beginning + baseaddr)
14592 - baseaddr);
14593 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14594 range_end + baseaddr)
14595 - baseaddr);
14596 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
14597 lowpc, highpc - 1, ranges_pst);
14598 }
14599
14600 /* FIXME: This is recording everything as a low-high
14601 segment of consecutive addresses. We should have a
14602 data structure for discontiguous block ranges
14603 instead. */
14604 if (! low_set)
14605 {
14606 low = range_beginning;
14607 high = range_end;
14608 low_set = 1;
14609 }
14610 else
14611 {
14612 if (range_beginning < low)
14613 low = range_beginning;
14614 if (range_end > high)
14615 high = range_end;
14616 }
14617 });
14618 if (!retval)
14619 return 0;
14620
14621 if (! low_set)
14622 /* If the first entry is an end-of-list marker, the range
14623 describes an empty scope, i.e. no instructions. */
14624 return 0;
14625
14626 if (low_return)
14627 *low_return = low;
14628 if (high_return)
14629 *high_return = high;
14630 return 1;
14631 }
14632
14633 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
14634 definition for the return value. *LOWPC and *HIGHPC are set iff
14635 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
14636
14637 static enum pc_bounds_kind
14638 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
14639 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14640 dwarf2_psymtab *pst)
14641 {
14642 struct dwarf2_per_objfile *dwarf2_per_objfile
14643 = cu->per_cu->dwarf2_per_objfile;
14644 struct attribute *attr;
14645 struct attribute *attr_high;
14646 CORE_ADDR low = 0;
14647 CORE_ADDR high = 0;
14648 enum pc_bounds_kind ret;
14649
14650 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14651 if (attr_high)
14652 {
14653 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14654 if (attr != nullptr)
14655 {
14656 low = attr_value_as_address (attr);
14657 high = attr_value_as_address (attr_high);
14658 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14659 high += low;
14660 }
14661 else
14662 /* Found high w/o low attribute. */
14663 return PC_BOUNDS_INVALID;
14664
14665 /* Found consecutive range of addresses. */
14666 ret = PC_BOUNDS_HIGH_LOW;
14667 }
14668 else
14669 {
14670 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14671 if (attr != NULL)
14672 {
14673 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
14674 We take advantage of the fact that DW_AT_ranges does not appear
14675 in DW_TAG_compile_unit of DWO files. */
14676 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14677 unsigned int ranges_offset = (DW_UNSND (attr)
14678 + (need_ranges_base
14679 ? cu->ranges_base
14680 : 0));
14681
14682 /* Value of the DW_AT_ranges attribute is the offset in the
14683 .debug_ranges section. */
14684 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
14685 return PC_BOUNDS_INVALID;
14686 /* Found discontinuous range of addresses. */
14687 ret = PC_BOUNDS_RANGES;
14688 }
14689 else
14690 return PC_BOUNDS_NOT_PRESENT;
14691 }
14692
14693 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
14694 if (high <= low)
14695 return PC_BOUNDS_INVALID;
14696
14697 /* When using the GNU linker, .gnu.linkonce. sections are used to
14698 eliminate duplicate copies of functions and vtables and such.
14699 The linker will arbitrarily choose one and discard the others.
14700 The AT_*_pc values for such functions refer to local labels in
14701 these sections. If the section from that file was discarded, the
14702 labels are not in the output, so the relocs get a value of 0.
14703 If this is a discarded function, mark the pc bounds as invalid,
14704 so that GDB will ignore it. */
14705 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
14706 return PC_BOUNDS_INVALID;
14707
14708 *lowpc = low;
14709 if (highpc)
14710 *highpc = high;
14711 return ret;
14712 }
14713
14714 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
14715 its low and high PC addresses. Do nothing if these addresses could not
14716 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14717 and HIGHPC to the high address if greater than HIGHPC. */
14718
14719 static void
14720 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14721 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14722 struct dwarf2_cu *cu)
14723 {
14724 CORE_ADDR low, high;
14725 struct die_info *child = die->child;
14726
14727 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
14728 {
14729 *lowpc = std::min (*lowpc, low);
14730 *highpc = std::max (*highpc, high);
14731 }
14732
14733 /* If the language does not allow nested subprograms (either inside
14734 subprograms or lexical blocks), we're done. */
14735 if (cu->language != language_ada)
14736 return;
14737
14738 /* Check all the children of the given DIE. If it contains nested
14739 subprograms, then check their pc bounds. Likewise, we need to
14740 check lexical blocks as well, as they may also contain subprogram
14741 definitions. */
14742 while (child && child->tag)
14743 {
14744 if (child->tag == DW_TAG_subprogram
14745 || child->tag == DW_TAG_lexical_block)
14746 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14747 child = sibling_die (child);
14748 }
14749 }
14750
14751 /* Get the low and high pc's represented by the scope DIE, and store
14752 them in *LOWPC and *HIGHPC. If the correct values can't be
14753 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14754
14755 static void
14756 get_scope_pc_bounds (struct die_info *die,
14757 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14758 struct dwarf2_cu *cu)
14759 {
14760 CORE_ADDR best_low = (CORE_ADDR) -1;
14761 CORE_ADDR best_high = (CORE_ADDR) 0;
14762 CORE_ADDR current_low, current_high;
14763
14764 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
14765 >= PC_BOUNDS_RANGES)
14766 {
14767 best_low = current_low;
14768 best_high = current_high;
14769 }
14770 else
14771 {
14772 struct die_info *child = die->child;
14773
14774 while (child && child->tag)
14775 {
14776 switch (child->tag) {
14777 case DW_TAG_subprogram:
14778 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14779 break;
14780 case DW_TAG_namespace:
14781 case DW_TAG_module:
14782 /* FIXME: carlton/2004-01-16: Should we do this for
14783 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14784 that current GCC's always emit the DIEs corresponding
14785 to definitions of methods of classes as children of a
14786 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14787 the DIEs giving the declarations, which could be
14788 anywhere). But I don't see any reason why the
14789 standards says that they have to be there. */
14790 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14791
14792 if (current_low != ((CORE_ADDR) -1))
14793 {
14794 best_low = std::min (best_low, current_low);
14795 best_high = std::max (best_high, current_high);
14796 }
14797 break;
14798 default:
14799 /* Ignore. */
14800 break;
14801 }
14802
14803 child = sibling_die (child);
14804 }
14805 }
14806
14807 *lowpc = best_low;
14808 *highpc = best_high;
14809 }
14810
14811 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14812 in DIE. */
14813
14814 static void
14815 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14816 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14817 {
14818 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14819 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14820 struct attribute *attr;
14821 struct attribute *attr_high;
14822
14823 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14824 if (attr_high)
14825 {
14826 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14827 if (attr != nullptr)
14828 {
14829 CORE_ADDR low = attr_value_as_address (attr);
14830 CORE_ADDR high = attr_value_as_address (attr_high);
14831
14832 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14833 high += low;
14834
14835 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14836 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14837 cu->get_builder ()->record_block_range (block, low, high - 1);
14838 }
14839 }
14840
14841 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14842 if (attr != nullptr)
14843 {
14844 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
14845 We take advantage of the fact that DW_AT_ranges does not appear
14846 in DW_TAG_compile_unit of DWO files. */
14847 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14848
14849 /* The value of the DW_AT_ranges attribute is the offset of the
14850 address range list in the .debug_ranges section. */
14851 unsigned long offset = (DW_UNSND (attr)
14852 + (need_ranges_base ? cu->ranges_base : 0));
14853
14854 std::vector<blockrange> blockvec;
14855 dwarf2_ranges_process (offset, cu,
14856 [&] (CORE_ADDR start, CORE_ADDR end)
14857 {
14858 start += baseaddr;
14859 end += baseaddr;
14860 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14861 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14862 cu->get_builder ()->record_block_range (block, start, end - 1);
14863 blockvec.emplace_back (start, end);
14864 });
14865
14866 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
14867 }
14868 }
14869
14870 /* Check whether the producer field indicates either of GCC < 4.6, or the
14871 Intel C/C++ compiler, and cache the result in CU. */
14872
14873 static void
14874 check_producer (struct dwarf2_cu *cu)
14875 {
14876 int major, minor;
14877
14878 if (cu->producer == NULL)
14879 {
14880 /* For unknown compilers expect their behavior is DWARF version
14881 compliant.
14882
14883 GCC started to support .debug_types sections by -gdwarf-4 since
14884 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14885 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14886 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14887 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14888 }
14889 else if (producer_is_gcc (cu->producer, &major, &minor))
14890 {
14891 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14892 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14893 }
14894 else if (producer_is_icc (cu->producer, &major, &minor))
14895 {
14896 cu->producer_is_icc = true;
14897 cu->producer_is_icc_lt_14 = major < 14;
14898 }
14899 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14900 cu->producer_is_codewarrior = true;
14901 else
14902 {
14903 /* For other non-GCC compilers, expect their behavior is DWARF version
14904 compliant. */
14905 }
14906
14907 cu->checked_producer = true;
14908 }
14909
14910 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14911 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14912 during 4.6.0 experimental. */
14913
14914 static bool
14915 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14916 {
14917 if (!cu->checked_producer)
14918 check_producer (cu);
14919
14920 return cu->producer_is_gxx_lt_4_6;
14921 }
14922
14923
14924 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14925 with incorrect is_stmt attributes. */
14926
14927 static bool
14928 producer_is_codewarrior (struct dwarf2_cu *cu)
14929 {
14930 if (!cu->checked_producer)
14931 check_producer (cu);
14932
14933 return cu->producer_is_codewarrior;
14934 }
14935
14936 /* Return the default accessibility type if it is not overridden by
14937 DW_AT_accessibility. */
14938
14939 static enum dwarf_access_attribute
14940 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14941 {
14942 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14943 {
14944 /* The default DWARF 2 accessibility for members is public, the default
14945 accessibility for inheritance is private. */
14946
14947 if (die->tag != DW_TAG_inheritance)
14948 return DW_ACCESS_public;
14949 else
14950 return DW_ACCESS_private;
14951 }
14952 else
14953 {
14954 /* DWARF 3+ defines the default accessibility a different way. The same
14955 rules apply now for DW_TAG_inheritance as for the members and it only
14956 depends on the container kind. */
14957
14958 if (die->parent->tag == DW_TAG_class_type)
14959 return DW_ACCESS_private;
14960 else
14961 return DW_ACCESS_public;
14962 }
14963 }
14964
14965 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14966 offset. If the attribute was not found return 0, otherwise return
14967 1. If it was found but could not properly be handled, set *OFFSET
14968 to 0. */
14969
14970 static int
14971 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14972 LONGEST *offset)
14973 {
14974 struct attribute *attr;
14975
14976 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14977 if (attr != NULL)
14978 {
14979 *offset = 0;
14980
14981 /* Note that we do not check for a section offset first here.
14982 This is because DW_AT_data_member_location is new in DWARF 4,
14983 so if we see it, we can assume that a constant form is really
14984 a constant and not a section offset. */
14985 if (attr_form_is_constant (attr))
14986 *offset = dwarf2_get_attr_constant_value (attr, 0);
14987 else if (attr_form_is_section_offset (attr))
14988 dwarf2_complex_location_expr_complaint ();
14989 else if (attr_form_is_block (attr))
14990 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14991 else
14992 dwarf2_complex_location_expr_complaint ();
14993
14994 return 1;
14995 }
14996
14997 return 0;
14998 }
14999
15000 /* Add an aggregate field to the field list. */
15001
15002 static void
15003 dwarf2_add_field (struct field_info *fip, struct die_info *die,
15004 struct dwarf2_cu *cu)
15005 {
15006 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15007 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15008 struct nextfield *new_field;
15009 struct attribute *attr;
15010 struct field *fp;
15011 const char *fieldname = "";
15012
15013 if (die->tag == DW_TAG_inheritance)
15014 {
15015 fip->baseclasses.emplace_back ();
15016 new_field = &fip->baseclasses.back ();
15017 }
15018 else
15019 {
15020 fip->fields.emplace_back ();
15021 new_field = &fip->fields.back ();
15022 }
15023
15024 fip->nfields++;
15025
15026 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15027 if (attr != nullptr)
15028 new_field->accessibility = DW_UNSND (attr);
15029 else
15030 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
15031 if (new_field->accessibility != DW_ACCESS_public)
15032 fip->non_public_fields = 1;
15033
15034 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15035 if (attr != nullptr)
15036 new_field->virtuality = DW_UNSND (attr);
15037 else
15038 new_field->virtuality = DW_VIRTUALITY_none;
15039
15040 fp = &new_field->field;
15041
15042 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
15043 {
15044 LONGEST offset;
15045
15046 /* Data member other than a C++ static data member. */
15047
15048 /* Get type of field. */
15049 fp->type = die_type (die, cu);
15050
15051 SET_FIELD_BITPOS (*fp, 0);
15052
15053 /* Get bit size of field (zero if none). */
15054 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
15055 if (attr != nullptr)
15056 {
15057 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
15058 }
15059 else
15060 {
15061 FIELD_BITSIZE (*fp) = 0;
15062 }
15063
15064 /* Get bit offset of field. */
15065 if (handle_data_member_location (die, cu, &offset))
15066 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
15067 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
15068 if (attr != nullptr)
15069 {
15070 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
15071 {
15072 /* For big endian bits, the DW_AT_bit_offset gives the
15073 additional bit offset from the MSB of the containing
15074 anonymous object to the MSB of the field. We don't
15075 have to do anything special since we don't need to
15076 know the size of the anonymous object. */
15077 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
15078 }
15079 else
15080 {
15081 /* For little endian bits, compute the bit offset to the
15082 MSB of the anonymous object, subtract off the number of
15083 bits from the MSB of the field to the MSB of the
15084 object, and then subtract off the number of bits of
15085 the field itself. The result is the bit offset of
15086 the LSB of the field. */
15087 int anonymous_size;
15088 int bit_offset = DW_UNSND (attr);
15089
15090 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15091 if (attr != nullptr)
15092 {
15093 /* The size of the anonymous object containing
15094 the bit field is explicit, so use the
15095 indicated size (in bytes). */
15096 anonymous_size = DW_UNSND (attr);
15097 }
15098 else
15099 {
15100 /* The size of the anonymous object containing
15101 the bit field must be inferred from the type
15102 attribute of the data member containing the
15103 bit field. */
15104 anonymous_size = TYPE_LENGTH (fp->type);
15105 }
15106 SET_FIELD_BITPOS (*fp,
15107 (FIELD_BITPOS (*fp)
15108 + anonymous_size * bits_per_byte
15109 - bit_offset - FIELD_BITSIZE (*fp)));
15110 }
15111 }
15112 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15113 if (attr != NULL)
15114 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15115 + dwarf2_get_attr_constant_value (attr, 0)));
15116
15117 /* Get name of field. */
15118 fieldname = dwarf2_name (die, cu);
15119 if (fieldname == NULL)
15120 fieldname = "";
15121
15122 /* The name is already allocated along with this objfile, so we don't
15123 need to duplicate it for the type. */
15124 fp->name = fieldname;
15125
15126 /* Change accessibility for artificial fields (e.g. virtual table
15127 pointer or virtual base class pointer) to private. */
15128 if (dwarf2_attr (die, DW_AT_artificial, cu))
15129 {
15130 FIELD_ARTIFICIAL (*fp) = 1;
15131 new_field->accessibility = DW_ACCESS_private;
15132 fip->non_public_fields = 1;
15133 }
15134 }
15135 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
15136 {
15137 /* C++ static member. */
15138
15139 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15140 is a declaration, but all versions of G++ as of this writing
15141 (so through at least 3.2.1) incorrectly generate
15142 DW_TAG_variable tags. */
15143
15144 const char *physname;
15145
15146 /* Get name of field. */
15147 fieldname = dwarf2_name (die, cu);
15148 if (fieldname == NULL)
15149 return;
15150
15151 attr = dwarf2_attr (die, DW_AT_const_value, cu);
15152 if (attr
15153 /* Only create a symbol if this is an external value.
15154 new_symbol checks this and puts the value in the global symbol
15155 table, which we want. If it is not external, new_symbol
15156 will try to put the value in cu->list_in_scope which is wrong. */
15157 && dwarf2_flag_true_p (die, DW_AT_external, cu))
15158 {
15159 /* A static const member, not much different than an enum as far as
15160 we're concerned, except that we can support more types. */
15161 new_symbol (die, NULL, cu);
15162 }
15163
15164 /* Get physical name. */
15165 physname = dwarf2_physname (fieldname, die, cu);
15166
15167 /* The name is already allocated along with this objfile, so we don't
15168 need to duplicate it for the type. */
15169 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
15170 FIELD_TYPE (*fp) = die_type (die, cu);
15171 FIELD_NAME (*fp) = fieldname;
15172 }
15173 else if (die->tag == DW_TAG_inheritance)
15174 {
15175 LONGEST offset;
15176
15177 /* C++ base class field. */
15178 if (handle_data_member_location (die, cu, &offset))
15179 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
15180 FIELD_BITSIZE (*fp) = 0;
15181 FIELD_TYPE (*fp) = die_type (die, cu);
15182 FIELD_NAME (*fp) = TYPE_NAME (fp->type);
15183 }
15184 else if (die->tag == DW_TAG_variant_part)
15185 {
15186 /* process_structure_scope will treat this DIE as a union. */
15187 process_structure_scope (die, cu);
15188
15189 /* The variant part is relative to the start of the enclosing
15190 structure. */
15191 SET_FIELD_BITPOS (*fp, 0);
15192 fp->type = get_die_type (die, cu);
15193 fp->artificial = 1;
15194 fp->name = "<<variant>>";
15195
15196 /* Normally a DW_TAG_variant_part won't have a size, but our
15197 representation requires one, so set it to the maximum of the
15198 child sizes, being sure to account for the offset at which
15199 each child is seen. */
15200 if (TYPE_LENGTH (fp->type) == 0)
15201 {
15202 unsigned max = 0;
15203 for (int i = 0; i < TYPE_NFIELDS (fp->type); ++i)
15204 {
15205 unsigned len = ((TYPE_FIELD_BITPOS (fp->type, i) + 7) / 8
15206 + TYPE_LENGTH (TYPE_FIELD_TYPE (fp->type, i)));
15207 if (len > max)
15208 max = len;
15209 }
15210 TYPE_LENGTH (fp->type) = max;
15211 }
15212 }
15213 else
15214 gdb_assert_not_reached ("missing case in dwarf2_add_field");
15215 }
15216
15217 /* Can the type given by DIE define another type? */
15218
15219 static bool
15220 type_can_define_types (const struct die_info *die)
15221 {
15222 switch (die->tag)
15223 {
15224 case DW_TAG_typedef:
15225 case DW_TAG_class_type:
15226 case DW_TAG_structure_type:
15227 case DW_TAG_union_type:
15228 case DW_TAG_enumeration_type:
15229 return true;
15230
15231 default:
15232 return false;
15233 }
15234 }
15235
15236 /* Add a type definition defined in the scope of the FIP's class. */
15237
15238 static void
15239 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15240 struct dwarf2_cu *cu)
15241 {
15242 struct decl_field fp;
15243 memset (&fp, 0, sizeof (fp));
15244
15245 gdb_assert (type_can_define_types (die));
15246
15247 /* Get name of field. NULL is okay here, meaning an anonymous type. */
15248 fp.name = dwarf2_name (die, cu);
15249 fp.type = read_type_die (die, cu);
15250
15251 /* Save accessibility. */
15252 enum dwarf_access_attribute accessibility;
15253 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15254 if (attr != NULL)
15255 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15256 else
15257 accessibility = dwarf2_default_access_attribute (die, cu);
15258 switch (accessibility)
15259 {
15260 case DW_ACCESS_public:
15261 /* The assumed value if neither private nor protected. */
15262 break;
15263 case DW_ACCESS_private:
15264 fp.is_private = 1;
15265 break;
15266 case DW_ACCESS_protected:
15267 fp.is_protected = 1;
15268 break;
15269 default:
15270 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
15271 }
15272
15273 if (die->tag == DW_TAG_typedef)
15274 fip->typedef_field_list.push_back (fp);
15275 else
15276 fip->nested_types_list.push_back (fp);
15277 }
15278
15279 /* Create the vector of fields, and attach it to the type. */
15280
15281 static void
15282 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
15283 struct dwarf2_cu *cu)
15284 {
15285 int nfields = fip->nfields;
15286
15287 /* Record the field count, allocate space for the array of fields,
15288 and create blank accessibility bitfields if necessary. */
15289 TYPE_NFIELDS (type) = nfields;
15290 TYPE_FIELDS (type) = (struct field *)
15291 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
15292
15293 if (fip->non_public_fields && cu->language != language_ada)
15294 {
15295 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15296
15297 TYPE_FIELD_PRIVATE_BITS (type) =
15298 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15299 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15300
15301 TYPE_FIELD_PROTECTED_BITS (type) =
15302 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15303 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15304
15305 TYPE_FIELD_IGNORE_BITS (type) =
15306 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15307 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
15308 }
15309
15310 /* If the type has baseclasses, allocate and clear a bit vector for
15311 TYPE_FIELD_VIRTUAL_BITS. */
15312 if (!fip->baseclasses.empty () && cu->language != language_ada)
15313 {
15314 int num_bytes = B_BYTES (fip->baseclasses.size ());
15315 unsigned char *pointer;
15316
15317 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15318 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
15319 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
15320 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15321 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
15322 }
15323
15324 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
15325 {
15326 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
15327
15328 for (int index = 0; index < nfields; ++index)
15329 {
15330 struct nextfield &field = fip->fields[index];
15331
15332 if (field.variant.is_discriminant)
15333 di->discriminant_index = index;
15334 else if (field.variant.default_branch)
15335 di->default_index = index;
15336 else
15337 di->discriminants[index] = field.variant.discriminant_value;
15338 }
15339 }
15340
15341 /* Copy the saved-up fields into the field vector. */
15342 for (int i = 0; i < nfields; ++i)
15343 {
15344 struct nextfield &field
15345 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15346 : fip->fields[i - fip->baseclasses.size ()]);
15347
15348 TYPE_FIELD (type, i) = field.field;
15349 switch (field.accessibility)
15350 {
15351 case DW_ACCESS_private:
15352 if (cu->language != language_ada)
15353 SET_TYPE_FIELD_PRIVATE (type, i);
15354 break;
15355
15356 case DW_ACCESS_protected:
15357 if (cu->language != language_ada)
15358 SET_TYPE_FIELD_PROTECTED (type, i);
15359 break;
15360
15361 case DW_ACCESS_public:
15362 break;
15363
15364 default:
15365 /* Unknown accessibility. Complain and treat it as public. */
15366 {
15367 complaint (_("unsupported accessibility %d"),
15368 field.accessibility);
15369 }
15370 break;
15371 }
15372 if (i < fip->baseclasses.size ())
15373 {
15374 switch (field.virtuality)
15375 {
15376 case DW_VIRTUALITY_virtual:
15377 case DW_VIRTUALITY_pure_virtual:
15378 if (cu->language == language_ada)
15379 error (_("unexpected virtuality in component of Ada type"));
15380 SET_TYPE_FIELD_VIRTUAL (type, i);
15381 break;
15382 }
15383 }
15384 }
15385 }
15386
15387 /* Return true if this member function is a constructor, false
15388 otherwise. */
15389
15390 static int
15391 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15392 {
15393 const char *fieldname;
15394 const char *type_name;
15395 int len;
15396
15397 if (die->parent == NULL)
15398 return 0;
15399
15400 if (die->parent->tag != DW_TAG_structure_type
15401 && die->parent->tag != DW_TAG_union_type
15402 && die->parent->tag != DW_TAG_class_type)
15403 return 0;
15404
15405 fieldname = dwarf2_name (die, cu);
15406 type_name = dwarf2_name (die->parent, cu);
15407 if (fieldname == NULL || type_name == NULL)
15408 return 0;
15409
15410 len = strlen (fieldname);
15411 return (strncmp (fieldname, type_name, len) == 0
15412 && (type_name[len] == '\0' || type_name[len] == '<'));
15413 }
15414
15415 /* Check if the given VALUE is a recognized enum
15416 dwarf_defaulted_attribute constant according to DWARF5 spec,
15417 Table 7.24. */
15418
15419 static bool
15420 is_valid_DW_AT_defaulted (ULONGEST value)
15421 {
15422 switch (value)
15423 {
15424 case DW_DEFAULTED_no:
15425 case DW_DEFAULTED_in_class:
15426 case DW_DEFAULTED_out_of_class:
15427 return true;
15428 }
15429
15430 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
15431 return false;
15432 }
15433
15434 /* Add a member function to the proper fieldlist. */
15435
15436 static void
15437 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
15438 struct type *type, struct dwarf2_cu *cu)
15439 {
15440 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15441 struct attribute *attr;
15442 int i;
15443 struct fnfieldlist *flp = nullptr;
15444 struct fn_field *fnp;
15445 const char *fieldname;
15446 struct type *this_type;
15447 enum dwarf_access_attribute accessibility;
15448
15449 if (cu->language == language_ada)
15450 error (_("unexpected member function in Ada type"));
15451
15452 /* Get name of member function. */
15453 fieldname = dwarf2_name (die, cu);
15454 if (fieldname == NULL)
15455 return;
15456
15457 /* Look up member function name in fieldlist. */
15458 for (i = 0; i < fip->fnfieldlists.size (); i++)
15459 {
15460 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
15461 {
15462 flp = &fip->fnfieldlists[i];
15463 break;
15464 }
15465 }
15466
15467 /* Create a new fnfieldlist if necessary. */
15468 if (flp == nullptr)
15469 {
15470 fip->fnfieldlists.emplace_back ();
15471 flp = &fip->fnfieldlists.back ();
15472 flp->name = fieldname;
15473 i = fip->fnfieldlists.size () - 1;
15474 }
15475
15476 /* Create a new member function field and add it to the vector of
15477 fnfieldlists. */
15478 flp->fnfields.emplace_back ();
15479 fnp = &flp->fnfields.back ();
15480
15481 /* Delay processing of the physname until later. */
15482 if (cu->language == language_cplus)
15483 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15484 die, cu);
15485 else
15486 {
15487 const char *physname = dwarf2_physname (fieldname, die, cu);
15488 fnp->physname = physname ? physname : "";
15489 }
15490
15491 fnp->type = alloc_type (objfile);
15492 this_type = read_type_die (die, cu);
15493 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
15494 {
15495 int nparams = TYPE_NFIELDS (this_type);
15496
15497 /* TYPE is the domain of this method, and THIS_TYPE is the type
15498 of the method itself (TYPE_CODE_METHOD). */
15499 smash_to_method_type (fnp->type, type,
15500 TYPE_TARGET_TYPE (this_type),
15501 TYPE_FIELDS (this_type),
15502 TYPE_NFIELDS (this_type),
15503 TYPE_VARARGS (this_type));
15504
15505 /* Handle static member functions.
15506 Dwarf2 has no clean way to discern C++ static and non-static
15507 member functions. G++ helps GDB by marking the first
15508 parameter for non-static member functions (which is the this
15509 pointer) as artificial. We obtain this information from
15510 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15511 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15512 fnp->voffset = VOFFSET_STATIC;
15513 }
15514 else
15515 complaint (_("member function type missing for '%s'"),
15516 dwarf2_full_name (fieldname, die, cu));
15517
15518 /* Get fcontext from DW_AT_containing_type if present. */
15519 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15520 fnp->fcontext = die_containing_type (die, cu);
15521
15522 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15523 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15524
15525 /* Get accessibility. */
15526 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15527 if (attr != nullptr)
15528 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15529 else
15530 accessibility = dwarf2_default_access_attribute (die, cu);
15531 switch (accessibility)
15532 {
15533 case DW_ACCESS_private:
15534 fnp->is_private = 1;
15535 break;
15536 case DW_ACCESS_protected:
15537 fnp->is_protected = 1;
15538 break;
15539 }
15540
15541 /* Check for artificial methods. */
15542 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15543 if (attr && DW_UNSND (attr) != 0)
15544 fnp->is_artificial = 1;
15545
15546 /* Check for defaulted methods. */
15547 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
15548 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
15549 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
15550
15551 /* Check for deleted methods. */
15552 attr = dwarf2_attr (die, DW_AT_deleted, cu);
15553 if (attr != nullptr && DW_UNSND (attr) != 0)
15554 fnp->is_deleted = 1;
15555
15556 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15557
15558 /* Get index in virtual function table if it is a virtual member
15559 function. For older versions of GCC, this is an offset in the
15560 appropriate virtual table, as specified by DW_AT_containing_type.
15561 For everyone else, it is an expression to be evaluated relative
15562 to the object address. */
15563
15564 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15565 if (attr != nullptr)
15566 {
15567 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
15568 {
15569 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15570 {
15571 /* Old-style GCC. */
15572 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15573 }
15574 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15575 || (DW_BLOCK (attr)->size > 1
15576 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15577 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15578 {
15579 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15580 if ((fnp->voffset % cu->header.addr_size) != 0)
15581 dwarf2_complex_location_expr_complaint ();
15582 else
15583 fnp->voffset /= cu->header.addr_size;
15584 fnp->voffset += 2;
15585 }
15586 else
15587 dwarf2_complex_location_expr_complaint ();
15588
15589 if (!fnp->fcontext)
15590 {
15591 /* If there is no `this' field and no DW_AT_containing_type,
15592 we cannot actually find a base class context for the
15593 vtable! */
15594 if (TYPE_NFIELDS (this_type) == 0
15595 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15596 {
15597 complaint (_("cannot determine context for virtual member "
15598 "function \"%s\" (offset %s)"),
15599 fieldname, sect_offset_str (die->sect_off));
15600 }
15601 else
15602 {
15603 fnp->fcontext
15604 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15605 }
15606 }
15607 }
15608 else if (attr_form_is_section_offset (attr))
15609 {
15610 dwarf2_complex_location_expr_complaint ();
15611 }
15612 else
15613 {
15614 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15615 fieldname);
15616 }
15617 }
15618 else
15619 {
15620 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15621 if (attr && DW_UNSND (attr))
15622 {
15623 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15624 complaint (_("Member function \"%s\" (offset %s) is virtual "
15625 "but the vtable offset is not specified"),
15626 fieldname, sect_offset_str (die->sect_off));
15627 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15628 TYPE_CPLUS_DYNAMIC (type) = 1;
15629 }
15630 }
15631 }
15632
15633 /* Create the vector of member function fields, and attach it to the type. */
15634
15635 static void
15636 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15637 struct dwarf2_cu *cu)
15638 {
15639 if (cu->language == language_ada)
15640 error (_("unexpected member functions in Ada type"));
15641
15642 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15643 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15644 TYPE_ALLOC (type,
15645 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
15646
15647 for (int i = 0; i < fip->fnfieldlists.size (); i++)
15648 {
15649 struct fnfieldlist &nf = fip->fnfieldlists[i];
15650 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15651
15652 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15653 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
15654 fn_flp->fn_fields = (struct fn_field *)
15655 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15656
15657 for (int k = 0; k < nf.fnfields.size (); ++k)
15658 fn_flp->fn_fields[k] = nf.fnfields[k];
15659 }
15660
15661 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
15662 }
15663
15664 /* Returns non-zero if NAME is the name of a vtable member in CU's
15665 language, zero otherwise. */
15666 static int
15667 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15668 {
15669 static const char vptr[] = "_vptr";
15670
15671 /* Look for the C++ form of the vtable. */
15672 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15673 return 1;
15674
15675 return 0;
15676 }
15677
15678 /* GCC outputs unnamed structures that are really pointers to member
15679 functions, with the ABI-specified layout. If TYPE describes
15680 such a structure, smash it into a member function type.
15681
15682 GCC shouldn't do this; it should just output pointer to member DIEs.
15683 This is GCC PR debug/28767. */
15684
15685 static void
15686 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15687 {
15688 struct type *pfn_type, *self_type, *new_type;
15689
15690 /* Check for a structure with no name and two children. */
15691 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
15692 return;
15693
15694 /* Check for __pfn and __delta members. */
15695 if (TYPE_FIELD_NAME (type, 0) == NULL
15696 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15697 || TYPE_FIELD_NAME (type, 1) == NULL
15698 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15699 return;
15700
15701 /* Find the type of the method. */
15702 pfn_type = TYPE_FIELD_TYPE (type, 0);
15703 if (pfn_type == NULL
15704 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
15705 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
15706 return;
15707
15708 /* Look for the "this" argument. */
15709 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15710 if (TYPE_NFIELDS (pfn_type) == 0
15711 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
15712 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
15713 return;
15714
15715 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
15716 new_type = alloc_type (objfile);
15717 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15718 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
15719 TYPE_VARARGS (pfn_type));
15720 smash_to_methodptr_type (type, new_type);
15721 }
15722
15723 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
15724 appropriate error checking and issuing complaints if there is a
15725 problem. */
15726
15727 static ULONGEST
15728 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15729 {
15730 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15731
15732 if (attr == nullptr)
15733 return 0;
15734
15735 if (!attr_form_is_constant (attr))
15736 {
15737 complaint (_("DW_AT_alignment must have constant form"
15738 " - DIE at %s [in module %s]"),
15739 sect_offset_str (die->sect_off),
15740 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15741 return 0;
15742 }
15743
15744 ULONGEST align;
15745 if (attr->form == DW_FORM_sdata)
15746 {
15747 LONGEST val = DW_SND (attr);
15748 if (val < 0)
15749 {
15750 complaint (_("DW_AT_alignment value must not be negative"
15751 " - DIE at %s [in module %s]"),
15752 sect_offset_str (die->sect_off),
15753 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15754 return 0;
15755 }
15756 align = val;
15757 }
15758 else
15759 align = DW_UNSND (attr);
15760
15761 if (align == 0)
15762 {
15763 complaint (_("DW_AT_alignment value must not be zero"
15764 " - DIE at %s [in module %s]"),
15765 sect_offset_str (die->sect_off),
15766 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15767 return 0;
15768 }
15769 if ((align & (align - 1)) != 0)
15770 {
15771 complaint (_("DW_AT_alignment value must be a power of 2"
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 return 0;
15776 }
15777
15778 return align;
15779 }
15780
15781 /* If the DIE has a DW_AT_alignment attribute, use its value to set
15782 the alignment for TYPE. */
15783
15784 static void
15785 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15786 struct type *type)
15787 {
15788 if (!set_type_align (type, get_alignment (cu, die)))
15789 complaint (_("DW_AT_alignment value too large"
15790 " - DIE at %s [in module %s]"),
15791 sect_offset_str (die->sect_off),
15792 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15793 }
15794
15795 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15796 constant for a type, according to DWARF5 spec, Table 5.5. */
15797
15798 static bool
15799 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15800 {
15801 switch (value)
15802 {
15803 case DW_CC_normal:
15804 case DW_CC_pass_by_reference:
15805 case DW_CC_pass_by_value:
15806 return true;
15807
15808 default:
15809 complaint (_("unrecognized DW_AT_calling_convention value "
15810 "(%s) for a type"), pulongest (value));
15811 return false;
15812 }
15813 }
15814
15815 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15816 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15817 also according to GNU-specific values (see include/dwarf2.h). */
15818
15819 static bool
15820 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15821 {
15822 switch (value)
15823 {
15824 case DW_CC_normal:
15825 case DW_CC_program:
15826 case DW_CC_nocall:
15827 return true;
15828
15829 case DW_CC_GNU_renesas_sh:
15830 case DW_CC_GNU_borland_fastcall_i386:
15831 case DW_CC_GDB_IBM_OpenCL:
15832 return true;
15833
15834 default:
15835 complaint (_("unrecognized DW_AT_calling_convention value "
15836 "(%s) for a subroutine"), pulongest (value));
15837 return false;
15838 }
15839 }
15840
15841 /* Called when we find the DIE that starts a structure or union scope
15842 (definition) to create a type for the structure or union. Fill in
15843 the type's name and general properties; the members will not be
15844 processed until process_structure_scope. A symbol table entry for
15845 the type will also not be done until process_structure_scope (assuming
15846 the type has a name).
15847
15848 NOTE: we need to call these functions regardless of whether or not the
15849 DIE has a DW_AT_name attribute, since it might be an anonymous
15850 structure or union. This gets the type entered into our set of
15851 user defined types. */
15852
15853 static struct type *
15854 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15855 {
15856 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15857 struct type *type;
15858 struct attribute *attr;
15859 const char *name;
15860
15861 /* If the definition of this type lives in .debug_types, read that type.
15862 Don't follow DW_AT_specification though, that will take us back up
15863 the chain and we want to go down. */
15864 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
15865 if (attr != nullptr)
15866 {
15867 type = get_DW_AT_signature_type (die, attr, cu);
15868
15869 /* The type's CU may not be the same as CU.
15870 Ensure TYPE is recorded with CU in die_type_hash. */
15871 return set_die_type (die, type, cu);
15872 }
15873
15874 type = alloc_type (objfile);
15875 INIT_CPLUS_SPECIFIC (type);
15876
15877 name = dwarf2_name (die, cu);
15878 if (name != NULL)
15879 {
15880 if (cu->language == language_cplus
15881 || cu->language == language_d
15882 || cu->language == language_rust)
15883 {
15884 const char *full_name = dwarf2_full_name (name, die, cu);
15885
15886 /* dwarf2_full_name might have already finished building the DIE's
15887 type. If so, there is no need to continue. */
15888 if (get_die_type (die, cu) != NULL)
15889 return get_die_type (die, cu);
15890
15891 TYPE_NAME (type) = full_name;
15892 }
15893 else
15894 {
15895 /* The name is already allocated along with this objfile, so
15896 we don't need to duplicate it for the type. */
15897 TYPE_NAME (type) = name;
15898 }
15899 }
15900
15901 if (die->tag == DW_TAG_structure_type)
15902 {
15903 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15904 }
15905 else if (die->tag == DW_TAG_union_type)
15906 {
15907 TYPE_CODE (type) = TYPE_CODE_UNION;
15908 }
15909 else if (die->tag == DW_TAG_variant_part)
15910 {
15911 TYPE_CODE (type) = TYPE_CODE_UNION;
15912 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
15913 }
15914 else
15915 {
15916 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15917 }
15918
15919 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15920 TYPE_DECLARED_CLASS (type) = 1;
15921
15922 /* Store the calling convention in the type if it's available in
15923 the die. Otherwise the calling convention remains set to
15924 the default value DW_CC_normal. */
15925 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15926 if (attr != nullptr
15927 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15928 {
15929 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15930 TYPE_CPLUS_CALLING_CONVENTION (type)
15931 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15932 }
15933
15934 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15935 if (attr != nullptr)
15936 {
15937 if (attr_form_is_constant (attr))
15938 TYPE_LENGTH (type) = DW_UNSND (attr);
15939 else
15940 {
15941 /* For the moment, dynamic type sizes are not supported
15942 by GDB's struct type. The actual size is determined
15943 on-demand when resolving the type of a given object,
15944 so set the type's length to zero for now. Otherwise,
15945 we record an expression as the length, and that expression
15946 could lead to a very large value, which could eventually
15947 lead to us trying to allocate that much memory when creating
15948 a value of that type. */
15949 TYPE_LENGTH (type) = 0;
15950 }
15951 }
15952 else
15953 {
15954 TYPE_LENGTH (type) = 0;
15955 }
15956
15957 maybe_set_alignment (cu, die, type);
15958
15959 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15960 {
15961 /* ICC<14 does not output the required DW_AT_declaration on
15962 incomplete types, but gives them a size of zero. */
15963 TYPE_STUB (type) = 1;
15964 }
15965 else
15966 TYPE_STUB_SUPPORTED (type) = 1;
15967
15968 if (die_is_declaration (die, cu))
15969 TYPE_STUB (type) = 1;
15970 else if (attr == NULL && die->child == NULL
15971 && producer_is_realview (cu->producer))
15972 /* RealView does not output the required DW_AT_declaration
15973 on incomplete types. */
15974 TYPE_STUB (type) = 1;
15975
15976 /* We need to add the type field to the die immediately so we don't
15977 infinitely recurse when dealing with pointers to the structure
15978 type within the structure itself. */
15979 set_die_type (die, type, cu);
15980
15981 /* set_die_type should be already done. */
15982 set_descriptive_type (type, die, cu);
15983
15984 return type;
15985 }
15986
15987 /* A helper for process_structure_scope that handles a single member
15988 DIE. */
15989
15990 static void
15991 handle_struct_member_die (struct die_info *child_die, struct type *type,
15992 struct field_info *fi,
15993 std::vector<struct symbol *> *template_args,
15994 struct dwarf2_cu *cu)
15995 {
15996 if (child_die->tag == DW_TAG_member
15997 || child_die->tag == DW_TAG_variable
15998 || child_die->tag == DW_TAG_variant_part)
15999 {
16000 /* NOTE: carlton/2002-11-05: A C++ static data member
16001 should be a DW_TAG_member that is a declaration, but
16002 all versions of G++ as of this writing (so through at
16003 least 3.2.1) incorrectly generate DW_TAG_variable
16004 tags for them instead. */
16005 dwarf2_add_field (fi, child_die, cu);
16006 }
16007 else if (child_die->tag == DW_TAG_subprogram)
16008 {
16009 /* Rust doesn't have member functions in the C++ sense.
16010 However, it does emit ordinary functions as children
16011 of a struct DIE. */
16012 if (cu->language == language_rust)
16013 read_func_scope (child_die, cu);
16014 else
16015 {
16016 /* C++ member function. */
16017 dwarf2_add_member_fn (fi, child_die, type, cu);
16018 }
16019 }
16020 else if (child_die->tag == DW_TAG_inheritance)
16021 {
16022 /* C++ base class field. */
16023 dwarf2_add_field (fi, child_die, cu);
16024 }
16025 else if (type_can_define_types (child_die))
16026 dwarf2_add_type_defn (fi, child_die, cu);
16027 else if (child_die->tag == DW_TAG_template_type_param
16028 || child_die->tag == DW_TAG_template_value_param)
16029 {
16030 struct symbol *arg = new_symbol (child_die, NULL, cu);
16031
16032 if (arg != NULL)
16033 template_args->push_back (arg);
16034 }
16035 else if (child_die->tag == DW_TAG_variant)
16036 {
16037 /* In a variant we want to get the discriminant and also add a
16038 field for our sole member child. */
16039 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
16040
16041 for (die_info *variant_child = child_die->child;
16042 variant_child != NULL;
16043 variant_child = sibling_die (variant_child))
16044 {
16045 if (variant_child->tag == DW_TAG_member)
16046 {
16047 handle_struct_member_die (variant_child, type, fi,
16048 template_args, cu);
16049 /* Only handle the one. */
16050 break;
16051 }
16052 }
16053
16054 /* We don't handle this but we might as well report it if we see
16055 it. */
16056 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
16057 complaint (_("DW_AT_discr_list is not supported yet"
16058 " - DIE at %s [in module %s]"),
16059 sect_offset_str (child_die->sect_off),
16060 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16061
16062 /* The first field was just added, so we can stash the
16063 discriminant there. */
16064 gdb_assert (!fi->fields.empty ());
16065 if (discr == NULL)
16066 fi->fields.back ().variant.default_branch = true;
16067 else
16068 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
16069 }
16070 }
16071
16072 /* Finish creating a structure or union type, including filling in
16073 its members and creating a symbol for it. */
16074
16075 static void
16076 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
16077 {
16078 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16079 struct die_info *child_die;
16080 struct type *type;
16081
16082 type = get_die_type (die, cu);
16083 if (type == NULL)
16084 type = read_structure_type (die, cu);
16085
16086 /* When reading a DW_TAG_variant_part, we need to notice when we
16087 read the discriminant member, so we can record it later in the
16088 discriminant_info. */
16089 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
16090 sect_offset discr_offset {};
16091 bool has_template_parameters = false;
16092
16093 if (is_variant_part)
16094 {
16095 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
16096 if (discr == NULL)
16097 {
16098 /* Maybe it's a univariant form, an extension we support.
16099 In this case arrange not to check the offset. */
16100 is_variant_part = false;
16101 }
16102 else if (attr_form_is_ref (discr))
16103 {
16104 struct dwarf2_cu *target_cu = cu;
16105 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
16106
16107 discr_offset = target_die->sect_off;
16108 }
16109 else
16110 {
16111 complaint (_("DW_AT_discr does not have DIE reference form"
16112 " - DIE at %s [in module %s]"),
16113 sect_offset_str (die->sect_off),
16114 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16115 is_variant_part = false;
16116 }
16117 }
16118
16119 if (die->child != NULL && ! die_is_declaration (die, cu))
16120 {
16121 struct field_info fi;
16122 std::vector<struct symbol *> template_args;
16123
16124 child_die = die->child;
16125
16126 while (child_die && child_die->tag)
16127 {
16128 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
16129
16130 if (is_variant_part && discr_offset == child_die->sect_off)
16131 fi.fields.back ().variant.is_discriminant = true;
16132
16133 child_die = sibling_die (child_die);
16134 }
16135
16136 /* Attach template arguments to type. */
16137 if (!template_args.empty ())
16138 {
16139 has_template_parameters = true;
16140 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16141 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
16142 TYPE_TEMPLATE_ARGUMENTS (type)
16143 = XOBNEWVEC (&objfile->objfile_obstack,
16144 struct symbol *,
16145 TYPE_N_TEMPLATE_ARGUMENTS (type));
16146 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
16147 template_args.data (),
16148 (TYPE_N_TEMPLATE_ARGUMENTS (type)
16149 * sizeof (struct symbol *)));
16150 }
16151
16152 /* Attach fields and member functions to the type. */
16153 if (fi.nfields)
16154 dwarf2_attach_fields_to_type (&fi, type, cu);
16155 if (!fi.fnfieldlists.empty ())
16156 {
16157 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
16158
16159 /* Get the type which refers to the base class (possibly this
16160 class itself) which contains the vtable pointer for the current
16161 class from the DW_AT_containing_type attribute. This use of
16162 DW_AT_containing_type is a GNU extension. */
16163
16164 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
16165 {
16166 struct type *t = die_containing_type (die, cu);
16167
16168 set_type_vptr_basetype (type, t);
16169 if (type == t)
16170 {
16171 int i;
16172
16173 /* Our own class provides vtbl ptr. */
16174 for (i = TYPE_NFIELDS (t) - 1;
16175 i >= TYPE_N_BASECLASSES (t);
16176 --i)
16177 {
16178 const char *fieldname = TYPE_FIELD_NAME (t, i);
16179
16180 if (is_vtable_name (fieldname, cu))
16181 {
16182 set_type_vptr_fieldno (type, i);
16183 break;
16184 }
16185 }
16186
16187 /* Complain if virtual function table field not found. */
16188 if (i < TYPE_N_BASECLASSES (t))
16189 complaint (_("virtual function table pointer "
16190 "not found when defining class '%s'"),
16191 TYPE_NAME (type) ? TYPE_NAME (type) : "");
16192 }
16193 else
16194 {
16195 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
16196 }
16197 }
16198 else if (cu->producer
16199 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
16200 {
16201 /* The IBM XLC compiler does not provide direct indication
16202 of the containing type, but the vtable pointer is
16203 always named __vfp. */
16204
16205 int i;
16206
16207 for (i = TYPE_NFIELDS (type) - 1;
16208 i >= TYPE_N_BASECLASSES (type);
16209 --i)
16210 {
16211 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16212 {
16213 set_type_vptr_fieldno (type, i);
16214 set_type_vptr_basetype (type, type);
16215 break;
16216 }
16217 }
16218 }
16219 }
16220
16221 /* Copy fi.typedef_field_list linked list elements content into the
16222 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
16223 if (!fi.typedef_field_list.empty ())
16224 {
16225 int count = fi.typedef_field_list.size ();
16226
16227 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16228 TYPE_TYPEDEF_FIELD_ARRAY (type)
16229 = ((struct decl_field *)
16230 TYPE_ALLOC (type,
16231 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
16232 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
16233
16234 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
16235 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
16236 }
16237
16238 /* Copy fi.nested_types_list linked list elements content into the
16239 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
16240 if (!fi.nested_types_list.empty () && cu->language != language_ada)
16241 {
16242 int count = fi.nested_types_list.size ();
16243
16244 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16245 TYPE_NESTED_TYPES_ARRAY (type)
16246 = ((struct decl_field *)
16247 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
16248 TYPE_NESTED_TYPES_COUNT (type) = count;
16249
16250 for (int i = 0; i < fi.nested_types_list.size (); ++i)
16251 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
16252 }
16253 }
16254
16255 quirk_gcc_member_function_pointer (type, objfile);
16256 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
16257 cu->rust_unions.push_back (type);
16258
16259 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16260 snapshots) has been known to create a die giving a declaration
16261 for a class that has, as a child, a die giving a definition for a
16262 nested class. So we have to process our children even if the
16263 current die is a declaration. Normally, of course, a declaration
16264 won't have any children at all. */
16265
16266 child_die = die->child;
16267
16268 while (child_die != NULL && child_die->tag)
16269 {
16270 if (child_die->tag == DW_TAG_member
16271 || child_die->tag == DW_TAG_variable
16272 || child_die->tag == DW_TAG_inheritance
16273 || child_die->tag == DW_TAG_template_value_param
16274 || child_die->tag == DW_TAG_template_type_param)
16275 {
16276 /* Do nothing. */
16277 }
16278 else
16279 process_die (child_die, cu);
16280
16281 child_die = sibling_die (child_die);
16282 }
16283
16284 /* Do not consider external references. According to the DWARF standard,
16285 these DIEs are identified by the fact that they have no byte_size
16286 attribute, and a declaration attribute. */
16287 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16288 || !die_is_declaration (die, cu))
16289 {
16290 struct symbol *sym = new_symbol (die, type, cu);
16291
16292 if (has_template_parameters)
16293 {
16294 struct symtab *symtab;
16295 if (sym != nullptr)
16296 symtab = symbol_symtab (sym);
16297 else if (cu->line_header != nullptr)
16298 {
16299 /* Any related symtab will do. */
16300 symtab
16301 = cu->line_header->file_names ()[0].symtab;
16302 }
16303 else
16304 {
16305 symtab = nullptr;
16306 complaint (_("could not find suitable "
16307 "symtab for template parameter"
16308 " - DIE at %s [in module %s]"),
16309 sect_offset_str (die->sect_off),
16310 objfile_name (objfile));
16311 }
16312
16313 if (symtab != nullptr)
16314 {
16315 /* Make sure that the symtab is set on the new symbols.
16316 Even though they don't appear in this symtab directly,
16317 other parts of gdb assume that symbols do, and this is
16318 reasonably true. */
16319 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
16320 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
16321 }
16322 }
16323 }
16324 }
16325
16326 /* Assuming DIE is an enumeration type, and TYPE is its associated type,
16327 update TYPE using some information only available in DIE's children. */
16328
16329 static void
16330 update_enumeration_type_from_children (struct die_info *die,
16331 struct type *type,
16332 struct dwarf2_cu *cu)
16333 {
16334 struct die_info *child_die;
16335 int unsigned_enum = 1;
16336 int flag_enum = 1;
16337 ULONGEST mask = 0;
16338
16339 auto_obstack obstack;
16340
16341 for (child_die = die->child;
16342 child_die != NULL && child_die->tag;
16343 child_die = sibling_die (child_die))
16344 {
16345 struct attribute *attr;
16346 LONGEST value;
16347 const gdb_byte *bytes;
16348 struct dwarf2_locexpr_baton *baton;
16349 const char *name;
16350
16351 if (child_die->tag != DW_TAG_enumerator)
16352 continue;
16353
16354 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16355 if (attr == NULL)
16356 continue;
16357
16358 name = dwarf2_name (child_die, cu);
16359 if (name == NULL)
16360 name = "<anonymous enumerator>";
16361
16362 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16363 &value, &bytes, &baton);
16364 if (value < 0)
16365 {
16366 unsigned_enum = 0;
16367 flag_enum = 0;
16368 }
16369 else if ((mask & value) != 0)
16370 flag_enum = 0;
16371 else
16372 mask |= value;
16373
16374 /* If we already know that the enum type is neither unsigned, nor
16375 a flag type, no need to look at the rest of the enumerates. */
16376 if (!unsigned_enum && !flag_enum)
16377 break;
16378 }
16379
16380 if (unsigned_enum)
16381 TYPE_UNSIGNED (type) = 1;
16382 if (flag_enum)
16383 TYPE_FLAG_ENUM (type) = 1;
16384 }
16385
16386 /* Given a DW_AT_enumeration_type die, set its type. We do not
16387 complete the type's fields yet, or create any symbols. */
16388
16389 static struct type *
16390 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
16391 {
16392 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16393 struct type *type;
16394 struct attribute *attr;
16395 const char *name;
16396
16397 /* If the definition of this type lives in .debug_types, read that type.
16398 Don't follow DW_AT_specification though, that will take us back up
16399 the chain and we want to go down. */
16400 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
16401 if (attr != nullptr)
16402 {
16403 type = get_DW_AT_signature_type (die, attr, cu);
16404
16405 /* The type's CU may not be the same as CU.
16406 Ensure TYPE is recorded with CU in die_type_hash. */
16407 return set_die_type (die, type, cu);
16408 }
16409
16410 type = alloc_type (objfile);
16411
16412 TYPE_CODE (type) = TYPE_CODE_ENUM;
16413 name = dwarf2_full_name (NULL, die, cu);
16414 if (name != NULL)
16415 TYPE_NAME (type) = name;
16416
16417 attr = dwarf2_attr (die, DW_AT_type, cu);
16418 if (attr != NULL)
16419 {
16420 struct type *underlying_type = die_type (die, cu);
16421
16422 TYPE_TARGET_TYPE (type) = underlying_type;
16423 }
16424
16425 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16426 if (attr != nullptr)
16427 {
16428 TYPE_LENGTH (type) = DW_UNSND (attr);
16429 }
16430 else
16431 {
16432 TYPE_LENGTH (type) = 0;
16433 }
16434
16435 maybe_set_alignment (cu, die, type);
16436
16437 /* The enumeration DIE can be incomplete. In Ada, any type can be
16438 declared as private in the package spec, and then defined only
16439 inside the package body. Such types are known as Taft Amendment
16440 Types. When another package uses such a type, an incomplete DIE
16441 may be generated by the compiler. */
16442 if (die_is_declaration (die, cu))
16443 TYPE_STUB (type) = 1;
16444
16445 /* Finish the creation of this type by using the enum's children.
16446 We must call this even when the underlying type has been provided
16447 so that we can determine if we're looking at a "flag" enum. */
16448 update_enumeration_type_from_children (die, type, cu);
16449
16450 /* If this type has an underlying type that is not a stub, then we
16451 may use its attributes. We always use the "unsigned" attribute
16452 in this situation, because ordinarily we guess whether the type
16453 is unsigned -- but the guess can be wrong and the underlying type
16454 can tell us the reality. However, we defer to a local size
16455 attribute if one exists, because this lets the compiler override
16456 the underlying type if needed. */
16457 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16458 {
16459 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
16460 if (TYPE_LENGTH (type) == 0)
16461 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
16462 if (TYPE_RAW_ALIGN (type) == 0
16463 && TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)) != 0)
16464 set_type_align (type, TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)));
16465 }
16466
16467 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16468
16469 return set_die_type (die, type, cu);
16470 }
16471
16472 /* Given a pointer to a die which begins an enumeration, process all
16473 the dies that define the members of the enumeration, and create the
16474 symbol for the enumeration type.
16475
16476 NOTE: We reverse the order of the element list. */
16477
16478 static void
16479 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16480 {
16481 struct type *this_type;
16482
16483 this_type = get_die_type (die, cu);
16484 if (this_type == NULL)
16485 this_type = read_enumeration_type (die, cu);
16486
16487 if (die->child != NULL)
16488 {
16489 struct die_info *child_die;
16490 struct symbol *sym;
16491 std::vector<struct field> fields;
16492 const char *name;
16493
16494 child_die = die->child;
16495 while (child_die && child_die->tag)
16496 {
16497 if (child_die->tag != DW_TAG_enumerator)
16498 {
16499 process_die (child_die, cu);
16500 }
16501 else
16502 {
16503 name = dwarf2_name (child_die, cu);
16504 if (name)
16505 {
16506 sym = new_symbol (child_die, this_type, cu);
16507
16508 fields.emplace_back ();
16509 struct field &field = fields.back ();
16510
16511 FIELD_NAME (field) = sym->linkage_name ();
16512 FIELD_TYPE (field) = NULL;
16513 SET_FIELD_ENUMVAL (field, SYMBOL_VALUE (sym));
16514 FIELD_BITSIZE (field) = 0;
16515 }
16516 }
16517
16518 child_die = sibling_die (child_die);
16519 }
16520
16521 if (!fields.empty ())
16522 {
16523 TYPE_NFIELDS (this_type) = fields.size ();
16524 TYPE_FIELDS (this_type) = (struct field *)
16525 TYPE_ALLOC (this_type, sizeof (struct field) * fields.size ());
16526 memcpy (TYPE_FIELDS (this_type), fields.data (),
16527 sizeof (struct field) * fields.size ());
16528 }
16529 }
16530
16531 /* If we are reading an enum from a .debug_types unit, and the enum
16532 is a declaration, and the enum is not the signatured type in the
16533 unit, then we do not want to add a symbol for it. Adding a
16534 symbol would in some cases obscure the true definition of the
16535 enum, giving users an incomplete type when the definition is
16536 actually available. Note that we do not want to do this for all
16537 enums which are just declarations, because C++0x allows forward
16538 enum declarations. */
16539 if (cu->per_cu->is_debug_types
16540 && die_is_declaration (die, cu))
16541 {
16542 struct signatured_type *sig_type;
16543
16544 sig_type = (struct signatured_type *) cu->per_cu;
16545 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16546 if (sig_type->type_offset_in_section != die->sect_off)
16547 return;
16548 }
16549
16550 new_symbol (die, this_type, cu);
16551 }
16552
16553 /* Extract all information from a DW_TAG_array_type DIE and put it in
16554 the DIE's type field. For now, this only handles one dimensional
16555 arrays. */
16556
16557 static struct type *
16558 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
16559 {
16560 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16561 struct die_info *child_die;
16562 struct type *type;
16563 struct type *element_type, *range_type, *index_type;
16564 struct attribute *attr;
16565 const char *name;
16566 struct dynamic_prop *byte_stride_prop = NULL;
16567 unsigned int bit_stride = 0;
16568
16569 element_type = die_type (die, cu);
16570
16571 /* The die_type call above may have already set the type for this DIE. */
16572 type = get_die_type (die, cu);
16573 if (type)
16574 return type;
16575
16576 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16577 if (attr != NULL)
16578 {
16579 int stride_ok;
16580 struct type *prop_type
16581 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
16582
16583 byte_stride_prop
16584 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16585 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16586 prop_type);
16587 if (!stride_ok)
16588 {
16589 complaint (_("unable to read array DW_AT_byte_stride "
16590 " - DIE at %s [in module %s]"),
16591 sect_offset_str (die->sect_off),
16592 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16593 /* Ignore this attribute. We will likely not be able to print
16594 arrays of this type correctly, but there is little we can do
16595 to help if we cannot read the attribute's value. */
16596 byte_stride_prop = NULL;
16597 }
16598 }
16599
16600 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16601 if (attr != NULL)
16602 bit_stride = DW_UNSND (attr);
16603
16604 /* Irix 6.2 native cc creates array types without children for
16605 arrays with unspecified length. */
16606 if (die->child == NULL)
16607 {
16608 index_type = objfile_type (objfile)->builtin_int;
16609 range_type = create_static_range_type (NULL, index_type, 0, -1);
16610 type = create_array_type_with_stride (NULL, element_type, range_type,
16611 byte_stride_prop, bit_stride);
16612 return set_die_type (die, type, cu);
16613 }
16614
16615 std::vector<struct type *> range_types;
16616 child_die = die->child;
16617 while (child_die && child_die->tag)
16618 {
16619 if (child_die->tag == DW_TAG_subrange_type)
16620 {
16621 struct type *child_type = read_type_die (child_die, cu);
16622
16623 if (child_type != NULL)
16624 {
16625 /* The range type was succesfully read. Save it for the
16626 array type creation. */
16627 range_types.push_back (child_type);
16628 }
16629 }
16630 child_die = sibling_die (child_die);
16631 }
16632
16633 /* Dwarf2 dimensions are output from left to right, create the
16634 necessary array types in backwards order. */
16635
16636 type = element_type;
16637
16638 if (read_array_order (die, cu) == DW_ORD_col_major)
16639 {
16640 int i = 0;
16641
16642 while (i < range_types.size ())
16643 type = create_array_type_with_stride (NULL, type, range_types[i++],
16644 byte_stride_prop, bit_stride);
16645 }
16646 else
16647 {
16648 size_t ndim = range_types.size ();
16649 while (ndim-- > 0)
16650 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16651 byte_stride_prop, bit_stride);
16652 }
16653
16654 /* Understand Dwarf2 support for vector types (like they occur on
16655 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16656 array type. This is not part of the Dwarf2/3 standard yet, but a
16657 custom vendor extension. The main difference between a regular
16658 array and the vector variant is that vectors are passed by value
16659 to functions. */
16660 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
16661 if (attr != nullptr)
16662 make_vector_type (type);
16663
16664 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16665 implementation may choose to implement triple vectors using this
16666 attribute. */
16667 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16668 if (attr != nullptr)
16669 {
16670 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16671 TYPE_LENGTH (type) = DW_UNSND (attr);
16672 else
16673 complaint (_("DW_AT_byte_size for array type smaller "
16674 "than the total size of elements"));
16675 }
16676
16677 name = dwarf2_name (die, cu);
16678 if (name)
16679 TYPE_NAME (type) = name;
16680
16681 maybe_set_alignment (cu, die, type);
16682
16683 /* Install the type in the die. */
16684 set_die_type (die, type, cu);
16685
16686 /* set_die_type should be already done. */
16687 set_descriptive_type (type, die, cu);
16688
16689 return type;
16690 }
16691
16692 static enum dwarf_array_dim_ordering
16693 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
16694 {
16695 struct attribute *attr;
16696
16697 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16698
16699 if (attr != nullptr)
16700 return (enum dwarf_array_dim_ordering) DW_SND (attr);
16701
16702 /* GNU F77 is a special case, as at 08/2004 array type info is the
16703 opposite order to the dwarf2 specification, but data is still
16704 laid out as per normal fortran.
16705
16706 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16707 version checking. */
16708
16709 if (cu->language == language_fortran
16710 && cu->producer && strstr (cu->producer, "GNU F77"))
16711 {
16712 return DW_ORD_row_major;
16713 }
16714
16715 switch (cu->language_defn->la_array_ordering)
16716 {
16717 case array_column_major:
16718 return DW_ORD_col_major;
16719 case array_row_major:
16720 default:
16721 return DW_ORD_row_major;
16722 };
16723 }
16724
16725 /* Extract all information from a DW_TAG_set_type DIE and put it in
16726 the DIE's type field. */
16727
16728 static struct type *
16729 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16730 {
16731 struct type *domain_type, *set_type;
16732 struct attribute *attr;
16733
16734 domain_type = die_type (die, cu);
16735
16736 /* The die_type call above may have already set the type for this DIE. */
16737 set_type = get_die_type (die, cu);
16738 if (set_type)
16739 return set_type;
16740
16741 set_type = create_set_type (NULL, domain_type);
16742
16743 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16744 if (attr != nullptr)
16745 TYPE_LENGTH (set_type) = DW_UNSND (attr);
16746
16747 maybe_set_alignment (cu, die, set_type);
16748
16749 return set_die_type (die, set_type, cu);
16750 }
16751
16752 /* A helper for read_common_block that creates a locexpr baton.
16753 SYM is the symbol which we are marking as computed.
16754 COMMON_DIE is the DIE for the common block.
16755 COMMON_LOC is the location expression attribute for the common
16756 block itself.
16757 MEMBER_LOC is the location expression attribute for the particular
16758 member of the common block that we are processing.
16759 CU is the CU from which the above come. */
16760
16761 static void
16762 mark_common_block_symbol_computed (struct symbol *sym,
16763 struct die_info *common_die,
16764 struct attribute *common_loc,
16765 struct attribute *member_loc,
16766 struct dwarf2_cu *cu)
16767 {
16768 struct dwarf2_per_objfile *dwarf2_per_objfile
16769 = cu->per_cu->dwarf2_per_objfile;
16770 struct objfile *objfile = dwarf2_per_objfile->objfile;
16771 struct dwarf2_locexpr_baton *baton;
16772 gdb_byte *ptr;
16773 unsigned int cu_off;
16774 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
16775 LONGEST offset = 0;
16776
16777 gdb_assert (common_loc && member_loc);
16778 gdb_assert (attr_form_is_block (common_loc));
16779 gdb_assert (attr_form_is_block (member_loc)
16780 || attr_form_is_constant (member_loc));
16781
16782 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
16783 baton->per_cu = cu->per_cu;
16784 gdb_assert (baton->per_cu);
16785
16786 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16787
16788 if (attr_form_is_constant (member_loc))
16789 {
16790 offset = dwarf2_get_attr_constant_value (member_loc, 0);
16791 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16792 }
16793 else
16794 baton->size += DW_BLOCK (member_loc)->size;
16795
16796 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
16797 baton->data = ptr;
16798
16799 *ptr++ = DW_OP_call4;
16800 cu_off = common_die->sect_off - cu->per_cu->sect_off;
16801 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16802 ptr += 4;
16803
16804 if (attr_form_is_constant (member_loc))
16805 {
16806 *ptr++ = DW_OP_addr;
16807 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16808 ptr += cu->header.addr_size;
16809 }
16810 else
16811 {
16812 /* We have to copy the data here, because DW_OP_call4 will only
16813 use a DW_AT_location attribute. */
16814 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16815 ptr += DW_BLOCK (member_loc)->size;
16816 }
16817
16818 *ptr++ = DW_OP_plus;
16819 gdb_assert (ptr - baton->data == baton->size);
16820
16821 SYMBOL_LOCATION_BATON (sym) = baton;
16822 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
16823 }
16824
16825 /* Create appropriate locally-scoped variables for all the
16826 DW_TAG_common_block entries. Also create a struct common_block
16827 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16828 is used to separate the common blocks name namespace from regular
16829 variable names. */
16830
16831 static void
16832 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
16833 {
16834 struct attribute *attr;
16835
16836 attr = dwarf2_attr (die, DW_AT_location, cu);
16837 if (attr != nullptr)
16838 {
16839 /* Support the .debug_loc offsets. */
16840 if (attr_form_is_block (attr))
16841 {
16842 /* Ok. */
16843 }
16844 else if (attr_form_is_section_offset (attr))
16845 {
16846 dwarf2_complex_location_expr_complaint ();
16847 attr = NULL;
16848 }
16849 else
16850 {
16851 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16852 "common block member");
16853 attr = NULL;
16854 }
16855 }
16856
16857 if (die->child != NULL)
16858 {
16859 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16860 struct die_info *child_die;
16861 size_t n_entries = 0, size;
16862 struct common_block *common_block;
16863 struct symbol *sym;
16864
16865 for (child_die = die->child;
16866 child_die && child_die->tag;
16867 child_die = sibling_die (child_die))
16868 ++n_entries;
16869
16870 size = (sizeof (struct common_block)
16871 + (n_entries - 1) * sizeof (struct symbol *));
16872 common_block
16873 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16874 size);
16875 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16876 common_block->n_entries = 0;
16877
16878 for (child_die = die->child;
16879 child_die && child_die->tag;
16880 child_die = sibling_die (child_die))
16881 {
16882 /* Create the symbol in the DW_TAG_common_block block in the current
16883 symbol scope. */
16884 sym = new_symbol (child_die, NULL, cu);
16885 if (sym != NULL)
16886 {
16887 struct attribute *member_loc;
16888
16889 common_block->contents[common_block->n_entries++] = sym;
16890
16891 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16892 cu);
16893 if (member_loc)
16894 {
16895 /* GDB has handled this for a long time, but it is
16896 not specified by DWARF. It seems to have been
16897 emitted by gfortran at least as recently as:
16898 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16899 complaint (_("Variable in common block has "
16900 "DW_AT_data_member_location "
16901 "- DIE at %s [in module %s]"),
16902 sect_offset_str (child_die->sect_off),
16903 objfile_name (objfile));
16904
16905 if (attr_form_is_section_offset (member_loc))
16906 dwarf2_complex_location_expr_complaint ();
16907 else if (attr_form_is_constant (member_loc)
16908 || attr_form_is_block (member_loc))
16909 {
16910 if (attr != nullptr)
16911 mark_common_block_symbol_computed (sym, die, attr,
16912 member_loc, cu);
16913 }
16914 else
16915 dwarf2_complex_location_expr_complaint ();
16916 }
16917 }
16918 }
16919
16920 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16921 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
16922 }
16923 }
16924
16925 /* Create a type for a C++ namespace. */
16926
16927 static struct type *
16928 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16929 {
16930 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16931 const char *previous_prefix, *name;
16932 int is_anonymous;
16933 struct type *type;
16934
16935 /* For extensions, reuse the type of the original namespace. */
16936 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16937 {
16938 struct die_info *ext_die;
16939 struct dwarf2_cu *ext_cu = cu;
16940
16941 ext_die = dwarf2_extension (die, &ext_cu);
16942 type = read_type_die (ext_die, ext_cu);
16943
16944 /* EXT_CU may not be the same as CU.
16945 Ensure TYPE is recorded with CU in die_type_hash. */
16946 return set_die_type (die, type, cu);
16947 }
16948
16949 name = namespace_name (die, &is_anonymous, cu);
16950
16951 /* Now build the name of the current namespace. */
16952
16953 previous_prefix = determine_prefix (die, cu);
16954 if (previous_prefix[0] != '\0')
16955 name = typename_concat (&objfile->objfile_obstack,
16956 previous_prefix, name, 0, cu);
16957
16958 /* Create the type. */
16959 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
16960
16961 return set_die_type (die, type, cu);
16962 }
16963
16964 /* Read a namespace scope. */
16965
16966 static void
16967 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16968 {
16969 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16970 int is_anonymous;
16971
16972 /* Add a symbol associated to this if we haven't seen the namespace
16973 before. Also, add a using directive if it's an anonymous
16974 namespace. */
16975
16976 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
16977 {
16978 struct type *type;
16979
16980 type = read_type_die (die, cu);
16981 new_symbol (die, type, cu);
16982
16983 namespace_name (die, &is_anonymous, cu);
16984 if (is_anonymous)
16985 {
16986 const char *previous_prefix = determine_prefix (die, cu);
16987
16988 std::vector<const char *> excludes;
16989 add_using_directive (using_directives (cu),
16990 previous_prefix, TYPE_NAME (type), NULL,
16991 NULL, excludes, 0, &objfile->objfile_obstack);
16992 }
16993 }
16994
16995 if (die->child != NULL)
16996 {
16997 struct die_info *child_die = die->child;
16998
16999 while (child_die && child_die->tag)
17000 {
17001 process_die (child_die, cu);
17002 child_die = sibling_die (child_die);
17003 }
17004 }
17005 }
17006
17007 /* Read a Fortran module as type. This DIE can be only a declaration used for
17008 imported module. Still we need that type as local Fortran "use ... only"
17009 declaration imports depend on the created type in determine_prefix. */
17010
17011 static struct type *
17012 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
17013 {
17014 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17015 const char *module_name;
17016 struct type *type;
17017
17018 module_name = dwarf2_name (die, cu);
17019 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
17020
17021 return set_die_type (die, type, cu);
17022 }
17023
17024 /* Read a Fortran module. */
17025
17026 static void
17027 read_module (struct die_info *die, struct dwarf2_cu *cu)
17028 {
17029 struct die_info *child_die = die->child;
17030 struct type *type;
17031
17032 type = read_type_die (die, cu);
17033 new_symbol (die, type, cu);
17034
17035 while (child_die && child_die->tag)
17036 {
17037 process_die (child_die, cu);
17038 child_die = sibling_die (child_die);
17039 }
17040 }
17041
17042 /* Return the name of the namespace represented by DIE. Set
17043 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
17044 namespace. */
17045
17046 static const char *
17047 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
17048 {
17049 struct die_info *current_die;
17050 const char *name = NULL;
17051
17052 /* Loop through the extensions until we find a name. */
17053
17054 for (current_die = die;
17055 current_die != NULL;
17056 current_die = dwarf2_extension (die, &cu))
17057 {
17058 /* We don't use dwarf2_name here so that we can detect the absence
17059 of a name -> anonymous namespace. */
17060 name = dwarf2_string_attr (die, DW_AT_name, cu);
17061
17062 if (name != NULL)
17063 break;
17064 }
17065
17066 /* Is it an anonymous namespace? */
17067
17068 *is_anonymous = (name == NULL);
17069 if (*is_anonymous)
17070 name = CP_ANONYMOUS_NAMESPACE_STR;
17071
17072 return name;
17073 }
17074
17075 /* Extract all information from a DW_TAG_pointer_type DIE and add to
17076 the user defined type vector. */
17077
17078 static struct type *
17079 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
17080 {
17081 struct gdbarch *gdbarch
17082 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
17083 struct comp_unit_head *cu_header = &cu->header;
17084 struct type *type;
17085 struct attribute *attr_byte_size;
17086 struct attribute *attr_address_class;
17087 int byte_size, addr_class;
17088 struct type *target_type;
17089
17090 target_type = die_type (die, cu);
17091
17092 /* The die_type call above may have already set the type for this DIE. */
17093 type = get_die_type (die, cu);
17094 if (type)
17095 return type;
17096
17097 type = lookup_pointer_type (target_type);
17098
17099 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
17100 if (attr_byte_size)
17101 byte_size = DW_UNSND (attr_byte_size);
17102 else
17103 byte_size = cu_header->addr_size;
17104
17105 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
17106 if (attr_address_class)
17107 addr_class = DW_UNSND (attr_address_class);
17108 else
17109 addr_class = DW_ADDR_none;
17110
17111 ULONGEST alignment = get_alignment (cu, die);
17112
17113 /* If the pointer size, alignment, or address class is different
17114 than the default, create a type variant marked as such and set
17115 the length accordingly. */
17116 if (TYPE_LENGTH (type) != byte_size
17117 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
17118 && alignment != TYPE_RAW_ALIGN (type))
17119 || addr_class != DW_ADDR_none)
17120 {
17121 if (gdbarch_address_class_type_flags_p (gdbarch))
17122 {
17123 int type_flags;
17124
17125 type_flags = gdbarch_address_class_type_flags
17126 (gdbarch, byte_size, addr_class);
17127 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17128 == 0);
17129 type = make_type_with_address_space (type, type_flags);
17130 }
17131 else if (TYPE_LENGTH (type) != byte_size)
17132 {
17133 complaint (_("invalid pointer size %d"), byte_size);
17134 }
17135 else if (TYPE_RAW_ALIGN (type) != alignment)
17136 {
17137 complaint (_("Invalid DW_AT_alignment"
17138 " - DIE at %s [in module %s]"),
17139 sect_offset_str (die->sect_off),
17140 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17141 }
17142 else
17143 {
17144 /* Should we also complain about unhandled address classes? */
17145 }
17146 }
17147
17148 TYPE_LENGTH (type) = byte_size;
17149 set_type_align (type, alignment);
17150 return set_die_type (die, type, cu);
17151 }
17152
17153 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17154 the user defined type vector. */
17155
17156 static struct type *
17157 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
17158 {
17159 struct type *type;
17160 struct type *to_type;
17161 struct type *domain;
17162
17163 to_type = die_type (die, cu);
17164 domain = die_containing_type (die, cu);
17165
17166 /* The calls above may have already set the type for this DIE. */
17167 type = get_die_type (die, cu);
17168 if (type)
17169 return type;
17170
17171 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
17172 type = lookup_methodptr_type (to_type);
17173 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
17174 {
17175 struct type *new_type
17176 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
17177
17178 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
17179 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
17180 TYPE_VARARGS (to_type));
17181 type = lookup_methodptr_type (new_type);
17182 }
17183 else
17184 type = lookup_memberptr_type (to_type, domain);
17185
17186 return set_die_type (die, type, cu);
17187 }
17188
17189 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
17190 the user defined type vector. */
17191
17192 static struct type *
17193 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
17194 enum type_code refcode)
17195 {
17196 struct comp_unit_head *cu_header = &cu->header;
17197 struct type *type, *target_type;
17198 struct attribute *attr;
17199
17200 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17201
17202 target_type = die_type (die, cu);
17203
17204 /* The die_type call above may have already set the type for this DIE. */
17205 type = get_die_type (die, cu);
17206 if (type)
17207 return type;
17208
17209 type = lookup_reference_type (target_type, refcode);
17210 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17211 if (attr != nullptr)
17212 {
17213 TYPE_LENGTH (type) = DW_UNSND (attr);
17214 }
17215 else
17216 {
17217 TYPE_LENGTH (type) = cu_header->addr_size;
17218 }
17219 maybe_set_alignment (cu, die, type);
17220 return set_die_type (die, type, cu);
17221 }
17222
17223 /* Add the given cv-qualifiers to the element type of the array. GCC
17224 outputs DWARF type qualifiers that apply to an array, not the
17225 element type. But GDB relies on the array element type to carry
17226 the cv-qualifiers. This mimics section 6.7.3 of the C99
17227 specification. */
17228
17229 static struct type *
17230 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17231 struct type *base_type, int cnst, int voltl)
17232 {
17233 struct type *el_type, *inner_array;
17234
17235 base_type = copy_type (base_type);
17236 inner_array = base_type;
17237
17238 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
17239 {
17240 TYPE_TARGET_TYPE (inner_array) =
17241 copy_type (TYPE_TARGET_TYPE (inner_array));
17242 inner_array = TYPE_TARGET_TYPE (inner_array);
17243 }
17244
17245 el_type = TYPE_TARGET_TYPE (inner_array);
17246 cnst |= TYPE_CONST (el_type);
17247 voltl |= TYPE_VOLATILE (el_type);
17248 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17249
17250 return set_die_type (die, base_type, cu);
17251 }
17252
17253 static struct type *
17254 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
17255 {
17256 struct type *base_type, *cv_type;
17257
17258 base_type = die_type (die, cu);
17259
17260 /* The die_type call above may have already set the type for this DIE. */
17261 cv_type = get_die_type (die, cu);
17262 if (cv_type)
17263 return cv_type;
17264
17265 /* In case the const qualifier is applied to an array type, the element type
17266 is so qualified, not the array type (section 6.7.3 of C99). */
17267 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17268 return add_array_cv_type (die, cu, base_type, 1, 0);
17269
17270 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17271 return set_die_type (die, cv_type, cu);
17272 }
17273
17274 static struct type *
17275 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
17276 {
17277 struct type *base_type, *cv_type;
17278
17279 base_type = die_type (die, cu);
17280
17281 /* The die_type call above may have already set the type for this DIE. */
17282 cv_type = get_die_type (die, cu);
17283 if (cv_type)
17284 return cv_type;
17285
17286 /* In case the volatile qualifier is applied to an array type, the
17287 element type is so qualified, not the array type (section 6.7.3
17288 of C99). */
17289 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17290 return add_array_cv_type (die, cu, base_type, 0, 1);
17291
17292 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17293 return set_die_type (die, cv_type, cu);
17294 }
17295
17296 /* Handle DW_TAG_restrict_type. */
17297
17298 static struct type *
17299 read_tag_restrict_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_restrict_type (base_type);
17311 return set_die_type (die, cv_type, cu);
17312 }
17313
17314 /* Handle DW_TAG_atomic_type. */
17315
17316 static struct type *
17317 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17318 {
17319 struct type *base_type, *cv_type;
17320
17321 base_type = die_type (die, cu);
17322
17323 /* The die_type call above may have already set the type for this DIE. */
17324 cv_type = get_die_type (die, cu);
17325 if (cv_type)
17326 return cv_type;
17327
17328 cv_type = make_atomic_type (base_type);
17329 return set_die_type (die, cv_type, cu);
17330 }
17331
17332 /* Extract all information from a DW_TAG_string_type DIE and add to
17333 the user defined type vector. It isn't really a user defined type,
17334 but it behaves like one, with other DIE's using an AT_user_def_type
17335 attribute to reference it. */
17336
17337 static struct type *
17338 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
17339 {
17340 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17341 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17342 struct type *type, *range_type, *index_type, *char_type;
17343 struct attribute *attr;
17344 struct dynamic_prop prop;
17345 bool length_is_constant = true;
17346 LONGEST length;
17347
17348 /* There are a couple of places where bit sizes might be made use of
17349 when parsing a DW_TAG_string_type, however, no producer that we know
17350 of make use of these. Handling bit sizes that are a multiple of the
17351 byte size is easy enough, but what about other bit sizes? Lets deal
17352 with that problem when we have to. Warn about these attributes being
17353 unsupported, then parse the type and ignore them like we always
17354 have. */
17355 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
17356 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
17357 {
17358 static bool warning_printed = false;
17359 if (!warning_printed)
17360 {
17361 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
17362 "currently supported on DW_TAG_string_type."));
17363 warning_printed = true;
17364 }
17365 }
17366
17367 attr = dwarf2_attr (die, DW_AT_string_length, cu);
17368 if (attr != nullptr && !attr_form_is_constant (attr))
17369 {
17370 /* The string length describes the location at which the length of
17371 the string can be found. The size of the length field can be
17372 specified with one of the attributes below. */
17373 struct type *prop_type;
17374 struct attribute *len
17375 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
17376 if (len == nullptr)
17377 len = dwarf2_attr (die, DW_AT_byte_size, cu);
17378 if (len != nullptr && attr_form_is_constant (len))
17379 {
17380 /* Pass 0 as the default as we know this attribute is constant
17381 and the default value will not be returned. */
17382 LONGEST sz = dwarf2_get_attr_constant_value (len, 0);
17383 prop_type = dwarf2_per_cu_int_type (cu->per_cu, sz, true);
17384 }
17385 else
17386 {
17387 /* If the size is not specified then we assume it is the size of
17388 an address on this target. */
17389 prop_type = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, true);
17390 }
17391
17392 /* Convert the attribute into a dynamic property. */
17393 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17394 length = 1;
17395 else
17396 length_is_constant = false;
17397 }
17398 else if (attr != nullptr)
17399 {
17400 /* This DW_AT_string_length just contains the length with no
17401 indirection. There's no need to create a dynamic property in this
17402 case. Pass 0 for the default value as we know it will not be
17403 returned in this case. */
17404 length = dwarf2_get_attr_constant_value (attr, 0);
17405 }
17406 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
17407 {
17408 /* We don't currently support non-constant byte sizes for strings. */
17409 length = dwarf2_get_attr_constant_value (attr, 1);
17410 }
17411 else
17412 {
17413 /* Use 1 as a fallback length if we have nothing else. */
17414 length = 1;
17415 }
17416
17417 index_type = objfile_type (objfile)->builtin_int;
17418 if (length_is_constant)
17419 range_type = create_static_range_type (NULL, index_type, 1, length);
17420 else
17421 {
17422 struct dynamic_prop low_bound;
17423
17424 low_bound.kind = PROP_CONST;
17425 low_bound.data.const_val = 1;
17426 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17427 }
17428 char_type = language_string_char_type (cu->language_defn, gdbarch);
17429 type = create_string_type (NULL, char_type, range_type);
17430
17431 return set_die_type (die, type, cu);
17432 }
17433
17434 /* Assuming that DIE corresponds to a function, returns nonzero
17435 if the function is prototyped. */
17436
17437 static int
17438 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17439 {
17440 struct attribute *attr;
17441
17442 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17443 if (attr && (DW_UNSND (attr) != 0))
17444 return 1;
17445
17446 /* The DWARF standard implies that the DW_AT_prototyped attribute
17447 is only meaningful for C, but the concept also extends to other
17448 languages that allow unprototyped functions (Eg: Objective C).
17449 For all other languages, assume that functions are always
17450 prototyped. */
17451 if (cu->language != language_c
17452 && cu->language != language_objc
17453 && cu->language != language_opencl)
17454 return 1;
17455
17456 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17457 prototyped and unprototyped functions; default to prototyped,
17458 since that is more common in modern code (and RealView warns
17459 about unprototyped functions). */
17460 if (producer_is_realview (cu->producer))
17461 return 1;
17462
17463 return 0;
17464 }
17465
17466 /* Handle DIES due to C code like:
17467
17468 struct foo
17469 {
17470 int (*funcp)(int a, long l);
17471 int b;
17472 };
17473
17474 ('funcp' generates a DW_TAG_subroutine_type DIE). */
17475
17476 static struct type *
17477 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
17478 {
17479 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17480 struct type *type; /* Type that this function returns. */
17481 struct type *ftype; /* Function that returns above type. */
17482 struct attribute *attr;
17483
17484 type = die_type (die, cu);
17485
17486 /* The die_type call above may have already set the type for this DIE. */
17487 ftype = get_die_type (die, cu);
17488 if (ftype)
17489 return ftype;
17490
17491 ftype = lookup_function_type (type);
17492
17493 if (prototyped_function_p (die, cu))
17494 TYPE_PROTOTYPED (ftype) = 1;
17495
17496 /* Store the calling convention in the type if it's available in
17497 the subroutine die. Otherwise set the calling convention to
17498 the default value DW_CC_normal. */
17499 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
17500 if (attr != nullptr
17501 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
17502 TYPE_CALLING_CONVENTION (ftype)
17503 = (enum dwarf_calling_convention) (DW_UNSND (attr));
17504 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17505 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17506 else
17507 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
17508
17509 /* Record whether the function returns normally to its caller or not
17510 if the DWARF producer set that information. */
17511 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17512 if (attr && (DW_UNSND (attr) != 0))
17513 TYPE_NO_RETURN (ftype) = 1;
17514
17515 /* We need to add the subroutine type to the die immediately so
17516 we don't infinitely recurse when dealing with parameters
17517 declared as the same subroutine type. */
17518 set_die_type (die, ftype, cu);
17519
17520 if (die->child != NULL)
17521 {
17522 struct type *void_type = objfile_type (objfile)->builtin_void;
17523 struct die_info *child_die;
17524 int nparams, iparams;
17525
17526 /* Count the number of parameters.
17527 FIXME: GDB currently ignores vararg functions, but knows about
17528 vararg member functions. */
17529 nparams = 0;
17530 child_die = die->child;
17531 while (child_die && child_die->tag)
17532 {
17533 if (child_die->tag == DW_TAG_formal_parameter)
17534 nparams++;
17535 else if (child_die->tag == DW_TAG_unspecified_parameters)
17536 TYPE_VARARGS (ftype) = 1;
17537 child_die = sibling_die (child_die);
17538 }
17539
17540 /* Allocate storage for parameters and fill them in. */
17541 TYPE_NFIELDS (ftype) = nparams;
17542 TYPE_FIELDS (ftype) = (struct field *)
17543 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
17544
17545 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17546 even if we error out during the parameters reading below. */
17547 for (iparams = 0; iparams < nparams; iparams++)
17548 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17549
17550 iparams = 0;
17551 child_die = die->child;
17552 while (child_die && child_die->tag)
17553 {
17554 if (child_die->tag == DW_TAG_formal_parameter)
17555 {
17556 struct type *arg_type;
17557
17558 /* DWARF version 2 has no clean way to discern C++
17559 static and non-static member functions. G++ helps
17560 GDB by marking the first parameter for non-static
17561 member functions (which is the this pointer) as
17562 artificial. We pass this information to
17563 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17564
17565 DWARF version 3 added DW_AT_object_pointer, which GCC
17566 4.5 does not yet generate. */
17567 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
17568 if (attr != nullptr)
17569 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17570 else
17571 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
17572 arg_type = die_type (child_die, cu);
17573
17574 /* RealView does not mark THIS as const, which the testsuite
17575 expects. GCC marks THIS as const in method definitions,
17576 but not in the class specifications (GCC PR 43053). */
17577 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17578 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17579 {
17580 int is_this = 0;
17581 struct dwarf2_cu *arg_cu = cu;
17582 const char *name = dwarf2_name (child_die, cu);
17583
17584 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17585 if (attr != nullptr)
17586 {
17587 /* If the compiler emits this, use it. */
17588 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17589 is_this = 1;
17590 }
17591 else if (name && strcmp (name, "this") == 0)
17592 /* Function definitions will have the argument names. */
17593 is_this = 1;
17594 else if (name == NULL && iparams == 0)
17595 /* Declarations may not have the names, so like
17596 elsewhere in GDB, assume an artificial first
17597 argument is "this". */
17598 is_this = 1;
17599
17600 if (is_this)
17601 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17602 arg_type, 0);
17603 }
17604
17605 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
17606 iparams++;
17607 }
17608 child_die = sibling_die (child_die);
17609 }
17610 }
17611
17612 return ftype;
17613 }
17614
17615 static struct type *
17616 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
17617 {
17618 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17619 const char *name = NULL;
17620 struct type *this_type, *target_type;
17621
17622 name = dwarf2_full_name (NULL, die, cu);
17623 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17624 TYPE_TARGET_STUB (this_type) = 1;
17625 set_die_type (die, this_type, cu);
17626 target_type = die_type (die, cu);
17627 if (target_type != this_type)
17628 TYPE_TARGET_TYPE (this_type) = target_type;
17629 else
17630 {
17631 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17632 spec and cause infinite loops in GDB. */
17633 complaint (_("Self-referential DW_TAG_typedef "
17634 "- DIE at %s [in module %s]"),
17635 sect_offset_str (die->sect_off), objfile_name (objfile));
17636 TYPE_TARGET_TYPE (this_type) = NULL;
17637 }
17638 return this_type;
17639 }
17640
17641 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17642 (which may be different from NAME) to the architecture back-end to allow
17643 it to guess the correct format if necessary. */
17644
17645 static struct type *
17646 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17647 const char *name_hint, enum bfd_endian byte_order)
17648 {
17649 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17650 const struct floatformat **format;
17651 struct type *type;
17652
17653 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17654 if (format)
17655 type = init_float_type (objfile, bits, name, format, byte_order);
17656 else
17657 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17658
17659 return type;
17660 }
17661
17662 /* Allocate an integer type of size BITS and name NAME. */
17663
17664 static struct type *
17665 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17666 int bits, int unsigned_p, const char *name)
17667 {
17668 struct type *type;
17669
17670 /* Versions of Intel's C Compiler generate an integer type called "void"
17671 instead of using DW_TAG_unspecified_type. This has been seen on
17672 at least versions 14, 17, and 18. */
17673 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17674 && strcmp (name, "void") == 0)
17675 type = objfile_type (objfile)->builtin_void;
17676 else
17677 type = init_integer_type (objfile, bits, unsigned_p, name);
17678
17679 return type;
17680 }
17681
17682 /* Initialise and return a floating point type of size BITS suitable for
17683 use as a component of a complex number. The NAME_HINT is passed through
17684 when initialising the floating point type and is the name of the complex
17685 type.
17686
17687 As DWARF doesn't currently provide an explicit name for the components
17688 of a complex number, but it can be helpful to have these components
17689 named, we try to select a suitable name based on the size of the
17690 component. */
17691 static struct type *
17692 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17693 struct objfile *objfile,
17694 int bits, const char *name_hint,
17695 enum bfd_endian byte_order)
17696 {
17697 gdbarch *gdbarch = get_objfile_arch (objfile);
17698 struct type *tt = nullptr;
17699
17700 /* Try to find a suitable floating point builtin type of size BITS.
17701 We're going to use the name of this type as the name for the complex
17702 target type that we are about to create. */
17703 switch (cu->language)
17704 {
17705 case language_fortran:
17706 switch (bits)
17707 {
17708 case 32:
17709 tt = builtin_f_type (gdbarch)->builtin_real;
17710 break;
17711 case 64:
17712 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17713 break;
17714 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17715 case 128:
17716 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17717 break;
17718 }
17719 break;
17720 default:
17721 switch (bits)
17722 {
17723 case 32:
17724 tt = builtin_type (gdbarch)->builtin_float;
17725 break;
17726 case 64:
17727 tt = builtin_type (gdbarch)->builtin_double;
17728 break;
17729 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17730 case 128:
17731 tt = builtin_type (gdbarch)->builtin_long_double;
17732 break;
17733 }
17734 break;
17735 }
17736
17737 /* If the type we found doesn't match the size we were looking for, then
17738 pretend we didn't find a type at all, the complex target type we
17739 create will then be nameless. */
17740 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
17741 tt = nullptr;
17742
17743 const char *name = (tt == nullptr) ? nullptr : TYPE_NAME (tt);
17744 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
17745 }
17746
17747 /* Find a representation of a given base type and install
17748 it in the TYPE field of the die. */
17749
17750 static struct type *
17751 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
17752 {
17753 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17754 struct type *type;
17755 struct attribute *attr;
17756 int encoding = 0, bits = 0;
17757 const char *name;
17758 gdbarch *arch;
17759
17760 attr = dwarf2_attr (die, DW_AT_encoding, cu);
17761 if (attr != nullptr)
17762 encoding = DW_UNSND (attr);
17763 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17764 if (attr != nullptr)
17765 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
17766 name = dwarf2_name (die, cu);
17767 if (!name)
17768 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
17769
17770 arch = get_objfile_arch (objfile);
17771 enum bfd_endian byte_order = gdbarch_byte_order (arch);
17772
17773 attr = dwarf2_attr (die, DW_AT_endianity, cu);
17774 if (attr)
17775 {
17776 int endianity = DW_UNSND (attr);
17777
17778 switch (endianity)
17779 {
17780 case DW_END_big:
17781 byte_order = BFD_ENDIAN_BIG;
17782 break;
17783 case DW_END_little:
17784 byte_order = BFD_ENDIAN_LITTLE;
17785 break;
17786 default:
17787 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
17788 break;
17789 }
17790 }
17791
17792 switch (encoding)
17793 {
17794 case DW_ATE_address:
17795 /* Turn DW_ATE_address into a void * pointer. */
17796 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
17797 type = init_pointer_type (objfile, bits, name, type);
17798 break;
17799 case DW_ATE_boolean:
17800 type = init_boolean_type (objfile, bits, 1, name);
17801 break;
17802 case DW_ATE_complex_float:
17803 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
17804 byte_order);
17805 type = init_complex_type (objfile, name, type);
17806 break;
17807 case DW_ATE_decimal_float:
17808 type = init_decfloat_type (objfile, bits, name);
17809 break;
17810 case DW_ATE_float:
17811 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
17812 break;
17813 case DW_ATE_signed:
17814 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17815 break;
17816 case DW_ATE_unsigned:
17817 if (cu->language == language_fortran
17818 && name
17819 && startswith (name, "character("))
17820 type = init_character_type (objfile, bits, 1, name);
17821 else
17822 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17823 break;
17824 case DW_ATE_signed_char:
17825 if (cu->language == language_ada || cu->language == language_m2
17826 || cu->language == language_pascal
17827 || cu->language == language_fortran)
17828 type = init_character_type (objfile, bits, 0, name);
17829 else
17830 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17831 break;
17832 case DW_ATE_unsigned_char:
17833 if (cu->language == language_ada || cu->language == language_m2
17834 || cu->language == language_pascal
17835 || cu->language == language_fortran
17836 || cu->language == language_rust)
17837 type = init_character_type (objfile, bits, 1, name);
17838 else
17839 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17840 break;
17841 case DW_ATE_UTF:
17842 {
17843 if (bits == 16)
17844 type = builtin_type (arch)->builtin_char16;
17845 else if (bits == 32)
17846 type = builtin_type (arch)->builtin_char32;
17847 else
17848 {
17849 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
17850 bits);
17851 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17852 }
17853 return set_die_type (die, type, cu);
17854 }
17855 break;
17856
17857 default:
17858 complaint (_("unsupported DW_AT_encoding: '%s'"),
17859 dwarf_type_encoding_name (encoding));
17860 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17861 break;
17862 }
17863
17864 if (name && strcmp (name, "char") == 0)
17865 TYPE_NOSIGN (type) = 1;
17866
17867 maybe_set_alignment (cu, die, type);
17868
17869 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
17870
17871 return set_die_type (die, type, cu);
17872 }
17873
17874 /* Parse dwarf attribute if it's a block, reference or constant and put the
17875 resulting value of the attribute into struct bound_prop.
17876 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17877
17878 static int
17879 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17880 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17881 struct type *default_type)
17882 {
17883 struct dwarf2_property_baton *baton;
17884 struct obstack *obstack
17885 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
17886
17887 gdb_assert (default_type != NULL);
17888
17889 if (attr == NULL || prop == NULL)
17890 return 0;
17891
17892 if (attr_form_is_block (attr))
17893 {
17894 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17895 baton->property_type = default_type;
17896 baton->locexpr.per_cu = cu->per_cu;
17897 baton->locexpr.size = DW_BLOCK (attr)->size;
17898 baton->locexpr.data = DW_BLOCK (attr)->data;
17899 switch (attr->name)
17900 {
17901 case DW_AT_string_length:
17902 baton->locexpr.is_reference = true;
17903 break;
17904 default:
17905 baton->locexpr.is_reference = false;
17906 break;
17907 }
17908 prop->data.baton = baton;
17909 prop->kind = PROP_LOCEXPR;
17910 gdb_assert (prop->data.baton != NULL);
17911 }
17912 else if (attr_form_is_ref (attr))
17913 {
17914 struct dwarf2_cu *target_cu = cu;
17915 struct die_info *target_die;
17916 struct attribute *target_attr;
17917
17918 target_die = follow_die_ref (die, attr, &target_cu);
17919 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17920 if (target_attr == NULL)
17921 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17922 target_cu);
17923 if (target_attr == NULL)
17924 return 0;
17925
17926 switch (target_attr->name)
17927 {
17928 case DW_AT_location:
17929 if (attr_form_is_section_offset (target_attr))
17930 {
17931 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17932 baton->property_type = die_type (target_die, target_cu);
17933 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17934 prop->data.baton = baton;
17935 prop->kind = PROP_LOCLIST;
17936 gdb_assert (prop->data.baton != NULL);
17937 }
17938 else if (attr_form_is_block (target_attr))
17939 {
17940 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17941 baton->property_type = die_type (target_die, target_cu);
17942 baton->locexpr.per_cu = cu->per_cu;
17943 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17944 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17945 baton->locexpr.is_reference = true;
17946 prop->data.baton = baton;
17947 prop->kind = PROP_LOCEXPR;
17948 gdb_assert (prop->data.baton != NULL);
17949 }
17950 else
17951 {
17952 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17953 "dynamic property");
17954 return 0;
17955 }
17956 break;
17957 case DW_AT_data_member_location:
17958 {
17959 LONGEST offset;
17960
17961 if (!handle_data_member_location (target_die, target_cu,
17962 &offset))
17963 return 0;
17964
17965 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17966 baton->property_type = read_type_die (target_die->parent,
17967 target_cu);
17968 baton->offset_info.offset = offset;
17969 baton->offset_info.type = die_type (target_die, target_cu);
17970 prop->data.baton = baton;
17971 prop->kind = PROP_ADDR_OFFSET;
17972 break;
17973 }
17974 }
17975 }
17976 else if (attr_form_is_constant (attr))
17977 {
17978 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
17979 prop->kind = PROP_CONST;
17980 }
17981 else
17982 {
17983 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17984 dwarf2_name (die, cu));
17985 return 0;
17986 }
17987
17988 return 1;
17989 }
17990
17991 /* Find an integer type SIZE_IN_BYTES bytes in size and return it.
17992 UNSIGNED_P controls if the integer is unsigned or not. */
17993
17994 static struct type *
17995 dwarf2_per_cu_int_type (struct dwarf2_per_cu_data *per_cu,
17996 int size_in_bytes, bool unsigned_p)
17997 {
17998 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
17999 struct type *int_type;
18000
18001 /* Helper macro to examine the various builtin types. */
18002 #define TRY_TYPE(F) \
18003 int_type = (unsigned_p \
18004 ? objfile_type (objfile)->builtin_unsigned_ ## F \
18005 : objfile_type (objfile)->builtin_ ## F); \
18006 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
18007 return int_type
18008
18009 TRY_TYPE (char);
18010 TRY_TYPE (short);
18011 TRY_TYPE (int);
18012 TRY_TYPE (long);
18013 TRY_TYPE (long_long);
18014
18015 #undef TRY_TYPE
18016
18017 gdb_assert_not_reached ("unable to find suitable integer type");
18018 }
18019
18020 /* Find an integer type the same size as the address size given in the
18021 compilation unit header for PER_CU. UNSIGNED_P controls if the integer
18022 is unsigned or not. */
18023
18024 static struct type *
18025 dwarf2_per_cu_addr_sized_int_type (struct dwarf2_per_cu_data *per_cu,
18026 bool unsigned_p)
18027 {
18028 int addr_size = dwarf2_per_cu_addr_size (per_cu);
18029 return dwarf2_per_cu_int_type (per_cu, addr_size, unsigned_p);
18030 }
18031
18032 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
18033 present (which is valid) then compute the default type based on the
18034 compilation units address size. */
18035
18036 static struct type *
18037 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
18038 {
18039 struct type *index_type = die_type (die, cu);
18040
18041 /* Dwarf-2 specifications explicitly allows to create subrange types
18042 without specifying a base type.
18043 In that case, the base type must be set to the type of
18044 the lower bound, upper bound or count, in that order, if any of these
18045 three attributes references an object that has a type.
18046 If no base type is found, the Dwarf-2 specifications say that
18047 a signed integer type of size equal to the size of an address should
18048 be used.
18049 For the following C code: `extern char gdb_int [];'
18050 GCC produces an empty range DIE.
18051 FIXME: muller/2010-05-28: Possible references to object for low bound,
18052 high bound or count are not yet handled by this code. */
18053 if (TYPE_CODE (index_type) == TYPE_CODE_VOID)
18054 index_type = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
18055
18056 return index_type;
18057 }
18058
18059 /* Read the given DW_AT_subrange DIE. */
18060
18061 static struct type *
18062 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
18063 {
18064 struct type *base_type, *orig_base_type;
18065 struct type *range_type;
18066 struct attribute *attr;
18067 struct dynamic_prop low, high;
18068 int low_default_is_valid;
18069 int high_bound_is_count = 0;
18070 const char *name;
18071 ULONGEST negative_mask;
18072
18073 orig_base_type = read_subrange_index_type (die, cu);
18074
18075 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
18076 whereas the real type might be. So, we use ORIG_BASE_TYPE when
18077 creating the range type, but we use the result of check_typedef
18078 when examining properties of the type. */
18079 base_type = check_typedef (orig_base_type);
18080
18081 /* The die_type call above may have already set the type for this DIE. */
18082 range_type = get_die_type (die, cu);
18083 if (range_type)
18084 return range_type;
18085
18086 low.kind = PROP_CONST;
18087 high.kind = PROP_CONST;
18088 high.data.const_val = 0;
18089
18090 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
18091 omitting DW_AT_lower_bound. */
18092 switch (cu->language)
18093 {
18094 case language_c:
18095 case language_cplus:
18096 low.data.const_val = 0;
18097 low_default_is_valid = 1;
18098 break;
18099 case language_fortran:
18100 low.data.const_val = 1;
18101 low_default_is_valid = 1;
18102 break;
18103 case language_d:
18104 case language_objc:
18105 case language_rust:
18106 low.data.const_val = 0;
18107 low_default_is_valid = (cu->header.version >= 4);
18108 break;
18109 case language_ada:
18110 case language_m2:
18111 case language_pascal:
18112 low.data.const_val = 1;
18113 low_default_is_valid = (cu->header.version >= 4);
18114 break;
18115 default:
18116 low.data.const_val = 0;
18117 low_default_is_valid = 0;
18118 break;
18119 }
18120
18121 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
18122 if (attr != nullptr)
18123 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
18124 else if (!low_default_is_valid)
18125 complaint (_("Missing DW_AT_lower_bound "
18126 "- DIE at %s [in module %s]"),
18127 sect_offset_str (die->sect_off),
18128 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
18129
18130 struct attribute *attr_ub, *attr_count;
18131 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
18132 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
18133 {
18134 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
18135 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
18136 {
18137 /* If bounds are constant do the final calculation here. */
18138 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
18139 high.data.const_val = low.data.const_val + high.data.const_val - 1;
18140 else
18141 high_bound_is_count = 1;
18142 }
18143 else
18144 {
18145 if (attr_ub != NULL)
18146 complaint (_("Unresolved DW_AT_upper_bound "
18147 "- DIE at %s [in module %s]"),
18148 sect_offset_str (die->sect_off),
18149 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
18150 if (attr_count != NULL)
18151 complaint (_("Unresolved DW_AT_count "
18152 "- DIE at %s [in module %s]"),
18153 sect_offset_str (die->sect_off),
18154 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
18155 }
18156 }
18157
18158 LONGEST bias = 0;
18159 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
18160 if (bias_attr != nullptr && attr_form_is_constant (bias_attr))
18161 bias = dwarf2_get_attr_constant_value (bias_attr, 0);
18162
18163 /* Normally, the DWARF producers are expected to use a signed
18164 constant form (Eg. DW_FORM_sdata) to express negative bounds.
18165 But this is unfortunately not always the case, as witnessed
18166 with GCC, for instance, where the ambiguous DW_FORM_dataN form
18167 is used instead. To work around that ambiguity, we treat
18168 the bounds as signed, and thus sign-extend their values, when
18169 the base type is signed. */
18170 negative_mask =
18171 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
18172 if (low.kind == PROP_CONST
18173 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
18174 low.data.const_val |= negative_mask;
18175 if (high.kind == PROP_CONST
18176 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
18177 high.data.const_val |= negative_mask;
18178
18179 /* Check for bit and byte strides. */
18180 struct dynamic_prop byte_stride_prop;
18181 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
18182 if (attr_byte_stride != nullptr)
18183 {
18184 struct type *prop_type
18185 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
18186 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
18187 prop_type);
18188 }
18189
18190 struct dynamic_prop bit_stride_prop;
18191 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
18192 if (attr_bit_stride != nullptr)
18193 {
18194 /* It only makes sense to have either a bit or byte stride. */
18195 if (attr_byte_stride != nullptr)
18196 {
18197 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
18198 "- DIE at %s [in module %s]"),
18199 sect_offset_str (die->sect_off),
18200 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
18201 attr_bit_stride = nullptr;
18202 }
18203 else
18204 {
18205 struct type *prop_type
18206 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
18207 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
18208 prop_type);
18209 }
18210 }
18211
18212 if (attr_byte_stride != nullptr
18213 || attr_bit_stride != nullptr)
18214 {
18215 bool byte_stride_p = (attr_byte_stride != nullptr);
18216 struct dynamic_prop *stride
18217 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
18218
18219 range_type
18220 = create_range_type_with_stride (NULL, orig_base_type, &low,
18221 &high, bias, stride, byte_stride_p);
18222 }
18223 else
18224 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
18225
18226 if (high_bound_is_count)
18227 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
18228
18229 /* Ada expects an empty array on no boundary attributes. */
18230 if (attr == NULL && cu->language != language_ada)
18231 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
18232
18233 name = dwarf2_name (die, cu);
18234 if (name)
18235 TYPE_NAME (range_type) = name;
18236
18237 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
18238 if (attr != nullptr)
18239 TYPE_LENGTH (range_type) = DW_UNSND (attr);
18240
18241 maybe_set_alignment (cu, die, range_type);
18242
18243 set_die_type (die, range_type, cu);
18244
18245 /* set_die_type should be already done. */
18246 set_descriptive_type (range_type, die, cu);
18247
18248 return range_type;
18249 }
18250
18251 static struct type *
18252 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
18253 {
18254 struct type *type;
18255
18256 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
18257 NULL);
18258 TYPE_NAME (type) = dwarf2_name (die, cu);
18259
18260 /* In Ada, an unspecified type is typically used when the description
18261 of the type is deferred to a different unit. When encountering
18262 such a type, we treat it as a stub, and try to resolve it later on,
18263 when needed. */
18264 if (cu->language == language_ada)
18265 TYPE_STUB (type) = 1;
18266
18267 return set_die_type (die, type, cu);
18268 }
18269
18270 /* Read a single die and all its descendents. Set the die's sibling
18271 field to NULL; set other fields in the die correctly, and set all
18272 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
18273 location of the info_ptr after reading all of those dies. PARENT
18274 is the parent of the die in question. */
18275
18276 static struct die_info *
18277 read_die_and_children (const struct die_reader_specs *reader,
18278 const gdb_byte *info_ptr,
18279 const gdb_byte **new_info_ptr,
18280 struct die_info *parent)
18281 {
18282 struct die_info *die;
18283 const gdb_byte *cur_ptr;
18284 int has_children;
18285
18286 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
18287 if (die == NULL)
18288 {
18289 *new_info_ptr = cur_ptr;
18290 return NULL;
18291 }
18292 store_in_ref_table (die, reader->cu);
18293
18294 if (has_children)
18295 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
18296 else
18297 {
18298 die->child = NULL;
18299 *new_info_ptr = cur_ptr;
18300 }
18301
18302 die->sibling = NULL;
18303 die->parent = parent;
18304 return die;
18305 }
18306
18307 /* Read a die, all of its descendents, and all of its siblings; set
18308 all of the fields of all of the dies correctly. Arguments are as
18309 in read_die_and_children. */
18310
18311 static struct die_info *
18312 read_die_and_siblings_1 (const struct die_reader_specs *reader,
18313 const gdb_byte *info_ptr,
18314 const gdb_byte **new_info_ptr,
18315 struct die_info *parent)
18316 {
18317 struct die_info *first_die, *last_sibling;
18318 const gdb_byte *cur_ptr;
18319
18320 cur_ptr = info_ptr;
18321 first_die = last_sibling = NULL;
18322
18323 while (1)
18324 {
18325 struct die_info *die
18326 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
18327
18328 if (die == NULL)
18329 {
18330 *new_info_ptr = cur_ptr;
18331 return first_die;
18332 }
18333
18334 if (!first_die)
18335 first_die = die;
18336 else
18337 last_sibling->sibling = die;
18338
18339 last_sibling = die;
18340 }
18341 }
18342
18343 /* Read a die, all of its descendents, and all of its siblings; set
18344 all of the fields of all of the dies correctly. Arguments are as
18345 in read_die_and_children.
18346 This the main entry point for reading a DIE and all its children. */
18347
18348 static struct die_info *
18349 read_die_and_siblings (const struct die_reader_specs *reader,
18350 const gdb_byte *info_ptr,
18351 const gdb_byte **new_info_ptr,
18352 struct die_info *parent)
18353 {
18354 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
18355 new_info_ptr, parent);
18356
18357 if (dwarf_die_debug)
18358 {
18359 fprintf_unfiltered (gdb_stdlog,
18360 "Read die from %s@0x%x of %s:\n",
18361 get_section_name (reader->die_section),
18362 (unsigned) (info_ptr - reader->die_section->buffer),
18363 bfd_get_filename (reader->abfd));
18364 dump_die (die, dwarf_die_debug);
18365 }
18366
18367 return die;
18368 }
18369
18370 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
18371 attributes.
18372 The caller is responsible for filling in the extra attributes
18373 and updating (*DIEP)->num_attrs.
18374 Set DIEP to point to a newly allocated die with its information,
18375 except for its child, sibling, and parent fields.
18376 Set HAS_CHILDREN to tell whether the die has children or not. */
18377
18378 static const gdb_byte *
18379 read_full_die_1 (const struct die_reader_specs *reader,
18380 struct die_info **diep, const gdb_byte *info_ptr,
18381 int *has_children, int num_extra_attrs)
18382 {
18383 unsigned int abbrev_number, bytes_read, i;
18384 struct abbrev_info *abbrev;
18385 struct die_info *die;
18386 struct dwarf2_cu *cu = reader->cu;
18387 bfd *abfd = reader->abfd;
18388
18389 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
18390 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18391 info_ptr += bytes_read;
18392 if (!abbrev_number)
18393 {
18394 *diep = NULL;
18395 *has_children = 0;
18396 return info_ptr;
18397 }
18398
18399 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
18400 if (!abbrev)
18401 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18402 abbrev_number,
18403 bfd_get_filename (abfd));
18404
18405 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
18406 die->sect_off = sect_off;
18407 die->tag = abbrev->tag;
18408 die->abbrev = abbrev_number;
18409
18410 /* Make the result usable.
18411 The caller needs to update num_attrs after adding the extra
18412 attributes. */
18413 die->num_attrs = abbrev->num_attrs;
18414
18415 std::vector<int> indexes_that_need_reprocess;
18416 for (i = 0; i < abbrev->num_attrs; ++i)
18417 {
18418 bool need_reprocess;
18419 info_ptr =
18420 read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18421 info_ptr, &need_reprocess);
18422 if (need_reprocess)
18423 indexes_that_need_reprocess.push_back (i);
18424 }
18425
18426 struct attribute *attr = dwarf2_attr_no_follow (die, DW_AT_str_offsets_base);
18427 if (attr != nullptr)
18428 cu->str_offsets_base = DW_UNSND (attr);
18429
18430 auto maybe_addr_base = lookup_addr_base(die);
18431 if (maybe_addr_base.has_value ())
18432 cu->addr_base = *maybe_addr_base;
18433 for (int index : indexes_that_need_reprocess)
18434 read_attribute_reprocess (reader, &die->attrs[index]);
18435 *diep = die;
18436 *has_children = abbrev->has_children;
18437 return info_ptr;
18438 }
18439
18440 /* Read a die and all its attributes.
18441 Set DIEP to point to a newly allocated die with its information,
18442 except for its child, sibling, and parent fields.
18443 Set HAS_CHILDREN to tell whether the die has children or not. */
18444
18445 static const gdb_byte *
18446 read_full_die (const struct die_reader_specs *reader,
18447 struct die_info **diep, const gdb_byte *info_ptr,
18448 int *has_children)
18449 {
18450 const gdb_byte *result;
18451
18452 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
18453
18454 if (dwarf_die_debug)
18455 {
18456 fprintf_unfiltered (gdb_stdlog,
18457 "Read die from %s@0x%x of %s:\n",
18458 get_section_name (reader->die_section),
18459 (unsigned) (info_ptr - reader->die_section->buffer),
18460 bfd_get_filename (reader->abfd));
18461 dump_die (*diep, dwarf_die_debug);
18462 }
18463
18464 return result;
18465 }
18466 \f
18467 /* Abbreviation tables.
18468
18469 In DWARF version 2, the description of the debugging information is
18470 stored in a separate .debug_abbrev section. Before we read any
18471 dies from a section we read in all abbreviations and install them
18472 in a hash table. */
18473
18474 /* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
18475
18476 struct abbrev_info *
18477 abbrev_table::alloc_abbrev ()
18478 {
18479 struct abbrev_info *abbrev;
18480
18481 abbrev = XOBNEW (&abbrev_obstack, struct abbrev_info);
18482 memset (abbrev, 0, sizeof (struct abbrev_info));
18483
18484 return abbrev;
18485 }
18486
18487 /* Add an abbreviation to the table. */
18488
18489 void
18490 abbrev_table::add_abbrev (unsigned int abbrev_number,
18491 struct abbrev_info *abbrev)
18492 {
18493 unsigned int hash_number;
18494
18495 hash_number = abbrev_number % ABBREV_HASH_SIZE;
18496 abbrev->next = m_abbrevs[hash_number];
18497 m_abbrevs[hash_number] = abbrev;
18498 }
18499
18500 /* Look up an abbrev in the table.
18501 Returns NULL if the abbrev is not found. */
18502
18503 struct abbrev_info *
18504 abbrev_table::lookup_abbrev (unsigned int abbrev_number)
18505 {
18506 unsigned int hash_number;
18507 struct abbrev_info *abbrev;
18508
18509 hash_number = abbrev_number % ABBREV_HASH_SIZE;
18510 abbrev = m_abbrevs[hash_number];
18511
18512 while (abbrev)
18513 {
18514 if (abbrev->number == abbrev_number)
18515 return abbrev;
18516 abbrev = abbrev->next;
18517 }
18518 return NULL;
18519 }
18520
18521 /* Read in an abbrev table. */
18522
18523 static abbrev_table_up
18524 abbrev_table_read_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
18525 struct dwarf2_section_info *section,
18526 sect_offset sect_off)
18527 {
18528 struct objfile *objfile = dwarf2_per_objfile->objfile;
18529 bfd *abfd = get_section_bfd_owner (section);
18530 const gdb_byte *abbrev_ptr;
18531 struct abbrev_info *cur_abbrev;
18532 unsigned int abbrev_number, bytes_read, abbrev_name;
18533 unsigned int abbrev_form;
18534 std::vector<struct attr_abbrev> cur_attrs;
18535
18536 abbrev_table_up abbrev_table (new struct abbrev_table (sect_off));
18537
18538 dwarf2_read_section (objfile, section);
18539 abbrev_ptr = section->buffer + to_underlying (sect_off);
18540 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18541 abbrev_ptr += bytes_read;
18542
18543 /* Loop until we reach an abbrev number of 0. */
18544 while (abbrev_number)
18545 {
18546 cur_attrs.clear ();
18547 cur_abbrev = abbrev_table->alloc_abbrev ();
18548
18549 /* read in abbrev header */
18550 cur_abbrev->number = abbrev_number;
18551 cur_abbrev->tag
18552 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18553 abbrev_ptr += bytes_read;
18554 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
18555 abbrev_ptr += 1;
18556
18557 /* now read in declarations */
18558 for (;;)
18559 {
18560 LONGEST implicit_const;
18561
18562 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18563 abbrev_ptr += bytes_read;
18564 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18565 abbrev_ptr += bytes_read;
18566 if (abbrev_form == DW_FORM_implicit_const)
18567 {
18568 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
18569 &bytes_read);
18570 abbrev_ptr += bytes_read;
18571 }
18572 else
18573 {
18574 /* Initialize it due to a false compiler warning. */
18575 implicit_const = -1;
18576 }
18577
18578 if (abbrev_name == 0)
18579 break;
18580
18581 cur_attrs.emplace_back ();
18582 struct attr_abbrev &cur_attr = cur_attrs.back ();
18583 cur_attr.name = (enum dwarf_attribute) abbrev_name;
18584 cur_attr.form = (enum dwarf_form) abbrev_form;
18585 cur_attr.implicit_const = implicit_const;
18586 ++cur_abbrev->num_attrs;
18587 }
18588
18589 cur_abbrev->attrs =
18590 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
18591 cur_abbrev->num_attrs);
18592 memcpy (cur_abbrev->attrs, cur_attrs.data (),
18593 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
18594
18595 abbrev_table->add_abbrev (abbrev_number, cur_abbrev);
18596
18597 /* Get next abbreviation.
18598 Under Irix6 the abbreviations for a compilation unit are not
18599 always properly terminated with an abbrev number of 0.
18600 Exit loop if we encounter an abbreviation which we have
18601 already read (which means we are about to read the abbreviations
18602 for the next compile unit) or if the end of the abbreviation
18603 table is reached. */
18604 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
18605 break;
18606 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18607 abbrev_ptr += bytes_read;
18608 if (abbrev_table->lookup_abbrev (abbrev_number) != NULL)
18609 break;
18610 }
18611
18612 return abbrev_table;
18613 }
18614
18615 /* Returns nonzero if TAG represents a type that we might generate a partial
18616 symbol for. */
18617
18618 static int
18619 is_type_tag_for_partial (int tag)
18620 {
18621 switch (tag)
18622 {
18623 #if 0
18624 /* Some types that would be reasonable to generate partial symbols for,
18625 that we don't at present. */
18626 case DW_TAG_array_type:
18627 case DW_TAG_file_type:
18628 case DW_TAG_ptr_to_member_type:
18629 case DW_TAG_set_type:
18630 case DW_TAG_string_type:
18631 case DW_TAG_subroutine_type:
18632 #endif
18633 case DW_TAG_base_type:
18634 case DW_TAG_class_type:
18635 case DW_TAG_interface_type:
18636 case DW_TAG_enumeration_type:
18637 case DW_TAG_structure_type:
18638 case DW_TAG_subrange_type:
18639 case DW_TAG_typedef:
18640 case DW_TAG_union_type:
18641 return 1;
18642 default:
18643 return 0;
18644 }
18645 }
18646
18647 /* Load all DIEs that are interesting for partial symbols into memory. */
18648
18649 static struct partial_die_info *
18650 load_partial_dies (const struct die_reader_specs *reader,
18651 const gdb_byte *info_ptr, int building_psymtab)
18652 {
18653 struct dwarf2_cu *cu = reader->cu;
18654 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18655 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
18656 unsigned int bytes_read;
18657 unsigned int load_all = 0;
18658 int nesting_level = 1;
18659
18660 parent_die = NULL;
18661 last_die = NULL;
18662
18663 gdb_assert (cu->per_cu != NULL);
18664 if (cu->per_cu->load_all_dies)
18665 load_all = 1;
18666
18667 cu->partial_dies
18668 = htab_create_alloc_ex (cu->header.length / 12,
18669 partial_die_hash,
18670 partial_die_eq,
18671 NULL,
18672 &cu->comp_unit_obstack,
18673 hashtab_obstack_allocate,
18674 dummy_obstack_deallocate);
18675
18676 while (1)
18677 {
18678 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
18679
18680 /* A NULL abbrev means the end of a series of children. */
18681 if (abbrev == NULL)
18682 {
18683 if (--nesting_level == 0)
18684 return first_die;
18685
18686 info_ptr += bytes_read;
18687 last_die = parent_die;
18688 parent_die = parent_die->die_parent;
18689 continue;
18690 }
18691
18692 /* Check for template arguments. We never save these; if
18693 they're seen, we just mark the parent, and go on our way. */
18694 if (parent_die != NULL
18695 && cu->language == language_cplus
18696 && (abbrev->tag == DW_TAG_template_type_param
18697 || abbrev->tag == DW_TAG_template_value_param))
18698 {
18699 parent_die->has_template_arguments = 1;
18700
18701 if (!load_all)
18702 {
18703 /* We don't need a partial DIE for the template argument. */
18704 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18705 continue;
18706 }
18707 }
18708
18709 /* We only recurse into c++ subprograms looking for template arguments.
18710 Skip their other children. */
18711 if (!load_all
18712 && cu->language == language_cplus
18713 && parent_die != NULL
18714 && parent_die->tag == DW_TAG_subprogram)
18715 {
18716 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18717 continue;
18718 }
18719
18720 /* Check whether this DIE is interesting enough to save. Normally
18721 we would not be interested in members here, but there may be
18722 later variables referencing them via DW_AT_specification (for
18723 static members). */
18724 if (!load_all
18725 && !is_type_tag_for_partial (abbrev->tag)
18726 && abbrev->tag != DW_TAG_constant
18727 && abbrev->tag != DW_TAG_enumerator
18728 && abbrev->tag != DW_TAG_subprogram
18729 && abbrev->tag != DW_TAG_inlined_subroutine
18730 && abbrev->tag != DW_TAG_lexical_block
18731 && abbrev->tag != DW_TAG_variable
18732 && abbrev->tag != DW_TAG_namespace
18733 && abbrev->tag != DW_TAG_module
18734 && abbrev->tag != DW_TAG_member
18735 && abbrev->tag != DW_TAG_imported_unit
18736 && abbrev->tag != DW_TAG_imported_declaration)
18737 {
18738 /* Otherwise we skip to the next sibling, if any. */
18739 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18740 continue;
18741 }
18742
18743 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18744 abbrev);
18745
18746 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
18747
18748 /* This two-pass algorithm for processing partial symbols has a
18749 high cost in cache pressure. Thus, handle some simple cases
18750 here which cover the majority of C partial symbols. DIEs
18751 which neither have specification tags in them, nor could have
18752 specification tags elsewhere pointing at them, can simply be
18753 processed and discarded.
18754
18755 This segment is also optional; scan_partial_symbols and
18756 add_partial_symbol will handle these DIEs if we chain
18757 them in normally. When compilers which do not emit large
18758 quantities of duplicate debug information are more common,
18759 this code can probably be removed. */
18760
18761 /* Any complete simple types at the top level (pretty much all
18762 of them, for a language without namespaces), can be processed
18763 directly. */
18764 if (parent_die == NULL
18765 && pdi.has_specification == 0
18766 && pdi.is_declaration == 0
18767 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18768 || pdi.tag == DW_TAG_base_type
18769 || pdi.tag == DW_TAG_subrange_type))
18770 {
18771 if (building_psymtab && pdi.name != NULL)
18772 add_psymbol_to_list (pdi.name, false,
18773 VAR_DOMAIN, LOC_TYPEDEF, -1,
18774 psymbol_placement::STATIC,
18775 0, cu->language, objfile);
18776 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18777 continue;
18778 }
18779
18780 /* The exception for DW_TAG_typedef with has_children above is
18781 a workaround of GCC PR debug/47510. In the case of this complaint
18782 type_name_or_error will error on such types later.
18783
18784 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18785 it could not find the child DIEs referenced later, this is checked
18786 above. In correct DWARF DW_TAG_typedef should have no children. */
18787
18788 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
18789 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
18790 "- DIE at %s [in module %s]"),
18791 sect_offset_str (pdi.sect_off), objfile_name (objfile));
18792
18793 /* If we're at the second level, and we're an enumerator, and
18794 our parent has no specification (meaning possibly lives in a
18795 namespace elsewhere), then we can add the partial symbol now
18796 instead of queueing it. */
18797 if (pdi.tag == DW_TAG_enumerator
18798 && parent_die != NULL
18799 && parent_die->die_parent == NULL
18800 && parent_die->tag == DW_TAG_enumeration_type
18801 && parent_die->has_specification == 0)
18802 {
18803 if (pdi.name == NULL)
18804 complaint (_("malformed enumerator DIE ignored"));
18805 else if (building_psymtab)
18806 add_psymbol_to_list (pdi.name, false,
18807 VAR_DOMAIN, LOC_CONST, -1,
18808 cu->language == language_cplus
18809 ? psymbol_placement::GLOBAL
18810 : psymbol_placement::STATIC,
18811 0, cu->language, objfile);
18812
18813 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18814 continue;
18815 }
18816
18817 struct partial_die_info *part_die
18818 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
18819
18820 /* We'll save this DIE so link it in. */
18821 part_die->die_parent = parent_die;
18822 part_die->die_sibling = NULL;
18823 part_die->die_child = NULL;
18824
18825 if (last_die && last_die == parent_die)
18826 last_die->die_child = part_die;
18827 else if (last_die)
18828 last_die->die_sibling = part_die;
18829
18830 last_die = part_die;
18831
18832 if (first_die == NULL)
18833 first_die = part_die;
18834
18835 /* Maybe add the DIE to the hash table. Not all DIEs that we
18836 find interesting need to be in the hash table, because we
18837 also have the parent/sibling/child chains; only those that we
18838 might refer to by offset later during partial symbol reading.
18839
18840 For now this means things that might have be the target of a
18841 DW_AT_specification, DW_AT_abstract_origin, or
18842 DW_AT_extension. DW_AT_extension will refer only to
18843 namespaces; DW_AT_abstract_origin refers to functions (and
18844 many things under the function DIE, but we do not recurse
18845 into function DIEs during partial symbol reading) and
18846 possibly variables as well; DW_AT_specification refers to
18847 declarations. Declarations ought to have the DW_AT_declaration
18848 flag. It happens that GCC forgets to put it in sometimes, but
18849 only for functions, not for types.
18850
18851 Adding more things than necessary to the hash table is harmless
18852 except for the performance cost. Adding too few will result in
18853 wasted time in find_partial_die, when we reread the compilation
18854 unit with load_all_dies set. */
18855
18856 if (load_all
18857 || abbrev->tag == DW_TAG_constant
18858 || abbrev->tag == DW_TAG_subprogram
18859 || abbrev->tag == DW_TAG_variable
18860 || abbrev->tag == DW_TAG_namespace
18861 || part_die->is_declaration)
18862 {
18863 void **slot;
18864
18865 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
18866 to_underlying (part_die->sect_off),
18867 INSERT);
18868 *slot = part_die;
18869 }
18870
18871 /* For some DIEs we want to follow their children (if any). For C
18872 we have no reason to follow the children of structures; for other
18873 languages we have to, so that we can get at method physnames
18874 to infer fully qualified class names, for DW_AT_specification,
18875 and for C++ template arguments. For C++, we also look one level
18876 inside functions to find template arguments (if the name of the
18877 function does not already contain the template arguments).
18878
18879 For Ada and Fortran, we need to scan the children of subprograms
18880 and lexical blocks as well because these languages allow the
18881 definition of nested entities that could be interesting for the
18882 debugger, such as nested subprograms for instance. */
18883 if (last_die->has_children
18884 && (load_all
18885 || last_die->tag == DW_TAG_namespace
18886 || last_die->tag == DW_TAG_module
18887 || last_die->tag == DW_TAG_enumeration_type
18888 || (cu->language == language_cplus
18889 && last_die->tag == DW_TAG_subprogram
18890 && (last_die->name == NULL
18891 || strchr (last_die->name, '<') == NULL))
18892 || (cu->language != language_c
18893 && (last_die->tag == DW_TAG_class_type
18894 || last_die->tag == DW_TAG_interface_type
18895 || last_die->tag == DW_TAG_structure_type
18896 || last_die->tag == DW_TAG_union_type))
18897 || ((cu->language == language_ada
18898 || cu->language == language_fortran)
18899 && (last_die->tag == DW_TAG_subprogram
18900 || last_die->tag == DW_TAG_lexical_block))))
18901 {
18902 nesting_level++;
18903 parent_die = last_die;
18904 continue;
18905 }
18906
18907 /* Otherwise we skip to the next sibling, if any. */
18908 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
18909
18910 /* Back to the top, do it again. */
18911 }
18912 }
18913
18914 partial_die_info::partial_die_info (sect_offset sect_off_,
18915 struct abbrev_info *abbrev)
18916 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18917 {
18918 }
18919
18920 /* Read a minimal amount of information into the minimal die structure.
18921 INFO_PTR should point just after the initial uleb128 of a DIE. */
18922
18923 const gdb_byte *
18924 partial_die_info::read (const struct die_reader_specs *reader,
18925 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
18926 {
18927 struct dwarf2_cu *cu = reader->cu;
18928 struct dwarf2_per_objfile *dwarf2_per_objfile
18929 = cu->per_cu->dwarf2_per_objfile;
18930 unsigned int i;
18931 int has_low_pc_attr = 0;
18932 int has_high_pc_attr = 0;
18933 int high_pc_relative = 0;
18934
18935 std::vector<struct attribute> attr_vec (abbrev.num_attrs);
18936 for (i = 0; i < abbrev.num_attrs; ++i)
18937 {
18938 bool need_reprocess;
18939 info_ptr = read_attribute (reader, &attr_vec[i], &abbrev.attrs[i],
18940 info_ptr, &need_reprocess);
18941 /* String and address offsets that need to do the reprocessing have
18942 already been read at this point, so there is no need to wait until
18943 the loop terminates to do the reprocessing. */
18944 if (need_reprocess)
18945 read_attribute_reprocess (reader, &attr_vec[i]);
18946 attribute &attr = attr_vec[i];
18947 /* Store the data if it is of an attribute we want to keep in a
18948 partial symbol table. */
18949 switch (attr.name)
18950 {
18951 case DW_AT_name:
18952 switch (tag)
18953 {
18954 case DW_TAG_compile_unit:
18955 case DW_TAG_partial_unit:
18956 case DW_TAG_type_unit:
18957 /* Compilation units have a DW_AT_name that is a filename, not
18958 a source language identifier. */
18959 case DW_TAG_enumeration_type:
18960 case DW_TAG_enumerator:
18961 /* These tags always have simple identifiers already; no need
18962 to canonicalize them. */
18963 name = DW_STRING (&attr);
18964 break;
18965 default:
18966 {
18967 struct objfile *objfile = dwarf2_per_objfile->objfile;
18968
18969 name
18970 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
18971 &objfile->per_bfd->storage_obstack);
18972 }
18973 break;
18974 }
18975 break;
18976 case DW_AT_linkage_name:
18977 case DW_AT_MIPS_linkage_name:
18978 /* Note that both forms of linkage name might appear. We
18979 assume they will be the same, and we only store the last
18980 one we see. */
18981 linkage_name = DW_STRING (&attr);
18982 break;
18983 case DW_AT_low_pc:
18984 has_low_pc_attr = 1;
18985 lowpc = attr_value_as_address (&attr);
18986 break;
18987 case DW_AT_high_pc:
18988 has_high_pc_attr = 1;
18989 highpc = attr_value_as_address (&attr);
18990 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
18991 high_pc_relative = 1;
18992 break;
18993 case DW_AT_location:
18994 /* Support the .debug_loc offsets. */
18995 if (attr_form_is_block (&attr))
18996 {
18997 d.locdesc = DW_BLOCK (&attr);
18998 }
18999 else if (attr_form_is_section_offset (&attr))
19000 {
19001 dwarf2_complex_location_expr_complaint ();
19002 }
19003 else
19004 {
19005 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
19006 "partial symbol information");
19007 }
19008 break;
19009 case DW_AT_external:
19010 is_external = DW_UNSND (&attr);
19011 break;
19012 case DW_AT_declaration:
19013 is_declaration = DW_UNSND (&attr);
19014 break;
19015 case DW_AT_type:
19016 has_type = 1;
19017 break;
19018 case DW_AT_abstract_origin:
19019 case DW_AT_specification:
19020 case DW_AT_extension:
19021 has_specification = 1;
19022 spec_offset = dwarf2_get_ref_die_offset (&attr);
19023 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
19024 || cu->per_cu->is_dwz);
19025 break;
19026 case DW_AT_sibling:
19027 /* Ignore absolute siblings, they might point outside of
19028 the current compile unit. */
19029 if (attr.form == DW_FORM_ref_addr)
19030 complaint (_("ignoring absolute DW_AT_sibling"));
19031 else
19032 {
19033 const gdb_byte *buffer = reader->buffer;
19034 sect_offset off = dwarf2_get_ref_die_offset (&attr);
19035 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
19036
19037 if (sibling_ptr < info_ptr)
19038 complaint (_("DW_AT_sibling points backwards"));
19039 else if (sibling_ptr > reader->buffer_end)
19040 dwarf2_section_buffer_overflow_complaint (reader->die_section);
19041 else
19042 sibling = sibling_ptr;
19043 }
19044 break;
19045 case DW_AT_byte_size:
19046 has_byte_size = 1;
19047 break;
19048 case DW_AT_const_value:
19049 has_const_value = 1;
19050 break;
19051 case DW_AT_calling_convention:
19052 /* DWARF doesn't provide a way to identify a program's source-level
19053 entry point. DW_AT_calling_convention attributes are only meant
19054 to describe functions' calling conventions.
19055
19056 However, because it's a necessary piece of information in
19057 Fortran, and before DWARF 4 DW_CC_program was the only
19058 piece of debugging information whose definition refers to
19059 a 'main program' at all, several compilers marked Fortran
19060 main programs with DW_CC_program --- even when those
19061 functions use the standard calling conventions.
19062
19063 Although DWARF now specifies a way to provide this
19064 information, we support this practice for backward
19065 compatibility. */
19066 if (DW_UNSND (&attr) == DW_CC_program
19067 && cu->language == language_fortran)
19068 main_subprogram = 1;
19069 break;
19070 case DW_AT_inline:
19071 if (DW_UNSND (&attr) == DW_INL_inlined
19072 || DW_UNSND (&attr) == DW_INL_declared_inlined)
19073 may_be_inlined = 1;
19074 break;
19075
19076 case DW_AT_import:
19077 if (tag == DW_TAG_imported_unit)
19078 {
19079 d.sect_off = dwarf2_get_ref_die_offset (&attr);
19080 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
19081 || cu->per_cu->is_dwz);
19082 }
19083 break;
19084
19085 case DW_AT_main_subprogram:
19086 main_subprogram = DW_UNSND (&attr);
19087 break;
19088
19089 case DW_AT_ranges:
19090 {
19091 /* It would be nice to reuse dwarf2_get_pc_bounds here,
19092 but that requires a full DIE, so instead we just
19093 reimplement it. */
19094 int need_ranges_base = tag != DW_TAG_compile_unit;
19095 unsigned int ranges_offset = (DW_UNSND (&attr)
19096 + (need_ranges_base
19097 ? cu->ranges_base
19098 : 0));
19099
19100 /* Value of the DW_AT_ranges attribute is the offset in the
19101 .debug_ranges section. */
19102 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
19103 nullptr))
19104 has_pc_info = 1;
19105 }
19106 break;
19107
19108 default:
19109 break;
19110 }
19111 }
19112
19113 /* For Ada, if both the name and the linkage name appear, we prefer
19114 the latter. This lets "catch exception" work better, regardless
19115 of the order in which the name and linkage name were emitted.
19116 Really, though, this is just a workaround for the fact that gdb
19117 doesn't store both the name and the linkage name. */
19118 if (cu->language == language_ada && linkage_name != nullptr)
19119 name = linkage_name;
19120
19121 if (high_pc_relative)
19122 highpc += lowpc;
19123
19124 if (has_low_pc_attr && has_high_pc_attr)
19125 {
19126 /* When using the GNU linker, .gnu.linkonce. sections are used to
19127 eliminate duplicate copies of functions and vtables and such.
19128 The linker will arbitrarily choose one and discard the others.
19129 The AT_*_pc values for such functions refer to local labels in
19130 these sections. If the section from that file was discarded, the
19131 labels are not in the output, so the relocs get a value of 0.
19132 If this is a discarded function, mark the pc bounds as invalid,
19133 so that GDB will ignore it. */
19134 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
19135 {
19136 struct objfile *objfile = dwarf2_per_objfile->objfile;
19137 struct gdbarch *gdbarch = get_objfile_arch (objfile);
19138
19139 complaint (_("DW_AT_low_pc %s is zero "
19140 "for DIE at %s [in module %s]"),
19141 paddress (gdbarch, lowpc),
19142 sect_offset_str (sect_off),
19143 objfile_name (objfile));
19144 }
19145 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
19146 else if (lowpc >= highpc)
19147 {
19148 struct objfile *objfile = dwarf2_per_objfile->objfile;
19149 struct gdbarch *gdbarch = get_objfile_arch (objfile);
19150
19151 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
19152 "for DIE at %s [in module %s]"),
19153 paddress (gdbarch, lowpc),
19154 paddress (gdbarch, highpc),
19155 sect_offset_str (sect_off),
19156 objfile_name (objfile));
19157 }
19158 else
19159 has_pc_info = 1;
19160 }
19161
19162 return info_ptr;
19163 }
19164
19165 /* Find a cached partial DIE at OFFSET in CU. */
19166
19167 struct partial_die_info *
19168 dwarf2_cu::find_partial_die (sect_offset sect_off)
19169 {
19170 struct partial_die_info *lookup_die = NULL;
19171 struct partial_die_info part_die (sect_off);
19172
19173 lookup_die = ((struct partial_die_info *)
19174 htab_find_with_hash (partial_dies, &part_die,
19175 to_underlying (sect_off)));
19176
19177 return lookup_die;
19178 }
19179
19180 /* Find a partial DIE at OFFSET, which may or may not be in CU,
19181 except in the case of .debug_types DIEs which do not reference
19182 outside their CU (they do however referencing other types via
19183 DW_FORM_ref_sig8). */
19184
19185 static const struct cu_partial_die_info
19186 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
19187 {
19188 struct dwarf2_per_objfile *dwarf2_per_objfile
19189 = cu->per_cu->dwarf2_per_objfile;
19190 struct objfile *objfile = dwarf2_per_objfile->objfile;
19191 struct dwarf2_per_cu_data *per_cu = NULL;
19192 struct partial_die_info *pd = NULL;
19193
19194 if (offset_in_dwz == cu->per_cu->is_dwz
19195 && offset_in_cu_p (&cu->header, sect_off))
19196 {
19197 pd = cu->find_partial_die (sect_off);
19198 if (pd != NULL)
19199 return { cu, pd };
19200 /* We missed recording what we needed.
19201 Load all dies and try again. */
19202 per_cu = cu->per_cu;
19203 }
19204 else
19205 {
19206 /* TUs don't reference other CUs/TUs (except via type signatures). */
19207 if (cu->per_cu->is_debug_types)
19208 {
19209 error (_("Dwarf Error: Type Unit at offset %s contains"
19210 " external reference to offset %s [in module %s].\n"),
19211 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
19212 bfd_get_filename (objfile->obfd));
19213 }
19214 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
19215 dwarf2_per_objfile);
19216
19217 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
19218 load_partial_comp_unit (per_cu);
19219
19220 per_cu->cu->last_used = 0;
19221 pd = per_cu->cu->find_partial_die (sect_off);
19222 }
19223
19224 /* If we didn't find it, and not all dies have been loaded,
19225 load them all and try again. */
19226
19227 if (pd == NULL && per_cu->load_all_dies == 0)
19228 {
19229 per_cu->load_all_dies = 1;
19230
19231 /* This is nasty. When we reread the DIEs, somewhere up the call chain
19232 THIS_CU->cu may already be in use. So we can't just free it and
19233 replace its DIEs with the ones we read in. Instead, we leave those
19234 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
19235 and clobber THIS_CU->cu->partial_dies with the hash table for the new
19236 set. */
19237 load_partial_comp_unit (per_cu);
19238
19239 pd = per_cu->cu->find_partial_die (sect_off);
19240 }
19241
19242 if (pd == NULL)
19243 internal_error (__FILE__, __LINE__,
19244 _("could not find partial DIE %s "
19245 "in cache [from module %s]\n"),
19246 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
19247 return { per_cu->cu, pd };
19248 }
19249
19250 /* See if we can figure out if the class lives in a namespace. We do
19251 this by looking for a member function; its demangled name will
19252 contain namespace info, if there is any. */
19253
19254 static void
19255 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
19256 struct dwarf2_cu *cu)
19257 {
19258 /* NOTE: carlton/2003-10-07: Getting the info this way changes
19259 what template types look like, because the demangler
19260 frequently doesn't give the same name as the debug info. We
19261 could fix this by only using the demangled name to get the
19262 prefix (but see comment in read_structure_type). */
19263
19264 struct partial_die_info *real_pdi;
19265 struct partial_die_info *child_pdi;
19266
19267 /* If this DIE (this DIE's specification, if any) has a parent, then
19268 we should not do this. We'll prepend the parent's fully qualified
19269 name when we create the partial symbol. */
19270
19271 real_pdi = struct_pdi;
19272 while (real_pdi->has_specification)
19273 {
19274 auto res = find_partial_die (real_pdi->spec_offset,
19275 real_pdi->spec_is_dwz, cu);
19276 real_pdi = res.pdi;
19277 cu = res.cu;
19278 }
19279
19280 if (real_pdi->die_parent != NULL)
19281 return;
19282
19283 for (child_pdi = struct_pdi->die_child;
19284 child_pdi != NULL;
19285 child_pdi = child_pdi->die_sibling)
19286 {
19287 if (child_pdi->tag == DW_TAG_subprogram
19288 && child_pdi->linkage_name != NULL)
19289 {
19290 gdb::unique_xmalloc_ptr<char> actual_class_name
19291 (language_class_name_from_physname (cu->language_defn,
19292 child_pdi->linkage_name));
19293 if (actual_class_name != NULL)
19294 {
19295 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19296 struct_pdi->name
19297 = obstack_strdup (&objfile->per_bfd->storage_obstack,
19298 actual_class_name.get ());
19299 }
19300 break;
19301 }
19302 }
19303 }
19304
19305 void
19306 partial_die_info::fixup (struct dwarf2_cu *cu)
19307 {
19308 /* Once we've fixed up a die, there's no point in doing so again.
19309 This also avoids a memory leak if we were to call
19310 guess_partial_die_structure_name multiple times. */
19311 if (fixup_called)
19312 return;
19313
19314 /* If we found a reference attribute and the DIE has no name, try
19315 to find a name in the referred to DIE. */
19316
19317 if (name == NULL && has_specification)
19318 {
19319 struct partial_die_info *spec_die;
19320
19321 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
19322 spec_die = res.pdi;
19323 cu = res.cu;
19324
19325 spec_die->fixup (cu);
19326
19327 if (spec_die->name)
19328 {
19329 name = spec_die->name;
19330
19331 /* Copy DW_AT_external attribute if it is set. */
19332 if (spec_die->is_external)
19333 is_external = spec_die->is_external;
19334 }
19335 }
19336
19337 /* Set default names for some unnamed DIEs. */
19338
19339 if (name == NULL && tag == DW_TAG_namespace)
19340 name = CP_ANONYMOUS_NAMESPACE_STR;
19341
19342 /* If there is no parent die to provide a namespace, and there are
19343 children, see if we can determine the namespace from their linkage
19344 name. */
19345 if (cu->language == language_cplus
19346 && !cu->per_cu->dwarf2_per_objfile->types.empty ()
19347 && die_parent == NULL
19348 && has_children
19349 && (tag == DW_TAG_class_type
19350 || tag == DW_TAG_structure_type
19351 || tag == DW_TAG_union_type))
19352 guess_partial_die_structure_name (this, cu);
19353
19354 /* GCC might emit a nameless struct or union that has a linkage
19355 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
19356 if (name == NULL
19357 && (tag == DW_TAG_class_type
19358 || tag == DW_TAG_interface_type
19359 || tag == DW_TAG_structure_type
19360 || tag == DW_TAG_union_type)
19361 && linkage_name != NULL)
19362 {
19363 gdb::unique_xmalloc_ptr<char> demangled
19364 (gdb_demangle (linkage_name, DMGL_TYPES));
19365 if (demangled != nullptr)
19366 {
19367 const char *base;
19368
19369 /* Strip any leading namespaces/classes, keep only the base name.
19370 DW_AT_name for named DIEs does not contain the prefixes. */
19371 base = strrchr (demangled.get (), ':');
19372 if (base && base > demangled.get () && base[-1] == ':')
19373 base++;
19374 else
19375 base = demangled.get ();
19376
19377 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19378 name = obstack_strdup (&objfile->per_bfd->storage_obstack, base);
19379 }
19380 }
19381
19382 fixup_called = 1;
19383 }
19384
19385 /* Process the attributes that had to be skipped in the first round. These
19386 attributes are the ones that need str_offsets_base or addr_base attributes.
19387 They could not have been processed in the first round, because at the time
19388 the values of str_offsets_base or addr_base may not have been known. */
19389 void read_attribute_reprocess (const struct die_reader_specs *reader,
19390 struct attribute *attr)
19391 {
19392 struct dwarf2_cu *cu = reader->cu;
19393 switch (attr->form)
19394 {
19395 case DW_FORM_addrx:
19396 case DW_FORM_GNU_addr_index:
19397 DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
19398 break;
19399 case DW_FORM_strx:
19400 case DW_FORM_strx1:
19401 case DW_FORM_strx2:
19402 case DW_FORM_strx3:
19403 case DW_FORM_strx4:
19404 case DW_FORM_GNU_str_index:
19405 {
19406 unsigned int str_index = DW_UNSND (attr);
19407 if (reader->dwo_file != NULL)
19408 {
19409 DW_STRING (attr) = read_dwo_str_index (reader, str_index);
19410 DW_STRING_IS_CANONICAL (attr) = 0;
19411 }
19412 else
19413 {
19414 DW_STRING (attr) = read_stub_str_index (cu, str_index);
19415 DW_STRING_IS_CANONICAL (attr) = 0;
19416 }
19417 break;
19418 }
19419 default:
19420 gdb_assert_not_reached (_("Unexpected DWARF form."));
19421 }
19422 }
19423
19424 /* Read an attribute value described by an attribute form. */
19425
19426 static const gdb_byte *
19427 read_attribute_value (const struct die_reader_specs *reader,
19428 struct attribute *attr, unsigned form,
19429 LONGEST implicit_const, const gdb_byte *info_ptr,
19430 bool *need_reprocess)
19431 {
19432 struct dwarf2_cu *cu = reader->cu;
19433 struct dwarf2_per_objfile *dwarf2_per_objfile
19434 = cu->per_cu->dwarf2_per_objfile;
19435 struct objfile *objfile = dwarf2_per_objfile->objfile;
19436 struct gdbarch *gdbarch = get_objfile_arch (objfile);
19437 bfd *abfd = reader->abfd;
19438 struct comp_unit_head *cu_header = &cu->header;
19439 unsigned int bytes_read;
19440 struct dwarf_block *blk;
19441 *need_reprocess = false;
19442
19443 attr->form = (enum dwarf_form) form;
19444 switch (form)
19445 {
19446 case DW_FORM_ref_addr:
19447 if (cu->header.version == 2)
19448 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
19449 else
19450 DW_UNSND (attr) = read_offset (abfd, info_ptr,
19451 &cu->header, &bytes_read);
19452 info_ptr += bytes_read;
19453 break;
19454 case DW_FORM_GNU_ref_alt:
19455 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19456 info_ptr += bytes_read;
19457 break;
19458 case DW_FORM_addr:
19459 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
19460 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
19461 info_ptr += bytes_read;
19462 break;
19463 case DW_FORM_block2:
19464 blk = dwarf_alloc_block (cu);
19465 blk->size = read_2_bytes (abfd, info_ptr);
19466 info_ptr += 2;
19467 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19468 info_ptr += blk->size;
19469 DW_BLOCK (attr) = blk;
19470 break;
19471 case DW_FORM_block4:
19472 blk = dwarf_alloc_block (cu);
19473 blk->size = read_4_bytes (abfd, info_ptr);
19474 info_ptr += 4;
19475 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19476 info_ptr += blk->size;
19477 DW_BLOCK (attr) = blk;
19478 break;
19479 case DW_FORM_data2:
19480 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
19481 info_ptr += 2;
19482 break;
19483 case DW_FORM_data4:
19484 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
19485 info_ptr += 4;
19486 break;
19487 case DW_FORM_data8:
19488 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
19489 info_ptr += 8;
19490 break;
19491 case DW_FORM_data16:
19492 blk = dwarf_alloc_block (cu);
19493 blk->size = 16;
19494 blk->data = read_n_bytes (abfd, info_ptr, 16);
19495 info_ptr += 16;
19496 DW_BLOCK (attr) = blk;
19497 break;
19498 case DW_FORM_sec_offset:
19499 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19500 info_ptr += bytes_read;
19501 break;
19502 case DW_FORM_string:
19503 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
19504 DW_STRING_IS_CANONICAL (attr) = 0;
19505 info_ptr += bytes_read;
19506 break;
19507 case DW_FORM_strp:
19508 if (!cu->per_cu->is_dwz)
19509 {
19510 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
19511 abfd, info_ptr, cu_header,
19512 &bytes_read);
19513 DW_STRING_IS_CANONICAL (attr) = 0;
19514 info_ptr += bytes_read;
19515 break;
19516 }
19517 /* FALLTHROUGH */
19518 case DW_FORM_line_strp:
19519 if (!cu->per_cu->is_dwz)
19520 {
19521 DW_STRING (attr) = read_indirect_line_string (dwarf2_per_objfile,
19522 abfd, info_ptr,
19523 cu_header, &bytes_read);
19524 DW_STRING_IS_CANONICAL (attr) = 0;
19525 info_ptr += bytes_read;
19526 break;
19527 }
19528 /* FALLTHROUGH */
19529 case DW_FORM_GNU_strp_alt:
19530 {
19531 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
19532 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
19533 &bytes_read);
19534
19535 DW_STRING (attr) = read_indirect_string_from_dwz (objfile,
19536 dwz, str_offset);
19537 DW_STRING_IS_CANONICAL (attr) = 0;
19538 info_ptr += bytes_read;
19539 }
19540 break;
19541 case DW_FORM_exprloc:
19542 case DW_FORM_block:
19543 blk = dwarf_alloc_block (cu);
19544 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19545 info_ptr += bytes_read;
19546 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19547 info_ptr += blk->size;
19548 DW_BLOCK (attr) = blk;
19549 break;
19550 case DW_FORM_block1:
19551 blk = dwarf_alloc_block (cu);
19552 blk->size = read_1_byte (abfd, info_ptr);
19553 info_ptr += 1;
19554 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19555 info_ptr += blk->size;
19556 DW_BLOCK (attr) = blk;
19557 break;
19558 case DW_FORM_data1:
19559 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19560 info_ptr += 1;
19561 break;
19562 case DW_FORM_flag:
19563 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19564 info_ptr += 1;
19565 break;
19566 case DW_FORM_flag_present:
19567 DW_UNSND (attr) = 1;
19568 break;
19569 case DW_FORM_sdata:
19570 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19571 info_ptr += bytes_read;
19572 break;
19573 case DW_FORM_udata:
19574 case DW_FORM_rnglistx:
19575 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19576 info_ptr += bytes_read;
19577 break;
19578 case DW_FORM_ref1:
19579 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19580 + read_1_byte (abfd, info_ptr));
19581 info_ptr += 1;
19582 break;
19583 case DW_FORM_ref2:
19584 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19585 + read_2_bytes (abfd, info_ptr));
19586 info_ptr += 2;
19587 break;
19588 case DW_FORM_ref4:
19589 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19590 + read_4_bytes (abfd, info_ptr));
19591 info_ptr += 4;
19592 break;
19593 case DW_FORM_ref8:
19594 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19595 + read_8_bytes (abfd, info_ptr));
19596 info_ptr += 8;
19597 break;
19598 case DW_FORM_ref_sig8:
19599 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
19600 info_ptr += 8;
19601 break;
19602 case DW_FORM_ref_udata:
19603 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19604 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
19605 info_ptr += bytes_read;
19606 break;
19607 case DW_FORM_indirect:
19608 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19609 info_ptr += bytes_read;
19610 if (form == DW_FORM_implicit_const)
19611 {
19612 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19613 info_ptr += bytes_read;
19614 }
19615 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19616 info_ptr, need_reprocess);
19617 break;
19618 case DW_FORM_implicit_const:
19619 DW_SND (attr) = implicit_const;
19620 break;
19621 case DW_FORM_addrx:
19622 case DW_FORM_GNU_addr_index:
19623 *need_reprocess = true;
19624 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19625 info_ptr += bytes_read;
19626 break;
19627 case DW_FORM_strx:
19628 case DW_FORM_strx1:
19629 case DW_FORM_strx2:
19630 case DW_FORM_strx3:
19631 case DW_FORM_strx4:
19632 case DW_FORM_GNU_str_index:
19633 {
19634 ULONGEST str_index;
19635 if (form == DW_FORM_strx1)
19636 {
19637 str_index = read_1_byte (abfd, info_ptr);
19638 info_ptr += 1;
19639 }
19640 else if (form == DW_FORM_strx2)
19641 {
19642 str_index = read_2_bytes (abfd, info_ptr);
19643 info_ptr += 2;
19644 }
19645 else if (form == DW_FORM_strx3)
19646 {
19647 str_index = read_3_bytes (abfd, info_ptr);
19648 info_ptr += 3;
19649 }
19650 else if (form == DW_FORM_strx4)
19651 {
19652 str_index = read_4_bytes (abfd, info_ptr);
19653 info_ptr += 4;
19654 }
19655 else
19656 {
19657 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19658 info_ptr += bytes_read;
19659 }
19660 *need_reprocess = true;
19661 DW_UNSND (attr) = str_index;
19662 }
19663 break;
19664 default:
19665 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
19666 dwarf_form_name (form),
19667 bfd_get_filename (abfd));
19668 }
19669
19670 /* Super hack. */
19671 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
19672 attr->form = DW_FORM_GNU_ref_alt;
19673
19674 /* We have seen instances where the compiler tried to emit a byte
19675 size attribute of -1 which ended up being encoded as an unsigned
19676 0xffffffff. Although 0xffffffff is technically a valid size value,
19677 an object of this size seems pretty unlikely so we can relatively
19678 safely treat these cases as if the size attribute was invalid and
19679 treat them as zero by default. */
19680 if (attr->name == DW_AT_byte_size
19681 && form == DW_FORM_data4
19682 && DW_UNSND (attr) >= 0xffffffff)
19683 {
19684 complaint
19685 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19686 hex_string (DW_UNSND (attr)));
19687 DW_UNSND (attr) = 0;
19688 }
19689
19690 return info_ptr;
19691 }
19692
19693 /* Read an attribute described by an abbreviated attribute. */
19694
19695 static const gdb_byte *
19696 read_attribute (const struct die_reader_specs *reader,
19697 struct attribute *attr, struct attr_abbrev *abbrev,
19698 const gdb_byte *info_ptr, bool *need_reprocess)
19699 {
19700 attr->name = abbrev->name;
19701 return read_attribute_value (reader, attr, abbrev->form,
19702 abbrev->implicit_const, info_ptr,
19703 need_reprocess);
19704 }
19705
19706 /* Read dwarf information from a buffer. */
19707
19708 static unsigned int
19709 read_1_byte (bfd *abfd, const gdb_byte *buf)
19710 {
19711 return bfd_get_8 (abfd, buf);
19712 }
19713
19714 static int
19715 read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
19716 {
19717 return bfd_get_signed_8 (abfd, buf);
19718 }
19719
19720 static unsigned int
19721 read_2_bytes (bfd *abfd, const gdb_byte *buf)
19722 {
19723 return bfd_get_16 (abfd, buf);
19724 }
19725
19726 static int
19727 read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
19728 {
19729 return bfd_get_signed_16 (abfd, buf);
19730 }
19731
19732 static unsigned int
19733 read_3_bytes (bfd *abfd, const gdb_byte *buf)
19734 {
19735 unsigned int result = 0;
19736 for (int i = 0; i < 3; ++i)
19737 {
19738 unsigned char byte = bfd_get_8 (abfd, buf);
19739 buf++;
19740 result |= ((unsigned int) byte << (i * 8));
19741 }
19742 return result;
19743 }
19744
19745 static unsigned int
19746 read_4_bytes (bfd *abfd, const gdb_byte *buf)
19747 {
19748 return bfd_get_32 (abfd, buf);
19749 }
19750
19751 static int
19752 read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
19753 {
19754 return bfd_get_signed_32 (abfd, buf);
19755 }
19756
19757 static ULONGEST
19758 read_8_bytes (bfd *abfd, const gdb_byte *buf)
19759 {
19760 return bfd_get_64 (abfd, buf);
19761 }
19762
19763 static CORE_ADDR
19764 read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
19765 unsigned int *bytes_read)
19766 {
19767 struct comp_unit_head *cu_header = &cu->header;
19768 CORE_ADDR retval = 0;
19769
19770 if (cu_header->signed_addr_p)
19771 {
19772 switch (cu_header->addr_size)
19773 {
19774 case 2:
19775 retval = bfd_get_signed_16 (abfd, buf);
19776 break;
19777 case 4:
19778 retval = bfd_get_signed_32 (abfd, buf);
19779 break;
19780 case 8:
19781 retval = bfd_get_signed_64 (abfd, buf);
19782 break;
19783 default:
19784 internal_error (__FILE__, __LINE__,
19785 _("read_address: bad switch, signed [in module %s]"),
19786 bfd_get_filename (abfd));
19787 }
19788 }
19789 else
19790 {
19791 switch (cu_header->addr_size)
19792 {
19793 case 2:
19794 retval = bfd_get_16 (abfd, buf);
19795 break;
19796 case 4:
19797 retval = bfd_get_32 (abfd, buf);
19798 break;
19799 case 8:
19800 retval = bfd_get_64 (abfd, buf);
19801 break;
19802 default:
19803 internal_error (__FILE__, __LINE__,
19804 _("read_address: bad switch, "
19805 "unsigned [in module %s]"),
19806 bfd_get_filename (abfd));
19807 }
19808 }
19809
19810 *bytes_read = cu_header->addr_size;
19811 return retval;
19812 }
19813
19814 /* Read the initial length from a section. The (draft) DWARF 3
19815 specification allows the initial length to take up either 4 bytes
19816 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
19817 bytes describe the length and all offsets will be 8 bytes in length
19818 instead of 4.
19819
19820 An older, non-standard 64-bit format is also handled by this
19821 function. The older format in question stores the initial length
19822 as an 8-byte quantity without an escape value. Lengths greater
19823 than 2^32 aren't very common which means that the initial 4 bytes
19824 is almost always zero. Since a length value of zero doesn't make
19825 sense for the 32-bit format, this initial zero can be considered to
19826 be an escape value which indicates the presence of the older 64-bit
19827 format. As written, the code can't detect (old format) lengths
19828 greater than 4GB. If it becomes necessary to handle lengths
19829 somewhat larger than 4GB, we could allow other small values (such
19830 as the non-sensical values of 1, 2, and 3) to also be used as
19831 escape values indicating the presence of the old format.
19832
19833 The value returned via bytes_read should be used to increment the
19834 relevant pointer after calling read_initial_length().
19835
19836 [ Note: read_initial_length() and read_offset() are based on the
19837 document entitled "DWARF Debugging Information Format", revision
19838 3, draft 8, dated November 19, 2001. This document was obtained
19839 from:
19840
19841 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
19842
19843 This document is only a draft and is subject to change. (So beware.)
19844
19845 Details regarding the older, non-standard 64-bit format were
19846 determined empirically by examining 64-bit ELF files produced by
19847 the SGI toolchain on an IRIX 6.5 machine.
19848
19849 - Kevin, July 16, 2002
19850 ] */
19851
19852 static LONGEST
19853 read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
19854 {
19855 LONGEST length = bfd_get_32 (abfd, buf);
19856
19857 if (length == 0xffffffff)
19858 {
19859 length = bfd_get_64 (abfd, buf + 4);
19860 *bytes_read = 12;
19861 }
19862 else if (length == 0)
19863 {
19864 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
19865 length = bfd_get_64 (abfd, buf);
19866 *bytes_read = 8;
19867 }
19868 else
19869 {
19870 *bytes_read = 4;
19871 }
19872
19873 return length;
19874 }
19875
19876 /* Cover function for read_initial_length.
19877 Returns the length of the object at BUF, and stores the size of the
19878 initial length in *BYTES_READ and stores the size that offsets will be in
19879 *OFFSET_SIZE.
19880 If the initial length size is not equivalent to that specified in
19881 CU_HEADER then issue a complaint.
19882 This is useful when reading non-comp-unit headers. */
19883
19884 static LONGEST
19885 read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
19886 const struct comp_unit_head *cu_header,
19887 unsigned int *bytes_read,
19888 unsigned int *offset_size)
19889 {
19890 LONGEST length = read_initial_length (abfd, buf, bytes_read);
19891
19892 gdb_assert (cu_header->initial_length_size == 4
19893 || cu_header->initial_length_size == 8
19894 || cu_header->initial_length_size == 12);
19895
19896 if (cu_header->initial_length_size != *bytes_read)
19897 complaint (_("intermixed 32-bit and 64-bit DWARF sections"));
19898
19899 *offset_size = (*bytes_read == 4) ? 4 : 8;
19900 return length;
19901 }
19902
19903 /* Read an offset from the data stream. The size of the offset is
19904 given by cu_header->offset_size. */
19905
19906 static LONGEST
19907 read_offset (bfd *abfd, const gdb_byte *buf,
19908 const struct comp_unit_head *cu_header,
19909 unsigned int *bytes_read)
19910 {
19911 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
19912
19913 *bytes_read = cu_header->offset_size;
19914 return offset;
19915 }
19916
19917 /* Read an offset from the data stream. */
19918
19919 static LONGEST
19920 read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
19921 {
19922 LONGEST retval = 0;
19923
19924 switch (offset_size)
19925 {
19926 case 4:
19927 retval = bfd_get_32 (abfd, buf);
19928 break;
19929 case 8:
19930 retval = bfd_get_64 (abfd, buf);
19931 break;
19932 default:
19933 internal_error (__FILE__, __LINE__,
19934 _("read_offset_1: bad switch [in module %s]"),
19935 bfd_get_filename (abfd));
19936 }
19937
19938 return retval;
19939 }
19940
19941 static const gdb_byte *
19942 read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
19943 {
19944 /* If the size of a host char is 8 bits, we can return a pointer
19945 to the buffer, otherwise we have to copy the data to a buffer
19946 allocated on the temporary obstack. */
19947 gdb_assert (HOST_CHAR_BIT == 8);
19948 return buf;
19949 }
19950
19951 static const char *
19952 read_direct_string (bfd *abfd, const gdb_byte *buf,
19953 unsigned int *bytes_read_ptr)
19954 {
19955 /* If the size of a host char is 8 bits, we can return a pointer
19956 to the string, otherwise we have to copy the string to a buffer
19957 allocated on the temporary obstack. */
19958 gdb_assert (HOST_CHAR_BIT == 8);
19959 if (*buf == '\0')
19960 {
19961 *bytes_read_ptr = 1;
19962 return NULL;
19963 }
19964 *bytes_read_ptr = strlen ((const char *) buf) + 1;
19965 return (const char *) buf;
19966 }
19967
19968 /* Return pointer to string at section SECT offset STR_OFFSET with error
19969 reporting strings FORM_NAME and SECT_NAME. */
19970
19971 static const char *
19972 read_indirect_string_at_offset_from (struct objfile *objfile,
19973 bfd *abfd, LONGEST str_offset,
19974 struct dwarf2_section_info *sect,
19975 const char *form_name,
19976 const char *sect_name)
19977 {
19978 dwarf2_read_section (objfile, sect);
19979 if (sect->buffer == NULL)
19980 error (_("%s used without %s section [in module %s]"),
19981 form_name, sect_name, bfd_get_filename (abfd));
19982 if (str_offset >= sect->size)
19983 error (_("%s pointing outside of %s section [in module %s]"),
19984 form_name, sect_name, bfd_get_filename (abfd));
19985 gdb_assert (HOST_CHAR_BIT == 8);
19986 if (sect->buffer[str_offset] == '\0')
19987 return NULL;
19988 return (const char *) (sect->buffer + str_offset);
19989 }
19990
19991 /* Return pointer to string at .debug_str offset STR_OFFSET. */
19992
19993 static const char *
19994 read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19995 bfd *abfd, LONGEST str_offset)
19996 {
19997 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19998 abfd, str_offset,
19999 &dwarf2_per_objfile->str,
20000 "DW_FORM_strp", ".debug_str");
20001 }
20002
20003 /* Return pointer to string at .debug_line_str offset STR_OFFSET. */
20004
20005 static const char *
20006 read_indirect_line_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
20007 bfd *abfd, LONGEST str_offset)
20008 {
20009 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
20010 abfd, str_offset,
20011 &dwarf2_per_objfile->line_str,
20012 "DW_FORM_line_strp",
20013 ".debug_line_str");
20014 }
20015
20016 /* Read a string at offset STR_OFFSET in the .debug_str section from
20017 the .dwz file DWZ. Throw an error if the offset is too large. If
20018 the string consists of a single NUL byte, return NULL; otherwise
20019 return a pointer to the string. */
20020
20021 static const char *
20022 read_indirect_string_from_dwz (struct objfile *objfile, struct dwz_file *dwz,
20023 LONGEST str_offset)
20024 {
20025 dwarf2_read_section (objfile, &dwz->str);
20026
20027 if (dwz->str.buffer == NULL)
20028 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
20029 "section [in module %s]"),
20030 bfd_get_filename (dwz->dwz_bfd.get ()));
20031 if (str_offset >= dwz->str.size)
20032 error (_("DW_FORM_GNU_strp_alt pointing outside of "
20033 ".debug_str section [in module %s]"),
20034 bfd_get_filename (dwz->dwz_bfd.get ()));
20035 gdb_assert (HOST_CHAR_BIT == 8);
20036 if (dwz->str.buffer[str_offset] == '\0')
20037 return NULL;
20038 return (const char *) (dwz->str.buffer + str_offset);
20039 }
20040
20041 /* Return pointer to string at .debug_str offset as read from BUF.
20042 BUF is assumed to be in a compilation unit described by CU_HEADER.
20043 Return *BYTES_READ_PTR count of bytes read from BUF. */
20044
20045 static const char *
20046 read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
20047 const gdb_byte *buf,
20048 const struct comp_unit_head *cu_header,
20049 unsigned int *bytes_read_ptr)
20050 {
20051 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
20052
20053 return read_indirect_string_at_offset (dwarf2_per_objfile, abfd, str_offset);
20054 }
20055
20056 /* Return pointer to string at .debug_line_str offset as read from BUF.
20057 BUF is assumed to be in a compilation unit described by CU_HEADER.
20058 Return *BYTES_READ_PTR count of bytes read from BUF. */
20059
20060 static const char *
20061 read_indirect_line_string (struct dwarf2_per_objfile *dwarf2_per_objfile,
20062 bfd *abfd, const gdb_byte *buf,
20063 const struct comp_unit_head *cu_header,
20064 unsigned int *bytes_read_ptr)
20065 {
20066 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
20067
20068 return read_indirect_line_string_at_offset (dwarf2_per_objfile, abfd,
20069 str_offset);
20070 }
20071
20072 ULONGEST
20073 read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
20074 unsigned int *bytes_read_ptr)
20075 {
20076 ULONGEST result;
20077 unsigned int num_read;
20078 int shift;
20079 unsigned char byte;
20080
20081 result = 0;
20082 shift = 0;
20083 num_read = 0;
20084 while (1)
20085 {
20086 byte = bfd_get_8 (abfd, buf);
20087 buf++;
20088 num_read++;
20089 result |= ((ULONGEST) (byte & 127) << shift);
20090 if ((byte & 128) == 0)
20091 {
20092 break;
20093 }
20094 shift += 7;
20095 }
20096 *bytes_read_ptr = num_read;
20097 return result;
20098 }
20099
20100 static LONGEST
20101 read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
20102 unsigned int *bytes_read_ptr)
20103 {
20104 ULONGEST result;
20105 int shift, num_read;
20106 unsigned char byte;
20107
20108 result = 0;
20109 shift = 0;
20110 num_read = 0;
20111 while (1)
20112 {
20113 byte = bfd_get_8 (abfd, buf);
20114 buf++;
20115 num_read++;
20116 result |= ((ULONGEST) (byte & 127) << shift);
20117 shift += 7;
20118 if ((byte & 128) == 0)
20119 {
20120 break;
20121 }
20122 }
20123 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
20124 result |= -(((ULONGEST) 1) << shift);
20125 *bytes_read_ptr = num_read;
20126 return result;
20127 }
20128
20129 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
20130 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
20131 ADDR_SIZE is the size of addresses from the CU header. */
20132
20133 static CORE_ADDR
20134 read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
20135 unsigned int addr_index, gdb::optional<ULONGEST> addr_base,
20136 int addr_size)
20137 {
20138 struct objfile *objfile = dwarf2_per_objfile->objfile;
20139 bfd *abfd = objfile->obfd;
20140 const gdb_byte *info_ptr;
20141 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
20142
20143 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
20144 if (dwarf2_per_objfile->addr.buffer == NULL)
20145 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
20146 objfile_name (objfile));
20147 if (addr_base_or_zero + addr_index * addr_size
20148 >= dwarf2_per_objfile->addr.size)
20149 error (_("DW_FORM_addr_index pointing outside of "
20150 ".debug_addr section [in module %s]"),
20151 objfile_name (objfile));
20152 info_ptr = (dwarf2_per_objfile->addr.buffer
20153 + addr_base_or_zero + addr_index * addr_size);
20154 if (addr_size == 4)
20155 return bfd_get_32 (abfd, info_ptr);
20156 else
20157 return bfd_get_64 (abfd, info_ptr);
20158 }
20159
20160 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
20161
20162 static CORE_ADDR
20163 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
20164 {
20165 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
20166 cu->addr_base, cu->header.addr_size);
20167 }
20168
20169 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
20170
20171 static CORE_ADDR
20172 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
20173 unsigned int *bytes_read)
20174 {
20175 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
20176 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
20177
20178 return read_addr_index (cu, addr_index);
20179 }
20180
20181 /* Given an index in .debug_addr, fetch the value.
20182 NOTE: This can be called during dwarf expression evaluation,
20183 long after the debug information has been read, and thus per_cu->cu
20184 may no longer exist. */
20185
20186 CORE_ADDR
20187 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
20188 unsigned int addr_index)
20189 {
20190 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
20191 struct dwarf2_cu *cu = per_cu->cu;
20192 gdb::optional<ULONGEST> addr_base;
20193 int addr_size;
20194
20195 /* We need addr_base and addr_size.
20196 If we don't have PER_CU->cu, we have to get it.
20197 Nasty, but the alternative is storing the needed info in PER_CU,
20198 which at this point doesn't seem justified: it's not clear how frequently
20199 it would get used and it would increase the size of every PER_CU.
20200 Entry points like dwarf2_per_cu_addr_size do a similar thing
20201 so we're not in uncharted territory here.
20202 Alas we need to be a bit more complicated as addr_base is contained
20203 in the DIE.
20204
20205 We don't need to read the entire CU(/TU).
20206 We just need the header and top level die.
20207
20208 IWBN to use the aging mechanism to let us lazily later discard the CU.
20209 For now we skip this optimization. */
20210
20211 if (cu != NULL)
20212 {
20213 addr_base = cu->addr_base;
20214 addr_size = cu->header.addr_size;
20215 }
20216 else
20217 {
20218 cutu_reader reader (per_cu, NULL, 0, 0, false);
20219 addr_base = reader.cu->addr_base;
20220 addr_size = reader.cu->header.addr_size;
20221 }
20222
20223 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
20224 addr_size);
20225 }
20226
20227 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
20228 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
20229 DWO file. */
20230
20231 static const char *
20232 read_str_index (struct dwarf2_cu *cu,
20233 struct dwarf2_section_info *str_section,
20234 struct dwarf2_section_info *str_offsets_section,
20235 ULONGEST str_offsets_base, ULONGEST str_index)
20236 {
20237 struct dwarf2_per_objfile *dwarf2_per_objfile
20238 = cu->per_cu->dwarf2_per_objfile;
20239 struct objfile *objfile = dwarf2_per_objfile->objfile;
20240 const char *objf_name = objfile_name (objfile);
20241 bfd *abfd = objfile->obfd;
20242 const gdb_byte *info_ptr;
20243 ULONGEST str_offset;
20244 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
20245
20246 dwarf2_read_section (objfile, str_section);
20247 dwarf2_read_section (objfile, str_offsets_section);
20248 if (str_section->buffer == NULL)
20249 error (_("%s used without %s section"
20250 " in CU at offset %s [in module %s]"),
20251 form_name, get_section_name (str_section),
20252 sect_offset_str (cu->header.sect_off), objf_name);
20253 if (str_offsets_section->buffer == NULL)
20254 error (_("%s used without %s section"
20255 " in CU at offset %s [in module %s]"),
20256 form_name, get_section_name (str_section),
20257 sect_offset_str (cu->header.sect_off), objf_name);
20258 info_ptr = (str_offsets_section->buffer
20259 + str_offsets_base
20260 + str_index * cu->header.offset_size);
20261 if (cu->header.offset_size == 4)
20262 str_offset = bfd_get_32 (abfd, info_ptr);
20263 else
20264 str_offset = bfd_get_64 (abfd, info_ptr);
20265 if (str_offset >= str_section->size)
20266 error (_("Offset from %s pointing outside of"
20267 " .debug_str.dwo section in CU at offset %s [in module %s]"),
20268 form_name, sect_offset_str (cu->header.sect_off), objf_name);
20269 return (const char *) (str_section->buffer + str_offset);
20270 }
20271
20272 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
20273
20274 static const char *
20275 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
20276 {
20277 ULONGEST str_offsets_base = reader->cu->header.version >= 5
20278 ? reader->cu->header.addr_size : 0;
20279 return read_str_index (reader->cu,
20280 &reader->dwo_file->sections.str,
20281 &reader->dwo_file->sections.str_offsets,
20282 str_offsets_base, str_index);
20283 }
20284
20285 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
20286
20287 static const char *
20288 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
20289 {
20290 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20291 const char *objf_name = objfile_name (objfile);
20292 static const char form_name[] = "DW_FORM_GNU_str_index";
20293 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
20294
20295 if (!cu->str_offsets_base.has_value ())
20296 error (_("%s used in Fission stub without %s"
20297 " in CU at offset 0x%lx [in module %s]"),
20298 form_name, str_offsets_attr_name,
20299 (long) cu->header.offset_size, objf_name);
20300
20301 return read_str_index (cu,
20302 &cu->per_cu->dwarf2_per_objfile->str,
20303 &cu->per_cu->dwarf2_per_objfile->str_offsets,
20304 *cu->str_offsets_base, str_index);
20305 }
20306
20307 /* Return the length of an LEB128 number in BUF. */
20308
20309 static int
20310 leb128_size (const gdb_byte *buf)
20311 {
20312 const gdb_byte *begin = buf;
20313 gdb_byte byte;
20314
20315 while (1)
20316 {
20317 byte = *buf++;
20318 if ((byte & 128) == 0)
20319 return buf - begin;
20320 }
20321 }
20322
20323 static void
20324 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
20325 {
20326 switch (lang)
20327 {
20328 case DW_LANG_C89:
20329 case DW_LANG_C99:
20330 case DW_LANG_C11:
20331 case DW_LANG_C:
20332 case DW_LANG_UPC:
20333 cu->language = language_c;
20334 break;
20335 case DW_LANG_Java:
20336 case DW_LANG_C_plus_plus:
20337 case DW_LANG_C_plus_plus_11:
20338 case DW_LANG_C_plus_plus_14:
20339 cu->language = language_cplus;
20340 break;
20341 case DW_LANG_D:
20342 cu->language = language_d;
20343 break;
20344 case DW_LANG_Fortran77:
20345 case DW_LANG_Fortran90:
20346 case DW_LANG_Fortran95:
20347 case DW_LANG_Fortran03:
20348 case DW_LANG_Fortran08:
20349 cu->language = language_fortran;
20350 break;
20351 case DW_LANG_Go:
20352 cu->language = language_go;
20353 break;
20354 case DW_LANG_Mips_Assembler:
20355 cu->language = language_asm;
20356 break;
20357 case DW_LANG_Ada83:
20358 case DW_LANG_Ada95:
20359 cu->language = language_ada;
20360 break;
20361 case DW_LANG_Modula2:
20362 cu->language = language_m2;
20363 break;
20364 case DW_LANG_Pascal83:
20365 cu->language = language_pascal;
20366 break;
20367 case DW_LANG_ObjC:
20368 cu->language = language_objc;
20369 break;
20370 case DW_LANG_Rust:
20371 case DW_LANG_Rust_old:
20372 cu->language = language_rust;
20373 break;
20374 case DW_LANG_Cobol74:
20375 case DW_LANG_Cobol85:
20376 default:
20377 cu->language = language_minimal;
20378 break;
20379 }
20380 cu->language_defn = language_def (cu->language);
20381 }
20382
20383 /* Return the named attribute or NULL if not there. */
20384
20385 static struct attribute *
20386 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20387 {
20388 for (;;)
20389 {
20390 unsigned int i;
20391 struct attribute *spec = NULL;
20392
20393 for (i = 0; i < die->num_attrs; ++i)
20394 {
20395 if (die->attrs[i].name == name)
20396 return &die->attrs[i];
20397 if (die->attrs[i].name == DW_AT_specification
20398 || die->attrs[i].name == DW_AT_abstract_origin)
20399 spec = &die->attrs[i];
20400 }
20401
20402 if (!spec)
20403 break;
20404
20405 die = follow_die_ref (die, spec, &cu);
20406 }
20407
20408 return NULL;
20409 }
20410
20411 /* Return the named attribute or NULL if not there,
20412 but do not follow DW_AT_specification, etc.
20413 This is for use in contexts where we're reading .debug_types dies.
20414 Following DW_AT_specification, DW_AT_abstract_origin will take us
20415 back up the chain, and we want to go down. */
20416
20417 static struct attribute *
20418 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
20419 {
20420 unsigned int i;
20421
20422 for (i = 0; i < die->num_attrs; ++i)
20423 if (die->attrs[i].name == name)
20424 return &die->attrs[i];
20425
20426 return NULL;
20427 }
20428
20429 /* Return the string associated with a string-typed attribute, or NULL if it
20430 is either not found or is of an incorrect type. */
20431
20432 static const char *
20433 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20434 {
20435 struct attribute *attr;
20436 const char *str = NULL;
20437
20438 attr = dwarf2_attr (die, name, cu);
20439
20440 if (attr != NULL)
20441 {
20442 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
20443 || attr->form == DW_FORM_string
20444 || attr->form == DW_FORM_strx
20445 || attr->form == DW_FORM_strx1
20446 || attr->form == DW_FORM_strx2
20447 || attr->form == DW_FORM_strx3
20448 || attr->form == DW_FORM_strx4
20449 || attr->form == DW_FORM_GNU_str_index
20450 || attr->form == DW_FORM_GNU_strp_alt)
20451 str = DW_STRING (attr);
20452 else
20453 complaint (_("string type expected for attribute %s for "
20454 "DIE at %s in module %s"),
20455 dwarf_attr_name (name), sect_offset_str (die->sect_off),
20456 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
20457 }
20458
20459 return str;
20460 }
20461
20462 /* Return the dwo name or NULL if not present. If present, it is in either
20463 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
20464 static const char *
20465 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
20466 {
20467 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
20468 if (dwo_name == nullptr)
20469 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
20470 return dwo_name;
20471 }
20472
20473 /* Return non-zero iff the attribute NAME is defined for the given DIE,
20474 and holds a non-zero value. This function should only be used for
20475 DW_FORM_flag or DW_FORM_flag_present attributes. */
20476
20477 static int
20478 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
20479 {
20480 struct attribute *attr = dwarf2_attr (die, name, cu);
20481
20482 return (attr && DW_UNSND (attr));
20483 }
20484
20485 static int
20486 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
20487 {
20488 /* A DIE is a declaration if it has a DW_AT_declaration attribute
20489 which value is non-zero. However, we have to be careful with
20490 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
20491 (via dwarf2_flag_true_p) follows this attribute. So we may
20492 end up accidently finding a declaration attribute that belongs
20493 to a different DIE referenced by the specification attribute,
20494 even though the given DIE does not have a declaration attribute. */
20495 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
20496 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
20497 }
20498
20499 /* Return the die giving the specification for DIE, if there is
20500 one. *SPEC_CU is the CU containing DIE on input, and the CU
20501 containing the return value on output. If there is no
20502 specification, but there is an abstract origin, that is
20503 returned. */
20504
20505 static struct die_info *
20506 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
20507 {
20508 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
20509 *spec_cu);
20510
20511 if (spec_attr == NULL)
20512 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
20513
20514 if (spec_attr == NULL)
20515 return NULL;
20516 else
20517 return follow_die_ref (die, spec_attr, spec_cu);
20518 }
20519
20520 /* Stub for free_line_header to match void * callback types. */
20521
20522 static void
20523 free_line_header_voidp (void *arg)
20524 {
20525 struct line_header *lh = (struct line_header *) arg;
20526
20527 delete lh;
20528 }
20529
20530 void
20531 line_header::add_include_dir (const char *include_dir)
20532 {
20533 if (dwarf_line_debug >= 2)
20534 {
20535 size_t new_size;
20536 if (version >= 5)
20537 new_size = m_include_dirs.size ();
20538 else
20539 new_size = m_include_dirs.size () + 1;
20540 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
20541 new_size, include_dir);
20542 }
20543 m_include_dirs.push_back (include_dir);
20544 }
20545
20546 void
20547 line_header::add_file_name (const char *name,
20548 dir_index d_index,
20549 unsigned int mod_time,
20550 unsigned int length)
20551 {
20552 if (dwarf_line_debug >= 2)
20553 {
20554 size_t new_size;
20555 if (version >= 5)
20556 new_size = file_names_size ();
20557 else
20558 new_size = file_names_size () + 1;
20559 fprintf_unfiltered (gdb_stdlog, "Adding file %zu: %s\n",
20560 new_size, name);
20561 }
20562 m_file_names.emplace_back (name, d_index, mod_time, length);
20563 }
20564
20565 /* A convenience function to find the proper .debug_line section for a CU. */
20566
20567 static struct dwarf2_section_info *
20568 get_debug_line_section (struct dwarf2_cu *cu)
20569 {
20570 struct dwarf2_section_info *section;
20571 struct dwarf2_per_objfile *dwarf2_per_objfile
20572 = cu->per_cu->dwarf2_per_objfile;
20573
20574 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
20575 DWO file. */
20576 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20577 section = &cu->dwo_unit->dwo_file->sections.line;
20578 else if (cu->per_cu->is_dwz)
20579 {
20580 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
20581
20582 section = &dwz->line;
20583 }
20584 else
20585 section = &dwarf2_per_objfile->line;
20586
20587 return section;
20588 }
20589
20590 /* Read directory or file name entry format, starting with byte of
20591 format count entries, ULEB128 pairs of entry formats, ULEB128 of
20592 entries count and the entries themselves in the described entry
20593 format. */
20594
20595 static void
20596 read_formatted_entries (struct dwarf2_per_objfile *dwarf2_per_objfile,
20597 bfd *abfd, const gdb_byte **bufp,
20598 struct line_header *lh,
20599 const struct comp_unit_head *cu_header,
20600 void (*callback) (struct line_header *lh,
20601 const char *name,
20602 dir_index d_index,
20603 unsigned int mod_time,
20604 unsigned int length))
20605 {
20606 gdb_byte format_count, formati;
20607 ULONGEST data_count, datai;
20608 const gdb_byte *buf = *bufp;
20609 const gdb_byte *format_header_data;
20610 unsigned int bytes_read;
20611
20612 format_count = read_1_byte (abfd, buf);
20613 buf += 1;
20614 format_header_data = buf;
20615 for (formati = 0; formati < format_count; formati++)
20616 {
20617 read_unsigned_leb128 (abfd, buf, &bytes_read);
20618 buf += bytes_read;
20619 read_unsigned_leb128 (abfd, buf, &bytes_read);
20620 buf += bytes_read;
20621 }
20622
20623 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
20624 buf += bytes_read;
20625 for (datai = 0; datai < data_count; datai++)
20626 {
20627 const gdb_byte *format = format_header_data;
20628 struct file_entry fe;
20629
20630 for (formati = 0; formati < format_count; formati++)
20631 {
20632 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
20633 format += bytes_read;
20634
20635 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
20636 format += bytes_read;
20637
20638 gdb::optional<const char *> string;
20639 gdb::optional<unsigned int> uint;
20640
20641 switch (form)
20642 {
20643 case DW_FORM_string:
20644 string.emplace (read_direct_string (abfd, buf, &bytes_read));
20645 buf += bytes_read;
20646 break;
20647
20648 case DW_FORM_line_strp:
20649 string.emplace (read_indirect_line_string (dwarf2_per_objfile,
20650 abfd, buf,
20651 cu_header,
20652 &bytes_read));
20653 buf += bytes_read;
20654 break;
20655
20656 case DW_FORM_data1:
20657 uint.emplace (read_1_byte (abfd, buf));
20658 buf += 1;
20659 break;
20660
20661 case DW_FORM_data2:
20662 uint.emplace (read_2_bytes (abfd, buf));
20663 buf += 2;
20664 break;
20665
20666 case DW_FORM_data4:
20667 uint.emplace (read_4_bytes (abfd, buf));
20668 buf += 4;
20669 break;
20670
20671 case DW_FORM_data8:
20672 uint.emplace (read_8_bytes (abfd, buf));
20673 buf += 8;
20674 break;
20675
20676 case DW_FORM_data16:
20677 /* This is used for MD5, but file_entry does not record MD5s. */
20678 buf += 16;
20679 break;
20680
20681 case DW_FORM_udata:
20682 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
20683 buf += bytes_read;
20684 break;
20685
20686 case DW_FORM_block:
20687 /* It is valid only for DW_LNCT_timestamp which is ignored by
20688 current GDB. */
20689 break;
20690 }
20691
20692 switch (content_type)
20693 {
20694 case DW_LNCT_path:
20695 if (string.has_value ())
20696 fe.name = *string;
20697 break;
20698 case DW_LNCT_directory_index:
20699 if (uint.has_value ())
20700 fe.d_index = (dir_index) *uint;
20701 break;
20702 case DW_LNCT_timestamp:
20703 if (uint.has_value ())
20704 fe.mod_time = *uint;
20705 break;
20706 case DW_LNCT_size:
20707 if (uint.has_value ())
20708 fe.length = *uint;
20709 break;
20710 case DW_LNCT_MD5:
20711 break;
20712 default:
20713 complaint (_("Unknown format content type %s"),
20714 pulongest (content_type));
20715 }
20716 }
20717
20718 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
20719 }
20720
20721 *bufp = buf;
20722 }
20723
20724 /* Read the statement program header starting at OFFSET in
20725 .debug_line, or .debug_line.dwo. Return a pointer
20726 to a struct line_header, allocated using xmalloc.
20727 Returns NULL if there is a problem reading the header, e.g., if it
20728 has a version we don't understand.
20729
20730 NOTE: the strings in the include directory and file name tables of
20731 the returned object point into the dwarf line section buffer,
20732 and must not be freed. */
20733
20734 static line_header_up
20735 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
20736 {
20737 const gdb_byte *line_ptr;
20738 unsigned int bytes_read, offset_size;
20739 int i;
20740 const char *cur_dir, *cur_file;
20741 struct dwarf2_section_info *section;
20742 bfd *abfd;
20743 struct dwarf2_per_objfile *dwarf2_per_objfile
20744 = cu->per_cu->dwarf2_per_objfile;
20745
20746 section = get_debug_line_section (cu);
20747 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
20748 if (section->buffer == NULL)
20749 {
20750 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20751 complaint (_("missing .debug_line.dwo section"));
20752 else
20753 complaint (_("missing .debug_line section"));
20754 return 0;
20755 }
20756
20757 /* We can't do this until we know the section is non-empty.
20758 Only then do we know we have such a section. */
20759 abfd = get_section_bfd_owner (section);
20760
20761 /* Make sure that at least there's room for the total_length field.
20762 That could be 12 bytes long, but we're just going to fudge that. */
20763 if (to_underlying (sect_off) + 4 >= section->size)
20764 {
20765 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20766 return 0;
20767 }
20768
20769 line_header_up lh (new line_header ());
20770
20771 lh->sect_off = sect_off;
20772 lh->offset_in_dwz = cu->per_cu->is_dwz;
20773
20774 line_ptr = section->buffer + to_underlying (sect_off);
20775
20776 /* Read in the header. */
20777 lh->total_length =
20778 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
20779 &bytes_read, &offset_size);
20780 line_ptr += bytes_read;
20781
20782 const gdb_byte *start_here = line_ptr;
20783
20784 if (line_ptr + lh->total_length > (section->buffer + section->size))
20785 {
20786 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20787 return 0;
20788 }
20789 lh->statement_program_end = start_here + lh->total_length;
20790 lh->version = read_2_bytes (abfd, line_ptr);
20791 line_ptr += 2;
20792 if (lh->version > 5)
20793 {
20794 /* This is a version we don't understand. The format could have
20795 changed in ways we don't handle properly so just punt. */
20796 complaint (_("unsupported version in .debug_line section"));
20797 return NULL;
20798 }
20799 if (lh->version >= 5)
20800 {
20801 gdb_byte segment_selector_size;
20802
20803 /* Skip address size. */
20804 read_1_byte (abfd, line_ptr);
20805 line_ptr += 1;
20806
20807 segment_selector_size = read_1_byte (abfd, line_ptr);
20808 line_ptr += 1;
20809 if (segment_selector_size != 0)
20810 {
20811 complaint (_("unsupported segment selector size %u "
20812 "in .debug_line section"),
20813 segment_selector_size);
20814 return NULL;
20815 }
20816 }
20817 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
20818 line_ptr += offset_size;
20819 lh->statement_program_start = line_ptr + lh->header_length;
20820 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
20821 line_ptr += 1;
20822 if (lh->version >= 4)
20823 {
20824 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
20825 line_ptr += 1;
20826 }
20827 else
20828 lh->maximum_ops_per_instruction = 1;
20829
20830 if (lh->maximum_ops_per_instruction == 0)
20831 {
20832 lh->maximum_ops_per_instruction = 1;
20833 complaint (_("invalid maximum_ops_per_instruction "
20834 "in `.debug_line' section"));
20835 }
20836
20837 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
20838 line_ptr += 1;
20839 lh->line_base = read_1_signed_byte (abfd, line_ptr);
20840 line_ptr += 1;
20841 lh->line_range = read_1_byte (abfd, line_ptr);
20842 line_ptr += 1;
20843 lh->opcode_base = read_1_byte (abfd, line_ptr);
20844 line_ptr += 1;
20845 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
20846
20847 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
20848 for (i = 1; i < lh->opcode_base; ++i)
20849 {
20850 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
20851 line_ptr += 1;
20852 }
20853
20854 if (lh->version >= 5)
20855 {
20856 /* Read directory table. */
20857 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20858 &cu->header,
20859 [] (struct line_header *header, const char *name,
20860 dir_index d_index, unsigned int mod_time,
20861 unsigned int length)
20862 {
20863 header->add_include_dir (name);
20864 });
20865
20866 /* Read file name table. */
20867 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20868 &cu->header,
20869 [] (struct line_header *header, const char *name,
20870 dir_index d_index, unsigned int mod_time,
20871 unsigned int length)
20872 {
20873 header->add_file_name (name, d_index, mod_time, length);
20874 });
20875 }
20876 else
20877 {
20878 /* Read directory table. */
20879 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20880 {
20881 line_ptr += bytes_read;
20882 lh->add_include_dir (cur_dir);
20883 }
20884 line_ptr += bytes_read;
20885
20886 /* Read file name table. */
20887 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20888 {
20889 unsigned int mod_time, length;
20890 dir_index d_index;
20891
20892 line_ptr += bytes_read;
20893 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20894 line_ptr += bytes_read;
20895 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20896 line_ptr += bytes_read;
20897 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20898 line_ptr += bytes_read;
20899
20900 lh->add_file_name (cur_file, d_index, mod_time, length);
20901 }
20902 line_ptr += bytes_read;
20903 }
20904
20905 if (line_ptr > (section->buffer + section->size))
20906 complaint (_("line number info header doesn't "
20907 "fit in `.debug_line' section"));
20908
20909 return lh;
20910 }
20911
20912 /* Subroutine of dwarf_decode_lines to simplify it.
20913 Return the file name of the psymtab for the given file_entry.
20914 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20915 If space for the result is malloc'd, *NAME_HOLDER will be set.
20916 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
20917
20918 static const char *
20919 psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
20920 const dwarf2_psymtab *pst,
20921 const char *comp_dir,
20922 gdb::unique_xmalloc_ptr<char> *name_holder)
20923 {
20924 const char *include_name = fe.name;
20925 const char *include_name_to_compare = include_name;
20926 const char *pst_filename;
20927 int file_is_pst;
20928
20929 const char *dir_name = fe.include_dir (lh);
20930
20931 gdb::unique_xmalloc_ptr<char> hold_compare;
20932 if (!IS_ABSOLUTE_PATH (include_name)
20933 && (dir_name != NULL || comp_dir != NULL))
20934 {
20935 /* Avoid creating a duplicate psymtab for PST.
20936 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20937 Before we do the comparison, however, we need to account
20938 for DIR_NAME and COMP_DIR.
20939 First prepend dir_name (if non-NULL). If we still don't
20940 have an absolute path prepend comp_dir (if non-NULL).
20941 However, the directory we record in the include-file's
20942 psymtab does not contain COMP_DIR (to match the
20943 corresponding symtab(s)).
20944
20945 Example:
20946
20947 bash$ cd /tmp
20948 bash$ gcc -g ./hello.c
20949 include_name = "hello.c"
20950 dir_name = "."
20951 DW_AT_comp_dir = comp_dir = "/tmp"
20952 DW_AT_name = "./hello.c"
20953
20954 */
20955
20956 if (dir_name != NULL)
20957 {
20958 name_holder->reset (concat (dir_name, SLASH_STRING,
20959 include_name, (char *) NULL));
20960 include_name = name_holder->get ();
20961 include_name_to_compare = include_name;
20962 }
20963 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20964 {
20965 hold_compare.reset (concat (comp_dir, SLASH_STRING,
20966 include_name, (char *) NULL));
20967 include_name_to_compare = hold_compare.get ();
20968 }
20969 }
20970
20971 pst_filename = pst->filename;
20972 gdb::unique_xmalloc_ptr<char> copied_name;
20973 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20974 {
20975 copied_name.reset (concat (pst->dirname, SLASH_STRING,
20976 pst_filename, (char *) NULL));
20977 pst_filename = copied_name.get ();
20978 }
20979
20980 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
20981
20982 if (file_is_pst)
20983 return NULL;
20984 return include_name;
20985 }
20986
20987 /* State machine to track the state of the line number program. */
20988
20989 class lnp_state_machine
20990 {
20991 public:
20992 /* Initialize a machine state for the start of a line number
20993 program. */
20994 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
20995 bool record_lines_p);
20996
20997 file_entry *current_file ()
20998 {
20999 /* lh->file_names is 0-based, but the file name numbers in the
21000 statement program are 1-based. */
21001 return m_line_header->file_name_at (m_file);
21002 }
21003
21004 /* Record the line in the state machine. END_SEQUENCE is true if
21005 we're processing the end of a sequence. */
21006 void record_line (bool end_sequence);
21007
21008 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
21009 nop-out rest of the lines in this sequence. */
21010 void check_line_address (struct dwarf2_cu *cu,
21011 const gdb_byte *line_ptr,
21012 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
21013
21014 void handle_set_discriminator (unsigned int discriminator)
21015 {
21016 m_discriminator = discriminator;
21017 m_line_has_non_zero_discriminator |= discriminator != 0;
21018 }
21019
21020 /* Handle DW_LNE_set_address. */
21021 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
21022 {
21023 m_op_index = 0;
21024 address += baseaddr;
21025 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
21026 }
21027
21028 /* Handle DW_LNS_advance_pc. */
21029 void handle_advance_pc (CORE_ADDR adjust);
21030
21031 /* Handle a special opcode. */
21032 void handle_special_opcode (unsigned char op_code);
21033
21034 /* Handle DW_LNS_advance_line. */
21035 void handle_advance_line (int line_delta)
21036 {
21037 advance_line (line_delta);
21038 }
21039
21040 /* Handle DW_LNS_set_file. */
21041 void handle_set_file (file_name_index file);
21042
21043 /* Handle DW_LNS_negate_stmt. */
21044 void handle_negate_stmt ()
21045 {
21046 m_is_stmt = !m_is_stmt;
21047 }
21048
21049 /* Handle DW_LNS_const_add_pc. */
21050 void handle_const_add_pc ();
21051
21052 /* Handle DW_LNS_fixed_advance_pc. */
21053 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
21054 {
21055 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21056 m_op_index = 0;
21057 }
21058
21059 /* Handle DW_LNS_copy. */
21060 void handle_copy ()
21061 {
21062 record_line (false);
21063 m_discriminator = 0;
21064 }
21065
21066 /* Handle DW_LNE_end_sequence. */
21067 void handle_end_sequence ()
21068 {
21069 m_currently_recording_lines = true;
21070 }
21071
21072 private:
21073 /* Advance the line by LINE_DELTA. */
21074 void advance_line (int line_delta)
21075 {
21076 m_line += line_delta;
21077
21078 if (line_delta != 0)
21079 m_line_has_non_zero_discriminator = m_discriminator != 0;
21080 }
21081
21082 struct dwarf2_cu *m_cu;
21083
21084 gdbarch *m_gdbarch;
21085
21086 /* True if we're recording lines.
21087 Otherwise we're building partial symtabs and are just interested in
21088 finding include files mentioned by the line number program. */
21089 bool m_record_lines_p;
21090
21091 /* The line number header. */
21092 line_header *m_line_header;
21093
21094 /* These are part of the standard DWARF line number state machine,
21095 and initialized according to the DWARF spec. */
21096
21097 unsigned char m_op_index = 0;
21098 /* The line table index of the current file. */
21099 file_name_index m_file = 1;
21100 unsigned int m_line = 1;
21101
21102 /* These are initialized in the constructor. */
21103
21104 CORE_ADDR m_address;
21105 bool m_is_stmt;
21106 unsigned int m_discriminator;
21107
21108 /* Additional bits of state we need to track. */
21109
21110 /* The last file that we called dwarf2_start_subfile for.
21111 This is only used for TLLs. */
21112 unsigned int m_last_file = 0;
21113 /* The last file a line number was recorded for. */
21114 struct subfile *m_last_subfile = NULL;
21115
21116 /* When true, record the lines we decode. */
21117 bool m_currently_recording_lines = false;
21118
21119 /* The last line number that was recorded, used to coalesce
21120 consecutive entries for the same line. This can happen, for
21121 example, when discriminators are present. PR 17276. */
21122 unsigned int m_last_line = 0;
21123 bool m_line_has_non_zero_discriminator = false;
21124 };
21125
21126 void
21127 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
21128 {
21129 CORE_ADDR addr_adj = (((m_op_index + adjust)
21130 / m_line_header->maximum_ops_per_instruction)
21131 * m_line_header->minimum_instruction_length);
21132 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21133 m_op_index = ((m_op_index + adjust)
21134 % m_line_header->maximum_ops_per_instruction);
21135 }
21136
21137 void
21138 lnp_state_machine::handle_special_opcode (unsigned char op_code)
21139 {
21140 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
21141 CORE_ADDR addr_adj = (((m_op_index
21142 + (adj_opcode / m_line_header->line_range))
21143 / m_line_header->maximum_ops_per_instruction)
21144 * m_line_header->minimum_instruction_length);
21145 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21146 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
21147 % m_line_header->maximum_ops_per_instruction);
21148
21149 int line_delta = (m_line_header->line_base
21150 + (adj_opcode % m_line_header->line_range));
21151 advance_line (line_delta);
21152 record_line (false);
21153 m_discriminator = 0;
21154 }
21155
21156 void
21157 lnp_state_machine::handle_set_file (file_name_index file)
21158 {
21159 m_file = file;
21160
21161 const file_entry *fe = current_file ();
21162 if (fe == NULL)
21163 dwarf2_debug_line_missing_file_complaint ();
21164 else if (m_record_lines_p)
21165 {
21166 const char *dir = fe->include_dir (m_line_header);
21167
21168 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
21169 m_line_has_non_zero_discriminator = m_discriminator != 0;
21170 dwarf2_start_subfile (m_cu, fe->name, dir);
21171 }
21172 }
21173
21174 void
21175 lnp_state_machine::handle_const_add_pc ()
21176 {
21177 CORE_ADDR adjust
21178 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
21179
21180 CORE_ADDR addr_adj
21181 = (((m_op_index + adjust)
21182 / m_line_header->maximum_ops_per_instruction)
21183 * m_line_header->minimum_instruction_length);
21184
21185 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21186 m_op_index = ((m_op_index + adjust)
21187 % m_line_header->maximum_ops_per_instruction);
21188 }
21189
21190 /* Return non-zero if we should add LINE to the line number table.
21191 LINE is the line to add, LAST_LINE is the last line that was added,
21192 LAST_SUBFILE is the subfile for LAST_LINE.
21193 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
21194 had a non-zero discriminator.
21195
21196 We have to be careful in the presence of discriminators.
21197 E.g., for this line:
21198
21199 for (i = 0; i < 100000; i++);
21200
21201 clang can emit four line number entries for that one line,
21202 each with a different discriminator.
21203 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
21204
21205 However, we want gdb to coalesce all four entries into one.
21206 Otherwise the user could stepi into the middle of the line and
21207 gdb would get confused about whether the pc really was in the
21208 middle of the line.
21209
21210 Things are further complicated by the fact that two consecutive
21211 line number entries for the same line is a heuristic used by gcc
21212 to denote the end of the prologue. So we can't just discard duplicate
21213 entries, we have to be selective about it. The heuristic we use is
21214 that we only collapse consecutive entries for the same line if at least
21215 one of those entries has a non-zero discriminator. PR 17276.
21216
21217 Note: Addresses in the line number state machine can never go backwards
21218 within one sequence, thus this coalescing is ok. */
21219
21220 static int
21221 dwarf_record_line_p (struct dwarf2_cu *cu,
21222 unsigned int line, unsigned int last_line,
21223 int line_has_non_zero_discriminator,
21224 struct subfile *last_subfile)
21225 {
21226 if (cu->get_builder ()->get_current_subfile () != last_subfile)
21227 return 1;
21228 if (line != last_line)
21229 return 1;
21230 /* Same line for the same file that we've seen already.
21231 As a last check, for pr 17276, only record the line if the line
21232 has never had a non-zero discriminator. */
21233 if (!line_has_non_zero_discriminator)
21234 return 1;
21235 return 0;
21236 }
21237
21238 /* Use the CU's builder to record line number LINE beginning at
21239 address ADDRESS in the line table of subfile SUBFILE. */
21240
21241 static void
21242 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
21243 unsigned int line, CORE_ADDR address,
21244 struct dwarf2_cu *cu)
21245 {
21246 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
21247
21248 if (dwarf_line_debug)
21249 {
21250 fprintf_unfiltered (gdb_stdlog,
21251 "Recording line %u, file %s, address %s\n",
21252 line, lbasename (subfile->name),
21253 paddress (gdbarch, address));
21254 }
21255
21256 if (cu != nullptr)
21257 cu->get_builder ()->record_line (subfile, line, addr);
21258 }
21259
21260 /* Subroutine of dwarf_decode_lines_1 to simplify it.
21261 Mark the end of a set of line number records.
21262 The arguments are the same as for dwarf_record_line_1.
21263 If SUBFILE is NULL the request is ignored. */
21264
21265 static void
21266 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
21267 CORE_ADDR address, struct dwarf2_cu *cu)
21268 {
21269 if (subfile == NULL)
21270 return;
21271
21272 if (dwarf_line_debug)
21273 {
21274 fprintf_unfiltered (gdb_stdlog,
21275 "Finishing current line, file %s, address %s\n",
21276 lbasename (subfile->name),
21277 paddress (gdbarch, address));
21278 }
21279
21280 dwarf_record_line_1 (gdbarch, subfile, 0, address, cu);
21281 }
21282
21283 void
21284 lnp_state_machine::record_line (bool end_sequence)
21285 {
21286 if (dwarf_line_debug)
21287 {
21288 fprintf_unfiltered (gdb_stdlog,
21289 "Processing actual line %u: file %u,"
21290 " address %s, is_stmt %u, discrim %u%s\n",
21291 m_line, m_file,
21292 paddress (m_gdbarch, m_address),
21293 m_is_stmt, m_discriminator,
21294 (end_sequence ? "\t(end sequence)" : ""));
21295 }
21296
21297 file_entry *fe = current_file ();
21298
21299 if (fe == NULL)
21300 dwarf2_debug_line_missing_file_complaint ();
21301 /* For now we ignore lines not starting on an instruction boundary.
21302 But not when processing end_sequence for compatibility with the
21303 previous version of the code. */
21304 else if (m_op_index == 0 || end_sequence)
21305 {
21306 fe->included_p = 1;
21307 if (m_record_lines_p
21308 && (producer_is_codewarrior (m_cu) || m_is_stmt || end_sequence))
21309 {
21310 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
21311 || end_sequence)
21312 {
21313 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
21314 m_currently_recording_lines ? m_cu : nullptr);
21315 }
21316
21317 if (!end_sequence)
21318 {
21319 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
21320 m_line_has_non_zero_discriminator,
21321 m_last_subfile))
21322 {
21323 buildsym_compunit *builder = m_cu->get_builder ();
21324 dwarf_record_line_1 (m_gdbarch,
21325 builder->get_current_subfile (),
21326 m_line, m_address,
21327 m_currently_recording_lines ? m_cu : nullptr);
21328 }
21329 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
21330 m_last_line = m_line;
21331 }
21332 }
21333 }
21334 }
21335
21336 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
21337 line_header *lh, bool record_lines_p)
21338 {
21339 m_cu = cu;
21340 m_gdbarch = arch;
21341 m_record_lines_p = record_lines_p;
21342 m_line_header = lh;
21343
21344 m_currently_recording_lines = true;
21345
21346 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
21347 was a line entry for it so that the backend has a chance to adjust it
21348 and also record it in case it needs it. This is currently used by MIPS
21349 code, cf. `mips_adjust_dwarf2_line'. */
21350 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
21351 m_is_stmt = lh->default_is_stmt;
21352 m_discriminator = 0;
21353 }
21354
21355 void
21356 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
21357 const gdb_byte *line_ptr,
21358 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
21359 {
21360 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
21361 the pc range of the CU. However, we restrict the test to only ADDRESS
21362 values of zero to preserve GDB's previous behaviour which is to handle
21363 the specific case of a function being GC'd by the linker. */
21364
21365 if (address == 0 && address < unrelocated_lowpc)
21366 {
21367 /* This line table is for a function which has been
21368 GCd by the linker. Ignore it. PR gdb/12528 */
21369
21370 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21371 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
21372
21373 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
21374 line_offset, objfile_name (objfile));
21375 m_currently_recording_lines = false;
21376 /* Note: m_currently_recording_lines is left as false until we see
21377 DW_LNE_end_sequence. */
21378 }
21379 }
21380
21381 /* Subroutine of dwarf_decode_lines to simplify it.
21382 Process the line number information in LH.
21383 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
21384 program in order to set included_p for every referenced header. */
21385
21386 static void
21387 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
21388 const int decode_for_pst_p, CORE_ADDR lowpc)
21389 {
21390 const gdb_byte *line_ptr, *extended_end;
21391 const gdb_byte *line_end;
21392 unsigned int bytes_read, extended_len;
21393 unsigned char op_code, extended_op;
21394 CORE_ADDR baseaddr;
21395 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21396 bfd *abfd = objfile->obfd;
21397 struct gdbarch *gdbarch = get_objfile_arch (objfile);
21398 /* True if we're recording line info (as opposed to building partial
21399 symtabs and just interested in finding include files mentioned by
21400 the line number program). */
21401 bool record_lines_p = !decode_for_pst_p;
21402
21403 baseaddr = objfile->text_section_offset ();
21404
21405 line_ptr = lh->statement_program_start;
21406 line_end = lh->statement_program_end;
21407
21408 /* Read the statement sequences until there's nothing left. */
21409 while (line_ptr < line_end)
21410 {
21411 /* The DWARF line number program state machine. Reset the state
21412 machine at the start of each sequence. */
21413 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
21414 bool end_sequence = false;
21415
21416 if (record_lines_p)
21417 {
21418 /* Start a subfile for the current file of the state
21419 machine. */
21420 const file_entry *fe = state_machine.current_file ();
21421
21422 if (fe != NULL)
21423 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
21424 }
21425
21426 /* Decode the table. */
21427 while (line_ptr < line_end && !end_sequence)
21428 {
21429 op_code = read_1_byte (abfd, line_ptr);
21430 line_ptr += 1;
21431
21432 if (op_code >= lh->opcode_base)
21433 {
21434 /* Special opcode. */
21435 state_machine.handle_special_opcode (op_code);
21436 }
21437 else switch (op_code)
21438 {
21439 case DW_LNS_extended_op:
21440 extended_len = read_unsigned_leb128 (abfd, line_ptr,
21441 &bytes_read);
21442 line_ptr += bytes_read;
21443 extended_end = line_ptr + extended_len;
21444 extended_op = read_1_byte (abfd, line_ptr);
21445 line_ptr += 1;
21446 switch (extended_op)
21447 {
21448 case DW_LNE_end_sequence:
21449 state_machine.handle_end_sequence ();
21450 end_sequence = true;
21451 break;
21452 case DW_LNE_set_address:
21453 {
21454 CORE_ADDR address
21455 = read_address (abfd, line_ptr, cu, &bytes_read);
21456 line_ptr += bytes_read;
21457
21458 state_machine.check_line_address (cu, line_ptr,
21459 lowpc - baseaddr, address);
21460 state_machine.handle_set_address (baseaddr, address);
21461 }
21462 break;
21463 case DW_LNE_define_file:
21464 {
21465 const char *cur_file;
21466 unsigned int mod_time, length;
21467 dir_index dindex;
21468
21469 cur_file = read_direct_string (abfd, line_ptr,
21470 &bytes_read);
21471 line_ptr += bytes_read;
21472 dindex = (dir_index)
21473 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21474 line_ptr += bytes_read;
21475 mod_time =
21476 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21477 line_ptr += bytes_read;
21478 length =
21479 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21480 line_ptr += bytes_read;
21481 lh->add_file_name (cur_file, dindex, mod_time, length);
21482 }
21483 break;
21484 case DW_LNE_set_discriminator:
21485 {
21486 /* The discriminator is not interesting to the
21487 debugger; just ignore it. We still need to
21488 check its value though:
21489 if there are consecutive entries for the same
21490 (non-prologue) line we want to coalesce them.
21491 PR 17276. */
21492 unsigned int discr
21493 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21494 line_ptr += bytes_read;
21495
21496 state_machine.handle_set_discriminator (discr);
21497 }
21498 break;
21499 default:
21500 complaint (_("mangled .debug_line section"));
21501 return;
21502 }
21503 /* Make sure that we parsed the extended op correctly. If e.g.
21504 we expected a different address size than the producer used,
21505 we may have read the wrong number of bytes. */
21506 if (line_ptr != extended_end)
21507 {
21508 complaint (_("mangled .debug_line section"));
21509 return;
21510 }
21511 break;
21512 case DW_LNS_copy:
21513 state_machine.handle_copy ();
21514 break;
21515 case DW_LNS_advance_pc:
21516 {
21517 CORE_ADDR adjust
21518 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21519 line_ptr += bytes_read;
21520
21521 state_machine.handle_advance_pc (adjust);
21522 }
21523 break;
21524 case DW_LNS_advance_line:
21525 {
21526 int line_delta
21527 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
21528 line_ptr += bytes_read;
21529
21530 state_machine.handle_advance_line (line_delta);
21531 }
21532 break;
21533 case DW_LNS_set_file:
21534 {
21535 file_name_index file
21536 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21537 &bytes_read);
21538 line_ptr += bytes_read;
21539
21540 state_machine.handle_set_file (file);
21541 }
21542 break;
21543 case DW_LNS_set_column:
21544 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21545 line_ptr += bytes_read;
21546 break;
21547 case DW_LNS_negate_stmt:
21548 state_machine.handle_negate_stmt ();
21549 break;
21550 case DW_LNS_set_basic_block:
21551 break;
21552 /* Add to the address register of the state machine the
21553 address increment value corresponding to special opcode
21554 255. I.e., this value is scaled by the minimum
21555 instruction length since special opcode 255 would have
21556 scaled the increment. */
21557 case DW_LNS_const_add_pc:
21558 state_machine.handle_const_add_pc ();
21559 break;
21560 case DW_LNS_fixed_advance_pc:
21561 {
21562 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
21563 line_ptr += 2;
21564
21565 state_machine.handle_fixed_advance_pc (addr_adj);
21566 }
21567 break;
21568 default:
21569 {
21570 /* Unknown standard opcode, ignore it. */
21571 int i;
21572
21573 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
21574 {
21575 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21576 line_ptr += bytes_read;
21577 }
21578 }
21579 }
21580 }
21581
21582 if (!end_sequence)
21583 dwarf2_debug_line_missing_end_sequence_complaint ();
21584
21585 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21586 in which case we still finish recording the last line). */
21587 state_machine.record_line (true);
21588 }
21589 }
21590
21591 /* Decode the Line Number Program (LNP) for the given line_header
21592 structure and CU. The actual information extracted and the type
21593 of structures created from the LNP depends on the value of PST.
21594
21595 1. If PST is NULL, then this procedure uses the data from the program
21596 to create all necessary symbol tables, and their linetables.
21597
21598 2. If PST is not NULL, this procedure reads the program to determine
21599 the list of files included by the unit represented by PST, and
21600 builds all the associated partial symbol tables.
21601
21602 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21603 It is used for relative paths in the line table.
21604 NOTE: When processing partial symtabs (pst != NULL),
21605 comp_dir == pst->dirname.
21606
21607 NOTE: It is important that psymtabs have the same file name (via strcmp)
21608 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21609 symtab we don't use it in the name of the psymtabs we create.
21610 E.g. expand_line_sal requires this when finding psymtabs to expand.
21611 A good testcase for this is mb-inline.exp.
21612
21613 LOWPC is the lowest address in CU (or 0 if not known).
21614
21615 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21616 for its PC<->lines mapping information. Otherwise only the filename
21617 table is read in. */
21618
21619 static void
21620 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
21621 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
21622 CORE_ADDR lowpc, int decode_mapping)
21623 {
21624 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21625 const int decode_for_pst_p = (pst != NULL);
21626
21627 if (decode_mapping)
21628 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
21629
21630 if (decode_for_pst_p)
21631 {
21632 /* Now that we're done scanning the Line Header Program, we can
21633 create the psymtab of each included file. */
21634 for (auto &file_entry : lh->file_names ())
21635 if (file_entry.included_p == 1)
21636 {
21637 gdb::unique_xmalloc_ptr<char> name_holder;
21638 const char *include_name =
21639 psymtab_include_file_name (lh, file_entry, pst,
21640 comp_dir, &name_holder);
21641 if (include_name != NULL)
21642 dwarf2_create_include_psymtab (include_name, pst, objfile);
21643 }
21644 }
21645 else
21646 {
21647 /* Make sure a symtab is created for every file, even files
21648 which contain only variables (i.e. no code with associated
21649 line numbers). */
21650 buildsym_compunit *builder = cu->get_builder ();
21651 struct compunit_symtab *cust = builder->get_compunit_symtab ();
21652
21653 for (auto &fe : lh->file_names ())
21654 {
21655 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
21656 if (builder->get_current_subfile ()->symtab == NULL)
21657 {
21658 builder->get_current_subfile ()->symtab
21659 = allocate_symtab (cust,
21660 builder->get_current_subfile ()->name);
21661 }
21662 fe.symtab = builder->get_current_subfile ()->symtab;
21663 }
21664 }
21665 }
21666
21667 /* Start a subfile for DWARF. FILENAME is the name of the file and
21668 DIRNAME the name of the source directory which contains FILENAME
21669 or NULL if not known.
21670 This routine tries to keep line numbers from identical absolute and
21671 relative file names in a common subfile.
21672
21673 Using the `list' example from the GDB testsuite, which resides in
21674 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21675 of /srcdir/list0.c yields the following debugging information for list0.c:
21676
21677 DW_AT_name: /srcdir/list0.c
21678 DW_AT_comp_dir: /compdir
21679 files.files[0].name: list0.h
21680 files.files[0].dir: /srcdir
21681 files.files[1].name: list0.c
21682 files.files[1].dir: /srcdir
21683
21684 The line number information for list0.c has to end up in a single
21685 subfile, so that `break /srcdir/list0.c:1' works as expected.
21686 start_subfile will ensure that this happens provided that we pass the
21687 concatenation of files.files[1].dir and files.files[1].name as the
21688 subfile's name. */
21689
21690 static void
21691 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
21692 const char *dirname)
21693 {
21694 gdb::unique_xmalloc_ptr<char> copy;
21695
21696 /* In order not to lose the line information directory,
21697 we concatenate it to the filename when it makes sense.
21698 Note that the Dwarf3 standard says (speaking of filenames in line
21699 information): ``The directory index is ignored for file names
21700 that represent full path names''. Thus ignoring dirname in the
21701 `else' branch below isn't an issue. */
21702
21703 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
21704 {
21705 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
21706 filename = copy.get ();
21707 }
21708
21709 cu->get_builder ()->start_subfile (filename);
21710 }
21711
21712 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
21713 buildsym_compunit constructor. */
21714
21715 struct compunit_symtab *
21716 dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
21717 CORE_ADDR low_pc)
21718 {
21719 gdb_assert (m_builder == nullptr);
21720
21721 m_builder.reset (new struct buildsym_compunit
21722 (per_cu->dwarf2_per_objfile->objfile,
21723 name, comp_dir, language, low_pc));
21724
21725 list_in_scope = get_builder ()->get_file_symbols ();
21726
21727 get_builder ()->record_debugformat ("DWARF 2");
21728 get_builder ()->record_producer (producer);
21729
21730 processing_has_namespace_info = false;
21731
21732 return get_builder ()->get_compunit_symtab ();
21733 }
21734
21735 static void
21736 var_decode_location (struct attribute *attr, struct symbol *sym,
21737 struct dwarf2_cu *cu)
21738 {
21739 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21740 struct comp_unit_head *cu_header = &cu->header;
21741
21742 /* NOTE drow/2003-01-30: There used to be a comment and some special
21743 code here to turn a symbol with DW_AT_external and a
21744 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21745 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21746 with some versions of binutils) where shared libraries could have
21747 relocations against symbols in their debug information - the
21748 minimal symbol would have the right address, but the debug info
21749 would not. It's no longer necessary, because we will explicitly
21750 apply relocations when we read in the debug information now. */
21751
21752 /* A DW_AT_location attribute with no contents indicates that a
21753 variable has been optimized away. */
21754 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
21755 {
21756 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21757 return;
21758 }
21759
21760 /* Handle one degenerate form of location expression specially, to
21761 preserve GDB's previous behavior when section offsets are
21762 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
21763 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
21764
21765 if (attr_form_is_block (attr)
21766 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
21767 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
21768 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
21769 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
21770 && (DW_BLOCK (attr)->size
21771 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
21772 {
21773 unsigned int dummy;
21774
21775 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
21776 SET_SYMBOL_VALUE_ADDRESS (sym,
21777 read_address (objfile->obfd,
21778 DW_BLOCK (attr)->data + 1,
21779 cu, &dummy));
21780 else
21781 SET_SYMBOL_VALUE_ADDRESS
21782 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
21783 &dummy));
21784 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
21785 fixup_symbol_section (sym, objfile);
21786 SET_SYMBOL_VALUE_ADDRESS
21787 (sym,
21788 SYMBOL_VALUE_ADDRESS (sym)
21789 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
21790 return;
21791 }
21792
21793 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21794 expression evaluator, and use LOC_COMPUTED only when necessary
21795 (i.e. when the value of a register or memory location is
21796 referenced, or a thread-local block, etc.). Then again, it might
21797 not be worthwhile. I'm assuming that it isn't unless performance
21798 or memory numbers show me otherwise. */
21799
21800 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
21801
21802 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
21803 cu->has_loclist = true;
21804 }
21805
21806 /* Given a pointer to a DWARF information entry, figure out if we need
21807 to make a symbol table entry for it, and if so, create a new entry
21808 and return a pointer to it.
21809 If TYPE is NULL, determine symbol type from the die, otherwise
21810 used the passed type.
21811 If SPACE is not NULL, use it to hold the new symbol. If it is
21812 NULL, allocate a new symbol on the objfile's obstack. */
21813
21814 static struct symbol *
21815 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21816 struct symbol *space)
21817 {
21818 struct dwarf2_per_objfile *dwarf2_per_objfile
21819 = cu->per_cu->dwarf2_per_objfile;
21820 struct objfile *objfile = dwarf2_per_objfile->objfile;
21821 struct gdbarch *gdbarch = get_objfile_arch (objfile);
21822 struct symbol *sym = NULL;
21823 const char *name;
21824 struct attribute *attr = NULL;
21825 struct attribute *attr2 = NULL;
21826 CORE_ADDR baseaddr;
21827 struct pending **list_to_add = NULL;
21828
21829 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
21830
21831 baseaddr = objfile->text_section_offset ();
21832
21833 name = dwarf2_name (die, cu);
21834 if (name)
21835 {
21836 const char *linkagename;
21837 int suppress_add = 0;
21838
21839 if (space)
21840 sym = space;
21841 else
21842 sym = allocate_symbol (objfile);
21843 OBJSTAT (objfile, n_syms++);
21844
21845 /* Cache this symbol's name and the name's demangled form (if any). */
21846 sym->set_language (cu->language, &objfile->objfile_obstack);
21847 linkagename = dwarf2_physname (name, die, cu);
21848 sym->compute_and_set_names (linkagename, false, objfile->per_bfd);
21849
21850 /* Fortran does not have mangling standard and the mangling does differ
21851 between gfortran, iFort etc. */
21852 if (cu->language == language_fortran
21853 && symbol_get_demangled_name (sym) == NULL)
21854 symbol_set_demangled_name (sym,
21855 dwarf2_full_name (name, die, cu),
21856 NULL);
21857
21858 /* Default assumptions.
21859 Use the passed type or decode it from the die. */
21860 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21861 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21862 if (type != NULL)
21863 SYMBOL_TYPE (sym) = type;
21864 else
21865 SYMBOL_TYPE (sym) = die_type (die, cu);
21866 attr = dwarf2_attr (die,
21867 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21868 cu);
21869 if (attr != nullptr)
21870 {
21871 SYMBOL_LINE (sym) = DW_UNSND (attr);
21872 }
21873
21874 attr = dwarf2_attr (die,
21875 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21876 cu);
21877 if (attr != nullptr)
21878 {
21879 file_name_index file_index = (file_name_index) DW_UNSND (attr);
21880 struct file_entry *fe;
21881
21882 if (cu->line_header != NULL)
21883 fe = cu->line_header->file_name_at (file_index);
21884 else
21885 fe = NULL;
21886
21887 if (fe == NULL)
21888 complaint (_("file index out of range"));
21889 else
21890 symbol_set_symtab (sym, fe->symtab);
21891 }
21892
21893 switch (die->tag)
21894 {
21895 case DW_TAG_label:
21896 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
21897 if (attr != nullptr)
21898 {
21899 CORE_ADDR addr;
21900
21901 addr = attr_value_as_address (attr);
21902 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
21903 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
21904 }
21905 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21906 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
21907 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
21908 add_symbol_to_list (sym, cu->list_in_scope);
21909 break;
21910 case DW_TAG_subprogram:
21911 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21912 finish_block. */
21913 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21914 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21915 if ((attr2 && (DW_UNSND (attr2) != 0))
21916 || cu->language == language_ada
21917 || cu->language == language_fortran)
21918 {
21919 /* Subprograms marked external are stored as a global symbol.
21920 Ada and Fortran subprograms, whether marked external or
21921 not, are always stored as a global symbol, because we want
21922 to be able to access them globally. For instance, we want
21923 to be able to break on a nested subprogram without having
21924 to specify the context. */
21925 list_to_add = cu->get_builder ()->get_global_symbols ();
21926 }
21927 else
21928 {
21929 list_to_add = cu->list_in_scope;
21930 }
21931 break;
21932 case DW_TAG_inlined_subroutine:
21933 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21934 finish_block. */
21935 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21936 SYMBOL_INLINED (sym) = 1;
21937 list_to_add = cu->list_in_scope;
21938 break;
21939 case DW_TAG_template_value_param:
21940 suppress_add = 1;
21941 /* Fall through. */
21942 case DW_TAG_constant:
21943 case DW_TAG_variable:
21944 case DW_TAG_member:
21945 /* Compilation with minimal debug info may result in
21946 variables with missing type entries. Change the
21947 misleading `void' type to something sensible. */
21948 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
21949 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
21950
21951 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21952 /* In the case of DW_TAG_member, we should only be called for
21953 static const members. */
21954 if (die->tag == DW_TAG_member)
21955 {
21956 /* dwarf2_add_field uses die_is_declaration,
21957 so we do the same. */
21958 gdb_assert (die_is_declaration (die, cu));
21959 gdb_assert (attr);
21960 }
21961 if (attr != nullptr)
21962 {
21963 dwarf2_const_value (attr, sym, cu);
21964 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21965 if (!suppress_add)
21966 {
21967 if (attr2 && (DW_UNSND (attr2) != 0))
21968 list_to_add = cu->get_builder ()->get_global_symbols ();
21969 else
21970 list_to_add = cu->list_in_scope;
21971 }
21972 break;
21973 }
21974 attr = dwarf2_attr (die, DW_AT_location, cu);
21975 if (attr != nullptr)
21976 {
21977 var_decode_location (attr, sym, cu);
21978 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21979
21980 /* Fortran explicitly imports any global symbols to the local
21981 scope by DW_TAG_common_block. */
21982 if (cu->language == language_fortran && die->parent
21983 && die->parent->tag == DW_TAG_common_block)
21984 attr2 = NULL;
21985
21986 if (SYMBOL_CLASS (sym) == LOC_STATIC
21987 && SYMBOL_VALUE_ADDRESS (sym) == 0
21988 && !dwarf2_per_objfile->has_section_at_zero)
21989 {
21990 /* When a static variable is eliminated by the linker,
21991 the corresponding debug information is not stripped
21992 out, but the variable address is set to null;
21993 do not add such variables into symbol table. */
21994 }
21995 else if (attr2 && (DW_UNSND (attr2) != 0))
21996 {
21997 if (SYMBOL_CLASS (sym) == LOC_STATIC
21998 && (objfile->flags & OBJF_MAINLINE) == 0
21999 && dwarf2_per_objfile->can_copy)
22000 {
22001 /* A global static variable might be subject to
22002 copy relocation. We first check for a local
22003 minsym, though, because maybe the symbol was
22004 marked hidden, in which case this would not
22005 apply. */
22006 bound_minimal_symbol found
22007 = (lookup_minimal_symbol_linkage
22008 (sym->linkage_name (), objfile));
22009 if (found.minsym != nullptr)
22010 sym->maybe_copied = 1;
22011 }
22012
22013 /* A variable with DW_AT_external is never static,
22014 but it may be block-scoped. */
22015 list_to_add
22016 = ((cu->list_in_scope
22017 == cu->get_builder ()->get_file_symbols ())
22018 ? cu->get_builder ()->get_global_symbols ()
22019 : cu->list_in_scope);
22020 }
22021 else
22022 list_to_add = cu->list_in_scope;
22023 }
22024 else
22025 {
22026 /* We do not know the address of this symbol.
22027 If it is an external symbol and we have type information
22028 for it, enter the symbol as a LOC_UNRESOLVED symbol.
22029 The address of the variable will then be determined from
22030 the minimal symbol table whenever the variable is
22031 referenced. */
22032 attr2 = dwarf2_attr (die, DW_AT_external, cu);
22033
22034 /* Fortran explicitly imports any global symbols to the local
22035 scope by DW_TAG_common_block. */
22036 if (cu->language == language_fortran && die->parent
22037 && die->parent->tag == DW_TAG_common_block)
22038 {
22039 /* SYMBOL_CLASS doesn't matter here because
22040 read_common_block is going to reset it. */
22041 if (!suppress_add)
22042 list_to_add = cu->list_in_scope;
22043 }
22044 else if (attr2 && (DW_UNSND (attr2) != 0)
22045 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
22046 {
22047 /* A variable with DW_AT_external is never static, but it
22048 may be block-scoped. */
22049 list_to_add
22050 = ((cu->list_in_scope
22051 == cu->get_builder ()->get_file_symbols ())
22052 ? cu->get_builder ()->get_global_symbols ()
22053 : cu->list_in_scope);
22054
22055 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
22056 }
22057 else if (!die_is_declaration (die, cu))
22058 {
22059 /* Use the default LOC_OPTIMIZED_OUT class. */
22060 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
22061 if (!suppress_add)
22062 list_to_add = cu->list_in_scope;
22063 }
22064 }
22065 break;
22066 case DW_TAG_formal_parameter:
22067 {
22068 /* If we are inside a function, mark this as an argument. If
22069 not, we might be looking at an argument to an inlined function
22070 when we do not have enough information to show inlined frames;
22071 pretend it's a local variable in that case so that the user can
22072 still see it. */
22073 struct context_stack *curr
22074 = cu->get_builder ()->get_current_context_stack ();
22075 if (curr != nullptr && curr->name != nullptr)
22076 SYMBOL_IS_ARGUMENT (sym) = 1;
22077 attr = dwarf2_attr (die, DW_AT_location, cu);
22078 if (attr != nullptr)
22079 {
22080 var_decode_location (attr, sym, cu);
22081 }
22082 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22083 if (attr != nullptr)
22084 {
22085 dwarf2_const_value (attr, sym, cu);
22086 }
22087
22088 list_to_add = cu->list_in_scope;
22089 }
22090 break;
22091 case DW_TAG_unspecified_parameters:
22092 /* From varargs functions; gdb doesn't seem to have any
22093 interest in this information, so just ignore it for now.
22094 (FIXME?) */
22095 break;
22096 case DW_TAG_template_type_param:
22097 suppress_add = 1;
22098 /* Fall through. */
22099 case DW_TAG_class_type:
22100 case DW_TAG_interface_type:
22101 case DW_TAG_structure_type:
22102 case DW_TAG_union_type:
22103 case DW_TAG_set_type:
22104 case DW_TAG_enumeration_type:
22105 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22106 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
22107
22108 {
22109 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
22110 really ever be static objects: otherwise, if you try
22111 to, say, break of a class's method and you're in a file
22112 which doesn't mention that class, it won't work unless
22113 the check for all static symbols in lookup_symbol_aux
22114 saves you. See the OtherFileClass tests in
22115 gdb.c++/namespace.exp. */
22116
22117 if (!suppress_add)
22118 {
22119 buildsym_compunit *builder = cu->get_builder ();
22120 list_to_add
22121 = (cu->list_in_scope == builder->get_file_symbols ()
22122 && cu->language == language_cplus
22123 ? builder->get_global_symbols ()
22124 : cu->list_in_scope);
22125
22126 /* The semantics of C++ state that "struct foo {
22127 ... }" also defines a typedef for "foo". */
22128 if (cu->language == language_cplus
22129 || cu->language == language_ada
22130 || cu->language == language_d
22131 || cu->language == language_rust)
22132 {
22133 /* The symbol's name is already allocated along
22134 with this objfile, so we don't need to
22135 duplicate it for the type. */
22136 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
22137 TYPE_NAME (SYMBOL_TYPE (sym)) = sym->search_name ();
22138 }
22139 }
22140 }
22141 break;
22142 case DW_TAG_typedef:
22143 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22144 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
22145 list_to_add = cu->list_in_scope;
22146 break;
22147 case DW_TAG_base_type:
22148 case DW_TAG_subrange_type:
22149 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22150 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
22151 list_to_add = cu->list_in_scope;
22152 break;
22153 case DW_TAG_enumerator:
22154 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22155 if (attr != nullptr)
22156 {
22157 dwarf2_const_value (attr, sym, cu);
22158 }
22159 {
22160 /* NOTE: carlton/2003-11-10: See comment above in the
22161 DW_TAG_class_type, etc. block. */
22162
22163 list_to_add
22164 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
22165 && cu->language == language_cplus
22166 ? cu->get_builder ()->get_global_symbols ()
22167 : cu->list_in_scope);
22168 }
22169 break;
22170 case DW_TAG_imported_declaration:
22171 case DW_TAG_namespace:
22172 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22173 list_to_add = cu->get_builder ()->get_global_symbols ();
22174 break;
22175 case DW_TAG_module:
22176 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22177 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
22178 list_to_add = cu->get_builder ()->get_global_symbols ();
22179 break;
22180 case DW_TAG_common_block:
22181 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
22182 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
22183 add_symbol_to_list (sym, cu->list_in_scope);
22184 break;
22185 default:
22186 /* Not a tag we recognize. Hopefully we aren't processing
22187 trash data, but since we must specifically ignore things
22188 we don't recognize, there is nothing else we should do at
22189 this point. */
22190 complaint (_("unsupported tag: '%s'"),
22191 dwarf_tag_name (die->tag));
22192 break;
22193 }
22194
22195 if (suppress_add)
22196 {
22197 sym->hash_next = objfile->template_symbols;
22198 objfile->template_symbols = sym;
22199 list_to_add = NULL;
22200 }
22201
22202 if (list_to_add != NULL)
22203 add_symbol_to_list (sym, list_to_add);
22204
22205 /* For the benefit of old versions of GCC, check for anonymous
22206 namespaces based on the demangled name. */
22207 if (!cu->processing_has_namespace_info
22208 && cu->language == language_cplus)
22209 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
22210 }
22211 return (sym);
22212 }
22213
22214 /* Given an attr with a DW_FORM_dataN value in host byte order,
22215 zero-extend it as appropriate for the symbol's type. The DWARF
22216 standard (v4) is not entirely clear about the meaning of using
22217 DW_FORM_dataN for a constant with a signed type, where the type is
22218 wider than the data. The conclusion of a discussion on the DWARF
22219 list was that this is unspecified. We choose to always zero-extend
22220 because that is the interpretation long in use by GCC. */
22221
22222 static gdb_byte *
22223 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
22224 struct dwarf2_cu *cu, LONGEST *value, int bits)
22225 {
22226 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22227 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
22228 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
22229 LONGEST l = DW_UNSND (attr);
22230
22231 if (bits < sizeof (*value) * 8)
22232 {
22233 l &= ((LONGEST) 1 << bits) - 1;
22234 *value = l;
22235 }
22236 else if (bits == sizeof (*value) * 8)
22237 *value = l;
22238 else
22239 {
22240 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
22241 store_unsigned_integer (bytes, bits / 8, byte_order, l);
22242 return bytes;
22243 }
22244
22245 return NULL;
22246 }
22247
22248 /* Read a constant value from an attribute. Either set *VALUE, or if
22249 the value does not fit in *VALUE, set *BYTES - either already
22250 allocated on the objfile obstack, or newly allocated on OBSTACK,
22251 or, set *BATON, if we translated the constant to a location
22252 expression. */
22253
22254 static void
22255 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
22256 const char *name, struct obstack *obstack,
22257 struct dwarf2_cu *cu,
22258 LONGEST *value, const gdb_byte **bytes,
22259 struct dwarf2_locexpr_baton **baton)
22260 {
22261 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22262 struct comp_unit_head *cu_header = &cu->header;
22263 struct dwarf_block *blk;
22264 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
22265 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22266
22267 *value = 0;
22268 *bytes = NULL;
22269 *baton = NULL;
22270
22271 switch (attr->form)
22272 {
22273 case DW_FORM_addr:
22274 case DW_FORM_addrx:
22275 case DW_FORM_GNU_addr_index:
22276 {
22277 gdb_byte *data;
22278
22279 if (TYPE_LENGTH (type) != cu_header->addr_size)
22280 dwarf2_const_value_length_mismatch_complaint (name,
22281 cu_header->addr_size,
22282 TYPE_LENGTH (type));
22283 /* Symbols of this form are reasonably rare, so we just
22284 piggyback on the existing location code rather than writing
22285 a new implementation of symbol_computed_ops. */
22286 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
22287 (*baton)->per_cu = cu->per_cu;
22288 gdb_assert ((*baton)->per_cu);
22289
22290 (*baton)->size = 2 + cu_header->addr_size;
22291 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
22292 (*baton)->data = data;
22293
22294 data[0] = DW_OP_addr;
22295 store_unsigned_integer (&data[1], cu_header->addr_size,
22296 byte_order, DW_ADDR (attr));
22297 data[cu_header->addr_size + 1] = DW_OP_stack_value;
22298 }
22299 break;
22300 case DW_FORM_string:
22301 case DW_FORM_strp:
22302 case DW_FORM_strx:
22303 case DW_FORM_GNU_str_index:
22304 case DW_FORM_GNU_strp_alt:
22305 /* DW_STRING is already allocated on the objfile obstack, point
22306 directly to it. */
22307 *bytes = (const gdb_byte *) DW_STRING (attr);
22308 break;
22309 case DW_FORM_block1:
22310 case DW_FORM_block2:
22311 case DW_FORM_block4:
22312 case DW_FORM_block:
22313 case DW_FORM_exprloc:
22314 case DW_FORM_data16:
22315 blk = DW_BLOCK (attr);
22316 if (TYPE_LENGTH (type) != blk->size)
22317 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
22318 TYPE_LENGTH (type));
22319 *bytes = blk->data;
22320 break;
22321
22322 /* The DW_AT_const_value attributes are supposed to carry the
22323 symbol's value "represented as it would be on the target
22324 architecture." By the time we get here, it's already been
22325 converted to host endianness, so we just need to sign- or
22326 zero-extend it as appropriate. */
22327 case DW_FORM_data1:
22328 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
22329 break;
22330 case DW_FORM_data2:
22331 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
22332 break;
22333 case DW_FORM_data4:
22334 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
22335 break;
22336 case DW_FORM_data8:
22337 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
22338 break;
22339
22340 case DW_FORM_sdata:
22341 case DW_FORM_implicit_const:
22342 *value = DW_SND (attr);
22343 break;
22344
22345 case DW_FORM_udata:
22346 *value = DW_UNSND (attr);
22347 break;
22348
22349 default:
22350 complaint (_("unsupported const value attribute form: '%s'"),
22351 dwarf_form_name (attr->form));
22352 *value = 0;
22353 break;
22354 }
22355 }
22356
22357
22358 /* Copy constant value from an attribute to a symbol. */
22359
22360 static void
22361 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
22362 struct dwarf2_cu *cu)
22363 {
22364 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22365 LONGEST value;
22366 const gdb_byte *bytes;
22367 struct dwarf2_locexpr_baton *baton;
22368
22369 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
22370 sym->print_name (),
22371 &objfile->objfile_obstack, cu,
22372 &value, &bytes, &baton);
22373
22374 if (baton != NULL)
22375 {
22376 SYMBOL_LOCATION_BATON (sym) = baton;
22377 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
22378 }
22379 else if (bytes != NULL)
22380 {
22381 SYMBOL_VALUE_BYTES (sym) = bytes;
22382 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
22383 }
22384 else
22385 {
22386 SYMBOL_VALUE (sym) = value;
22387 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
22388 }
22389 }
22390
22391 /* Return the type of the die in question using its DW_AT_type attribute. */
22392
22393 static struct type *
22394 die_type (struct die_info *die, struct dwarf2_cu *cu)
22395 {
22396 struct attribute *type_attr;
22397
22398 type_attr = dwarf2_attr (die, DW_AT_type, cu);
22399 if (!type_attr)
22400 {
22401 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22402 /* A missing DW_AT_type represents a void type. */
22403 return objfile_type (objfile)->builtin_void;
22404 }
22405
22406 return lookup_die_type (die, type_attr, cu);
22407 }
22408
22409 /* True iff CU's producer generates GNAT Ada auxiliary information
22410 that allows to find parallel types through that information instead
22411 of having to do expensive parallel lookups by type name. */
22412
22413 static int
22414 need_gnat_info (struct dwarf2_cu *cu)
22415 {
22416 /* Assume that the Ada compiler was GNAT, which always produces
22417 the auxiliary information. */
22418 return (cu->language == language_ada);
22419 }
22420
22421 /* Return the auxiliary type of the die in question using its
22422 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
22423 attribute is not present. */
22424
22425 static struct type *
22426 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
22427 {
22428 struct attribute *type_attr;
22429
22430 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
22431 if (!type_attr)
22432 return NULL;
22433
22434 return lookup_die_type (die, type_attr, cu);
22435 }
22436
22437 /* If DIE has a descriptive_type attribute, then set the TYPE's
22438 descriptive type accordingly. */
22439
22440 static void
22441 set_descriptive_type (struct type *type, struct die_info *die,
22442 struct dwarf2_cu *cu)
22443 {
22444 struct type *descriptive_type = die_descriptive_type (die, cu);
22445
22446 if (descriptive_type)
22447 {
22448 ALLOCATE_GNAT_AUX_TYPE (type);
22449 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
22450 }
22451 }
22452
22453 /* Return the containing type of the die in question using its
22454 DW_AT_containing_type attribute. */
22455
22456 static struct type *
22457 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
22458 {
22459 struct attribute *type_attr;
22460 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22461
22462 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
22463 if (!type_attr)
22464 error (_("Dwarf Error: Problem turning containing type into gdb type "
22465 "[in module %s]"), objfile_name (objfile));
22466
22467 return lookup_die_type (die, type_attr, cu);
22468 }
22469
22470 /* Return an error marker type to use for the ill formed type in DIE/CU. */
22471
22472 static struct type *
22473 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22474 {
22475 struct dwarf2_per_objfile *dwarf2_per_objfile
22476 = cu->per_cu->dwarf2_per_objfile;
22477 struct objfile *objfile = dwarf2_per_objfile->objfile;
22478 char *saved;
22479
22480 std::string message
22481 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
22482 objfile_name (objfile),
22483 sect_offset_str (cu->header.sect_off),
22484 sect_offset_str (die->sect_off));
22485 saved = obstack_strdup (&objfile->objfile_obstack, message);
22486
22487 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
22488 }
22489
22490 /* Look up the type of DIE in CU using its type attribute ATTR.
22491 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22492 DW_AT_containing_type.
22493 If there is no type substitute an error marker. */
22494
22495 static struct type *
22496 lookup_die_type (struct die_info *die, const struct attribute *attr,
22497 struct dwarf2_cu *cu)
22498 {
22499 struct dwarf2_per_objfile *dwarf2_per_objfile
22500 = cu->per_cu->dwarf2_per_objfile;
22501 struct objfile *objfile = dwarf2_per_objfile->objfile;
22502 struct type *this_type;
22503
22504 gdb_assert (attr->name == DW_AT_type
22505 || attr->name == DW_AT_GNAT_descriptive_type
22506 || attr->name == DW_AT_containing_type);
22507
22508 /* First see if we have it cached. */
22509
22510 if (attr->form == DW_FORM_GNU_ref_alt)
22511 {
22512 struct dwarf2_per_cu_data *per_cu;
22513 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
22514
22515 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
22516 dwarf2_per_objfile);
22517 this_type = get_die_type_at_offset (sect_off, per_cu);
22518 }
22519 else if (attr_form_is_ref (attr))
22520 {
22521 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
22522
22523 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
22524 }
22525 else if (attr->form == DW_FORM_ref_sig8)
22526 {
22527 ULONGEST signature = DW_SIGNATURE (attr);
22528
22529 return get_signatured_type (die, signature, cu);
22530 }
22531 else
22532 {
22533 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
22534 " at %s [in module %s]"),
22535 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
22536 objfile_name (objfile));
22537 return build_error_marker_type (cu, die);
22538 }
22539
22540 /* If not cached we need to read it in. */
22541
22542 if (this_type == NULL)
22543 {
22544 struct die_info *type_die = NULL;
22545 struct dwarf2_cu *type_cu = cu;
22546
22547 if (attr_form_is_ref (attr))
22548 type_die = follow_die_ref (die, attr, &type_cu);
22549 if (type_die == NULL)
22550 return build_error_marker_type (cu, die);
22551 /* If we find the type now, it's probably because the type came
22552 from an inter-CU reference and the type's CU got expanded before
22553 ours. */
22554 this_type = read_type_die (type_die, type_cu);
22555 }
22556
22557 /* If we still don't have a type use an error marker. */
22558
22559 if (this_type == NULL)
22560 return build_error_marker_type (cu, die);
22561
22562 return this_type;
22563 }
22564
22565 /* Return the type in DIE, CU.
22566 Returns NULL for invalid types.
22567
22568 This first does a lookup in die_type_hash,
22569 and only reads the die in if necessary.
22570
22571 NOTE: This can be called when reading in partial or full symbols. */
22572
22573 static struct type *
22574 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
22575 {
22576 struct type *this_type;
22577
22578 this_type = get_die_type (die, cu);
22579 if (this_type)
22580 return this_type;
22581
22582 return read_type_die_1 (die, cu);
22583 }
22584
22585 /* Read the type in DIE, CU.
22586 Returns NULL for invalid types. */
22587
22588 static struct type *
22589 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22590 {
22591 struct type *this_type = NULL;
22592
22593 switch (die->tag)
22594 {
22595 case DW_TAG_class_type:
22596 case DW_TAG_interface_type:
22597 case DW_TAG_structure_type:
22598 case DW_TAG_union_type:
22599 this_type = read_structure_type (die, cu);
22600 break;
22601 case DW_TAG_enumeration_type:
22602 this_type = read_enumeration_type (die, cu);
22603 break;
22604 case DW_TAG_subprogram:
22605 case DW_TAG_subroutine_type:
22606 case DW_TAG_inlined_subroutine:
22607 this_type = read_subroutine_type (die, cu);
22608 break;
22609 case DW_TAG_array_type:
22610 this_type = read_array_type (die, cu);
22611 break;
22612 case DW_TAG_set_type:
22613 this_type = read_set_type (die, cu);
22614 break;
22615 case DW_TAG_pointer_type:
22616 this_type = read_tag_pointer_type (die, cu);
22617 break;
22618 case DW_TAG_ptr_to_member_type:
22619 this_type = read_tag_ptr_to_member_type (die, cu);
22620 break;
22621 case DW_TAG_reference_type:
22622 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22623 break;
22624 case DW_TAG_rvalue_reference_type:
22625 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
22626 break;
22627 case DW_TAG_const_type:
22628 this_type = read_tag_const_type (die, cu);
22629 break;
22630 case DW_TAG_volatile_type:
22631 this_type = read_tag_volatile_type (die, cu);
22632 break;
22633 case DW_TAG_restrict_type:
22634 this_type = read_tag_restrict_type (die, cu);
22635 break;
22636 case DW_TAG_string_type:
22637 this_type = read_tag_string_type (die, cu);
22638 break;
22639 case DW_TAG_typedef:
22640 this_type = read_typedef (die, cu);
22641 break;
22642 case DW_TAG_subrange_type:
22643 this_type = read_subrange_type (die, cu);
22644 break;
22645 case DW_TAG_base_type:
22646 this_type = read_base_type (die, cu);
22647 break;
22648 case DW_TAG_unspecified_type:
22649 this_type = read_unspecified_type (die, cu);
22650 break;
22651 case DW_TAG_namespace:
22652 this_type = read_namespace_type (die, cu);
22653 break;
22654 case DW_TAG_module:
22655 this_type = read_module_type (die, cu);
22656 break;
22657 case DW_TAG_atomic_type:
22658 this_type = read_tag_atomic_type (die, cu);
22659 break;
22660 default:
22661 complaint (_("unexpected tag in read_type_die: '%s'"),
22662 dwarf_tag_name (die->tag));
22663 break;
22664 }
22665
22666 return this_type;
22667 }
22668
22669 /* See if we can figure out if the class lives in a namespace. We do
22670 this by looking for a member function; its demangled name will
22671 contain namespace info, if there is any.
22672 Return the computed name or NULL.
22673 Space for the result is allocated on the objfile's obstack.
22674 This is the full-die version of guess_partial_die_structure_name.
22675 In this case we know DIE has no useful parent. */
22676
22677 static const char *
22678 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22679 {
22680 struct die_info *spec_die;
22681 struct dwarf2_cu *spec_cu;
22682 struct die_info *child;
22683 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22684
22685 spec_cu = cu;
22686 spec_die = die_specification (die, &spec_cu);
22687 if (spec_die != NULL)
22688 {
22689 die = spec_die;
22690 cu = spec_cu;
22691 }
22692
22693 for (child = die->child;
22694 child != NULL;
22695 child = child->sibling)
22696 {
22697 if (child->tag == DW_TAG_subprogram)
22698 {
22699 const char *linkage_name = dw2_linkage_name (child, cu);
22700
22701 if (linkage_name != NULL)
22702 {
22703 gdb::unique_xmalloc_ptr<char> actual_name
22704 (language_class_name_from_physname (cu->language_defn,
22705 linkage_name));
22706 const char *name = NULL;
22707
22708 if (actual_name != NULL)
22709 {
22710 const char *die_name = dwarf2_name (die, cu);
22711
22712 if (die_name != NULL
22713 && strcmp (die_name, actual_name.get ()) != 0)
22714 {
22715 /* Strip off the class name from the full name.
22716 We want the prefix. */
22717 int die_name_len = strlen (die_name);
22718 int actual_name_len = strlen (actual_name.get ());
22719 const char *ptr = actual_name.get ();
22720
22721 /* Test for '::' as a sanity check. */
22722 if (actual_name_len > die_name_len + 2
22723 && ptr[actual_name_len - die_name_len - 1] == ':')
22724 name = obstack_strndup (
22725 &objfile->per_bfd->storage_obstack,
22726 ptr, actual_name_len - die_name_len - 2);
22727 }
22728 }
22729 return name;
22730 }
22731 }
22732 }
22733
22734 return NULL;
22735 }
22736
22737 /* GCC might emit a nameless typedef that has a linkage name. Determine the
22738 prefix part in such case. See
22739 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22740
22741 static const char *
22742 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22743 {
22744 struct attribute *attr;
22745 const char *base;
22746
22747 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22748 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22749 return NULL;
22750
22751 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
22752 return NULL;
22753
22754 attr = dw2_linkage_name_attr (die, cu);
22755 if (attr == NULL || DW_STRING (attr) == NULL)
22756 return NULL;
22757
22758 /* dwarf2_name had to be already called. */
22759 gdb_assert (DW_STRING_IS_CANONICAL (attr));
22760
22761 /* Strip the base name, keep any leading namespaces/classes. */
22762 base = strrchr (DW_STRING (attr), ':');
22763 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
22764 return "";
22765
22766 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22767 return obstack_strndup (&objfile->per_bfd->storage_obstack,
22768 DW_STRING (attr),
22769 &base[-1] - DW_STRING (attr));
22770 }
22771
22772 /* Return the name of the namespace/class that DIE is defined within,
22773 or "" if we can't tell. The caller should not xfree the result.
22774
22775 For example, if we're within the method foo() in the following
22776 code:
22777
22778 namespace N {
22779 class C {
22780 void foo () {
22781 }
22782 };
22783 }
22784
22785 then determine_prefix on foo's die will return "N::C". */
22786
22787 static const char *
22788 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
22789 {
22790 struct dwarf2_per_objfile *dwarf2_per_objfile
22791 = cu->per_cu->dwarf2_per_objfile;
22792 struct die_info *parent, *spec_die;
22793 struct dwarf2_cu *spec_cu;
22794 struct type *parent_type;
22795 const char *retval;
22796
22797 if (cu->language != language_cplus
22798 && cu->language != language_fortran && cu->language != language_d
22799 && cu->language != language_rust)
22800 return "";
22801
22802 retval = anonymous_struct_prefix (die, cu);
22803 if (retval)
22804 return retval;
22805
22806 /* We have to be careful in the presence of DW_AT_specification.
22807 For example, with GCC 3.4, given the code
22808
22809 namespace N {
22810 void foo() {
22811 // Definition of N::foo.
22812 }
22813 }
22814
22815 then we'll have a tree of DIEs like this:
22816
22817 1: DW_TAG_compile_unit
22818 2: DW_TAG_namespace // N
22819 3: DW_TAG_subprogram // declaration of N::foo
22820 4: DW_TAG_subprogram // definition of N::foo
22821 DW_AT_specification // refers to die #3
22822
22823 Thus, when processing die #4, we have to pretend that we're in
22824 the context of its DW_AT_specification, namely the contex of die
22825 #3. */
22826 spec_cu = cu;
22827 spec_die = die_specification (die, &spec_cu);
22828 if (spec_die == NULL)
22829 parent = die->parent;
22830 else
22831 {
22832 parent = spec_die->parent;
22833 cu = spec_cu;
22834 }
22835
22836 if (parent == NULL)
22837 return "";
22838 else if (parent->building_fullname)
22839 {
22840 const char *name;
22841 const char *parent_name;
22842
22843 /* It has been seen on RealView 2.2 built binaries,
22844 DW_TAG_template_type_param types actually _defined_ as
22845 children of the parent class:
22846
22847 enum E {};
22848 template class <class Enum> Class{};
22849 Class<enum E> class_e;
22850
22851 1: DW_TAG_class_type (Class)
22852 2: DW_TAG_enumeration_type (E)
22853 3: DW_TAG_enumerator (enum1:0)
22854 3: DW_TAG_enumerator (enum2:1)
22855 ...
22856 2: DW_TAG_template_type_param
22857 DW_AT_type DW_FORM_ref_udata (E)
22858
22859 Besides being broken debug info, it can put GDB into an
22860 infinite loop. Consider:
22861
22862 When we're building the full name for Class<E>, we'll start
22863 at Class, and go look over its template type parameters,
22864 finding E. We'll then try to build the full name of E, and
22865 reach here. We're now trying to build the full name of E,
22866 and look over the parent DIE for containing scope. In the
22867 broken case, if we followed the parent DIE of E, we'd again
22868 find Class, and once again go look at its template type
22869 arguments, etc., etc. Simply don't consider such parent die
22870 as source-level parent of this die (it can't be, the language
22871 doesn't allow it), and break the loop here. */
22872 name = dwarf2_name (die, cu);
22873 parent_name = dwarf2_name (parent, cu);
22874 complaint (_("template param type '%s' defined within parent '%s'"),
22875 name ? name : "<unknown>",
22876 parent_name ? parent_name : "<unknown>");
22877 return "";
22878 }
22879 else
22880 switch (parent->tag)
22881 {
22882 case DW_TAG_namespace:
22883 parent_type = read_type_die (parent, cu);
22884 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22885 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22886 Work around this problem here. */
22887 if (cu->language == language_cplus
22888 && strcmp (TYPE_NAME (parent_type), "::") == 0)
22889 return "";
22890 /* We give a name to even anonymous namespaces. */
22891 return TYPE_NAME (parent_type);
22892 case DW_TAG_class_type:
22893 case DW_TAG_interface_type:
22894 case DW_TAG_structure_type:
22895 case DW_TAG_union_type:
22896 case DW_TAG_module:
22897 parent_type = read_type_die (parent, cu);
22898 if (TYPE_NAME (parent_type) != NULL)
22899 return TYPE_NAME (parent_type);
22900 else
22901 /* An anonymous structure is only allowed non-static data
22902 members; no typedefs, no member functions, et cetera.
22903 So it does not need a prefix. */
22904 return "";
22905 case DW_TAG_compile_unit:
22906 case DW_TAG_partial_unit:
22907 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22908 if (cu->language == language_cplus
22909 && !dwarf2_per_objfile->types.empty ()
22910 && die->child != NULL
22911 && (die->tag == DW_TAG_class_type
22912 || die->tag == DW_TAG_structure_type
22913 || die->tag == DW_TAG_union_type))
22914 {
22915 const char *name = guess_full_die_structure_name (die, cu);
22916 if (name != NULL)
22917 return name;
22918 }
22919 return "";
22920 case DW_TAG_subprogram:
22921 /* Nested subroutines in Fortran get a prefix with the name
22922 of the parent's subroutine. */
22923 if (cu->language == language_fortran)
22924 {
22925 if ((die->tag == DW_TAG_subprogram)
22926 && (dwarf2_name (parent, cu) != NULL))
22927 return dwarf2_name (parent, cu);
22928 }
22929 return determine_prefix (parent, cu);
22930 case DW_TAG_enumeration_type:
22931 parent_type = read_type_die (parent, cu);
22932 if (TYPE_DECLARED_CLASS (parent_type))
22933 {
22934 if (TYPE_NAME (parent_type) != NULL)
22935 return TYPE_NAME (parent_type);
22936 return "";
22937 }
22938 /* Fall through. */
22939 default:
22940 return determine_prefix (parent, cu);
22941 }
22942 }
22943
22944 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22945 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22946 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22947 an obconcat, otherwise allocate storage for the result. The CU argument is
22948 used to determine the language and hence, the appropriate separator. */
22949
22950 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
22951
22952 static char *
22953 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22954 int physname, struct dwarf2_cu *cu)
22955 {
22956 const char *lead = "";
22957 const char *sep;
22958
22959 if (suffix == NULL || suffix[0] == '\0'
22960 || prefix == NULL || prefix[0] == '\0')
22961 sep = "";
22962 else if (cu->language == language_d)
22963 {
22964 /* For D, the 'main' function could be defined in any module, but it
22965 should never be prefixed. */
22966 if (strcmp (suffix, "D main") == 0)
22967 {
22968 prefix = "";
22969 sep = "";
22970 }
22971 else
22972 sep = ".";
22973 }
22974 else if (cu->language == language_fortran && physname)
22975 {
22976 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22977 DW_AT_MIPS_linkage_name is preferred and used instead. */
22978
22979 lead = "__";
22980 sep = "_MOD_";
22981 }
22982 else
22983 sep = "::";
22984
22985 if (prefix == NULL)
22986 prefix = "";
22987 if (suffix == NULL)
22988 suffix = "";
22989
22990 if (obs == NULL)
22991 {
22992 char *retval
22993 = ((char *)
22994 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
22995
22996 strcpy (retval, lead);
22997 strcat (retval, prefix);
22998 strcat (retval, sep);
22999 strcat (retval, suffix);
23000 return retval;
23001 }
23002 else
23003 {
23004 /* We have an obstack. */
23005 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
23006 }
23007 }
23008
23009 /* Return sibling of die, NULL if no sibling. */
23010
23011 static struct die_info *
23012 sibling_die (struct die_info *die)
23013 {
23014 return die->sibling;
23015 }
23016
23017 /* Get name of a die, return NULL if not found. */
23018
23019 static const char *
23020 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
23021 struct obstack *obstack)
23022 {
23023 if (name && cu->language == language_cplus)
23024 {
23025 std::string canon_name = cp_canonicalize_string (name);
23026
23027 if (!canon_name.empty ())
23028 {
23029 if (canon_name != name)
23030 name = obstack_strdup (obstack, canon_name);
23031 }
23032 }
23033
23034 return name;
23035 }
23036
23037 /* Get name of a die, return NULL if not found.
23038 Anonymous namespaces are converted to their magic string. */
23039
23040 static const char *
23041 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
23042 {
23043 struct attribute *attr;
23044 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
23045
23046 attr = dwarf2_attr (die, DW_AT_name, cu);
23047 if ((!attr || !DW_STRING (attr))
23048 && die->tag != DW_TAG_namespace
23049 && die->tag != DW_TAG_class_type
23050 && die->tag != DW_TAG_interface_type
23051 && die->tag != DW_TAG_structure_type
23052 && die->tag != DW_TAG_union_type)
23053 return NULL;
23054
23055 switch (die->tag)
23056 {
23057 case DW_TAG_compile_unit:
23058 case DW_TAG_partial_unit:
23059 /* Compilation units have a DW_AT_name that is a filename, not
23060 a source language identifier. */
23061 case DW_TAG_enumeration_type:
23062 case DW_TAG_enumerator:
23063 /* These tags always have simple identifiers already; no need
23064 to canonicalize them. */
23065 return DW_STRING (attr);
23066
23067 case DW_TAG_namespace:
23068 if (attr != NULL && DW_STRING (attr) != NULL)
23069 return DW_STRING (attr);
23070 return CP_ANONYMOUS_NAMESPACE_STR;
23071
23072 case DW_TAG_class_type:
23073 case DW_TAG_interface_type:
23074 case DW_TAG_structure_type:
23075 case DW_TAG_union_type:
23076 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
23077 structures or unions. These were of the form "._%d" in GCC 4.1,
23078 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
23079 and GCC 4.4. We work around this problem by ignoring these. */
23080 if (attr && DW_STRING (attr)
23081 && (startswith (DW_STRING (attr), "._")
23082 || startswith (DW_STRING (attr), "<anonymous")))
23083 return NULL;
23084
23085 /* GCC might emit a nameless typedef that has a linkage name. See
23086 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
23087 if (!attr || DW_STRING (attr) == NULL)
23088 {
23089 attr = dw2_linkage_name_attr (die, cu);
23090 if (attr == NULL || DW_STRING (attr) == NULL)
23091 return NULL;
23092
23093 /* Avoid demangling DW_STRING (attr) the second time on a second
23094 call for the same DIE. */
23095 if (!DW_STRING_IS_CANONICAL (attr))
23096 {
23097 gdb::unique_xmalloc_ptr<char> demangled
23098 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
23099
23100 const char *base;
23101
23102 /* FIXME: we already did this for the partial symbol... */
23103 DW_STRING (attr)
23104 = obstack_strdup (&objfile->per_bfd->storage_obstack,
23105 demangled.get ());
23106 DW_STRING_IS_CANONICAL (attr) = 1;
23107
23108 /* Strip any leading namespaces/classes, keep only the base name.
23109 DW_AT_name for named DIEs does not contain the prefixes. */
23110 base = strrchr (DW_STRING (attr), ':');
23111 if (base && base > DW_STRING (attr) && base[-1] == ':')
23112 return &base[1];
23113 else
23114 return DW_STRING (attr);
23115 }
23116 }
23117 break;
23118
23119 default:
23120 break;
23121 }
23122
23123 if (!DW_STRING_IS_CANONICAL (attr))
23124 {
23125 DW_STRING (attr)
23126 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
23127 &objfile->per_bfd->storage_obstack);
23128 DW_STRING_IS_CANONICAL (attr) = 1;
23129 }
23130 return DW_STRING (attr);
23131 }
23132
23133 /* Return the die that this die in an extension of, or NULL if there
23134 is none. *EXT_CU is the CU containing DIE on input, and the CU
23135 containing the return value on output. */
23136
23137 static struct die_info *
23138 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
23139 {
23140 struct attribute *attr;
23141
23142 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
23143 if (attr == NULL)
23144 return NULL;
23145
23146 return follow_die_ref (die, attr, ext_cu);
23147 }
23148
23149 /* A convenience function that returns an "unknown" DWARF name,
23150 including the value of V. STR is the name of the entity being
23151 printed, e.g., "TAG". */
23152
23153 static const char *
23154 dwarf_unknown (const char *str, unsigned v)
23155 {
23156 char *cell = get_print_cell ();
23157 xsnprintf (cell, PRINT_CELL_SIZE, "DW_%s_<unknown: %u>", str, v);
23158 return cell;
23159 }
23160
23161 /* Convert a DIE tag into its string name. */
23162
23163 static const char *
23164 dwarf_tag_name (unsigned tag)
23165 {
23166 const char *name = get_DW_TAG_name (tag);
23167
23168 if (name == NULL)
23169 return dwarf_unknown ("TAG", tag);
23170
23171 return name;
23172 }
23173
23174 /* Convert a DWARF attribute code into its string name. */
23175
23176 static const char *
23177 dwarf_attr_name (unsigned attr)
23178 {
23179 const char *name;
23180
23181 #ifdef MIPS /* collides with DW_AT_HP_block_index */
23182 if (attr == DW_AT_MIPS_fde)
23183 return "DW_AT_MIPS_fde";
23184 #else
23185 if (attr == DW_AT_HP_block_index)
23186 return "DW_AT_HP_block_index";
23187 #endif
23188
23189 name = get_DW_AT_name (attr);
23190
23191 if (name == NULL)
23192 return dwarf_unknown ("AT", attr);
23193
23194 return name;
23195 }
23196
23197 /* Convert a unit type to corresponding DW_UT name. */
23198
23199 static const char *
23200 dwarf_unit_type_name (int unit_type) {
23201 switch (unit_type)
23202 {
23203 case 0x01:
23204 return "DW_UT_compile (0x01)";
23205 case 0x02:
23206 return "DW_UT_type (0x02)";
23207 case 0x03:
23208 return "DW_UT_partial (0x03)";
23209 case 0x04:
23210 return "DW_UT_skeleton (0x04)";
23211 case 0x05:
23212 return "DW_UT_split_compile (0x05)";
23213 case 0x06:
23214 return "DW_UT_split_type (0x06)";
23215 case 0x80:
23216 return "DW_UT_lo_user (0x80)";
23217 case 0xff:
23218 return "DW_UT_hi_user (0xff)";
23219 default:
23220 return nullptr;
23221 }
23222 }
23223
23224 /* Convert a DWARF value form code into its string name. */
23225
23226 static const char *
23227 dwarf_form_name (unsigned form)
23228 {
23229 const char *name = get_DW_FORM_name (form);
23230
23231 if (name == NULL)
23232 return dwarf_unknown ("FORM", form);
23233
23234 return name;
23235 }
23236
23237 static const char *
23238 dwarf_bool_name (unsigned mybool)
23239 {
23240 if (mybool)
23241 return "TRUE";
23242 else
23243 return "FALSE";
23244 }
23245
23246 /* Convert a DWARF type code into its string name. */
23247
23248 static const char *
23249 dwarf_type_encoding_name (unsigned enc)
23250 {
23251 const char *name = get_DW_ATE_name (enc);
23252
23253 if (name == NULL)
23254 return dwarf_unknown ("ATE", enc);
23255
23256 return name;
23257 }
23258
23259 static void
23260 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
23261 {
23262 unsigned int i;
23263
23264 print_spaces (indent, f);
23265 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
23266 dwarf_tag_name (die->tag), die->abbrev,
23267 sect_offset_str (die->sect_off));
23268
23269 if (die->parent != NULL)
23270 {
23271 print_spaces (indent, f);
23272 fprintf_unfiltered (f, " parent at offset: %s\n",
23273 sect_offset_str (die->parent->sect_off));
23274 }
23275
23276 print_spaces (indent, f);
23277 fprintf_unfiltered (f, " has children: %s\n",
23278 dwarf_bool_name (die->child != NULL));
23279
23280 print_spaces (indent, f);
23281 fprintf_unfiltered (f, " attributes:\n");
23282
23283 for (i = 0; i < die->num_attrs; ++i)
23284 {
23285 print_spaces (indent, f);
23286 fprintf_unfiltered (f, " %s (%s) ",
23287 dwarf_attr_name (die->attrs[i].name),
23288 dwarf_form_name (die->attrs[i].form));
23289
23290 switch (die->attrs[i].form)
23291 {
23292 case DW_FORM_addr:
23293 case DW_FORM_addrx:
23294 case DW_FORM_GNU_addr_index:
23295 fprintf_unfiltered (f, "address: ");
23296 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
23297 break;
23298 case DW_FORM_block2:
23299 case DW_FORM_block4:
23300 case DW_FORM_block:
23301 case DW_FORM_block1:
23302 fprintf_unfiltered (f, "block: size %s",
23303 pulongest (DW_BLOCK (&die->attrs[i])->size));
23304 break;
23305 case DW_FORM_exprloc:
23306 fprintf_unfiltered (f, "expression: size %s",
23307 pulongest (DW_BLOCK (&die->attrs[i])->size));
23308 break;
23309 case DW_FORM_data16:
23310 fprintf_unfiltered (f, "constant of 16 bytes");
23311 break;
23312 case DW_FORM_ref_addr:
23313 fprintf_unfiltered (f, "ref address: ");
23314 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
23315 break;
23316 case DW_FORM_GNU_ref_alt:
23317 fprintf_unfiltered (f, "alt ref address: ");
23318 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
23319 break;
23320 case DW_FORM_ref1:
23321 case DW_FORM_ref2:
23322 case DW_FORM_ref4:
23323 case DW_FORM_ref8:
23324 case DW_FORM_ref_udata:
23325 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
23326 (long) (DW_UNSND (&die->attrs[i])));
23327 break;
23328 case DW_FORM_data1:
23329 case DW_FORM_data2:
23330 case DW_FORM_data4:
23331 case DW_FORM_data8:
23332 case DW_FORM_udata:
23333 case DW_FORM_sdata:
23334 fprintf_unfiltered (f, "constant: %s",
23335 pulongest (DW_UNSND (&die->attrs[i])));
23336 break;
23337 case DW_FORM_sec_offset:
23338 fprintf_unfiltered (f, "section offset: %s",
23339 pulongest (DW_UNSND (&die->attrs[i])));
23340 break;
23341 case DW_FORM_ref_sig8:
23342 fprintf_unfiltered (f, "signature: %s",
23343 hex_string (DW_SIGNATURE (&die->attrs[i])));
23344 break;
23345 case DW_FORM_string:
23346 case DW_FORM_strp:
23347 case DW_FORM_line_strp:
23348 case DW_FORM_strx:
23349 case DW_FORM_GNU_str_index:
23350 case DW_FORM_GNU_strp_alt:
23351 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
23352 DW_STRING (&die->attrs[i])
23353 ? DW_STRING (&die->attrs[i]) : "",
23354 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
23355 break;
23356 case DW_FORM_flag:
23357 if (DW_UNSND (&die->attrs[i]))
23358 fprintf_unfiltered (f, "flag: TRUE");
23359 else
23360 fprintf_unfiltered (f, "flag: FALSE");
23361 break;
23362 case DW_FORM_flag_present:
23363 fprintf_unfiltered (f, "flag: TRUE");
23364 break;
23365 case DW_FORM_indirect:
23366 /* The reader will have reduced the indirect form to
23367 the "base form" so this form should not occur. */
23368 fprintf_unfiltered (f,
23369 "unexpected attribute form: DW_FORM_indirect");
23370 break;
23371 case DW_FORM_implicit_const:
23372 fprintf_unfiltered (f, "constant: %s",
23373 plongest (DW_SND (&die->attrs[i])));
23374 break;
23375 default:
23376 fprintf_unfiltered (f, "unsupported attribute form: %d.",
23377 die->attrs[i].form);
23378 break;
23379 }
23380 fprintf_unfiltered (f, "\n");
23381 }
23382 }
23383
23384 static void
23385 dump_die_for_error (struct die_info *die)
23386 {
23387 dump_die_shallow (gdb_stderr, 0, die);
23388 }
23389
23390 static void
23391 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
23392 {
23393 int indent = level * 4;
23394
23395 gdb_assert (die != NULL);
23396
23397 if (level >= max_level)
23398 return;
23399
23400 dump_die_shallow (f, indent, die);
23401
23402 if (die->child != NULL)
23403 {
23404 print_spaces (indent, f);
23405 fprintf_unfiltered (f, " Children:");
23406 if (level + 1 < max_level)
23407 {
23408 fprintf_unfiltered (f, "\n");
23409 dump_die_1 (f, level + 1, max_level, die->child);
23410 }
23411 else
23412 {
23413 fprintf_unfiltered (f,
23414 " [not printed, max nesting level reached]\n");
23415 }
23416 }
23417
23418 if (die->sibling != NULL && level > 0)
23419 {
23420 dump_die_1 (f, level, max_level, die->sibling);
23421 }
23422 }
23423
23424 /* This is called from the pdie macro in gdbinit.in.
23425 It's not static so gcc will keep a copy callable from gdb. */
23426
23427 void
23428 dump_die (struct die_info *die, int max_level)
23429 {
23430 dump_die_1 (gdb_stdlog, 0, max_level, die);
23431 }
23432
23433 static void
23434 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
23435 {
23436 void **slot;
23437
23438 slot = htab_find_slot_with_hash (cu->die_hash, die,
23439 to_underlying (die->sect_off),
23440 INSERT);
23441
23442 *slot = die;
23443 }
23444
23445 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
23446 required kind. */
23447
23448 static sect_offset
23449 dwarf2_get_ref_die_offset (const struct attribute *attr)
23450 {
23451 if (attr_form_is_ref (attr))
23452 return (sect_offset) DW_UNSND (attr);
23453
23454 complaint (_("unsupported die ref attribute form: '%s'"),
23455 dwarf_form_name (attr->form));
23456 return {};
23457 }
23458
23459 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
23460 * the value held by the attribute is not constant. */
23461
23462 static LONGEST
23463 dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
23464 {
23465 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
23466 return DW_SND (attr);
23467 else if (attr->form == DW_FORM_udata
23468 || attr->form == DW_FORM_data1
23469 || attr->form == DW_FORM_data2
23470 || attr->form == DW_FORM_data4
23471 || attr->form == DW_FORM_data8)
23472 return DW_UNSND (attr);
23473 else
23474 {
23475 /* For DW_FORM_data16 see attr_form_is_constant. */
23476 complaint (_("Attribute value is not a constant (%s)"),
23477 dwarf_form_name (attr->form));
23478 return default_value;
23479 }
23480 }
23481
23482 /* Follow reference or signature attribute ATTR of SRC_DIE.
23483 On entry *REF_CU is the CU of SRC_DIE.
23484 On exit *REF_CU is the CU of the result. */
23485
23486 static struct die_info *
23487 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
23488 struct dwarf2_cu **ref_cu)
23489 {
23490 struct die_info *die;
23491
23492 if (attr_form_is_ref (attr))
23493 die = follow_die_ref (src_die, attr, ref_cu);
23494 else if (attr->form == DW_FORM_ref_sig8)
23495 die = follow_die_sig (src_die, attr, ref_cu);
23496 else
23497 {
23498 dump_die_for_error (src_die);
23499 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
23500 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23501 }
23502
23503 return die;
23504 }
23505
23506 /* Follow reference OFFSET.
23507 On entry *REF_CU is the CU of the source die referencing OFFSET.
23508 On exit *REF_CU is the CU of the result.
23509 Returns NULL if OFFSET is invalid. */
23510
23511 static struct die_info *
23512 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
23513 struct dwarf2_cu **ref_cu)
23514 {
23515 struct die_info temp_die;
23516 struct dwarf2_cu *target_cu, *cu = *ref_cu;
23517 struct dwarf2_per_objfile *dwarf2_per_objfile
23518 = cu->per_cu->dwarf2_per_objfile;
23519
23520 gdb_assert (cu->per_cu != NULL);
23521
23522 target_cu = cu;
23523
23524 if (cu->per_cu->is_debug_types)
23525 {
23526 /* .debug_types CUs cannot reference anything outside their CU.
23527 If they need to, they have to reference a signatured type via
23528 DW_FORM_ref_sig8. */
23529 if (!offset_in_cu_p (&cu->header, sect_off))
23530 return NULL;
23531 }
23532 else if (offset_in_dwz != cu->per_cu->is_dwz
23533 || !offset_in_cu_p (&cu->header, sect_off))
23534 {
23535 struct dwarf2_per_cu_data *per_cu;
23536
23537 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
23538 dwarf2_per_objfile);
23539
23540 /* If necessary, add it to the queue and load its DIEs. */
23541 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
23542 load_full_comp_unit (per_cu, false, cu->language);
23543
23544 target_cu = per_cu->cu;
23545 }
23546 else if (cu->dies == NULL)
23547 {
23548 /* We're loading full DIEs during partial symbol reading. */
23549 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
23550 load_full_comp_unit (cu->per_cu, false, language_minimal);
23551 }
23552
23553 *ref_cu = target_cu;
23554 temp_die.sect_off = sect_off;
23555
23556 if (target_cu != cu)
23557 target_cu->ancestor = cu;
23558
23559 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
23560 &temp_die,
23561 to_underlying (sect_off));
23562 }
23563
23564 /* Follow reference attribute ATTR of SRC_DIE.
23565 On entry *REF_CU is the CU of SRC_DIE.
23566 On exit *REF_CU is the CU of the result. */
23567
23568 static struct die_info *
23569 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
23570 struct dwarf2_cu **ref_cu)
23571 {
23572 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
23573 struct dwarf2_cu *cu = *ref_cu;
23574 struct die_info *die;
23575
23576 die = follow_die_offset (sect_off,
23577 (attr->form == DW_FORM_GNU_ref_alt
23578 || cu->per_cu->is_dwz),
23579 ref_cu);
23580 if (!die)
23581 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
23582 "at %s [in module %s]"),
23583 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
23584 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
23585
23586 return die;
23587 }
23588
23589 /* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
23590 Returned value is intended for DW_OP_call*. Returned
23591 dwarf2_locexpr_baton->data has lifetime of
23592 PER_CU->DWARF2_PER_OBJFILE->OBJFILE. */
23593
23594 struct dwarf2_locexpr_baton
23595 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
23596 struct dwarf2_per_cu_data *per_cu,
23597 CORE_ADDR (*get_frame_pc) (void *baton),
23598 void *baton, bool resolve_abstract_p)
23599 {
23600 struct dwarf2_cu *cu;
23601 struct die_info *die;
23602 struct attribute *attr;
23603 struct dwarf2_locexpr_baton retval;
23604 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
23605 struct objfile *objfile = dwarf2_per_objfile->objfile;
23606
23607 if (per_cu->cu == NULL)
23608 load_cu (per_cu, false);
23609 cu = per_cu->cu;
23610 if (cu == NULL)
23611 {
23612 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23613 Instead just throw an error, not much else we can do. */
23614 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23615 sect_offset_str (sect_off), objfile_name (objfile));
23616 }
23617
23618 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23619 if (!die)
23620 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23621 sect_offset_str (sect_off), objfile_name (objfile));
23622
23623 attr = dwarf2_attr (die, DW_AT_location, cu);
23624 if (!attr && resolve_abstract_p
23625 && (dwarf2_per_objfile->abstract_to_concrete.find (die->sect_off)
23626 != dwarf2_per_objfile->abstract_to_concrete.end ()))
23627 {
23628 CORE_ADDR pc = (*get_frame_pc) (baton);
23629 CORE_ADDR baseaddr = objfile->text_section_offset ();
23630 struct gdbarch *gdbarch = get_objfile_arch (objfile);
23631
23632 for (const auto &cand_off
23633 : dwarf2_per_objfile->abstract_to_concrete[die->sect_off])
23634 {
23635 struct dwarf2_cu *cand_cu = cu;
23636 struct die_info *cand
23637 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
23638 if (!cand
23639 || !cand->parent
23640 || cand->parent->tag != DW_TAG_subprogram)
23641 continue;
23642
23643 CORE_ADDR pc_low, pc_high;
23644 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
23645 if (pc_low == ((CORE_ADDR) -1))
23646 continue;
23647 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
23648 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
23649 if (!(pc_low <= pc && pc < pc_high))
23650 continue;
23651
23652 die = cand;
23653 attr = dwarf2_attr (die, DW_AT_location, cu);
23654 break;
23655 }
23656 }
23657
23658 if (!attr)
23659 {
23660 /* DWARF: "If there is no such attribute, then there is no effect.".
23661 DATA is ignored if SIZE is 0. */
23662
23663 retval.data = NULL;
23664 retval.size = 0;
23665 }
23666 else if (attr_form_is_section_offset (attr))
23667 {
23668 struct dwarf2_loclist_baton loclist_baton;
23669 CORE_ADDR pc = (*get_frame_pc) (baton);
23670 size_t size;
23671
23672 fill_in_loclist_baton (cu, &loclist_baton, attr);
23673
23674 retval.data = dwarf2_find_location_expression (&loclist_baton,
23675 &size, pc);
23676 retval.size = size;
23677 }
23678 else
23679 {
23680 if (!attr_form_is_block (attr))
23681 error (_("Dwarf Error: DIE at %s referenced in module %s "
23682 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
23683 sect_offset_str (sect_off), objfile_name (objfile));
23684
23685 retval.data = DW_BLOCK (attr)->data;
23686 retval.size = DW_BLOCK (attr)->size;
23687 }
23688 retval.per_cu = cu->per_cu;
23689
23690 age_cached_comp_units (dwarf2_per_objfile);
23691
23692 return retval;
23693 }
23694
23695 /* Like dwarf2_fetch_die_loc_sect_off, but take a CU
23696 offset. */
23697
23698 struct dwarf2_locexpr_baton
23699 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
23700 struct dwarf2_per_cu_data *per_cu,
23701 CORE_ADDR (*get_frame_pc) (void *baton),
23702 void *baton)
23703 {
23704 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
23705
23706 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
23707 }
23708
23709 /* Write a constant of a given type as target-ordered bytes into
23710 OBSTACK. */
23711
23712 static const gdb_byte *
23713 write_constant_as_bytes (struct obstack *obstack,
23714 enum bfd_endian byte_order,
23715 struct type *type,
23716 ULONGEST value,
23717 LONGEST *len)
23718 {
23719 gdb_byte *result;
23720
23721 *len = TYPE_LENGTH (type);
23722 result = (gdb_byte *) obstack_alloc (obstack, *len);
23723 store_unsigned_integer (result, *len, byte_order, value);
23724
23725 return result;
23726 }
23727
23728 /* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
23729 pointer to the constant bytes and set LEN to the length of the
23730 data. If memory is needed, allocate it on OBSTACK. If the DIE
23731 does not have a DW_AT_const_value, return NULL. */
23732
23733 const gdb_byte *
23734 dwarf2_fetch_constant_bytes (sect_offset sect_off,
23735 struct dwarf2_per_cu_data *per_cu,
23736 struct obstack *obstack,
23737 LONGEST *len)
23738 {
23739 struct dwarf2_cu *cu;
23740 struct die_info *die;
23741 struct attribute *attr;
23742 const gdb_byte *result = NULL;
23743 struct type *type;
23744 LONGEST value;
23745 enum bfd_endian byte_order;
23746 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
23747
23748 if (per_cu->cu == NULL)
23749 load_cu (per_cu, false);
23750 cu = per_cu->cu;
23751 if (cu == NULL)
23752 {
23753 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23754 Instead just throw an error, not much else we can do. */
23755 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23756 sect_offset_str (sect_off), objfile_name (objfile));
23757 }
23758
23759 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23760 if (!die)
23761 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23762 sect_offset_str (sect_off), objfile_name (objfile));
23763
23764 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23765 if (attr == NULL)
23766 return NULL;
23767
23768 byte_order = (bfd_big_endian (objfile->obfd)
23769 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23770
23771 switch (attr->form)
23772 {
23773 case DW_FORM_addr:
23774 case DW_FORM_addrx:
23775 case DW_FORM_GNU_addr_index:
23776 {
23777 gdb_byte *tem;
23778
23779 *len = cu->header.addr_size;
23780 tem = (gdb_byte *) obstack_alloc (obstack, *len);
23781 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
23782 result = tem;
23783 }
23784 break;
23785 case DW_FORM_string:
23786 case DW_FORM_strp:
23787 case DW_FORM_strx:
23788 case DW_FORM_GNU_str_index:
23789 case DW_FORM_GNU_strp_alt:
23790 /* DW_STRING is already allocated on the objfile obstack, point
23791 directly to it. */
23792 result = (const gdb_byte *) DW_STRING (attr);
23793 *len = strlen (DW_STRING (attr));
23794 break;
23795 case DW_FORM_block1:
23796 case DW_FORM_block2:
23797 case DW_FORM_block4:
23798 case DW_FORM_block:
23799 case DW_FORM_exprloc:
23800 case DW_FORM_data16:
23801 result = DW_BLOCK (attr)->data;
23802 *len = DW_BLOCK (attr)->size;
23803 break;
23804
23805 /* The DW_AT_const_value attributes are supposed to carry the
23806 symbol's value "represented as it would be on the target
23807 architecture." By the time we get here, it's already been
23808 converted to host endianness, so we just need to sign- or
23809 zero-extend it as appropriate. */
23810 case DW_FORM_data1:
23811 type = die_type (die, cu);
23812 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23813 if (result == NULL)
23814 result = write_constant_as_bytes (obstack, byte_order,
23815 type, value, len);
23816 break;
23817 case DW_FORM_data2:
23818 type = die_type (die, cu);
23819 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23820 if (result == NULL)
23821 result = write_constant_as_bytes (obstack, byte_order,
23822 type, value, len);
23823 break;
23824 case DW_FORM_data4:
23825 type = die_type (die, cu);
23826 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23827 if (result == NULL)
23828 result = write_constant_as_bytes (obstack, byte_order,
23829 type, value, len);
23830 break;
23831 case DW_FORM_data8:
23832 type = die_type (die, cu);
23833 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23834 if (result == NULL)
23835 result = write_constant_as_bytes (obstack, byte_order,
23836 type, value, len);
23837 break;
23838
23839 case DW_FORM_sdata:
23840 case DW_FORM_implicit_const:
23841 type = die_type (die, cu);
23842 result = write_constant_as_bytes (obstack, byte_order,
23843 type, DW_SND (attr), len);
23844 break;
23845
23846 case DW_FORM_udata:
23847 type = die_type (die, cu);
23848 result = write_constant_as_bytes (obstack, byte_order,
23849 type, DW_UNSND (attr), len);
23850 break;
23851
23852 default:
23853 complaint (_("unsupported const value attribute form: '%s'"),
23854 dwarf_form_name (attr->form));
23855 break;
23856 }
23857
23858 return result;
23859 }
23860
23861 /* Return the type of the die at OFFSET in PER_CU. Return NULL if no
23862 valid type for this die is found. */
23863
23864 struct type *
23865 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
23866 struct dwarf2_per_cu_data *per_cu)
23867 {
23868 struct dwarf2_cu *cu;
23869 struct die_info *die;
23870
23871 if (per_cu->cu == NULL)
23872 load_cu (per_cu, false);
23873 cu = per_cu->cu;
23874 if (!cu)
23875 return NULL;
23876
23877 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23878 if (!die)
23879 return NULL;
23880
23881 return die_type (die, cu);
23882 }
23883
23884 /* Return the type of the DIE at DIE_OFFSET in the CU named by
23885 PER_CU. */
23886
23887 struct type *
23888 dwarf2_get_die_type (cu_offset die_offset,
23889 struct dwarf2_per_cu_data *per_cu)
23890 {
23891 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
23892 return get_die_type_at_offset (die_offset_sect, per_cu);
23893 }
23894
23895 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
23896 On entry *REF_CU is the CU of SRC_DIE.
23897 On exit *REF_CU is the CU of the result.
23898 Returns NULL if the referenced DIE isn't found. */
23899
23900 static struct die_info *
23901 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23902 struct dwarf2_cu **ref_cu)
23903 {
23904 struct die_info temp_die;
23905 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
23906 struct die_info *die;
23907
23908 /* While it might be nice to assert sig_type->type == NULL here,
23909 we can get here for DW_AT_imported_declaration where we need
23910 the DIE not the type. */
23911
23912 /* If necessary, add it to the queue and load its DIEs. */
23913
23914 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
23915 read_signatured_type (sig_type);
23916
23917 sig_cu = sig_type->per_cu.cu;
23918 gdb_assert (sig_cu != NULL);
23919 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23920 temp_die.sect_off = sig_type->type_offset_in_section;
23921 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
23922 to_underlying (temp_die.sect_off));
23923 if (die)
23924 {
23925 struct dwarf2_per_objfile *dwarf2_per_objfile
23926 = (*ref_cu)->per_cu->dwarf2_per_objfile;
23927
23928 /* For .gdb_index version 7 keep track of included TUs.
23929 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23930 if (dwarf2_per_objfile->index_table != NULL
23931 && dwarf2_per_objfile->index_table->version <= 7)
23932 {
23933 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
23934 }
23935
23936 *ref_cu = sig_cu;
23937 if (sig_cu != cu)
23938 sig_cu->ancestor = cu;
23939
23940 return die;
23941 }
23942
23943 return NULL;
23944 }
23945
23946 /* Follow signatured type referenced by ATTR in SRC_DIE.
23947 On entry *REF_CU is the CU of SRC_DIE.
23948 On exit *REF_CU is the CU of the result.
23949 The result is the DIE of the type.
23950 If the referenced type cannot be found an error is thrown. */
23951
23952 static struct die_info *
23953 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
23954 struct dwarf2_cu **ref_cu)
23955 {
23956 ULONGEST signature = DW_SIGNATURE (attr);
23957 struct signatured_type *sig_type;
23958 struct die_info *die;
23959
23960 gdb_assert (attr->form == DW_FORM_ref_sig8);
23961
23962 sig_type = lookup_signatured_type (*ref_cu, signature);
23963 /* sig_type will be NULL if the signatured type is missing from
23964 the debug info. */
23965 if (sig_type == NULL)
23966 {
23967 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23968 " from DIE at %s [in module %s]"),
23969 hex_string (signature), sect_offset_str (src_die->sect_off),
23970 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23971 }
23972
23973 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23974 if (die == NULL)
23975 {
23976 dump_die_for_error (src_die);
23977 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23978 " from DIE at %s [in module %s]"),
23979 hex_string (signature), sect_offset_str (src_die->sect_off),
23980 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23981 }
23982
23983 return die;
23984 }
23985
23986 /* Get the type specified by SIGNATURE referenced in DIE/CU,
23987 reading in and processing the type unit if necessary. */
23988
23989 static struct type *
23990 get_signatured_type (struct die_info *die, ULONGEST signature,
23991 struct dwarf2_cu *cu)
23992 {
23993 struct dwarf2_per_objfile *dwarf2_per_objfile
23994 = cu->per_cu->dwarf2_per_objfile;
23995 struct signatured_type *sig_type;
23996 struct dwarf2_cu *type_cu;
23997 struct die_info *type_die;
23998 struct type *type;
23999
24000 sig_type = lookup_signatured_type (cu, signature);
24001 /* sig_type will be NULL if the signatured type is missing from
24002 the debug info. */
24003 if (sig_type == NULL)
24004 {
24005 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
24006 " from DIE at %s [in module %s]"),
24007 hex_string (signature), sect_offset_str (die->sect_off),
24008 objfile_name (dwarf2_per_objfile->objfile));
24009 return build_error_marker_type (cu, die);
24010 }
24011
24012 /* If we already know the type we're done. */
24013 if (sig_type->type != NULL)
24014 return sig_type->type;
24015
24016 type_cu = cu;
24017 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
24018 if (type_die != NULL)
24019 {
24020 /* N.B. We need to call get_die_type to ensure only one type for this DIE
24021 is created. This is important, for example, because for c++ classes
24022 we need TYPE_NAME set which is only done by new_symbol. Blech. */
24023 type = read_type_die (type_die, type_cu);
24024 if (type == NULL)
24025 {
24026 complaint (_("Dwarf Error: Cannot build signatured type %s"
24027 " referenced from DIE at %s [in module %s]"),
24028 hex_string (signature), sect_offset_str (die->sect_off),
24029 objfile_name (dwarf2_per_objfile->objfile));
24030 type = build_error_marker_type (cu, die);
24031 }
24032 }
24033 else
24034 {
24035 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
24036 " from DIE at %s [in module %s]"),
24037 hex_string (signature), sect_offset_str (die->sect_off),
24038 objfile_name (dwarf2_per_objfile->objfile));
24039 type = build_error_marker_type (cu, die);
24040 }
24041 sig_type->type = type;
24042
24043 return type;
24044 }
24045
24046 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
24047 reading in and processing the type unit if necessary. */
24048
24049 static struct type *
24050 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
24051 struct dwarf2_cu *cu) /* ARI: editCase function */
24052 {
24053 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
24054 if (attr_form_is_ref (attr))
24055 {
24056 struct dwarf2_cu *type_cu = cu;
24057 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
24058
24059 return read_type_die (type_die, type_cu);
24060 }
24061 else if (attr->form == DW_FORM_ref_sig8)
24062 {
24063 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
24064 }
24065 else
24066 {
24067 struct dwarf2_per_objfile *dwarf2_per_objfile
24068 = cu->per_cu->dwarf2_per_objfile;
24069
24070 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
24071 " at %s [in module %s]"),
24072 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
24073 objfile_name (dwarf2_per_objfile->objfile));
24074 return build_error_marker_type (cu, die);
24075 }
24076 }
24077
24078 /* Load the DIEs associated with type unit PER_CU into memory. */
24079
24080 static void
24081 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
24082 {
24083 struct signatured_type *sig_type;
24084
24085 /* Caller is responsible for ensuring type_unit_groups don't get here. */
24086 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
24087
24088 /* We have the per_cu, but we need the signatured_type.
24089 Fortunately this is an easy translation. */
24090 gdb_assert (per_cu->is_debug_types);
24091 sig_type = (struct signatured_type *) per_cu;
24092
24093 gdb_assert (per_cu->cu == NULL);
24094
24095 read_signatured_type (sig_type);
24096
24097 gdb_assert (per_cu->cu != NULL);
24098 }
24099
24100 /* Read in a signatured type and build its CU and DIEs.
24101 If the type is a stub for the real type in a DWO file,
24102 read in the real type from the DWO file as well. */
24103
24104 static void
24105 read_signatured_type (struct signatured_type *sig_type)
24106 {
24107 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
24108
24109 gdb_assert (per_cu->is_debug_types);
24110 gdb_assert (per_cu->cu == NULL);
24111
24112 cutu_reader reader (per_cu, NULL, 0, 1, false);
24113
24114 if (!reader.dummy_p)
24115 {
24116 struct dwarf2_cu *cu = reader.cu;
24117 const gdb_byte *info_ptr = reader.info_ptr;
24118
24119 gdb_assert (cu->die_hash == NULL);
24120 cu->die_hash =
24121 htab_create_alloc_ex (cu->header.length / 12,
24122 die_hash,
24123 die_eq,
24124 NULL,
24125 &cu->comp_unit_obstack,
24126 hashtab_obstack_allocate,
24127 dummy_obstack_deallocate);
24128
24129 if (reader.has_children)
24130 reader.comp_unit_die->child
24131 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
24132 reader.comp_unit_die);
24133 cu->dies = reader.comp_unit_die;
24134 /* comp_unit_die is not stored in die_hash, no need. */
24135
24136 /* We try not to read any attributes in this function, because
24137 not all CUs needed for references have been loaded yet, and
24138 symbol table processing isn't initialized. But we have to
24139 set the CU language, or we won't be able to build types
24140 correctly. Similarly, if we do not read the producer, we can
24141 not apply producer-specific interpretation. */
24142 prepare_one_comp_unit (cu, cu->dies, language_minimal);
24143 }
24144
24145 sig_type->per_cu.tu_read = 1;
24146 }
24147
24148 /* Decode simple location descriptions.
24149 Given a pointer to a dwarf block that defines a location, compute
24150 the location and return the value.
24151
24152 NOTE drow/2003-11-18: This function is called in two situations
24153 now: for the address of static or global variables (partial symbols
24154 only) and for offsets into structures which are expected to be
24155 (more or less) constant. The partial symbol case should go away,
24156 and only the constant case should remain. That will let this
24157 function complain more accurately. A few special modes are allowed
24158 without complaint for global variables (for instance, global
24159 register values and thread-local values).
24160
24161 A location description containing no operations indicates that the
24162 object is optimized out. The return value is 0 for that case.
24163 FIXME drow/2003-11-16: No callers check for this case any more; soon all
24164 callers will only want a very basic result and this can become a
24165 complaint.
24166
24167 Note that stack[0] is unused except as a default error return. */
24168
24169 static CORE_ADDR
24170 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
24171 {
24172 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
24173 size_t i;
24174 size_t size = blk->size;
24175 const gdb_byte *data = blk->data;
24176 CORE_ADDR stack[64];
24177 int stacki;
24178 unsigned int bytes_read, unsnd;
24179 gdb_byte op;
24180
24181 i = 0;
24182 stacki = 0;
24183 stack[stacki] = 0;
24184 stack[++stacki] = 0;
24185
24186 while (i < size)
24187 {
24188 op = data[i++];
24189 switch (op)
24190 {
24191 case DW_OP_lit0:
24192 case DW_OP_lit1:
24193 case DW_OP_lit2:
24194 case DW_OP_lit3:
24195 case DW_OP_lit4:
24196 case DW_OP_lit5:
24197 case DW_OP_lit6:
24198 case DW_OP_lit7:
24199 case DW_OP_lit8:
24200 case DW_OP_lit9:
24201 case DW_OP_lit10:
24202 case DW_OP_lit11:
24203 case DW_OP_lit12:
24204 case DW_OP_lit13:
24205 case DW_OP_lit14:
24206 case DW_OP_lit15:
24207 case DW_OP_lit16:
24208 case DW_OP_lit17:
24209 case DW_OP_lit18:
24210 case DW_OP_lit19:
24211 case DW_OP_lit20:
24212 case DW_OP_lit21:
24213 case DW_OP_lit22:
24214 case DW_OP_lit23:
24215 case DW_OP_lit24:
24216 case DW_OP_lit25:
24217 case DW_OP_lit26:
24218 case DW_OP_lit27:
24219 case DW_OP_lit28:
24220 case DW_OP_lit29:
24221 case DW_OP_lit30:
24222 case DW_OP_lit31:
24223 stack[++stacki] = op - DW_OP_lit0;
24224 break;
24225
24226 case DW_OP_reg0:
24227 case DW_OP_reg1:
24228 case DW_OP_reg2:
24229 case DW_OP_reg3:
24230 case DW_OP_reg4:
24231 case DW_OP_reg5:
24232 case DW_OP_reg6:
24233 case DW_OP_reg7:
24234 case DW_OP_reg8:
24235 case DW_OP_reg9:
24236 case DW_OP_reg10:
24237 case DW_OP_reg11:
24238 case DW_OP_reg12:
24239 case DW_OP_reg13:
24240 case DW_OP_reg14:
24241 case DW_OP_reg15:
24242 case DW_OP_reg16:
24243 case DW_OP_reg17:
24244 case DW_OP_reg18:
24245 case DW_OP_reg19:
24246 case DW_OP_reg20:
24247 case DW_OP_reg21:
24248 case DW_OP_reg22:
24249 case DW_OP_reg23:
24250 case DW_OP_reg24:
24251 case DW_OP_reg25:
24252 case DW_OP_reg26:
24253 case DW_OP_reg27:
24254 case DW_OP_reg28:
24255 case DW_OP_reg29:
24256 case DW_OP_reg30:
24257 case DW_OP_reg31:
24258 stack[++stacki] = op - DW_OP_reg0;
24259 if (i < size)
24260 dwarf2_complex_location_expr_complaint ();
24261 break;
24262
24263 case DW_OP_regx:
24264 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
24265 i += bytes_read;
24266 stack[++stacki] = unsnd;
24267 if (i < size)
24268 dwarf2_complex_location_expr_complaint ();
24269 break;
24270
24271 case DW_OP_addr:
24272 stack[++stacki] = read_address (objfile->obfd, &data[i],
24273 cu, &bytes_read);
24274 i += bytes_read;
24275 break;
24276
24277 case DW_OP_const1u:
24278 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
24279 i += 1;
24280 break;
24281
24282 case DW_OP_const1s:
24283 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
24284 i += 1;
24285 break;
24286
24287 case DW_OP_const2u:
24288 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
24289 i += 2;
24290 break;
24291
24292 case DW_OP_const2s:
24293 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
24294 i += 2;
24295 break;
24296
24297 case DW_OP_const4u:
24298 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
24299 i += 4;
24300 break;
24301
24302 case DW_OP_const4s:
24303 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
24304 i += 4;
24305 break;
24306
24307 case DW_OP_const8u:
24308 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
24309 i += 8;
24310 break;
24311
24312 case DW_OP_constu:
24313 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
24314 &bytes_read);
24315 i += bytes_read;
24316 break;
24317
24318 case DW_OP_consts:
24319 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
24320 i += bytes_read;
24321 break;
24322
24323 case DW_OP_dup:
24324 stack[stacki + 1] = stack[stacki];
24325 stacki++;
24326 break;
24327
24328 case DW_OP_plus:
24329 stack[stacki - 1] += stack[stacki];
24330 stacki--;
24331 break;
24332
24333 case DW_OP_plus_uconst:
24334 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
24335 &bytes_read);
24336 i += bytes_read;
24337 break;
24338
24339 case DW_OP_minus:
24340 stack[stacki - 1] -= stack[stacki];
24341 stacki--;
24342 break;
24343
24344 case DW_OP_deref:
24345 /* If we're not the last op, then we definitely can't encode
24346 this using GDB's address_class enum. This is valid for partial
24347 global symbols, although the variable's address will be bogus
24348 in the psymtab. */
24349 if (i < size)
24350 dwarf2_complex_location_expr_complaint ();
24351 break;
24352
24353 case DW_OP_GNU_push_tls_address:
24354 case DW_OP_form_tls_address:
24355 /* The top of the stack has the offset from the beginning
24356 of the thread control block at which the variable is located. */
24357 /* Nothing should follow this operator, so the top of stack would
24358 be returned. */
24359 /* This is valid for partial global symbols, but the variable's
24360 address will be bogus in the psymtab. Make it always at least
24361 non-zero to not look as a variable garbage collected by linker
24362 which have DW_OP_addr 0. */
24363 if (i < size)
24364 dwarf2_complex_location_expr_complaint ();
24365 stack[stacki]++;
24366 break;
24367
24368 case DW_OP_GNU_uninit:
24369 break;
24370
24371 case DW_OP_addrx:
24372 case DW_OP_GNU_addr_index:
24373 case DW_OP_GNU_const_index:
24374 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
24375 &bytes_read);
24376 i += bytes_read;
24377 break;
24378
24379 default:
24380 {
24381 const char *name = get_DW_OP_name (op);
24382
24383 if (name)
24384 complaint (_("unsupported stack op: '%s'"),
24385 name);
24386 else
24387 complaint (_("unsupported stack op: '%02x'"),
24388 op);
24389 }
24390
24391 return (stack[stacki]);
24392 }
24393
24394 /* Enforce maximum stack depth of SIZE-1 to avoid writing
24395 outside of the allocated space. Also enforce minimum>0. */
24396 if (stacki >= ARRAY_SIZE (stack) - 1)
24397 {
24398 complaint (_("location description stack overflow"));
24399 return 0;
24400 }
24401
24402 if (stacki <= 0)
24403 {
24404 complaint (_("location description stack underflow"));
24405 return 0;
24406 }
24407 }
24408 return (stack[stacki]);
24409 }
24410
24411 /* memory allocation interface */
24412
24413 static struct dwarf_block *
24414 dwarf_alloc_block (struct dwarf2_cu *cu)
24415 {
24416 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
24417 }
24418
24419 static struct die_info *
24420 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
24421 {
24422 struct die_info *die;
24423 size_t size = sizeof (struct die_info);
24424
24425 if (num_attrs > 1)
24426 size += (num_attrs - 1) * sizeof (struct attribute);
24427
24428 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
24429 memset (die, 0, sizeof (struct die_info));
24430 return (die);
24431 }
24432
24433 \f
24434 /* Macro support. */
24435
24436 /* Return file name relative to the compilation directory of file number I in
24437 *LH's file name table. The result is allocated using xmalloc; the caller is
24438 responsible for freeing it. */
24439
24440 static char *
24441 file_file_name (int file, struct line_header *lh)
24442 {
24443 /* Is the file number a valid index into the line header's file name
24444 table? Remember that file numbers start with one, not zero. */
24445 if (lh->is_valid_file_index (file))
24446 {
24447 const file_entry *fe = lh->file_name_at (file);
24448
24449 if (!IS_ABSOLUTE_PATH (fe->name))
24450 {
24451 const char *dir = fe->include_dir (lh);
24452 if (dir != NULL)
24453 return concat (dir, SLASH_STRING, fe->name, (char *) NULL);
24454 }
24455 return xstrdup (fe->name);
24456 }
24457 else
24458 {
24459 /* The compiler produced a bogus file number. We can at least
24460 record the macro definitions made in the file, even if we
24461 won't be able to find the file by name. */
24462 char fake_name[80];
24463
24464 xsnprintf (fake_name, sizeof (fake_name),
24465 "<bad macro file number %d>", file);
24466
24467 complaint (_("bad file number in macro information (%d)"),
24468 file);
24469
24470 return xstrdup (fake_name);
24471 }
24472 }
24473
24474 /* Return the full name of file number I in *LH's file name table.
24475 Use COMP_DIR as the name of the current directory of the
24476 compilation. The result is allocated using xmalloc; the caller is
24477 responsible for freeing it. */
24478 static char *
24479 file_full_name (int file, struct line_header *lh, const char *comp_dir)
24480 {
24481 /* Is the file number a valid index into the line header's file name
24482 table? Remember that file numbers start with one, not zero. */
24483 if (lh->is_valid_file_index (file))
24484 {
24485 char *relative = file_file_name (file, lh);
24486
24487 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
24488 return relative;
24489 return reconcat (relative, comp_dir, SLASH_STRING,
24490 relative, (char *) NULL);
24491 }
24492 else
24493 return file_file_name (file, lh);
24494 }
24495
24496
24497 static struct macro_source_file *
24498 macro_start_file (struct dwarf2_cu *cu,
24499 int file, int line,
24500 struct macro_source_file *current_file,
24501 struct line_header *lh)
24502 {
24503 /* File name relative to the compilation directory of this source file. */
24504 char *file_name = file_file_name (file, lh);
24505
24506 if (! current_file)
24507 {
24508 /* Note: We don't create a macro table for this compilation unit
24509 at all until we actually get a filename. */
24510 struct macro_table *macro_table = cu->get_builder ()->get_macro_table ();
24511
24512 /* If we have no current file, then this must be the start_file
24513 directive for the compilation unit's main source file. */
24514 current_file = macro_set_main (macro_table, file_name);
24515 macro_define_special (macro_table);
24516 }
24517 else
24518 current_file = macro_include (current_file, line, file_name);
24519
24520 xfree (file_name);
24521
24522 return current_file;
24523 }
24524
24525 static const char *
24526 consume_improper_spaces (const char *p, const char *body)
24527 {
24528 if (*p == ' ')
24529 {
24530 complaint (_("macro definition contains spaces "
24531 "in formal argument list:\n`%s'"),
24532 body);
24533
24534 while (*p == ' ')
24535 p++;
24536 }
24537
24538 return p;
24539 }
24540
24541
24542 static void
24543 parse_macro_definition (struct macro_source_file *file, int line,
24544 const char *body)
24545 {
24546 const char *p;
24547
24548 /* The body string takes one of two forms. For object-like macro
24549 definitions, it should be:
24550
24551 <macro name> " " <definition>
24552
24553 For function-like macro definitions, it should be:
24554
24555 <macro name> "() " <definition>
24556 or
24557 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
24558
24559 Spaces may appear only where explicitly indicated, and in the
24560 <definition>.
24561
24562 The Dwarf 2 spec says that an object-like macro's name is always
24563 followed by a space, but versions of GCC around March 2002 omit
24564 the space when the macro's definition is the empty string.
24565
24566 The Dwarf 2 spec says that there should be no spaces between the
24567 formal arguments in a function-like macro's formal argument list,
24568 but versions of GCC around March 2002 include spaces after the
24569 commas. */
24570
24571
24572 /* Find the extent of the macro name. The macro name is terminated
24573 by either a space or null character (for an object-like macro) or
24574 an opening paren (for a function-like macro). */
24575 for (p = body; *p; p++)
24576 if (*p == ' ' || *p == '(')
24577 break;
24578
24579 if (*p == ' ' || *p == '\0')
24580 {
24581 /* It's an object-like macro. */
24582 int name_len = p - body;
24583 std::string name (body, name_len);
24584 const char *replacement;
24585
24586 if (*p == ' ')
24587 replacement = body + name_len + 1;
24588 else
24589 {
24590 dwarf2_macro_malformed_definition_complaint (body);
24591 replacement = body + name_len;
24592 }
24593
24594 macro_define_object (file, line, name.c_str (), replacement);
24595 }
24596 else if (*p == '(')
24597 {
24598 /* It's a function-like macro. */
24599 std::string name (body, p - body);
24600 int argc = 0;
24601 int argv_size = 1;
24602 char **argv = XNEWVEC (char *, argv_size);
24603
24604 p++;
24605
24606 p = consume_improper_spaces (p, body);
24607
24608 /* Parse the formal argument list. */
24609 while (*p && *p != ')')
24610 {
24611 /* Find the extent of the current argument name. */
24612 const char *arg_start = p;
24613
24614 while (*p && *p != ',' && *p != ')' && *p != ' ')
24615 p++;
24616
24617 if (! *p || p == arg_start)
24618 dwarf2_macro_malformed_definition_complaint (body);
24619 else
24620 {
24621 /* Make sure argv has room for the new argument. */
24622 if (argc >= argv_size)
24623 {
24624 argv_size *= 2;
24625 argv = XRESIZEVEC (char *, argv, argv_size);
24626 }
24627
24628 argv[argc++] = savestring (arg_start, p - arg_start);
24629 }
24630
24631 p = consume_improper_spaces (p, body);
24632
24633 /* Consume the comma, if present. */
24634 if (*p == ',')
24635 {
24636 p++;
24637
24638 p = consume_improper_spaces (p, body);
24639 }
24640 }
24641
24642 if (*p == ')')
24643 {
24644 p++;
24645
24646 if (*p == ' ')
24647 /* Perfectly formed definition, no complaints. */
24648 macro_define_function (file, line, name.c_str (),
24649 argc, (const char **) argv,
24650 p + 1);
24651 else if (*p == '\0')
24652 {
24653 /* Complain, but do define it. */
24654 dwarf2_macro_malformed_definition_complaint (body);
24655 macro_define_function (file, line, name.c_str (),
24656 argc, (const char **) argv,
24657 p);
24658 }
24659 else
24660 /* Just complain. */
24661 dwarf2_macro_malformed_definition_complaint (body);
24662 }
24663 else
24664 /* Just complain. */
24665 dwarf2_macro_malformed_definition_complaint (body);
24666
24667 {
24668 int i;
24669
24670 for (i = 0; i < argc; i++)
24671 xfree (argv[i]);
24672 }
24673 xfree (argv);
24674 }
24675 else
24676 dwarf2_macro_malformed_definition_complaint (body);
24677 }
24678
24679 /* Skip some bytes from BYTES according to the form given in FORM.
24680 Returns the new pointer. */
24681
24682 static const gdb_byte *
24683 skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
24684 enum dwarf_form form,
24685 unsigned int offset_size,
24686 struct dwarf2_section_info *section)
24687 {
24688 unsigned int bytes_read;
24689
24690 switch (form)
24691 {
24692 case DW_FORM_data1:
24693 case DW_FORM_flag:
24694 ++bytes;
24695 break;
24696
24697 case DW_FORM_data2:
24698 bytes += 2;
24699 break;
24700
24701 case DW_FORM_data4:
24702 bytes += 4;
24703 break;
24704
24705 case DW_FORM_data8:
24706 bytes += 8;
24707 break;
24708
24709 case DW_FORM_data16:
24710 bytes += 16;
24711 break;
24712
24713 case DW_FORM_string:
24714 read_direct_string (abfd, bytes, &bytes_read);
24715 bytes += bytes_read;
24716 break;
24717
24718 case DW_FORM_sec_offset:
24719 case DW_FORM_strp:
24720 case DW_FORM_GNU_strp_alt:
24721 bytes += offset_size;
24722 break;
24723
24724 case DW_FORM_block:
24725 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
24726 bytes += bytes_read;
24727 break;
24728
24729 case DW_FORM_block1:
24730 bytes += 1 + read_1_byte (abfd, bytes);
24731 break;
24732 case DW_FORM_block2:
24733 bytes += 2 + read_2_bytes (abfd, bytes);
24734 break;
24735 case DW_FORM_block4:
24736 bytes += 4 + read_4_bytes (abfd, bytes);
24737 break;
24738
24739 case DW_FORM_addrx:
24740 case DW_FORM_sdata:
24741 case DW_FORM_strx:
24742 case DW_FORM_udata:
24743 case DW_FORM_GNU_addr_index:
24744 case DW_FORM_GNU_str_index:
24745 bytes = gdb_skip_leb128 (bytes, buffer_end);
24746 if (bytes == NULL)
24747 {
24748 dwarf2_section_buffer_overflow_complaint (section);
24749 return NULL;
24750 }
24751 break;
24752
24753 case DW_FORM_implicit_const:
24754 break;
24755
24756 default:
24757 {
24758 complaint (_("invalid form 0x%x in `%s'"),
24759 form, get_section_name (section));
24760 return NULL;
24761 }
24762 }
24763
24764 return bytes;
24765 }
24766
24767 /* A helper for dwarf_decode_macros that handles skipping an unknown
24768 opcode. Returns an updated pointer to the macro data buffer; or,
24769 on error, issues a complaint and returns NULL. */
24770
24771 static const gdb_byte *
24772 skip_unknown_opcode (unsigned int opcode,
24773 const gdb_byte **opcode_definitions,
24774 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
24775 bfd *abfd,
24776 unsigned int offset_size,
24777 struct dwarf2_section_info *section)
24778 {
24779 unsigned int bytes_read, i;
24780 unsigned long arg;
24781 const gdb_byte *defn;
24782
24783 if (opcode_definitions[opcode] == NULL)
24784 {
24785 complaint (_("unrecognized DW_MACFINO opcode 0x%x"),
24786 opcode);
24787 return NULL;
24788 }
24789
24790 defn = opcode_definitions[opcode];
24791 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
24792 defn += bytes_read;
24793
24794 for (i = 0; i < arg; ++i)
24795 {
24796 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
24797 (enum dwarf_form) defn[i], offset_size,
24798 section);
24799 if (mac_ptr == NULL)
24800 {
24801 /* skip_form_bytes already issued the complaint. */
24802 return NULL;
24803 }
24804 }
24805
24806 return mac_ptr;
24807 }
24808
24809 /* A helper function which parses the header of a macro section.
24810 If the macro section is the extended (for now called "GNU") type,
24811 then this updates *OFFSET_SIZE. Returns a pointer to just after
24812 the header, or issues a complaint and returns NULL on error. */
24813
24814 static const gdb_byte *
24815 dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
24816 bfd *abfd,
24817 const gdb_byte *mac_ptr,
24818 unsigned int *offset_size,
24819 int section_is_gnu)
24820 {
24821 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
24822
24823 if (section_is_gnu)
24824 {
24825 unsigned int version, flags;
24826
24827 version = read_2_bytes (abfd, mac_ptr);
24828 if (version != 4 && version != 5)
24829 {
24830 complaint (_("unrecognized version `%d' in .debug_macro section"),
24831 version);
24832 return NULL;
24833 }
24834 mac_ptr += 2;
24835
24836 flags = read_1_byte (abfd, mac_ptr);
24837 ++mac_ptr;
24838 *offset_size = (flags & 1) ? 8 : 4;
24839
24840 if ((flags & 2) != 0)
24841 /* We don't need the line table offset. */
24842 mac_ptr += *offset_size;
24843
24844 /* Vendor opcode descriptions. */
24845 if ((flags & 4) != 0)
24846 {
24847 unsigned int i, count;
24848
24849 count = read_1_byte (abfd, mac_ptr);
24850 ++mac_ptr;
24851 for (i = 0; i < count; ++i)
24852 {
24853 unsigned int opcode, bytes_read;
24854 unsigned long arg;
24855
24856 opcode = read_1_byte (abfd, mac_ptr);
24857 ++mac_ptr;
24858 opcode_definitions[opcode] = mac_ptr;
24859 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24860 mac_ptr += bytes_read;
24861 mac_ptr += arg;
24862 }
24863 }
24864 }
24865
24866 return mac_ptr;
24867 }
24868
24869 /* A helper for dwarf_decode_macros that handles the GNU extensions,
24870 including DW_MACRO_import. */
24871
24872 static void
24873 dwarf_decode_macro_bytes (struct dwarf2_cu *cu,
24874 bfd *abfd,
24875 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
24876 struct macro_source_file *current_file,
24877 struct line_header *lh,
24878 struct dwarf2_section_info *section,
24879 int section_is_gnu, int section_is_dwz,
24880 unsigned int offset_size,
24881 htab_t include_hash)
24882 {
24883 struct dwarf2_per_objfile *dwarf2_per_objfile
24884 = cu->per_cu->dwarf2_per_objfile;
24885 struct objfile *objfile = dwarf2_per_objfile->objfile;
24886 enum dwarf_macro_record_type macinfo_type;
24887 int at_commandline;
24888 const gdb_byte *opcode_definitions[256];
24889
24890 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24891 &offset_size, section_is_gnu);
24892 if (mac_ptr == NULL)
24893 {
24894 /* We already issued a complaint. */
24895 return;
24896 }
24897
24898 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
24899 GDB is still reading the definitions from command line. First
24900 DW_MACINFO_start_file will need to be ignored as it was already executed
24901 to create CURRENT_FILE for the main source holding also the command line
24902 definitions. On first met DW_MACINFO_start_file this flag is reset to
24903 normally execute all the remaining DW_MACINFO_start_file macinfos. */
24904
24905 at_commandline = 1;
24906
24907 do
24908 {
24909 /* Do we at least have room for a macinfo type byte? */
24910 if (mac_ptr >= mac_end)
24911 {
24912 dwarf2_section_buffer_overflow_complaint (section);
24913 break;
24914 }
24915
24916 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
24917 mac_ptr++;
24918
24919 /* Note that we rely on the fact that the corresponding GNU and
24920 DWARF constants are the same. */
24921 DIAGNOSTIC_PUSH
24922 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
24923 switch (macinfo_type)
24924 {
24925 /* A zero macinfo type indicates the end of the macro
24926 information. */
24927 case 0:
24928 break;
24929
24930 case DW_MACRO_define:
24931 case DW_MACRO_undef:
24932 case DW_MACRO_define_strp:
24933 case DW_MACRO_undef_strp:
24934 case DW_MACRO_define_sup:
24935 case DW_MACRO_undef_sup:
24936 {
24937 unsigned int bytes_read;
24938 int line;
24939 const char *body;
24940 int is_define;
24941
24942 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24943 mac_ptr += bytes_read;
24944
24945 if (macinfo_type == DW_MACRO_define
24946 || macinfo_type == DW_MACRO_undef)
24947 {
24948 body = read_direct_string (abfd, mac_ptr, &bytes_read);
24949 mac_ptr += bytes_read;
24950 }
24951 else
24952 {
24953 LONGEST str_offset;
24954
24955 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
24956 mac_ptr += offset_size;
24957
24958 if (macinfo_type == DW_MACRO_define_sup
24959 || macinfo_type == DW_MACRO_undef_sup
24960 || section_is_dwz)
24961 {
24962 struct dwz_file *dwz
24963 = dwarf2_get_dwz_file (dwarf2_per_objfile);
24964
24965 body = read_indirect_string_from_dwz (objfile,
24966 dwz, str_offset);
24967 }
24968 else
24969 body = read_indirect_string_at_offset (dwarf2_per_objfile,
24970 abfd, str_offset);
24971 }
24972
24973 is_define = (macinfo_type == DW_MACRO_define
24974 || macinfo_type == DW_MACRO_define_strp
24975 || macinfo_type == DW_MACRO_define_sup);
24976 if (! current_file)
24977 {
24978 /* DWARF violation as no main source is present. */
24979 complaint (_("debug info with no main source gives macro %s "
24980 "on line %d: %s"),
24981 is_define ? _("definition") : _("undefinition"),
24982 line, body);
24983 break;
24984 }
24985 if ((line == 0 && !at_commandline)
24986 || (line != 0 && at_commandline))
24987 complaint (_("debug info gives %s macro %s with %s line %d: %s"),
24988 at_commandline ? _("command-line") : _("in-file"),
24989 is_define ? _("definition") : _("undefinition"),
24990 line == 0 ? _("zero") : _("non-zero"), line, body);
24991
24992 if (body == NULL)
24993 {
24994 /* Fedora's rpm-build's "debugedit" binary
24995 corrupted .debug_macro sections.
24996
24997 For more info, see
24998 https://bugzilla.redhat.com/show_bug.cgi?id=1708786 */
24999 complaint (_("debug info gives %s invalid macro %s "
25000 "without body (corrupted?) at line %d "
25001 "on file %s"),
25002 at_commandline ? _("command-line") : _("in-file"),
25003 is_define ? _("definition") : _("undefinition"),
25004 line, current_file->filename);
25005 }
25006 else if (is_define)
25007 parse_macro_definition (current_file, line, body);
25008 else
25009 {
25010 gdb_assert (macinfo_type == DW_MACRO_undef
25011 || macinfo_type == DW_MACRO_undef_strp
25012 || macinfo_type == DW_MACRO_undef_sup);
25013 macro_undef (current_file, line, body);
25014 }
25015 }
25016 break;
25017
25018 case DW_MACRO_start_file:
25019 {
25020 unsigned int bytes_read;
25021 int line, file;
25022
25023 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25024 mac_ptr += bytes_read;
25025 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25026 mac_ptr += bytes_read;
25027
25028 if ((line == 0 && !at_commandline)
25029 || (line != 0 && at_commandline))
25030 complaint (_("debug info gives source %d included "
25031 "from %s at %s line %d"),
25032 file, at_commandline ? _("command-line") : _("file"),
25033 line == 0 ? _("zero") : _("non-zero"), line);
25034
25035 if (at_commandline)
25036 {
25037 /* This DW_MACRO_start_file was executed in the
25038 pass one. */
25039 at_commandline = 0;
25040 }
25041 else
25042 current_file = macro_start_file (cu, file, line, current_file,
25043 lh);
25044 }
25045 break;
25046
25047 case DW_MACRO_end_file:
25048 if (! current_file)
25049 complaint (_("macro debug info has an unmatched "
25050 "`close_file' directive"));
25051 else
25052 {
25053 current_file = current_file->included_by;
25054 if (! current_file)
25055 {
25056 enum dwarf_macro_record_type next_type;
25057
25058 /* GCC circa March 2002 doesn't produce the zero
25059 type byte marking the end of the compilation
25060 unit. Complain if it's not there, but exit no
25061 matter what. */
25062
25063 /* Do we at least have room for a macinfo type byte? */
25064 if (mac_ptr >= mac_end)
25065 {
25066 dwarf2_section_buffer_overflow_complaint (section);
25067 return;
25068 }
25069
25070 /* We don't increment mac_ptr here, so this is just
25071 a look-ahead. */
25072 next_type
25073 = (enum dwarf_macro_record_type) read_1_byte (abfd,
25074 mac_ptr);
25075 if (next_type != 0)
25076 complaint (_("no terminating 0-type entry for "
25077 "macros in `.debug_macinfo' section"));
25078
25079 return;
25080 }
25081 }
25082 break;
25083
25084 case DW_MACRO_import:
25085 case DW_MACRO_import_sup:
25086 {
25087 LONGEST offset;
25088 void **slot;
25089 bfd *include_bfd = abfd;
25090 struct dwarf2_section_info *include_section = section;
25091 const gdb_byte *include_mac_end = mac_end;
25092 int is_dwz = section_is_dwz;
25093 const gdb_byte *new_mac_ptr;
25094
25095 offset = read_offset_1 (abfd, mac_ptr, offset_size);
25096 mac_ptr += offset_size;
25097
25098 if (macinfo_type == DW_MACRO_import_sup)
25099 {
25100 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
25101
25102 dwarf2_read_section (objfile, &dwz->macro);
25103
25104 include_section = &dwz->macro;
25105 include_bfd = get_section_bfd_owner (include_section);
25106 include_mac_end = dwz->macro.buffer + dwz->macro.size;
25107 is_dwz = 1;
25108 }
25109
25110 new_mac_ptr = include_section->buffer + offset;
25111 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
25112
25113 if (*slot != NULL)
25114 {
25115 /* This has actually happened; see
25116 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
25117 complaint (_("recursive DW_MACRO_import in "
25118 ".debug_macro section"));
25119 }
25120 else
25121 {
25122 *slot = (void *) new_mac_ptr;
25123
25124 dwarf_decode_macro_bytes (cu, include_bfd, new_mac_ptr,
25125 include_mac_end, current_file, lh,
25126 section, section_is_gnu, is_dwz,
25127 offset_size, include_hash);
25128
25129 htab_remove_elt (include_hash, (void *) new_mac_ptr);
25130 }
25131 }
25132 break;
25133
25134 case DW_MACINFO_vendor_ext:
25135 if (!section_is_gnu)
25136 {
25137 unsigned int bytes_read;
25138
25139 /* This reads the constant, but since we don't recognize
25140 any vendor extensions, we ignore it. */
25141 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25142 mac_ptr += bytes_read;
25143 read_direct_string (abfd, mac_ptr, &bytes_read);
25144 mac_ptr += bytes_read;
25145
25146 /* We don't recognize any vendor extensions. */
25147 break;
25148 }
25149 /* FALLTHROUGH */
25150
25151 default:
25152 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
25153 mac_ptr, mac_end, abfd, offset_size,
25154 section);
25155 if (mac_ptr == NULL)
25156 return;
25157 break;
25158 }
25159 DIAGNOSTIC_POP
25160 } while (macinfo_type != 0);
25161 }
25162
25163 static void
25164 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
25165 int section_is_gnu)
25166 {
25167 struct dwarf2_per_objfile *dwarf2_per_objfile
25168 = cu->per_cu->dwarf2_per_objfile;
25169 struct objfile *objfile = dwarf2_per_objfile->objfile;
25170 struct line_header *lh = cu->line_header;
25171 bfd *abfd;
25172 const gdb_byte *mac_ptr, *mac_end;
25173 struct macro_source_file *current_file = 0;
25174 enum dwarf_macro_record_type macinfo_type;
25175 unsigned int offset_size = cu->header.offset_size;
25176 const gdb_byte *opcode_definitions[256];
25177 void **slot;
25178 struct dwarf2_section_info *section;
25179 const char *section_name;
25180
25181 if (cu->dwo_unit != NULL)
25182 {
25183 if (section_is_gnu)
25184 {
25185 section = &cu->dwo_unit->dwo_file->sections.macro;
25186 section_name = ".debug_macro.dwo";
25187 }
25188 else
25189 {
25190 section = &cu->dwo_unit->dwo_file->sections.macinfo;
25191 section_name = ".debug_macinfo.dwo";
25192 }
25193 }
25194 else
25195 {
25196 if (section_is_gnu)
25197 {
25198 section = &dwarf2_per_objfile->macro;
25199 section_name = ".debug_macro";
25200 }
25201 else
25202 {
25203 section = &dwarf2_per_objfile->macinfo;
25204 section_name = ".debug_macinfo";
25205 }
25206 }
25207
25208 dwarf2_read_section (objfile, section);
25209 if (section->buffer == NULL)
25210 {
25211 complaint (_("missing %s section"), section_name);
25212 return;
25213 }
25214 abfd = get_section_bfd_owner (section);
25215
25216 /* First pass: Find the name of the base filename.
25217 This filename is needed in order to process all macros whose definition
25218 (or undefinition) comes from the command line. These macros are defined
25219 before the first DW_MACINFO_start_file entry, and yet still need to be
25220 associated to the base file.
25221
25222 To determine the base file name, we scan the macro definitions until we
25223 reach the first DW_MACINFO_start_file entry. We then initialize
25224 CURRENT_FILE accordingly so that any macro definition found before the
25225 first DW_MACINFO_start_file can still be associated to the base file. */
25226
25227 mac_ptr = section->buffer + offset;
25228 mac_end = section->buffer + section->size;
25229
25230 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
25231 &offset_size, section_is_gnu);
25232 if (mac_ptr == NULL)
25233 {
25234 /* We already issued a complaint. */
25235 return;
25236 }
25237
25238 do
25239 {
25240 /* Do we at least have room for a macinfo type byte? */
25241 if (mac_ptr >= mac_end)
25242 {
25243 /* Complaint is printed during the second pass as GDB will probably
25244 stop the first pass earlier upon finding
25245 DW_MACINFO_start_file. */
25246 break;
25247 }
25248
25249 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
25250 mac_ptr++;
25251
25252 /* Note that we rely on the fact that the corresponding GNU and
25253 DWARF constants are the same. */
25254 DIAGNOSTIC_PUSH
25255 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
25256 switch (macinfo_type)
25257 {
25258 /* A zero macinfo type indicates the end of the macro
25259 information. */
25260 case 0:
25261 break;
25262
25263 case DW_MACRO_define:
25264 case DW_MACRO_undef:
25265 /* Only skip the data by MAC_PTR. */
25266 {
25267 unsigned int bytes_read;
25268
25269 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25270 mac_ptr += bytes_read;
25271 read_direct_string (abfd, mac_ptr, &bytes_read);
25272 mac_ptr += bytes_read;
25273 }
25274 break;
25275
25276 case DW_MACRO_start_file:
25277 {
25278 unsigned int bytes_read;
25279 int line, file;
25280
25281 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25282 mac_ptr += bytes_read;
25283 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25284 mac_ptr += bytes_read;
25285
25286 current_file = macro_start_file (cu, file, line, current_file, lh);
25287 }
25288 break;
25289
25290 case DW_MACRO_end_file:
25291 /* No data to skip by MAC_PTR. */
25292 break;
25293
25294 case DW_MACRO_define_strp:
25295 case DW_MACRO_undef_strp:
25296 case DW_MACRO_define_sup:
25297 case DW_MACRO_undef_sup:
25298 {
25299 unsigned int bytes_read;
25300
25301 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25302 mac_ptr += bytes_read;
25303 mac_ptr += offset_size;
25304 }
25305 break;
25306
25307 case DW_MACRO_import:
25308 case DW_MACRO_import_sup:
25309 /* Note that, according to the spec, a transparent include
25310 chain cannot call DW_MACRO_start_file. So, we can just
25311 skip this opcode. */
25312 mac_ptr += offset_size;
25313 break;
25314
25315 case DW_MACINFO_vendor_ext:
25316 /* Only skip the data by MAC_PTR. */
25317 if (!section_is_gnu)
25318 {
25319 unsigned int bytes_read;
25320
25321 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25322 mac_ptr += bytes_read;
25323 read_direct_string (abfd, mac_ptr, &bytes_read);
25324 mac_ptr += bytes_read;
25325 }
25326 /* FALLTHROUGH */
25327
25328 default:
25329 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
25330 mac_ptr, mac_end, abfd, offset_size,
25331 section);
25332 if (mac_ptr == NULL)
25333 return;
25334 break;
25335 }
25336 DIAGNOSTIC_POP
25337 } while (macinfo_type != 0 && current_file == NULL);
25338
25339 /* Second pass: Process all entries.
25340
25341 Use the AT_COMMAND_LINE flag to determine whether we are still processing
25342 command-line macro definitions/undefinitions. This flag is unset when we
25343 reach the first DW_MACINFO_start_file entry. */
25344
25345 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
25346 htab_eq_pointer,
25347 NULL, xcalloc, xfree));
25348 mac_ptr = section->buffer + offset;
25349 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
25350 *slot = (void *) mac_ptr;
25351 dwarf_decode_macro_bytes (cu, abfd, mac_ptr, mac_end,
25352 current_file, lh, section,
25353 section_is_gnu, 0, offset_size,
25354 include_hash.get ());
25355 }
25356
25357 /* Check if the attribute's form is a DW_FORM_block*
25358 if so return true else false. */
25359
25360 static int
25361 attr_form_is_block (const struct attribute *attr)
25362 {
25363 return (attr == NULL ? 0 :
25364 attr->form == DW_FORM_block1
25365 || attr->form == DW_FORM_block2
25366 || attr->form == DW_FORM_block4
25367 || attr->form == DW_FORM_block
25368 || attr->form == DW_FORM_exprloc);
25369 }
25370
25371 /* Return non-zero if ATTR's value is a section offset --- classes
25372 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
25373 You may use DW_UNSND (attr) to retrieve such offsets.
25374
25375 Section 7.5.4, "Attribute Encodings", explains that no attribute
25376 may have a value that belongs to more than one of these classes; it
25377 would be ambiguous if we did, because we use the same forms for all
25378 of them. */
25379
25380 static int
25381 attr_form_is_section_offset (const struct attribute *attr)
25382 {
25383 return (attr->form == DW_FORM_data4
25384 || attr->form == DW_FORM_data8
25385 || attr->form == DW_FORM_sec_offset);
25386 }
25387
25388 /* Return non-zero if ATTR's value falls in the 'constant' class, or
25389 zero otherwise. When this function returns true, you can apply
25390 dwarf2_get_attr_constant_value to it.
25391
25392 However, note that for some attributes you must check
25393 attr_form_is_section_offset before using this test. DW_FORM_data4
25394 and DW_FORM_data8 are members of both the constant class, and of
25395 the classes that contain offsets into other debug sections
25396 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
25397 that, if an attribute's can be either a constant or one of the
25398 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
25399 taken as section offsets, not constants.
25400
25401 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
25402 cannot handle that. */
25403
25404 static int
25405 attr_form_is_constant (const struct attribute *attr)
25406 {
25407 switch (attr->form)
25408 {
25409 case DW_FORM_sdata:
25410 case DW_FORM_udata:
25411 case DW_FORM_data1:
25412 case DW_FORM_data2:
25413 case DW_FORM_data4:
25414 case DW_FORM_data8:
25415 case DW_FORM_implicit_const:
25416 return 1;
25417 default:
25418 return 0;
25419 }
25420 }
25421
25422
25423 /* DW_ADDR is always stored already as sect_offset; despite for the forms
25424 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
25425
25426 static int
25427 attr_form_is_ref (const struct attribute *attr)
25428 {
25429 switch (attr->form)
25430 {
25431 case DW_FORM_ref_addr:
25432 case DW_FORM_ref1:
25433 case DW_FORM_ref2:
25434 case DW_FORM_ref4:
25435 case DW_FORM_ref8:
25436 case DW_FORM_ref_udata:
25437 case DW_FORM_GNU_ref_alt:
25438 return 1;
25439 default:
25440 return 0;
25441 }
25442 }
25443
25444 /* Return the .debug_loc section to use for CU.
25445 For DWO files use .debug_loc.dwo. */
25446
25447 static struct dwarf2_section_info *
25448 cu_debug_loc_section (struct dwarf2_cu *cu)
25449 {
25450 struct dwarf2_per_objfile *dwarf2_per_objfile
25451 = cu->per_cu->dwarf2_per_objfile;
25452
25453 if (cu->dwo_unit)
25454 {
25455 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
25456
25457 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
25458 }
25459 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
25460 : &dwarf2_per_objfile->loc);
25461 }
25462
25463 /* A helper function that fills in a dwarf2_loclist_baton. */
25464
25465 static void
25466 fill_in_loclist_baton (struct dwarf2_cu *cu,
25467 struct dwarf2_loclist_baton *baton,
25468 const struct attribute *attr)
25469 {
25470 struct dwarf2_per_objfile *dwarf2_per_objfile
25471 = cu->per_cu->dwarf2_per_objfile;
25472 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
25473
25474 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
25475
25476 baton->per_cu = cu->per_cu;
25477 gdb_assert (baton->per_cu);
25478 /* We don't know how long the location list is, but make sure we
25479 don't run off the edge of the section. */
25480 baton->size = section->size - DW_UNSND (attr);
25481 baton->data = section->buffer + DW_UNSND (attr);
25482 baton->base_address = cu->base_address;
25483 baton->from_dwo = cu->dwo_unit != NULL;
25484 }
25485
25486 static void
25487 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
25488 struct dwarf2_cu *cu, int is_block)
25489 {
25490 struct dwarf2_per_objfile *dwarf2_per_objfile
25491 = cu->per_cu->dwarf2_per_objfile;
25492 struct objfile *objfile = dwarf2_per_objfile->objfile;
25493 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
25494
25495 if (attr_form_is_section_offset (attr)
25496 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
25497 the section. If so, fall through to the complaint in the
25498 other branch. */
25499 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
25500 {
25501 struct dwarf2_loclist_baton *baton;
25502
25503 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
25504
25505 fill_in_loclist_baton (cu, baton, attr);
25506
25507 if (cu->base_known == 0)
25508 complaint (_("Location list used without "
25509 "specifying the CU base address."));
25510
25511 SYMBOL_ACLASS_INDEX (sym) = (is_block
25512 ? dwarf2_loclist_block_index
25513 : dwarf2_loclist_index);
25514 SYMBOL_LOCATION_BATON (sym) = baton;
25515 }
25516 else
25517 {
25518 struct dwarf2_locexpr_baton *baton;
25519
25520 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
25521 baton->per_cu = cu->per_cu;
25522 gdb_assert (baton->per_cu);
25523
25524 if (attr_form_is_block (attr))
25525 {
25526 /* Note that we're just copying the block's data pointer
25527 here, not the actual data. We're still pointing into the
25528 info_buffer for SYM's objfile; right now we never release
25529 that buffer, but when we do clean up properly this may
25530 need to change. */
25531 baton->size = DW_BLOCK (attr)->size;
25532 baton->data = DW_BLOCK (attr)->data;
25533 }
25534 else
25535 {
25536 dwarf2_invalid_attrib_class_complaint ("location description",
25537 sym->natural_name ());
25538 baton->size = 0;
25539 }
25540
25541 SYMBOL_ACLASS_INDEX (sym) = (is_block
25542 ? dwarf2_locexpr_block_index
25543 : dwarf2_locexpr_index);
25544 SYMBOL_LOCATION_BATON (sym) = baton;
25545 }
25546 }
25547
25548 /* Return the OBJFILE associated with the compilation unit CU. If CU
25549 came from a separate debuginfo file, then the master objfile is
25550 returned. */
25551
25552 struct objfile *
25553 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
25554 {
25555 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
25556
25557 /* Return the master objfile, so that we can report and look up the
25558 correct file containing this variable. */
25559 if (objfile->separate_debug_objfile_backlink)
25560 objfile = objfile->separate_debug_objfile_backlink;
25561
25562 return objfile;
25563 }
25564
25565 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
25566 (CU_HEADERP is unused in such case) or prepare a temporary copy at
25567 CU_HEADERP first. */
25568
25569 static const struct comp_unit_head *
25570 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
25571 struct dwarf2_per_cu_data *per_cu)
25572 {
25573 const gdb_byte *info_ptr;
25574
25575 if (per_cu->cu)
25576 return &per_cu->cu->header;
25577
25578 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
25579
25580 memset (cu_headerp, 0, sizeof (*cu_headerp));
25581 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
25582 rcuh_kind::COMPILE);
25583
25584 return cu_headerp;
25585 }
25586
25587 /* Return the address size given in the compilation unit header for CU. */
25588
25589 int
25590 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
25591 {
25592 struct comp_unit_head cu_header_local;
25593 const struct comp_unit_head *cu_headerp;
25594
25595 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25596
25597 return cu_headerp->addr_size;
25598 }
25599
25600 /* Return the offset size given in the compilation unit header for CU. */
25601
25602 int
25603 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
25604 {
25605 struct comp_unit_head cu_header_local;
25606 const struct comp_unit_head *cu_headerp;
25607
25608 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25609
25610 return cu_headerp->offset_size;
25611 }
25612
25613 /* See its dwarf2loc.h declaration. */
25614
25615 int
25616 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
25617 {
25618 struct comp_unit_head cu_header_local;
25619 const struct comp_unit_head *cu_headerp;
25620
25621 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25622
25623 if (cu_headerp->version == 2)
25624 return cu_headerp->addr_size;
25625 else
25626 return cu_headerp->offset_size;
25627 }
25628
25629 /* Return the text offset of the CU. The returned offset comes from
25630 this CU's objfile. If this objfile came from a separate debuginfo
25631 file, then the offset may be different from the corresponding
25632 offset in the parent objfile. */
25633
25634 CORE_ADDR
25635 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
25636 {
25637 return per_cu->dwarf2_per_objfile->objfile->text_section_offset ();
25638 }
25639
25640 /* Return a type that is a generic pointer type, the size of which matches
25641 the address size given in the compilation unit header for PER_CU. */
25642 static struct type *
25643 dwarf2_per_cu_addr_type (struct dwarf2_per_cu_data *per_cu)
25644 {
25645 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
25646 struct type *void_type = objfile_type (objfile)->builtin_void;
25647 struct type *addr_type = lookup_pointer_type (void_type);
25648 int addr_size = dwarf2_per_cu_addr_size (per_cu);
25649
25650 if (TYPE_LENGTH (addr_type) == addr_size)
25651 return addr_type;
25652
25653 addr_type
25654 = dwarf2_per_cu_addr_sized_int_type (per_cu, TYPE_UNSIGNED (addr_type));
25655 return addr_type;
25656 }
25657
25658 /* Return DWARF version number of PER_CU. */
25659
25660 short
25661 dwarf2_version (struct dwarf2_per_cu_data *per_cu)
25662 {
25663 return per_cu->dwarf_version;
25664 }
25665
25666 /* Locate the .debug_info compilation unit from CU's objfile which contains
25667 the DIE at OFFSET. Raises an error on failure. */
25668
25669 static struct dwarf2_per_cu_data *
25670 dwarf2_find_containing_comp_unit (sect_offset sect_off,
25671 unsigned int offset_in_dwz,
25672 struct dwarf2_per_objfile *dwarf2_per_objfile)
25673 {
25674 struct dwarf2_per_cu_data *this_cu;
25675 int low, high;
25676
25677 low = 0;
25678 high = dwarf2_per_objfile->all_comp_units.size () - 1;
25679 while (high > low)
25680 {
25681 struct dwarf2_per_cu_data *mid_cu;
25682 int mid = low + (high - low) / 2;
25683
25684 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
25685 if (mid_cu->is_dwz > offset_in_dwz
25686 || (mid_cu->is_dwz == offset_in_dwz
25687 && mid_cu->sect_off + mid_cu->length >= sect_off))
25688 high = mid;
25689 else
25690 low = mid + 1;
25691 }
25692 gdb_assert (low == high);
25693 this_cu = dwarf2_per_objfile->all_comp_units[low];
25694 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
25695 {
25696 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
25697 error (_("Dwarf Error: could not find partial DIE containing "
25698 "offset %s [in module %s]"),
25699 sect_offset_str (sect_off),
25700 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
25701
25702 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
25703 <= sect_off);
25704 return dwarf2_per_objfile->all_comp_units[low-1];
25705 }
25706 else
25707 {
25708 if (low == dwarf2_per_objfile->all_comp_units.size () - 1
25709 && sect_off >= this_cu->sect_off + this_cu->length)
25710 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
25711 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
25712 return this_cu;
25713 }
25714 }
25715
25716 /* Initialize dwarf2_cu CU, owned by PER_CU. */
25717
25718 dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
25719 : per_cu (per_cu_),
25720 mark (false),
25721 has_loclist (false),
25722 checked_producer (false),
25723 producer_is_gxx_lt_4_6 (false),
25724 producer_is_gcc_lt_4_3 (false),
25725 producer_is_icc (false),
25726 producer_is_icc_lt_14 (false),
25727 producer_is_codewarrior (false),
25728 processing_has_namespace_info (false)
25729 {
25730 per_cu->cu = this;
25731 }
25732
25733 /* Destroy a dwarf2_cu. */
25734
25735 dwarf2_cu::~dwarf2_cu ()
25736 {
25737 per_cu->cu = NULL;
25738 }
25739
25740 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
25741
25742 static void
25743 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
25744 enum language pretend_language)
25745 {
25746 struct attribute *attr;
25747
25748 /* Set the language we're debugging. */
25749 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
25750 if (attr != nullptr)
25751 set_cu_language (DW_UNSND (attr), cu);
25752 else
25753 {
25754 cu->language = pretend_language;
25755 cu->language_defn = language_def (cu->language);
25756 }
25757
25758 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
25759 }
25760
25761 /* Increase the age counter on each cached compilation unit, and free
25762 any that are too old. */
25763
25764 static void
25765 age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
25766 {
25767 struct dwarf2_per_cu_data *per_cu, **last_chain;
25768
25769 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
25770 per_cu = dwarf2_per_objfile->read_in_chain;
25771 while (per_cu != NULL)
25772 {
25773 per_cu->cu->last_used ++;
25774 if (per_cu->cu->last_used <= dwarf_max_cache_age)
25775 dwarf2_mark (per_cu->cu);
25776 per_cu = per_cu->cu->read_in_chain;
25777 }
25778
25779 per_cu = dwarf2_per_objfile->read_in_chain;
25780 last_chain = &dwarf2_per_objfile->read_in_chain;
25781 while (per_cu != NULL)
25782 {
25783 struct dwarf2_per_cu_data *next_cu;
25784
25785 next_cu = per_cu->cu->read_in_chain;
25786
25787 if (!per_cu->cu->mark)
25788 {
25789 delete per_cu->cu;
25790 *last_chain = next_cu;
25791 }
25792 else
25793 last_chain = &per_cu->cu->read_in_chain;
25794
25795 per_cu = next_cu;
25796 }
25797 }
25798
25799 /* Remove a single compilation unit from the cache. */
25800
25801 static void
25802 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
25803 {
25804 struct dwarf2_per_cu_data *per_cu, **last_chain;
25805 struct dwarf2_per_objfile *dwarf2_per_objfile
25806 = target_per_cu->dwarf2_per_objfile;
25807
25808 per_cu = dwarf2_per_objfile->read_in_chain;
25809 last_chain = &dwarf2_per_objfile->read_in_chain;
25810 while (per_cu != NULL)
25811 {
25812 struct dwarf2_per_cu_data *next_cu;
25813
25814 next_cu = per_cu->cu->read_in_chain;
25815
25816 if (per_cu == target_per_cu)
25817 {
25818 delete per_cu->cu;
25819 per_cu->cu = NULL;
25820 *last_chain = next_cu;
25821 break;
25822 }
25823 else
25824 last_chain = &per_cu->cu->read_in_chain;
25825
25826 per_cu = next_cu;
25827 }
25828 }
25829
25830 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25831 We store these in a hash table separate from the DIEs, and preserve them
25832 when the DIEs are flushed out of cache.
25833
25834 The CU "per_cu" pointer is needed because offset alone is not enough to
25835 uniquely identify the type. A file may have multiple .debug_types sections,
25836 or the type may come from a DWO file. Furthermore, while it's more logical
25837 to use per_cu->section+offset, with Fission the section with the data is in
25838 the DWO file but we don't know that section at the point we need it.
25839 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25840 because we can enter the lookup routine, get_die_type_at_offset, from
25841 outside this file, and thus won't necessarily have PER_CU->cu.
25842 Fortunately, PER_CU is stable for the life of the objfile. */
25843
25844 struct dwarf2_per_cu_offset_and_type
25845 {
25846 const struct dwarf2_per_cu_data *per_cu;
25847 sect_offset sect_off;
25848 struct type *type;
25849 };
25850
25851 /* Hash function for a dwarf2_per_cu_offset_and_type. */
25852
25853 static hashval_t
25854 per_cu_offset_and_type_hash (const void *item)
25855 {
25856 const struct dwarf2_per_cu_offset_and_type *ofs
25857 = (const struct dwarf2_per_cu_offset_and_type *) item;
25858
25859 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
25860 }
25861
25862 /* Equality function for a dwarf2_per_cu_offset_and_type. */
25863
25864 static int
25865 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
25866 {
25867 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25868 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25869 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25870 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
25871
25872 return (ofs_lhs->per_cu == ofs_rhs->per_cu
25873 && ofs_lhs->sect_off == ofs_rhs->sect_off);
25874 }
25875
25876 /* Set the type associated with DIE to TYPE. Save it in CU's hash
25877 table if necessary. For convenience, return TYPE.
25878
25879 The DIEs reading must have careful ordering to:
25880 * Not cause infinite loops trying to read in DIEs as a prerequisite for
25881 reading current DIE.
25882 * Not trying to dereference contents of still incompletely read in types
25883 while reading in other DIEs.
25884 * Enable referencing still incompletely read in types just by a pointer to
25885 the type without accessing its fields.
25886
25887 Therefore caller should follow these rules:
25888 * Try to fetch any prerequisite types we may need to build this DIE type
25889 before building the type and calling set_die_type.
25890 * After building type call set_die_type for current DIE as soon as
25891 possible before fetching more types to complete the current type.
25892 * Make the type as complete as possible before fetching more types. */
25893
25894 static struct type *
25895 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
25896 {
25897 struct dwarf2_per_objfile *dwarf2_per_objfile
25898 = cu->per_cu->dwarf2_per_objfile;
25899 struct dwarf2_per_cu_offset_and_type **slot, ofs;
25900 struct objfile *objfile = dwarf2_per_objfile->objfile;
25901 struct attribute *attr;
25902 struct dynamic_prop prop;
25903
25904 /* For Ada types, make sure that the gnat-specific data is always
25905 initialized (if not already set). There are a few types where
25906 we should not be doing so, because the type-specific area is
25907 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25908 where the type-specific area is used to store the floatformat).
25909 But this is not a problem, because the gnat-specific information
25910 is actually not needed for these types. */
25911 if (need_gnat_info (cu)
25912 && TYPE_CODE (type) != TYPE_CODE_FUNC
25913 && TYPE_CODE (type) != TYPE_CODE_FLT
25914 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
25915 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
25916 && TYPE_CODE (type) != TYPE_CODE_METHOD
25917 && !HAVE_GNAT_AUX_INFO (type))
25918 INIT_GNAT_SPECIFIC (type);
25919
25920 /* Read DW_AT_allocated and set in type. */
25921 attr = dwarf2_attr (die, DW_AT_allocated, cu);
25922 if (attr_form_is_block (attr))
25923 {
25924 struct type *prop_type
25925 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
25926 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
25927 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
25928 }
25929 else if (attr != NULL)
25930 {
25931 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
25932 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25933 sect_offset_str (die->sect_off));
25934 }
25935
25936 /* Read DW_AT_associated and set in type. */
25937 attr = dwarf2_attr (die, DW_AT_associated, cu);
25938 if (attr_form_is_block (attr))
25939 {
25940 struct type *prop_type
25941 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
25942 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
25943 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
25944 }
25945 else if (attr != NULL)
25946 {
25947 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
25948 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25949 sect_offset_str (die->sect_off));
25950 }
25951
25952 /* Read DW_AT_data_location and set in type. */
25953 attr = dwarf2_attr (die, DW_AT_data_location, cu);
25954 if (attr_to_dynamic_prop (attr, die, cu, &prop,
25955 dwarf2_per_cu_addr_type (cu->per_cu)))
25956 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
25957
25958 if (dwarf2_per_objfile->die_type_hash == NULL)
25959 {
25960 dwarf2_per_objfile->die_type_hash =
25961 htab_create_alloc_ex (127,
25962 per_cu_offset_and_type_hash,
25963 per_cu_offset_and_type_eq,
25964 NULL,
25965 &objfile->objfile_obstack,
25966 hashtab_obstack_allocate,
25967 dummy_obstack_deallocate);
25968 }
25969
25970 ofs.per_cu = cu->per_cu;
25971 ofs.sect_off = die->sect_off;
25972 ofs.type = type;
25973 slot = (struct dwarf2_per_cu_offset_and_type **)
25974 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
25975 if (*slot)
25976 complaint (_("A problem internal to GDB: DIE %s has type already set"),
25977 sect_offset_str (die->sect_off));
25978 *slot = XOBNEW (&objfile->objfile_obstack,
25979 struct dwarf2_per_cu_offset_and_type);
25980 **slot = ofs;
25981 return type;
25982 }
25983
25984 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
25985 or return NULL if the die does not have a saved type. */
25986
25987 static struct type *
25988 get_die_type_at_offset (sect_offset sect_off,
25989 struct dwarf2_per_cu_data *per_cu)
25990 {
25991 struct dwarf2_per_cu_offset_and_type *slot, ofs;
25992 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
25993
25994 if (dwarf2_per_objfile->die_type_hash == NULL)
25995 return NULL;
25996
25997 ofs.per_cu = per_cu;
25998 ofs.sect_off = sect_off;
25999 slot = ((struct dwarf2_per_cu_offset_and_type *)
26000 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
26001 if (slot)
26002 return slot->type;
26003 else
26004 return NULL;
26005 }
26006
26007 /* Look up the type for DIE in CU in die_type_hash,
26008 or return NULL if DIE does not have a saved type. */
26009
26010 static struct type *
26011 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
26012 {
26013 return get_die_type_at_offset (die->sect_off, cu->per_cu);
26014 }
26015
26016 /* Add a dependence relationship from CU to REF_PER_CU. */
26017
26018 static void
26019 dwarf2_add_dependence (struct dwarf2_cu *cu,
26020 struct dwarf2_per_cu_data *ref_per_cu)
26021 {
26022 void **slot;
26023
26024 if (cu->dependencies == NULL)
26025 cu->dependencies
26026 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
26027 NULL, &cu->comp_unit_obstack,
26028 hashtab_obstack_allocate,
26029 dummy_obstack_deallocate);
26030
26031 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
26032 if (*slot == NULL)
26033 *slot = ref_per_cu;
26034 }
26035
26036 /* Subroutine of dwarf2_mark to pass to htab_traverse.
26037 Set the mark field in every compilation unit in the
26038 cache that we must keep because we are keeping CU. */
26039
26040 static int
26041 dwarf2_mark_helper (void **slot, void *data)
26042 {
26043 struct dwarf2_per_cu_data *per_cu;
26044
26045 per_cu = (struct dwarf2_per_cu_data *) *slot;
26046
26047 /* cu->dependencies references may not yet have been ever read if QUIT aborts
26048 reading of the chain. As such dependencies remain valid it is not much
26049 useful to track and undo them during QUIT cleanups. */
26050 if (per_cu->cu == NULL)
26051 return 1;
26052
26053 if (per_cu->cu->mark)
26054 return 1;
26055 per_cu->cu->mark = true;
26056
26057 if (per_cu->cu->dependencies != NULL)
26058 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
26059
26060 return 1;
26061 }
26062
26063 /* Set the mark field in CU and in every other compilation unit in the
26064 cache that we must keep because we are keeping CU. */
26065
26066 static void
26067 dwarf2_mark (struct dwarf2_cu *cu)
26068 {
26069 if (cu->mark)
26070 return;
26071 cu->mark = true;
26072 if (cu->dependencies != NULL)
26073 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
26074 }
26075
26076 static void
26077 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
26078 {
26079 while (per_cu)
26080 {
26081 per_cu->cu->mark = false;
26082 per_cu = per_cu->cu->read_in_chain;
26083 }
26084 }
26085
26086 /* Trivial hash function for partial_die_info: the hash value of a DIE
26087 is its offset in .debug_info for this objfile. */
26088
26089 static hashval_t
26090 partial_die_hash (const void *item)
26091 {
26092 const struct partial_die_info *part_die
26093 = (const struct partial_die_info *) item;
26094
26095 return to_underlying (part_die->sect_off);
26096 }
26097
26098 /* Trivial comparison function for partial_die_info structures: two DIEs
26099 are equal if they have the same offset. */
26100
26101 static int
26102 partial_die_eq (const void *item_lhs, const void *item_rhs)
26103 {
26104 const struct partial_die_info *part_die_lhs
26105 = (const struct partial_die_info *) item_lhs;
26106 const struct partial_die_info *part_die_rhs
26107 = (const struct partial_die_info *) item_rhs;
26108
26109 return part_die_lhs->sect_off == part_die_rhs->sect_off;
26110 }
26111
26112 struct cmd_list_element *set_dwarf_cmdlist;
26113 struct cmd_list_element *show_dwarf_cmdlist;
26114
26115 static void
26116 set_dwarf_cmd (const char *args, int from_tty)
26117 {
26118 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
26119 gdb_stdout);
26120 }
26121
26122 static void
26123 show_dwarf_cmd (const char *args, int from_tty)
26124 {
26125 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
26126 }
26127
26128 bool dwarf_always_disassemble;
26129
26130 static void
26131 show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
26132 struct cmd_list_element *c, const char *value)
26133 {
26134 fprintf_filtered (file,
26135 _("Whether to always disassemble "
26136 "DWARF expressions is %s.\n"),
26137 value);
26138 }
26139
26140 static void
26141 show_check_physname (struct ui_file *file, int from_tty,
26142 struct cmd_list_element *c, const char *value)
26143 {
26144 fprintf_filtered (file,
26145 _("Whether to check \"physname\" is %s.\n"),
26146 value);
26147 }
26148
26149 void _initialize_dwarf2_read ();
26150 void
26151 _initialize_dwarf2_read ()
26152 {
26153 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
26154 Set DWARF specific variables.\n\
26155 Configure DWARF variables such as the cache size."),
26156 &set_dwarf_cmdlist, "maintenance set dwarf ",
26157 0/*allow-unknown*/, &maintenance_set_cmdlist);
26158
26159 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
26160 Show DWARF specific variables.\n\
26161 Show DWARF variables such as the cache size."),
26162 &show_dwarf_cmdlist, "maintenance show dwarf ",
26163 0/*allow-unknown*/, &maintenance_show_cmdlist);
26164
26165 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
26166 &dwarf_max_cache_age, _("\
26167 Set the upper bound on the age of cached DWARF compilation units."), _("\
26168 Show the upper bound on the age of cached DWARF compilation units."), _("\
26169 A higher limit means that cached compilation units will be stored\n\
26170 in memory longer, and more total memory will be used. Zero disables\n\
26171 caching, which can slow down startup."),
26172 NULL,
26173 show_dwarf_max_cache_age,
26174 &set_dwarf_cmdlist,
26175 &show_dwarf_cmdlist);
26176
26177 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
26178 &dwarf_always_disassemble, _("\
26179 Set whether `info address' always disassembles DWARF expressions."), _("\
26180 Show whether `info address' always disassembles DWARF expressions."), _("\
26181 When enabled, DWARF expressions are always printed in an assembly-like\n\
26182 syntax. When disabled, expressions will be printed in a more\n\
26183 conversational style, when possible."),
26184 NULL,
26185 show_dwarf_always_disassemble,
26186 &set_dwarf_cmdlist,
26187 &show_dwarf_cmdlist);
26188
26189 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
26190 Set debugging of the DWARF reader."), _("\
26191 Show debugging of the DWARF reader."), _("\
26192 When enabled (non-zero), debugging messages are printed during DWARF\n\
26193 reading and symtab expansion. A value of 1 (one) provides basic\n\
26194 information. A value greater than 1 provides more verbose information."),
26195 NULL,
26196 NULL,
26197 &setdebuglist, &showdebuglist);
26198
26199 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
26200 Set debugging of the DWARF DIE reader."), _("\
26201 Show debugging of the DWARF DIE reader."), _("\
26202 When enabled (non-zero), DIEs are dumped after they are read in.\n\
26203 The value is the maximum depth to print."),
26204 NULL,
26205 NULL,
26206 &setdebuglist, &showdebuglist);
26207
26208 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
26209 Set debugging of the dwarf line reader."), _("\
26210 Show debugging of the dwarf line reader."), _("\
26211 When enabled (non-zero), line number entries are dumped as they are read in.\n\
26212 A value of 1 (one) provides basic information.\n\
26213 A value greater than 1 provides more verbose information."),
26214 NULL,
26215 NULL,
26216 &setdebuglist, &showdebuglist);
26217
26218 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
26219 Set cross-checking of \"physname\" code against demangler."), _("\
26220 Show cross-checking of \"physname\" code against demangler."), _("\
26221 When enabled, GDB's internal \"physname\" code is checked against\n\
26222 the demangler."),
26223 NULL, show_check_physname,
26224 &setdebuglist, &showdebuglist);
26225
26226 add_setshow_boolean_cmd ("use-deprecated-index-sections",
26227 no_class, &use_deprecated_index_sections, _("\
26228 Set whether to use deprecated gdb_index sections."), _("\
26229 Show whether to use deprecated gdb_index sections."), _("\
26230 When enabled, deprecated .gdb_index sections are used anyway.\n\
26231 Normally they are ignored either because of a missing feature or\n\
26232 performance issue.\n\
26233 Warning: This option must be enabled before gdb reads the file."),
26234 NULL,
26235 NULL,
26236 &setlist, &showlist);
26237
26238 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
26239 &dwarf2_locexpr_funcs);
26240 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
26241 &dwarf2_loclist_funcs);
26242
26243 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
26244 &dwarf2_block_frame_base_locexpr_funcs);
26245 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
26246 &dwarf2_block_frame_base_loclist_funcs);
26247
26248 #if GDB_SELF_TEST
26249 selftests::register_test ("dw2_expand_symtabs_matching",
26250 selftests::dw2_expand_symtabs_matching::run_test);
26251 #endif
26252 }
This page took 0.661508 seconds and 4 git commands to generate.