Merge branch 'master' into merge-job
[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 Copyright (C) 2019-2020 Advanced Micro Devices, Inc. All rights reserved.
5
6 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
7 Inc. with support from Florida State University (under contract
8 with the Ada Joint Program Office), and Silicon Graphics, Inc.
9 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
10 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
11 support.
12
13 This file is part of GDB.
14
15 This program is free software; you can redistribute it and/or modify
16 it under the terms of the GNU General Public License as published by
17 the Free Software Foundation; either version 3 of the License, or
18 (at your option) any later version.
19
20 This program is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
24
25 You should have received a copy of the GNU General Public License
26 along with this program. If not, see <http://www.gnu.org/licenses/>. */
27
28 /* FIXME: Various die-reading functions need to be more careful with
29 reading off the end of the section.
30 E.g., load_partial_dies, read_partial_die. */
31
32 #include "defs.h"
33 #include "dwarf2read.h"
34 #include "dwarf-index-cache.h"
35 #include "dwarf-index-common.h"
36 #include "bfd.h"
37 #include "elf-bfd.h"
38 #include "symtab.h"
39 #include "gdbtypes.h"
40 #include "objfiles.h"
41 #include "dwarf2.h"
42 #include "buildsym.h"
43 #include "demangle.h"
44 #include "gdb-demangle.h"
45 #include "filenames.h" /* for DOSish file names */
46 #include "macrotab.h"
47 #include "language.h"
48 #include "complaints.h"
49 #include "dwarf2expr.h"
50 #include "dwarf2loc.h"
51 #include "cp-support.h"
52 #include "hashtab.h"
53 #include "command.h"
54 #include "gdbcmd.h"
55 #include "block.h"
56 #include "addrmap.h"
57 #include "typeprint.h"
58 #include "psympriv.h"
59 #include "c-lang.h"
60 #include "go-lang.h"
61 #include "valprint.h"
62 #include "gdbcore.h" /* for gnutarget */
63 #include "gdb/gdb-index.h"
64 #include "gdb_bfd.h"
65 #include "f-lang.h"
66 #include "source.h"
67 #include "build-id.h"
68 #include "namespace.h"
69 #include "gdbsupport/function-view.h"
70 #include "gdbsupport/gdb_optional.h"
71 #include "gdbsupport/underlying.h"
72 #include "gdbsupport/hash_enum.h"
73 #include "filename-seen-cache.h"
74 #include "producer.h"
75 #include <fcntl.h>
76 #include <algorithm>
77 #include <unordered_map>
78 #include "gdbsupport/selftest.h"
79 #include "rust-lang.h"
80 #include "gdbsupport/pathstuff.h"
81
82 /* When == 1, print basic high level tracing messages.
83 When > 1, be more verbose.
84 This is in contrast to the low level DIE reading of dwarf_die_debug. */
85 static unsigned int dwarf_read_debug = 0;
86
87 /* When non-zero, dump DIEs after they are read in. */
88 static unsigned int dwarf_die_debug = 0;
89
90 /* When non-zero, dump line number entries as they are read in. */
91 static unsigned int dwarf_line_debug = 0;
92
93 /* When true, cross-check physname against demangler. */
94 static bool check_physname = false;
95
96 /* When true, do not reject deprecated .gdb_index sections. */
97 static bool use_deprecated_index_sections = false;
98
99 static const struct objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
100
101 /* The "aclass" indices for various kinds of computed DWARF symbols. */
102
103 static int dwarf2_locexpr_index;
104 static int dwarf2_loclist_index;
105 static int dwarf2_locexpr_block_index;
106 static int dwarf2_loclist_block_index;
107
108 /* An index into a (C++) symbol name component in a symbol name as
109 recorded in the mapped_index's symbol table. For each C++ symbol
110 in the symbol table, we record one entry for the start of each
111 component in the symbol in a table of name components, and then
112 sort the table, in order to be able to binary search symbol names,
113 ignoring leading namespaces, both completion and regular look up.
114 For example, for symbol "A::B::C", we'll have an entry that points
115 to "A::B::C", another that points to "B::C", and another for "C".
116 Note that function symbols in GDB index have no parameter
117 information, just the function/method names. You can convert a
118 name_component to a "const char *" using the
119 'mapped_index::symbol_name_at(offset_type)' method. */
120
121 struct name_component
122 {
123 /* Offset in the symbol name where the component starts. Stored as
124 a (32-bit) offset instead of a pointer to save memory and improve
125 locality on 64-bit architectures. */
126 offset_type name_offset;
127
128 /* The symbol's index in the symbol and constant pool tables of a
129 mapped_index. */
130 offset_type idx;
131 };
132
133 /* Base class containing bits shared by both .gdb_index and
134 .debug_name indexes. */
135
136 struct mapped_index_base
137 {
138 mapped_index_base () = default;
139 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
140
141 /* The name_component table (a sorted vector). See name_component's
142 description above. */
143 std::vector<name_component> name_components;
144
145 /* How NAME_COMPONENTS is sorted. */
146 enum case_sensitivity name_components_casing;
147
148 /* Return the number of names in the symbol table. */
149 virtual size_t symbol_name_count () const = 0;
150
151 /* Get the name of the symbol at IDX in the symbol table. */
152 virtual const char *symbol_name_at (offset_type idx) const = 0;
153
154 /* Return whether the name at IDX in the symbol table should be
155 ignored. */
156 virtual bool symbol_name_slot_invalid (offset_type idx) const
157 {
158 return false;
159 }
160
161 /* Build the symbol name component sorted vector, if we haven't
162 yet. */
163 void build_name_components ();
164
165 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
166 possible matches for LN_NO_PARAMS in the name component
167 vector. */
168 std::pair<std::vector<name_component>::const_iterator,
169 std::vector<name_component>::const_iterator>
170 find_name_components_bounds (const lookup_name_info &ln_no_params,
171 enum language lang) const;
172
173 /* Prevent deleting/destroying via a base class pointer. */
174 protected:
175 ~mapped_index_base() = default;
176 };
177
178 /* A description of the mapped index. The file format is described in
179 a comment by the code that writes the index. */
180 struct mapped_index final : public mapped_index_base
181 {
182 /* A slot/bucket in the symbol table hash. */
183 struct symbol_table_slot
184 {
185 const offset_type name;
186 const offset_type vec;
187 };
188
189 /* Index data format version. */
190 int version = 0;
191
192 /* The address table data. */
193 gdb::array_view<const gdb_byte> address_table;
194
195 /* The symbol table, implemented as a hash table. */
196 gdb::array_view<symbol_table_slot> symbol_table;
197
198 /* A pointer to the constant pool. */
199 const char *constant_pool = nullptr;
200
201 bool symbol_name_slot_invalid (offset_type idx) const override
202 {
203 const auto &bucket = this->symbol_table[idx];
204 return bucket.name == 0 && bucket.vec == 0;
205 }
206
207 /* Convenience method to get at the name of the symbol at IDX in the
208 symbol table. */
209 const char *symbol_name_at (offset_type idx) const override
210 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
211
212 size_t symbol_name_count () const override
213 { return this->symbol_table.size (); }
214 };
215
216 /* A description of the mapped .debug_names.
217 Uninitialized map has CU_COUNT 0. */
218 struct mapped_debug_names final : public mapped_index_base
219 {
220 mapped_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile_)
221 : dwarf2_per_objfile (dwarf2_per_objfile_)
222 {}
223
224 struct dwarf2_per_objfile *dwarf2_per_objfile;
225 bfd_endian dwarf5_byte_order;
226 bool dwarf5_is_dwarf64;
227 bool augmentation_is_gdb;
228 uint8_t offset_size;
229 uint32_t cu_count = 0;
230 uint32_t tu_count, bucket_count, name_count;
231 const gdb_byte *cu_table_reordered, *tu_table_reordered;
232 const uint32_t *bucket_table_reordered, *hash_table_reordered;
233 const gdb_byte *name_table_string_offs_reordered;
234 const gdb_byte *name_table_entry_offs_reordered;
235 const gdb_byte *entry_pool;
236
237 struct index_val
238 {
239 ULONGEST dwarf_tag;
240 struct attr
241 {
242 /* Attribute name DW_IDX_*. */
243 ULONGEST dw_idx;
244
245 /* Attribute form DW_FORM_*. */
246 ULONGEST form;
247
248 /* Value if FORM is DW_FORM_implicit_const. */
249 LONGEST implicit_const;
250 };
251 std::vector<attr> attr_vec;
252 };
253
254 std::unordered_map<ULONGEST, index_val> abbrev_map;
255
256 const char *namei_to_name (uint32_t namei) const;
257
258 /* Implementation of the mapped_index_base virtual interface, for
259 the name_components cache. */
260
261 const char *symbol_name_at (offset_type idx) const override
262 { return namei_to_name (idx); }
263
264 size_t symbol_name_count () const override
265 { return this->name_count; }
266 };
267
268 /* See dwarf2read.h. */
269
270 dwarf2_per_objfile *
271 get_dwarf2_per_objfile (struct objfile *objfile)
272 {
273 return dwarf2_objfile_data_key.get (objfile);
274 }
275
276 /* Default names of the debugging sections. */
277
278 /* Note that if the debugging section has been compressed, it might
279 have a name like .zdebug_info. */
280
281 static const struct dwarf2_debug_sections dwarf2_elf_names =
282 {
283 { ".debug_info", ".zdebug_info" },
284 { ".debug_abbrev", ".zdebug_abbrev" },
285 { ".debug_line", ".zdebug_line" },
286 { ".debug_loc", ".zdebug_loc" },
287 { ".debug_loclists", ".zdebug_loclists" },
288 { ".debug_macinfo", ".zdebug_macinfo" },
289 { ".debug_macro", ".zdebug_macro" },
290 { ".debug_str", ".zdebug_str" },
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_GNU_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 attribute if present, zero otherwise
512 (zero is a valid value though).
513 Note this value comes from the Fission stub CU/TU's DIE. */
514 ULONGEST addr_base = 0;
515
516 /* The DW_AT_ranges_base attribute if present, zero otherwise
517 (zero is a valid value though).
518 Note this value comes from the Fission stub CU/TU's DIE.
519 Also note that the value is zero in the non-DWO case so this value can
520 be used without needing to know whether DWO files are in use or not.
521 N.B. This does not apply to DW_AT_ranges appearing in
522 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
523 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
524 DW_AT_ranges_base *would* have to be applied, and we'd have to care
525 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
526 ULONGEST ranges_base = 0;
527
528 /* When reading debug info generated by older versions of rustc, we
529 have to rewrite some union types to be struct types with a
530 variant part. This rewriting must be done after the CU is fully
531 read in, because otherwise at the point of rewriting some struct
532 type might not have been fully processed. So, we keep a list of
533 all such types here and process them after expansion. */
534 std::vector<struct type *> rust_unions;
535
536 /* Mark used when releasing cached dies. */
537 bool mark : 1;
538
539 /* This CU references .debug_loc. See the symtab->locations_valid field.
540 This test is imperfect as there may exist optimized debug code not using
541 any location list and still facing inlining issues if handled as
542 unoptimized code. For a future better test see GCC PR other/32998. */
543 bool has_loclist : 1;
544
545 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
546 if all the producer_is_* fields are valid. This information is cached
547 because profiling CU expansion showed excessive time spent in
548 producer_is_gxx_lt_4_6. */
549 bool checked_producer : 1;
550 bool producer_is_gxx_lt_4_6 : 1;
551 bool producer_is_gcc_lt_4_3 : 1;
552 bool producer_is_icc : 1;
553 bool producer_is_icc_lt_14 : 1;
554 bool producer_is_codewarrior : 1;
555
556 /* When true, the file that we're processing is known to have
557 debugging info for C++ namespaces. GCC 3.3.x did not produce
558 this information, but later versions do. */
559
560 bool processing_has_namespace_info : 1;
561
562 struct partial_die_info *find_partial_die (sect_offset sect_off);
563
564 /* If this CU was inherited by another CU (via specification,
565 abstract_origin, etc), this is the ancestor CU. */
566 dwarf2_cu *ancestor;
567
568 /* Get the buildsym_compunit for this CU. */
569 buildsym_compunit *get_builder ()
570 {
571 /* If this CU has a builder associated with it, use that. */
572 if (m_builder != nullptr)
573 return m_builder.get ();
574
575 /* Otherwise, search ancestors for a valid builder. */
576 if (ancestor != nullptr)
577 return ancestor->get_builder ();
578
579 return nullptr;
580 }
581 };
582
583 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
584 This includes type_unit_group and quick_file_names. */
585
586 struct stmt_list_hash
587 {
588 /* The DWO unit this table is from or NULL if there is none. */
589 struct dwo_unit *dwo_unit;
590
591 /* Offset in .debug_line or .debug_line.dwo. */
592 sect_offset line_sect_off;
593 };
594
595 /* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
596 an object of this type. */
597
598 struct type_unit_group
599 {
600 /* dwarf2read.c's main "handle" on a TU symtab.
601 To simplify things we create an artificial CU that "includes" all the
602 type units using this stmt_list so that the rest of the code still has
603 a "per_cu" handle on the symtab.
604 This PER_CU is recognized by having no section. */
605 #define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
606 struct dwarf2_per_cu_data per_cu;
607
608 /* The TUs that share this DW_AT_stmt_list entry.
609 This is added to while parsing type units to build partial symtabs,
610 and is deleted afterwards and not used again. */
611 std::vector<signatured_type *> *tus;
612
613 /* The compunit symtab.
614 Type units in a group needn't all be defined in the same source file,
615 so we create an essentially anonymous symtab as the compunit symtab. */
616 struct compunit_symtab *compunit_symtab;
617
618 /* The data used to construct the hash key. */
619 struct stmt_list_hash hash;
620
621 /* The number of symtabs from the line header.
622 The value here must match line_header.num_file_names. */
623 unsigned int num_symtabs;
624
625 /* The symbol tables for this TU (obtained from the files listed in
626 DW_AT_stmt_list).
627 WARNING: The order of entries here must match the order of entries
628 in the line header. After the first TU using this type_unit_group, the
629 line header for the subsequent TUs is recreated from this. This is done
630 because we need to use the same symtabs for each TU using the same
631 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
632 there's no guarantee the line header doesn't have duplicate entries. */
633 struct symtab **symtabs;
634 };
635
636 /* These sections are what may appear in a (real or virtual) DWO file. */
637
638 struct dwo_sections
639 {
640 struct dwarf2_section_info abbrev;
641 struct dwarf2_section_info line;
642 struct dwarf2_section_info loc;
643 struct dwarf2_section_info loclists;
644 struct dwarf2_section_info macinfo;
645 struct dwarf2_section_info macro;
646 struct dwarf2_section_info str;
647 struct dwarf2_section_info str_offsets;
648 /* In the case of a virtual DWO file, these two are unused. */
649 struct dwarf2_section_info info;
650 std::vector<dwarf2_section_info> types;
651 };
652
653 /* CUs/TUs in DWP/DWO files. */
654
655 struct dwo_unit
656 {
657 /* Backlink to the containing struct dwo_file. */
658 struct dwo_file *dwo_file;
659
660 /* The "id" that distinguishes this CU/TU.
661 .debug_info calls this "dwo_id", .debug_types calls this "signature".
662 Since signatures came first, we stick with it for consistency. */
663 ULONGEST signature;
664
665 /* The section this CU/TU lives in, in the DWO file. */
666 struct dwarf2_section_info *section;
667
668 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
669 sect_offset sect_off;
670 unsigned int length;
671
672 /* For types, offset in the type's DIE of the type defined by this TU. */
673 cu_offset type_offset_in_tu;
674 };
675
676 /* include/dwarf2.h defines the DWP section codes.
677 It defines a max value but it doesn't define a min value, which we
678 use for error checking, so provide one. */
679
680 enum dwp_v2_section_ids
681 {
682 DW_SECT_MIN = 1
683 };
684
685 /* Data for one DWO file.
686
687 This includes virtual DWO files (a virtual DWO file is a DWO file as it
688 appears in a DWP file). DWP files don't really have DWO files per se -
689 comdat folding of types "loses" the DWO file they came from, and from
690 a high level view DWP files appear to contain a mass of random types.
691 However, to maintain consistency with the non-DWP case we pretend DWP
692 files contain virtual DWO files, and we assign each TU with one virtual
693 DWO file (generally based on the line and abbrev section offsets -
694 a heuristic that seems to work in practice). */
695
696 struct dwo_file
697 {
698 dwo_file () = default;
699 DISABLE_COPY_AND_ASSIGN (dwo_file);
700
701 /* The DW_AT_GNU_dwo_name attribute.
702 For virtual DWO files the name is constructed from the section offsets
703 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
704 from related CU+TUs. */
705 const char *dwo_name = nullptr;
706
707 /* The DW_AT_comp_dir attribute. */
708 const char *comp_dir = nullptr;
709
710 /* The bfd, when the file is open. Otherwise this is NULL.
711 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
712 gdb_bfd_ref_ptr dbfd;
713
714 /* The sections that make up this DWO file.
715 Remember that for virtual DWO files in DWP V2, these are virtual
716 sections (for lack of a better name). */
717 struct dwo_sections sections {};
718
719 /* The CUs in the file.
720 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
721 an extension to handle LLVM's Link Time Optimization output (where
722 multiple source files may be compiled into a single object/dwo pair). */
723 htab_t cus {};
724
725 /* Table of TUs in the file.
726 Each element is a struct dwo_unit. */
727 htab_t tus {};
728 };
729
730 /* These sections are what may appear in a DWP file. */
731
732 struct dwp_sections
733 {
734 /* These are used by both DWP version 1 and 2. */
735 struct dwarf2_section_info str;
736 struct dwarf2_section_info cu_index;
737 struct dwarf2_section_info tu_index;
738
739 /* These are only used by DWP version 2 files.
740 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
741 sections are referenced by section number, and are not recorded here.
742 In DWP version 2 there is at most one copy of all these sections, each
743 section being (effectively) comprised of the concatenation of all of the
744 individual sections that exist in the version 1 format.
745 To keep the code simple we treat each of these concatenated pieces as a
746 section itself (a virtual section?). */
747 struct dwarf2_section_info abbrev;
748 struct dwarf2_section_info info;
749 struct dwarf2_section_info line;
750 struct dwarf2_section_info loc;
751 struct dwarf2_section_info macinfo;
752 struct dwarf2_section_info macro;
753 struct dwarf2_section_info str_offsets;
754 struct dwarf2_section_info types;
755 };
756
757 /* These sections are what may appear in a virtual DWO file in DWP version 1.
758 A virtual DWO file is a DWO file as it appears in a DWP file. */
759
760 struct virtual_v1_dwo_sections
761 {
762 struct dwarf2_section_info abbrev;
763 struct dwarf2_section_info line;
764 struct dwarf2_section_info loc;
765 struct dwarf2_section_info macinfo;
766 struct dwarf2_section_info macro;
767 struct dwarf2_section_info str_offsets;
768 /* Each DWP hash table entry records one CU or one TU.
769 That is recorded here, and copied to dwo_unit.section. */
770 struct dwarf2_section_info info_or_types;
771 };
772
773 /* Similar to virtual_v1_dwo_sections, but for DWP version 2.
774 In version 2, the sections of the DWO files are concatenated together
775 and stored in one section of that name. Thus each ELF section contains
776 several "virtual" sections. */
777
778 struct virtual_v2_dwo_sections
779 {
780 bfd_size_type abbrev_offset;
781 bfd_size_type abbrev_size;
782
783 bfd_size_type line_offset;
784 bfd_size_type line_size;
785
786 bfd_size_type loc_offset;
787 bfd_size_type loc_size;
788
789 bfd_size_type macinfo_offset;
790 bfd_size_type macinfo_size;
791
792 bfd_size_type macro_offset;
793 bfd_size_type macro_size;
794
795 bfd_size_type str_offsets_offset;
796 bfd_size_type str_offsets_size;
797
798 /* Each DWP hash table entry records one CU or one TU.
799 That is recorded here, and copied to dwo_unit.section. */
800 bfd_size_type info_or_types_offset;
801 bfd_size_type info_or_types_size;
802 };
803
804 /* Contents of DWP hash tables. */
805
806 struct dwp_hash_table
807 {
808 uint32_t version, nr_columns;
809 uint32_t nr_units, nr_slots;
810 const gdb_byte *hash_table, *unit_table;
811 union
812 {
813 struct
814 {
815 const gdb_byte *indices;
816 } v1;
817 struct
818 {
819 /* This is indexed by column number and gives the id of the section
820 in that column. */
821 #define MAX_NR_V2_DWO_SECTIONS \
822 (1 /* .debug_info or .debug_types */ \
823 + 1 /* .debug_abbrev */ \
824 + 1 /* .debug_line */ \
825 + 1 /* .debug_loc */ \
826 + 1 /* .debug_str_offsets */ \
827 + 1 /* .debug_macro or .debug_macinfo */)
828 int section_ids[MAX_NR_V2_DWO_SECTIONS];
829 const gdb_byte *offsets;
830 const gdb_byte *sizes;
831 } v2;
832 } section_pool;
833 };
834
835 /* Data for one DWP file. */
836
837 struct dwp_file
838 {
839 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
840 : name (name_),
841 dbfd (std::move (abfd))
842 {
843 }
844
845 /* Name of the file. */
846 const char *name;
847
848 /* File format version. */
849 int version = 0;
850
851 /* The bfd. */
852 gdb_bfd_ref_ptr dbfd;
853
854 /* Section info for this file. */
855 struct dwp_sections sections {};
856
857 /* Table of CUs in the file. */
858 const struct dwp_hash_table *cus = nullptr;
859
860 /* Table of TUs in the file. */
861 const struct dwp_hash_table *tus = nullptr;
862
863 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
864 htab_t loaded_cus {};
865 htab_t loaded_tus {};
866
867 /* Table to map ELF section numbers to their sections.
868 This is only needed for the DWP V1 file format. */
869 unsigned int num_sections = 0;
870 asection **elf_sections = nullptr;
871 };
872
873 /* Struct used to pass misc. parameters to read_die_and_children, et
874 al. which are used for both .debug_info and .debug_types dies.
875 All parameters here are unchanging for the life of the call. This
876 struct exists to abstract away the constant parameters of die reading. */
877
878 struct die_reader_specs
879 {
880 /* The bfd of die_section. */
881 bfd* abfd;
882
883 /* The CU of the DIE we are parsing. */
884 struct dwarf2_cu *cu;
885
886 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
887 struct dwo_file *dwo_file;
888
889 /* The section the die comes from.
890 This is either .debug_info or .debug_types, or the .dwo variants. */
891 struct dwarf2_section_info *die_section;
892
893 /* die_section->buffer. */
894 const gdb_byte *buffer;
895
896 /* The end of the buffer. */
897 const gdb_byte *buffer_end;
898
899 /* The value of the DW_AT_comp_dir attribute. */
900 const char *comp_dir;
901
902 /* The abbreviation table to use when reading the DIEs. */
903 struct abbrev_table *abbrev_table;
904 };
905
906 /* Type of function passed to init_cutu_and_read_dies, et.al. */
907 typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
908 const gdb_byte *info_ptr,
909 struct die_info *comp_unit_die,
910 int has_children,
911 void *data);
912
913 /* dir_index is 1-based in DWARF 4 and before, and is 0-based in DWARF 5 and
914 later. */
915 typedef int dir_index;
916
917 /* file_name_index is 1-based in DWARF 4 and before, and is 0-based in DWARF 5
918 and later. */
919 typedef int file_name_index;
920
921 struct file_entry
922 {
923 file_entry () = default;
924
925 file_entry (const char *name_, dir_index d_index_,
926 unsigned int mod_time_, unsigned int length_)
927 : name (name_),
928 d_index (d_index_),
929 mod_time (mod_time_),
930 length (length_)
931 {}
932
933 /* Return the include directory at D_INDEX stored in LH. Returns
934 NULL if D_INDEX is out of bounds. */
935 const char *include_dir (const line_header *lh) const;
936
937 /* The file name. Note this is an observing pointer. The memory is
938 owned by debug_line_buffer. */
939 const char *name {};
940
941 /* The directory index (1-based). */
942 dir_index d_index {};
943
944 unsigned int mod_time {};
945
946 unsigned int length {};
947
948 /* True if referenced by the Line Number Program. */
949 bool included_p {};
950
951 /* The associated symbol table, if any. */
952 struct symtab *symtab {};
953 };
954
955 /* The line number information for a compilation unit (found in the
956 .debug_line section) begins with a "statement program header",
957 which contains the following information. */
958 struct line_header
959 {
960 line_header ()
961 : offset_in_dwz {}
962 {}
963
964 /* Add an entry to the include directory table. */
965 void add_include_dir (const char *include_dir);
966
967 /* Add an entry to the file name table. */
968 void add_file_name (const char *name, dir_index d_index,
969 unsigned int mod_time, unsigned int length);
970
971 /* Return the include dir at INDEX (0-based in DWARF 5 and 1-based before).
972 Returns NULL if INDEX is out of bounds. */
973 const char *include_dir_at (dir_index index) const
974 {
975 int vec_index;
976 if (version >= 5)
977 vec_index = index;
978 else
979 vec_index = index - 1;
980 if (vec_index < 0 || vec_index >= m_include_dirs.size ())
981 return NULL;
982 return m_include_dirs[vec_index];
983 }
984
985 bool is_valid_file_index (int file_index)
986 {
987 if (version >= 5)
988 return 0 <= file_index && file_index < file_names_size ();
989 return 1 <= file_index && file_index <= file_names_size ();
990 }
991
992 /* Return the file name at INDEX (0-based in DWARF 5 and 1-based before).
993 Returns NULL if INDEX is out of bounds. */
994 file_entry *file_name_at (file_name_index index)
995 {
996 int vec_index;
997 if (version >= 5)
998 vec_index = index;
999 else
1000 vec_index = index - 1;
1001 if (vec_index < 0 || vec_index >= m_file_names.size ())
1002 return NULL;
1003 return &m_file_names[vec_index];
1004 }
1005
1006 /* The indexes are 0-based in DWARF 5 and 1-based in DWARF 4. Therefore,
1007 this method should only be used to iterate through all file entries in an
1008 index-agnostic manner. */
1009 std::vector<file_entry> &file_names ()
1010 { return m_file_names; }
1011
1012 /* Offset of line number information in .debug_line section. */
1013 sect_offset sect_off {};
1014
1015 /* OFFSET is for struct dwz_file associated with dwarf2_per_objfile. */
1016 unsigned offset_in_dwz : 1; /* Can't initialize bitfields in-class. */
1017
1018 unsigned int total_length {};
1019 unsigned short version {};
1020 unsigned int header_length {};
1021 unsigned char minimum_instruction_length {};
1022 unsigned char maximum_ops_per_instruction {};
1023 unsigned char default_is_stmt {};
1024 int line_base {};
1025 unsigned char line_range {};
1026 unsigned char opcode_base {};
1027
1028 /* standard_opcode_lengths[i] is the number of operands for the
1029 standard opcode whose value is i. This means that
1030 standard_opcode_lengths[0] is unused, and the last meaningful
1031 element is standard_opcode_lengths[opcode_base - 1]. */
1032 std::unique_ptr<unsigned char[]> standard_opcode_lengths;
1033
1034 int file_names_size ()
1035 { return m_file_names.size(); }
1036
1037 /* The start and end of the statement program following this
1038 header. These point into dwarf2_per_objfile->line_buffer. */
1039 const gdb_byte *statement_program_start {}, *statement_program_end {};
1040
1041 private:
1042 /* The include_directories table. Note these are observing
1043 pointers. The memory is owned by debug_line_buffer. */
1044 std::vector<const char *> m_include_dirs;
1045
1046 /* The file_names table. This is private because the meaning of indexes
1047 differs among DWARF versions (The first valid index is 1 in DWARF 4 and
1048 before, and is 0 in DWARF 5 and later). So the client should use
1049 file_name_at method for access. */
1050 std::vector<file_entry> m_file_names;
1051 };
1052
1053 typedef std::unique_ptr<line_header> line_header_up;
1054
1055 const char *
1056 file_entry::include_dir (const line_header *lh) const
1057 {
1058 return lh->include_dir_at (d_index);
1059 }
1060
1061 /* When we construct a partial symbol table entry we only
1062 need this much information. */
1063 struct partial_die_info : public allocate_on_obstack
1064 {
1065 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
1066
1067 /* Disable assign but still keep copy ctor, which is needed
1068 load_partial_dies. */
1069 partial_die_info& operator=(const partial_die_info& rhs) = delete;
1070
1071 /* Adjust the partial die before generating a symbol for it. This
1072 function may set the is_external flag or change the DIE's
1073 name. */
1074 void fixup (struct dwarf2_cu *cu);
1075
1076 /* Read a minimal amount of information into the minimal die
1077 structure. */
1078 const gdb_byte *read (const struct die_reader_specs *reader,
1079 const struct abbrev_info &abbrev,
1080 const gdb_byte *info_ptr);
1081
1082 /* Offset of this DIE. */
1083 const sect_offset sect_off;
1084
1085 /* DWARF-2 tag for this DIE. */
1086 const ENUM_BITFIELD(dwarf_tag) tag : 16;
1087
1088 /* Assorted flags describing the data found in this DIE. */
1089 const unsigned int has_children : 1;
1090
1091 unsigned int is_external : 1;
1092 unsigned int is_declaration : 1;
1093 unsigned int has_type : 1;
1094 unsigned int has_specification : 1;
1095 unsigned int has_pc_info : 1;
1096 unsigned int may_be_inlined : 1;
1097
1098 /* This DIE has been marked DW_AT_main_subprogram. */
1099 unsigned int main_subprogram : 1;
1100
1101 /* Flag set if the SCOPE field of this structure has been
1102 computed. */
1103 unsigned int scope_set : 1;
1104
1105 /* Flag set if the DIE has a byte_size attribute. */
1106 unsigned int has_byte_size : 1;
1107
1108 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1109 unsigned int has_const_value : 1;
1110
1111 /* Flag set if any of the DIE's children are template arguments. */
1112 unsigned int has_template_arguments : 1;
1113
1114 /* Flag set if fixup has been called on this die. */
1115 unsigned int fixup_called : 1;
1116
1117 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1118 unsigned int is_dwz : 1;
1119
1120 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1121 unsigned int spec_is_dwz : 1;
1122
1123 /* The name of this DIE. Normally the value of DW_AT_name, but
1124 sometimes a default name for unnamed DIEs. */
1125 const char *name = nullptr;
1126
1127 /* The linkage name, if present. */
1128 const char *linkage_name = nullptr;
1129
1130 /* The scope to prepend to our children. This is generally
1131 allocated on the comp_unit_obstack, so will disappear
1132 when this compilation unit leaves the cache. */
1133 const char *scope = nullptr;
1134
1135 /* Some data associated with the partial DIE. The tag determines
1136 which field is live. */
1137 union
1138 {
1139 /* The location description associated with this DIE, if any. */
1140 struct dwarf_block *locdesc;
1141 /* The offset of an import, for DW_TAG_imported_unit. */
1142 sect_offset sect_off;
1143 } d {};
1144
1145 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1146 CORE_ADDR lowpc = 0;
1147 CORE_ADDR highpc = 0;
1148
1149 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1150 DW_AT_sibling, if any. */
1151 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1152 could return DW_AT_sibling values to its caller load_partial_dies. */
1153 const gdb_byte *sibling = nullptr;
1154
1155 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1156 DW_AT_specification (or DW_AT_abstract_origin or
1157 DW_AT_extension). */
1158 sect_offset spec_offset {};
1159
1160 /* Pointers to this DIE's parent, first child, and next sibling,
1161 if any. */
1162 struct partial_die_info *die_parent = nullptr;
1163 struct partial_die_info *die_child = nullptr;
1164 struct partial_die_info *die_sibling = nullptr;
1165
1166 friend struct partial_die_info *
1167 dwarf2_cu::find_partial_die (sect_offset sect_off);
1168
1169 private:
1170 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1171 partial_die_info (sect_offset sect_off)
1172 : partial_die_info (sect_off, DW_TAG_padding, 0)
1173 {
1174 }
1175
1176 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1177 int has_children_)
1178 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1179 {
1180 is_external = 0;
1181 is_declaration = 0;
1182 has_type = 0;
1183 has_specification = 0;
1184 has_pc_info = 0;
1185 may_be_inlined = 0;
1186 main_subprogram = 0;
1187 scope_set = 0;
1188 has_byte_size = 0;
1189 has_const_value = 0;
1190 has_template_arguments = 0;
1191 fixup_called = 0;
1192 is_dwz = 0;
1193 spec_is_dwz = 0;
1194 }
1195 };
1196
1197 /* This data structure holds the information of an abbrev. */
1198 struct abbrev_info
1199 {
1200 unsigned int number; /* number identifying abbrev */
1201 enum dwarf_tag tag; /* dwarf tag */
1202 unsigned short has_children; /* boolean */
1203 unsigned short num_attrs; /* number of attributes */
1204 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1205 struct abbrev_info *next; /* next in chain */
1206 };
1207
1208 struct attr_abbrev
1209 {
1210 ENUM_BITFIELD(dwarf_attribute) name : 16;
1211 ENUM_BITFIELD(dwarf_form) form : 16;
1212
1213 /* It is valid only if FORM is DW_FORM_implicit_const. */
1214 LONGEST implicit_const;
1215 };
1216
1217 /* Size of abbrev_table.abbrev_hash_table. */
1218 #define ABBREV_HASH_SIZE 121
1219
1220 /* Top level data structure to contain an abbreviation table. */
1221
1222 struct abbrev_table
1223 {
1224 explicit abbrev_table (sect_offset off)
1225 : sect_off (off)
1226 {
1227 m_abbrevs =
1228 XOBNEWVEC (&abbrev_obstack, struct abbrev_info *, ABBREV_HASH_SIZE);
1229 memset (m_abbrevs, 0, ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
1230 }
1231
1232 DISABLE_COPY_AND_ASSIGN (abbrev_table);
1233
1234 /* Allocate space for a struct abbrev_info object in
1235 ABBREV_TABLE. */
1236 struct abbrev_info *alloc_abbrev ();
1237
1238 /* Add an abbreviation to the table. */
1239 void add_abbrev (unsigned int abbrev_number, struct abbrev_info *abbrev);
1240
1241 /* Look up an abbrev in the table.
1242 Returns NULL if the abbrev is not found. */
1243
1244 struct abbrev_info *lookup_abbrev (unsigned int abbrev_number);
1245
1246
1247 /* Where the abbrev table came from.
1248 This is used as a sanity check when the table is used. */
1249 const sect_offset sect_off;
1250
1251 /* Storage for the abbrev table. */
1252 auto_obstack abbrev_obstack;
1253
1254 private:
1255
1256 /* Hash table of abbrevs.
1257 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1258 It could be statically allocated, but the previous code didn't so we
1259 don't either. */
1260 struct abbrev_info **m_abbrevs;
1261 };
1262
1263 typedef std::unique_ptr<struct abbrev_table> abbrev_table_up;
1264
1265 /* Attributes have a name and a value. */
1266 struct attribute
1267 {
1268 ENUM_BITFIELD(dwarf_attribute) name : 16;
1269 ENUM_BITFIELD(dwarf_form) form : 15;
1270
1271 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1272 field should be in u.str (existing only for DW_STRING) but it is kept
1273 here for better struct attribute alignment. */
1274 unsigned int string_is_canonical : 1;
1275
1276 union
1277 {
1278 const char *str;
1279 struct dwarf_block *blk;
1280 ULONGEST unsnd;
1281 LONGEST snd;
1282 CORE_ADDR addr;
1283 ULONGEST signature;
1284 }
1285 u;
1286 };
1287
1288 /* This data structure holds a complete die structure. */
1289 struct die_info
1290 {
1291 /* DWARF-2 tag for this DIE. */
1292 ENUM_BITFIELD(dwarf_tag) tag : 16;
1293
1294 /* Number of attributes */
1295 unsigned char num_attrs;
1296
1297 /* True if we're presently building the full type name for the
1298 type derived from this DIE. */
1299 unsigned char building_fullname : 1;
1300
1301 /* True if this die is in process. PR 16581. */
1302 unsigned char in_process : 1;
1303
1304 /* Abbrev number */
1305 unsigned int abbrev;
1306
1307 /* Offset in .debug_info or .debug_types section. */
1308 sect_offset sect_off;
1309
1310 /* The dies in a compilation unit form an n-ary tree. PARENT
1311 points to this die's parent; CHILD points to the first child of
1312 this node; and all the children of a given node are chained
1313 together via their SIBLING fields. */
1314 struct die_info *child; /* Its first child, if any. */
1315 struct die_info *sibling; /* Its next sibling, if any. */
1316 struct die_info *parent; /* Its parent, if any. */
1317
1318 /* An array of attributes, with NUM_ATTRS elements. There may be
1319 zero, but it's not common and zero-sized arrays are not
1320 sufficiently portable C. */
1321 struct attribute attrs[1];
1322 };
1323
1324 /* Get at parts of an attribute structure. */
1325
1326 #define DW_STRING(attr) ((attr)->u.str)
1327 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
1328 #define DW_UNSND(attr) ((attr)->u.unsnd)
1329 #define DW_BLOCK(attr) ((attr)->u.blk)
1330 #define DW_SND(attr) ((attr)->u.snd)
1331 #define DW_ADDR(attr) ((attr)->u.addr)
1332 #define DW_SIGNATURE(attr) ((attr)->u.signature)
1333
1334 /* Blocks are a bunch of untyped bytes. */
1335 struct dwarf_block
1336 {
1337 size_t size;
1338
1339 /* Valid only if SIZE is not zero. */
1340 const gdb_byte *data;
1341 };
1342
1343 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1344 but this would require a corresponding change in unpack_field_as_long
1345 and friends. */
1346 static int bits_per_byte = 8;
1347
1348 /* When reading a variant or variant part, we track a bit more
1349 information about the field, and store it in an object of this
1350 type. */
1351
1352 struct variant_field
1353 {
1354 /* If we see a DW_TAG_variant, then this will be the discriminant
1355 value. */
1356 ULONGEST discriminant_value;
1357 /* If we see a DW_TAG_variant, then this will be set if this is the
1358 default branch. */
1359 bool default_branch;
1360 /* While reading a DW_TAG_variant_part, this will be set if this
1361 field is the discriminant. */
1362 bool is_discriminant;
1363 };
1364
1365 struct nextfield
1366 {
1367 int accessibility = 0;
1368 int virtuality = 0;
1369 /* Extra information to describe a variant or variant part. */
1370 struct variant_field variant {};
1371 struct field field {};
1372 };
1373
1374 struct fnfieldlist
1375 {
1376 const char *name = nullptr;
1377 std::vector<struct fn_field> fnfields;
1378 };
1379
1380 /* The routines that read and process dies for a C struct or C++ class
1381 pass lists of data member fields and lists of member function fields
1382 in an instance of a field_info structure, as defined below. */
1383 struct field_info
1384 {
1385 /* List of data member and baseclasses fields. */
1386 std::vector<struct nextfield> fields;
1387 std::vector<struct nextfield> baseclasses;
1388
1389 /* Number of fields (including baseclasses). */
1390 int nfields = 0;
1391
1392 /* Set if the accessibility of one of the fields is not public. */
1393 int non_public_fields = 0;
1394
1395 /* Member function fieldlist array, contains name of possibly overloaded
1396 member function, number of overloaded member functions and a pointer
1397 to the head of the member function field chain. */
1398 std::vector<struct fnfieldlist> fnfieldlists;
1399
1400 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1401 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1402 std::vector<struct decl_field> typedef_field_list;
1403
1404 /* Nested types defined by this class and the number of elements in this
1405 list. */
1406 std::vector<struct decl_field> nested_types_list;
1407 };
1408
1409 /* One item on the queue of compilation units to read in full symbols
1410 for. */
1411 struct dwarf2_queue_item
1412 {
1413 struct dwarf2_per_cu_data *per_cu;
1414 enum language pretend_language;
1415 struct dwarf2_queue_item *next;
1416 };
1417
1418 /* The current queue. */
1419 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1420
1421 /* Loaded secondary compilation units are kept in memory until they
1422 have not been referenced for the processing of this many
1423 compilation units. Set this to zero to disable caching. Cache
1424 sizes of up to at least twenty will improve startup time for
1425 typical inter-CU-reference binaries, at an obvious memory cost. */
1426 static int dwarf_max_cache_age = 5;
1427 static void
1428 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1429 struct cmd_list_element *c, const char *value)
1430 {
1431 fprintf_filtered (file, _("The upper bound on the age of cached "
1432 "DWARF compilation units is %s.\n"),
1433 value);
1434 }
1435 \f
1436 /* local function prototypes */
1437
1438 static const char *get_section_name (const struct dwarf2_section_info *);
1439
1440 static const char *get_section_file_name (const struct dwarf2_section_info *);
1441
1442 static void dwarf2_find_base_address (struct die_info *die,
1443 struct dwarf2_cu *cu);
1444
1445 static struct partial_symtab *create_partial_symtab
1446 (struct dwarf2_per_cu_data *per_cu, const char *name);
1447
1448 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1449 const gdb_byte *info_ptr,
1450 struct die_info *type_unit_die,
1451 int has_children, void *data);
1452
1453 static void dwarf2_build_psymtabs_hard
1454 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1455
1456 static void scan_partial_symbols (struct partial_die_info *,
1457 CORE_ADDR *, CORE_ADDR *,
1458 int, struct dwarf2_cu *);
1459
1460 static void add_partial_symbol (struct partial_die_info *,
1461 struct dwarf2_cu *);
1462
1463 static void add_partial_namespace (struct partial_die_info *pdi,
1464 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1465 int set_addrmap, struct dwarf2_cu *cu);
1466
1467 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1468 CORE_ADDR *highpc, int set_addrmap,
1469 struct dwarf2_cu *cu);
1470
1471 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1472 struct dwarf2_cu *cu);
1473
1474 static void add_partial_subprogram (struct partial_die_info *pdi,
1475 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1476 int need_pc, struct dwarf2_cu *cu);
1477
1478 static void dwarf2_read_symtab (struct partial_symtab *,
1479 struct objfile *);
1480
1481 static void psymtab_to_symtab_1 (struct partial_symtab *);
1482
1483 static abbrev_table_up abbrev_table_read_table
1484 (struct dwarf2_per_objfile *dwarf2_per_objfile, struct dwarf2_section_info *,
1485 sect_offset);
1486
1487 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1488
1489 static struct partial_die_info *load_partial_dies
1490 (const struct die_reader_specs *, const gdb_byte *, int);
1491
1492 /* A pair of partial_die_info and compilation unit. */
1493 struct cu_partial_die_info
1494 {
1495 /* The compilation unit of the partial_die_info. */
1496 struct dwarf2_cu *cu;
1497 /* A partial_die_info. */
1498 struct partial_die_info *pdi;
1499
1500 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1501 : cu (cu),
1502 pdi (pdi)
1503 { /* Nothing. */ }
1504
1505 private:
1506 cu_partial_die_info () = delete;
1507 };
1508
1509 static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1510 struct dwarf2_cu *);
1511
1512 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1513 struct attribute *, struct attr_abbrev *,
1514 const gdb_byte *);
1515
1516 static unsigned int read_1_byte (bfd *, const gdb_byte *);
1517
1518 static int read_1_signed_byte (bfd *, const gdb_byte *);
1519
1520 static unsigned int read_2_bytes (bfd *, const gdb_byte *);
1521
1522 /* Read the next three bytes (little-endian order) as an unsigned integer. */
1523 static unsigned int read_3_bytes (bfd *, const gdb_byte *);
1524
1525 static unsigned int read_4_bytes (bfd *, const gdb_byte *);
1526
1527 static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
1528
1529 static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
1530 unsigned int *);
1531
1532 static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
1533
1534 static LONGEST read_checked_initial_length_and_offset
1535 (bfd *, const gdb_byte *, const struct comp_unit_head *,
1536 unsigned int *, unsigned int *);
1537
1538 static LONGEST read_offset (bfd *, const gdb_byte *,
1539 const struct comp_unit_head *,
1540 unsigned int *);
1541
1542 static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
1543
1544 static sect_offset read_abbrev_offset
1545 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1546 struct dwarf2_section_info *, sect_offset);
1547
1548 static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
1549
1550 static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
1551
1552 static const char *read_indirect_string
1553 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1554 const struct comp_unit_head *, unsigned int *);
1555
1556 static const char *read_indirect_line_string
1557 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1558 const struct comp_unit_head *, unsigned int *);
1559
1560 static const char *read_indirect_string_at_offset
1561 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
1562 LONGEST str_offset);
1563
1564 static const char *read_indirect_string_from_dwz
1565 (struct objfile *objfile, struct dwz_file *, LONGEST);
1566
1567 static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
1568
1569 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1570 const gdb_byte *,
1571 unsigned int *);
1572
1573 static const char *read_str_index (const struct die_reader_specs *reader,
1574 ULONGEST str_index);
1575
1576 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1577
1578 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1579 struct dwarf2_cu *);
1580
1581 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1582 unsigned int);
1583
1584 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1585 struct dwarf2_cu *cu);
1586
1587 static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1588
1589 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1590 struct dwarf2_cu *cu);
1591
1592 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1593
1594 static struct die_info *die_specification (struct die_info *die,
1595 struct dwarf2_cu **);
1596
1597 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1598 struct dwarf2_cu *cu);
1599
1600 static void dwarf_decode_lines (struct line_header *, const char *,
1601 struct dwarf2_cu *, struct partial_symtab *,
1602 CORE_ADDR, int decode_mapping);
1603
1604 static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1605 const char *);
1606
1607 static struct symbol *new_symbol (struct die_info *, struct type *,
1608 struct dwarf2_cu *, struct symbol * = NULL);
1609
1610 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1611 struct dwarf2_cu *);
1612
1613 static void dwarf2_const_value_attr (const struct attribute *attr,
1614 struct type *type,
1615 const char *name,
1616 struct obstack *obstack,
1617 struct dwarf2_cu *cu, LONGEST *value,
1618 const gdb_byte **bytes,
1619 struct dwarf2_locexpr_baton **baton);
1620
1621 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1622
1623 static int need_gnat_info (struct dwarf2_cu *);
1624
1625 static struct type *die_descriptive_type (struct die_info *,
1626 struct dwarf2_cu *);
1627
1628 static void set_descriptive_type (struct type *, struct die_info *,
1629 struct dwarf2_cu *);
1630
1631 static struct type *die_containing_type (struct die_info *,
1632 struct dwarf2_cu *);
1633
1634 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1635 struct dwarf2_cu *);
1636
1637 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1638
1639 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1640
1641 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1642
1643 static char *typename_concat (struct obstack *obs, const char *prefix,
1644 const char *suffix, int physname,
1645 struct dwarf2_cu *cu);
1646
1647 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1648
1649 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1650
1651 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1652
1653 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1654
1655 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1656
1657 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1658
1659 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1660 struct dwarf2_cu *, struct partial_symtab *);
1661
1662 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1663 values. Keep the items ordered with increasing constraints compliance. */
1664 enum pc_bounds_kind
1665 {
1666 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1667 PC_BOUNDS_NOT_PRESENT,
1668
1669 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1670 were present but they do not form a valid range of PC addresses. */
1671 PC_BOUNDS_INVALID,
1672
1673 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1674 PC_BOUNDS_RANGES,
1675
1676 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1677 PC_BOUNDS_HIGH_LOW,
1678 };
1679
1680 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1681 CORE_ADDR *, CORE_ADDR *,
1682 struct dwarf2_cu *,
1683 struct partial_symtab *);
1684
1685 static void get_scope_pc_bounds (struct die_info *,
1686 CORE_ADDR *, CORE_ADDR *,
1687 struct dwarf2_cu *);
1688
1689 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1690 CORE_ADDR, struct dwarf2_cu *);
1691
1692 static void dwarf2_add_field (struct field_info *, struct die_info *,
1693 struct dwarf2_cu *);
1694
1695 static void dwarf2_attach_fields_to_type (struct field_info *,
1696 struct type *, struct dwarf2_cu *);
1697
1698 static void dwarf2_add_member_fn (struct field_info *,
1699 struct die_info *, struct type *,
1700 struct dwarf2_cu *);
1701
1702 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1703 struct type *,
1704 struct dwarf2_cu *);
1705
1706 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1707
1708 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1709
1710 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1711
1712 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1713
1714 static struct using_direct **using_directives (struct dwarf2_cu *cu);
1715
1716 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1717
1718 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1719
1720 static struct type *read_module_type (struct die_info *die,
1721 struct dwarf2_cu *cu);
1722
1723 static const char *namespace_name (struct die_info *die,
1724 int *is_anonymous, struct dwarf2_cu *);
1725
1726 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1727
1728 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1729
1730 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1731 struct dwarf2_cu *);
1732
1733 static struct die_info *read_die_and_siblings_1
1734 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1735 struct die_info *);
1736
1737 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1738 const gdb_byte *info_ptr,
1739 const gdb_byte **new_info_ptr,
1740 struct die_info *parent);
1741
1742 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1743 struct die_info **, const gdb_byte *,
1744 int *, int);
1745
1746 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1747 struct die_info **, const gdb_byte *,
1748 int *);
1749
1750 static void process_die (struct die_info *, struct dwarf2_cu *);
1751
1752 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1753 struct obstack *);
1754
1755 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1756
1757 static const char *dwarf2_full_name (const char *name,
1758 struct die_info *die,
1759 struct dwarf2_cu *cu);
1760
1761 static const char *dwarf2_physname (const char *name, struct die_info *die,
1762 struct dwarf2_cu *cu);
1763
1764 static struct die_info *dwarf2_extension (struct die_info *die,
1765 struct dwarf2_cu **);
1766
1767 static const char *dwarf_tag_name (unsigned int);
1768
1769 static const char *dwarf_attr_name (unsigned int);
1770
1771 static const char *dwarf_unit_type_name (int unit_type);
1772
1773 static const char *dwarf_form_name (unsigned int);
1774
1775 static const char *dwarf_bool_name (unsigned int);
1776
1777 static const char *dwarf_type_encoding_name (unsigned int);
1778
1779 static struct die_info *sibling_die (struct die_info *);
1780
1781 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1782
1783 static void dump_die_for_error (struct die_info *);
1784
1785 static void dump_die_1 (struct ui_file *, int level, int max_level,
1786 struct die_info *);
1787
1788 /*static*/ void dump_die (struct die_info *, int max_level);
1789
1790 static void store_in_ref_table (struct die_info *,
1791 struct dwarf2_cu *);
1792
1793 static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
1794
1795 static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
1796
1797 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1798 const struct attribute *,
1799 struct dwarf2_cu **);
1800
1801 static struct die_info *follow_die_ref (struct die_info *,
1802 const struct attribute *,
1803 struct dwarf2_cu **);
1804
1805 static struct die_info *follow_die_sig (struct die_info *,
1806 const struct attribute *,
1807 struct dwarf2_cu **);
1808
1809 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1810 struct dwarf2_cu *);
1811
1812 static struct type *get_DW_AT_signature_type (struct die_info *,
1813 const struct attribute *,
1814 struct dwarf2_cu *);
1815
1816 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1817
1818 static void read_signatured_type (struct signatured_type *);
1819
1820 static int attr_to_dynamic_prop (const struct attribute *attr,
1821 struct die_info *die, struct dwarf2_cu *cu,
1822 struct dynamic_prop *prop, struct type *type);
1823
1824 /* memory allocation interface */
1825
1826 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1827
1828 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1829
1830 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1831
1832 static int attr_form_is_block (const struct attribute *);
1833
1834 static int attr_form_is_section_offset (const struct attribute *);
1835
1836 static int attr_form_is_constant (const struct attribute *);
1837
1838 static int attr_form_is_ref (const struct attribute *);
1839
1840 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1841 struct dwarf2_loclist_baton *baton,
1842 const struct attribute *attr);
1843
1844 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1845 struct symbol *sym,
1846 struct dwarf2_cu *cu,
1847 int is_block);
1848
1849 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1850 const gdb_byte *info_ptr,
1851 struct abbrev_info *abbrev);
1852
1853 static hashval_t partial_die_hash (const void *item);
1854
1855 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1856
1857 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1858 (sect_offset sect_off, unsigned int offset_in_dwz,
1859 struct dwarf2_per_objfile *dwarf2_per_objfile);
1860
1861 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1862 struct die_info *comp_unit_die,
1863 enum language pretend_language);
1864
1865 static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1866
1867 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1868
1869 static struct type *set_die_type (struct die_info *, struct type *,
1870 struct dwarf2_cu *);
1871
1872 static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1873
1874 static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1875
1876 static void load_full_comp_unit (struct dwarf2_per_cu_data *, bool,
1877 enum language);
1878
1879 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1880 enum language);
1881
1882 static void process_full_type_unit (struct dwarf2_per_cu_data *,
1883 enum language);
1884
1885 static void dwarf2_add_dependence (struct dwarf2_cu *,
1886 struct dwarf2_per_cu_data *);
1887
1888 static void dwarf2_mark (struct dwarf2_cu *);
1889
1890 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1891
1892 static struct type *get_die_type_at_offset (sect_offset,
1893 struct dwarf2_per_cu_data *);
1894
1895 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1896
1897 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1898 enum language pretend_language);
1899
1900 static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
1901
1902 static struct type *dwarf2_per_cu_addr_type (struct dwarf2_per_cu_data *per_cu);
1903 static struct type *dwarf2_per_cu_addr_sized_int_type
1904 (struct dwarf2_per_cu_data *per_cu, bool unsigned_p);
1905 static struct type *dwarf2_per_cu_int_type
1906 (struct dwarf2_per_cu_data *per_cu, int size_in_bytes,
1907 bool unsigned_p);
1908
1909 /* Class, the destructor of which frees all allocated queue entries. This
1910 will only have work to do if an error was thrown while processing the
1911 dwarf. If no error was thrown then the queue entries should have all
1912 been processed, and freed, as we went along. */
1913
1914 class dwarf2_queue_guard
1915 {
1916 public:
1917 dwarf2_queue_guard () = default;
1918
1919 /* Free any entries remaining on the queue. There should only be
1920 entries left if we hit an error while processing the dwarf. */
1921 ~dwarf2_queue_guard ()
1922 {
1923 struct dwarf2_queue_item *item, *last;
1924
1925 item = dwarf2_queue;
1926 while (item)
1927 {
1928 /* Anything still marked queued is likely to be in an
1929 inconsistent state, so discard it. */
1930 if (item->per_cu->queued)
1931 {
1932 if (item->per_cu->cu != NULL)
1933 free_one_cached_comp_unit (item->per_cu);
1934 item->per_cu->queued = 0;
1935 }
1936
1937 last = item;
1938 item = item->next;
1939 xfree (last);
1940 }
1941
1942 dwarf2_queue = dwarf2_queue_tail = NULL;
1943 }
1944 };
1945
1946 /* The return type of find_file_and_directory. Note, the enclosed
1947 string pointers are only valid while this object is valid. */
1948
1949 struct file_and_directory
1950 {
1951 /* The filename. This is never NULL. */
1952 const char *name;
1953
1954 /* The compilation directory. NULL if not known. If we needed to
1955 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1956 points directly to the DW_AT_comp_dir string attribute owned by
1957 the obstack that owns the DIE. */
1958 const char *comp_dir;
1959
1960 /* If we needed to build a new string for comp_dir, this is what
1961 owns the storage. */
1962 std::string comp_dir_storage;
1963 };
1964
1965 static file_and_directory find_file_and_directory (struct die_info *die,
1966 struct dwarf2_cu *cu);
1967
1968 static char *file_full_name (int file, struct line_header *lh,
1969 const char *comp_dir);
1970
1971 /* Expected enum dwarf_unit_type for read_comp_unit_head. */
1972 enum class rcuh_kind { COMPILE, TYPE };
1973
1974 static const gdb_byte *read_and_check_comp_unit_head
1975 (struct dwarf2_per_objfile* dwarf2_per_objfile,
1976 struct comp_unit_head *header,
1977 struct dwarf2_section_info *section,
1978 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
1979 rcuh_kind section_kind);
1980
1981 static void init_cutu_and_read_dies
1982 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
1983 int use_existing_cu, int keep, bool skip_partial,
1984 die_reader_func_ftype *die_reader_func, void *data);
1985
1986 static void init_cutu_and_read_dies_simple
1987 (struct dwarf2_per_cu_data *this_cu,
1988 die_reader_func_ftype *die_reader_func, void *data);
1989
1990 static htab_t allocate_signatured_type_table (struct objfile *objfile);
1991
1992 static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1993
1994 static struct dwo_unit *lookup_dwo_unit_in_dwp
1995 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1996 struct dwp_file *dwp_file, const char *comp_dir,
1997 ULONGEST signature, int is_debug_types);
1998
1999 static struct dwp_file *get_dwp_file
2000 (struct dwarf2_per_objfile *dwarf2_per_objfile);
2001
2002 static struct dwo_unit *lookup_dwo_comp_unit
2003 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
2004
2005 static struct dwo_unit *lookup_dwo_type_unit
2006 (struct signatured_type *, const char *, const char *);
2007
2008 static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
2009
2010 /* A unique pointer to a dwo_file. */
2011
2012 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
2013
2014 static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
2015
2016 static void check_producer (struct dwarf2_cu *cu);
2017
2018 static void free_line_header_voidp (void *arg);
2019 \f
2020 /* Various complaints about symbol reading that don't abort the process. */
2021
2022 static void
2023 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2024 {
2025 complaint (_("statement list doesn't fit in .debug_line section"));
2026 }
2027
2028 static void
2029 dwarf2_debug_line_missing_file_complaint (void)
2030 {
2031 complaint (_(".debug_line section has line data without a file"));
2032 }
2033
2034 static void
2035 dwarf2_debug_line_missing_end_sequence_complaint (void)
2036 {
2037 complaint (_(".debug_line section has line "
2038 "program sequence without an end"));
2039 }
2040
2041 static void
2042 dwarf2_complex_location_expr_complaint (void)
2043 {
2044 complaint (_("location expression too complex"));
2045 }
2046
2047 static void
2048 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
2049 int arg3)
2050 {
2051 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
2052 arg1, arg2, arg3);
2053 }
2054
2055 static void
2056 dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2057 {
2058 complaint (_("debug info runs off end of %s section"
2059 " [in module %s]"),
2060 get_section_name (section),
2061 get_section_file_name (section));
2062 }
2063
2064 static void
2065 dwarf2_macro_malformed_definition_complaint (const char *arg1)
2066 {
2067 complaint (_("macro debug info contains a "
2068 "malformed macro definition:\n`%s'"),
2069 arg1);
2070 }
2071
2072 static void
2073 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
2074 {
2075 complaint (_("invalid attribute class or form for '%s' in '%s'"),
2076 arg1, arg2);
2077 }
2078
2079 /* Hash function for line_header_hash. */
2080
2081 static hashval_t
2082 line_header_hash (const struct line_header *ofs)
2083 {
2084 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
2085 }
2086
2087 /* Hash function for htab_create_alloc_ex for line_header_hash. */
2088
2089 static hashval_t
2090 line_header_hash_voidp (const void *item)
2091 {
2092 const struct line_header *ofs = (const struct line_header *) item;
2093
2094 return line_header_hash (ofs);
2095 }
2096
2097 /* Equality function for line_header_hash. */
2098
2099 static int
2100 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
2101 {
2102 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
2103 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
2104
2105 return (ofs_lhs->sect_off == ofs_rhs->sect_off
2106 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
2107 }
2108
2109 \f
2110
2111 /* Read the given attribute value as an address, taking the attribute's
2112 form into account. */
2113
2114 static CORE_ADDR
2115 attr_value_as_address (struct attribute *attr)
2116 {
2117 CORE_ADDR addr;
2118
2119 if (attr->form != DW_FORM_addr && attr->form != DW_FORM_addrx
2120 && attr->form != DW_FORM_GNU_addr_index)
2121 {
2122 /* Aside from a few clearly defined exceptions, attributes that
2123 contain an address must always be in DW_FORM_addr form.
2124 Unfortunately, some compilers happen to be violating this
2125 requirement by encoding addresses using other forms, such
2126 as DW_FORM_data4 for example. For those broken compilers,
2127 we try to do our best, without any guarantee of success,
2128 to interpret the address correctly. It would also be nice
2129 to generate a complaint, but that would require us to maintain
2130 a list of legitimate cases where a non-address form is allowed,
2131 as well as update callers to pass in at least the CU's DWARF
2132 version. This is more overhead than what we're willing to
2133 expand for a pretty rare case. */
2134 addr = DW_UNSND (attr);
2135 }
2136 else
2137 addr = DW_ADDR (attr);
2138
2139 return addr;
2140 }
2141
2142 /* See declaration. */
2143
2144 dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
2145 const dwarf2_debug_sections *names,
2146 bool can_copy_)
2147 : objfile (objfile_),
2148 can_copy (can_copy_)
2149 {
2150 if (names == NULL)
2151 names = &dwarf2_elf_names;
2152
2153 bfd *obfd = objfile->obfd;
2154
2155 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
2156 locate_sections (obfd, sec, *names);
2157 }
2158
2159 dwarf2_per_objfile::~dwarf2_per_objfile ()
2160 {
2161 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
2162 free_cached_comp_units ();
2163
2164 if (quick_file_names_table)
2165 htab_delete (quick_file_names_table);
2166
2167 if (line_header_hash)
2168 htab_delete (line_header_hash);
2169
2170 for (dwarf2_per_cu_data *per_cu : all_comp_units)
2171 per_cu->imported_symtabs_free ();
2172
2173 for (signatured_type *sig_type : all_type_units)
2174 sig_type->per_cu.imported_symtabs_free ();
2175
2176 /* Everything else should be on the objfile obstack. */
2177 }
2178
2179 /* See declaration. */
2180
2181 void
2182 dwarf2_per_objfile::free_cached_comp_units ()
2183 {
2184 dwarf2_per_cu_data *per_cu = read_in_chain;
2185 dwarf2_per_cu_data **last_chain = &read_in_chain;
2186 while (per_cu != NULL)
2187 {
2188 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
2189
2190 delete per_cu->cu;
2191 *last_chain = next_cu;
2192 per_cu = next_cu;
2193 }
2194 }
2195
2196 /* A helper class that calls free_cached_comp_units on
2197 destruction. */
2198
2199 class free_cached_comp_units
2200 {
2201 public:
2202
2203 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
2204 : m_per_objfile (per_objfile)
2205 {
2206 }
2207
2208 ~free_cached_comp_units ()
2209 {
2210 m_per_objfile->free_cached_comp_units ();
2211 }
2212
2213 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
2214
2215 private:
2216
2217 dwarf2_per_objfile *m_per_objfile;
2218 };
2219
2220 /* Try to locate the sections we need for DWARF 2 debugging
2221 information and return true if we have enough to do something.
2222 NAMES points to the dwarf2 section names, or is NULL if the standard
2223 ELF names are used. CAN_COPY is true for formats where symbol
2224 interposition is possible and so symbol values must follow copy
2225 relocation rules. */
2226
2227 int
2228 dwarf2_has_info (struct objfile *objfile,
2229 const struct dwarf2_debug_sections *names,
2230 bool can_copy)
2231 {
2232 if (objfile->flags & OBJF_READNEVER)
2233 return 0;
2234
2235 struct dwarf2_per_objfile *dwarf2_per_objfile
2236 = get_dwarf2_per_objfile (objfile);
2237
2238 if (dwarf2_per_objfile == NULL)
2239 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile,
2240 names,
2241 can_copy);
2242
2243 return (!dwarf2_per_objfile->info.is_virtual
2244 && dwarf2_per_objfile->info.s.section != NULL
2245 && !dwarf2_per_objfile->abbrev.is_virtual
2246 && dwarf2_per_objfile->abbrev.s.section != NULL);
2247 }
2248
2249 /* Return the containing section of virtual section SECTION. */
2250
2251 static struct dwarf2_section_info *
2252 get_containing_section (const struct dwarf2_section_info *section)
2253 {
2254 gdb_assert (section->is_virtual);
2255 return section->s.containing_section;
2256 }
2257
2258 /* Return the bfd owner of SECTION. */
2259
2260 static struct bfd *
2261 get_section_bfd_owner (const struct dwarf2_section_info *section)
2262 {
2263 if (section->is_virtual)
2264 {
2265 section = get_containing_section (section);
2266 gdb_assert (!section->is_virtual);
2267 }
2268 return section->s.section->owner;
2269 }
2270
2271 /* Return the bfd section of SECTION.
2272 Returns NULL if the section is not present. */
2273
2274 static asection *
2275 get_section_bfd_section (const struct dwarf2_section_info *section)
2276 {
2277 if (section->is_virtual)
2278 {
2279 section = get_containing_section (section);
2280 gdb_assert (!section->is_virtual);
2281 }
2282 return section->s.section;
2283 }
2284
2285 /* Return the name of SECTION. */
2286
2287 static const char *
2288 get_section_name (const struct dwarf2_section_info *section)
2289 {
2290 asection *sectp = get_section_bfd_section (section);
2291
2292 gdb_assert (sectp != NULL);
2293 return bfd_section_name (sectp);
2294 }
2295
2296 /* Return the name of the file SECTION is in. */
2297
2298 static const char *
2299 get_section_file_name (const struct dwarf2_section_info *section)
2300 {
2301 bfd *abfd = get_section_bfd_owner (section);
2302
2303 return bfd_get_filename (abfd);
2304 }
2305
2306 /* Return the id of SECTION.
2307 Returns 0 if SECTION doesn't exist. */
2308
2309 static int
2310 get_section_id (const struct dwarf2_section_info *section)
2311 {
2312 asection *sectp = get_section_bfd_section (section);
2313
2314 if (sectp == NULL)
2315 return 0;
2316 return sectp->id;
2317 }
2318
2319 /* Return the flags of SECTION.
2320 SECTION (or containing section if this is a virtual section) must exist. */
2321
2322 static int
2323 get_section_flags (const struct dwarf2_section_info *section)
2324 {
2325 asection *sectp = get_section_bfd_section (section);
2326
2327 gdb_assert (sectp != NULL);
2328 return bfd_section_flags (sectp);
2329 }
2330
2331 /* When loading sections, we look either for uncompressed section or for
2332 compressed section names. */
2333
2334 static int
2335 section_is_p (const char *section_name,
2336 const struct dwarf2_section_names *names)
2337 {
2338 if (names->normal != NULL
2339 && strcmp (section_name, names->normal) == 0)
2340 return 1;
2341 if (names->compressed != NULL
2342 && strcmp (section_name, names->compressed) == 0)
2343 return 1;
2344 return 0;
2345 }
2346
2347 /* See declaration. */
2348
2349 void
2350 dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
2351 const dwarf2_debug_sections &names)
2352 {
2353 flagword aflag = bfd_section_flags (sectp);
2354
2355 if ((aflag & SEC_HAS_CONTENTS) == 0)
2356 {
2357 }
2358 else if (elf_section_data (sectp)->this_hdr.sh_size
2359 > bfd_get_file_size (abfd))
2360 {
2361 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
2362 warning (_("Discarding section %s which has a section size (%s"
2363 ") larger than the file size [in module %s]"),
2364 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
2365 bfd_get_filename (abfd));
2366 }
2367 else if (section_is_p (sectp->name, &names.info))
2368 {
2369 this->info.s.section = sectp;
2370 this->info.size = bfd_section_size (sectp);
2371 }
2372 else if (section_is_p (sectp->name, &names.abbrev))
2373 {
2374 this->abbrev.s.section = sectp;
2375 this->abbrev.size = bfd_section_size (sectp);
2376 }
2377 else if (section_is_p (sectp->name, &names.line))
2378 {
2379 this->line.s.section = sectp;
2380 this->line.size = bfd_section_size (sectp);
2381 }
2382 else if (section_is_p (sectp->name, &names.loc))
2383 {
2384 this->loc.s.section = sectp;
2385 this->loc.size = bfd_section_size (sectp);
2386 }
2387 else if (section_is_p (sectp->name, &names.loclists))
2388 {
2389 this->loclists.s.section = sectp;
2390 this->loclists.size = bfd_section_size (sectp);
2391 }
2392 else if (section_is_p (sectp->name, &names.macinfo))
2393 {
2394 this->macinfo.s.section = sectp;
2395 this->macinfo.size = bfd_section_size (sectp);
2396 }
2397 else if (section_is_p (sectp->name, &names.macro))
2398 {
2399 this->macro.s.section = sectp;
2400 this->macro.size = bfd_section_size (sectp);
2401 }
2402 else if (section_is_p (sectp->name, &names.str))
2403 {
2404 this->str.s.section = sectp;
2405 this->str.size = bfd_section_size (sectp);
2406 }
2407 else if (section_is_p (sectp->name, &names.line_str))
2408 {
2409 this->line_str.s.section = sectp;
2410 this->line_str.size = bfd_section_size (sectp);
2411 }
2412 else if (section_is_p (sectp->name, &names.addr))
2413 {
2414 this->addr.s.section = sectp;
2415 this->addr.size = bfd_section_size (sectp);
2416 }
2417 else if (section_is_p (sectp->name, &names.frame))
2418 {
2419 this->frame.s.section = sectp;
2420 this->frame.size = bfd_section_size (sectp);
2421 }
2422 else if (section_is_p (sectp->name, &names.eh_frame))
2423 {
2424 this->eh_frame.s.section = sectp;
2425 this->eh_frame.size = bfd_section_size (sectp);
2426 }
2427 else if (section_is_p (sectp->name, &names.ranges))
2428 {
2429 this->ranges.s.section = sectp;
2430 this->ranges.size = bfd_section_size (sectp);
2431 }
2432 else if (section_is_p (sectp->name, &names.rnglists))
2433 {
2434 this->rnglists.s.section = sectp;
2435 this->rnglists.size = bfd_section_size (sectp);
2436 }
2437 else if (section_is_p (sectp->name, &names.types))
2438 {
2439 struct dwarf2_section_info type_section;
2440
2441 memset (&type_section, 0, sizeof (type_section));
2442 type_section.s.section = sectp;
2443 type_section.size = bfd_section_size (sectp);
2444
2445 this->types.push_back (type_section);
2446 }
2447 else if (section_is_p (sectp->name, &names.gdb_index))
2448 {
2449 this->gdb_index.s.section = sectp;
2450 this->gdb_index.size = bfd_section_size (sectp);
2451 }
2452 else if (section_is_p (sectp->name, &names.debug_names))
2453 {
2454 this->debug_names.s.section = sectp;
2455 this->debug_names.size = bfd_section_size (sectp);
2456 }
2457 else if (section_is_p (sectp->name, &names.debug_aranges))
2458 {
2459 this->debug_aranges.s.section = sectp;
2460 this->debug_aranges.size = bfd_section_size (sectp);
2461 }
2462
2463 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2464 && bfd_section_vma (sectp) == 0)
2465 this->has_section_at_zero = true;
2466 }
2467
2468 /* A helper function that decides whether a section is empty,
2469 or not present. */
2470
2471 static int
2472 dwarf2_section_empty_p (const struct dwarf2_section_info *section)
2473 {
2474 if (section->is_virtual)
2475 return section->size == 0;
2476 return section->s.section == NULL || section->size == 0;
2477 }
2478
2479 /* See dwarf2read.h. */
2480
2481 void
2482 dwarf2_read_section (struct objfile *objfile, dwarf2_section_info *info)
2483 {
2484 asection *sectp;
2485 bfd *abfd;
2486 gdb_byte *buf, *retbuf;
2487
2488 if (info->readin)
2489 return;
2490 info->buffer = NULL;
2491 info->readin = true;
2492
2493 if (dwarf2_section_empty_p (info))
2494 return;
2495
2496 sectp = get_section_bfd_section (info);
2497
2498 /* If this is a virtual section we need to read in the real one first. */
2499 if (info->is_virtual)
2500 {
2501 struct dwarf2_section_info *containing_section =
2502 get_containing_section (info);
2503
2504 gdb_assert (sectp != NULL);
2505 if ((sectp->flags & SEC_RELOC) != 0)
2506 {
2507 error (_("Dwarf Error: DWP format V2 with relocations is not"
2508 " supported in section %s [in module %s]"),
2509 get_section_name (info), get_section_file_name (info));
2510 }
2511 dwarf2_read_section (objfile, containing_section);
2512 /* Other code should have already caught virtual sections that don't
2513 fit. */
2514 gdb_assert (info->virtual_offset + info->size
2515 <= containing_section->size);
2516 /* If the real section is empty or there was a problem reading the
2517 section we shouldn't get here. */
2518 gdb_assert (containing_section->buffer != NULL);
2519 info->buffer = containing_section->buffer + info->virtual_offset;
2520 return;
2521 }
2522
2523 /* If the section has relocations, we must read it ourselves.
2524 Otherwise we attach it to the BFD. */
2525 if ((sectp->flags & SEC_RELOC) == 0)
2526 {
2527 info->buffer = gdb_bfd_map_section (sectp, &info->size);
2528 return;
2529 }
2530
2531 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, info->size);
2532 info->buffer = buf;
2533
2534 /* When debugging .o files, we may need to apply relocations; see
2535 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2536 We never compress sections in .o files, so we only need to
2537 try this when the section is not compressed. */
2538 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
2539 if (retbuf != NULL)
2540 {
2541 info->buffer = retbuf;
2542 return;
2543 }
2544
2545 abfd = get_section_bfd_owner (info);
2546 gdb_assert (abfd != NULL);
2547
2548 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2549 || bfd_bread (buf, info->size, abfd) != info->size)
2550 {
2551 error (_("Dwarf Error: Can't read DWARF data"
2552 " in section %s [in module %s]"),
2553 bfd_section_name (sectp), bfd_get_filename (abfd));
2554 }
2555 }
2556
2557 /* A helper function that returns the size of a section in a safe way.
2558 If you are positive that the section has been read before using the
2559 size, then it is safe to refer to the dwarf2_section_info object's
2560 "size" field directly. In other cases, you must call this
2561 function, because for compressed sections the size field is not set
2562 correctly until the section has been read. */
2563
2564 static bfd_size_type
2565 dwarf2_section_size (struct objfile *objfile,
2566 struct dwarf2_section_info *info)
2567 {
2568 if (!info->readin)
2569 dwarf2_read_section (objfile, info);
2570 return info->size;
2571 }
2572
2573 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2574 SECTION_NAME. */
2575
2576 void
2577 dwarf2_get_section_info (struct objfile *objfile,
2578 enum dwarf2_section_enum sect,
2579 asection **sectp, const gdb_byte **bufp,
2580 bfd_size_type *sizep)
2581 {
2582 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
2583 struct dwarf2_section_info *info;
2584
2585 /* We may see an objfile without any DWARF, in which case we just
2586 return nothing. */
2587 if (data == NULL)
2588 {
2589 *sectp = NULL;
2590 *bufp = NULL;
2591 *sizep = 0;
2592 return;
2593 }
2594 switch (sect)
2595 {
2596 case DWARF2_DEBUG_FRAME:
2597 info = &data->frame;
2598 break;
2599 case DWARF2_EH_FRAME:
2600 info = &data->eh_frame;
2601 break;
2602 default:
2603 gdb_assert_not_reached ("unexpected section");
2604 }
2605
2606 dwarf2_read_section (objfile, info);
2607
2608 *sectp = get_section_bfd_section (info);
2609 *bufp = info->buffer;
2610 *sizep = info->size;
2611 }
2612
2613 /* A helper function to find the sections for a .dwz file. */
2614
2615 static void
2616 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2617 {
2618 struct dwz_file *dwz_file = (struct dwz_file *) arg;
2619
2620 /* Note that we only support the standard ELF names, because .dwz
2621 is ELF-only (at the time of writing). */
2622 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2623 {
2624 dwz_file->abbrev.s.section = sectp;
2625 dwz_file->abbrev.size = bfd_section_size (sectp);
2626 }
2627 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2628 {
2629 dwz_file->info.s.section = sectp;
2630 dwz_file->info.size = bfd_section_size (sectp);
2631 }
2632 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2633 {
2634 dwz_file->str.s.section = sectp;
2635 dwz_file->str.size = bfd_section_size (sectp);
2636 }
2637 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2638 {
2639 dwz_file->line.s.section = sectp;
2640 dwz_file->line.size = bfd_section_size (sectp);
2641 }
2642 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2643 {
2644 dwz_file->macro.s.section = sectp;
2645 dwz_file->macro.size = bfd_section_size (sectp);
2646 }
2647 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2648 {
2649 dwz_file->gdb_index.s.section = sectp;
2650 dwz_file->gdb_index.size = bfd_section_size (sectp);
2651 }
2652 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2653 {
2654 dwz_file->debug_names.s.section = sectp;
2655 dwz_file->debug_names.size = bfd_section_size (sectp);
2656 }
2657 }
2658
2659 /* See dwarf2read.h. */
2660
2661 struct dwz_file *
2662 dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
2663 {
2664 const char *filename;
2665 bfd_size_type buildid_len_arg;
2666 size_t buildid_len;
2667 bfd_byte *buildid;
2668
2669 if (dwarf2_per_objfile->dwz_file != NULL)
2670 return dwarf2_per_objfile->dwz_file.get ();
2671
2672 bfd_set_error (bfd_error_no_error);
2673 gdb::unique_xmalloc_ptr<char> data
2674 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2675 &buildid_len_arg, &buildid));
2676 if (data == NULL)
2677 {
2678 if (bfd_get_error () == bfd_error_no_error)
2679 return NULL;
2680 error (_("could not read '.gnu_debugaltlink' section: %s"),
2681 bfd_errmsg (bfd_get_error ()));
2682 }
2683
2684 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
2685
2686 buildid_len = (size_t) buildid_len_arg;
2687
2688 filename = data.get ();
2689
2690 std::string abs_storage;
2691 if (!IS_ABSOLUTE_PATH (filename))
2692 {
2693 gdb::unique_xmalloc_ptr<char> abs
2694 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
2695
2696 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
2697 filename = abs_storage.c_str ();
2698 }
2699
2700 /* First try the file name given in the section. If that doesn't
2701 work, try to use the build-id instead. */
2702 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
2703 if (dwz_bfd != NULL)
2704 {
2705 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2706 dwz_bfd.reset (nullptr);
2707 }
2708
2709 if (dwz_bfd == NULL)
2710 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2711
2712 if (dwz_bfd == NULL)
2713 error (_("could not find '.gnu_debugaltlink' file for %s"),
2714 objfile_name (dwarf2_per_objfile->objfile));
2715
2716 std::unique_ptr<struct dwz_file> result
2717 (new struct dwz_file (std::move (dwz_bfd)));
2718
2719 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2720 result.get ());
2721
2722 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd,
2723 result->dwz_bfd.get ());
2724 dwarf2_per_objfile->dwz_file = std::move (result);
2725 return dwarf2_per_objfile->dwz_file.get ();
2726 }
2727 \f
2728 /* DWARF quick_symbols_functions support. */
2729
2730 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2731 unique line tables, so we maintain a separate table of all .debug_line
2732 derived entries to support the sharing.
2733 All the quick functions need is the list of file names. We discard the
2734 line_header when we're done and don't need to record it here. */
2735 struct quick_file_names
2736 {
2737 /* The data used to construct the hash key. */
2738 struct stmt_list_hash hash;
2739
2740 /* The number of entries in file_names, real_names. */
2741 unsigned int num_file_names;
2742
2743 /* The file names from the line table, after being run through
2744 file_full_name. */
2745 const char **file_names;
2746
2747 /* The file names from the line table after being run through
2748 gdb_realpath. These are computed lazily. */
2749 const char **real_names;
2750 };
2751
2752 /* When using the index (and thus not using psymtabs), each CU has an
2753 object of this type. This is used to hold information needed by
2754 the various "quick" methods. */
2755 struct dwarf2_per_cu_quick_data
2756 {
2757 /* The file table. This can be NULL if there was no file table
2758 or it's currently not read in.
2759 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2760 struct quick_file_names *file_names;
2761
2762 /* The corresponding symbol table. This is NULL if symbols for this
2763 CU have not yet been read. */
2764 struct compunit_symtab *compunit_symtab;
2765
2766 /* A temporary mark bit used when iterating over all CUs in
2767 expand_symtabs_matching. */
2768 unsigned int mark : 1;
2769
2770 /* True if we've tried to read the file table and found there isn't one.
2771 There will be no point in trying to read it again next time. */
2772 unsigned int no_file_data : 1;
2773 };
2774
2775 /* Utility hash function for a stmt_list_hash. */
2776
2777 static hashval_t
2778 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2779 {
2780 hashval_t v = 0;
2781
2782 if (stmt_list_hash->dwo_unit != NULL)
2783 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2784 v += to_underlying (stmt_list_hash->line_sect_off);
2785 return v;
2786 }
2787
2788 /* Utility equality function for a stmt_list_hash. */
2789
2790 static int
2791 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2792 const struct stmt_list_hash *rhs)
2793 {
2794 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2795 return 0;
2796 if (lhs->dwo_unit != NULL
2797 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2798 return 0;
2799
2800 return lhs->line_sect_off == rhs->line_sect_off;
2801 }
2802
2803 /* Hash function for a quick_file_names. */
2804
2805 static hashval_t
2806 hash_file_name_entry (const void *e)
2807 {
2808 const struct quick_file_names *file_data
2809 = (const struct quick_file_names *) e;
2810
2811 return hash_stmt_list_entry (&file_data->hash);
2812 }
2813
2814 /* Equality function for a quick_file_names. */
2815
2816 static int
2817 eq_file_name_entry (const void *a, const void *b)
2818 {
2819 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2820 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2821
2822 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2823 }
2824
2825 /* Delete function for a quick_file_names. */
2826
2827 static void
2828 delete_file_name_entry (void *e)
2829 {
2830 struct quick_file_names *file_data = (struct quick_file_names *) e;
2831 int i;
2832
2833 for (i = 0; i < file_data->num_file_names; ++i)
2834 {
2835 xfree ((void*) file_data->file_names[i]);
2836 if (file_data->real_names)
2837 xfree ((void*) file_data->real_names[i]);
2838 }
2839
2840 /* The space for the struct itself lives on objfile_obstack,
2841 so we don't free it here. */
2842 }
2843
2844 /* Create a quick_file_names hash table. */
2845
2846 static htab_t
2847 create_quick_file_names_table (unsigned int nr_initial_entries)
2848 {
2849 return htab_create_alloc (nr_initial_entries,
2850 hash_file_name_entry, eq_file_name_entry,
2851 delete_file_name_entry, xcalloc, xfree);
2852 }
2853
2854 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2855 have to be created afterwards. You should call age_cached_comp_units after
2856 processing PER_CU->CU. dw2_setup must have been already called. */
2857
2858 static void
2859 load_cu (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2860 {
2861 if (per_cu->is_debug_types)
2862 load_full_type_unit (per_cu);
2863 else
2864 load_full_comp_unit (per_cu, skip_partial, language_minimal);
2865
2866 if (per_cu->cu == NULL)
2867 return; /* Dummy CU. */
2868
2869 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2870 }
2871
2872 /* Read in the symbols for PER_CU. */
2873
2874 static void
2875 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2876 {
2877 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2878
2879 /* Skip type_unit_groups, reading the type units they contain
2880 is handled elsewhere. */
2881 if (IS_TYPE_UNIT_GROUP (per_cu))
2882 return;
2883
2884 /* The destructor of dwarf2_queue_guard frees any entries left on
2885 the queue. After this point we're guaranteed to leave this function
2886 with the dwarf queue empty. */
2887 dwarf2_queue_guard q_guard;
2888
2889 if (dwarf2_per_objfile->using_index
2890 ? per_cu->v.quick->compunit_symtab == NULL
2891 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2892 {
2893 queue_comp_unit (per_cu, language_minimal);
2894 load_cu (per_cu, skip_partial);
2895
2896 /* If we just loaded a CU from a DWO, and we're working with an index
2897 that may badly handle TUs, load all the TUs in that DWO as well.
2898 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2899 if (!per_cu->is_debug_types
2900 && per_cu->cu != NULL
2901 && per_cu->cu->dwo_unit != NULL
2902 && dwarf2_per_objfile->index_table != NULL
2903 && dwarf2_per_objfile->index_table->version <= 7
2904 /* DWP files aren't supported yet. */
2905 && get_dwp_file (dwarf2_per_objfile) == NULL)
2906 queue_and_load_all_dwo_tus (per_cu);
2907 }
2908
2909 process_queue (dwarf2_per_objfile);
2910
2911 /* Age the cache, releasing compilation units that have not
2912 been used recently. */
2913 age_cached_comp_units (dwarf2_per_objfile);
2914 }
2915
2916 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2917 the objfile from which this CU came. Returns the resulting symbol
2918 table. */
2919
2920 static struct compunit_symtab *
2921 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2922 {
2923 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2924
2925 gdb_assert (dwarf2_per_objfile->using_index);
2926 if (!per_cu->v.quick->compunit_symtab)
2927 {
2928 free_cached_comp_units freer (dwarf2_per_objfile);
2929 scoped_restore decrementer = increment_reading_symtab ();
2930 dw2_do_instantiate_symtab (per_cu, skip_partial);
2931 process_cu_includes (dwarf2_per_objfile);
2932 }
2933
2934 return per_cu->v.quick->compunit_symtab;
2935 }
2936
2937 /* See declaration. */
2938
2939 dwarf2_per_cu_data *
2940 dwarf2_per_objfile::get_cutu (int index)
2941 {
2942 if (index >= this->all_comp_units.size ())
2943 {
2944 index -= this->all_comp_units.size ();
2945 gdb_assert (index < this->all_type_units.size ());
2946 return &this->all_type_units[index]->per_cu;
2947 }
2948
2949 return this->all_comp_units[index];
2950 }
2951
2952 /* See declaration. */
2953
2954 dwarf2_per_cu_data *
2955 dwarf2_per_objfile::get_cu (int index)
2956 {
2957 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
2958
2959 return this->all_comp_units[index];
2960 }
2961
2962 /* See declaration. */
2963
2964 signatured_type *
2965 dwarf2_per_objfile::get_tu (int index)
2966 {
2967 gdb_assert (index >= 0 && index < this->all_type_units.size ());
2968
2969 return this->all_type_units[index];
2970 }
2971
2972 /* Return a new dwarf2_per_cu_data allocated on OBJFILE's
2973 objfile_obstack, and constructed with the specified field
2974 values. */
2975
2976 static dwarf2_per_cu_data *
2977 create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2978 struct dwarf2_section_info *section,
2979 int is_dwz,
2980 sect_offset sect_off, ULONGEST length)
2981 {
2982 struct objfile *objfile = dwarf2_per_objfile->objfile;
2983 dwarf2_per_cu_data *the_cu
2984 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2985 struct dwarf2_per_cu_data);
2986 the_cu->sect_off = sect_off;
2987 the_cu->length = length;
2988 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
2989 the_cu->section = section;
2990 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2991 struct dwarf2_per_cu_quick_data);
2992 the_cu->is_dwz = is_dwz;
2993 return the_cu;
2994 }
2995
2996 /* A helper for create_cus_from_index that handles a given list of
2997 CUs. */
2998
2999 static void
3000 create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
3001 const gdb_byte *cu_list, offset_type n_elements,
3002 struct dwarf2_section_info *section,
3003 int is_dwz)
3004 {
3005 for (offset_type i = 0; i < n_elements; i += 2)
3006 {
3007 gdb_static_assert (sizeof (ULONGEST) >= 8);
3008
3009 sect_offset sect_off
3010 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
3011 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
3012 cu_list += 2 * 8;
3013
3014 dwarf2_per_cu_data *per_cu
3015 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
3016 sect_off, length);
3017 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
3018 }
3019 }
3020
3021 /* Read the CU list from the mapped index, and use it to create all
3022 the CU objects for this objfile. */
3023
3024 static void
3025 create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
3026 const gdb_byte *cu_list, offset_type cu_list_elements,
3027 const gdb_byte *dwz_list, offset_type dwz_elements)
3028 {
3029 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
3030 dwarf2_per_objfile->all_comp_units.reserve
3031 ((cu_list_elements + dwz_elements) / 2);
3032
3033 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
3034 &dwarf2_per_objfile->info, 0);
3035
3036 if (dwz_elements == 0)
3037 return;
3038
3039 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3040 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
3041 &dwz->info, 1);
3042 }
3043
3044 /* Create the signatured type hash table from the index. */
3045
3046 static void
3047 create_signatured_type_table_from_index
3048 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3049 struct dwarf2_section_info *section,
3050 const gdb_byte *bytes,
3051 offset_type elements)
3052 {
3053 struct objfile *objfile = dwarf2_per_objfile->objfile;
3054
3055 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
3056 dwarf2_per_objfile->all_type_units.reserve (elements / 3);
3057
3058 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
3059
3060 for (offset_type i = 0; i < elements; i += 3)
3061 {
3062 struct signatured_type *sig_type;
3063 ULONGEST signature;
3064 void **slot;
3065 cu_offset type_offset_in_tu;
3066
3067 gdb_static_assert (sizeof (ULONGEST) >= 8);
3068 sect_offset sect_off
3069 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
3070 type_offset_in_tu
3071 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
3072 BFD_ENDIAN_LITTLE);
3073 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
3074 bytes += 3 * 8;
3075
3076 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3077 struct signatured_type);
3078 sig_type->signature = signature;
3079 sig_type->type_offset_in_tu = type_offset_in_tu;
3080 sig_type->per_cu.is_debug_types = 1;
3081 sig_type->per_cu.section = section;
3082 sig_type->per_cu.sect_off = sect_off;
3083 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3084 sig_type->per_cu.v.quick
3085 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3086 struct dwarf2_per_cu_quick_data);
3087
3088 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3089 *slot = sig_type;
3090
3091 dwarf2_per_objfile->all_type_units.push_back (sig_type);
3092 }
3093
3094 dwarf2_per_objfile->signatured_types = sig_types_hash;
3095 }
3096
3097 /* Create the signatured type hash table from .debug_names. */
3098
3099 static void
3100 create_signatured_type_table_from_debug_names
3101 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3102 const mapped_debug_names &map,
3103 struct dwarf2_section_info *section,
3104 struct dwarf2_section_info *abbrev_section)
3105 {
3106 struct objfile *objfile = dwarf2_per_objfile->objfile;
3107
3108 dwarf2_read_section (objfile, section);
3109 dwarf2_read_section (objfile, abbrev_section);
3110
3111 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
3112 dwarf2_per_objfile->all_type_units.reserve (map.tu_count);
3113
3114 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
3115
3116 for (uint32_t i = 0; i < map.tu_count; ++i)
3117 {
3118 struct signatured_type *sig_type;
3119 void **slot;
3120
3121 sect_offset sect_off
3122 = (sect_offset) (extract_unsigned_integer
3123 (map.tu_table_reordered + i * map.offset_size,
3124 map.offset_size,
3125 map.dwarf5_byte_order));
3126
3127 comp_unit_head cu_header;
3128 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
3129 abbrev_section,
3130 section->buffer + to_underlying (sect_off),
3131 rcuh_kind::TYPE);
3132
3133 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3134 struct signatured_type);
3135 sig_type->signature = cu_header.signature;
3136 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
3137 sig_type->per_cu.is_debug_types = 1;
3138 sig_type->per_cu.section = section;
3139 sig_type->per_cu.sect_off = sect_off;
3140 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3141 sig_type->per_cu.v.quick
3142 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3143 struct dwarf2_per_cu_quick_data);
3144
3145 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3146 *slot = sig_type;
3147
3148 dwarf2_per_objfile->all_type_units.push_back (sig_type);
3149 }
3150
3151 dwarf2_per_objfile->signatured_types = sig_types_hash;
3152 }
3153
3154 /* Read the address map data from the mapped index, and use it to
3155 populate the objfile's psymtabs_addrmap. */
3156
3157 static void
3158 create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
3159 struct mapped_index *index)
3160 {
3161 struct objfile *objfile = dwarf2_per_objfile->objfile;
3162 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3163 const gdb_byte *iter, *end;
3164 struct addrmap *mutable_map;
3165 CORE_ADDR baseaddr;
3166
3167 auto_obstack temp_obstack;
3168
3169 mutable_map = addrmap_create_mutable (&temp_obstack);
3170
3171 iter = index->address_table.data ();
3172 end = iter + index->address_table.size ();
3173
3174 baseaddr = objfile->section_offsets[SECT_OFF_TEXT (objfile)];
3175
3176 while (iter < end)
3177 {
3178 ULONGEST hi, lo, cu_index;
3179 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3180 iter += 8;
3181 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3182 iter += 8;
3183 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
3184 iter += 4;
3185
3186 if (lo > hi)
3187 {
3188 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
3189 hex_string (lo), hex_string (hi));
3190 continue;
3191 }
3192
3193 if (cu_index >= dwarf2_per_objfile->all_comp_units.size ())
3194 {
3195 complaint (_(".gdb_index address table has invalid CU number %u"),
3196 (unsigned) cu_index);
3197 continue;
3198 }
3199
3200 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
3201 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
3202 addrmap_set_empty (mutable_map, lo, hi - 1,
3203 dwarf2_per_objfile->get_cu (cu_index));
3204 }
3205
3206 objfile->partial_symtabs->psymtabs_addrmap
3207 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
3208 }
3209
3210 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
3211 populate the objfile's psymtabs_addrmap. */
3212
3213 static void
3214 create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
3215 struct dwarf2_section_info *section)
3216 {
3217 struct objfile *objfile = dwarf2_per_objfile->objfile;
3218 bfd *abfd = objfile->obfd;
3219 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3220 const CORE_ADDR baseaddr = objfile->section_offsets[SECT_OFF_TEXT (objfile)];
3221
3222 auto_obstack temp_obstack;
3223 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
3224
3225 std::unordered_map<sect_offset,
3226 dwarf2_per_cu_data *,
3227 gdb::hash_enum<sect_offset>>
3228 debug_info_offset_to_per_cu;
3229 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3230 {
3231 const auto insertpair
3232 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
3233 if (!insertpair.second)
3234 {
3235 warning (_("Section .debug_aranges in %s has duplicate "
3236 "debug_info_offset %s, ignoring .debug_aranges."),
3237 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
3238 return;
3239 }
3240 }
3241
3242 dwarf2_read_section (objfile, section);
3243
3244 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
3245
3246 const gdb_byte *addr = section->buffer;
3247
3248 while (addr < section->buffer + section->size)
3249 {
3250 const gdb_byte *const entry_addr = addr;
3251 unsigned int bytes_read;
3252
3253 const LONGEST entry_length = read_initial_length (abfd, addr,
3254 &bytes_read);
3255 addr += bytes_read;
3256
3257 const gdb_byte *const entry_end = addr + entry_length;
3258 const bool dwarf5_is_dwarf64 = bytes_read != 4;
3259 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
3260 if (addr + entry_length > section->buffer + section->size)
3261 {
3262 warning (_("Section .debug_aranges in %s entry at offset %s "
3263 "length %s exceeds section length %s, "
3264 "ignoring .debug_aranges."),
3265 objfile_name (objfile),
3266 plongest (entry_addr - section->buffer),
3267 plongest (bytes_read + entry_length),
3268 pulongest (section->size));
3269 return;
3270 }
3271
3272 /* The version number. */
3273 const uint16_t version = read_2_bytes (abfd, addr);
3274 addr += 2;
3275 if (version != 2)
3276 {
3277 warning (_("Section .debug_aranges in %s entry at offset %s "
3278 "has unsupported version %d, ignoring .debug_aranges."),
3279 objfile_name (objfile),
3280 plongest (entry_addr - section->buffer), version);
3281 return;
3282 }
3283
3284 const uint64_t debug_info_offset
3285 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
3286 addr += offset_size;
3287 const auto per_cu_it
3288 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
3289 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
3290 {
3291 warning (_("Section .debug_aranges in %s entry at offset %s "
3292 "debug_info_offset %s does not exists, "
3293 "ignoring .debug_aranges."),
3294 objfile_name (objfile),
3295 plongest (entry_addr - section->buffer),
3296 pulongest (debug_info_offset));
3297 return;
3298 }
3299 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
3300
3301 const uint8_t address_size = *addr++;
3302 if (address_size < 1 || address_size > 8)
3303 {
3304 warning (_("Section .debug_aranges in %s entry at offset %s "
3305 "address_size %u is invalid, ignoring .debug_aranges."),
3306 objfile_name (objfile),
3307 plongest (entry_addr - section->buffer), address_size);
3308 return;
3309 }
3310
3311 const uint8_t segment_selector_size = *addr++;
3312 if (segment_selector_size != 0)
3313 {
3314 warning (_("Section .debug_aranges in %s entry at offset %s "
3315 "segment_selector_size %u is not supported, "
3316 "ignoring .debug_aranges."),
3317 objfile_name (objfile),
3318 plongest (entry_addr - section->buffer),
3319 segment_selector_size);
3320 return;
3321 }
3322
3323 /* Must pad to an alignment boundary that is twice the address
3324 size. It is undocumented by the DWARF standard but GCC does
3325 use it. */
3326 for (size_t padding = ((-(addr - section->buffer))
3327 & (2 * address_size - 1));
3328 padding > 0; padding--)
3329 if (*addr++ != 0)
3330 {
3331 warning (_("Section .debug_aranges in %s entry at offset %s "
3332 "padding is not zero, ignoring .debug_aranges."),
3333 objfile_name (objfile),
3334 plongest (entry_addr - section->buffer));
3335 return;
3336 }
3337
3338 for (;;)
3339 {
3340 if (addr + 2 * address_size > entry_end)
3341 {
3342 warning (_("Section .debug_aranges in %s entry at offset %s "
3343 "address list is not properly terminated, "
3344 "ignoring .debug_aranges."),
3345 objfile_name (objfile),
3346 plongest (entry_addr - section->buffer));
3347 return;
3348 }
3349 ULONGEST start = extract_unsigned_integer (addr, address_size,
3350 dwarf5_byte_order);
3351 addr += address_size;
3352 ULONGEST length = extract_unsigned_integer (addr, address_size,
3353 dwarf5_byte_order);
3354 addr += address_size;
3355 if (start == 0 && length == 0)
3356 break;
3357 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
3358 {
3359 /* Symbol was eliminated due to a COMDAT group. */
3360 continue;
3361 }
3362 ULONGEST end = start + length;
3363 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
3364 - baseaddr);
3365 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
3366 - baseaddr);
3367 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
3368 }
3369 }
3370
3371 objfile->partial_symtabs->psymtabs_addrmap
3372 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
3373 }
3374
3375 /* Find a slot in the mapped index INDEX for the object named NAME.
3376 If NAME is found, set *VEC_OUT to point to the CU vector in the
3377 constant pool and return true. If NAME cannot be found, return
3378 false. */
3379
3380 static bool
3381 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3382 offset_type **vec_out)
3383 {
3384 offset_type hash;
3385 offset_type slot, step;
3386 int (*cmp) (const char *, const char *);
3387
3388 gdb::unique_xmalloc_ptr<char> without_params;
3389 if (current_language->la_language == language_cplus
3390 || current_language->la_language == language_fortran
3391 || current_language->la_language == language_d)
3392 {
3393 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3394 not contain any. */
3395
3396 if (strchr (name, '(') != NULL)
3397 {
3398 without_params = cp_remove_params (name);
3399
3400 if (without_params != NULL)
3401 name = without_params.get ();
3402 }
3403 }
3404
3405 /* Index version 4 did not support case insensitive searches. But the
3406 indices for case insensitive languages are built in lowercase, therefore
3407 simulate our NAME being searched is also lowercased. */
3408 hash = mapped_index_string_hash ((index->version == 4
3409 && case_sensitivity == case_sensitive_off
3410 ? 5 : index->version),
3411 name);
3412
3413 slot = hash & (index->symbol_table.size () - 1);
3414 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
3415 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
3416
3417 for (;;)
3418 {
3419 const char *str;
3420
3421 const auto &bucket = index->symbol_table[slot];
3422 if (bucket.name == 0 && bucket.vec == 0)
3423 return false;
3424
3425 str = index->constant_pool + MAYBE_SWAP (bucket.name);
3426 if (!cmp (name, str))
3427 {
3428 *vec_out = (offset_type *) (index->constant_pool
3429 + MAYBE_SWAP (bucket.vec));
3430 return true;
3431 }
3432
3433 slot = (slot + step) & (index->symbol_table.size () - 1);
3434 }
3435 }
3436
3437 /* A helper function that reads the .gdb_index from BUFFER and fills
3438 in MAP. FILENAME is the name of the file containing the data;
3439 it is used for error reporting. DEPRECATED_OK is true if it is
3440 ok to use deprecated sections.
3441
3442 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3443 out parameters that are filled in with information about the CU and
3444 TU lists in the section.
3445
3446 Returns true if all went well, false otherwise. */
3447
3448 static bool
3449 read_gdb_index_from_buffer (struct objfile *objfile,
3450 const char *filename,
3451 bool deprecated_ok,
3452 gdb::array_view<const gdb_byte> buffer,
3453 struct mapped_index *map,
3454 const gdb_byte **cu_list,
3455 offset_type *cu_list_elements,
3456 const gdb_byte **types_list,
3457 offset_type *types_list_elements)
3458 {
3459 const gdb_byte *addr = &buffer[0];
3460
3461 /* Version check. */
3462 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
3463 /* Versions earlier than 3 emitted every copy of a psymbol. This
3464 causes the index to behave very poorly for certain requests. Version 3
3465 contained incomplete addrmap. So, it seems better to just ignore such
3466 indices. */
3467 if (version < 4)
3468 {
3469 static int warning_printed = 0;
3470 if (!warning_printed)
3471 {
3472 warning (_("Skipping obsolete .gdb_index section in %s."),
3473 filename);
3474 warning_printed = 1;
3475 }
3476 return 0;
3477 }
3478 /* Index version 4 uses a different hash function than index version
3479 5 and later.
3480
3481 Versions earlier than 6 did not emit psymbols for inlined
3482 functions. Using these files will cause GDB not to be able to
3483 set breakpoints on inlined functions by name, so we ignore these
3484 indices unless the user has done
3485 "set use-deprecated-index-sections on". */
3486 if (version < 6 && !deprecated_ok)
3487 {
3488 static int warning_printed = 0;
3489 if (!warning_printed)
3490 {
3491 warning (_("\
3492 Skipping deprecated .gdb_index section in %s.\n\
3493 Do \"set use-deprecated-index-sections on\" before the file is read\n\
3494 to use the section anyway."),
3495 filename);
3496 warning_printed = 1;
3497 }
3498 return 0;
3499 }
3500 /* Version 7 indices generated by gold refer to the CU for a symbol instead
3501 of the TU (for symbols coming from TUs),
3502 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3503 Plus gold-generated indices can have duplicate entries for global symbols,
3504 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3505 These are just performance bugs, and we can't distinguish gdb-generated
3506 indices from gold-generated ones, so issue no warning here. */
3507
3508 /* Indexes with higher version than the one supported by GDB may be no
3509 longer backward compatible. */
3510 if (version > 8)
3511 return 0;
3512
3513 map->version = version;
3514
3515 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
3516
3517 int i = 0;
3518 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3519 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3520 / 8);
3521 ++i;
3522
3523 *types_list = addr + MAYBE_SWAP (metadata[i]);
3524 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3525 - MAYBE_SWAP (metadata[i]))
3526 / 8);
3527 ++i;
3528
3529 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3530 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3531 map->address_table
3532 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
3533 ++i;
3534
3535 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3536 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3537 map->symbol_table
3538 = gdb::array_view<mapped_index::symbol_table_slot>
3539 ((mapped_index::symbol_table_slot *) symbol_table,
3540 (mapped_index::symbol_table_slot *) symbol_table_end);
3541
3542 ++i;
3543 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
3544
3545 return 1;
3546 }
3547
3548 /* Callback types for dwarf2_read_gdb_index. */
3549
3550 typedef gdb::function_view
3551 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_objfile *)>
3552 get_gdb_index_contents_ftype;
3553 typedef gdb::function_view
3554 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3555 get_gdb_index_contents_dwz_ftype;
3556
3557 /* Read .gdb_index. If everything went ok, initialize the "quick"
3558 elements of all the CUs and return 1. Otherwise, return 0. */
3559
3560 static int
3561 dwarf2_read_gdb_index
3562 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3563 get_gdb_index_contents_ftype get_gdb_index_contents,
3564 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
3565 {
3566 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3567 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3568 struct dwz_file *dwz;
3569 struct objfile *objfile = dwarf2_per_objfile->objfile;
3570
3571 gdb::array_view<const gdb_byte> main_index_contents
3572 = get_gdb_index_contents (objfile, dwarf2_per_objfile);
3573
3574 if (main_index_contents.empty ())
3575 return 0;
3576
3577 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3578 if (!read_gdb_index_from_buffer (objfile, objfile_name (objfile),
3579 use_deprecated_index_sections,
3580 main_index_contents, map.get (), &cu_list,
3581 &cu_list_elements, &types_list,
3582 &types_list_elements))
3583 return 0;
3584
3585 /* Don't use the index if it's empty. */
3586 if (map->symbol_table.empty ())
3587 return 0;
3588
3589 /* If there is a .dwz file, read it so we can get its CU list as
3590 well. */
3591 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3592 if (dwz != NULL)
3593 {
3594 struct mapped_index dwz_map;
3595 const gdb_byte *dwz_types_ignore;
3596 offset_type dwz_types_elements_ignore;
3597
3598 gdb::array_view<const gdb_byte> dwz_index_content
3599 = get_gdb_index_contents_dwz (objfile, dwz);
3600
3601 if (dwz_index_content.empty ())
3602 return 0;
3603
3604 if (!read_gdb_index_from_buffer (objfile,
3605 bfd_get_filename (dwz->dwz_bfd.get ()),
3606 1, dwz_index_content, &dwz_map,
3607 &dwz_list, &dwz_list_elements,
3608 &dwz_types_ignore,
3609 &dwz_types_elements_ignore))
3610 {
3611 warning (_("could not read '.gdb_index' section from %s; skipping"),
3612 bfd_get_filename (dwz->dwz_bfd.get ()));
3613 return 0;
3614 }
3615 }
3616
3617 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3618 dwz_list, dwz_list_elements);
3619
3620 if (types_list_elements)
3621 {
3622 /* We can only handle a single .debug_types when we have an
3623 index. */
3624 if (dwarf2_per_objfile->types.size () != 1)
3625 return 0;
3626
3627 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
3628
3629 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3630 types_list, types_list_elements);
3631 }
3632
3633 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
3634
3635 dwarf2_per_objfile->index_table = std::move (map);
3636 dwarf2_per_objfile->using_index = 1;
3637 dwarf2_per_objfile->quick_file_names_table =
3638 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
3639
3640 return 1;
3641 }
3642
3643 /* die_reader_func for dw2_get_file_names. */
3644
3645 static void
3646 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3647 const gdb_byte *info_ptr,
3648 struct die_info *comp_unit_die,
3649 int has_children,
3650 void *data)
3651 {
3652 struct dwarf2_cu *cu = reader->cu;
3653 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3654 struct dwarf2_per_objfile *dwarf2_per_objfile
3655 = cu->per_cu->dwarf2_per_objfile;
3656 struct objfile *objfile = dwarf2_per_objfile->objfile;
3657 struct dwarf2_per_cu_data *lh_cu;
3658 struct attribute *attr;
3659 void **slot;
3660 struct quick_file_names *qfn;
3661
3662 gdb_assert (! this_cu->is_debug_types);
3663
3664 /* Our callers never want to match partial units -- instead they
3665 will match the enclosing full CU. */
3666 if (comp_unit_die->tag == DW_TAG_partial_unit)
3667 {
3668 this_cu->v.quick->no_file_data = 1;
3669 return;
3670 }
3671
3672 lh_cu = this_cu;
3673 slot = NULL;
3674
3675 line_header_up lh;
3676 sect_offset line_offset {};
3677
3678 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3679 if (attr != nullptr)
3680 {
3681 struct quick_file_names find_entry;
3682
3683 line_offset = (sect_offset) DW_UNSND (attr);
3684
3685 /* We may have already read in this line header (TU line header sharing).
3686 If we have we're done. */
3687 find_entry.hash.dwo_unit = cu->dwo_unit;
3688 find_entry.hash.line_sect_off = line_offset;
3689 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3690 &find_entry, INSERT);
3691 if (*slot != NULL)
3692 {
3693 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3694 return;
3695 }
3696
3697 lh = dwarf_decode_line_header (line_offset, cu);
3698 }
3699 if (lh == NULL)
3700 {
3701 lh_cu->v.quick->no_file_data = 1;
3702 return;
3703 }
3704
3705 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
3706 qfn->hash.dwo_unit = cu->dwo_unit;
3707 qfn->hash.line_sect_off = line_offset;
3708 gdb_assert (slot != NULL);
3709 *slot = qfn;
3710
3711 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3712
3713 int offset = 0;
3714 if (strcmp (fnd.name, "<unknown>") != 0)
3715 ++offset;
3716
3717 qfn->num_file_names = offset + lh->file_names_size ();
3718 qfn->file_names =
3719 XOBNEWVEC (&objfile->objfile_obstack, const char *, qfn->num_file_names);
3720 if (offset != 0)
3721 qfn->file_names[0] = xstrdup (fnd.name);
3722 for (int i = 0; i < lh->file_names_size (); ++i)
3723 qfn->file_names[i + offset] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
3724 qfn->real_names = NULL;
3725
3726 lh_cu->v.quick->file_names = qfn;
3727 }
3728
3729 /* A helper for the "quick" functions which attempts to read the line
3730 table for THIS_CU. */
3731
3732 static struct quick_file_names *
3733 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
3734 {
3735 /* This should never be called for TUs. */
3736 gdb_assert (! this_cu->is_debug_types);
3737 /* Nor type unit groups. */
3738 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
3739
3740 if (this_cu->v.quick->file_names != NULL)
3741 return this_cu->v.quick->file_names;
3742 /* If we know there is no line data, no point in looking again. */
3743 if (this_cu->v.quick->no_file_data)
3744 return NULL;
3745
3746 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
3747
3748 if (this_cu->v.quick->no_file_data)
3749 return NULL;
3750 return this_cu->v.quick->file_names;
3751 }
3752
3753 /* A helper for the "quick" functions which computes and caches the
3754 real path for a given file name from the line table. */
3755
3756 static const char *
3757 dw2_get_real_path (struct objfile *objfile,
3758 struct quick_file_names *qfn, int index)
3759 {
3760 if (qfn->real_names == NULL)
3761 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
3762 qfn->num_file_names, const char *);
3763
3764 if (qfn->real_names[index] == NULL)
3765 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3766
3767 return qfn->real_names[index];
3768 }
3769
3770 static struct symtab *
3771 dw2_find_last_source_symtab (struct objfile *objfile)
3772 {
3773 struct dwarf2_per_objfile *dwarf2_per_objfile
3774 = get_dwarf2_per_objfile (objfile);
3775 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->all_comp_units.back ();
3776 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, false);
3777
3778 if (cust == NULL)
3779 return NULL;
3780
3781 return compunit_primary_filetab (cust);
3782 }
3783
3784 /* Traversal function for dw2_forget_cached_source_info. */
3785
3786 static int
3787 dw2_free_cached_file_names (void **slot, void *info)
3788 {
3789 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3790
3791 if (file_data->real_names)
3792 {
3793 int i;
3794
3795 for (i = 0; i < file_data->num_file_names; ++i)
3796 {
3797 xfree ((void*) file_data->real_names[i]);
3798 file_data->real_names[i] = NULL;
3799 }
3800 }
3801
3802 return 1;
3803 }
3804
3805 static void
3806 dw2_forget_cached_source_info (struct objfile *objfile)
3807 {
3808 struct dwarf2_per_objfile *dwarf2_per_objfile
3809 = get_dwarf2_per_objfile (objfile);
3810
3811 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3812 dw2_free_cached_file_names, NULL);
3813 }
3814
3815 /* Helper function for dw2_map_symtabs_matching_filename that expands
3816 the symtabs and calls the iterator. */
3817
3818 static int
3819 dw2_map_expand_apply (struct objfile *objfile,
3820 struct dwarf2_per_cu_data *per_cu,
3821 const char *name, const char *real_path,
3822 gdb::function_view<bool (symtab *)> callback)
3823 {
3824 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3825
3826 /* Don't visit already-expanded CUs. */
3827 if (per_cu->v.quick->compunit_symtab)
3828 return 0;
3829
3830 /* This may expand more than one symtab, and we want to iterate over
3831 all of them. */
3832 dw2_instantiate_symtab (per_cu, false);
3833
3834 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3835 last_made, callback);
3836 }
3837
3838 /* Implementation of the map_symtabs_matching_filename method. */
3839
3840 static bool
3841 dw2_map_symtabs_matching_filename
3842 (struct objfile *objfile, const char *name, const char *real_path,
3843 gdb::function_view<bool (symtab *)> callback)
3844 {
3845 const char *name_basename = lbasename (name);
3846 struct dwarf2_per_objfile *dwarf2_per_objfile
3847 = get_dwarf2_per_objfile (objfile);
3848
3849 /* The rule is CUs specify all the files, including those used by
3850 any TU, so there's no need to scan TUs here. */
3851
3852 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3853 {
3854 /* We only need to look at symtabs not already expanded. */
3855 if (per_cu->v.quick->compunit_symtab)
3856 continue;
3857
3858 quick_file_names *file_data = dw2_get_file_names (per_cu);
3859 if (file_data == NULL)
3860 continue;
3861
3862 for (int j = 0; j < file_data->num_file_names; ++j)
3863 {
3864 const char *this_name = file_data->file_names[j];
3865 const char *this_real_name;
3866
3867 if (compare_filenames_for_search (this_name, name))
3868 {
3869 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3870 callback))
3871 return true;
3872 continue;
3873 }
3874
3875 /* Before we invoke realpath, which can get expensive when many
3876 files are involved, do a quick comparison of the basenames. */
3877 if (! basenames_may_differ
3878 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3879 continue;
3880
3881 this_real_name = dw2_get_real_path (objfile, file_data, j);
3882 if (compare_filenames_for_search (this_real_name, name))
3883 {
3884 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3885 callback))
3886 return true;
3887 continue;
3888 }
3889
3890 if (real_path != NULL)
3891 {
3892 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3893 gdb_assert (IS_ABSOLUTE_PATH (name));
3894 if (this_real_name != NULL
3895 && FILENAME_CMP (real_path, this_real_name) == 0)
3896 {
3897 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3898 callback))
3899 return true;
3900 continue;
3901 }
3902 }
3903 }
3904 }
3905
3906 return false;
3907 }
3908
3909 /* Struct used to manage iterating over all CUs looking for a symbol. */
3910
3911 struct dw2_symtab_iterator
3912 {
3913 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3914 struct dwarf2_per_objfile *dwarf2_per_objfile;
3915 /* If set, only look for symbols that match that block. Valid values are
3916 GLOBAL_BLOCK and STATIC_BLOCK. */
3917 gdb::optional<block_enum> block_index;
3918 /* The kind of symbol we're looking for. */
3919 domain_enum domain;
3920 /* The list of CUs from the index entry of the symbol,
3921 or NULL if not found. */
3922 offset_type *vec;
3923 /* The next element in VEC to look at. */
3924 int next;
3925 /* The number of elements in VEC, or zero if there is no match. */
3926 int length;
3927 /* Have we seen a global version of the symbol?
3928 If so we can ignore all further global instances.
3929 This is to work around gold/15646, inefficient gold-generated
3930 indices. */
3931 int global_seen;
3932 };
3933
3934 /* Initialize the index symtab iterator ITER. */
3935
3936 static void
3937 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3938 struct dwarf2_per_objfile *dwarf2_per_objfile,
3939 gdb::optional<block_enum> block_index,
3940 domain_enum domain,
3941 const char *name)
3942 {
3943 iter->dwarf2_per_objfile = dwarf2_per_objfile;
3944 iter->block_index = block_index;
3945 iter->domain = domain;
3946 iter->next = 0;
3947 iter->global_seen = 0;
3948
3949 mapped_index *index = dwarf2_per_objfile->index_table.get ();
3950
3951 /* index is NULL if OBJF_READNOW. */
3952 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
3953 iter->length = MAYBE_SWAP (*iter->vec);
3954 else
3955 {
3956 iter->vec = NULL;
3957 iter->length = 0;
3958 }
3959 }
3960
3961 /* Return the next matching CU or NULL if there are no more. */
3962
3963 static struct dwarf2_per_cu_data *
3964 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3965 {
3966 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3967
3968 for ( ; iter->next < iter->length; ++iter->next)
3969 {
3970 offset_type cu_index_and_attrs =
3971 MAYBE_SWAP (iter->vec[iter->next + 1]);
3972 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3973 gdb_index_symbol_kind symbol_kind =
3974 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3975 /* Only check the symbol attributes if they're present.
3976 Indices prior to version 7 don't record them,
3977 and indices >= 7 may elide them for certain symbols
3978 (gold does this). */
3979 int attrs_valid =
3980 (dwarf2_per_objfile->index_table->version >= 7
3981 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3982
3983 /* Don't crash on bad data. */
3984 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
3985 + dwarf2_per_objfile->all_type_units.size ()))
3986 {
3987 complaint (_(".gdb_index entry has bad CU index"
3988 " [in module %s]"),
3989 objfile_name (dwarf2_per_objfile->objfile));
3990 continue;
3991 }
3992
3993 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
3994
3995 /* Skip if already read in. */
3996 if (per_cu->v.quick->compunit_symtab)
3997 continue;
3998
3999 /* Check static vs global. */
4000 if (attrs_valid)
4001 {
4002 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4003
4004 if (iter->block_index.has_value ())
4005 {
4006 bool want_static = *iter->block_index == STATIC_BLOCK;
4007
4008 if (is_static != want_static)
4009 continue;
4010 }
4011
4012 /* Work around gold/15646. */
4013 if (!is_static && iter->global_seen)
4014 continue;
4015 if (!is_static)
4016 iter->global_seen = 1;
4017 }
4018
4019 /* Only check the symbol's kind if it has one. */
4020 if (attrs_valid)
4021 {
4022 switch (iter->domain)
4023 {
4024 case VAR_DOMAIN:
4025 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
4026 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
4027 /* Some types are also in VAR_DOMAIN. */
4028 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4029 continue;
4030 break;
4031 case STRUCT_DOMAIN:
4032 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4033 continue;
4034 break;
4035 case LABEL_DOMAIN:
4036 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4037 continue;
4038 break;
4039 case MODULE_DOMAIN:
4040 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4041 continue;
4042 break;
4043 default:
4044 break;
4045 }
4046 }
4047
4048 ++iter->next;
4049 return per_cu;
4050 }
4051
4052 return NULL;
4053 }
4054
4055 static struct compunit_symtab *
4056 dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
4057 const char *name, domain_enum domain)
4058 {
4059 struct compunit_symtab *stab_best = NULL;
4060 struct dwarf2_per_objfile *dwarf2_per_objfile
4061 = get_dwarf2_per_objfile (objfile);
4062
4063 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
4064
4065 struct dw2_symtab_iterator iter;
4066 struct dwarf2_per_cu_data *per_cu;
4067
4068 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
4069
4070 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4071 {
4072 struct symbol *sym, *with_opaque = NULL;
4073 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
4074 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
4075 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
4076
4077 sym = block_find_symbol (block, name, domain,
4078 block_find_non_opaque_type_preferred,
4079 &with_opaque);
4080
4081 /* Some caution must be observed with overloaded functions
4082 and methods, since the index will not contain any overload
4083 information (but NAME might contain it). */
4084
4085 if (sym != NULL
4086 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
4087 return stab;
4088 if (with_opaque != NULL
4089 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
4090 stab_best = stab;
4091
4092 /* Keep looking through other CUs. */
4093 }
4094
4095 return stab_best;
4096 }
4097
4098 static void
4099 dw2_print_stats (struct objfile *objfile)
4100 {
4101 struct dwarf2_per_objfile *dwarf2_per_objfile
4102 = get_dwarf2_per_objfile (objfile);
4103 int total = (dwarf2_per_objfile->all_comp_units.size ()
4104 + dwarf2_per_objfile->all_type_units.size ());
4105 int count = 0;
4106
4107 for (int i = 0; i < total; ++i)
4108 {
4109 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
4110
4111 if (!per_cu->v.quick->compunit_symtab)
4112 ++count;
4113 }
4114 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
4115 printf_filtered (_(" Number of unread CUs: %d\n"), count);
4116 }
4117
4118 /* This dumps minimal information about the index.
4119 It is called via "mt print objfiles".
4120 One use is to verify .gdb_index has been loaded by the
4121 gdb.dwarf2/gdb-index.exp testcase. */
4122
4123 static void
4124 dw2_dump (struct objfile *objfile)
4125 {
4126 struct dwarf2_per_objfile *dwarf2_per_objfile
4127 = get_dwarf2_per_objfile (objfile);
4128
4129 gdb_assert (dwarf2_per_objfile->using_index);
4130 printf_filtered (".gdb_index:");
4131 if (dwarf2_per_objfile->index_table != NULL)
4132 {
4133 printf_filtered (" version %d\n",
4134 dwarf2_per_objfile->index_table->version);
4135 }
4136 else
4137 printf_filtered (" faked for \"readnow\"\n");
4138 printf_filtered ("\n");
4139 }
4140
4141 static void
4142 dw2_expand_symtabs_for_function (struct objfile *objfile,
4143 const char *func_name)
4144 {
4145 struct dwarf2_per_objfile *dwarf2_per_objfile
4146 = get_dwarf2_per_objfile (objfile);
4147
4148 struct dw2_symtab_iterator iter;
4149 struct dwarf2_per_cu_data *per_cu;
4150
4151 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
4152
4153 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4154 dw2_instantiate_symtab (per_cu, false);
4155
4156 }
4157
4158 static void
4159 dw2_expand_all_symtabs (struct objfile *objfile)
4160 {
4161 struct dwarf2_per_objfile *dwarf2_per_objfile
4162 = get_dwarf2_per_objfile (objfile);
4163 int total_units = (dwarf2_per_objfile->all_comp_units.size ()
4164 + dwarf2_per_objfile->all_type_units.size ());
4165
4166 for (int i = 0; i < total_units; ++i)
4167 {
4168 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
4169
4170 /* We don't want to directly expand a partial CU, because if we
4171 read it with the wrong language, then assertion failures can
4172 be triggered later on. See PR symtab/23010. So, tell
4173 dw2_instantiate_symtab to skip partial CUs -- any important
4174 partial CU will be read via DW_TAG_imported_unit anyway. */
4175 dw2_instantiate_symtab (per_cu, true);
4176 }
4177 }
4178
4179 static void
4180 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
4181 const char *fullname)
4182 {
4183 struct dwarf2_per_objfile *dwarf2_per_objfile
4184 = get_dwarf2_per_objfile (objfile);
4185
4186 /* We don't need to consider type units here.
4187 This is only called for examining code, e.g. expand_line_sal.
4188 There can be an order of magnitude (or more) more type units
4189 than comp units, and we avoid them if we can. */
4190
4191 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4192 {
4193 /* We only need to look at symtabs not already expanded. */
4194 if (per_cu->v.quick->compunit_symtab)
4195 continue;
4196
4197 quick_file_names *file_data = dw2_get_file_names (per_cu);
4198 if (file_data == NULL)
4199 continue;
4200
4201 for (int j = 0; j < file_data->num_file_names; ++j)
4202 {
4203 const char *this_fullname = file_data->file_names[j];
4204
4205 if (filename_cmp (this_fullname, fullname) == 0)
4206 {
4207 dw2_instantiate_symtab (per_cu, false);
4208 break;
4209 }
4210 }
4211 }
4212 }
4213
4214 static void
4215 dw2_map_matching_symbols
4216 (struct objfile *objfile,
4217 const lookup_name_info &name, domain_enum domain,
4218 int global,
4219 gdb::function_view<symbol_found_callback_ftype> callback,
4220 symbol_compare_ftype *ordered_compare)
4221 {
4222 /* Currently unimplemented; used for Ada. The function can be called if the
4223 current language is Ada for a non-Ada objfile using GNU index. As Ada
4224 does not look for non-Ada symbols this function should just return. */
4225 }
4226
4227 /* Starting from a search name, return the string that finds the upper
4228 bound of all strings that start with SEARCH_NAME in a sorted name
4229 list. Returns the empty string to indicate that the upper bound is
4230 the end of the list. */
4231
4232 static std::string
4233 make_sort_after_prefix_name (const char *search_name)
4234 {
4235 /* When looking to complete "func", we find the upper bound of all
4236 symbols that start with "func" by looking for where we'd insert
4237 the closest string that would follow "func" in lexicographical
4238 order. Usually, that's "func"-with-last-character-incremented,
4239 i.e. "fund". Mind non-ASCII characters, though. Usually those
4240 will be UTF-8 multi-byte sequences, but we can't be certain.
4241 Especially mind the 0xff character, which is a valid character in
4242 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
4243 rule out compilers allowing it in identifiers. Note that
4244 conveniently, strcmp/strcasecmp are specified to compare
4245 characters interpreted as unsigned char. So what we do is treat
4246 the whole string as a base 256 number composed of a sequence of
4247 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
4248 to 0, and carries 1 to the following more-significant position.
4249 If the very first character in SEARCH_NAME ends up incremented
4250 and carries/overflows, then the upper bound is the end of the
4251 list. The string after the empty string is also the empty
4252 string.
4253
4254 Some examples of this operation:
4255
4256 SEARCH_NAME => "+1" RESULT
4257
4258 "abc" => "abd"
4259 "ab\xff" => "ac"
4260 "\xff" "a" "\xff" => "\xff" "b"
4261 "\xff" => ""
4262 "\xff\xff" => ""
4263 "" => ""
4264
4265 Then, with these symbols for example:
4266
4267 func
4268 func1
4269 fund
4270
4271 completing "func" looks for symbols between "func" and
4272 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
4273 which finds "func" and "func1", but not "fund".
4274
4275 And with:
4276
4277 funcÿ (Latin1 'ÿ' [0xff])
4278 funcÿ1
4279 fund
4280
4281 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
4282 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
4283
4284 And with:
4285
4286 ÿÿ (Latin1 'ÿ' [0xff])
4287 ÿÿ1
4288
4289 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
4290 the end of the list.
4291 */
4292 std::string after = search_name;
4293 while (!after.empty () && (unsigned char) after.back () == 0xff)
4294 after.pop_back ();
4295 if (!after.empty ())
4296 after.back () = (unsigned char) after.back () + 1;
4297 return after;
4298 }
4299
4300 /* See declaration. */
4301
4302 std::pair<std::vector<name_component>::const_iterator,
4303 std::vector<name_component>::const_iterator>
4304 mapped_index_base::find_name_components_bounds
4305 (const lookup_name_info &lookup_name_without_params, language lang) const
4306 {
4307 auto *name_cmp
4308 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4309
4310 const char *lang_name
4311 = lookup_name_without_params.language_lookup_name (lang).c_str ();
4312
4313 /* Comparison function object for lower_bound that matches against a
4314 given symbol name. */
4315 auto lookup_compare_lower = [&] (const name_component &elem,
4316 const char *name)
4317 {
4318 const char *elem_qualified = this->symbol_name_at (elem.idx);
4319 const char *elem_name = elem_qualified + elem.name_offset;
4320 return name_cmp (elem_name, name) < 0;
4321 };
4322
4323 /* Comparison function object for upper_bound that matches against a
4324 given symbol name. */
4325 auto lookup_compare_upper = [&] (const char *name,
4326 const name_component &elem)
4327 {
4328 const char *elem_qualified = this->symbol_name_at (elem.idx);
4329 const char *elem_name = elem_qualified + elem.name_offset;
4330 return name_cmp (name, elem_name) < 0;
4331 };
4332
4333 auto begin = this->name_components.begin ();
4334 auto end = this->name_components.end ();
4335
4336 /* Find the lower bound. */
4337 auto lower = [&] ()
4338 {
4339 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
4340 return begin;
4341 else
4342 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
4343 } ();
4344
4345 /* Find the upper bound. */
4346 auto upper = [&] ()
4347 {
4348 if (lookup_name_without_params.completion_mode ())
4349 {
4350 /* In completion mode, we want UPPER to point past all
4351 symbols names that have the same prefix. I.e., with
4352 these symbols, and completing "func":
4353
4354 function << lower bound
4355 function1
4356 other_function << upper bound
4357
4358 We find the upper bound by looking for the insertion
4359 point of "func"-with-last-character-incremented,
4360 i.e. "fund". */
4361 std::string after = make_sort_after_prefix_name (lang_name);
4362 if (after.empty ())
4363 return end;
4364 return std::lower_bound (lower, end, after.c_str (),
4365 lookup_compare_lower);
4366 }
4367 else
4368 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
4369 } ();
4370
4371 return {lower, upper};
4372 }
4373
4374 /* See declaration. */
4375
4376 void
4377 mapped_index_base::build_name_components ()
4378 {
4379 if (!this->name_components.empty ())
4380 return;
4381
4382 this->name_components_casing = case_sensitivity;
4383 auto *name_cmp
4384 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4385
4386 /* The code below only knows how to break apart components of C++
4387 symbol names (and other languages that use '::' as
4388 namespace/module separator) and Ada symbol names. */
4389 auto count = this->symbol_name_count ();
4390 for (offset_type idx = 0; idx < count; idx++)
4391 {
4392 if (this->symbol_name_slot_invalid (idx))
4393 continue;
4394
4395 const char *name = this->symbol_name_at (idx);
4396
4397 /* Add each name component to the name component table. */
4398 unsigned int previous_len = 0;
4399
4400 if (strstr (name, "::") != nullptr)
4401 {
4402 for (unsigned int current_len = cp_find_first_component (name);
4403 name[current_len] != '\0';
4404 current_len += cp_find_first_component (name + current_len))
4405 {
4406 gdb_assert (name[current_len] == ':');
4407 this->name_components.push_back ({previous_len, idx});
4408 /* Skip the '::'. */
4409 current_len += 2;
4410 previous_len = current_len;
4411 }
4412 }
4413 else
4414 {
4415 /* Handle the Ada encoded (aka mangled) form here. */
4416 for (const char *iter = strstr (name, "__");
4417 iter != nullptr;
4418 iter = strstr (iter, "__"))
4419 {
4420 this->name_components.push_back ({previous_len, idx});
4421 iter += 2;
4422 previous_len = iter - name;
4423 }
4424 }
4425
4426 this->name_components.push_back ({previous_len, idx});
4427 }
4428
4429 /* Sort name_components elements by name. */
4430 auto name_comp_compare = [&] (const name_component &left,
4431 const name_component &right)
4432 {
4433 const char *left_qualified = this->symbol_name_at (left.idx);
4434 const char *right_qualified = this->symbol_name_at (right.idx);
4435
4436 const char *left_name = left_qualified + left.name_offset;
4437 const char *right_name = right_qualified + right.name_offset;
4438
4439 return name_cmp (left_name, right_name) < 0;
4440 };
4441
4442 std::sort (this->name_components.begin (),
4443 this->name_components.end (),
4444 name_comp_compare);
4445 }
4446
4447 /* Helper for dw2_expand_symtabs_matching that works with a
4448 mapped_index_base instead of the containing objfile. This is split
4449 to a separate function in order to be able to unit test the
4450 name_components matching using a mock mapped_index_base. For each
4451 symbol name that matches, calls MATCH_CALLBACK, passing it the
4452 symbol's index in the mapped_index_base symbol table. */
4453
4454 static void
4455 dw2_expand_symtabs_matching_symbol
4456 (mapped_index_base &index,
4457 const lookup_name_info &lookup_name_in,
4458 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4459 enum search_domain kind,
4460 gdb::function_view<bool (offset_type)> match_callback)
4461 {
4462 lookup_name_info lookup_name_without_params
4463 = lookup_name_in.make_ignore_params ();
4464
4465 /* Build the symbol name component sorted vector, if we haven't
4466 yet. */
4467 index.build_name_components ();
4468
4469 /* The same symbol may appear more than once in the range though.
4470 E.g., if we're looking for symbols that complete "w", and we have
4471 a symbol named "w1::w2", we'll find the two name components for
4472 that same symbol in the range. To be sure we only call the
4473 callback once per symbol, we first collect the symbol name
4474 indexes that matched in a temporary vector and ignore
4475 duplicates. */
4476 std::vector<offset_type> matches;
4477
4478 struct name_and_matcher
4479 {
4480 symbol_name_matcher_ftype *matcher;
4481 const std::string &name;
4482
4483 bool operator== (const name_and_matcher &other) const
4484 {
4485 return matcher == other.matcher && name == other.name;
4486 }
4487 };
4488
4489 /* A vector holding all the different symbol name matchers, for all
4490 languages. */
4491 std::vector<name_and_matcher> matchers;
4492
4493 for (int i = 0; i < nr_languages; i++)
4494 {
4495 enum language lang_e = (enum language) i;
4496
4497 const language_defn *lang = language_def (lang_e);
4498 symbol_name_matcher_ftype *name_matcher
4499 = get_symbol_name_matcher (lang, lookup_name_without_params);
4500
4501 name_and_matcher key {
4502 name_matcher,
4503 lookup_name_without_params.language_lookup_name (lang_e)
4504 };
4505
4506 /* Don't insert the same comparison routine more than once.
4507 Note that we do this linear walk. This is not a problem in
4508 practice because the number of supported languages is
4509 low. */
4510 if (std::find (matchers.begin (), matchers.end (), key)
4511 != matchers.end ())
4512 continue;
4513 matchers.push_back (std::move (key));
4514
4515 auto bounds
4516 = index.find_name_components_bounds (lookup_name_without_params,
4517 lang_e);
4518
4519 /* Now for each symbol name in range, check to see if we have a name
4520 match, and if so, call the MATCH_CALLBACK callback. */
4521
4522 for (; bounds.first != bounds.second; ++bounds.first)
4523 {
4524 const char *qualified = index.symbol_name_at (bounds.first->idx);
4525
4526 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4527 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4528 continue;
4529
4530 matches.push_back (bounds.first->idx);
4531 }
4532 }
4533
4534 std::sort (matches.begin (), matches.end ());
4535
4536 /* Finally call the callback, once per match. */
4537 ULONGEST prev = -1;
4538 for (offset_type idx : matches)
4539 {
4540 if (prev != idx)
4541 {
4542 if (!match_callback (idx))
4543 break;
4544 prev = idx;
4545 }
4546 }
4547
4548 /* Above we use a type wider than idx's for 'prev', since 0 and
4549 (offset_type)-1 are both possible values. */
4550 static_assert (sizeof (prev) > sizeof (offset_type), "");
4551 }
4552
4553 #if GDB_SELF_TEST
4554
4555 namespace selftests { namespace dw2_expand_symtabs_matching {
4556
4557 /* A mock .gdb_index/.debug_names-like name index table, enough to
4558 exercise dw2_expand_symtabs_matching_symbol, which works with the
4559 mapped_index_base interface. Builds an index from the symbol list
4560 passed as parameter to the constructor. */
4561 class mock_mapped_index : public mapped_index_base
4562 {
4563 public:
4564 mock_mapped_index (gdb::array_view<const char *> symbols)
4565 : m_symbol_table (symbols)
4566 {}
4567
4568 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
4569
4570 /* Return the number of names in the symbol table. */
4571 size_t symbol_name_count () const override
4572 {
4573 return m_symbol_table.size ();
4574 }
4575
4576 /* Get the name of the symbol at IDX in the symbol table. */
4577 const char *symbol_name_at (offset_type idx) const override
4578 {
4579 return m_symbol_table[idx];
4580 }
4581
4582 private:
4583 gdb::array_view<const char *> m_symbol_table;
4584 };
4585
4586 /* Convenience function that converts a NULL pointer to a "<null>"
4587 string, to pass to print routines. */
4588
4589 static const char *
4590 string_or_null (const char *str)
4591 {
4592 return str != NULL ? str : "<null>";
4593 }
4594
4595 /* Check if a lookup_name_info built from
4596 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4597 index. EXPECTED_LIST is the list of expected matches, in expected
4598 matching order. If no match expected, then an empty list is
4599 specified. Returns true on success. On failure prints a warning
4600 indicating the file:line that failed, and returns false. */
4601
4602 static bool
4603 check_match (const char *file, int line,
4604 mock_mapped_index &mock_index,
4605 const char *name, symbol_name_match_type match_type,
4606 bool completion_mode,
4607 std::initializer_list<const char *> expected_list)
4608 {
4609 lookup_name_info lookup_name (name, match_type, completion_mode);
4610
4611 bool matched = true;
4612
4613 auto mismatch = [&] (const char *expected_str,
4614 const char *got)
4615 {
4616 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4617 "expected=\"%s\", got=\"%s\"\n"),
4618 file, line,
4619 (match_type == symbol_name_match_type::FULL
4620 ? "FULL" : "WILD"),
4621 name, string_or_null (expected_str), string_or_null (got));
4622 matched = false;
4623 };
4624
4625 auto expected_it = expected_list.begin ();
4626 auto expected_end = expected_list.end ();
4627
4628 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4629 NULL, ALL_DOMAIN,
4630 [&] (offset_type idx)
4631 {
4632 const char *matched_name = mock_index.symbol_name_at (idx);
4633 const char *expected_str
4634 = expected_it == expected_end ? NULL : *expected_it++;
4635
4636 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4637 mismatch (expected_str, matched_name);
4638 return true;
4639 });
4640
4641 const char *expected_str
4642 = expected_it == expected_end ? NULL : *expected_it++;
4643 if (expected_str != NULL)
4644 mismatch (expected_str, NULL);
4645
4646 return matched;
4647 }
4648
4649 /* The symbols added to the mock mapped_index for testing (in
4650 canonical form). */
4651 static const char *test_symbols[] = {
4652 "function",
4653 "std::bar",
4654 "std::zfunction",
4655 "std::zfunction2",
4656 "w1::w2",
4657 "ns::foo<char*>",
4658 "ns::foo<int>",
4659 "ns::foo<long>",
4660 "ns2::tmpl<int>::foo2",
4661 "(anonymous namespace)::A::B::C",
4662
4663 /* These are used to check that the increment-last-char in the
4664 matching algorithm for completion doesn't match "t1_fund" when
4665 completing "t1_func". */
4666 "t1_func",
4667 "t1_func1",
4668 "t1_fund",
4669 "t1_fund1",
4670
4671 /* A UTF-8 name with multi-byte sequences to make sure that
4672 cp-name-parser understands this as a single identifier ("função"
4673 is "function" in PT). */
4674 u8"u8função",
4675
4676 /* \377 (0xff) is Latin1 'ÿ'. */
4677 "yfunc\377",
4678
4679 /* \377 (0xff) is Latin1 'ÿ'. */
4680 "\377",
4681 "\377\377123",
4682
4683 /* A name with all sorts of complications. Starts with "z" to make
4684 it easier for the completion tests below. */
4685 #define Z_SYM_NAME \
4686 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4687 "::tuple<(anonymous namespace)::ui*, " \
4688 "std::default_delete<(anonymous namespace)::ui>, void>"
4689
4690 Z_SYM_NAME
4691 };
4692
4693 /* Returns true if the mapped_index_base::find_name_component_bounds
4694 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4695 in completion mode. */
4696
4697 static bool
4698 check_find_bounds_finds (mapped_index_base &index,
4699 const char *search_name,
4700 gdb::array_view<const char *> expected_syms)
4701 {
4702 lookup_name_info lookup_name (search_name,
4703 symbol_name_match_type::FULL, true);
4704
4705 auto bounds = index.find_name_components_bounds (lookup_name,
4706 language_cplus);
4707
4708 size_t distance = std::distance (bounds.first, bounds.second);
4709 if (distance != expected_syms.size ())
4710 return false;
4711
4712 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4713 {
4714 auto nc_elem = bounds.first + exp_elem;
4715 const char *qualified = index.symbol_name_at (nc_elem->idx);
4716 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4717 return false;
4718 }
4719
4720 return true;
4721 }
4722
4723 /* Test the lower-level mapped_index::find_name_component_bounds
4724 method. */
4725
4726 static void
4727 test_mapped_index_find_name_component_bounds ()
4728 {
4729 mock_mapped_index mock_index (test_symbols);
4730
4731 mock_index.build_name_components ();
4732
4733 /* Test the lower-level mapped_index::find_name_component_bounds
4734 method in completion mode. */
4735 {
4736 static const char *expected_syms[] = {
4737 "t1_func",
4738 "t1_func1",
4739 };
4740
4741 SELF_CHECK (check_find_bounds_finds (mock_index,
4742 "t1_func", expected_syms));
4743 }
4744
4745 /* Check that the increment-last-char in the name matching algorithm
4746 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4747 {
4748 static const char *expected_syms1[] = {
4749 "\377",
4750 "\377\377123",
4751 };
4752 SELF_CHECK (check_find_bounds_finds (mock_index,
4753 "\377", expected_syms1));
4754
4755 static const char *expected_syms2[] = {
4756 "\377\377123",
4757 };
4758 SELF_CHECK (check_find_bounds_finds (mock_index,
4759 "\377\377", expected_syms2));
4760 }
4761 }
4762
4763 /* Test dw2_expand_symtabs_matching_symbol. */
4764
4765 static void
4766 test_dw2_expand_symtabs_matching_symbol ()
4767 {
4768 mock_mapped_index mock_index (test_symbols);
4769
4770 /* We let all tests run until the end even if some fails, for debug
4771 convenience. */
4772 bool any_mismatch = false;
4773
4774 /* Create the expected symbols list (an initializer_list). Needed
4775 because lists have commas, and we need to pass them to CHECK,
4776 which is a macro. */
4777 #define EXPECT(...) { __VA_ARGS__ }
4778
4779 /* Wrapper for check_match that passes down the current
4780 __FILE__/__LINE__. */
4781 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4782 any_mismatch |= !check_match (__FILE__, __LINE__, \
4783 mock_index, \
4784 NAME, MATCH_TYPE, COMPLETION_MODE, \
4785 EXPECTED_LIST)
4786
4787 /* Identity checks. */
4788 for (const char *sym : test_symbols)
4789 {
4790 /* Should be able to match all existing symbols. */
4791 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4792 EXPECT (sym));
4793
4794 /* Should be able to match all existing symbols with
4795 parameters. */
4796 std::string with_params = std::string (sym) + "(int)";
4797 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4798 EXPECT (sym));
4799
4800 /* Should be able to match all existing symbols with
4801 parameters and qualifiers. */
4802 with_params = std::string (sym) + " ( int ) const";
4803 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4804 EXPECT (sym));
4805
4806 /* This should really find sym, but cp-name-parser.y doesn't
4807 know about lvalue/rvalue qualifiers yet. */
4808 with_params = std::string (sym) + " ( int ) &&";
4809 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4810 {});
4811 }
4812
4813 /* Check that the name matching algorithm for completion doesn't get
4814 confused with Latin1 'ÿ' / 0xff. */
4815 {
4816 static const char str[] = "\377";
4817 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4818 EXPECT ("\377", "\377\377123"));
4819 }
4820
4821 /* Check that the increment-last-char in the matching algorithm for
4822 completion doesn't match "t1_fund" when completing "t1_func". */
4823 {
4824 static const char str[] = "t1_func";
4825 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4826 EXPECT ("t1_func", "t1_func1"));
4827 }
4828
4829 /* Check that completion mode works at each prefix of the expected
4830 symbol name. */
4831 {
4832 static const char str[] = "function(int)";
4833 size_t len = strlen (str);
4834 std::string lookup;
4835
4836 for (size_t i = 1; i < len; i++)
4837 {
4838 lookup.assign (str, i);
4839 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4840 EXPECT ("function"));
4841 }
4842 }
4843
4844 /* While "w" is a prefix of both components, the match function
4845 should still only be called once. */
4846 {
4847 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4848 EXPECT ("w1::w2"));
4849 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4850 EXPECT ("w1::w2"));
4851 }
4852
4853 /* Same, with a "complicated" symbol. */
4854 {
4855 static const char str[] = Z_SYM_NAME;
4856 size_t len = strlen (str);
4857 std::string lookup;
4858
4859 for (size_t i = 1; i < len; i++)
4860 {
4861 lookup.assign (str, i);
4862 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4863 EXPECT (Z_SYM_NAME));
4864 }
4865 }
4866
4867 /* In FULL mode, an incomplete symbol doesn't match. */
4868 {
4869 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4870 {});
4871 }
4872
4873 /* A complete symbol with parameters matches any overload, since the
4874 index has no overload info. */
4875 {
4876 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4877 EXPECT ("std::zfunction", "std::zfunction2"));
4878 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4879 EXPECT ("std::zfunction", "std::zfunction2"));
4880 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4881 EXPECT ("std::zfunction", "std::zfunction2"));
4882 }
4883
4884 /* Check that whitespace is ignored appropriately. A symbol with a
4885 template argument list. */
4886 {
4887 static const char expected[] = "ns::foo<int>";
4888 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4889 EXPECT (expected));
4890 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4891 EXPECT (expected));
4892 }
4893
4894 /* Check that whitespace is ignored appropriately. A symbol with a
4895 template argument list that includes a pointer. */
4896 {
4897 static const char expected[] = "ns::foo<char*>";
4898 /* Try both completion and non-completion modes. */
4899 static const bool completion_mode[2] = {false, true};
4900 for (size_t i = 0; i < 2; i++)
4901 {
4902 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4903 completion_mode[i], EXPECT (expected));
4904 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4905 completion_mode[i], EXPECT (expected));
4906
4907 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4908 completion_mode[i], EXPECT (expected));
4909 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4910 completion_mode[i], EXPECT (expected));
4911 }
4912 }
4913
4914 {
4915 /* Check method qualifiers are ignored. */
4916 static const char expected[] = "ns::foo<char*>";
4917 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4918 symbol_name_match_type::FULL, true, EXPECT (expected));
4919 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4920 symbol_name_match_type::FULL, true, EXPECT (expected));
4921 CHECK_MATCH ("foo < char * > ( int ) const",
4922 symbol_name_match_type::WILD, true, EXPECT (expected));
4923 CHECK_MATCH ("foo < char * > ( int ) &&",
4924 symbol_name_match_type::WILD, true, EXPECT (expected));
4925 }
4926
4927 /* Test lookup names that don't match anything. */
4928 {
4929 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4930 {});
4931
4932 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4933 {});
4934 }
4935
4936 /* Some wild matching tests, exercising "(anonymous namespace)",
4937 which should not be confused with a parameter list. */
4938 {
4939 static const char *syms[] = {
4940 "A::B::C",
4941 "B::C",
4942 "C",
4943 "A :: B :: C ( int )",
4944 "B :: C ( int )",
4945 "C ( int )",
4946 };
4947
4948 for (const char *s : syms)
4949 {
4950 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4951 EXPECT ("(anonymous namespace)::A::B::C"));
4952 }
4953 }
4954
4955 {
4956 static const char expected[] = "ns2::tmpl<int>::foo2";
4957 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4958 EXPECT (expected));
4959 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4960 EXPECT (expected));
4961 }
4962
4963 SELF_CHECK (!any_mismatch);
4964
4965 #undef EXPECT
4966 #undef CHECK_MATCH
4967 }
4968
4969 static void
4970 run_test ()
4971 {
4972 test_mapped_index_find_name_component_bounds ();
4973 test_dw2_expand_symtabs_matching_symbol ();
4974 }
4975
4976 }} // namespace selftests::dw2_expand_symtabs_matching
4977
4978 #endif /* GDB_SELF_TEST */
4979
4980 /* If FILE_MATCHER is NULL or if PER_CU has
4981 dwarf2_per_cu_quick_data::MARK set (see
4982 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4983 EXPANSION_NOTIFY on it. */
4984
4985 static void
4986 dw2_expand_symtabs_matching_one
4987 (struct dwarf2_per_cu_data *per_cu,
4988 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4989 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4990 {
4991 if (file_matcher == NULL || per_cu->v.quick->mark)
4992 {
4993 bool symtab_was_null
4994 = (per_cu->v.quick->compunit_symtab == NULL);
4995
4996 dw2_instantiate_symtab (per_cu, false);
4997
4998 if (expansion_notify != NULL
4999 && symtab_was_null
5000 && per_cu->v.quick->compunit_symtab != NULL)
5001 expansion_notify (per_cu->v.quick->compunit_symtab);
5002 }
5003 }
5004
5005 /* Helper for dw2_expand_matching symtabs. Called on each symbol
5006 matched, to expand corresponding CUs that were marked. IDX is the
5007 index of the symbol name that matched. */
5008
5009 static void
5010 dw2_expand_marked_cus
5011 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
5012 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5013 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5014 search_domain kind)
5015 {
5016 offset_type *vec, vec_len, vec_idx;
5017 bool global_seen = false;
5018 mapped_index &index = *dwarf2_per_objfile->index_table;
5019
5020 vec = (offset_type *) (index.constant_pool
5021 + MAYBE_SWAP (index.symbol_table[idx].vec));
5022 vec_len = MAYBE_SWAP (vec[0]);
5023 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
5024 {
5025 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
5026 /* This value is only valid for index versions >= 7. */
5027 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
5028 gdb_index_symbol_kind symbol_kind =
5029 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
5030 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
5031 /* Only check the symbol attributes if they're present.
5032 Indices prior to version 7 don't record them,
5033 and indices >= 7 may elide them for certain symbols
5034 (gold does this). */
5035 int attrs_valid =
5036 (index.version >= 7
5037 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
5038
5039 /* Work around gold/15646. */
5040 if (attrs_valid)
5041 {
5042 if (!is_static && global_seen)
5043 continue;
5044 if (!is_static)
5045 global_seen = true;
5046 }
5047
5048 /* Only check the symbol's kind if it has one. */
5049 if (attrs_valid)
5050 {
5051 switch (kind)
5052 {
5053 case VARIABLES_DOMAIN:
5054 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
5055 continue;
5056 break;
5057 case FUNCTIONS_DOMAIN:
5058 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
5059 continue;
5060 break;
5061 case TYPES_DOMAIN:
5062 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
5063 continue;
5064 break;
5065 case MODULES_DOMAIN:
5066 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
5067 continue;
5068 break;
5069 default:
5070 break;
5071 }
5072 }
5073
5074 /* Don't crash on bad data. */
5075 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
5076 + dwarf2_per_objfile->all_type_units.size ()))
5077 {
5078 complaint (_(".gdb_index entry has bad CU index"
5079 " [in module %s]"),
5080 objfile_name (dwarf2_per_objfile->objfile));
5081 continue;
5082 }
5083
5084 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
5085 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5086 expansion_notify);
5087 }
5088 }
5089
5090 /* If FILE_MATCHER is non-NULL, set all the
5091 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
5092 that match FILE_MATCHER. */
5093
5094 static void
5095 dw_expand_symtabs_matching_file_matcher
5096 (struct dwarf2_per_objfile *dwarf2_per_objfile,
5097 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
5098 {
5099 if (file_matcher == NULL)
5100 return;
5101
5102 objfile *const objfile = dwarf2_per_objfile->objfile;
5103
5104 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
5105 htab_eq_pointer,
5106 NULL, xcalloc, xfree));
5107 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
5108 htab_eq_pointer,
5109 NULL, xcalloc, xfree));
5110
5111 /* The rule is CUs specify all the files, including those used by
5112 any TU, so there's no need to scan TUs here. */
5113
5114 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5115 {
5116 QUIT;
5117
5118 per_cu->v.quick->mark = 0;
5119
5120 /* We only need to look at symtabs not already expanded. */
5121 if (per_cu->v.quick->compunit_symtab)
5122 continue;
5123
5124 quick_file_names *file_data = dw2_get_file_names (per_cu);
5125 if (file_data == NULL)
5126 continue;
5127
5128 if (htab_find (visited_not_found.get (), file_data) != NULL)
5129 continue;
5130 else if (htab_find (visited_found.get (), file_data) != NULL)
5131 {
5132 per_cu->v.quick->mark = 1;
5133 continue;
5134 }
5135
5136 for (int j = 0; j < file_data->num_file_names; ++j)
5137 {
5138 const char *this_real_name;
5139
5140 if (file_matcher (file_data->file_names[j], false))
5141 {
5142 per_cu->v.quick->mark = 1;
5143 break;
5144 }
5145
5146 /* Before we invoke realpath, which can get expensive when many
5147 files are involved, do a quick comparison of the basenames. */
5148 if (!basenames_may_differ
5149 && !file_matcher (lbasename (file_data->file_names[j]),
5150 true))
5151 continue;
5152
5153 this_real_name = dw2_get_real_path (objfile, file_data, j);
5154 if (file_matcher (this_real_name, false))
5155 {
5156 per_cu->v.quick->mark = 1;
5157 break;
5158 }
5159 }
5160
5161 void **slot = htab_find_slot (per_cu->v.quick->mark
5162 ? visited_found.get ()
5163 : visited_not_found.get (),
5164 file_data, INSERT);
5165 *slot = file_data;
5166 }
5167 }
5168
5169 static void
5170 dw2_expand_symtabs_matching
5171 (struct objfile *objfile,
5172 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5173 const lookup_name_info &lookup_name,
5174 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5175 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5176 enum search_domain kind)
5177 {
5178 struct dwarf2_per_objfile *dwarf2_per_objfile
5179 = get_dwarf2_per_objfile (objfile);
5180
5181 /* index_table is NULL if OBJF_READNOW. */
5182 if (!dwarf2_per_objfile->index_table)
5183 return;
5184
5185 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
5186
5187 mapped_index &index = *dwarf2_per_objfile->index_table;
5188
5189 dw2_expand_symtabs_matching_symbol (index, lookup_name,
5190 symbol_matcher,
5191 kind, [&] (offset_type idx)
5192 {
5193 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
5194 expansion_notify, kind);
5195 return true;
5196 });
5197 }
5198
5199 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
5200 symtab. */
5201
5202 static struct compunit_symtab *
5203 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
5204 CORE_ADDR pc)
5205 {
5206 int i;
5207
5208 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
5209 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
5210 return cust;
5211
5212 if (cust->includes == NULL)
5213 return NULL;
5214
5215 for (i = 0; cust->includes[i]; ++i)
5216 {
5217 struct compunit_symtab *s = cust->includes[i];
5218
5219 s = recursively_find_pc_sect_compunit_symtab (s, pc);
5220 if (s != NULL)
5221 return s;
5222 }
5223
5224 return NULL;
5225 }
5226
5227 static struct compunit_symtab *
5228 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
5229 struct bound_minimal_symbol msymbol,
5230 CORE_ADDR pc,
5231 struct obj_section *section,
5232 int warn_if_readin)
5233 {
5234 struct dwarf2_per_cu_data *data;
5235 struct compunit_symtab *result;
5236
5237 if (!objfile->partial_symtabs->psymtabs_addrmap)
5238 return NULL;
5239
5240 CORE_ADDR baseaddr = objfile->section_offsets[SECT_OFF_TEXT (objfile)];
5241 data = (struct dwarf2_per_cu_data *) addrmap_find
5242 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
5243 if (!data)
5244 return NULL;
5245
5246 if (warn_if_readin && data->v.quick->compunit_symtab)
5247 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
5248 paddress (get_objfile_arch (objfile), pc));
5249
5250 result
5251 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data,
5252 false),
5253 pc);
5254 gdb_assert (result != NULL);
5255 return result;
5256 }
5257
5258 static void
5259 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
5260 void *data, int need_fullname)
5261 {
5262 struct dwarf2_per_objfile *dwarf2_per_objfile
5263 = get_dwarf2_per_objfile (objfile);
5264
5265 if (!dwarf2_per_objfile->filenames_cache)
5266 {
5267 dwarf2_per_objfile->filenames_cache.emplace ();
5268
5269 htab_up visited (htab_create_alloc (10,
5270 htab_hash_pointer, htab_eq_pointer,
5271 NULL, xcalloc, xfree));
5272
5273 /* The rule is CUs specify all the files, including those used
5274 by any TU, so there's no need to scan TUs here. We can
5275 ignore file names coming from already-expanded CUs. */
5276
5277 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5278 {
5279 if (per_cu->v.quick->compunit_symtab)
5280 {
5281 void **slot = htab_find_slot (visited.get (),
5282 per_cu->v.quick->file_names,
5283 INSERT);
5284
5285 *slot = per_cu->v.quick->file_names;
5286 }
5287 }
5288
5289 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5290 {
5291 /* We only need to look at symtabs not already expanded. */
5292 if (per_cu->v.quick->compunit_symtab)
5293 continue;
5294
5295 quick_file_names *file_data = dw2_get_file_names (per_cu);
5296 if (file_data == NULL)
5297 continue;
5298
5299 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
5300 if (*slot)
5301 {
5302 /* Already visited. */
5303 continue;
5304 }
5305 *slot = file_data;
5306
5307 for (int j = 0; j < file_data->num_file_names; ++j)
5308 {
5309 const char *filename = file_data->file_names[j];
5310 dwarf2_per_objfile->filenames_cache->seen (filename);
5311 }
5312 }
5313 }
5314
5315 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
5316 {
5317 gdb::unique_xmalloc_ptr<char> this_real_name;
5318
5319 if (need_fullname)
5320 this_real_name = gdb_realpath (filename);
5321 (*fun) (filename, this_real_name.get (), data);
5322 });
5323 }
5324
5325 static int
5326 dw2_has_symbols (struct objfile *objfile)
5327 {
5328 return 1;
5329 }
5330
5331 const struct quick_symbol_functions dwarf2_gdb_index_functions =
5332 {
5333 dw2_has_symbols,
5334 dw2_find_last_source_symtab,
5335 dw2_forget_cached_source_info,
5336 dw2_map_symtabs_matching_filename,
5337 dw2_lookup_symbol,
5338 dw2_print_stats,
5339 dw2_dump,
5340 dw2_expand_symtabs_for_function,
5341 dw2_expand_all_symtabs,
5342 dw2_expand_symtabs_with_fullname,
5343 dw2_map_matching_symbols,
5344 dw2_expand_symtabs_matching,
5345 dw2_find_pc_sect_compunit_symtab,
5346 NULL,
5347 dw2_map_symbol_filenames
5348 };
5349
5350 /* DWARF-5 debug_names reader. */
5351
5352 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5353 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5354
5355 /* A helper function that reads the .debug_names section in SECTION
5356 and fills in MAP. FILENAME is the name of the file containing the
5357 section; it is used for error reporting.
5358
5359 Returns true if all went well, false otherwise. */
5360
5361 static bool
5362 read_debug_names_from_section (struct objfile *objfile,
5363 const char *filename,
5364 struct dwarf2_section_info *section,
5365 mapped_debug_names &map)
5366 {
5367 if (dwarf2_section_empty_p (section))
5368 return false;
5369
5370 /* Older elfutils strip versions could keep the section in the main
5371 executable while splitting it for the separate debug info file. */
5372 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
5373 return false;
5374
5375 dwarf2_read_section (objfile, section);
5376
5377 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
5378
5379 const gdb_byte *addr = section->buffer;
5380
5381 bfd *const abfd = get_section_bfd_owner (section);
5382
5383 unsigned int bytes_read;
5384 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5385 addr += bytes_read;
5386
5387 map.dwarf5_is_dwarf64 = bytes_read != 4;
5388 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5389 if (bytes_read + length != section->size)
5390 {
5391 /* There may be multiple per-CU indices. */
5392 warning (_("Section .debug_names in %s length %s does not match "
5393 "section length %s, ignoring .debug_names."),
5394 filename, plongest (bytes_read + length),
5395 pulongest (section->size));
5396 return false;
5397 }
5398
5399 /* The version number. */
5400 uint16_t version = read_2_bytes (abfd, addr);
5401 addr += 2;
5402 if (version != 5)
5403 {
5404 warning (_("Section .debug_names in %s has unsupported version %d, "
5405 "ignoring .debug_names."),
5406 filename, version);
5407 return false;
5408 }
5409
5410 /* Padding. */
5411 uint16_t padding = read_2_bytes (abfd, addr);
5412 addr += 2;
5413 if (padding != 0)
5414 {
5415 warning (_("Section .debug_names in %s has unsupported padding %d, "
5416 "ignoring .debug_names."),
5417 filename, padding);
5418 return false;
5419 }
5420
5421 /* comp_unit_count - The number of CUs in the CU list. */
5422 map.cu_count = read_4_bytes (abfd, addr);
5423 addr += 4;
5424
5425 /* local_type_unit_count - The number of TUs in the local TU
5426 list. */
5427 map.tu_count = read_4_bytes (abfd, addr);
5428 addr += 4;
5429
5430 /* foreign_type_unit_count - The number of TUs in the foreign TU
5431 list. */
5432 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5433 addr += 4;
5434 if (foreign_tu_count != 0)
5435 {
5436 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5437 "ignoring .debug_names."),
5438 filename, static_cast<unsigned long> (foreign_tu_count));
5439 return false;
5440 }
5441
5442 /* bucket_count - The number of hash buckets in the hash lookup
5443 table. */
5444 map.bucket_count = read_4_bytes (abfd, addr);
5445 addr += 4;
5446
5447 /* name_count - The number of unique names in the index. */
5448 map.name_count = read_4_bytes (abfd, addr);
5449 addr += 4;
5450
5451 /* abbrev_table_size - The size in bytes of the abbreviations
5452 table. */
5453 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5454 addr += 4;
5455
5456 /* augmentation_string_size - The size in bytes of the augmentation
5457 string. This value is rounded up to a multiple of 4. */
5458 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5459 addr += 4;
5460 map.augmentation_is_gdb = ((augmentation_string_size
5461 == sizeof (dwarf5_augmentation))
5462 && memcmp (addr, dwarf5_augmentation,
5463 sizeof (dwarf5_augmentation)) == 0);
5464 augmentation_string_size += (-augmentation_string_size) & 3;
5465 addr += augmentation_string_size;
5466
5467 /* List of CUs */
5468 map.cu_table_reordered = addr;
5469 addr += map.cu_count * map.offset_size;
5470
5471 /* List of Local TUs */
5472 map.tu_table_reordered = addr;
5473 addr += map.tu_count * map.offset_size;
5474
5475 /* Hash Lookup Table */
5476 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5477 addr += map.bucket_count * 4;
5478 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5479 addr += map.name_count * 4;
5480
5481 /* Name Table */
5482 map.name_table_string_offs_reordered = addr;
5483 addr += map.name_count * map.offset_size;
5484 map.name_table_entry_offs_reordered = addr;
5485 addr += map.name_count * map.offset_size;
5486
5487 const gdb_byte *abbrev_table_start = addr;
5488 for (;;)
5489 {
5490 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5491 addr += bytes_read;
5492 if (index_num == 0)
5493 break;
5494
5495 const auto insertpair
5496 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5497 if (!insertpair.second)
5498 {
5499 warning (_("Section .debug_names in %s has duplicate index %s, "
5500 "ignoring .debug_names."),
5501 filename, pulongest (index_num));
5502 return false;
5503 }
5504 mapped_debug_names::index_val &indexval = insertpair.first->second;
5505 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5506 addr += bytes_read;
5507
5508 for (;;)
5509 {
5510 mapped_debug_names::index_val::attr attr;
5511 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5512 addr += bytes_read;
5513 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5514 addr += bytes_read;
5515 if (attr.form == DW_FORM_implicit_const)
5516 {
5517 attr.implicit_const = read_signed_leb128 (abfd, addr,
5518 &bytes_read);
5519 addr += bytes_read;
5520 }
5521 if (attr.dw_idx == 0 && attr.form == 0)
5522 break;
5523 indexval.attr_vec.push_back (std::move (attr));
5524 }
5525 }
5526 if (addr != abbrev_table_start + abbrev_table_size)
5527 {
5528 warning (_("Section .debug_names in %s has abbreviation_table "
5529 "of size %s vs. written as %u, ignoring .debug_names."),
5530 filename, plongest (addr - abbrev_table_start),
5531 abbrev_table_size);
5532 return false;
5533 }
5534 map.entry_pool = addr;
5535
5536 return true;
5537 }
5538
5539 /* A helper for create_cus_from_debug_names that handles the MAP's CU
5540 list. */
5541
5542 static void
5543 create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
5544 const mapped_debug_names &map,
5545 dwarf2_section_info &section,
5546 bool is_dwz)
5547 {
5548 sect_offset sect_off_prev;
5549 for (uint32_t i = 0; i <= map.cu_count; ++i)
5550 {
5551 sect_offset sect_off_next;
5552 if (i < map.cu_count)
5553 {
5554 sect_off_next
5555 = (sect_offset) (extract_unsigned_integer
5556 (map.cu_table_reordered + i * map.offset_size,
5557 map.offset_size,
5558 map.dwarf5_byte_order));
5559 }
5560 else
5561 sect_off_next = (sect_offset) section.size;
5562 if (i >= 1)
5563 {
5564 const ULONGEST length = sect_off_next - sect_off_prev;
5565 dwarf2_per_cu_data *per_cu
5566 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
5567 sect_off_prev, length);
5568 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
5569 }
5570 sect_off_prev = sect_off_next;
5571 }
5572 }
5573
5574 /* Read the CU list from the mapped index, and use it to create all
5575 the CU objects for this dwarf2_per_objfile. */
5576
5577 static void
5578 create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
5579 const mapped_debug_names &map,
5580 const mapped_debug_names &dwz_map)
5581 {
5582 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
5583 dwarf2_per_objfile->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
5584
5585 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5586 dwarf2_per_objfile->info,
5587 false /* is_dwz */);
5588
5589 if (dwz_map.cu_count == 0)
5590 return;
5591
5592 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5593 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
5594 true /* is_dwz */);
5595 }
5596
5597 /* Read .debug_names. If everything went ok, initialize the "quick"
5598 elements of all the CUs and return true. Otherwise, return false. */
5599
5600 static bool
5601 dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
5602 {
5603 std::unique_ptr<mapped_debug_names> map
5604 (new mapped_debug_names (dwarf2_per_objfile));
5605 mapped_debug_names dwz_map (dwarf2_per_objfile);
5606 struct objfile *objfile = dwarf2_per_objfile->objfile;
5607
5608 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5609 &dwarf2_per_objfile->debug_names,
5610 *map))
5611 return false;
5612
5613 /* Don't use the index if it's empty. */
5614 if (map->name_count == 0)
5615 return false;
5616
5617 /* If there is a .dwz file, read it so we can get its CU list as
5618 well. */
5619 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5620 if (dwz != NULL)
5621 {
5622 if (!read_debug_names_from_section (objfile,
5623 bfd_get_filename (dwz->dwz_bfd.get ()),
5624 &dwz->debug_names, dwz_map))
5625 {
5626 warning (_("could not read '.debug_names' section from %s; skipping"),
5627 bfd_get_filename (dwz->dwz_bfd.get ()));
5628 return false;
5629 }
5630 }
5631
5632 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
5633
5634 if (map->tu_count != 0)
5635 {
5636 /* We can only handle a single .debug_types when we have an
5637 index. */
5638 if (dwarf2_per_objfile->types.size () != 1)
5639 return false;
5640
5641 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
5642
5643 create_signatured_type_table_from_debug_names
5644 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->abbrev);
5645 }
5646
5647 create_addrmap_from_aranges (dwarf2_per_objfile,
5648 &dwarf2_per_objfile->debug_aranges);
5649
5650 dwarf2_per_objfile->debug_names_table = std::move (map);
5651 dwarf2_per_objfile->using_index = 1;
5652 dwarf2_per_objfile->quick_file_names_table =
5653 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
5654
5655 return true;
5656 }
5657
5658 /* Type used to manage iterating over all CUs looking for a symbol for
5659 .debug_names. */
5660
5661 class dw2_debug_names_iterator
5662 {
5663 public:
5664 dw2_debug_names_iterator (const mapped_debug_names &map,
5665 gdb::optional<block_enum> block_index,
5666 domain_enum domain,
5667 const char *name)
5668 : m_map (map), m_block_index (block_index), m_domain (domain),
5669 m_addr (find_vec_in_debug_names (map, name))
5670 {}
5671
5672 dw2_debug_names_iterator (const mapped_debug_names &map,
5673 search_domain search, uint32_t namei)
5674 : m_map (map),
5675 m_search (search),
5676 m_addr (find_vec_in_debug_names (map, namei))
5677 {}
5678
5679 dw2_debug_names_iterator (const mapped_debug_names &map,
5680 block_enum block_index, domain_enum domain,
5681 uint32_t namei)
5682 : m_map (map), m_block_index (block_index), m_domain (domain),
5683 m_addr (find_vec_in_debug_names (map, namei))
5684 {}
5685
5686 /* Return the next matching CU or NULL if there are no more. */
5687 dwarf2_per_cu_data *next ();
5688
5689 private:
5690 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5691 const char *name);
5692 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5693 uint32_t namei);
5694
5695 /* The internalized form of .debug_names. */
5696 const mapped_debug_names &m_map;
5697
5698 /* If set, only look for symbols that match that block. Valid values are
5699 GLOBAL_BLOCK and STATIC_BLOCK. */
5700 const gdb::optional<block_enum> m_block_index;
5701
5702 /* The kind of symbol we're looking for. */
5703 const domain_enum m_domain = UNDEF_DOMAIN;
5704 const search_domain m_search = ALL_DOMAIN;
5705
5706 /* The list of CUs from the index entry of the symbol, or NULL if
5707 not found. */
5708 const gdb_byte *m_addr;
5709 };
5710
5711 const char *
5712 mapped_debug_names::namei_to_name (uint32_t namei) const
5713 {
5714 const ULONGEST namei_string_offs
5715 = extract_unsigned_integer ((name_table_string_offs_reordered
5716 + namei * offset_size),
5717 offset_size,
5718 dwarf5_byte_order);
5719 return read_indirect_string_at_offset
5720 (dwarf2_per_objfile, dwarf2_per_objfile->objfile->obfd, namei_string_offs);
5721 }
5722
5723 /* Find a slot in .debug_names for the object named NAME. If NAME is
5724 found, return pointer to its pool data. If NAME cannot be found,
5725 return NULL. */
5726
5727 const gdb_byte *
5728 dw2_debug_names_iterator::find_vec_in_debug_names
5729 (const mapped_debug_names &map, const char *name)
5730 {
5731 int (*cmp) (const char *, const char *);
5732
5733 gdb::unique_xmalloc_ptr<char> without_params;
5734 if (current_language->la_language == language_cplus
5735 || current_language->la_language == language_fortran
5736 || current_language->la_language == language_d)
5737 {
5738 /* NAME is already canonical. Drop any qualifiers as
5739 .debug_names does not contain any. */
5740
5741 if (strchr (name, '(') != NULL)
5742 {
5743 without_params = cp_remove_params (name);
5744 if (without_params != NULL)
5745 name = without_params.get ();
5746 }
5747 }
5748
5749 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5750
5751 const uint32_t full_hash = dwarf5_djb_hash (name);
5752 uint32_t namei
5753 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5754 (map.bucket_table_reordered
5755 + (full_hash % map.bucket_count)), 4,
5756 map.dwarf5_byte_order);
5757 if (namei == 0)
5758 return NULL;
5759 --namei;
5760 if (namei >= map.name_count)
5761 {
5762 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5763 "[in module %s]"),
5764 namei, map.name_count,
5765 objfile_name (map.dwarf2_per_objfile->objfile));
5766 return NULL;
5767 }
5768
5769 for (;;)
5770 {
5771 const uint32_t namei_full_hash
5772 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5773 (map.hash_table_reordered + namei), 4,
5774 map.dwarf5_byte_order);
5775 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5776 return NULL;
5777
5778 if (full_hash == namei_full_hash)
5779 {
5780 const char *const namei_string = map.namei_to_name (namei);
5781
5782 #if 0 /* An expensive sanity check. */
5783 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5784 {
5785 complaint (_("Wrong .debug_names hash for string at index %u "
5786 "[in module %s]"),
5787 namei, objfile_name (dwarf2_per_objfile->objfile));
5788 return NULL;
5789 }
5790 #endif
5791
5792 if (cmp (namei_string, name) == 0)
5793 {
5794 const ULONGEST namei_entry_offs
5795 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5796 + namei * map.offset_size),
5797 map.offset_size, map.dwarf5_byte_order);
5798 return map.entry_pool + namei_entry_offs;
5799 }
5800 }
5801
5802 ++namei;
5803 if (namei >= map.name_count)
5804 return NULL;
5805 }
5806 }
5807
5808 const gdb_byte *
5809 dw2_debug_names_iterator::find_vec_in_debug_names
5810 (const mapped_debug_names &map, uint32_t namei)
5811 {
5812 if (namei >= map.name_count)
5813 {
5814 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5815 "[in module %s]"),
5816 namei, map.name_count,
5817 objfile_name (map.dwarf2_per_objfile->objfile));
5818 return NULL;
5819 }
5820
5821 const ULONGEST namei_entry_offs
5822 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5823 + namei * map.offset_size),
5824 map.offset_size, map.dwarf5_byte_order);
5825 return map.entry_pool + namei_entry_offs;
5826 }
5827
5828 /* See dw2_debug_names_iterator. */
5829
5830 dwarf2_per_cu_data *
5831 dw2_debug_names_iterator::next ()
5832 {
5833 if (m_addr == NULL)
5834 return NULL;
5835
5836 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5837 struct objfile *objfile = dwarf2_per_objfile->objfile;
5838 bfd *const abfd = objfile->obfd;
5839
5840 again:
5841
5842 unsigned int bytes_read;
5843 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5844 m_addr += bytes_read;
5845 if (abbrev == 0)
5846 return NULL;
5847
5848 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5849 if (indexval_it == m_map.abbrev_map.cend ())
5850 {
5851 complaint (_("Wrong .debug_names undefined abbrev code %s "
5852 "[in module %s]"),
5853 pulongest (abbrev), objfile_name (objfile));
5854 return NULL;
5855 }
5856 const mapped_debug_names::index_val &indexval = indexval_it->second;
5857 enum class symbol_linkage {
5858 unknown,
5859 static_,
5860 extern_,
5861 } symbol_linkage_ = symbol_linkage::unknown;
5862 dwarf2_per_cu_data *per_cu = NULL;
5863 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5864 {
5865 ULONGEST ull;
5866 switch (attr.form)
5867 {
5868 case DW_FORM_implicit_const:
5869 ull = attr.implicit_const;
5870 break;
5871 case DW_FORM_flag_present:
5872 ull = 1;
5873 break;
5874 case DW_FORM_udata:
5875 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5876 m_addr += bytes_read;
5877 break;
5878 default:
5879 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5880 dwarf_form_name (attr.form),
5881 objfile_name (objfile));
5882 return NULL;
5883 }
5884 switch (attr.dw_idx)
5885 {
5886 case DW_IDX_compile_unit:
5887 /* Don't crash on bad data. */
5888 if (ull >= dwarf2_per_objfile->all_comp_units.size ())
5889 {
5890 complaint (_(".debug_names entry has bad CU index %s"
5891 " [in module %s]"),
5892 pulongest (ull),
5893 objfile_name (dwarf2_per_objfile->objfile));
5894 continue;
5895 }
5896 per_cu = dwarf2_per_objfile->get_cutu (ull);
5897 break;
5898 case DW_IDX_type_unit:
5899 /* Don't crash on bad data. */
5900 if (ull >= dwarf2_per_objfile->all_type_units.size ())
5901 {
5902 complaint (_(".debug_names entry has bad TU index %s"
5903 " [in module %s]"),
5904 pulongest (ull),
5905 objfile_name (dwarf2_per_objfile->objfile));
5906 continue;
5907 }
5908 per_cu = &dwarf2_per_objfile->get_tu (ull)->per_cu;
5909 break;
5910 case DW_IDX_GNU_internal:
5911 if (!m_map.augmentation_is_gdb)
5912 break;
5913 symbol_linkage_ = symbol_linkage::static_;
5914 break;
5915 case DW_IDX_GNU_external:
5916 if (!m_map.augmentation_is_gdb)
5917 break;
5918 symbol_linkage_ = symbol_linkage::extern_;
5919 break;
5920 }
5921 }
5922
5923 /* Skip if already read in. */
5924 if (per_cu->v.quick->compunit_symtab)
5925 goto again;
5926
5927 /* Check static vs global. */
5928 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
5929 {
5930 const bool want_static = *m_block_index == STATIC_BLOCK;
5931 const bool symbol_is_static =
5932 symbol_linkage_ == symbol_linkage::static_;
5933 if (want_static != symbol_is_static)
5934 goto again;
5935 }
5936
5937 /* Match dw2_symtab_iter_next, symbol_kind
5938 and debug_names::psymbol_tag. */
5939 switch (m_domain)
5940 {
5941 case VAR_DOMAIN:
5942 switch (indexval.dwarf_tag)
5943 {
5944 case DW_TAG_variable:
5945 case DW_TAG_subprogram:
5946 /* Some types are also in VAR_DOMAIN. */
5947 case DW_TAG_typedef:
5948 case DW_TAG_structure_type:
5949 break;
5950 default:
5951 goto again;
5952 }
5953 break;
5954 case STRUCT_DOMAIN:
5955 switch (indexval.dwarf_tag)
5956 {
5957 case DW_TAG_typedef:
5958 case DW_TAG_structure_type:
5959 break;
5960 default:
5961 goto again;
5962 }
5963 break;
5964 case LABEL_DOMAIN:
5965 switch (indexval.dwarf_tag)
5966 {
5967 case 0:
5968 case DW_TAG_variable:
5969 break;
5970 default:
5971 goto again;
5972 }
5973 break;
5974 case MODULE_DOMAIN:
5975 switch (indexval.dwarf_tag)
5976 {
5977 case DW_TAG_module:
5978 break;
5979 default:
5980 goto again;
5981 }
5982 break;
5983 default:
5984 break;
5985 }
5986
5987 /* Match dw2_expand_symtabs_matching, symbol_kind and
5988 debug_names::psymbol_tag. */
5989 switch (m_search)
5990 {
5991 case VARIABLES_DOMAIN:
5992 switch (indexval.dwarf_tag)
5993 {
5994 case DW_TAG_variable:
5995 break;
5996 default:
5997 goto again;
5998 }
5999 break;
6000 case FUNCTIONS_DOMAIN:
6001 switch (indexval.dwarf_tag)
6002 {
6003 case DW_TAG_subprogram:
6004 break;
6005 default:
6006 goto again;
6007 }
6008 break;
6009 case TYPES_DOMAIN:
6010 switch (indexval.dwarf_tag)
6011 {
6012 case DW_TAG_typedef:
6013 case DW_TAG_structure_type:
6014 break;
6015 default:
6016 goto again;
6017 }
6018 break;
6019 case MODULES_DOMAIN:
6020 switch (indexval.dwarf_tag)
6021 {
6022 case DW_TAG_module:
6023 break;
6024 default:
6025 goto again;
6026 }
6027 default:
6028 break;
6029 }
6030
6031 return per_cu;
6032 }
6033
6034 static struct compunit_symtab *
6035 dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
6036 const char *name, domain_enum domain)
6037 {
6038 struct dwarf2_per_objfile *dwarf2_per_objfile
6039 = get_dwarf2_per_objfile (objfile);
6040
6041 const auto &mapp = dwarf2_per_objfile->debug_names_table;
6042 if (!mapp)
6043 {
6044 /* index is NULL if OBJF_READNOW. */
6045 return NULL;
6046 }
6047 const auto &map = *mapp;
6048
6049 dw2_debug_names_iterator iter (map, block_index, domain, name);
6050
6051 struct compunit_symtab *stab_best = NULL;
6052 struct dwarf2_per_cu_data *per_cu;
6053 while ((per_cu = iter.next ()) != NULL)
6054 {
6055 struct symbol *sym, *with_opaque = NULL;
6056 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
6057 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
6058 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
6059
6060 sym = block_find_symbol (block, name, domain,
6061 block_find_non_opaque_type_preferred,
6062 &with_opaque);
6063
6064 /* Some caution must be observed with overloaded functions and
6065 methods, since the index will not contain any overload
6066 information (but NAME might contain it). */
6067
6068 if (sym != NULL
6069 && strcmp_iw (sym->search_name (), name) == 0)
6070 return stab;
6071 if (with_opaque != NULL
6072 && strcmp_iw (with_opaque->search_name (), name) == 0)
6073 stab_best = stab;
6074
6075 /* Keep looking through other CUs. */
6076 }
6077
6078 return stab_best;
6079 }
6080
6081 /* This dumps minimal information about .debug_names. It is called
6082 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
6083 uses this to verify that .debug_names has been loaded. */
6084
6085 static void
6086 dw2_debug_names_dump (struct objfile *objfile)
6087 {
6088 struct dwarf2_per_objfile *dwarf2_per_objfile
6089 = get_dwarf2_per_objfile (objfile);
6090
6091 gdb_assert (dwarf2_per_objfile->using_index);
6092 printf_filtered (".debug_names:");
6093 if (dwarf2_per_objfile->debug_names_table)
6094 printf_filtered (" exists\n");
6095 else
6096 printf_filtered (" faked for \"readnow\"\n");
6097 printf_filtered ("\n");
6098 }
6099
6100 static void
6101 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
6102 const char *func_name)
6103 {
6104 struct dwarf2_per_objfile *dwarf2_per_objfile
6105 = get_dwarf2_per_objfile (objfile);
6106
6107 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
6108 if (dwarf2_per_objfile->debug_names_table)
6109 {
6110 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6111
6112 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
6113
6114 struct dwarf2_per_cu_data *per_cu;
6115 while ((per_cu = iter.next ()) != NULL)
6116 dw2_instantiate_symtab (per_cu, false);
6117 }
6118 }
6119
6120 static void
6121 dw2_debug_names_map_matching_symbols
6122 (struct objfile *objfile,
6123 const lookup_name_info &name, domain_enum domain,
6124 int global,
6125 gdb::function_view<symbol_found_callback_ftype> callback,
6126 symbol_compare_ftype *ordered_compare)
6127 {
6128 struct dwarf2_per_objfile *dwarf2_per_objfile
6129 = get_dwarf2_per_objfile (objfile);
6130
6131 /* debug_names_table is NULL if OBJF_READNOW. */
6132 if (!dwarf2_per_objfile->debug_names_table)
6133 return;
6134
6135 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6136 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
6137
6138 const char *match_name = name.ada ().lookup_name ().c_str ();
6139 auto matcher = [&] (const char *symname)
6140 {
6141 if (ordered_compare == nullptr)
6142 return true;
6143 return ordered_compare (symname, match_name) == 0;
6144 };
6145
6146 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
6147 [&] (offset_type namei)
6148 {
6149 /* The name was matched, now expand corresponding CUs that were
6150 marked. */
6151 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
6152
6153 struct dwarf2_per_cu_data *per_cu;
6154 while ((per_cu = iter.next ()) != NULL)
6155 dw2_expand_symtabs_matching_one (per_cu, nullptr, nullptr);
6156 return true;
6157 });
6158
6159 /* It's a shame we couldn't do this inside the
6160 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
6161 that have already been expanded. Instead, this loop matches what
6162 the psymtab code does. */
6163 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
6164 {
6165 struct compunit_symtab *cust = per_cu->v.quick->compunit_symtab;
6166 if (cust != nullptr)
6167 {
6168 const struct block *block
6169 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
6170 if (!iterate_over_symbols_terminated (block, name,
6171 domain, callback))
6172 break;
6173 }
6174 }
6175 }
6176
6177 static void
6178 dw2_debug_names_expand_symtabs_matching
6179 (struct objfile *objfile,
6180 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
6181 const lookup_name_info &lookup_name,
6182 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
6183 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
6184 enum search_domain kind)
6185 {
6186 struct dwarf2_per_objfile *dwarf2_per_objfile
6187 = get_dwarf2_per_objfile (objfile);
6188
6189 /* debug_names_table is NULL if OBJF_READNOW. */
6190 if (!dwarf2_per_objfile->debug_names_table)
6191 return;
6192
6193 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
6194
6195 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6196
6197 dw2_expand_symtabs_matching_symbol (map, lookup_name,
6198 symbol_matcher,
6199 kind, [&] (offset_type namei)
6200 {
6201 /* The name was matched, now expand corresponding CUs that were
6202 marked. */
6203 dw2_debug_names_iterator iter (map, kind, namei);
6204
6205 struct dwarf2_per_cu_data *per_cu;
6206 while ((per_cu = iter.next ()) != NULL)
6207 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
6208 expansion_notify);
6209 return true;
6210 });
6211 }
6212
6213 const struct quick_symbol_functions dwarf2_debug_names_functions =
6214 {
6215 dw2_has_symbols,
6216 dw2_find_last_source_symtab,
6217 dw2_forget_cached_source_info,
6218 dw2_map_symtabs_matching_filename,
6219 dw2_debug_names_lookup_symbol,
6220 dw2_print_stats,
6221 dw2_debug_names_dump,
6222 dw2_debug_names_expand_symtabs_for_function,
6223 dw2_expand_all_symtabs,
6224 dw2_expand_symtabs_with_fullname,
6225 dw2_debug_names_map_matching_symbols,
6226 dw2_debug_names_expand_symtabs_matching,
6227 dw2_find_pc_sect_compunit_symtab,
6228 NULL,
6229 dw2_map_symbol_filenames
6230 };
6231
6232 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
6233 to either a dwarf2_per_objfile or dwz_file object. */
6234
6235 template <typename T>
6236 static gdb::array_view<const gdb_byte>
6237 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
6238 {
6239 dwarf2_section_info *section = &section_owner->gdb_index;
6240
6241 if (dwarf2_section_empty_p (section))
6242 return {};
6243
6244 /* Older elfutils strip versions could keep the section in the main
6245 executable while splitting it for the separate debug info file. */
6246 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
6247 return {};
6248
6249 dwarf2_read_section (obj, section);
6250
6251 /* dwarf2_section_info::size is a bfd_size_type, while
6252 gdb::array_view works with size_t. On 32-bit hosts, with
6253 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
6254 is 32-bit. So we need an explicit narrowing conversion here.
6255 This is fine, because it's impossible to allocate or mmap an
6256 array/buffer larger than what size_t can represent. */
6257 return gdb::make_array_view (section->buffer, section->size);
6258 }
6259
6260 /* Lookup the index cache for the contents of the index associated to
6261 DWARF2_OBJ. */
6262
6263 static gdb::array_view<const gdb_byte>
6264 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_objfile *dwarf2_obj)
6265 {
6266 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
6267 if (build_id == nullptr)
6268 return {};
6269
6270 return global_index_cache.lookup_gdb_index (build_id,
6271 &dwarf2_obj->index_cache_res);
6272 }
6273
6274 /* Same as the above, but for DWZ. */
6275
6276 static gdb::array_view<const gdb_byte>
6277 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
6278 {
6279 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
6280 if (build_id == nullptr)
6281 return {};
6282
6283 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
6284 }
6285
6286 /* See symfile.h. */
6287
6288 bool
6289 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
6290 {
6291 struct dwarf2_per_objfile *dwarf2_per_objfile
6292 = get_dwarf2_per_objfile (objfile);
6293
6294 /* If we're about to read full symbols, don't bother with the
6295 indices. In this case we also don't care if some other debug
6296 format is making psymtabs, because they are all about to be
6297 expanded anyway. */
6298 if ((objfile->flags & OBJF_READNOW))
6299 {
6300 dwarf2_per_objfile->using_index = 1;
6301 create_all_comp_units (dwarf2_per_objfile);
6302 create_all_type_units (dwarf2_per_objfile);
6303 dwarf2_per_objfile->quick_file_names_table
6304 = create_quick_file_names_table
6305 (dwarf2_per_objfile->all_comp_units.size ());
6306
6307 for (int i = 0; i < (dwarf2_per_objfile->all_comp_units.size ()
6308 + dwarf2_per_objfile->all_type_units.size ()); ++i)
6309 {
6310 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
6311
6312 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6313 struct dwarf2_per_cu_quick_data);
6314 }
6315
6316 /* Return 1 so that gdb sees the "quick" functions. However,
6317 these functions will be no-ops because we will have expanded
6318 all symtabs. */
6319 *index_kind = dw_index_kind::GDB_INDEX;
6320 return true;
6321 }
6322
6323 if (dwarf2_read_debug_names (dwarf2_per_objfile))
6324 {
6325 *index_kind = dw_index_kind::DEBUG_NAMES;
6326 return true;
6327 }
6328
6329 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
6330 get_gdb_index_contents_from_section<struct dwarf2_per_objfile>,
6331 get_gdb_index_contents_from_section<dwz_file>))
6332 {
6333 *index_kind = dw_index_kind::GDB_INDEX;
6334 return true;
6335 }
6336
6337 /* ... otherwise, try to find the index in the index cache. */
6338 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
6339 get_gdb_index_contents_from_cache,
6340 get_gdb_index_contents_from_cache_dwz))
6341 {
6342 global_index_cache.hit ();
6343 *index_kind = dw_index_kind::GDB_INDEX;
6344 return true;
6345 }
6346
6347 global_index_cache.miss ();
6348 return false;
6349 }
6350
6351 \f
6352
6353 /* Build a partial symbol table. */
6354
6355 void
6356 dwarf2_build_psymtabs (struct objfile *objfile)
6357 {
6358 struct dwarf2_per_objfile *dwarf2_per_objfile
6359 = get_dwarf2_per_objfile (objfile);
6360
6361 init_psymbol_list (objfile, 1024);
6362
6363 try
6364 {
6365 /* This isn't really ideal: all the data we allocate on the
6366 objfile's obstack is still uselessly kept around. However,
6367 freeing it seems unsafe. */
6368 psymtab_discarder psymtabs (objfile);
6369 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
6370 psymtabs.keep ();
6371
6372 /* (maybe) store an index in the cache. */
6373 global_index_cache.store (dwarf2_per_objfile);
6374 }
6375 catch (const gdb_exception_error &except)
6376 {
6377 exception_print (gdb_stderr, except);
6378 }
6379 }
6380
6381 /* Return the total length of the CU described by HEADER. */
6382
6383 static unsigned int
6384 get_cu_length (const struct comp_unit_head *header)
6385 {
6386 return header->initial_length_size + header->length;
6387 }
6388
6389 /* Return TRUE if SECT_OFF is within CU_HEADER. */
6390
6391 static inline bool
6392 offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
6393 {
6394 sect_offset bottom = cu_header->sect_off;
6395 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
6396
6397 return sect_off >= bottom && sect_off < top;
6398 }
6399
6400 /* Find the base address of the compilation unit for range lists and
6401 location lists. It will normally be specified by DW_AT_low_pc.
6402 In DWARF-3 draft 4, the base address could be overridden by
6403 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6404 compilation units with discontinuous ranges. */
6405
6406 static void
6407 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6408 {
6409 struct attribute *attr;
6410
6411 cu->base_known = 0;
6412 cu->base_address = 0;
6413
6414 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6415 if (attr != nullptr)
6416 {
6417 cu->base_address = attr_value_as_address (attr);
6418 cu->base_known = 1;
6419 }
6420 else
6421 {
6422 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6423 if (attr != nullptr)
6424 {
6425 cu->base_address = attr_value_as_address (attr);
6426 cu->base_known = 1;
6427 }
6428 }
6429 }
6430
6431 /* Read in the comp unit header information from the debug_info at info_ptr.
6432 Use rcuh_kind::COMPILE as the default type if not known by the caller.
6433 NOTE: This leaves members offset, first_die_offset to be filled in
6434 by the caller. */
6435
6436 static const gdb_byte *
6437 read_comp_unit_head (struct comp_unit_head *cu_header,
6438 const gdb_byte *info_ptr,
6439 struct dwarf2_section_info *section,
6440 rcuh_kind section_kind)
6441 {
6442 int signed_addr;
6443 unsigned int bytes_read;
6444 const char *filename = get_section_file_name (section);
6445 bfd *abfd = get_section_bfd_owner (section);
6446
6447 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
6448 cu_header->initial_length_size = bytes_read;
6449 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
6450 info_ptr += bytes_read;
6451 cu_header->version = read_2_bytes (abfd, info_ptr);
6452 if (cu_header->version < 2 || cu_header->version > 5)
6453 error (_("Dwarf Error: wrong version in compilation unit header "
6454 "(is %d, should be 2, 3, 4 or 5) [in module %s]"),
6455 cu_header->version, filename);
6456 info_ptr += 2;
6457 if (cu_header->version < 5)
6458 switch (section_kind)
6459 {
6460 case rcuh_kind::COMPILE:
6461 cu_header->unit_type = DW_UT_compile;
6462 break;
6463 case rcuh_kind::TYPE:
6464 cu_header->unit_type = DW_UT_type;
6465 break;
6466 default:
6467 internal_error (__FILE__, __LINE__,
6468 _("read_comp_unit_head: invalid section_kind"));
6469 }
6470 else
6471 {
6472 cu_header->unit_type = static_cast<enum dwarf_unit_type>
6473 (read_1_byte (abfd, info_ptr));
6474 info_ptr += 1;
6475 switch (cu_header->unit_type)
6476 {
6477 case DW_UT_compile:
6478 case DW_UT_partial:
6479 case DW_UT_skeleton:
6480 case DW_UT_split_compile:
6481 if (section_kind != rcuh_kind::COMPILE)
6482 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6483 "(is %s, should be %s) [in module %s]"),
6484 dwarf_unit_type_name (cu_header->unit_type),
6485 dwarf_unit_type_name (DW_UT_type), filename);
6486 break;
6487 case DW_UT_type:
6488 case DW_UT_split_type:
6489 section_kind = rcuh_kind::TYPE;
6490 break;
6491 default:
6492 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6493 "(is %#04x, should be one of: %s, %s, %s, %s or %s) "
6494 "[in module %s]"), cu_header->unit_type,
6495 dwarf_unit_type_name (DW_UT_compile),
6496 dwarf_unit_type_name (DW_UT_skeleton),
6497 dwarf_unit_type_name (DW_UT_split_compile),
6498 dwarf_unit_type_name (DW_UT_type),
6499 dwarf_unit_type_name (DW_UT_split_type), filename);
6500 }
6501
6502 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6503 info_ptr += 1;
6504 }
6505 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
6506 cu_header,
6507 &bytes_read);
6508 info_ptr += bytes_read;
6509 if (cu_header->version < 5)
6510 {
6511 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6512 info_ptr += 1;
6513 }
6514 signed_addr = bfd_get_sign_extend_vma (abfd);
6515 if (signed_addr < 0)
6516 internal_error (__FILE__, __LINE__,
6517 _("read_comp_unit_head: dwarf from non elf file"));
6518 cu_header->signed_addr_p = signed_addr;
6519
6520 bool header_has_signature = section_kind == rcuh_kind::TYPE
6521 || cu_header->unit_type == DW_UT_skeleton
6522 || cu_header->unit_type == DW_UT_split_compile;
6523
6524 if (header_has_signature)
6525 {
6526 cu_header->signature = read_8_bytes (abfd, info_ptr);
6527 info_ptr += 8;
6528 }
6529
6530 if (section_kind == rcuh_kind::TYPE)
6531 {
6532 LONGEST type_offset;
6533 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
6534 info_ptr += bytes_read;
6535 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
6536 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
6537 error (_("Dwarf Error: Too big type_offset in compilation unit "
6538 "header (is %s) [in module %s]"), plongest (type_offset),
6539 filename);
6540 }
6541
6542 return info_ptr;
6543 }
6544
6545 /* Helper function that returns the proper abbrev section for
6546 THIS_CU. */
6547
6548 static struct dwarf2_section_info *
6549 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6550 {
6551 struct dwarf2_section_info *abbrev;
6552 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6553
6554 if (this_cu->is_dwz)
6555 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
6556 else
6557 abbrev = &dwarf2_per_objfile->abbrev;
6558
6559 return abbrev;
6560 }
6561
6562 /* Subroutine of read_and_check_comp_unit_head and
6563 read_and_check_type_unit_head to simplify them.
6564 Perform various error checking on the header. */
6565
6566 static void
6567 error_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6568 struct comp_unit_head *header,
6569 struct dwarf2_section_info *section,
6570 struct dwarf2_section_info *abbrev_section)
6571 {
6572 const char *filename = get_section_file_name (section);
6573
6574 if (to_underlying (header->abbrev_sect_off)
6575 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
6576 error (_("Dwarf Error: bad offset (%s) in compilation unit header "
6577 "(offset %s + 6) [in module %s]"),
6578 sect_offset_str (header->abbrev_sect_off),
6579 sect_offset_str (header->sect_off),
6580 filename);
6581
6582 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
6583 avoid potential 32-bit overflow. */
6584 if (((ULONGEST) header->sect_off + get_cu_length (header))
6585 > section->size)
6586 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
6587 "(offset %s + 0) [in module %s]"),
6588 header->length, sect_offset_str (header->sect_off),
6589 filename);
6590 }
6591
6592 /* Read in a CU/TU header and perform some basic error checking.
6593 The contents of the header are stored in HEADER.
6594 The result is a pointer to the start of the first DIE. */
6595
6596 static const gdb_byte *
6597 read_and_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6598 struct comp_unit_head *header,
6599 struct dwarf2_section_info *section,
6600 struct dwarf2_section_info *abbrev_section,
6601 const gdb_byte *info_ptr,
6602 rcuh_kind section_kind)
6603 {
6604 const gdb_byte *beg_of_comp_unit = info_ptr;
6605
6606 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
6607
6608 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
6609
6610 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
6611
6612 error_check_comp_unit_head (dwarf2_per_objfile, header, section,
6613 abbrev_section);
6614
6615 return info_ptr;
6616 }
6617
6618 /* Fetch the abbreviation table offset from a comp or type unit header. */
6619
6620 static sect_offset
6621 read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6622 struct dwarf2_section_info *section,
6623 sect_offset sect_off)
6624 {
6625 bfd *abfd = get_section_bfd_owner (section);
6626 const gdb_byte *info_ptr;
6627 unsigned int initial_length_size, offset_size;
6628 uint16_t version;
6629
6630 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
6631 info_ptr = section->buffer + to_underlying (sect_off);
6632 read_initial_length (abfd, info_ptr, &initial_length_size);
6633 offset_size = initial_length_size == 4 ? 4 : 8;
6634 info_ptr += initial_length_size;
6635
6636 version = read_2_bytes (abfd, info_ptr);
6637 info_ptr += 2;
6638 if (version >= 5)
6639 {
6640 /* Skip unit type and address size. */
6641 info_ptr += 2;
6642 }
6643
6644 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
6645 }
6646
6647 /* Allocate a new partial symtab for file named NAME and mark this new
6648 partial symtab as being an include of PST. */
6649
6650 static void
6651 dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
6652 struct objfile *objfile)
6653 {
6654 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
6655
6656 if (!IS_ABSOLUTE_PATH (subpst->filename))
6657 {
6658 /* It shares objfile->objfile_obstack. */
6659 subpst->dirname = pst->dirname;
6660 }
6661
6662 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
6663 subpst->dependencies[0] = pst;
6664 subpst->number_of_dependencies = 1;
6665
6666 subpst->read_symtab = pst->read_symtab;
6667
6668 /* No private part is necessary for include psymtabs. This property
6669 can be used to differentiate between such include psymtabs and
6670 the regular ones. */
6671 subpst->read_symtab_private = NULL;
6672 }
6673
6674 /* Read the Line Number Program data and extract the list of files
6675 included by the source file represented by PST. Build an include
6676 partial symtab for each of these included files. */
6677
6678 static void
6679 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
6680 struct die_info *die,
6681 struct partial_symtab *pst)
6682 {
6683 line_header_up lh;
6684 struct attribute *attr;
6685
6686 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6687 if (attr != nullptr)
6688 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
6689 if (lh == NULL)
6690 return; /* No linetable, so no includes. */
6691
6692 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6693 that we pass in the raw text_low here; that is ok because we're
6694 only decoding the line table to make include partial symtabs, and
6695 so the addresses aren't really used. */
6696 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
6697 pst->raw_text_low (), 1);
6698 }
6699
6700 static hashval_t
6701 hash_signatured_type (const void *item)
6702 {
6703 const struct signatured_type *sig_type
6704 = (const struct signatured_type *) item;
6705
6706 /* This drops the top 32 bits of the signature, but is ok for a hash. */
6707 return sig_type->signature;
6708 }
6709
6710 static int
6711 eq_signatured_type (const void *item_lhs, const void *item_rhs)
6712 {
6713 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6714 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
6715
6716 return lhs->signature == rhs->signature;
6717 }
6718
6719 /* Allocate a hash table for signatured types. */
6720
6721 static htab_t
6722 allocate_signatured_type_table (struct objfile *objfile)
6723 {
6724 return htab_create_alloc_ex (41,
6725 hash_signatured_type,
6726 eq_signatured_type,
6727 NULL,
6728 &objfile->objfile_obstack,
6729 hashtab_obstack_allocate,
6730 dummy_obstack_deallocate);
6731 }
6732
6733 /* A helper function to add a signatured type CU to a table. */
6734
6735 static int
6736 add_signatured_type_cu_to_table (void **slot, void *datum)
6737 {
6738 struct signatured_type *sigt = (struct signatured_type *) *slot;
6739 std::vector<signatured_type *> *all_type_units
6740 = (std::vector<signatured_type *> *) datum;
6741
6742 all_type_units->push_back (sigt);
6743
6744 return 1;
6745 }
6746
6747 /* A helper for create_debug_types_hash_table. Read types from SECTION
6748 and fill them into TYPES_HTAB. It will process only type units,
6749 therefore DW_UT_type. */
6750
6751 static void
6752 create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6753 struct dwo_file *dwo_file,
6754 dwarf2_section_info *section, htab_t &types_htab,
6755 rcuh_kind section_kind)
6756 {
6757 struct objfile *objfile = dwarf2_per_objfile->objfile;
6758 struct dwarf2_section_info *abbrev_section;
6759 bfd *abfd;
6760 const gdb_byte *info_ptr, *end_ptr;
6761
6762 abbrev_section = (dwo_file != NULL
6763 ? &dwo_file->sections.abbrev
6764 : &dwarf2_per_objfile->abbrev);
6765
6766 if (dwarf_read_debug)
6767 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6768 get_section_name (section),
6769 get_section_file_name (abbrev_section));
6770
6771 dwarf2_read_section (objfile, section);
6772 info_ptr = section->buffer;
6773
6774 if (info_ptr == NULL)
6775 return;
6776
6777 /* We can't set abfd until now because the section may be empty or
6778 not present, in which case the bfd is unknown. */
6779 abfd = get_section_bfd_owner (section);
6780
6781 /* We don't use init_cutu_and_read_dies_simple, or some such, here
6782 because we don't need to read any dies: the signature is in the
6783 header. */
6784
6785 end_ptr = info_ptr + section->size;
6786 while (info_ptr < end_ptr)
6787 {
6788 struct signatured_type *sig_type;
6789 struct dwo_unit *dwo_tu;
6790 void **slot;
6791 const gdb_byte *ptr = info_ptr;
6792 struct comp_unit_head header;
6793 unsigned int length;
6794
6795 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6796
6797 /* Initialize it due to a false compiler warning. */
6798 header.signature = -1;
6799 header.type_cu_offset_in_tu = (cu_offset) -1;
6800
6801 /* We need to read the type's signature in order to build the hash
6802 table, but we don't need anything else just yet. */
6803
6804 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
6805 abbrev_section, ptr, section_kind);
6806
6807 length = get_cu_length (&header);
6808
6809 /* Skip dummy type units. */
6810 if (ptr >= info_ptr + length
6811 || peek_abbrev_code (abfd, ptr) == 0
6812 || header.unit_type != DW_UT_type)
6813 {
6814 info_ptr += length;
6815 continue;
6816 }
6817
6818 if (types_htab == NULL)
6819 {
6820 if (dwo_file)
6821 types_htab = allocate_dwo_unit_table (objfile);
6822 else
6823 types_htab = allocate_signatured_type_table (objfile);
6824 }
6825
6826 if (dwo_file)
6827 {
6828 sig_type = NULL;
6829 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6830 struct dwo_unit);
6831 dwo_tu->dwo_file = dwo_file;
6832 dwo_tu->signature = header.signature;
6833 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6834 dwo_tu->section = section;
6835 dwo_tu->sect_off = sect_off;
6836 dwo_tu->length = length;
6837 }
6838 else
6839 {
6840 /* N.B.: type_offset is not usable if this type uses a DWO file.
6841 The real type_offset is in the DWO file. */
6842 dwo_tu = NULL;
6843 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6844 struct signatured_type);
6845 sig_type->signature = header.signature;
6846 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6847 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6848 sig_type->per_cu.is_debug_types = 1;
6849 sig_type->per_cu.section = section;
6850 sig_type->per_cu.sect_off = sect_off;
6851 sig_type->per_cu.length = length;
6852 }
6853
6854 slot = htab_find_slot (types_htab,
6855 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6856 INSERT);
6857 gdb_assert (slot != NULL);
6858 if (*slot != NULL)
6859 {
6860 sect_offset dup_sect_off;
6861
6862 if (dwo_file)
6863 {
6864 const struct dwo_unit *dup_tu
6865 = (const struct dwo_unit *) *slot;
6866
6867 dup_sect_off = dup_tu->sect_off;
6868 }
6869 else
6870 {
6871 const struct signatured_type *dup_tu
6872 = (const struct signatured_type *) *slot;
6873
6874 dup_sect_off = dup_tu->per_cu.sect_off;
6875 }
6876
6877 complaint (_("debug type entry at offset %s is duplicate to"
6878 " the entry at offset %s, signature %s"),
6879 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
6880 hex_string (header.signature));
6881 }
6882 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
6883
6884 if (dwarf_read_debug > 1)
6885 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6886 sect_offset_str (sect_off),
6887 hex_string (header.signature));
6888
6889 info_ptr += length;
6890 }
6891 }
6892
6893 /* Create the hash table of all entries in the .debug_types
6894 (or .debug_types.dwo) section(s).
6895 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6896 otherwise it is NULL.
6897
6898 The result is a pointer to the hash table or NULL if there are no types.
6899
6900 Note: This function processes DWO files only, not DWP files. */
6901
6902 static void
6903 create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6904 struct dwo_file *dwo_file,
6905 gdb::array_view<dwarf2_section_info> type_sections,
6906 htab_t &types_htab)
6907 {
6908 for (dwarf2_section_info &section : type_sections)
6909 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
6910 types_htab, rcuh_kind::TYPE);
6911 }
6912
6913 /* Create the hash table of all entries in the .debug_types section,
6914 and initialize all_type_units.
6915 The result is zero if there is an error (e.g. missing .debug_types section),
6916 otherwise non-zero. */
6917
6918 static int
6919 create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6920 {
6921 htab_t types_htab = NULL;
6922
6923 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6924 &dwarf2_per_objfile->info, types_htab,
6925 rcuh_kind::COMPILE);
6926 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6927 dwarf2_per_objfile->types, types_htab);
6928 if (types_htab == NULL)
6929 {
6930 dwarf2_per_objfile->signatured_types = NULL;
6931 return 0;
6932 }
6933
6934 dwarf2_per_objfile->signatured_types = types_htab;
6935
6936 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
6937 dwarf2_per_objfile->all_type_units.reserve (htab_elements (types_htab));
6938
6939 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table,
6940 &dwarf2_per_objfile->all_type_units);
6941
6942 return 1;
6943 }
6944
6945 /* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6946 If SLOT is non-NULL, it is the entry to use in the hash table.
6947 Otherwise we find one. */
6948
6949 static struct signatured_type *
6950 add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6951 void **slot)
6952 {
6953 struct objfile *objfile = dwarf2_per_objfile->objfile;
6954
6955 if (dwarf2_per_objfile->all_type_units.size ()
6956 == dwarf2_per_objfile->all_type_units.capacity ())
6957 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
6958
6959 signatured_type *sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6960 struct signatured_type);
6961
6962 dwarf2_per_objfile->all_type_units.push_back (sig_type);
6963 sig_type->signature = sig;
6964 sig_type->per_cu.is_debug_types = 1;
6965 if (dwarf2_per_objfile->using_index)
6966 {
6967 sig_type->per_cu.v.quick =
6968 OBSTACK_ZALLOC (&objfile->objfile_obstack,
6969 struct dwarf2_per_cu_quick_data);
6970 }
6971
6972 if (slot == NULL)
6973 {
6974 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
6975 sig_type, INSERT);
6976 }
6977 gdb_assert (*slot == NULL);
6978 *slot = sig_type;
6979 /* The rest of sig_type must be filled in by the caller. */
6980 return sig_type;
6981 }
6982
6983 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6984 Fill in SIG_ENTRY with DWO_ENTRY. */
6985
6986 static void
6987 fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
6988 struct signatured_type *sig_entry,
6989 struct dwo_unit *dwo_entry)
6990 {
6991 /* Make sure we're not clobbering something we don't expect to. */
6992 gdb_assert (! sig_entry->per_cu.queued);
6993 gdb_assert (sig_entry->per_cu.cu == NULL);
6994 if (dwarf2_per_objfile->using_index)
6995 {
6996 gdb_assert (sig_entry->per_cu.v.quick != NULL);
6997 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6998 }
6999 else
7000 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
7001 gdb_assert (sig_entry->signature == dwo_entry->signature);
7002 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
7003 gdb_assert (sig_entry->type_unit_group == NULL);
7004 gdb_assert (sig_entry->dwo_unit == NULL);
7005
7006 sig_entry->per_cu.section = dwo_entry->section;
7007 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7008 sig_entry->per_cu.length = dwo_entry->length;
7009 sig_entry->per_cu.reading_dwo_directly = 1;
7010 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
7011 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
7012 sig_entry->dwo_unit = dwo_entry;
7013 }
7014
7015 /* Subroutine of lookup_signatured_type.
7016 If we haven't read the TU yet, create the signatured_type data structure
7017 for a TU to be read in directly from a DWO file, bypassing the stub.
7018 This is the "Stay in DWO Optimization": When there is no DWP file and we're
7019 using .gdb_index, then when reading a CU we want to stay in the DWO file
7020 containing that CU. Otherwise we could end up reading several other DWO
7021 files (due to comdat folding) to process the transitive closure of all the
7022 mentioned TUs, and that can be slow. The current DWO file will have every
7023 type signature that it needs.
7024 We only do this for .gdb_index because in the psymtab case we already have
7025 to read all the DWOs to build the type unit groups. */
7026
7027 static struct signatured_type *
7028 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7029 {
7030 struct dwarf2_per_objfile *dwarf2_per_objfile
7031 = cu->per_cu->dwarf2_per_objfile;
7032 struct objfile *objfile = dwarf2_per_objfile->objfile;
7033 struct dwo_file *dwo_file;
7034 struct dwo_unit find_dwo_entry, *dwo_entry;
7035 struct signatured_type find_sig_entry, *sig_entry;
7036 void **slot;
7037
7038 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7039
7040 /* If TU skeletons have been removed then we may not have read in any
7041 TUs yet. */
7042 if (dwarf2_per_objfile->signatured_types == NULL)
7043 {
7044 dwarf2_per_objfile->signatured_types
7045 = allocate_signatured_type_table (objfile);
7046 }
7047
7048 /* We only ever need to read in one copy of a signatured type.
7049 Use the global signatured_types array to do our own comdat-folding
7050 of types. If this is the first time we're reading this TU, and
7051 the TU has an entry in .gdb_index, replace the recorded data from
7052 .gdb_index with this TU. */
7053
7054 find_sig_entry.signature = sig;
7055 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7056 &find_sig_entry, INSERT);
7057 sig_entry = (struct signatured_type *) *slot;
7058
7059 /* We can get here with the TU already read, *or* in the process of being
7060 read. Don't reassign the global entry to point to this DWO if that's
7061 the case. Also note that if the TU is already being read, it may not
7062 have come from a DWO, the program may be a mix of Fission-compiled
7063 code and non-Fission-compiled code. */
7064
7065 /* Have we already tried to read this TU?
7066 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7067 needn't exist in the global table yet). */
7068 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
7069 return sig_entry;
7070
7071 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
7072 dwo_unit of the TU itself. */
7073 dwo_file = cu->dwo_unit->dwo_file;
7074
7075 /* Ok, this is the first time we're reading this TU. */
7076 if (dwo_file->tus == NULL)
7077 return NULL;
7078 find_dwo_entry.signature = sig;
7079 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
7080 if (dwo_entry == NULL)
7081 return NULL;
7082
7083 /* If the global table doesn't have an entry for this TU, add one. */
7084 if (sig_entry == NULL)
7085 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
7086
7087 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
7088 sig_entry->per_cu.tu_read = 1;
7089 return sig_entry;
7090 }
7091
7092 /* Subroutine of lookup_signatured_type.
7093 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
7094 then try the DWP file. If the TU stub (skeleton) has been removed then
7095 it won't be in .gdb_index. */
7096
7097 static struct signatured_type *
7098 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7099 {
7100 struct dwarf2_per_objfile *dwarf2_per_objfile
7101 = cu->per_cu->dwarf2_per_objfile;
7102 struct objfile *objfile = dwarf2_per_objfile->objfile;
7103 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
7104 struct dwo_unit *dwo_entry;
7105 struct signatured_type find_sig_entry, *sig_entry;
7106 void **slot;
7107
7108 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7109 gdb_assert (dwp_file != NULL);
7110
7111 /* If TU skeletons have been removed then we may not have read in any
7112 TUs yet. */
7113 if (dwarf2_per_objfile->signatured_types == NULL)
7114 {
7115 dwarf2_per_objfile->signatured_types
7116 = allocate_signatured_type_table (objfile);
7117 }
7118
7119 find_sig_entry.signature = sig;
7120 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7121 &find_sig_entry, INSERT);
7122 sig_entry = (struct signatured_type *) *slot;
7123
7124 /* Have we already tried to read this TU?
7125 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7126 needn't exist in the global table yet). */
7127 if (sig_entry != NULL)
7128 return sig_entry;
7129
7130 if (dwp_file->tus == NULL)
7131 return NULL;
7132 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
7133 sig, 1 /* is_debug_types */);
7134 if (dwo_entry == NULL)
7135 return NULL;
7136
7137 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
7138 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
7139
7140 return sig_entry;
7141 }
7142
7143 /* Lookup a signature based type for DW_FORM_ref_sig8.
7144 Returns NULL if signature SIG is not present in the table.
7145 It is up to the caller to complain about this. */
7146
7147 static struct signatured_type *
7148 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7149 {
7150 struct dwarf2_per_objfile *dwarf2_per_objfile
7151 = cu->per_cu->dwarf2_per_objfile;
7152
7153 if (cu->dwo_unit
7154 && dwarf2_per_objfile->using_index)
7155 {
7156 /* We're in a DWO/DWP file, and we're using .gdb_index.
7157 These cases require special processing. */
7158 if (get_dwp_file (dwarf2_per_objfile) == NULL)
7159 return lookup_dwo_signatured_type (cu, sig);
7160 else
7161 return lookup_dwp_signatured_type (cu, sig);
7162 }
7163 else
7164 {
7165 struct signatured_type find_entry, *entry;
7166
7167 if (dwarf2_per_objfile->signatured_types == NULL)
7168 return NULL;
7169 find_entry.signature = sig;
7170 entry = ((struct signatured_type *)
7171 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
7172 return entry;
7173 }
7174 }
7175 \f
7176 /* Low level DIE reading support. */
7177
7178 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
7179
7180 static void
7181 init_cu_die_reader (struct die_reader_specs *reader,
7182 struct dwarf2_cu *cu,
7183 struct dwarf2_section_info *section,
7184 struct dwo_file *dwo_file,
7185 struct abbrev_table *abbrev_table)
7186 {
7187 gdb_assert (section->readin && section->buffer != NULL);
7188 reader->abfd = get_section_bfd_owner (section);
7189 reader->cu = cu;
7190 reader->dwo_file = dwo_file;
7191 reader->die_section = section;
7192 reader->buffer = section->buffer;
7193 reader->buffer_end = section->buffer + section->size;
7194 reader->comp_dir = NULL;
7195 reader->abbrev_table = abbrev_table;
7196 }
7197
7198 /* Subroutine of init_cutu_and_read_dies to simplify it.
7199 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
7200 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
7201 already.
7202
7203 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
7204 from it to the DIE in the DWO. If NULL we are skipping the stub.
7205 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
7206 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
7207 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
7208 STUB_COMP_DIR may be non-NULL.
7209 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
7210 are filled in with the info of the DIE from the DWO file.
7211 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
7212 from the dwo. Since *RESULT_READER references this abbrev table, it must be
7213 kept around for at least as long as *RESULT_READER.
7214
7215 The result is non-zero if a valid (non-dummy) DIE was found. */
7216
7217 static int
7218 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
7219 struct dwo_unit *dwo_unit,
7220 struct die_info *stub_comp_unit_die,
7221 const char *stub_comp_dir,
7222 struct die_reader_specs *result_reader,
7223 const gdb_byte **result_info_ptr,
7224 struct die_info **result_comp_unit_die,
7225 int *result_has_children,
7226 abbrev_table_up *result_dwo_abbrev_table)
7227 {
7228 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7229 struct objfile *objfile = dwarf2_per_objfile->objfile;
7230 struct dwarf2_cu *cu = this_cu->cu;
7231 bfd *abfd;
7232 const gdb_byte *begin_info_ptr, *info_ptr;
7233 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
7234 int i,num_extra_attrs;
7235 struct dwarf2_section_info *dwo_abbrev_section;
7236 struct attribute *attr;
7237 struct die_info *comp_unit_die;
7238
7239 /* At most one of these may be provided. */
7240 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
7241
7242 /* These attributes aren't processed until later:
7243 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
7244 DW_AT_comp_dir is used now, to find the DWO file, but it is also
7245 referenced later. However, these attributes are found in the stub
7246 which we won't have later. In order to not impose this complication
7247 on the rest of the code, we read them here and copy them to the
7248 DWO CU/TU die. */
7249
7250 stmt_list = NULL;
7251 low_pc = NULL;
7252 high_pc = NULL;
7253 ranges = NULL;
7254 comp_dir = NULL;
7255
7256 if (stub_comp_unit_die != NULL)
7257 {
7258 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
7259 DWO file. */
7260 if (! this_cu->is_debug_types)
7261 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
7262 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
7263 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
7264 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
7265 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
7266
7267 /* There should be a DW_AT_addr_base attribute here (if needed).
7268 We need the value before we can process DW_FORM_GNU_addr_index
7269 or DW_FORM_addrx. */
7270 cu->addr_base = 0;
7271 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
7272 if (attr != nullptr)
7273 cu->addr_base = DW_UNSND (attr);
7274
7275 /* There should be a DW_AT_ranges_base attribute here (if needed).
7276 We need the value before we can process DW_AT_ranges. */
7277 cu->ranges_base = 0;
7278 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
7279 if (attr != nullptr)
7280 cu->ranges_base = DW_UNSND (attr);
7281 }
7282 else if (stub_comp_dir != NULL)
7283 {
7284 /* Reconstruct the comp_dir attribute to simplify the code below. */
7285 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
7286 comp_dir->name = DW_AT_comp_dir;
7287 comp_dir->form = DW_FORM_string;
7288 DW_STRING_IS_CANONICAL (comp_dir) = 0;
7289 DW_STRING (comp_dir) = stub_comp_dir;
7290 }
7291
7292 /* Set up for reading the DWO CU/TU. */
7293 cu->dwo_unit = dwo_unit;
7294 dwarf2_section_info *section = dwo_unit->section;
7295 dwarf2_read_section (objfile, section);
7296 abfd = get_section_bfd_owner (section);
7297 begin_info_ptr = info_ptr = (section->buffer
7298 + to_underlying (dwo_unit->sect_off));
7299 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
7300
7301 if (this_cu->is_debug_types)
7302 {
7303 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
7304
7305 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7306 &cu->header, section,
7307 dwo_abbrev_section,
7308 info_ptr, rcuh_kind::TYPE);
7309 /* This is not an assert because it can be caused by bad debug info. */
7310 if (sig_type->signature != cu->header.signature)
7311 {
7312 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
7313 " TU at offset %s [in module %s]"),
7314 hex_string (sig_type->signature),
7315 hex_string (cu->header.signature),
7316 sect_offset_str (dwo_unit->sect_off),
7317 bfd_get_filename (abfd));
7318 }
7319 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7320 /* For DWOs coming from DWP files, we don't know the CU length
7321 nor the type's offset in the TU until now. */
7322 dwo_unit->length = get_cu_length (&cu->header);
7323 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
7324
7325 /* Establish the type offset that can be used to lookup the type.
7326 For DWO files, we don't know it until now. */
7327 sig_type->type_offset_in_section
7328 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
7329 }
7330 else
7331 {
7332 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7333 &cu->header, section,
7334 dwo_abbrev_section,
7335 info_ptr, rcuh_kind::COMPILE);
7336 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7337 /* For DWOs coming from DWP files, we don't know the CU length
7338 until now. */
7339 dwo_unit->length = get_cu_length (&cu->header);
7340 }
7341
7342 *result_dwo_abbrev_table
7343 = abbrev_table_read_table (dwarf2_per_objfile, dwo_abbrev_section,
7344 cu->header.abbrev_sect_off);
7345 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
7346 result_dwo_abbrev_table->get ());
7347
7348 /* Read in the die, but leave space to copy over the attributes
7349 from the stub. This has the benefit of simplifying the rest of
7350 the code - all the work to maintain the illusion of a single
7351 DW_TAG_{compile,type}_unit DIE is done here. */
7352 num_extra_attrs = ((stmt_list != NULL)
7353 + (low_pc != NULL)
7354 + (high_pc != NULL)
7355 + (ranges != NULL)
7356 + (comp_dir != NULL));
7357 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
7358 result_has_children, num_extra_attrs);
7359
7360 /* Copy over the attributes from the stub to the DIE we just read in. */
7361 comp_unit_die = *result_comp_unit_die;
7362 i = comp_unit_die->num_attrs;
7363 if (stmt_list != NULL)
7364 comp_unit_die->attrs[i++] = *stmt_list;
7365 if (low_pc != NULL)
7366 comp_unit_die->attrs[i++] = *low_pc;
7367 if (high_pc != NULL)
7368 comp_unit_die->attrs[i++] = *high_pc;
7369 if (ranges != NULL)
7370 comp_unit_die->attrs[i++] = *ranges;
7371 if (comp_dir != NULL)
7372 comp_unit_die->attrs[i++] = *comp_dir;
7373 comp_unit_die->num_attrs += num_extra_attrs;
7374
7375 if (dwarf_die_debug)
7376 {
7377 fprintf_unfiltered (gdb_stdlog,
7378 "Read die from %s@0x%x of %s:\n",
7379 get_section_name (section),
7380 (unsigned) (begin_info_ptr - section->buffer),
7381 bfd_get_filename (abfd));
7382 dump_die (comp_unit_die, dwarf_die_debug);
7383 }
7384
7385 /* Save the comp_dir attribute. If there is no DWP file then we'll read
7386 TUs by skipping the stub and going directly to the entry in the DWO file.
7387 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
7388 to get it via circuitous means. Blech. */
7389 if (comp_dir != NULL)
7390 result_reader->comp_dir = DW_STRING (comp_dir);
7391
7392 /* Skip dummy compilation units. */
7393 if (info_ptr >= begin_info_ptr + dwo_unit->length
7394 || peek_abbrev_code (abfd, info_ptr) == 0)
7395 return 0;
7396
7397 *result_info_ptr = info_ptr;
7398 return 1;
7399 }
7400
7401 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
7402 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
7403 signature is part of the header. */
7404 static gdb::optional<ULONGEST>
7405 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
7406 {
7407 if (cu->header.version >= 5)
7408 return cu->header.signature;
7409 struct attribute *attr;
7410 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7411 if (attr == nullptr)
7412 return gdb::optional<ULONGEST> ();
7413 return DW_UNSND (attr);
7414 }
7415
7416 /* Subroutine of init_cutu_and_read_dies to simplify it.
7417 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
7418 Returns NULL if the specified DWO unit cannot be found. */
7419
7420 static struct dwo_unit *
7421 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
7422 struct die_info *comp_unit_die)
7423 {
7424 struct dwarf2_cu *cu = this_cu->cu;
7425 struct dwo_unit *dwo_unit;
7426 const char *comp_dir, *dwo_name;
7427
7428 gdb_assert (cu != NULL);
7429
7430 /* Yeah, we look dwo_name up again, but it simplifies the code. */
7431 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7432 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7433
7434 if (this_cu->is_debug_types)
7435 {
7436 struct signatured_type *sig_type;
7437
7438 /* Since this_cu is the first member of struct signatured_type,
7439 we can go from a pointer to one to a pointer to the other. */
7440 sig_type = (struct signatured_type *) this_cu;
7441 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
7442 }
7443 else
7444 {
7445 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
7446 if (!signature.has_value ())
7447 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7448 " [in module %s]"),
7449 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
7450 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
7451 *signature);
7452 }
7453
7454 return dwo_unit;
7455 }
7456
7457 /* Subroutine of init_cutu_and_read_dies to simplify it.
7458 See it for a description of the parameters.
7459 Read a TU directly from a DWO file, bypassing the stub. */
7460
7461 static void
7462 init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
7463 int use_existing_cu, int keep,
7464 die_reader_func_ftype *die_reader_func,
7465 void *data)
7466 {
7467 std::unique_ptr<dwarf2_cu> new_cu;
7468 struct signatured_type *sig_type;
7469 struct die_reader_specs reader;
7470 const gdb_byte *info_ptr;
7471 struct die_info *comp_unit_die;
7472 int has_children;
7473 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7474
7475 /* Verify we can do the following downcast, and that we have the
7476 data we need. */
7477 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7478 sig_type = (struct signatured_type *) this_cu;
7479 gdb_assert (sig_type->dwo_unit != NULL);
7480
7481 if (use_existing_cu && this_cu->cu != NULL)
7482 {
7483 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
7484 /* There's no need to do the rereading_dwo_cu handling that
7485 init_cutu_and_read_dies does since we don't read the stub. */
7486 }
7487 else
7488 {
7489 /* If !use_existing_cu, this_cu->cu must be NULL. */
7490 gdb_assert (this_cu->cu == NULL);
7491 new_cu.reset (new dwarf2_cu (this_cu));
7492 }
7493
7494 /* A future optimization, if needed, would be to use an existing
7495 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7496 could share abbrev tables. */
7497
7498 /* The abbreviation table used by READER, this must live at least as long as
7499 READER. */
7500 abbrev_table_up dwo_abbrev_table;
7501
7502 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
7503 NULL /* stub_comp_unit_die */,
7504 sig_type->dwo_unit->dwo_file->comp_dir,
7505 &reader, &info_ptr,
7506 &comp_unit_die, &has_children,
7507 &dwo_abbrev_table) == 0)
7508 {
7509 /* Dummy die. */
7510 return;
7511 }
7512
7513 /* All the "real" work is done here. */
7514 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7515
7516 /* This duplicates the code in init_cutu_and_read_dies,
7517 but the alternative is making the latter more complex.
7518 This function is only for the special case of using DWO files directly:
7519 no point in overly complicating the general case just to handle this. */
7520 if (new_cu != NULL && keep)
7521 {
7522 /* Link this CU into read_in_chain. */
7523 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7524 dwarf2_per_objfile->read_in_chain = this_cu;
7525 /* The chain owns it now. */
7526 new_cu.release ();
7527 }
7528 }
7529
7530 /* Initialize a CU (or TU) and read its DIEs.
7531 If the CU defers to a DWO file, read the DWO file as well.
7532
7533 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7534 Otherwise the table specified in the comp unit header is read in and used.
7535 This is an optimization for when we already have the abbrev table.
7536
7537 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
7538 Otherwise, a new CU is allocated with xmalloc.
7539
7540 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
7541 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
7542
7543 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
7544 linker) then DIE_READER_FUNC will not get called. */
7545
7546 static void
7547 init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
7548 struct abbrev_table *abbrev_table,
7549 int use_existing_cu, int keep,
7550 bool skip_partial,
7551 die_reader_func_ftype *die_reader_func,
7552 void *data)
7553 {
7554 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7555 struct objfile *objfile = dwarf2_per_objfile->objfile;
7556 struct dwarf2_section_info *section = this_cu->section;
7557 bfd *abfd = get_section_bfd_owner (section);
7558 struct dwarf2_cu *cu;
7559 const gdb_byte *begin_info_ptr, *info_ptr;
7560 struct die_reader_specs reader;
7561 struct die_info *comp_unit_die;
7562 int has_children;
7563 struct signatured_type *sig_type = NULL;
7564 struct dwarf2_section_info *abbrev_section;
7565 /* Non-zero if CU currently points to a DWO file and we need to
7566 reread it. When this happens we need to reread the skeleton die
7567 before we can reread the DWO file (this only applies to CUs, not TUs). */
7568 int rereading_dwo_cu = 0;
7569
7570 if (dwarf_die_debug)
7571 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7572 this_cu->is_debug_types ? "type" : "comp",
7573 sect_offset_str (this_cu->sect_off));
7574
7575 if (use_existing_cu)
7576 gdb_assert (keep);
7577
7578 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7579 file (instead of going through the stub), short-circuit all of this. */
7580 if (this_cu->reading_dwo_directly)
7581 {
7582 /* Narrow down the scope of possibilities to have to understand. */
7583 gdb_assert (this_cu->is_debug_types);
7584 gdb_assert (abbrev_table == NULL);
7585 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
7586 die_reader_func, data);
7587 return;
7588 }
7589
7590 /* This is cheap if the section is already read in. */
7591 dwarf2_read_section (objfile, section);
7592
7593 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7594
7595 abbrev_section = get_abbrev_section_for_cu (this_cu);
7596
7597 std::unique_ptr<dwarf2_cu> new_cu;
7598 if (use_existing_cu && this_cu->cu != NULL)
7599 {
7600 cu = this_cu->cu;
7601 /* If this CU is from a DWO file we need to start over, we need to
7602 refetch the attributes from the skeleton CU.
7603 This could be optimized by retrieving those attributes from when we
7604 were here the first time: the previous comp_unit_die was stored in
7605 comp_unit_obstack. But there's no data yet that we need this
7606 optimization. */
7607 if (cu->dwo_unit != NULL)
7608 rereading_dwo_cu = 1;
7609 }
7610 else
7611 {
7612 /* If !use_existing_cu, this_cu->cu must be NULL. */
7613 gdb_assert (this_cu->cu == NULL);
7614 new_cu.reset (new dwarf2_cu (this_cu));
7615 cu = new_cu.get ();
7616 }
7617
7618 /* Get the header. */
7619 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
7620 {
7621 /* We already have the header, there's no need to read it in again. */
7622 info_ptr += to_underlying (cu->header.first_die_cu_offset);
7623 }
7624 else
7625 {
7626 if (this_cu->is_debug_types)
7627 {
7628 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7629 &cu->header, section,
7630 abbrev_section, info_ptr,
7631 rcuh_kind::TYPE);
7632
7633 /* Since per_cu is the first member of struct signatured_type,
7634 we can go from a pointer to one to a pointer to the other. */
7635 sig_type = (struct signatured_type *) this_cu;
7636 gdb_assert (sig_type->signature == cu->header.signature);
7637 gdb_assert (sig_type->type_offset_in_tu
7638 == cu->header.type_cu_offset_in_tu);
7639 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7640
7641 /* LENGTH has not been set yet for type units if we're
7642 using .gdb_index. */
7643 this_cu->length = get_cu_length (&cu->header);
7644
7645 /* Establish the type offset that can be used to lookup the type. */
7646 sig_type->type_offset_in_section =
7647 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
7648
7649 this_cu->dwarf_version = cu->header.version;
7650 }
7651 else
7652 {
7653 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7654 &cu->header, section,
7655 abbrev_section,
7656 info_ptr,
7657 rcuh_kind::COMPILE);
7658
7659 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7660 gdb_assert (this_cu->length == get_cu_length (&cu->header));
7661 this_cu->dwarf_version = cu->header.version;
7662 }
7663 }
7664
7665 /* Skip dummy compilation units. */
7666 if (info_ptr >= begin_info_ptr + this_cu->length
7667 || peek_abbrev_code (abfd, info_ptr) == 0)
7668 return;
7669
7670 /* If we don't have them yet, read the abbrevs for this compilation unit.
7671 And if we need to read them now, make sure they're freed when we're
7672 done (own the table through ABBREV_TABLE_HOLDER). */
7673 abbrev_table_up abbrev_table_holder;
7674 if (abbrev_table != NULL)
7675 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7676 else
7677 {
7678 abbrev_table_holder
7679 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7680 cu->header.abbrev_sect_off);
7681 abbrev_table = abbrev_table_holder.get ();
7682 }
7683
7684 /* Read the top level CU/TU die. */
7685 init_cu_die_reader (&reader, cu, section, NULL, abbrev_table);
7686 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
7687
7688 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
7689 return;
7690
7691 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
7692 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7693 table from the DWO file and pass the ownership over to us. It will be
7694 referenced from READER, so we must make sure to free it after we're done
7695 with READER.
7696
7697 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7698 DWO CU, that this test will fail (the attribute will not be present). */
7699 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7700 abbrev_table_up dwo_abbrev_table;
7701 if (dwo_name != nullptr)
7702 {
7703 struct dwo_unit *dwo_unit;
7704 struct die_info *dwo_comp_unit_die;
7705
7706 if (has_children)
7707 {
7708 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
7709 " has children (offset %s) [in module %s]"),
7710 sect_offset_str (this_cu->sect_off),
7711 bfd_get_filename (abfd));
7712 }
7713 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
7714 if (dwo_unit != NULL)
7715 {
7716 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
7717 comp_unit_die, NULL,
7718 &reader, &info_ptr,
7719 &dwo_comp_unit_die, &has_children,
7720 &dwo_abbrev_table) == 0)
7721 {
7722 /* Dummy die. */
7723 return;
7724 }
7725 comp_unit_die = dwo_comp_unit_die;
7726 }
7727 else
7728 {
7729 /* Yikes, we couldn't find the rest of the DIE, we only have
7730 the stub. A complaint has already been logged. There's
7731 not much more we can do except pass on the stub DIE to
7732 die_reader_func. We don't want to throw an error on bad
7733 debug info. */
7734 }
7735 }
7736
7737 /* All of the above is setup for this call. Yikes. */
7738 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7739
7740 /* Done, clean up. */
7741 if (new_cu != NULL && keep)
7742 {
7743 /* Link this CU into read_in_chain. */
7744 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7745 dwarf2_per_objfile->read_in_chain = this_cu;
7746 /* The chain owns it now. */
7747 new_cu.release ();
7748 }
7749 }
7750
7751 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
7752 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed
7753 to have already done the lookup to find the DWO file).
7754
7755 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
7756 THIS_CU->is_debug_types, but nothing else.
7757
7758 We fill in THIS_CU->length.
7759
7760 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
7761 linker) then DIE_READER_FUNC will not get called.
7762
7763 THIS_CU->cu is always freed when done.
7764 This is done in order to not leave THIS_CU->cu in a state where we have
7765 to care whether it refers to the "main" CU or the DWO CU. */
7766
7767 static void
7768 init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
7769 struct dwo_file *dwo_file,
7770 die_reader_func_ftype *die_reader_func,
7771 void *data)
7772 {
7773 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7774 struct objfile *objfile = dwarf2_per_objfile->objfile;
7775 struct dwarf2_section_info *section = this_cu->section;
7776 bfd *abfd = get_section_bfd_owner (section);
7777 struct dwarf2_section_info *abbrev_section;
7778 const gdb_byte *begin_info_ptr, *info_ptr;
7779 struct die_reader_specs reader;
7780 struct die_info *comp_unit_die;
7781 int has_children;
7782
7783 if (dwarf_die_debug)
7784 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7785 this_cu->is_debug_types ? "type" : "comp",
7786 sect_offset_str (this_cu->sect_off));
7787
7788 gdb_assert (this_cu->cu == NULL);
7789
7790 abbrev_section = (dwo_file != NULL
7791 ? &dwo_file->sections.abbrev
7792 : get_abbrev_section_for_cu (this_cu));
7793
7794 /* This is cheap if the section is already read in. */
7795 dwarf2_read_section (objfile, section);
7796
7797 struct dwarf2_cu cu (this_cu);
7798
7799 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7800 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7801 &cu.header, section,
7802 abbrev_section, info_ptr,
7803 (this_cu->is_debug_types
7804 ? rcuh_kind::TYPE
7805 : rcuh_kind::COMPILE));
7806
7807 this_cu->length = get_cu_length (&cu.header);
7808
7809 /* Skip dummy compilation units. */
7810 if (info_ptr >= begin_info_ptr + this_cu->length
7811 || peek_abbrev_code (abfd, info_ptr) == 0)
7812 return;
7813
7814 abbrev_table_up abbrev_table
7815 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7816 cu.header.abbrev_sect_off);
7817
7818 init_cu_die_reader (&reader, &cu, section, dwo_file, abbrev_table.get ());
7819 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
7820
7821 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7822 }
7823
7824 /* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
7825 does not lookup the specified DWO file.
7826 This cannot be used to read DWO files.
7827
7828 THIS_CU->cu is always freed when done.
7829 This is done in order to not leave THIS_CU->cu in a state where we have
7830 to care whether it refers to the "main" CU or the DWO CU.
7831 We can revisit this if the data shows there's a performance issue. */
7832
7833 static void
7834 init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
7835 die_reader_func_ftype *die_reader_func,
7836 void *data)
7837 {
7838 init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
7839 }
7840 \f
7841 /* Type Unit Groups.
7842
7843 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7844 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7845 so that all types coming from the same compilation (.o file) are grouped
7846 together. A future step could be to put the types in the same symtab as
7847 the CU the types ultimately came from. */
7848
7849 static hashval_t
7850 hash_type_unit_group (const void *item)
7851 {
7852 const struct type_unit_group *tu_group
7853 = (const struct type_unit_group *) item;
7854
7855 return hash_stmt_list_entry (&tu_group->hash);
7856 }
7857
7858 static int
7859 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
7860 {
7861 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7862 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
7863
7864 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
7865 }
7866
7867 /* Allocate a hash table for type unit groups. */
7868
7869 static htab_t
7870 allocate_type_unit_groups_table (struct objfile *objfile)
7871 {
7872 return htab_create_alloc_ex (3,
7873 hash_type_unit_group,
7874 eq_type_unit_group,
7875 NULL,
7876 &objfile->objfile_obstack,
7877 hashtab_obstack_allocate,
7878 dummy_obstack_deallocate);
7879 }
7880
7881 /* Type units that don't have DW_AT_stmt_list are grouped into their own
7882 partial symtabs. We combine several TUs per psymtab to not let the size
7883 of any one psymtab grow too big. */
7884 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7885 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
7886
7887 /* Helper routine for get_type_unit_group.
7888 Create the type_unit_group object used to hold one or more TUs. */
7889
7890 static struct type_unit_group *
7891 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
7892 {
7893 struct dwarf2_per_objfile *dwarf2_per_objfile
7894 = cu->per_cu->dwarf2_per_objfile;
7895 struct objfile *objfile = dwarf2_per_objfile->objfile;
7896 struct dwarf2_per_cu_data *per_cu;
7897 struct type_unit_group *tu_group;
7898
7899 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7900 struct type_unit_group);
7901 per_cu = &tu_group->per_cu;
7902 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
7903
7904 if (dwarf2_per_objfile->using_index)
7905 {
7906 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7907 struct dwarf2_per_cu_quick_data);
7908 }
7909 else
7910 {
7911 unsigned int line_offset = to_underlying (line_offset_struct);
7912 struct partial_symtab *pst;
7913 std::string name;
7914
7915 /* Give the symtab a useful name for debug purposes. */
7916 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7917 name = string_printf ("<type_units_%d>",
7918 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7919 else
7920 name = string_printf ("<type_units_at_0x%x>", line_offset);
7921
7922 pst = create_partial_symtab (per_cu, name.c_str ());
7923 pst->anonymous = 1;
7924 }
7925
7926 tu_group->hash.dwo_unit = cu->dwo_unit;
7927 tu_group->hash.line_sect_off = line_offset_struct;
7928
7929 return tu_group;
7930 }
7931
7932 /* Look up the type_unit_group for type unit CU, and create it if necessary.
7933 STMT_LIST is a DW_AT_stmt_list attribute. */
7934
7935 static struct type_unit_group *
7936 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7937 {
7938 struct dwarf2_per_objfile *dwarf2_per_objfile
7939 = cu->per_cu->dwarf2_per_objfile;
7940 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7941 struct type_unit_group *tu_group;
7942 void **slot;
7943 unsigned int line_offset;
7944 struct type_unit_group type_unit_group_for_lookup;
7945
7946 if (dwarf2_per_objfile->type_unit_groups == NULL)
7947 {
7948 dwarf2_per_objfile->type_unit_groups =
7949 allocate_type_unit_groups_table (dwarf2_per_objfile->objfile);
7950 }
7951
7952 /* Do we need to create a new group, or can we use an existing one? */
7953
7954 if (stmt_list)
7955 {
7956 line_offset = DW_UNSND (stmt_list);
7957 ++tu_stats->nr_symtab_sharers;
7958 }
7959 else
7960 {
7961 /* Ugh, no stmt_list. Rare, but we have to handle it.
7962 We can do various things here like create one group per TU or
7963 spread them over multiple groups to split up the expansion work.
7964 To avoid worst case scenarios (too many groups or too large groups)
7965 we, umm, group them in bunches. */
7966 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7967 | (tu_stats->nr_stmt_less_type_units
7968 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7969 ++tu_stats->nr_stmt_less_type_units;
7970 }
7971
7972 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7973 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7974 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
7975 &type_unit_group_for_lookup, INSERT);
7976 if (*slot != NULL)
7977 {
7978 tu_group = (struct type_unit_group *) *slot;
7979 gdb_assert (tu_group != NULL);
7980 }
7981 else
7982 {
7983 sect_offset line_offset_struct = (sect_offset) line_offset;
7984 tu_group = create_type_unit_group (cu, line_offset_struct);
7985 *slot = tu_group;
7986 ++tu_stats->nr_symtabs;
7987 }
7988
7989 return tu_group;
7990 }
7991 \f
7992 /* Partial symbol tables. */
7993
7994 /* Create a psymtab named NAME and assign it to PER_CU.
7995
7996 The caller must fill in the following details:
7997 dirname, textlow, texthigh. */
7998
7999 static struct partial_symtab *
8000 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
8001 {
8002 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
8003 struct partial_symtab *pst;
8004
8005 pst = start_psymtab_common (objfile, name, 0);
8006
8007 pst->psymtabs_addrmap_supported = 1;
8008
8009 /* This is the glue that links PST into GDB's symbol API. */
8010 pst->read_symtab_private = per_cu;
8011 pst->read_symtab = dwarf2_read_symtab;
8012 per_cu->v.psymtab = pst;
8013
8014 return pst;
8015 }
8016
8017 /* The DATA object passed to process_psymtab_comp_unit_reader has this
8018 type. */
8019
8020 struct process_psymtab_comp_unit_data
8021 {
8022 /* True if we are reading a DW_TAG_partial_unit. */
8023
8024 int want_partial_unit;
8025
8026 /* The "pretend" language that is used if the CU doesn't declare a
8027 language. */
8028
8029 enum language pretend_language;
8030 };
8031
8032 /* die_reader_func for process_psymtab_comp_unit. */
8033
8034 static void
8035 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
8036 const gdb_byte *info_ptr,
8037 struct die_info *comp_unit_die,
8038 int has_children,
8039 void *data)
8040 {
8041 struct dwarf2_cu *cu = reader->cu;
8042 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
8043 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8044 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8045 CORE_ADDR baseaddr;
8046 CORE_ADDR best_lowpc = 0, best_highpc = 0;
8047 struct partial_symtab *pst;
8048 enum pc_bounds_kind cu_bounds_kind;
8049 const char *filename;
8050 struct process_psymtab_comp_unit_data *info
8051 = (struct process_psymtab_comp_unit_data *) data;
8052
8053 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
8054 return;
8055
8056 gdb_assert (! per_cu->is_debug_types);
8057
8058 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
8059
8060 /* Allocate a new partial symbol table structure. */
8061 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
8062 if (filename == NULL)
8063 filename = "";
8064
8065 pst = create_partial_symtab (per_cu, filename);
8066
8067 /* This must be done before calling dwarf2_build_include_psymtabs. */
8068 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
8069
8070 baseaddr = objfile->section_offsets[SECT_OFF_TEXT (objfile)];
8071
8072 dwarf2_find_base_address (comp_unit_die, cu);
8073
8074 /* Possibly set the default values of LOWPC and HIGHPC from
8075 `DW_AT_ranges'. */
8076 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
8077 &best_highpc, cu, pst);
8078 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
8079 {
8080 CORE_ADDR low
8081 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
8082 - baseaddr);
8083 CORE_ADDR high
8084 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
8085 - baseaddr - 1);
8086 /* Store the contiguous range if it is not empty; it can be
8087 empty for CUs with no code. */
8088 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8089 low, high, pst);
8090 }
8091
8092 /* Check if comp unit has_children.
8093 If so, read the rest of the partial symbols from this comp unit.
8094 If not, there's no more debug_info for this comp unit. */
8095 if (has_children)
8096 {
8097 struct partial_die_info *first_die;
8098 CORE_ADDR lowpc, highpc;
8099
8100 lowpc = ((CORE_ADDR) -1);
8101 highpc = ((CORE_ADDR) 0);
8102
8103 first_die = load_partial_dies (reader, info_ptr, 1);
8104
8105 scan_partial_symbols (first_die, &lowpc, &highpc,
8106 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
8107
8108 /* If we didn't find a lowpc, set it to highpc to avoid
8109 complaints from `maint check'. */
8110 if (lowpc == ((CORE_ADDR) -1))
8111 lowpc = highpc;
8112
8113 /* If the compilation unit didn't have an explicit address range,
8114 then use the information extracted from its child dies. */
8115 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
8116 {
8117 best_lowpc = lowpc;
8118 best_highpc = highpc;
8119 }
8120 }
8121 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
8122 best_lowpc + baseaddr)
8123 - baseaddr);
8124 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
8125 best_highpc + baseaddr)
8126 - baseaddr);
8127
8128 end_psymtab_common (objfile, pst);
8129
8130 if (!cu->per_cu->imported_symtabs_empty ())
8131 {
8132 int i;
8133 int len = cu->per_cu->imported_symtabs_size ();
8134
8135 /* Fill in 'dependencies' here; we fill in 'users' in a
8136 post-pass. */
8137 pst->number_of_dependencies = len;
8138 pst->dependencies
8139 = objfile->partial_symtabs->allocate_dependencies (len);
8140 for (i = 0; i < len; ++i)
8141 {
8142 pst->dependencies[i]
8143 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
8144 }
8145
8146 cu->per_cu->imported_symtabs_free ();
8147 }
8148
8149 /* Get the list of files included in the current compilation unit,
8150 and build a psymtab for each of them. */
8151 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
8152
8153 if (dwarf_read_debug)
8154 fprintf_unfiltered (gdb_stdlog,
8155 "Psymtab for %s unit @%s: %s - %s"
8156 ", %d global, %d static syms\n",
8157 per_cu->is_debug_types ? "type" : "comp",
8158 sect_offset_str (per_cu->sect_off),
8159 paddress (gdbarch, pst->text_low (objfile)),
8160 paddress (gdbarch, pst->text_high (objfile)),
8161 pst->n_global_syms, pst->n_static_syms);
8162 }
8163
8164 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8165 Process compilation unit THIS_CU for a psymtab. */
8166
8167 static void
8168 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
8169 int want_partial_unit,
8170 enum language pretend_language)
8171 {
8172 /* If this compilation unit was already read in, free the
8173 cached copy in order to read it in again. This is
8174 necessary because we skipped some symbols when we first
8175 read in the compilation unit (see load_partial_dies).
8176 This problem could be avoided, but the benefit is unclear. */
8177 if (this_cu->cu != NULL)
8178 free_one_cached_comp_unit (this_cu);
8179
8180 if (this_cu->is_debug_types)
8181 init_cutu_and_read_dies (this_cu, NULL, 0, 0, false,
8182 build_type_psymtabs_reader, NULL);
8183 else
8184 {
8185 process_psymtab_comp_unit_data info;
8186 info.want_partial_unit = want_partial_unit;
8187 info.pretend_language = pretend_language;
8188 init_cutu_and_read_dies (this_cu, NULL, 0, 0, false,
8189 process_psymtab_comp_unit_reader, &info);
8190 }
8191
8192 /* Age out any secondary CUs. */
8193 age_cached_comp_units (this_cu->dwarf2_per_objfile);
8194 }
8195
8196 /* Reader function for build_type_psymtabs. */
8197
8198 static void
8199 build_type_psymtabs_reader (const struct die_reader_specs *reader,
8200 const gdb_byte *info_ptr,
8201 struct die_info *type_unit_die,
8202 int has_children,
8203 void *data)
8204 {
8205 struct dwarf2_per_objfile *dwarf2_per_objfile
8206 = reader->cu->per_cu->dwarf2_per_objfile;
8207 struct objfile *objfile = dwarf2_per_objfile->objfile;
8208 struct dwarf2_cu *cu = reader->cu;
8209 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8210 struct signatured_type *sig_type;
8211 struct type_unit_group *tu_group;
8212 struct attribute *attr;
8213 struct partial_die_info *first_die;
8214 CORE_ADDR lowpc, highpc;
8215 struct partial_symtab *pst;
8216
8217 gdb_assert (data == NULL);
8218 gdb_assert (per_cu->is_debug_types);
8219 sig_type = (struct signatured_type *) per_cu;
8220
8221 if (! has_children)
8222 return;
8223
8224 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
8225 tu_group = get_type_unit_group (cu, attr);
8226
8227 if (tu_group->tus == nullptr)
8228 tu_group->tus = new std::vector<signatured_type *>;
8229 tu_group->tus->push_back (sig_type);
8230
8231 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
8232 pst = create_partial_symtab (per_cu, "");
8233 pst->anonymous = 1;
8234
8235 first_die = load_partial_dies (reader, info_ptr, 1);
8236
8237 lowpc = (CORE_ADDR) -1;
8238 highpc = (CORE_ADDR) 0;
8239 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
8240
8241 end_psymtab_common (objfile, pst);
8242 }
8243
8244 /* Struct used to sort TUs by their abbreviation table offset. */
8245
8246 struct tu_abbrev_offset
8247 {
8248 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
8249 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
8250 {}
8251
8252 signatured_type *sig_type;
8253 sect_offset abbrev_offset;
8254 };
8255
8256 /* Helper routine for build_type_psymtabs_1, passed to std::sort. */
8257
8258 static bool
8259 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
8260 const struct tu_abbrev_offset &b)
8261 {
8262 return a.abbrev_offset < b.abbrev_offset;
8263 }
8264
8265 /* Efficiently read all the type units.
8266 This does the bulk of the work for build_type_psymtabs.
8267
8268 The efficiency is because we sort TUs by the abbrev table they use and
8269 only read each abbrev table once. In one program there are 200K TUs
8270 sharing 8K abbrev tables.
8271
8272 The main purpose of this function is to support building the
8273 dwarf2_per_objfile->type_unit_groups table.
8274 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
8275 can collapse the search space by grouping them by stmt_list.
8276 The savings can be significant, in the same program from above the 200K TUs
8277 share 8K stmt_list tables.
8278
8279 FUNC is expected to call get_type_unit_group, which will create the
8280 struct type_unit_group if necessary and add it to
8281 dwarf2_per_objfile->type_unit_groups. */
8282
8283 static void
8284 build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
8285 {
8286 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8287 abbrev_table_up abbrev_table;
8288 sect_offset abbrev_offset;
8289
8290 /* It's up to the caller to not call us multiple times. */
8291 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
8292
8293 if (dwarf2_per_objfile->all_type_units.empty ())
8294 return;
8295
8296 /* TUs typically share abbrev tables, and there can be way more TUs than
8297 abbrev tables. Sort by abbrev table to reduce the number of times we
8298 read each abbrev table in.
8299 Alternatives are to punt or to maintain a cache of abbrev tables.
8300 This is simpler and efficient enough for now.
8301
8302 Later we group TUs by their DW_AT_stmt_list value (as this defines the
8303 symtab to use). Typically TUs with the same abbrev offset have the same
8304 stmt_list value too so in practice this should work well.
8305
8306 The basic algorithm here is:
8307
8308 sort TUs by abbrev table
8309 for each TU with same abbrev table:
8310 read abbrev table if first user
8311 read TU top level DIE
8312 [IWBN if DWO skeletons had DW_AT_stmt_list]
8313 call FUNC */
8314
8315 if (dwarf_read_debug)
8316 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
8317
8318 /* Sort in a separate table to maintain the order of all_type_units
8319 for .gdb_index: TU indices directly index all_type_units. */
8320 std::vector<tu_abbrev_offset> sorted_by_abbrev;
8321 sorted_by_abbrev.reserve (dwarf2_per_objfile->all_type_units.size ());
8322
8323 for (signatured_type *sig_type : dwarf2_per_objfile->all_type_units)
8324 sorted_by_abbrev.emplace_back
8325 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
8326 sig_type->per_cu.section,
8327 sig_type->per_cu.sect_off));
8328
8329 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
8330 sort_tu_by_abbrev_offset);
8331
8332 abbrev_offset = (sect_offset) ~(unsigned) 0;
8333
8334 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
8335 {
8336 /* Switch to the next abbrev table if necessary. */
8337 if (abbrev_table == NULL
8338 || tu.abbrev_offset != abbrev_offset)
8339 {
8340 abbrev_offset = tu.abbrev_offset;
8341 abbrev_table =
8342 abbrev_table_read_table (dwarf2_per_objfile,
8343 &dwarf2_per_objfile->abbrev,
8344 abbrev_offset);
8345 ++tu_stats->nr_uniq_abbrev_tables;
8346 }
8347
8348 init_cutu_and_read_dies (&tu.sig_type->per_cu, abbrev_table.get (),
8349 0, 0, false, build_type_psymtabs_reader, NULL);
8350 }
8351 }
8352
8353 /* Print collected type unit statistics. */
8354
8355 static void
8356 print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
8357 {
8358 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8359
8360 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
8361 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
8362 dwarf2_per_objfile->all_type_units.size ());
8363 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
8364 tu_stats->nr_uniq_abbrev_tables);
8365 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
8366 tu_stats->nr_symtabs);
8367 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
8368 tu_stats->nr_symtab_sharers);
8369 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
8370 tu_stats->nr_stmt_less_type_units);
8371 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
8372 tu_stats->nr_all_type_units_reallocs);
8373 }
8374
8375 /* Traversal function for build_type_psymtabs. */
8376
8377 static int
8378 build_type_psymtab_dependencies (void **slot, void *info)
8379 {
8380 struct dwarf2_per_objfile *dwarf2_per_objfile
8381 = (struct dwarf2_per_objfile *) info;
8382 struct objfile *objfile = dwarf2_per_objfile->objfile;
8383 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
8384 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
8385 struct partial_symtab *pst = per_cu->v.psymtab;
8386 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
8387 int i;
8388
8389 gdb_assert (len > 0);
8390 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
8391
8392 pst->number_of_dependencies = len;
8393 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
8394 for (i = 0; i < len; ++i)
8395 {
8396 struct signatured_type *iter = tu_group->tus->at (i);
8397 gdb_assert (iter->per_cu.is_debug_types);
8398 pst->dependencies[i] = iter->per_cu.v.psymtab;
8399 iter->type_unit_group = tu_group;
8400 }
8401
8402 delete tu_group->tus;
8403 tu_group->tus = nullptr;
8404
8405 return 1;
8406 }
8407
8408 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8409 Build partial symbol tables for the .debug_types comp-units. */
8410
8411 static void
8412 build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
8413 {
8414 if (! create_all_type_units (dwarf2_per_objfile))
8415 return;
8416
8417 build_type_psymtabs_1 (dwarf2_per_objfile);
8418 }
8419
8420 /* Traversal function for process_skeletonless_type_unit.
8421 Read a TU in a DWO file and build partial symbols for it. */
8422
8423 static int
8424 process_skeletonless_type_unit (void **slot, void *info)
8425 {
8426 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
8427 struct dwarf2_per_objfile *dwarf2_per_objfile
8428 = (struct dwarf2_per_objfile *) info;
8429 struct signatured_type find_entry, *entry;
8430
8431 /* If this TU doesn't exist in the global table, add it and read it in. */
8432
8433 if (dwarf2_per_objfile->signatured_types == NULL)
8434 {
8435 dwarf2_per_objfile->signatured_types
8436 = allocate_signatured_type_table (dwarf2_per_objfile->objfile);
8437 }
8438
8439 find_entry.signature = dwo_unit->signature;
8440 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
8441 INSERT);
8442 /* If we've already seen this type there's nothing to do. What's happening
8443 is we're doing our own version of comdat-folding here. */
8444 if (*slot != NULL)
8445 return 1;
8446
8447 /* This does the job that create_all_type_units would have done for
8448 this TU. */
8449 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
8450 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
8451 *slot = entry;
8452
8453 /* This does the job that build_type_psymtabs_1 would have done. */
8454 init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0, false,
8455 build_type_psymtabs_reader, NULL);
8456
8457 return 1;
8458 }
8459
8460 /* Traversal function for process_skeletonless_type_units. */
8461
8462 static int
8463 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8464 {
8465 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8466
8467 if (dwo_file->tus != NULL)
8468 {
8469 htab_traverse_noresize (dwo_file->tus,
8470 process_skeletonless_type_unit, info);
8471 }
8472
8473 return 1;
8474 }
8475
8476 /* Scan all TUs of DWO files, verifying we've processed them.
8477 This is needed in case a TU was emitted without its skeleton.
8478 Note: This can't be done until we know what all the DWO files are. */
8479
8480 static void
8481 process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
8482 {
8483 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
8484 if (get_dwp_file (dwarf2_per_objfile) == NULL
8485 && dwarf2_per_objfile->dwo_files != NULL)
8486 {
8487 htab_traverse_noresize (dwarf2_per_objfile->dwo_files.get (),
8488 process_dwo_file_for_skeletonless_type_units,
8489 dwarf2_per_objfile);
8490 }
8491 }
8492
8493 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
8494
8495 static void
8496 set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
8497 {
8498 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
8499 {
8500 struct partial_symtab *pst = per_cu->v.psymtab;
8501
8502 if (pst == NULL)
8503 continue;
8504
8505 for (int j = 0; j < pst->number_of_dependencies; ++j)
8506 {
8507 /* Set the 'user' field only if it is not already set. */
8508 if (pst->dependencies[j]->user == NULL)
8509 pst->dependencies[j]->user = pst;
8510 }
8511 }
8512 }
8513
8514 /* Build the partial symbol table by doing a quick pass through the
8515 .debug_info and .debug_abbrev sections. */
8516
8517 static void
8518 dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
8519 {
8520 struct objfile *objfile = dwarf2_per_objfile->objfile;
8521
8522 if (dwarf_read_debug)
8523 {
8524 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
8525 objfile_name (objfile));
8526 }
8527
8528 dwarf2_per_objfile->reading_partial_symbols = 1;
8529
8530 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
8531
8532 /* Any cached compilation units will be linked by the per-objfile
8533 read_in_chain. Make sure to free them when we're done. */
8534 free_cached_comp_units freer (dwarf2_per_objfile);
8535
8536 build_type_psymtabs (dwarf2_per_objfile);
8537
8538 create_all_comp_units (dwarf2_per_objfile);
8539
8540 /* Create a temporary address map on a temporary obstack. We later
8541 copy this to the final obstack. */
8542 auto_obstack temp_obstack;
8543
8544 scoped_restore save_psymtabs_addrmap
8545 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
8546 addrmap_create_mutable (&temp_obstack));
8547
8548 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
8549 process_psymtab_comp_unit (per_cu, 0, language_minimal);
8550
8551 /* This has to wait until we read the CUs, we need the list of DWOs. */
8552 process_skeletonless_type_units (dwarf2_per_objfile);
8553
8554 /* Now that all TUs have been processed we can fill in the dependencies. */
8555 if (dwarf2_per_objfile->type_unit_groups != NULL)
8556 {
8557 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
8558 build_type_psymtab_dependencies, dwarf2_per_objfile);
8559 }
8560
8561 if (dwarf_read_debug)
8562 print_tu_stats (dwarf2_per_objfile);
8563
8564 set_partial_user (dwarf2_per_objfile);
8565
8566 objfile->partial_symtabs->psymtabs_addrmap
8567 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
8568 objfile->partial_symtabs->obstack ());
8569 /* At this point we want to keep the address map. */
8570 save_psymtabs_addrmap.release ();
8571
8572 if (dwarf_read_debug)
8573 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
8574 objfile_name (objfile));
8575 }
8576
8577 /* die_reader_func for load_partial_comp_unit. */
8578
8579 static void
8580 load_partial_comp_unit_reader (const struct die_reader_specs *reader,
8581 const gdb_byte *info_ptr,
8582 struct die_info *comp_unit_die,
8583 int has_children,
8584 void *data)
8585 {
8586 struct dwarf2_cu *cu = reader->cu;
8587
8588 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
8589
8590 /* Check if comp unit has_children.
8591 If so, read the rest of the partial symbols from this comp unit.
8592 If not, there's no more debug_info for this comp unit. */
8593 if (has_children)
8594 load_partial_dies (reader, info_ptr, 0);
8595 }
8596
8597 /* Load the partial DIEs for a secondary CU into memory.
8598 This is also used when rereading a primary CU with load_all_dies. */
8599
8600 static void
8601 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
8602 {
8603 init_cutu_and_read_dies (this_cu, NULL, 1, 1, false,
8604 load_partial_comp_unit_reader, NULL);
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->section_offsets[SECT_OFF_TEXT (objfile)];
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->section_offsets[SECT_OFF_TEXT (objfile)];
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 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
9342 if (attr.form == DW_FORM_ref_addr)
9343 complaint (_("ignoring absolute DW_AT_sibling"));
9344 else
9345 {
9346 sect_offset off = dwarf2_get_ref_die_offset (&attr);
9347 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
9348
9349 if (sibling_ptr < info_ptr)
9350 complaint (_("DW_AT_sibling points backwards"));
9351 else if (sibling_ptr > reader->buffer_end)
9352 dwarf2_section_buffer_overflow_complaint (reader->die_section);
9353 else
9354 return sibling_ptr;
9355 }
9356 }
9357
9358 /* If it isn't DW_AT_sibling, skip this attribute. */
9359 form = abbrev->attrs[i].form;
9360 skip_attribute:
9361 switch (form)
9362 {
9363 case DW_FORM_ref_addr:
9364 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
9365 and later it is offset sized. */
9366 if (cu->header.version == 2)
9367 info_ptr += cu->header.addr_size;
9368 else
9369 info_ptr += cu->header.offset_size;
9370 break;
9371 case DW_FORM_GNU_ref_alt:
9372 info_ptr += cu->header.offset_size;
9373 break;
9374 case DW_FORM_addr:
9375 info_ptr += cu->header.addr_size;
9376 break;
9377 case DW_FORM_data1:
9378 case DW_FORM_ref1:
9379 case DW_FORM_flag:
9380 case DW_FORM_strx1:
9381 info_ptr += 1;
9382 break;
9383 case DW_FORM_flag_present:
9384 case DW_FORM_implicit_const:
9385 break;
9386 case DW_FORM_data2:
9387 case DW_FORM_ref2:
9388 case DW_FORM_strx2:
9389 info_ptr += 2;
9390 break;
9391 case DW_FORM_strx3:
9392 info_ptr += 3;
9393 break;
9394 case DW_FORM_data4:
9395 case DW_FORM_ref4:
9396 case DW_FORM_strx4:
9397 info_ptr += 4;
9398 break;
9399 case DW_FORM_data8:
9400 case DW_FORM_ref8:
9401 case DW_FORM_ref_sig8:
9402 info_ptr += 8;
9403 break;
9404 case DW_FORM_data16:
9405 info_ptr += 16;
9406 break;
9407 case DW_FORM_string:
9408 read_direct_string (abfd, info_ptr, &bytes_read);
9409 info_ptr += bytes_read;
9410 break;
9411 case DW_FORM_sec_offset:
9412 case DW_FORM_strp:
9413 case DW_FORM_GNU_strp_alt:
9414 info_ptr += cu->header.offset_size;
9415 break;
9416 case DW_FORM_exprloc:
9417 case DW_FORM_block:
9418 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9419 info_ptr += bytes_read;
9420 break;
9421 case DW_FORM_block1:
9422 info_ptr += 1 + read_1_byte (abfd, info_ptr);
9423 break;
9424 case DW_FORM_block2:
9425 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
9426 break;
9427 case DW_FORM_block4:
9428 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
9429 break;
9430 case DW_FORM_addrx:
9431 case DW_FORM_strx:
9432 case DW_FORM_sdata:
9433 case DW_FORM_udata:
9434 case DW_FORM_ref_udata:
9435 case DW_FORM_GNU_addr_index:
9436 case DW_FORM_GNU_str_index:
9437 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
9438 break;
9439 case DW_FORM_indirect:
9440 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9441 info_ptr += bytes_read;
9442 /* We need to continue parsing from here, so just go back to
9443 the top. */
9444 goto skip_attribute;
9445
9446 default:
9447 error (_("Dwarf Error: Cannot handle %s "
9448 "in DWARF reader [in module %s]"),
9449 dwarf_form_name (form),
9450 bfd_get_filename (abfd));
9451 }
9452 }
9453
9454 if (abbrev->has_children)
9455 return skip_children (reader, info_ptr);
9456 else
9457 return info_ptr;
9458 }
9459
9460 /* Locate ORIG_PDI's sibling.
9461 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
9462
9463 static const gdb_byte *
9464 locate_pdi_sibling (const struct die_reader_specs *reader,
9465 struct partial_die_info *orig_pdi,
9466 const gdb_byte *info_ptr)
9467 {
9468 /* Do we know the sibling already? */
9469
9470 if (orig_pdi->sibling)
9471 return orig_pdi->sibling;
9472
9473 /* Are there any children to deal with? */
9474
9475 if (!orig_pdi->has_children)
9476 return info_ptr;
9477
9478 /* Skip the children the long way. */
9479
9480 return skip_children (reader, info_ptr);
9481 }
9482
9483 /* Expand this partial symbol table into a full symbol table. SELF is
9484 not NULL. */
9485
9486 static void
9487 dwarf2_read_symtab (struct partial_symtab *self,
9488 struct objfile *objfile)
9489 {
9490 struct dwarf2_per_objfile *dwarf2_per_objfile
9491 = get_dwarf2_per_objfile (objfile);
9492
9493 if (self->readin)
9494 {
9495 warning (_("bug: psymtab for %s is already read in."),
9496 self->filename);
9497 }
9498 else
9499 {
9500 if (info_verbose)
9501 {
9502 printf_filtered (_("Reading in symbols for %s..."),
9503 self->filename);
9504 gdb_flush (gdb_stdout);
9505 }
9506
9507 /* If this psymtab is constructed from a debug-only objfile, the
9508 has_section_at_zero flag will not necessarily be correct. We
9509 can get the correct value for this flag by looking at the data
9510 associated with the (presumably stripped) associated objfile. */
9511 if (objfile->separate_debug_objfile_backlink)
9512 {
9513 struct dwarf2_per_objfile *dpo_backlink
9514 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
9515
9516 dwarf2_per_objfile->has_section_at_zero
9517 = dpo_backlink->has_section_at_zero;
9518 }
9519
9520 dwarf2_per_objfile->reading_partial_symbols = 0;
9521
9522 psymtab_to_symtab_1 (self);
9523
9524 /* Finish up the debug error message. */
9525 if (info_verbose)
9526 printf_filtered (_("done.\n"));
9527 }
9528
9529 process_cu_includes (dwarf2_per_objfile);
9530 }
9531 \f
9532 /* Reading in full CUs. */
9533
9534 /* Add PER_CU to the queue. */
9535
9536 static void
9537 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
9538 enum language pretend_language)
9539 {
9540 struct dwarf2_queue_item *item;
9541
9542 per_cu->queued = 1;
9543 item = XNEW (struct dwarf2_queue_item);
9544 item->per_cu = per_cu;
9545 item->pretend_language = pretend_language;
9546 item->next = NULL;
9547
9548 if (dwarf2_queue == NULL)
9549 dwarf2_queue = item;
9550 else
9551 dwarf2_queue_tail->next = item;
9552
9553 dwarf2_queue_tail = item;
9554 }
9555
9556 /* If PER_CU is not yet queued, add it to the queue.
9557 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9558 dependency.
9559 The result is non-zero if PER_CU was queued, otherwise the result is zero
9560 meaning either PER_CU is already queued or it is already loaded.
9561
9562 N.B. There is an invariant here that if a CU is queued then it is loaded.
9563 The caller is required to load PER_CU if we return non-zero. */
9564
9565 static int
9566 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
9567 struct dwarf2_per_cu_data *per_cu,
9568 enum language pretend_language)
9569 {
9570 /* We may arrive here during partial symbol reading, if we need full
9571 DIEs to process an unusual case (e.g. template arguments). Do
9572 not queue PER_CU, just tell our caller to load its DIEs. */
9573 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
9574 {
9575 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
9576 return 1;
9577 return 0;
9578 }
9579
9580 /* Mark the dependence relation so that we don't flush PER_CU
9581 too early. */
9582 if (dependent_cu != NULL)
9583 dwarf2_add_dependence (dependent_cu, per_cu);
9584
9585 /* If it's already on the queue, we have nothing to do. */
9586 if (per_cu->queued)
9587 return 0;
9588
9589 /* If the compilation unit is already loaded, just mark it as
9590 used. */
9591 if (per_cu->cu != NULL)
9592 {
9593 per_cu->cu->last_used = 0;
9594 return 0;
9595 }
9596
9597 /* Add it to the queue. */
9598 queue_comp_unit (per_cu, pretend_language);
9599
9600 return 1;
9601 }
9602
9603 /* Process the queue. */
9604
9605 static void
9606 process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
9607 {
9608 struct dwarf2_queue_item *item, *next_item;
9609
9610 if (dwarf_read_debug)
9611 {
9612 fprintf_unfiltered (gdb_stdlog,
9613 "Expanding one or more symtabs of objfile %s ...\n",
9614 objfile_name (dwarf2_per_objfile->objfile));
9615 }
9616
9617 /* The queue starts out with one item, but following a DIE reference
9618 may load a new CU, adding it to the end of the queue. */
9619 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
9620 {
9621 if ((dwarf2_per_objfile->using_index
9622 ? !item->per_cu->v.quick->compunit_symtab
9623 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
9624 /* Skip dummy CUs. */
9625 && item->per_cu->cu != NULL)
9626 {
9627 struct dwarf2_per_cu_data *per_cu = item->per_cu;
9628 unsigned int debug_print_threshold;
9629 char buf[100];
9630
9631 if (per_cu->is_debug_types)
9632 {
9633 struct signatured_type *sig_type =
9634 (struct signatured_type *) per_cu;
9635
9636 sprintf (buf, "TU %s at offset %s",
9637 hex_string (sig_type->signature),
9638 sect_offset_str (per_cu->sect_off));
9639 /* There can be 100s of TUs.
9640 Only print them in verbose mode. */
9641 debug_print_threshold = 2;
9642 }
9643 else
9644 {
9645 sprintf (buf, "CU at offset %s",
9646 sect_offset_str (per_cu->sect_off));
9647 debug_print_threshold = 1;
9648 }
9649
9650 if (dwarf_read_debug >= debug_print_threshold)
9651 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
9652
9653 if (per_cu->is_debug_types)
9654 process_full_type_unit (per_cu, item->pretend_language);
9655 else
9656 process_full_comp_unit (per_cu, item->pretend_language);
9657
9658 if (dwarf_read_debug >= debug_print_threshold)
9659 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
9660 }
9661
9662 item->per_cu->queued = 0;
9663 next_item = item->next;
9664 xfree (item);
9665 }
9666
9667 dwarf2_queue_tail = NULL;
9668
9669 if (dwarf_read_debug)
9670 {
9671 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
9672 objfile_name (dwarf2_per_objfile->objfile));
9673 }
9674 }
9675
9676 /* Read in full symbols for PST, and anything it depends on. */
9677
9678 static void
9679 psymtab_to_symtab_1 (struct partial_symtab *pst)
9680 {
9681 struct dwarf2_per_cu_data *per_cu;
9682 int i;
9683
9684 if (pst->readin)
9685 return;
9686
9687 for (i = 0; i < pst->number_of_dependencies; i++)
9688 if (!pst->dependencies[i]->readin
9689 && pst->dependencies[i]->user == NULL)
9690 {
9691 /* Inform about additional files that need to be read in. */
9692 if (info_verbose)
9693 {
9694 /* FIXME: i18n: Need to make this a single string. */
9695 fputs_filtered (" ", gdb_stdout);
9696 wrap_here ("");
9697 fputs_filtered ("and ", gdb_stdout);
9698 wrap_here ("");
9699 printf_filtered ("%s...", pst->dependencies[i]->filename);
9700 wrap_here (""); /* Flush output. */
9701 gdb_flush (gdb_stdout);
9702 }
9703 psymtab_to_symtab_1 (pst->dependencies[i]);
9704 }
9705
9706 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
9707
9708 if (per_cu == NULL)
9709 {
9710 /* It's an include file, no symbols to read for it.
9711 Everything is in the parent symtab. */
9712 pst->readin = 1;
9713 return;
9714 }
9715
9716 dw2_do_instantiate_symtab (per_cu, false);
9717 }
9718
9719 /* Trivial hash function for die_info: the hash value of a DIE
9720 is its offset in .debug_info for this objfile. */
9721
9722 static hashval_t
9723 die_hash (const void *item)
9724 {
9725 const struct die_info *die = (const struct die_info *) item;
9726
9727 return to_underlying (die->sect_off);
9728 }
9729
9730 /* Trivial comparison function for die_info structures: two DIEs
9731 are equal if they have the same offset. */
9732
9733 static int
9734 die_eq (const void *item_lhs, const void *item_rhs)
9735 {
9736 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9737 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
9738
9739 return die_lhs->sect_off == die_rhs->sect_off;
9740 }
9741
9742 /* die_reader_func for load_full_comp_unit.
9743 This is identical to read_signatured_type_reader,
9744 but is kept separate for now. */
9745
9746 static void
9747 load_full_comp_unit_reader (const struct die_reader_specs *reader,
9748 const gdb_byte *info_ptr,
9749 struct die_info *comp_unit_die,
9750 int has_children,
9751 void *data)
9752 {
9753 struct dwarf2_cu *cu = reader->cu;
9754 enum language *language_ptr = (enum language *) data;
9755
9756 gdb_assert (cu->die_hash == NULL);
9757 cu->die_hash =
9758 htab_create_alloc_ex (cu->header.length / 12,
9759 die_hash,
9760 die_eq,
9761 NULL,
9762 &cu->comp_unit_obstack,
9763 hashtab_obstack_allocate,
9764 dummy_obstack_deallocate);
9765
9766 if (has_children)
9767 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
9768 &info_ptr, comp_unit_die);
9769 cu->dies = comp_unit_die;
9770 /* comp_unit_die is not stored in die_hash, no need. */
9771
9772 /* We try not to read any attributes in this function, because not
9773 all CUs needed for references have been loaded yet, and symbol
9774 table processing isn't initialized. But we have to set the CU language,
9775 or we won't be able to build types correctly.
9776 Similarly, if we do not read the producer, we can not apply
9777 producer-specific interpretation. */
9778 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
9779 }
9780
9781 /* Load the DIEs associated with PER_CU into memory. */
9782
9783 static void
9784 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
9785 bool skip_partial,
9786 enum language pretend_language)
9787 {
9788 gdb_assert (! this_cu->is_debug_types);
9789
9790 init_cutu_and_read_dies (this_cu, NULL, 1, 1, skip_partial,
9791 load_full_comp_unit_reader, &pretend_language);
9792 }
9793
9794 /* Add a DIE to the delayed physname list. */
9795
9796 static void
9797 add_to_method_list (struct type *type, int fnfield_index, int index,
9798 const char *name, struct die_info *die,
9799 struct dwarf2_cu *cu)
9800 {
9801 struct delayed_method_info mi;
9802 mi.type = type;
9803 mi.fnfield_index = fnfield_index;
9804 mi.index = index;
9805 mi.name = name;
9806 mi.die = die;
9807 cu->method_list.push_back (mi);
9808 }
9809
9810 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9811 "const" / "volatile". If so, decrements LEN by the length of the
9812 modifier and return true. Otherwise return false. */
9813
9814 template<size_t N>
9815 static bool
9816 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9817 {
9818 size_t mod_len = sizeof (mod) - 1;
9819 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9820 {
9821 len -= mod_len;
9822 return true;
9823 }
9824 return false;
9825 }
9826
9827 /* Compute the physnames of any methods on the CU's method list.
9828
9829 The computation of method physnames is delayed in order to avoid the
9830 (bad) condition that one of the method's formal parameters is of an as yet
9831 incomplete type. */
9832
9833 static void
9834 compute_delayed_physnames (struct dwarf2_cu *cu)
9835 {
9836 /* Only C++ delays computing physnames. */
9837 if (cu->method_list.empty ())
9838 return;
9839 gdb_assert (cu->language == language_cplus);
9840
9841 for (const delayed_method_info &mi : cu->method_list)
9842 {
9843 const char *physname;
9844 struct fn_fieldlist *fn_flp
9845 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9846 physname = dwarf2_physname (mi.name, mi.die, cu);
9847 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
9848 = physname ? physname : "";
9849
9850 /* Since there's no tag to indicate whether a method is a
9851 const/volatile overload, extract that information out of the
9852 demangled name. */
9853 if (physname != NULL)
9854 {
9855 size_t len = strlen (physname);
9856
9857 while (1)
9858 {
9859 if (physname[len] == ')') /* shortcut */
9860 break;
9861 else if (check_modifier (physname, len, " const"))
9862 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
9863 else if (check_modifier (physname, len, " volatile"))
9864 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
9865 else
9866 break;
9867 }
9868 }
9869 }
9870
9871 /* The list is no longer needed. */
9872 cu->method_list.clear ();
9873 }
9874
9875 /* Go objects should be embedded in a DW_TAG_module DIE,
9876 and it's not clear if/how imported objects will appear.
9877 To keep Go support simple until that's worked out,
9878 go back through what we've read and create something usable.
9879 We could do this while processing each DIE, and feels kinda cleaner,
9880 but that way is more invasive.
9881 This is to, for example, allow the user to type "p var" or "b main"
9882 without having to specify the package name, and allow lookups
9883 of module.object to work in contexts that use the expression
9884 parser. */
9885
9886 static void
9887 fixup_go_packaging (struct dwarf2_cu *cu)
9888 {
9889 gdb::unique_xmalloc_ptr<char> package_name;
9890 struct pending *list;
9891 int i;
9892
9893 for (list = *cu->get_builder ()->get_global_symbols ();
9894 list != NULL;
9895 list = list->next)
9896 {
9897 for (i = 0; i < list->nsyms; ++i)
9898 {
9899 struct symbol *sym = list->symbol[i];
9900
9901 if (sym->language () == language_go
9902 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9903 {
9904 gdb::unique_xmalloc_ptr<char> this_package_name
9905 (go_symbol_package_name (sym));
9906
9907 if (this_package_name == NULL)
9908 continue;
9909 if (package_name == NULL)
9910 package_name = std::move (this_package_name);
9911 else
9912 {
9913 struct objfile *objfile
9914 = cu->per_cu->dwarf2_per_objfile->objfile;
9915 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
9916 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
9917 (symbol_symtab (sym) != NULL
9918 ? symtab_to_filename_for_display
9919 (symbol_symtab (sym))
9920 : objfile_name (objfile)),
9921 this_package_name.get (), package_name.get ());
9922 }
9923 }
9924 }
9925 }
9926
9927 if (package_name != NULL)
9928 {
9929 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9930 const char *saved_package_name
9931 = obstack_strdup (&objfile->per_bfd->storage_obstack, package_name.get ());
9932 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9933 saved_package_name);
9934 struct symbol *sym;
9935
9936 sym = allocate_symbol (objfile);
9937 sym->set_language (language_go, &objfile->objfile_obstack);
9938 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
9939 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9940 e.g., "main" finds the "main" module and not C's main(). */
9941 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9942 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9943 SYMBOL_TYPE (sym) = type;
9944
9945 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
9946 }
9947 }
9948
9949 /* Allocate a fully-qualified name consisting of the two parts on the
9950 obstack. */
9951
9952 static const char *
9953 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9954 {
9955 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9956 }
9957
9958 /* A helper that allocates a struct discriminant_info to attach to a
9959 union type. */
9960
9961 static struct discriminant_info *
9962 alloc_discriminant_info (struct type *type, int discriminant_index,
9963 int default_index)
9964 {
9965 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9966 gdb_assert (discriminant_index == -1
9967 || (discriminant_index >= 0
9968 && discriminant_index < TYPE_NFIELDS (type)));
9969 gdb_assert (default_index == -1
9970 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
9971
9972 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
9973
9974 struct discriminant_info *disc
9975 = ((struct discriminant_info *)
9976 TYPE_ZALLOC (type,
9977 offsetof (struct discriminant_info, discriminants)
9978 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
9979 disc->default_index = default_index;
9980 disc->discriminant_index = discriminant_index;
9981
9982 struct dynamic_prop prop;
9983 prop.kind = PROP_UNDEFINED;
9984 prop.data.baton = disc;
9985
9986 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
9987
9988 return disc;
9989 }
9990
9991 /* Some versions of rustc emitted enums in an unusual way.
9992
9993 Ordinary enums were emitted as unions. The first element of each
9994 structure in the union was named "RUST$ENUM$DISR". This element
9995 held the discriminant.
9996
9997 These versions of Rust also implemented the "non-zero"
9998 optimization. When the enum had two values, and one is empty and
9999 the other holds a pointer that cannot be zero, the pointer is used
10000 as the discriminant, with a zero value meaning the empty variant.
10001 Here, the union's first member is of the form
10002 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
10003 where the fieldnos are the indices of the fields that should be
10004 traversed in order to find the field (which may be several fields deep)
10005 and the variantname is the name of the variant of the case when the
10006 field is zero.
10007
10008 This function recognizes whether TYPE is of one of these forms,
10009 and, if so, smashes it to be a variant type. */
10010
10011 static void
10012 quirk_rust_enum (struct type *type, struct objfile *objfile)
10013 {
10014 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
10015
10016 /* We don't need to deal with empty enums. */
10017 if (TYPE_NFIELDS (type) == 0)
10018 return;
10019
10020 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
10021 if (TYPE_NFIELDS (type) == 1
10022 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
10023 {
10024 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
10025
10026 /* Decode the field name to find the offset of the
10027 discriminant. */
10028 ULONGEST bit_offset = 0;
10029 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
10030 while (name[0] >= '0' && name[0] <= '9')
10031 {
10032 char *tail;
10033 unsigned long index = strtoul (name, &tail, 10);
10034 name = tail;
10035 if (*name != '$'
10036 || index >= TYPE_NFIELDS (field_type)
10037 || (TYPE_FIELD_LOC_KIND (field_type, index)
10038 != FIELD_LOC_KIND_BITPOS))
10039 {
10040 complaint (_("Could not parse Rust enum encoding string \"%s\""
10041 "[in module %s]"),
10042 TYPE_FIELD_NAME (type, 0),
10043 objfile_name (objfile));
10044 return;
10045 }
10046 ++name;
10047
10048 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
10049 field_type = TYPE_FIELD_TYPE (field_type, index);
10050 }
10051
10052 /* Make a union to hold the variants. */
10053 struct type *union_type = alloc_type (objfile);
10054 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10055 TYPE_NFIELDS (union_type) = 3;
10056 TYPE_FIELDS (union_type)
10057 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
10058 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
10059 set_type_align (union_type, TYPE_RAW_ALIGN (type));
10060
10061 /* Put the discriminant must at index 0. */
10062 TYPE_FIELD_TYPE (union_type, 0) = field_type;
10063 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10064 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10065 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
10066
10067 /* The order of fields doesn't really matter, so put the real
10068 field at index 1 and the data-less field at index 2. */
10069 struct discriminant_info *disc
10070 = alloc_discriminant_info (union_type, 0, 1);
10071 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
10072 TYPE_FIELD_NAME (union_type, 1)
10073 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
10074 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
10075 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
10076 TYPE_FIELD_NAME (union_type, 1));
10077
10078 const char *dataless_name
10079 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
10080 name);
10081 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
10082 dataless_name);
10083 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
10084 /* NAME points into the original discriminant name, which
10085 already has the correct lifetime. */
10086 TYPE_FIELD_NAME (union_type, 2) = name;
10087 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
10088 disc->discriminants[2] = 0;
10089
10090 /* Smash this type to be a structure type. We have to do this
10091 because the type has already been recorded. */
10092 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10093 TYPE_NFIELDS (type) = 1;
10094 TYPE_FIELDS (type)
10095 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
10096
10097 /* Install the variant part. */
10098 TYPE_FIELD_TYPE (type, 0) = union_type;
10099 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10100 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10101 }
10102 /* A union with a single anonymous field is probably an old-style
10103 univariant enum. */
10104 else if (TYPE_NFIELDS (type) == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
10105 {
10106 /* Smash this type to be a structure type. We have to do this
10107 because the type has already been recorded. */
10108 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10109
10110 /* Make a union to hold the variants. */
10111 struct type *union_type = alloc_type (objfile);
10112 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10113 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
10114 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
10115 set_type_align (union_type, TYPE_RAW_ALIGN (type));
10116 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
10117
10118 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
10119 const char *variant_name
10120 = rust_last_path_segment (TYPE_NAME (field_type));
10121 TYPE_FIELD_NAME (union_type, 0) = variant_name;
10122 TYPE_NAME (field_type)
10123 = rust_fully_qualify (&objfile->objfile_obstack,
10124 TYPE_NAME (type), variant_name);
10125
10126 /* Install the union in the outer struct type. */
10127 TYPE_NFIELDS (type) = 1;
10128 TYPE_FIELDS (type)
10129 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
10130 TYPE_FIELD_TYPE (type, 0) = union_type;
10131 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10132 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10133
10134 alloc_discriminant_info (union_type, -1, 0);
10135 }
10136 else
10137 {
10138 struct type *disr_type = nullptr;
10139 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
10140 {
10141 disr_type = TYPE_FIELD_TYPE (type, i);
10142
10143 if (TYPE_CODE (disr_type) != TYPE_CODE_STRUCT)
10144 {
10145 /* All fields of a true enum will be structs. */
10146 return;
10147 }
10148 else if (TYPE_NFIELDS (disr_type) == 0)
10149 {
10150 /* Could be data-less variant, so keep going. */
10151 disr_type = nullptr;
10152 }
10153 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
10154 "RUST$ENUM$DISR") != 0)
10155 {
10156 /* Not a Rust enum. */
10157 return;
10158 }
10159 else
10160 {
10161 /* Found one. */
10162 break;
10163 }
10164 }
10165
10166 /* If we got here without a discriminant, then it's probably
10167 just a union. */
10168 if (disr_type == nullptr)
10169 return;
10170
10171 /* Smash this type to be a structure type. We have to do this
10172 because the type has already been recorded. */
10173 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10174
10175 /* Make a union to hold the variants. */
10176 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
10177 struct type *union_type = alloc_type (objfile);
10178 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10179 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
10180 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
10181 set_type_align (union_type, TYPE_RAW_ALIGN (type));
10182 TYPE_FIELDS (union_type)
10183 = (struct field *) TYPE_ZALLOC (union_type,
10184 (TYPE_NFIELDS (union_type)
10185 * sizeof (struct field)));
10186
10187 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
10188 TYPE_NFIELDS (type) * sizeof (struct field));
10189
10190 /* Install the discriminant at index 0 in the union. */
10191 TYPE_FIELD (union_type, 0) = *disr_field;
10192 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10193 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10194
10195 /* Install the union in the outer struct type. */
10196 TYPE_FIELD_TYPE (type, 0) = union_type;
10197 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10198 TYPE_NFIELDS (type) = 1;
10199
10200 /* Set the size and offset of the union type. */
10201 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10202
10203 /* We need a way to find the correct discriminant given a
10204 variant name. For convenience we build a map here. */
10205 struct type *enum_type = FIELD_TYPE (*disr_field);
10206 std::unordered_map<std::string, ULONGEST> discriminant_map;
10207 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
10208 {
10209 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
10210 {
10211 const char *name
10212 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
10213 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
10214 }
10215 }
10216
10217 int n_fields = TYPE_NFIELDS (union_type);
10218 struct discriminant_info *disc
10219 = alloc_discriminant_info (union_type, 0, -1);
10220 /* Skip the discriminant here. */
10221 for (int i = 1; i < n_fields; ++i)
10222 {
10223 /* Find the final word in the name of this variant's type.
10224 That name can be used to look up the correct
10225 discriminant. */
10226 const char *variant_name
10227 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
10228 i)));
10229
10230 auto iter = discriminant_map.find (variant_name);
10231 if (iter != discriminant_map.end ())
10232 disc->discriminants[i] = iter->second;
10233
10234 /* Remove the discriminant field, if it exists. */
10235 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
10236 if (TYPE_NFIELDS (sub_type) > 0)
10237 {
10238 --TYPE_NFIELDS (sub_type);
10239 ++TYPE_FIELDS (sub_type);
10240 }
10241 TYPE_FIELD_NAME (union_type, i) = variant_name;
10242 TYPE_NAME (sub_type)
10243 = rust_fully_qualify (&objfile->objfile_obstack,
10244 TYPE_NAME (type), variant_name);
10245 }
10246 }
10247 }
10248
10249 /* Rewrite some Rust unions to be structures with variants parts. */
10250
10251 static void
10252 rust_union_quirks (struct dwarf2_cu *cu)
10253 {
10254 gdb_assert (cu->language == language_rust);
10255 for (type *type_ : cu->rust_unions)
10256 quirk_rust_enum (type_, cu->per_cu->dwarf2_per_objfile->objfile);
10257 /* We don't need this any more. */
10258 cu->rust_unions.clear ();
10259 }
10260
10261 /* Return the symtab for PER_CU. This works properly regardless of
10262 whether we're using the index or psymtabs. */
10263
10264 static struct compunit_symtab *
10265 get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
10266 {
10267 return (per_cu->dwarf2_per_objfile->using_index
10268 ? per_cu->v.quick->compunit_symtab
10269 : per_cu->v.psymtab->compunit_symtab);
10270 }
10271
10272 /* A helper function for computing the list of all symbol tables
10273 included by PER_CU. */
10274
10275 static void
10276 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
10277 htab_t all_children, htab_t all_type_symtabs,
10278 struct dwarf2_per_cu_data *per_cu,
10279 struct compunit_symtab *immediate_parent)
10280 {
10281 void **slot;
10282 struct compunit_symtab *cust;
10283
10284 slot = htab_find_slot (all_children, per_cu, INSERT);
10285 if (*slot != NULL)
10286 {
10287 /* This inclusion and its children have been processed. */
10288 return;
10289 }
10290
10291 *slot = per_cu;
10292 /* Only add a CU if it has a symbol table. */
10293 cust = get_compunit_symtab (per_cu);
10294 if (cust != NULL)
10295 {
10296 /* If this is a type unit only add its symbol table if we haven't
10297 seen it yet (type unit per_cu's can share symtabs). */
10298 if (per_cu->is_debug_types)
10299 {
10300 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
10301 if (*slot == NULL)
10302 {
10303 *slot = cust;
10304 result->push_back (cust);
10305 if (cust->user == NULL)
10306 cust->user = immediate_parent;
10307 }
10308 }
10309 else
10310 {
10311 result->push_back (cust);
10312 if (cust->user == NULL)
10313 cust->user = immediate_parent;
10314 }
10315 }
10316
10317 if (!per_cu->imported_symtabs_empty ())
10318 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
10319 {
10320 recursively_compute_inclusions (result, all_children,
10321 all_type_symtabs, ptr, cust);
10322 }
10323 }
10324
10325 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
10326 PER_CU. */
10327
10328 static void
10329 compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
10330 {
10331 gdb_assert (! per_cu->is_debug_types);
10332
10333 if (!per_cu->imported_symtabs_empty ())
10334 {
10335 int len;
10336 std::vector<compunit_symtab *> result_symtabs;
10337 htab_t all_children, all_type_symtabs;
10338 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
10339
10340 /* If we don't have a symtab, we can just skip this case. */
10341 if (cust == NULL)
10342 return;
10343
10344 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10345 NULL, xcalloc, xfree);
10346 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10347 NULL, xcalloc, xfree);
10348
10349 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
10350 {
10351 recursively_compute_inclusions (&result_symtabs, all_children,
10352 all_type_symtabs, ptr, cust);
10353 }
10354
10355 /* Now we have a transitive closure of all the included symtabs. */
10356 len = result_symtabs.size ();
10357 cust->includes
10358 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
10359 struct compunit_symtab *, len + 1);
10360 memcpy (cust->includes, result_symtabs.data (),
10361 len * sizeof (compunit_symtab *));
10362 cust->includes[len] = NULL;
10363
10364 htab_delete (all_children);
10365 htab_delete (all_type_symtabs);
10366 }
10367 }
10368
10369 /* Compute the 'includes' field for the symtabs of all the CUs we just
10370 read. */
10371
10372 static void
10373 process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
10374 {
10375 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->just_read_cus)
10376 {
10377 if (! iter->is_debug_types)
10378 compute_compunit_symtab_includes (iter);
10379 }
10380
10381 dwarf2_per_objfile->just_read_cus.clear ();
10382 }
10383
10384 /* Generate full symbol information for PER_CU, whose DIEs have
10385 already been loaded into memory. */
10386
10387 static void
10388 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
10389 enum language pretend_language)
10390 {
10391 struct dwarf2_cu *cu = per_cu->cu;
10392 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10393 struct objfile *objfile = dwarf2_per_objfile->objfile;
10394 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10395 CORE_ADDR lowpc, highpc;
10396 struct compunit_symtab *cust;
10397 CORE_ADDR baseaddr;
10398 struct block *static_block;
10399 CORE_ADDR addr;
10400
10401 baseaddr = objfile->section_offsets[SECT_OFF_TEXT (objfile)];
10402
10403 /* Clear the list here in case something was left over. */
10404 cu->method_list.clear ();
10405
10406 cu->language = pretend_language;
10407 cu->language_defn = language_def (cu->language);
10408
10409 /* Do line number decoding in read_file_scope () */
10410 process_die (cu->dies, cu);
10411
10412 /* For now fudge the Go package. */
10413 if (cu->language == language_go)
10414 fixup_go_packaging (cu);
10415
10416 /* Now that we have processed all the DIEs in the CU, all the types
10417 should be complete, and it should now be safe to compute all of the
10418 physnames. */
10419 compute_delayed_physnames (cu);
10420
10421 if (cu->language == language_rust)
10422 rust_union_quirks (cu);
10423
10424 /* Some compilers don't define a DW_AT_high_pc attribute for the
10425 compilation unit. If the DW_AT_high_pc is missing, synthesize
10426 it, by scanning the DIE's below the compilation unit. */
10427 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
10428
10429 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
10430 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
10431
10432 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10433 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10434 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10435 addrmap to help ensure it has an accurate map of pc values belonging to
10436 this comp unit. */
10437 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10438
10439 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
10440 SECT_OFF_TEXT (objfile),
10441 0);
10442
10443 if (cust != NULL)
10444 {
10445 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
10446
10447 /* Set symtab language to language from DW_AT_language. If the
10448 compilation is from a C file generated by language preprocessors, do
10449 not set the language if it was already deduced by start_subfile. */
10450 if (!(cu->language == language_c
10451 && COMPUNIT_FILETABS (cust)->language != language_unknown))
10452 COMPUNIT_FILETABS (cust)->language = cu->language;
10453
10454 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10455 produce DW_AT_location with location lists but it can be possibly
10456 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10457 there were bugs in prologue debug info, fixed later in GCC-4.5
10458 by "unwind info for epilogues" patch (which is not directly related).
10459
10460 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10461 needed, it would be wrong due to missing DW_AT_producer there.
10462
10463 Still one can confuse GDB by using non-standard GCC compilation
10464 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
10465 */
10466 if (cu->has_loclist && gcc_4_minor >= 5)
10467 cust->locations_valid = 1;
10468
10469 if (gcc_4_minor >= 5)
10470 cust->epilogue_unwind_valid = 1;
10471
10472 cust->call_site_htab = cu->call_site_htab;
10473 }
10474
10475 if (dwarf2_per_objfile->using_index)
10476 per_cu->v.quick->compunit_symtab = cust;
10477 else
10478 {
10479 struct partial_symtab *pst = per_cu->v.psymtab;
10480 pst->compunit_symtab = cust;
10481 pst->readin = 1;
10482 }
10483
10484 /* Push it for inclusion processing later. */
10485 dwarf2_per_objfile->just_read_cus.push_back (per_cu);
10486
10487 /* Not needed any more. */
10488 cu->reset_builder ();
10489 }
10490
10491 /* Generate full symbol information for type unit PER_CU, whose DIEs have
10492 already been loaded into memory. */
10493
10494 static void
10495 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
10496 enum language pretend_language)
10497 {
10498 struct dwarf2_cu *cu = per_cu->cu;
10499 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10500 struct objfile *objfile = dwarf2_per_objfile->objfile;
10501 struct compunit_symtab *cust;
10502 struct signatured_type *sig_type;
10503
10504 gdb_assert (per_cu->is_debug_types);
10505 sig_type = (struct signatured_type *) per_cu;
10506
10507 /* Clear the list here in case something was left over. */
10508 cu->method_list.clear ();
10509
10510 cu->language = pretend_language;
10511 cu->language_defn = language_def (cu->language);
10512
10513 /* The symbol tables are set up in read_type_unit_scope. */
10514 process_die (cu->dies, cu);
10515
10516 /* For now fudge the Go package. */
10517 if (cu->language == language_go)
10518 fixup_go_packaging (cu);
10519
10520 /* Now that we have processed all the DIEs in the CU, all the types
10521 should be complete, and it should now be safe to compute all of the
10522 physnames. */
10523 compute_delayed_physnames (cu);
10524
10525 if (cu->language == language_rust)
10526 rust_union_quirks (cu);
10527
10528 /* TUs share symbol tables.
10529 If this is the first TU to use this symtab, complete the construction
10530 of it with end_expandable_symtab. Otherwise, complete the addition of
10531 this TU's symbols to the existing symtab. */
10532 if (sig_type->type_unit_group->compunit_symtab == NULL)
10533 {
10534 buildsym_compunit *builder = cu->get_builder ();
10535 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
10536 sig_type->type_unit_group->compunit_symtab = cust;
10537
10538 if (cust != NULL)
10539 {
10540 /* Set symtab language to language from DW_AT_language. If the
10541 compilation is from a C file generated by language preprocessors,
10542 do not set the language if it was already deduced by
10543 start_subfile. */
10544 if (!(cu->language == language_c
10545 && COMPUNIT_FILETABS (cust)->language != language_c))
10546 COMPUNIT_FILETABS (cust)->language = cu->language;
10547 }
10548 }
10549 else
10550 {
10551 cu->get_builder ()->augment_type_symtab ();
10552 cust = sig_type->type_unit_group->compunit_symtab;
10553 }
10554
10555 if (dwarf2_per_objfile->using_index)
10556 per_cu->v.quick->compunit_symtab = cust;
10557 else
10558 {
10559 struct partial_symtab *pst = per_cu->v.psymtab;
10560 pst->compunit_symtab = cust;
10561 pst->readin = 1;
10562 }
10563
10564 /* Not needed any more. */
10565 cu->reset_builder ();
10566 }
10567
10568 /* Process an imported unit DIE. */
10569
10570 static void
10571 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10572 {
10573 struct attribute *attr;
10574
10575 /* For now we don't handle imported units in type units. */
10576 if (cu->per_cu->is_debug_types)
10577 {
10578 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10579 " supported in type units [in module %s]"),
10580 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
10581 }
10582
10583 attr = dwarf2_attr (die, DW_AT_import, cu);
10584 if (attr != NULL)
10585 {
10586 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10587 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10588 dwarf2_per_cu_data *per_cu
10589 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
10590 cu->per_cu->dwarf2_per_objfile);
10591
10592 /* If necessary, add it to the queue and load its DIEs. */
10593 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
10594 load_full_comp_unit (per_cu, false, cu->language);
10595
10596 cu->per_cu->imported_symtabs_push (per_cu);
10597 }
10598 }
10599
10600 /* RAII object that represents a process_die scope: i.e.,
10601 starts/finishes processing a DIE. */
10602 class process_die_scope
10603 {
10604 public:
10605 process_die_scope (die_info *die, dwarf2_cu *cu)
10606 : m_die (die), m_cu (cu)
10607 {
10608 /* We should only be processing DIEs not already in process. */
10609 gdb_assert (!m_die->in_process);
10610 m_die->in_process = true;
10611 }
10612
10613 ~process_die_scope ()
10614 {
10615 m_die->in_process = false;
10616
10617 /* If we're done processing the DIE for the CU that owns the line
10618 header, we don't need the line header anymore. */
10619 if (m_cu->line_header_die_owner == m_die)
10620 {
10621 delete m_cu->line_header;
10622 m_cu->line_header = NULL;
10623 m_cu->line_header_die_owner = NULL;
10624 }
10625 }
10626
10627 private:
10628 die_info *m_die;
10629 dwarf2_cu *m_cu;
10630 };
10631
10632 /* Process a die and its children. */
10633
10634 static void
10635 process_die (struct die_info *die, struct dwarf2_cu *cu)
10636 {
10637 process_die_scope scope (die, cu);
10638
10639 switch (die->tag)
10640 {
10641 case DW_TAG_padding:
10642 break;
10643 case DW_TAG_compile_unit:
10644 case DW_TAG_partial_unit:
10645 read_file_scope (die, cu);
10646 break;
10647 case DW_TAG_type_unit:
10648 read_type_unit_scope (die, cu);
10649 break;
10650 case DW_TAG_subprogram:
10651 /* Nested subprograms in Fortran get a prefix. */
10652 if (cu->language == language_fortran
10653 && die->parent != NULL
10654 && die->parent->tag == DW_TAG_subprogram)
10655 cu->processing_has_namespace_info = true;
10656 /* Fall through. */
10657 case DW_TAG_inlined_subroutine:
10658 read_func_scope (die, cu);
10659 break;
10660 case DW_TAG_lexical_block:
10661 case DW_TAG_try_block:
10662 case DW_TAG_catch_block:
10663 read_lexical_block_scope (die, cu);
10664 break;
10665 case DW_TAG_call_site:
10666 case DW_TAG_GNU_call_site:
10667 read_call_site_scope (die, cu);
10668 break;
10669 case DW_TAG_class_type:
10670 case DW_TAG_interface_type:
10671 case DW_TAG_structure_type:
10672 case DW_TAG_union_type:
10673 process_structure_scope (die, cu);
10674 break;
10675 case DW_TAG_enumeration_type:
10676 process_enumeration_scope (die, cu);
10677 break;
10678
10679 /* These dies have a type, but processing them does not create
10680 a symbol or recurse to process the children. Therefore we can
10681 read them on-demand through read_type_die. */
10682 case DW_TAG_subroutine_type:
10683 case DW_TAG_set_type:
10684 case DW_TAG_array_type:
10685 case DW_TAG_pointer_type:
10686 case DW_TAG_ptr_to_member_type:
10687 case DW_TAG_reference_type:
10688 case DW_TAG_rvalue_reference_type:
10689 case DW_TAG_string_type:
10690 break;
10691
10692 case DW_TAG_base_type:
10693 case DW_TAG_subrange_type:
10694 case DW_TAG_typedef:
10695 /* Add a typedef symbol for the type definition, if it has a
10696 DW_AT_name. */
10697 new_symbol (die, read_type_die (die, cu), cu);
10698 break;
10699 case DW_TAG_common_block:
10700 read_common_block (die, cu);
10701 break;
10702 case DW_TAG_common_inclusion:
10703 break;
10704 case DW_TAG_namespace:
10705 cu->processing_has_namespace_info = true;
10706 read_namespace (die, cu);
10707 break;
10708 case DW_TAG_module:
10709 cu->processing_has_namespace_info = true;
10710 read_module (die, cu);
10711 break;
10712 case DW_TAG_imported_declaration:
10713 cu->processing_has_namespace_info = true;
10714 if (read_namespace_alias (die, cu))
10715 break;
10716 /* The declaration is not a global namespace alias. */
10717 /* Fall through. */
10718 case DW_TAG_imported_module:
10719 cu->processing_has_namespace_info = true;
10720 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10721 || cu->language != language_fortran))
10722 complaint (_("Tag '%s' has unexpected children"),
10723 dwarf_tag_name (die->tag));
10724 read_import_statement (die, cu);
10725 break;
10726
10727 case DW_TAG_imported_unit:
10728 process_imported_unit_die (die, cu);
10729 break;
10730
10731 case DW_TAG_variable:
10732 read_variable (die, cu);
10733 break;
10734
10735 default:
10736 new_symbol (die, NULL, cu);
10737 break;
10738 }
10739 }
10740 \f
10741 /* DWARF name computation. */
10742
10743 /* A helper function for dwarf2_compute_name which determines whether DIE
10744 needs to have the name of the scope prepended to the name listed in the
10745 die. */
10746
10747 static int
10748 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10749 {
10750 struct attribute *attr;
10751
10752 switch (die->tag)
10753 {
10754 case DW_TAG_namespace:
10755 case DW_TAG_typedef:
10756 case DW_TAG_class_type:
10757 case DW_TAG_interface_type:
10758 case DW_TAG_structure_type:
10759 case DW_TAG_union_type:
10760 case DW_TAG_enumeration_type:
10761 case DW_TAG_enumerator:
10762 case DW_TAG_subprogram:
10763 case DW_TAG_inlined_subroutine:
10764 case DW_TAG_member:
10765 case DW_TAG_imported_declaration:
10766 return 1;
10767
10768 case DW_TAG_variable:
10769 case DW_TAG_constant:
10770 /* We only need to prefix "globally" visible variables. These include
10771 any variable marked with DW_AT_external or any variable that
10772 lives in a namespace. [Variables in anonymous namespaces
10773 require prefixing, but they are not DW_AT_external.] */
10774
10775 if (dwarf2_attr (die, DW_AT_specification, cu))
10776 {
10777 struct dwarf2_cu *spec_cu = cu;
10778
10779 return die_needs_namespace (die_specification (die, &spec_cu),
10780 spec_cu);
10781 }
10782
10783 attr = dwarf2_attr (die, DW_AT_external, cu);
10784 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10785 && die->parent->tag != DW_TAG_module)
10786 return 0;
10787 /* A variable in a lexical block of some kind does not need a
10788 namespace, even though in C++ such variables may be external
10789 and have a mangled name. */
10790 if (die->parent->tag == DW_TAG_lexical_block
10791 || die->parent->tag == DW_TAG_try_block
10792 || die->parent->tag == DW_TAG_catch_block
10793 || die->parent->tag == DW_TAG_subprogram)
10794 return 0;
10795 return 1;
10796
10797 default:
10798 return 0;
10799 }
10800 }
10801
10802 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10803 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10804 defined for the given DIE. */
10805
10806 static struct attribute *
10807 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10808 {
10809 struct attribute *attr;
10810
10811 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10812 if (attr == NULL)
10813 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10814
10815 return attr;
10816 }
10817
10818 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10819 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10820 defined for the given DIE. */
10821
10822 static const char *
10823 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10824 {
10825 const char *linkage_name;
10826
10827 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10828 if (linkage_name == NULL)
10829 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10830
10831 return linkage_name;
10832 }
10833
10834 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
10835 compute the physname for the object, which include a method's:
10836 - formal parameters (C++),
10837 - receiver type (Go),
10838
10839 The term "physname" is a bit confusing.
10840 For C++, for example, it is the demangled name.
10841 For Go, for example, it's the mangled name.
10842
10843 For Ada, return the DIE's linkage name rather than the fully qualified
10844 name. PHYSNAME is ignored..
10845
10846 The result is allocated on the objfile_obstack and canonicalized. */
10847
10848 static const char *
10849 dwarf2_compute_name (const char *name,
10850 struct die_info *die, struct dwarf2_cu *cu,
10851 int physname)
10852 {
10853 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10854
10855 if (name == NULL)
10856 name = dwarf2_name (die, cu);
10857
10858 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10859 but otherwise compute it by typename_concat inside GDB.
10860 FIXME: Actually this is not really true, or at least not always true.
10861 It's all very confusing. compute_and_set_names doesn't try to demangle
10862 Fortran names because there is no mangling standard. So new_symbol
10863 will set the demangled name to the result of dwarf2_full_name, and it is
10864 the demangled name that GDB uses if it exists. */
10865 if (cu->language == language_ada
10866 || (cu->language == language_fortran && physname))
10867 {
10868 /* For Ada unit, we prefer the linkage name over the name, as
10869 the former contains the exported name, which the user expects
10870 to be able to reference. Ideally, we want the user to be able
10871 to reference this entity using either natural or linkage name,
10872 but we haven't started looking at this enhancement yet. */
10873 const char *linkage_name = dw2_linkage_name (die, cu);
10874
10875 if (linkage_name != NULL)
10876 return linkage_name;
10877 }
10878
10879 /* These are the only languages we know how to qualify names in. */
10880 if (name != NULL
10881 && (cu->language == language_cplus
10882 || cu->language == language_fortran || cu->language == language_d
10883 || cu->language == language_rust))
10884 {
10885 if (die_needs_namespace (die, cu))
10886 {
10887 const char *prefix;
10888 const char *canonical_name = NULL;
10889
10890 string_file buf;
10891
10892 prefix = determine_prefix (die, cu);
10893 if (*prefix != '\0')
10894 {
10895 gdb::unique_xmalloc_ptr<char> prefixed_name
10896 (typename_concat (NULL, prefix, name, physname, cu));
10897
10898 buf.puts (prefixed_name.get ());
10899 }
10900 else
10901 buf.puts (name);
10902
10903 /* Template parameters may be specified in the DIE's DW_AT_name, or
10904 as children with DW_TAG_template_type_param or
10905 DW_TAG_value_type_param. If the latter, add them to the name
10906 here. If the name already has template parameters, then
10907 skip this step; some versions of GCC emit both, and
10908 it is more efficient to use the pre-computed name.
10909
10910 Something to keep in mind about this process: it is very
10911 unlikely, or in some cases downright impossible, to produce
10912 something that will match the mangled name of a function.
10913 If the definition of the function has the same debug info,
10914 we should be able to match up with it anyway. But fallbacks
10915 using the minimal symbol, for instance to find a method
10916 implemented in a stripped copy of libstdc++, will not work.
10917 If we do not have debug info for the definition, we will have to
10918 match them up some other way.
10919
10920 When we do name matching there is a related problem with function
10921 templates; two instantiated function templates are allowed to
10922 differ only by their return types, which we do not add here. */
10923
10924 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10925 {
10926 struct attribute *attr;
10927 struct die_info *child;
10928 int first = 1;
10929
10930 die->building_fullname = 1;
10931
10932 for (child = die->child; child != NULL; child = child->sibling)
10933 {
10934 struct type *type;
10935 LONGEST value;
10936 const gdb_byte *bytes;
10937 struct dwarf2_locexpr_baton *baton;
10938 struct value *v;
10939
10940 if (child->tag != DW_TAG_template_type_param
10941 && child->tag != DW_TAG_template_value_param)
10942 continue;
10943
10944 if (first)
10945 {
10946 buf.puts ("<");
10947 first = 0;
10948 }
10949 else
10950 buf.puts (", ");
10951
10952 attr = dwarf2_attr (child, DW_AT_type, cu);
10953 if (attr == NULL)
10954 {
10955 complaint (_("template parameter missing DW_AT_type"));
10956 buf.puts ("UNKNOWN_TYPE");
10957 continue;
10958 }
10959 type = die_type (child, cu);
10960
10961 if (child->tag == DW_TAG_template_type_param)
10962 {
10963 c_print_type (type, "", &buf, -1, 0, cu->language,
10964 &type_print_raw_options);
10965 continue;
10966 }
10967
10968 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10969 if (attr == NULL)
10970 {
10971 complaint (_("template parameter missing "
10972 "DW_AT_const_value"));
10973 buf.puts ("UNKNOWN_VALUE");
10974 continue;
10975 }
10976
10977 dwarf2_const_value_attr (attr, type, name,
10978 &cu->comp_unit_obstack, cu,
10979 &value, &bytes, &baton);
10980
10981 if (TYPE_NOSIGN (type))
10982 /* GDB prints characters as NUMBER 'CHAR'. If that's
10983 changed, this can use value_print instead. */
10984 c_printchar (value, type, &buf);
10985 else
10986 {
10987 struct value_print_options opts;
10988
10989 if (baton != NULL)
10990 v = dwarf2_evaluate_loc_desc (type, NULL,
10991 baton->data,
10992 baton->size,
10993 baton->per_cu);
10994 else if (bytes != NULL)
10995 {
10996 v = allocate_value (type);
10997 memcpy (value_contents_writeable (v), bytes,
10998 TYPE_LENGTH (type));
10999 }
11000 else
11001 v = value_from_longest (type, value);
11002
11003 /* Specify decimal so that we do not depend on
11004 the radix. */
11005 get_formatted_print_options (&opts, 'd');
11006 opts.raw = 1;
11007 value_print (v, &buf, &opts);
11008 release_value (v);
11009 }
11010 }
11011
11012 die->building_fullname = 0;
11013
11014 if (!first)
11015 {
11016 /* Close the argument list, with a space if necessary
11017 (nested templates). */
11018 if (!buf.empty () && buf.string ().back () == '>')
11019 buf.puts (" >");
11020 else
11021 buf.puts (">");
11022 }
11023 }
11024
11025 /* For C++ methods, append formal parameter type
11026 information, if PHYSNAME. */
11027
11028 if (physname && die->tag == DW_TAG_subprogram
11029 && cu->language == language_cplus)
11030 {
11031 struct type *type = read_type_die (die, cu);
11032
11033 c_type_print_args (type, &buf, 1, cu->language,
11034 &type_print_raw_options);
11035
11036 if (cu->language == language_cplus)
11037 {
11038 /* Assume that an artificial first parameter is
11039 "this", but do not crash if it is not. RealView
11040 marks unnamed (and thus unused) parameters as
11041 artificial; there is no way to differentiate
11042 the two cases. */
11043 if (TYPE_NFIELDS (type) > 0
11044 && TYPE_FIELD_ARTIFICIAL (type, 0)
11045 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
11046 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
11047 0))))
11048 buf.puts (" const");
11049 }
11050 }
11051
11052 const std::string &intermediate_name = buf.string ();
11053
11054 if (cu->language == language_cplus)
11055 canonical_name
11056 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
11057 &objfile->per_bfd->storage_obstack);
11058
11059 /* If we only computed INTERMEDIATE_NAME, or if
11060 INTERMEDIATE_NAME is already canonical, then we need to
11061 copy it to the appropriate obstack. */
11062 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
11063 name = obstack_strdup (&objfile->per_bfd->storage_obstack,
11064 intermediate_name);
11065 else
11066 name = canonical_name;
11067 }
11068 }
11069
11070 return name;
11071 }
11072
11073 /* Return the fully qualified name of DIE, based on its DW_AT_name.
11074 If scope qualifiers are appropriate they will be added. The result
11075 will be allocated on the storage_obstack, or NULL if the DIE does
11076 not have a name. NAME may either be from a previous call to
11077 dwarf2_name or NULL.
11078
11079 The output string will be canonicalized (if C++). */
11080
11081 static const char *
11082 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
11083 {
11084 return dwarf2_compute_name (name, die, cu, 0);
11085 }
11086
11087 /* Construct a physname for the given DIE in CU. NAME may either be
11088 from a previous call to dwarf2_name or NULL. The result will be
11089 allocated on the objfile_objstack or NULL if the DIE does not have a
11090 name.
11091
11092 The output string will be canonicalized (if C++). */
11093
11094 static const char *
11095 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
11096 {
11097 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
11098 const char *retval, *mangled = NULL, *canon = NULL;
11099 int need_copy = 1;
11100
11101 /* In this case dwarf2_compute_name is just a shortcut not building anything
11102 on its own. */
11103 if (!die_needs_namespace (die, cu))
11104 return dwarf2_compute_name (name, die, cu, 1);
11105
11106 mangled = dw2_linkage_name (die, cu);
11107
11108 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
11109 See https://github.com/rust-lang/rust/issues/32925. */
11110 if (cu->language == language_rust && mangled != NULL
11111 && strchr (mangled, '{') != NULL)
11112 mangled = NULL;
11113
11114 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
11115 has computed. */
11116 gdb::unique_xmalloc_ptr<char> demangled;
11117 if (mangled != NULL)
11118 {
11119
11120 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
11121 {
11122 /* Do nothing (do not demangle the symbol name). */
11123 }
11124 else if (cu->language == language_go)
11125 {
11126 /* This is a lie, but we already lie to the caller new_symbol.
11127 new_symbol assumes we return the mangled name.
11128 This just undoes that lie until things are cleaned up. */
11129 }
11130 else
11131 {
11132 /* Use DMGL_RET_DROP for C++ template functions to suppress
11133 their return type. It is easier for GDB users to search
11134 for such functions as `name(params)' than `long name(params)'.
11135 In such case the minimal symbol names do not match the full
11136 symbol names but for template functions there is never a need
11137 to look up their definition from their declaration so
11138 the only disadvantage remains the minimal symbol variant
11139 `long name(params)' does not have the proper inferior type. */
11140 demangled.reset (gdb_demangle (mangled,
11141 (DMGL_PARAMS | DMGL_ANSI
11142 | DMGL_RET_DROP)));
11143 }
11144 if (demangled)
11145 canon = demangled.get ();
11146 else
11147 {
11148 canon = mangled;
11149 need_copy = 0;
11150 }
11151 }
11152
11153 if (canon == NULL || check_physname)
11154 {
11155 const char *physname = dwarf2_compute_name (name, die, cu, 1);
11156
11157 if (canon != NULL && strcmp (physname, canon) != 0)
11158 {
11159 /* It may not mean a bug in GDB. The compiler could also
11160 compute DW_AT_linkage_name incorrectly. But in such case
11161 GDB would need to be bug-to-bug compatible. */
11162
11163 complaint (_("Computed physname <%s> does not match demangled <%s> "
11164 "(from linkage <%s>) - DIE at %s [in module %s]"),
11165 physname, canon, mangled, sect_offset_str (die->sect_off),
11166 objfile_name (objfile));
11167
11168 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
11169 is available here - over computed PHYSNAME. It is safer
11170 against both buggy GDB and buggy compilers. */
11171
11172 retval = canon;
11173 }
11174 else
11175 {
11176 retval = physname;
11177 need_copy = 0;
11178 }
11179 }
11180 else
11181 retval = canon;
11182
11183 if (need_copy)
11184 retval = obstack_strdup (&objfile->per_bfd->storage_obstack, retval);
11185
11186 return retval;
11187 }
11188
11189 /* Inspect DIE in CU for a namespace alias. If one exists, record
11190 a new symbol for it.
11191
11192 Returns 1 if a namespace alias was recorded, 0 otherwise. */
11193
11194 static int
11195 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
11196 {
11197 struct attribute *attr;
11198
11199 /* If the die does not have a name, this is not a namespace
11200 alias. */
11201 attr = dwarf2_attr (die, DW_AT_name, cu);
11202 if (attr != NULL)
11203 {
11204 int num;
11205 struct die_info *d = die;
11206 struct dwarf2_cu *imported_cu = cu;
11207
11208 /* If the compiler has nested DW_AT_imported_declaration DIEs,
11209 keep inspecting DIEs until we hit the underlying import. */
11210 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
11211 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
11212 {
11213 attr = dwarf2_attr (d, DW_AT_import, cu);
11214 if (attr == NULL)
11215 break;
11216
11217 d = follow_die_ref (d, attr, &imported_cu);
11218 if (d->tag != DW_TAG_imported_declaration)
11219 break;
11220 }
11221
11222 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
11223 {
11224 complaint (_("DIE at %s has too many recursively imported "
11225 "declarations"), sect_offset_str (d->sect_off));
11226 return 0;
11227 }
11228
11229 if (attr != NULL)
11230 {
11231 struct type *type;
11232 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
11233
11234 type = get_die_type_at_offset (sect_off, cu->per_cu);
11235 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
11236 {
11237 /* This declaration is a global namespace alias. Add
11238 a symbol for it whose type is the aliased namespace. */
11239 new_symbol (die, type, cu);
11240 return 1;
11241 }
11242 }
11243 }
11244
11245 return 0;
11246 }
11247
11248 /* Return the using directives repository (global or local?) to use in the
11249 current context for CU.
11250
11251 For Ada, imported declarations can materialize renamings, which *may* be
11252 global. However it is impossible (for now?) in DWARF to distinguish
11253 "external" imported declarations and "static" ones. As all imported
11254 declarations seem to be static in all other languages, make them all CU-wide
11255 global only in Ada. */
11256
11257 static struct using_direct **
11258 using_directives (struct dwarf2_cu *cu)
11259 {
11260 if (cu->language == language_ada
11261 && cu->get_builder ()->outermost_context_p ())
11262 return cu->get_builder ()->get_global_using_directives ();
11263 else
11264 return cu->get_builder ()->get_local_using_directives ();
11265 }
11266
11267 /* Read the import statement specified by the given die and record it. */
11268
11269 static void
11270 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
11271 {
11272 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
11273 struct attribute *import_attr;
11274 struct die_info *imported_die, *child_die;
11275 struct dwarf2_cu *imported_cu;
11276 const char *imported_name;
11277 const char *imported_name_prefix;
11278 const char *canonical_name;
11279 const char *import_alias;
11280 const char *imported_declaration = NULL;
11281 const char *import_prefix;
11282 std::vector<const char *> excludes;
11283
11284 import_attr = dwarf2_attr (die, DW_AT_import, cu);
11285 if (import_attr == NULL)
11286 {
11287 complaint (_("Tag '%s' has no DW_AT_import"),
11288 dwarf_tag_name (die->tag));
11289 return;
11290 }
11291
11292 imported_cu = cu;
11293 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
11294 imported_name = dwarf2_name (imported_die, imported_cu);
11295 if (imported_name == NULL)
11296 {
11297 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
11298
11299 The import in the following code:
11300 namespace A
11301 {
11302 typedef int B;
11303 }
11304
11305 int main ()
11306 {
11307 using A::B;
11308 B b;
11309 return b;
11310 }
11311
11312 ...
11313 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
11314 <52> DW_AT_decl_file : 1
11315 <53> DW_AT_decl_line : 6
11316 <54> DW_AT_import : <0x75>
11317 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
11318 <59> DW_AT_name : B
11319 <5b> DW_AT_decl_file : 1
11320 <5c> DW_AT_decl_line : 2
11321 <5d> DW_AT_type : <0x6e>
11322 ...
11323 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
11324 <76> DW_AT_byte_size : 4
11325 <77> DW_AT_encoding : 5 (signed)
11326
11327 imports the wrong die ( 0x75 instead of 0x58 ).
11328 This case will be ignored until the gcc bug is fixed. */
11329 return;
11330 }
11331
11332 /* Figure out the local name after import. */
11333 import_alias = dwarf2_name (die, cu);
11334
11335 /* Figure out where the statement is being imported to. */
11336 import_prefix = determine_prefix (die, cu);
11337
11338 /* Figure out what the scope of the imported die is and prepend it
11339 to the name of the imported die. */
11340 imported_name_prefix = determine_prefix (imported_die, imported_cu);
11341
11342 if (imported_die->tag != DW_TAG_namespace
11343 && imported_die->tag != DW_TAG_module)
11344 {
11345 imported_declaration = imported_name;
11346 canonical_name = imported_name_prefix;
11347 }
11348 else if (strlen (imported_name_prefix) > 0)
11349 canonical_name = obconcat (&objfile->objfile_obstack,
11350 imported_name_prefix,
11351 (cu->language == language_d ? "." : "::"),
11352 imported_name, (char *) NULL);
11353 else
11354 canonical_name = imported_name;
11355
11356 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
11357 for (child_die = die->child; child_die && child_die->tag;
11358 child_die = sibling_die (child_die))
11359 {
11360 /* DWARF-4: A Fortran use statement with a “rename list” may be
11361 represented by an imported module entry with an import attribute
11362 referring to the module and owned entries corresponding to those
11363 entities that are renamed as part of being imported. */
11364
11365 if (child_die->tag != DW_TAG_imported_declaration)
11366 {
11367 complaint (_("child DW_TAG_imported_declaration expected "
11368 "- DIE at %s [in module %s]"),
11369 sect_offset_str (child_die->sect_off),
11370 objfile_name (objfile));
11371 continue;
11372 }
11373
11374 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
11375 if (import_attr == NULL)
11376 {
11377 complaint (_("Tag '%s' has no DW_AT_import"),
11378 dwarf_tag_name (child_die->tag));
11379 continue;
11380 }
11381
11382 imported_cu = cu;
11383 imported_die = follow_die_ref_or_sig (child_die, import_attr,
11384 &imported_cu);
11385 imported_name = dwarf2_name (imported_die, imported_cu);
11386 if (imported_name == NULL)
11387 {
11388 complaint (_("child DW_TAG_imported_declaration has unknown "
11389 "imported name - DIE at %s [in module %s]"),
11390 sect_offset_str (child_die->sect_off),
11391 objfile_name (objfile));
11392 continue;
11393 }
11394
11395 excludes.push_back (imported_name);
11396
11397 process_die (child_die, cu);
11398 }
11399
11400 add_using_directive (using_directives (cu),
11401 import_prefix,
11402 canonical_name,
11403 import_alias,
11404 imported_declaration,
11405 excludes,
11406 0,
11407 &objfile->objfile_obstack);
11408 }
11409
11410 /* ICC<14 does not output the required DW_AT_declaration on incomplete
11411 types, but gives them a size of zero. Starting with version 14,
11412 ICC is compatible with GCC. */
11413
11414 static bool
11415 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
11416 {
11417 if (!cu->checked_producer)
11418 check_producer (cu);
11419
11420 return cu->producer_is_icc_lt_14;
11421 }
11422
11423 /* ICC generates a DW_AT_type for C void functions. This was observed on
11424 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
11425 which says that void functions should not have a DW_AT_type. */
11426
11427 static bool
11428 producer_is_icc (struct dwarf2_cu *cu)
11429 {
11430 if (!cu->checked_producer)
11431 check_producer (cu);
11432
11433 return cu->producer_is_icc;
11434 }
11435
11436 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11437 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11438 this, it was first present in GCC release 4.3.0. */
11439
11440 static bool
11441 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11442 {
11443 if (!cu->checked_producer)
11444 check_producer (cu);
11445
11446 return cu->producer_is_gcc_lt_4_3;
11447 }
11448
11449 static file_and_directory
11450 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
11451 {
11452 file_and_directory res;
11453
11454 /* Find the filename. Do not use dwarf2_name here, since the filename
11455 is not a source language identifier. */
11456 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11457 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
11458
11459 if (res.comp_dir == NULL
11460 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11461 && IS_ABSOLUTE_PATH (res.name))
11462 {
11463 res.comp_dir_storage = ldirname (res.name);
11464 if (!res.comp_dir_storage.empty ())
11465 res.comp_dir = res.comp_dir_storage.c_str ();
11466 }
11467 if (res.comp_dir != NULL)
11468 {
11469 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11470 directory, get rid of it. */
11471 const char *cp = strchr (res.comp_dir, ':');
11472
11473 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11474 res.comp_dir = cp + 1;
11475 }
11476
11477 if (res.name == NULL)
11478 res.name = "<unknown>";
11479
11480 return res;
11481 }
11482
11483 /* Handle DW_AT_stmt_list for a compilation unit.
11484 DIE is the DW_TAG_compile_unit die for CU.
11485 COMP_DIR is the compilation directory. LOWPC is passed to
11486 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
11487
11488 static void
11489 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
11490 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
11491 {
11492 struct dwarf2_per_objfile *dwarf2_per_objfile
11493 = cu->per_cu->dwarf2_per_objfile;
11494 struct objfile *objfile = dwarf2_per_objfile->objfile;
11495 struct attribute *attr;
11496 struct line_header line_header_local;
11497 hashval_t line_header_local_hash;
11498 void **slot;
11499 int decode_mapping;
11500
11501 gdb_assert (! cu->per_cu->is_debug_types);
11502
11503 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
11504 if (attr == NULL)
11505 return;
11506
11507 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11508
11509 /* The line header hash table is only created if needed (it exists to
11510 prevent redundant reading of the line table for partial_units).
11511 If we're given a partial_unit, we'll need it. If we're given a
11512 compile_unit, then use the line header hash table if it's already
11513 created, but don't create one just yet. */
11514
11515 if (dwarf2_per_objfile->line_header_hash == NULL
11516 && die->tag == DW_TAG_partial_unit)
11517 {
11518 dwarf2_per_objfile->line_header_hash
11519 = htab_create_alloc_ex (127, line_header_hash_voidp,
11520 line_header_eq_voidp,
11521 free_line_header_voidp,
11522 &objfile->objfile_obstack,
11523 hashtab_obstack_allocate,
11524 dummy_obstack_deallocate);
11525 }
11526
11527 line_header_local.sect_off = line_offset;
11528 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11529 line_header_local_hash = line_header_hash (&line_header_local);
11530 if (dwarf2_per_objfile->line_header_hash != NULL)
11531 {
11532 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11533 &line_header_local,
11534 line_header_local_hash, NO_INSERT);
11535
11536 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11537 is not present in *SLOT (since if there is something in *SLOT then
11538 it will be for a partial_unit). */
11539 if (die->tag == DW_TAG_partial_unit && slot != NULL)
11540 {
11541 gdb_assert (*slot != NULL);
11542 cu->line_header = (struct line_header *) *slot;
11543 return;
11544 }
11545 }
11546
11547 /* dwarf_decode_line_header does not yet provide sufficient information.
11548 We always have to call also dwarf_decode_lines for it. */
11549 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11550 if (lh == NULL)
11551 return;
11552
11553 cu->line_header = lh.release ();
11554 cu->line_header_die_owner = die;
11555
11556 if (dwarf2_per_objfile->line_header_hash == NULL)
11557 slot = NULL;
11558 else
11559 {
11560 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11561 &line_header_local,
11562 line_header_local_hash, INSERT);
11563 gdb_assert (slot != NULL);
11564 }
11565 if (slot != NULL && *slot == NULL)
11566 {
11567 /* This newly decoded line number information unit will be owned
11568 by line_header_hash hash table. */
11569 *slot = cu->line_header;
11570 cu->line_header_die_owner = NULL;
11571 }
11572 else
11573 {
11574 /* We cannot free any current entry in (*slot) as that struct line_header
11575 may be already used by multiple CUs. Create only temporary decoded
11576 line_header for this CU - it may happen at most once for each line
11577 number information unit. And if we're not using line_header_hash
11578 then this is what we want as well. */
11579 gdb_assert (die->tag != DW_TAG_partial_unit);
11580 }
11581 decode_mapping = (die->tag != DW_TAG_partial_unit);
11582 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11583 decode_mapping);
11584
11585 }
11586
11587 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
11588
11589 static void
11590 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
11591 {
11592 struct dwarf2_per_objfile *dwarf2_per_objfile
11593 = cu->per_cu->dwarf2_per_objfile;
11594 struct objfile *objfile = dwarf2_per_objfile->objfile;
11595 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11596 CORE_ADDR lowpc = ((CORE_ADDR) -1);
11597 CORE_ADDR highpc = ((CORE_ADDR) 0);
11598 struct attribute *attr;
11599 struct die_info *child_die;
11600 CORE_ADDR baseaddr;
11601
11602 prepare_one_comp_unit (cu, die, cu->language);
11603 baseaddr = objfile->section_offsets[SECT_OFF_TEXT (objfile)];
11604
11605 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
11606
11607 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11608 from finish_block. */
11609 if (lowpc == ((CORE_ADDR) -1))
11610 lowpc = highpc;
11611 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11612
11613 file_and_directory fnd = find_file_and_directory (die, cu);
11614
11615 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11616 standardised yet. As a workaround for the language detection we fall
11617 back to the DW_AT_producer string. */
11618 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11619 cu->language = language_opencl;
11620
11621 /* Similar hack for Go. */
11622 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11623 set_cu_language (DW_LANG_Go, cu);
11624
11625 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
11626
11627 /* Decode line number information if present. We do this before
11628 processing child DIEs, so that the line header table is available
11629 for DW_AT_decl_file. */
11630 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
11631
11632 /* Process all dies in compilation unit. */
11633 if (die->child != NULL)
11634 {
11635 child_die = die->child;
11636 while (child_die && child_die->tag)
11637 {
11638 process_die (child_die, cu);
11639 child_die = sibling_die (child_die);
11640 }
11641 }
11642
11643 /* Decode macro information, if present. Dwarf 2 macro information
11644 refers to information in the line number info statement program
11645 header, so we can only read it if we've read the header
11646 successfully. */
11647 attr = dwarf2_attr (die, DW_AT_macros, cu);
11648 if (attr == NULL)
11649 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
11650 if (attr && cu->line_header)
11651 {
11652 if (dwarf2_attr (die, DW_AT_macro_info, cu))
11653 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
11654
11655 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
11656 }
11657 else
11658 {
11659 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11660 if (attr && cu->line_header)
11661 {
11662 unsigned int macro_offset = DW_UNSND (attr);
11663
11664 dwarf_decode_macros (cu, macro_offset, 0);
11665 }
11666 }
11667 }
11668
11669 void
11670 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
11671 {
11672 struct type_unit_group *tu_group;
11673 int first_time;
11674 struct attribute *attr;
11675 unsigned int i;
11676 struct signatured_type *sig_type;
11677
11678 gdb_assert (per_cu->is_debug_types);
11679 sig_type = (struct signatured_type *) per_cu;
11680
11681 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
11682
11683 /* If we're using .gdb_index (includes -readnow) then
11684 per_cu->type_unit_group may not have been set up yet. */
11685 if (sig_type->type_unit_group == NULL)
11686 sig_type->type_unit_group = get_type_unit_group (this, attr);
11687 tu_group = sig_type->type_unit_group;
11688
11689 /* If we've already processed this stmt_list there's no real need to
11690 do it again, we could fake it and just recreate the part we need
11691 (file name,index -> symtab mapping). If data shows this optimization
11692 is useful we can do it then. */
11693 first_time = tu_group->compunit_symtab == NULL;
11694
11695 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11696 debug info. */
11697 line_header_up lh;
11698 if (attr != NULL)
11699 {
11700 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11701 lh = dwarf_decode_line_header (line_offset, this);
11702 }
11703 if (lh == NULL)
11704 {
11705 if (first_time)
11706 start_symtab ("", NULL, 0);
11707 else
11708 {
11709 gdb_assert (tu_group->symtabs == NULL);
11710 gdb_assert (m_builder == nullptr);
11711 struct compunit_symtab *cust = tu_group->compunit_symtab;
11712 m_builder.reset (new struct buildsym_compunit
11713 (COMPUNIT_OBJFILE (cust), "",
11714 COMPUNIT_DIRNAME (cust),
11715 compunit_language (cust),
11716 0, cust));
11717 }
11718 return;
11719 }
11720
11721 line_header = lh.release ();
11722 line_header_die_owner = die;
11723
11724 if (first_time)
11725 {
11726 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
11727
11728 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11729 still initializing it, and our caller (a few levels up)
11730 process_full_type_unit still needs to know if this is the first
11731 time. */
11732
11733 tu_group->num_symtabs = line_header->file_names_size ();
11734 tu_group->symtabs = XNEWVEC (struct symtab *,
11735 line_header->file_names_size ());
11736
11737 auto &file_names = line_header->file_names ();
11738 for (i = 0; i < file_names.size (); ++i)
11739 {
11740 file_entry &fe = file_names[i];
11741 dwarf2_start_subfile (this, fe.name,
11742 fe.include_dir (line_header));
11743 buildsym_compunit *b = get_builder ();
11744 if (b->get_current_subfile ()->symtab == NULL)
11745 {
11746 /* NOTE: start_subfile will recognize when it's been
11747 passed a file it has already seen. So we can't
11748 assume there's a simple mapping from
11749 cu->line_header->file_names to subfiles, plus
11750 cu->line_header->file_names may contain dups. */
11751 b->get_current_subfile ()->symtab
11752 = allocate_symtab (cust, b->get_current_subfile ()->name);
11753 }
11754
11755 fe.symtab = b->get_current_subfile ()->symtab;
11756 tu_group->symtabs[i] = fe.symtab;
11757 }
11758 }
11759 else
11760 {
11761 gdb_assert (m_builder == nullptr);
11762 struct compunit_symtab *cust = tu_group->compunit_symtab;
11763 m_builder.reset (new struct buildsym_compunit
11764 (COMPUNIT_OBJFILE (cust), "",
11765 COMPUNIT_DIRNAME (cust),
11766 compunit_language (cust),
11767 0, cust));
11768
11769 auto &file_names = line_header->file_names ();
11770 for (i = 0; i < file_names.size (); ++i)
11771 {
11772 file_entry &fe = file_names[i];
11773 fe.symtab = tu_group->symtabs[i];
11774 }
11775 }
11776
11777 /* The main symtab is allocated last. Type units don't have DW_AT_name
11778 so they don't have a "real" (so to speak) symtab anyway.
11779 There is later code that will assign the main symtab to all symbols
11780 that don't have one. We need to handle the case of a symbol with a
11781 missing symtab (DW_AT_decl_file) anyway. */
11782 }
11783
11784 /* Process DW_TAG_type_unit.
11785 For TUs we want to skip the first top level sibling if it's not the
11786 actual type being defined by this TU. In this case the first top
11787 level sibling is there to provide context only. */
11788
11789 static void
11790 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11791 {
11792 struct die_info *child_die;
11793
11794 prepare_one_comp_unit (cu, die, language_minimal);
11795
11796 /* Initialize (or reinitialize) the machinery for building symtabs.
11797 We do this before processing child DIEs, so that the line header table
11798 is available for DW_AT_decl_file. */
11799 cu->setup_type_unit_groups (die);
11800
11801 if (die->child != NULL)
11802 {
11803 child_die = die->child;
11804 while (child_die && child_die->tag)
11805 {
11806 process_die (child_die, cu);
11807 child_die = sibling_die (child_die);
11808 }
11809 }
11810 }
11811 \f
11812 /* DWO/DWP files.
11813
11814 http://gcc.gnu.org/wiki/DebugFission
11815 http://gcc.gnu.org/wiki/DebugFissionDWP
11816
11817 To simplify handling of both DWO files ("object" files with the DWARF info)
11818 and DWP files (a file with the DWOs packaged up into one file), we treat
11819 DWP files as having a collection of virtual DWO files. */
11820
11821 static hashval_t
11822 hash_dwo_file (const void *item)
11823 {
11824 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
11825 hashval_t hash;
11826
11827 hash = htab_hash_string (dwo_file->dwo_name);
11828 if (dwo_file->comp_dir != NULL)
11829 hash += htab_hash_string (dwo_file->comp_dir);
11830 return hash;
11831 }
11832
11833 static int
11834 eq_dwo_file (const void *item_lhs, const void *item_rhs)
11835 {
11836 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11837 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
11838
11839 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11840 return 0;
11841 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11842 return lhs->comp_dir == rhs->comp_dir;
11843 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
11844 }
11845
11846 /* Allocate a hash table for DWO files. */
11847
11848 static htab_up
11849 allocate_dwo_file_hash_table (struct objfile *objfile)
11850 {
11851 auto delete_dwo_file = [] (void *item)
11852 {
11853 struct dwo_file *dwo_file = (struct dwo_file *) item;
11854
11855 delete dwo_file;
11856 };
11857
11858 return htab_up (htab_create_alloc_ex (41,
11859 hash_dwo_file,
11860 eq_dwo_file,
11861 delete_dwo_file,
11862 &objfile->objfile_obstack,
11863 hashtab_obstack_allocate,
11864 dummy_obstack_deallocate));
11865 }
11866
11867 /* Lookup DWO file DWO_NAME. */
11868
11869 static void **
11870 lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11871 const char *dwo_name,
11872 const char *comp_dir)
11873 {
11874 struct dwo_file find_entry;
11875 void **slot;
11876
11877 if (dwarf2_per_objfile->dwo_files == NULL)
11878 dwarf2_per_objfile->dwo_files
11879 = allocate_dwo_file_hash_table (dwarf2_per_objfile->objfile);
11880
11881 find_entry.dwo_name = dwo_name;
11882 find_entry.comp_dir = comp_dir;
11883 slot = htab_find_slot (dwarf2_per_objfile->dwo_files.get (), &find_entry,
11884 INSERT);
11885
11886 return slot;
11887 }
11888
11889 static hashval_t
11890 hash_dwo_unit (const void *item)
11891 {
11892 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11893
11894 /* This drops the top 32 bits of the id, but is ok for a hash. */
11895 return dwo_unit->signature;
11896 }
11897
11898 static int
11899 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11900 {
11901 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11902 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11903
11904 /* The signature is assumed to be unique within the DWO file.
11905 So while object file CU dwo_id's always have the value zero,
11906 that's OK, assuming each object file DWO file has only one CU,
11907 and that's the rule for now. */
11908 return lhs->signature == rhs->signature;
11909 }
11910
11911 /* Allocate a hash table for DWO CUs,TUs.
11912 There is one of these tables for each of CUs,TUs for each DWO file. */
11913
11914 static htab_t
11915 allocate_dwo_unit_table (struct objfile *objfile)
11916 {
11917 /* Start out with a pretty small number.
11918 Generally DWO files contain only one CU and maybe some TUs. */
11919 return htab_create_alloc_ex (3,
11920 hash_dwo_unit,
11921 eq_dwo_unit,
11922 NULL,
11923 &objfile->objfile_obstack,
11924 hashtab_obstack_allocate,
11925 dummy_obstack_deallocate);
11926 }
11927
11928 /* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
11929
11930 struct create_dwo_cu_data
11931 {
11932 struct dwo_file *dwo_file;
11933 struct dwo_unit dwo_unit;
11934 };
11935
11936 /* die_reader_func for create_dwo_cu. */
11937
11938 static void
11939 create_dwo_cu_reader (const struct die_reader_specs *reader,
11940 const gdb_byte *info_ptr,
11941 struct die_info *comp_unit_die,
11942 int has_children,
11943 void *datap)
11944 {
11945 struct dwarf2_cu *cu = reader->cu;
11946 sect_offset sect_off = cu->per_cu->sect_off;
11947 struct dwarf2_section_info *section = cu->per_cu->section;
11948 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
11949 struct dwo_file *dwo_file = data->dwo_file;
11950 struct dwo_unit *dwo_unit = &data->dwo_unit;
11951
11952 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11953 if (!signature.has_value ())
11954 {
11955 complaint (_("Dwarf Error: debug entry at offset %s is missing"
11956 " its dwo_id [in module %s]"),
11957 sect_offset_str (sect_off), dwo_file->dwo_name);
11958 return;
11959 }
11960
11961 dwo_unit->dwo_file = dwo_file;
11962 dwo_unit->signature = *signature;
11963 dwo_unit->section = section;
11964 dwo_unit->sect_off = sect_off;
11965 dwo_unit->length = cu->per_cu->length;
11966
11967 if (dwarf_read_debug)
11968 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11969 sect_offset_str (sect_off),
11970 hex_string (dwo_unit->signature));
11971 }
11972
11973 /* Create the dwo_units for the CUs in a DWO_FILE.
11974 Note: This function processes DWO files only, not DWP files. */
11975
11976 static void
11977 create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11978 struct dwo_file &dwo_file, dwarf2_section_info &section,
11979 htab_t &cus_htab)
11980 {
11981 struct objfile *objfile = dwarf2_per_objfile->objfile;
11982 const gdb_byte *info_ptr, *end_ptr;
11983
11984 dwarf2_read_section (objfile, &section);
11985 info_ptr = section.buffer;
11986
11987 if (info_ptr == NULL)
11988 return;
11989
11990 if (dwarf_read_debug)
11991 {
11992 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
11993 get_section_name (&section),
11994 get_section_file_name (&section));
11995 }
11996
11997 end_ptr = info_ptr + section.size;
11998 while (info_ptr < end_ptr)
11999 {
12000 struct dwarf2_per_cu_data per_cu;
12001 struct create_dwo_cu_data create_dwo_cu_data;
12002 struct dwo_unit *dwo_unit;
12003 void **slot;
12004 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
12005
12006 memset (&create_dwo_cu_data.dwo_unit, 0,
12007 sizeof (create_dwo_cu_data.dwo_unit));
12008 memset (&per_cu, 0, sizeof (per_cu));
12009 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
12010 per_cu.is_debug_types = 0;
12011 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
12012 per_cu.section = &section;
12013 create_dwo_cu_data.dwo_file = &dwo_file;
12014
12015 init_cutu_and_read_dies_no_follow (
12016 &per_cu, &dwo_file, create_dwo_cu_reader, &create_dwo_cu_data);
12017 info_ptr += per_cu.length;
12018
12019 // If the unit could not be parsed, skip it.
12020 if (create_dwo_cu_data.dwo_unit.dwo_file == NULL)
12021 continue;
12022
12023 if (cus_htab == NULL)
12024 cus_htab = allocate_dwo_unit_table (objfile);
12025
12026 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12027 *dwo_unit = create_dwo_cu_data.dwo_unit;
12028 slot = htab_find_slot (cus_htab, dwo_unit, INSERT);
12029 gdb_assert (slot != NULL);
12030 if (*slot != NULL)
12031 {
12032 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
12033 sect_offset dup_sect_off = dup_cu->sect_off;
12034
12035 complaint (_("debug cu entry at offset %s is duplicate to"
12036 " the entry at offset %s, signature %s"),
12037 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
12038 hex_string (dwo_unit->signature));
12039 }
12040 *slot = (void *)dwo_unit;
12041 }
12042 }
12043
12044 /* DWP file .debug_{cu,tu}_index section format:
12045 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
12046
12047 DWP Version 1:
12048
12049 Both index sections have the same format, and serve to map a 64-bit
12050 signature to a set of section numbers. Each section begins with a header,
12051 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
12052 indexes, and a pool of 32-bit section numbers. The index sections will be
12053 aligned at 8-byte boundaries in the file.
12054
12055 The index section header consists of:
12056
12057 V, 32 bit version number
12058 -, 32 bits unused
12059 N, 32 bit number of compilation units or type units in the index
12060 M, 32 bit number of slots in the hash table
12061
12062 Numbers are recorded using the byte order of the application binary.
12063
12064 The hash table begins at offset 16 in the section, and consists of an array
12065 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
12066 order of the application binary). Unused slots in the hash table are 0.
12067 (We rely on the extreme unlikeliness of a signature being exactly 0.)
12068
12069 The parallel table begins immediately after the hash table
12070 (at offset 16 + 8 * M from the beginning of the section), and consists of an
12071 array of 32-bit indexes (using the byte order of the application binary),
12072 corresponding 1-1 with slots in the hash table. Each entry in the parallel
12073 table contains a 32-bit index into the pool of section numbers. For unused
12074 hash table slots, the corresponding entry in the parallel table will be 0.
12075
12076 The pool of section numbers begins immediately following the hash table
12077 (at offset 16 + 12 * M from the beginning of the section). The pool of
12078 section numbers consists of an array of 32-bit words (using the byte order
12079 of the application binary). Each item in the array is indexed starting
12080 from 0. The hash table entry provides the index of the first section
12081 number in the set. Additional section numbers in the set follow, and the
12082 set is terminated by a 0 entry (section number 0 is not used in ELF).
12083
12084 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
12085 section must be the first entry in the set, and the .debug_abbrev.dwo must
12086 be the second entry. Other members of the set may follow in any order.
12087
12088 ---
12089
12090 DWP Version 2:
12091
12092 DWP Version 2 combines all the .debug_info, etc. sections into one,
12093 and the entries in the index tables are now offsets into these sections.
12094 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
12095 section.
12096
12097 Index Section Contents:
12098 Header
12099 Hash Table of Signatures dwp_hash_table.hash_table
12100 Parallel Table of Indices dwp_hash_table.unit_table
12101 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
12102 Table of Section Sizes dwp_hash_table.v2.sizes
12103
12104 The index section header consists of:
12105
12106 V, 32 bit version number
12107 L, 32 bit number of columns in the table of section offsets
12108 N, 32 bit number of compilation units or type units in the index
12109 M, 32 bit number of slots in the hash table
12110
12111 Numbers are recorded using the byte order of the application binary.
12112
12113 The hash table has the same format as version 1.
12114 The parallel table of indices has the same format as version 1,
12115 except that the entries are origin-1 indices into the table of sections
12116 offsets and the table of section sizes.
12117
12118 The table of offsets begins immediately following the parallel table
12119 (at offset 16 + 12 * M from the beginning of the section). The table is
12120 a two-dimensional array of 32-bit words (using the byte order of the
12121 application binary), with L columns and N+1 rows, in row-major order.
12122 Each row in the array is indexed starting from 0. The first row provides
12123 a key to the remaining rows: each column in this row provides an identifier
12124 for a debug section, and the offsets in the same column of subsequent rows
12125 refer to that section. The section identifiers are:
12126
12127 DW_SECT_INFO 1 .debug_info.dwo
12128 DW_SECT_TYPES 2 .debug_types.dwo
12129 DW_SECT_ABBREV 3 .debug_abbrev.dwo
12130 DW_SECT_LINE 4 .debug_line.dwo
12131 DW_SECT_LOC 5 .debug_loc.dwo
12132 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
12133 DW_SECT_MACINFO 7 .debug_macinfo.dwo
12134 DW_SECT_MACRO 8 .debug_macro.dwo
12135
12136 The offsets provided by the CU and TU index sections are the base offsets
12137 for the contributions made by each CU or TU to the corresponding section
12138 in the package file. Each CU and TU header contains an abbrev_offset
12139 field, used to find the abbreviations table for that CU or TU within the
12140 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
12141 be interpreted as relative to the base offset given in the index section.
12142 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
12143 should be interpreted as relative to the base offset for .debug_line.dwo,
12144 and offsets into other debug sections obtained from DWARF attributes should
12145 also be interpreted as relative to the corresponding base offset.
12146
12147 The table of sizes begins immediately following the table of offsets.
12148 Like the table of offsets, it is a two-dimensional array of 32-bit words,
12149 with L columns and N rows, in row-major order. Each row in the array is
12150 indexed starting from 1 (row 0 is shared by the two tables).
12151
12152 ---
12153
12154 Hash table lookup is handled the same in version 1 and 2:
12155
12156 We assume that N and M will not exceed 2^32 - 1.
12157 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
12158
12159 Given a 64-bit compilation unit signature or a type signature S, an entry
12160 in the hash table is located as follows:
12161
12162 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
12163 the low-order k bits all set to 1.
12164
12165 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
12166
12167 3) If the hash table entry at index H matches the signature, use that
12168 entry. If the hash table entry at index H is unused (all zeroes),
12169 terminate the search: the signature is not present in the table.
12170
12171 4) Let H = (H + H') modulo M. Repeat at Step 3.
12172
12173 Because M > N and H' and M are relatively prime, the search is guaranteed
12174 to stop at an unused slot or find the match. */
12175
12176 /* Create a hash table to map DWO IDs to their CU/TU entry in
12177 .debug_{info,types}.dwo in DWP_FILE.
12178 Returns NULL if there isn't one.
12179 Note: This function processes DWP files only, not DWO files. */
12180
12181 static struct dwp_hash_table *
12182 create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
12183 struct dwp_file *dwp_file, int is_debug_types)
12184 {
12185 struct objfile *objfile = dwarf2_per_objfile->objfile;
12186 bfd *dbfd = dwp_file->dbfd.get ();
12187 const gdb_byte *index_ptr, *index_end;
12188 struct dwarf2_section_info *index;
12189 uint32_t version, nr_columns, nr_units, nr_slots;
12190 struct dwp_hash_table *htab;
12191
12192 if (is_debug_types)
12193 index = &dwp_file->sections.tu_index;
12194 else
12195 index = &dwp_file->sections.cu_index;
12196
12197 if (dwarf2_section_empty_p (index))
12198 return NULL;
12199 dwarf2_read_section (objfile, index);
12200
12201 index_ptr = index->buffer;
12202 index_end = index_ptr + index->size;
12203
12204 version = read_4_bytes (dbfd, index_ptr);
12205 index_ptr += 4;
12206 if (version == 2)
12207 nr_columns = read_4_bytes (dbfd, index_ptr);
12208 else
12209 nr_columns = 0;
12210 index_ptr += 4;
12211 nr_units = read_4_bytes (dbfd, index_ptr);
12212 index_ptr += 4;
12213 nr_slots = read_4_bytes (dbfd, index_ptr);
12214 index_ptr += 4;
12215
12216 if (version != 1 && version != 2)
12217 {
12218 error (_("Dwarf Error: unsupported DWP file version (%s)"
12219 " [in module %s]"),
12220 pulongest (version), dwp_file->name);
12221 }
12222 if (nr_slots != (nr_slots & -nr_slots))
12223 {
12224 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
12225 " is not power of 2 [in module %s]"),
12226 pulongest (nr_slots), dwp_file->name);
12227 }
12228
12229 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
12230 htab->version = version;
12231 htab->nr_columns = nr_columns;
12232 htab->nr_units = nr_units;
12233 htab->nr_slots = nr_slots;
12234 htab->hash_table = index_ptr;
12235 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
12236
12237 /* Exit early if the table is empty. */
12238 if (nr_slots == 0 || nr_units == 0
12239 || (version == 2 && nr_columns == 0))
12240 {
12241 /* All must be zero. */
12242 if (nr_slots != 0 || nr_units != 0
12243 || (version == 2 && nr_columns != 0))
12244 {
12245 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
12246 " all zero [in modules %s]"),
12247 dwp_file->name);
12248 }
12249 return htab;
12250 }
12251
12252 if (version == 1)
12253 {
12254 htab->section_pool.v1.indices =
12255 htab->unit_table + sizeof (uint32_t) * nr_slots;
12256 /* It's harder to decide whether the section is too small in v1.
12257 V1 is deprecated anyway so we punt. */
12258 }
12259 else
12260 {
12261 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
12262 int *ids = htab->section_pool.v2.section_ids;
12263 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
12264 /* Reverse map for error checking. */
12265 int ids_seen[DW_SECT_MAX + 1];
12266 int i;
12267
12268 if (nr_columns < 2)
12269 {
12270 error (_("Dwarf Error: bad DWP hash table, too few columns"
12271 " in section table [in module %s]"),
12272 dwp_file->name);
12273 }
12274 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
12275 {
12276 error (_("Dwarf Error: bad DWP hash table, too many columns"
12277 " in section table [in module %s]"),
12278 dwp_file->name);
12279 }
12280 memset (ids, 255, sizeof_ids);
12281 memset (ids_seen, 255, sizeof (ids_seen));
12282 for (i = 0; i < nr_columns; ++i)
12283 {
12284 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
12285
12286 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
12287 {
12288 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
12289 " in section table [in module %s]"),
12290 id, dwp_file->name);
12291 }
12292 if (ids_seen[id] != -1)
12293 {
12294 error (_("Dwarf Error: bad DWP hash table, duplicate section"
12295 " id %d in section table [in module %s]"),
12296 id, dwp_file->name);
12297 }
12298 ids_seen[id] = i;
12299 ids[i] = id;
12300 }
12301 /* Must have exactly one info or types section. */
12302 if (((ids_seen[DW_SECT_INFO] != -1)
12303 + (ids_seen[DW_SECT_TYPES] != -1))
12304 != 1)
12305 {
12306 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
12307 " DWO info/types section [in module %s]"),
12308 dwp_file->name);
12309 }
12310 /* Must have an abbrev section. */
12311 if (ids_seen[DW_SECT_ABBREV] == -1)
12312 {
12313 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
12314 " section [in module %s]"),
12315 dwp_file->name);
12316 }
12317 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
12318 htab->section_pool.v2.sizes =
12319 htab->section_pool.v2.offsets + (sizeof (uint32_t)
12320 * nr_units * nr_columns);
12321 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
12322 * nr_units * nr_columns))
12323 > index_end)
12324 {
12325 error (_("Dwarf Error: DWP index section is corrupt (too small)"
12326 " [in module %s]"),
12327 dwp_file->name);
12328 }
12329 }
12330
12331 return htab;
12332 }
12333
12334 /* Update SECTIONS with the data from SECTP.
12335
12336 This function is like the other "locate" section routines that are
12337 passed to bfd_map_over_sections, but in this context the sections to
12338 read comes from the DWP V1 hash table, not the full ELF section table.
12339
12340 The result is non-zero for success, or zero if an error was found. */
12341
12342 static int
12343 locate_v1_virtual_dwo_sections (asection *sectp,
12344 struct virtual_v1_dwo_sections *sections)
12345 {
12346 const struct dwop_section_names *names = &dwop_section_names;
12347
12348 if (section_is_p (sectp->name, &names->abbrev_dwo))
12349 {
12350 /* There can be only one. */
12351 if (sections->abbrev.s.section != NULL)
12352 return 0;
12353 sections->abbrev.s.section = sectp;
12354 sections->abbrev.size = bfd_section_size (sectp);
12355 }
12356 else if (section_is_p (sectp->name, &names->info_dwo)
12357 || section_is_p (sectp->name, &names->types_dwo))
12358 {
12359 /* There can be only one. */
12360 if (sections->info_or_types.s.section != NULL)
12361 return 0;
12362 sections->info_or_types.s.section = sectp;
12363 sections->info_or_types.size = bfd_section_size (sectp);
12364 }
12365 else if (section_is_p (sectp->name, &names->line_dwo))
12366 {
12367 /* There can be only one. */
12368 if (sections->line.s.section != NULL)
12369 return 0;
12370 sections->line.s.section = sectp;
12371 sections->line.size = bfd_section_size (sectp);
12372 }
12373 else if (section_is_p (sectp->name, &names->loc_dwo))
12374 {
12375 /* There can be only one. */
12376 if (sections->loc.s.section != NULL)
12377 return 0;
12378 sections->loc.s.section = sectp;
12379 sections->loc.size = bfd_section_size (sectp);
12380 }
12381 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12382 {
12383 /* There can be only one. */
12384 if (sections->macinfo.s.section != NULL)
12385 return 0;
12386 sections->macinfo.s.section = sectp;
12387 sections->macinfo.size = bfd_section_size (sectp);
12388 }
12389 else if (section_is_p (sectp->name, &names->macro_dwo))
12390 {
12391 /* There can be only one. */
12392 if (sections->macro.s.section != NULL)
12393 return 0;
12394 sections->macro.s.section = sectp;
12395 sections->macro.size = bfd_section_size (sectp);
12396 }
12397 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12398 {
12399 /* There can be only one. */
12400 if (sections->str_offsets.s.section != NULL)
12401 return 0;
12402 sections->str_offsets.s.section = sectp;
12403 sections->str_offsets.size = bfd_section_size (sectp);
12404 }
12405 else
12406 {
12407 /* No other kind of section is valid. */
12408 return 0;
12409 }
12410
12411 return 1;
12412 }
12413
12414 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12415 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12416 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12417 This is for DWP version 1 files. */
12418
12419 static struct dwo_unit *
12420 create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12421 struct dwp_file *dwp_file,
12422 uint32_t unit_index,
12423 const char *comp_dir,
12424 ULONGEST signature, int is_debug_types)
12425 {
12426 struct objfile *objfile = dwarf2_per_objfile->objfile;
12427 const struct dwp_hash_table *dwp_htab =
12428 is_debug_types ? dwp_file->tus : dwp_file->cus;
12429 bfd *dbfd = dwp_file->dbfd.get ();
12430 const char *kind = is_debug_types ? "TU" : "CU";
12431 struct dwo_file *dwo_file;
12432 struct dwo_unit *dwo_unit;
12433 struct virtual_v1_dwo_sections sections;
12434 void **dwo_file_slot;
12435 int i;
12436
12437 gdb_assert (dwp_file->version == 1);
12438
12439 if (dwarf_read_debug)
12440 {
12441 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
12442 kind,
12443 pulongest (unit_index), hex_string (signature),
12444 dwp_file->name);
12445 }
12446
12447 /* Fetch the sections of this DWO unit.
12448 Put a limit on the number of sections we look for so that bad data
12449 doesn't cause us to loop forever. */
12450
12451 #define MAX_NR_V1_DWO_SECTIONS \
12452 (1 /* .debug_info or .debug_types */ \
12453 + 1 /* .debug_abbrev */ \
12454 + 1 /* .debug_line */ \
12455 + 1 /* .debug_loc */ \
12456 + 1 /* .debug_str_offsets */ \
12457 + 1 /* .debug_macro or .debug_macinfo */ \
12458 + 1 /* trailing zero */)
12459
12460 memset (&sections, 0, sizeof (sections));
12461
12462 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
12463 {
12464 asection *sectp;
12465 uint32_t section_nr =
12466 read_4_bytes (dbfd,
12467 dwp_htab->section_pool.v1.indices
12468 + (unit_index + i) * sizeof (uint32_t));
12469
12470 if (section_nr == 0)
12471 break;
12472 if (section_nr >= dwp_file->num_sections)
12473 {
12474 error (_("Dwarf Error: bad DWP hash table, section number too large"
12475 " [in module %s]"),
12476 dwp_file->name);
12477 }
12478
12479 sectp = dwp_file->elf_sections[section_nr];
12480 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
12481 {
12482 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12483 " [in module %s]"),
12484 dwp_file->name);
12485 }
12486 }
12487
12488 if (i < 2
12489 || dwarf2_section_empty_p (&sections.info_or_types)
12490 || dwarf2_section_empty_p (&sections.abbrev))
12491 {
12492 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12493 " [in module %s]"),
12494 dwp_file->name);
12495 }
12496 if (i == MAX_NR_V1_DWO_SECTIONS)
12497 {
12498 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12499 " [in module %s]"),
12500 dwp_file->name);
12501 }
12502
12503 /* It's easier for the rest of the code if we fake a struct dwo_file and
12504 have dwo_unit "live" in that. At least for now.
12505
12506 The DWP file can be made up of a random collection of CUs and TUs.
12507 However, for each CU + set of TUs that came from the same original DWO
12508 file, we can combine them back into a virtual DWO file to save space
12509 (fewer struct dwo_file objects to allocate). Remember that for really
12510 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12511
12512 std::string virtual_dwo_name =
12513 string_printf ("virtual-dwo/%d-%d-%d-%d",
12514 get_section_id (&sections.abbrev),
12515 get_section_id (&sections.line),
12516 get_section_id (&sections.loc),
12517 get_section_id (&sections.str_offsets));
12518 /* Can we use an existing virtual DWO file? */
12519 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12520 virtual_dwo_name.c_str (),
12521 comp_dir);
12522 /* Create one if necessary. */
12523 if (*dwo_file_slot == NULL)
12524 {
12525 if (dwarf_read_debug)
12526 {
12527 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12528 virtual_dwo_name.c_str ());
12529 }
12530 dwo_file = new struct dwo_file;
12531 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
12532 virtual_dwo_name);
12533 dwo_file->comp_dir = comp_dir;
12534 dwo_file->sections.abbrev = sections.abbrev;
12535 dwo_file->sections.line = sections.line;
12536 dwo_file->sections.loc = sections.loc;
12537 dwo_file->sections.macinfo = sections.macinfo;
12538 dwo_file->sections.macro = sections.macro;
12539 dwo_file->sections.str_offsets = sections.str_offsets;
12540 /* The "str" section is global to the entire DWP file. */
12541 dwo_file->sections.str = dwp_file->sections.str;
12542 /* The info or types section is assigned below to dwo_unit,
12543 there's no need to record it in dwo_file.
12544 Also, we can't simply record type sections in dwo_file because
12545 we record a pointer into the vector in dwo_unit. As we collect more
12546 types we'll grow the vector and eventually have to reallocate space
12547 for it, invalidating all copies of pointers into the previous
12548 contents. */
12549 *dwo_file_slot = dwo_file;
12550 }
12551 else
12552 {
12553 if (dwarf_read_debug)
12554 {
12555 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12556 virtual_dwo_name.c_str ());
12557 }
12558 dwo_file = (struct dwo_file *) *dwo_file_slot;
12559 }
12560
12561 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12562 dwo_unit->dwo_file = dwo_file;
12563 dwo_unit->signature = signature;
12564 dwo_unit->section =
12565 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
12566 *dwo_unit->section = sections.info_or_types;
12567 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12568
12569 return dwo_unit;
12570 }
12571
12572 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
12573 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
12574 piece within that section used by a TU/CU, return a virtual section
12575 of just that piece. */
12576
12577 static struct dwarf2_section_info
12578 create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
12579 struct dwarf2_section_info *section,
12580 bfd_size_type offset, bfd_size_type size)
12581 {
12582 struct dwarf2_section_info result;
12583 asection *sectp;
12584
12585 gdb_assert (section != NULL);
12586 gdb_assert (!section->is_virtual);
12587
12588 memset (&result, 0, sizeof (result));
12589 result.s.containing_section = section;
12590 result.is_virtual = true;
12591
12592 if (size == 0)
12593 return result;
12594
12595 sectp = get_section_bfd_section (section);
12596
12597 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12598 bounds of the real section. This is a pretty-rare event, so just
12599 flag an error (easier) instead of a warning and trying to cope. */
12600 if (sectp == NULL
12601 || offset + size > bfd_section_size (sectp))
12602 {
12603 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
12604 " in section %s [in module %s]"),
12605 sectp ? bfd_section_name (sectp) : "<unknown>",
12606 objfile_name (dwarf2_per_objfile->objfile));
12607 }
12608
12609 result.virtual_offset = offset;
12610 result.size = size;
12611 return result;
12612 }
12613
12614 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12615 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12616 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12617 This is for DWP version 2 files. */
12618
12619 static struct dwo_unit *
12620 create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12621 struct dwp_file *dwp_file,
12622 uint32_t unit_index,
12623 const char *comp_dir,
12624 ULONGEST signature, int is_debug_types)
12625 {
12626 struct objfile *objfile = dwarf2_per_objfile->objfile;
12627 const struct dwp_hash_table *dwp_htab =
12628 is_debug_types ? dwp_file->tus : dwp_file->cus;
12629 bfd *dbfd = dwp_file->dbfd.get ();
12630 const char *kind = is_debug_types ? "TU" : "CU";
12631 struct dwo_file *dwo_file;
12632 struct dwo_unit *dwo_unit;
12633 struct virtual_v2_dwo_sections sections;
12634 void **dwo_file_slot;
12635 int i;
12636
12637 gdb_assert (dwp_file->version == 2);
12638
12639 if (dwarf_read_debug)
12640 {
12641 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12642 kind,
12643 pulongest (unit_index), hex_string (signature),
12644 dwp_file->name);
12645 }
12646
12647 /* Fetch the section offsets of this DWO unit. */
12648
12649 memset (&sections, 0, sizeof (sections));
12650
12651 for (i = 0; i < dwp_htab->nr_columns; ++i)
12652 {
12653 uint32_t offset = read_4_bytes (dbfd,
12654 dwp_htab->section_pool.v2.offsets
12655 + (((unit_index - 1) * dwp_htab->nr_columns
12656 + i)
12657 * sizeof (uint32_t)));
12658 uint32_t size = read_4_bytes (dbfd,
12659 dwp_htab->section_pool.v2.sizes
12660 + (((unit_index - 1) * dwp_htab->nr_columns
12661 + i)
12662 * sizeof (uint32_t)));
12663
12664 switch (dwp_htab->section_pool.v2.section_ids[i])
12665 {
12666 case DW_SECT_INFO:
12667 case DW_SECT_TYPES:
12668 sections.info_or_types_offset = offset;
12669 sections.info_or_types_size = size;
12670 break;
12671 case DW_SECT_ABBREV:
12672 sections.abbrev_offset = offset;
12673 sections.abbrev_size = size;
12674 break;
12675 case DW_SECT_LINE:
12676 sections.line_offset = offset;
12677 sections.line_size = size;
12678 break;
12679 case DW_SECT_LOC:
12680 sections.loc_offset = offset;
12681 sections.loc_size = size;
12682 break;
12683 case DW_SECT_STR_OFFSETS:
12684 sections.str_offsets_offset = offset;
12685 sections.str_offsets_size = size;
12686 break;
12687 case DW_SECT_MACINFO:
12688 sections.macinfo_offset = offset;
12689 sections.macinfo_size = size;
12690 break;
12691 case DW_SECT_MACRO:
12692 sections.macro_offset = offset;
12693 sections.macro_size = size;
12694 break;
12695 }
12696 }
12697
12698 /* It's easier for the rest of the code if we fake a struct dwo_file and
12699 have dwo_unit "live" in that. At least for now.
12700
12701 The DWP file can be made up of a random collection of CUs and TUs.
12702 However, for each CU + set of TUs that came from the same original DWO
12703 file, we can combine them back into a virtual DWO file to save space
12704 (fewer struct dwo_file objects to allocate). Remember that for really
12705 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12706
12707 std::string virtual_dwo_name =
12708 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12709 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12710 (long) (sections.line_size ? sections.line_offset : 0),
12711 (long) (sections.loc_size ? sections.loc_offset : 0),
12712 (long) (sections.str_offsets_size
12713 ? sections.str_offsets_offset : 0));
12714 /* Can we use an existing virtual DWO file? */
12715 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12716 virtual_dwo_name.c_str (),
12717 comp_dir);
12718 /* Create one if necessary. */
12719 if (*dwo_file_slot == NULL)
12720 {
12721 if (dwarf_read_debug)
12722 {
12723 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12724 virtual_dwo_name.c_str ());
12725 }
12726 dwo_file = new struct dwo_file;
12727 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
12728 virtual_dwo_name);
12729 dwo_file->comp_dir = comp_dir;
12730 dwo_file->sections.abbrev =
12731 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
12732 sections.abbrev_offset, sections.abbrev_size);
12733 dwo_file->sections.line =
12734 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
12735 sections.line_offset, sections.line_size);
12736 dwo_file->sections.loc =
12737 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
12738 sections.loc_offset, sections.loc_size);
12739 dwo_file->sections.macinfo =
12740 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
12741 sections.macinfo_offset, sections.macinfo_size);
12742 dwo_file->sections.macro =
12743 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
12744 sections.macro_offset, sections.macro_size);
12745 dwo_file->sections.str_offsets =
12746 create_dwp_v2_section (dwarf2_per_objfile,
12747 &dwp_file->sections.str_offsets,
12748 sections.str_offsets_offset,
12749 sections.str_offsets_size);
12750 /* The "str" section is global to the entire DWP file. */
12751 dwo_file->sections.str = dwp_file->sections.str;
12752 /* The info or types section is assigned below to dwo_unit,
12753 there's no need to record it in dwo_file.
12754 Also, we can't simply record type sections in dwo_file because
12755 we record a pointer into the vector in dwo_unit. As we collect more
12756 types we'll grow the vector and eventually have to reallocate space
12757 for it, invalidating all copies of pointers into the previous
12758 contents. */
12759 *dwo_file_slot = dwo_file;
12760 }
12761 else
12762 {
12763 if (dwarf_read_debug)
12764 {
12765 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12766 virtual_dwo_name.c_str ());
12767 }
12768 dwo_file = (struct dwo_file *) *dwo_file_slot;
12769 }
12770
12771 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12772 dwo_unit->dwo_file = dwo_file;
12773 dwo_unit->signature = signature;
12774 dwo_unit->section =
12775 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
12776 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12777 is_debug_types
12778 ? &dwp_file->sections.types
12779 : &dwp_file->sections.info,
12780 sections.info_or_types_offset,
12781 sections.info_or_types_size);
12782 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12783
12784 return dwo_unit;
12785 }
12786
12787 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12788 Returns NULL if the signature isn't found. */
12789
12790 static struct dwo_unit *
12791 lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12792 struct dwp_file *dwp_file, const char *comp_dir,
12793 ULONGEST signature, int is_debug_types)
12794 {
12795 const struct dwp_hash_table *dwp_htab =
12796 is_debug_types ? dwp_file->tus : dwp_file->cus;
12797 bfd *dbfd = dwp_file->dbfd.get ();
12798 uint32_t mask = dwp_htab->nr_slots - 1;
12799 uint32_t hash = signature & mask;
12800 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12801 unsigned int i;
12802 void **slot;
12803 struct dwo_unit find_dwo_cu;
12804
12805 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12806 find_dwo_cu.signature = signature;
12807 slot = htab_find_slot (is_debug_types
12808 ? dwp_file->loaded_tus
12809 : dwp_file->loaded_cus,
12810 &find_dwo_cu, INSERT);
12811
12812 if (*slot != NULL)
12813 return (struct dwo_unit *) *slot;
12814
12815 /* Use a for loop so that we don't loop forever on bad debug info. */
12816 for (i = 0; i < dwp_htab->nr_slots; ++i)
12817 {
12818 ULONGEST signature_in_table;
12819
12820 signature_in_table =
12821 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
12822 if (signature_in_table == signature)
12823 {
12824 uint32_t unit_index =
12825 read_4_bytes (dbfd,
12826 dwp_htab->unit_table + hash * sizeof (uint32_t));
12827
12828 if (dwp_file->version == 1)
12829 {
12830 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12831 dwp_file, unit_index,
12832 comp_dir, signature,
12833 is_debug_types);
12834 }
12835 else
12836 {
12837 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12838 dwp_file, unit_index,
12839 comp_dir, signature,
12840 is_debug_types);
12841 }
12842 return (struct dwo_unit *) *slot;
12843 }
12844 if (signature_in_table == 0)
12845 return NULL;
12846 hash = (hash + hash2) & mask;
12847 }
12848
12849 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12850 " [in module %s]"),
12851 dwp_file->name);
12852 }
12853
12854 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12855 Open the file specified by FILE_NAME and hand it off to BFD for
12856 preliminary analysis. Return a newly initialized bfd *, which
12857 includes a canonicalized copy of FILE_NAME.
12858 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12859 SEARCH_CWD is true if the current directory is to be searched.
12860 It will be searched before debug-file-directory.
12861 If successful, the file is added to the bfd include table of the
12862 objfile's bfd (see gdb_bfd_record_inclusion).
12863 If unable to find/open the file, return NULL.
12864 NOTE: This function is derived from symfile_bfd_open. */
12865
12866 static gdb_bfd_ref_ptr
12867 try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12868 const char *file_name, int is_dwp, int search_cwd)
12869 {
12870 int desc;
12871 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12872 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12873 to debug_file_directory. */
12874 const char *search_path;
12875 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12876
12877 gdb::unique_xmalloc_ptr<char> search_path_holder;
12878 if (search_cwd)
12879 {
12880 if (*debug_file_directory != '\0')
12881 {
12882 search_path_holder.reset (concat (".", dirname_separator_string,
12883 debug_file_directory,
12884 (char *) NULL));
12885 search_path = search_path_holder.get ();
12886 }
12887 else
12888 search_path = ".";
12889 }
12890 else
12891 search_path = debug_file_directory;
12892
12893 openp_flags flags = OPF_RETURN_REALPATH;
12894 if (is_dwp)
12895 flags |= OPF_SEARCH_IN_PATH;
12896
12897 gdb::unique_xmalloc_ptr<char> absolute_name;
12898 desc = openp (search_path, flags, file_name,
12899 O_RDONLY | O_BINARY, &absolute_name);
12900 if (desc < 0)
12901 return NULL;
12902
12903 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12904 gnutarget, desc));
12905 if (sym_bfd == NULL)
12906 return NULL;
12907 bfd_set_cacheable (sym_bfd.get (), 1);
12908
12909 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12910 return NULL;
12911
12912 /* Success. Record the bfd as having been included by the objfile's bfd.
12913 This is important because things like demangled_names_hash lives in the
12914 objfile's per_bfd space and may have references to things like symbol
12915 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12916 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
12917
12918 return sym_bfd;
12919 }
12920
12921 /* Try to open DWO file FILE_NAME.
12922 COMP_DIR is the DW_AT_comp_dir attribute.
12923 The result is the bfd handle of the file.
12924 If there is a problem finding or opening the file, return NULL.
12925 Upon success, the canonicalized path of the file is stored in the bfd,
12926 same as symfile_bfd_open. */
12927
12928 static gdb_bfd_ref_ptr
12929 open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12930 const char *file_name, const char *comp_dir)
12931 {
12932 if (IS_ABSOLUTE_PATH (file_name))
12933 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12934 0 /*is_dwp*/, 0 /*search_cwd*/);
12935
12936 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12937
12938 if (comp_dir != NULL)
12939 {
12940 gdb::unique_xmalloc_ptr<char> path_to_try
12941 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
12942
12943 /* NOTE: If comp_dir is a relative path, this will also try the
12944 search path, which seems useful. */
12945 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
12946 path_to_try.get (),
12947 0 /*is_dwp*/,
12948 1 /*search_cwd*/));
12949 if (abfd != NULL)
12950 return abfd;
12951 }
12952
12953 /* That didn't work, try debug-file-directory, which, despite its name,
12954 is a list of paths. */
12955
12956 if (*debug_file_directory == '\0')
12957 return NULL;
12958
12959 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12960 0 /*is_dwp*/, 1 /*search_cwd*/);
12961 }
12962
12963 /* This function is mapped across the sections and remembers the offset and
12964 size of each of the DWO debugging sections we are interested in. */
12965
12966 static void
12967 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12968 {
12969 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
12970 const struct dwop_section_names *names = &dwop_section_names;
12971
12972 if (section_is_p (sectp->name, &names->abbrev_dwo))
12973 {
12974 dwo_sections->abbrev.s.section = sectp;
12975 dwo_sections->abbrev.size = bfd_section_size (sectp);
12976 }
12977 else if (section_is_p (sectp->name, &names->info_dwo))
12978 {
12979 dwo_sections->info.s.section = sectp;
12980 dwo_sections->info.size = bfd_section_size (sectp);
12981 }
12982 else if (section_is_p (sectp->name, &names->line_dwo))
12983 {
12984 dwo_sections->line.s.section = sectp;
12985 dwo_sections->line.size = bfd_section_size (sectp);
12986 }
12987 else if (section_is_p (sectp->name, &names->loc_dwo))
12988 {
12989 dwo_sections->loc.s.section = sectp;
12990 dwo_sections->loc.size = bfd_section_size (sectp);
12991 }
12992 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12993 {
12994 dwo_sections->macinfo.s.section = sectp;
12995 dwo_sections->macinfo.size = bfd_section_size (sectp);
12996 }
12997 else if (section_is_p (sectp->name, &names->macro_dwo))
12998 {
12999 dwo_sections->macro.s.section = sectp;
13000 dwo_sections->macro.size = bfd_section_size (sectp);
13001 }
13002 else if (section_is_p (sectp->name, &names->str_dwo))
13003 {
13004 dwo_sections->str.s.section = sectp;
13005 dwo_sections->str.size = bfd_section_size (sectp);
13006 }
13007 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13008 {
13009 dwo_sections->str_offsets.s.section = sectp;
13010 dwo_sections->str_offsets.size = bfd_section_size (sectp);
13011 }
13012 else if (section_is_p (sectp->name, &names->types_dwo))
13013 {
13014 struct dwarf2_section_info type_section;
13015
13016 memset (&type_section, 0, sizeof (type_section));
13017 type_section.s.section = sectp;
13018 type_section.size = bfd_section_size (sectp);
13019 dwo_sections->types.push_back (type_section);
13020 }
13021 }
13022
13023 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
13024 by PER_CU. This is for the non-DWP case.
13025 The result is NULL if DWO_NAME can't be found. */
13026
13027 static struct dwo_file *
13028 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
13029 const char *dwo_name, const char *comp_dir)
13030 {
13031 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
13032
13033 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
13034 if (dbfd == NULL)
13035 {
13036 if (dwarf_read_debug)
13037 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
13038 return NULL;
13039 }
13040
13041 dwo_file_up dwo_file (new struct dwo_file);
13042 dwo_file->dwo_name = dwo_name;
13043 dwo_file->comp_dir = comp_dir;
13044 dwo_file->dbfd = std::move (dbfd);
13045
13046 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
13047 &dwo_file->sections);
13048
13049 create_cus_hash_table (dwarf2_per_objfile, *dwo_file, dwo_file->sections.info,
13050 dwo_file->cus);
13051
13052 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
13053 dwo_file->sections.types, dwo_file->tus);
13054
13055 if (dwarf_read_debug)
13056 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
13057
13058 return dwo_file.release ();
13059 }
13060
13061 /* This function is mapped across the sections and remembers the offset and
13062 size of each of the DWP debugging sections common to version 1 and 2 that
13063 we are interested in. */
13064
13065 static void
13066 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
13067 void *dwp_file_ptr)
13068 {
13069 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
13070 const struct dwop_section_names *names = &dwop_section_names;
13071 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13072
13073 /* Record the ELF section number for later lookup: this is what the
13074 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13075 gdb_assert (elf_section_nr < dwp_file->num_sections);
13076 dwp_file->elf_sections[elf_section_nr] = sectp;
13077
13078 /* Look for specific sections that we need. */
13079 if (section_is_p (sectp->name, &names->str_dwo))
13080 {
13081 dwp_file->sections.str.s.section = sectp;
13082 dwp_file->sections.str.size = bfd_section_size (sectp);
13083 }
13084 else if (section_is_p (sectp->name, &names->cu_index))
13085 {
13086 dwp_file->sections.cu_index.s.section = sectp;
13087 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
13088 }
13089 else if (section_is_p (sectp->name, &names->tu_index))
13090 {
13091 dwp_file->sections.tu_index.s.section = sectp;
13092 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
13093 }
13094 }
13095
13096 /* This function is mapped across the sections and remembers the offset and
13097 size of each of the DWP version 2 debugging sections that we are interested
13098 in. This is split into a separate function because we don't know if we
13099 have version 1 or 2 until we parse the cu_index/tu_index sections. */
13100
13101 static void
13102 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
13103 {
13104 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
13105 const struct dwop_section_names *names = &dwop_section_names;
13106 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13107
13108 /* Record the ELF section number for later lookup: this is what the
13109 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13110 gdb_assert (elf_section_nr < dwp_file->num_sections);
13111 dwp_file->elf_sections[elf_section_nr] = sectp;
13112
13113 /* Look for specific sections that we need. */
13114 if (section_is_p (sectp->name, &names->abbrev_dwo))
13115 {
13116 dwp_file->sections.abbrev.s.section = sectp;
13117 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
13118 }
13119 else if (section_is_p (sectp->name, &names->info_dwo))
13120 {
13121 dwp_file->sections.info.s.section = sectp;
13122 dwp_file->sections.info.size = bfd_section_size (sectp);
13123 }
13124 else if (section_is_p (sectp->name, &names->line_dwo))
13125 {
13126 dwp_file->sections.line.s.section = sectp;
13127 dwp_file->sections.line.size = bfd_section_size (sectp);
13128 }
13129 else if (section_is_p (sectp->name, &names->loc_dwo))
13130 {
13131 dwp_file->sections.loc.s.section = sectp;
13132 dwp_file->sections.loc.size = bfd_section_size (sectp);
13133 }
13134 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13135 {
13136 dwp_file->sections.macinfo.s.section = sectp;
13137 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
13138 }
13139 else if (section_is_p (sectp->name, &names->macro_dwo))
13140 {
13141 dwp_file->sections.macro.s.section = sectp;
13142 dwp_file->sections.macro.size = bfd_section_size (sectp);
13143 }
13144 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13145 {
13146 dwp_file->sections.str_offsets.s.section = sectp;
13147 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
13148 }
13149 else if (section_is_p (sectp->name, &names->types_dwo))
13150 {
13151 dwp_file->sections.types.s.section = sectp;
13152 dwp_file->sections.types.size = bfd_section_size (sectp);
13153 }
13154 }
13155
13156 /* Hash function for dwp_file loaded CUs/TUs. */
13157
13158 static hashval_t
13159 hash_dwp_loaded_cutus (const void *item)
13160 {
13161 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
13162
13163 /* This drops the top 32 bits of the signature, but is ok for a hash. */
13164 return dwo_unit->signature;
13165 }
13166
13167 /* Equality function for dwp_file loaded CUs/TUs. */
13168
13169 static int
13170 eq_dwp_loaded_cutus (const void *a, const void *b)
13171 {
13172 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13173 const struct dwo_unit *dub = (const struct dwo_unit *) b;
13174
13175 return dua->signature == dub->signature;
13176 }
13177
13178 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
13179
13180 static htab_t
13181 allocate_dwp_loaded_cutus_table (struct objfile *objfile)
13182 {
13183 return htab_create_alloc_ex (3,
13184 hash_dwp_loaded_cutus,
13185 eq_dwp_loaded_cutus,
13186 NULL,
13187 &objfile->objfile_obstack,
13188 hashtab_obstack_allocate,
13189 dummy_obstack_deallocate);
13190 }
13191
13192 /* Try to open DWP file FILE_NAME.
13193 The result is the bfd handle of the file.
13194 If there is a problem finding or opening the file, return NULL.
13195 Upon success, the canonicalized path of the file is stored in the bfd,
13196 same as symfile_bfd_open. */
13197
13198 static gdb_bfd_ref_ptr
13199 open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
13200 const char *file_name)
13201 {
13202 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
13203 1 /*is_dwp*/,
13204 1 /*search_cwd*/));
13205 if (abfd != NULL)
13206 return abfd;
13207
13208 /* Work around upstream bug 15652.
13209 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13210 [Whether that's a "bug" is debatable, but it is getting in our way.]
13211 We have no real idea where the dwp file is, because gdb's realpath-ing
13212 of the executable's path may have discarded the needed info.
13213 [IWBN if the dwp file name was recorded in the executable, akin to
13214 .gnu_debuglink, but that doesn't exist yet.]
13215 Strip the directory from FILE_NAME and search again. */
13216 if (*debug_file_directory != '\0')
13217 {
13218 /* Don't implicitly search the current directory here.
13219 If the user wants to search "." to handle this case,
13220 it must be added to debug-file-directory. */
13221 return try_open_dwop_file (dwarf2_per_objfile,
13222 lbasename (file_name), 1 /*is_dwp*/,
13223 0 /*search_cwd*/);
13224 }
13225
13226 return NULL;
13227 }
13228
13229 /* Initialize the use of the DWP file for the current objfile.
13230 By convention the name of the DWP file is ${objfile}.dwp.
13231 The result is NULL if it can't be found. */
13232
13233 static std::unique_ptr<struct dwp_file>
13234 open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
13235 {
13236 struct objfile *objfile = dwarf2_per_objfile->objfile;
13237
13238 /* Try to find first .dwp for the binary file before any symbolic links
13239 resolving. */
13240
13241 /* If the objfile is a debug file, find the name of the real binary
13242 file and get the name of dwp file from there. */
13243 std::string dwp_name;
13244 if (objfile->separate_debug_objfile_backlink != NULL)
13245 {
13246 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13247 const char *backlink_basename = lbasename (backlink->original_name);
13248
13249 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
13250 }
13251 else
13252 dwp_name = objfile->original_name;
13253
13254 dwp_name += ".dwp";
13255
13256 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
13257 if (dbfd == NULL
13258 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13259 {
13260 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
13261 dwp_name = objfile_name (objfile);
13262 dwp_name += ".dwp";
13263 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
13264 }
13265
13266 if (dbfd == NULL)
13267 {
13268 if (dwarf_read_debug)
13269 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
13270 return std::unique_ptr<dwp_file> ();
13271 }
13272
13273 const char *name = bfd_get_filename (dbfd.get ());
13274 std::unique_ptr<struct dwp_file> dwp_file
13275 (new struct dwp_file (name, std::move (dbfd)));
13276
13277 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
13278 dwp_file->elf_sections =
13279 OBSTACK_CALLOC (&objfile->objfile_obstack,
13280 dwp_file->num_sections, asection *);
13281
13282 bfd_map_over_sections (dwp_file->dbfd.get (),
13283 dwarf2_locate_common_dwp_sections,
13284 dwp_file.get ());
13285
13286 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
13287 0);
13288
13289 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
13290 1);
13291
13292 /* The DWP file version is stored in the hash table. Oh well. */
13293 if (dwp_file->cus && dwp_file->tus
13294 && dwp_file->cus->version != dwp_file->tus->version)
13295 {
13296 /* Technically speaking, we should try to limp along, but this is
13297 pretty bizarre. We use pulongest here because that's the established
13298 portability solution (e.g, we cannot use %u for uint32_t). */
13299 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13300 " TU version %s [in DWP file %s]"),
13301 pulongest (dwp_file->cus->version),
13302 pulongest (dwp_file->tus->version), dwp_name.c_str ());
13303 }
13304
13305 if (dwp_file->cus)
13306 dwp_file->version = dwp_file->cus->version;
13307 else if (dwp_file->tus)
13308 dwp_file->version = dwp_file->tus->version;
13309 else
13310 dwp_file->version = 2;
13311
13312 if (dwp_file->version == 2)
13313 bfd_map_over_sections (dwp_file->dbfd.get (),
13314 dwarf2_locate_v2_dwp_sections,
13315 dwp_file.get ());
13316
13317 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
13318 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
13319
13320 if (dwarf_read_debug)
13321 {
13322 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
13323 fprintf_unfiltered (gdb_stdlog,
13324 " %s CUs, %s TUs\n",
13325 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13326 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
13327 }
13328
13329 return dwp_file;
13330 }
13331
13332 /* Wrapper around open_and_init_dwp_file, only open it once. */
13333
13334 static struct dwp_file *
13335 get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
13336 {
13337 if (! dwarf2_per_objfile->dwp_checked)
13338 {
13339 dwarf2_per_objfile->dwp_file
13340 = open_and_init_dwp_file (dwarf2_per_objfile);
13341 dwarf2_per_objfile->dwp_checked = 1;
13342 }
13343 return dwarf2_per_objfile->dwp_file.get ();
13344 }
13345
13346 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13347 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13348 or in the DWP file for the objfile, referenced by THIS_UNIT.
13349 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
13350 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13351
13352 This is called, for example, when wanting to read a variable with a
13353 complex location. Therefore we don't want to do file i/o for every call.
13354 Therefore we don't want to look for a DWO file on every call.
13355 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13356 then we check if we've already seen DWO_NAME, and only THEN do we check
13357 for a DWO file.
13358
13359 The result is a pointer to the dwo_unit object or NULL if we didn't find it
13360 (dwo_id mismatch or couldn't find the DWO/DWP file). */
13361
13362 static struct dwo_unit *
13363 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
13364 const char *dwo_name, const char *comp_dir,
13365 ULONGEST signature, int is_debug_types)
13366 {
13367 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
13368 struct objfile *objfile = dwarf2_per_objfile->objfile;
13369 const char *kind = is_debug_types ? "TU" : "CU";
13370 void **dwo_file_slot;
13371 struct dwo_file *dwo_file;
13372 struct dwp_file *dwp_file;
13373
13374 /* First see if there's a DWP file.
13375 If we have a DWP file but didn't find the DWO inside it, don't
13376 look for the original DWO file. It makes gdb behave differently
13377 depending on whether one is debugging in the build tree. */
13378
13379 dwp_file = get_dwp_file (dwarf2_per_objfile);
13380 if (dwp_file != NULL)
13381 {
13382 const struct dwp_hash_table *dwp_htab =
13383 is_debug_types ? dwp_file->tus : dwp_file->cus;
13384
13385 if (dwp_htab != NULL)
13386 {
13387 struct dwo_unit *dwo_cutu =
13388 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
13389 signature, is_debug_types);
13390
13391 if (dwo_cutu != NULL)
13392 {
13393 if (dwarf_read_debug)
13394 {
13395 fprintf_unfiltered (gdb_stdlog,
13396 "Virtual DWO %s %s found: @%s\n",
13397 kind, hex_string (signature),
13398 host_address_to_string (dwo_cutu));
13399 }
13400 return dwo_cutu;
13401 }
13402 }
13403 }
13404 else
13405 {
13406 /* No DWP file, look for the DWO file. */
13407
13408 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
13409 dwo_name, comp_dir);
13410 if (*dwo_file_slot == NULL)
13411 {
13412 /* Read in the file and build a table of the CUs/TUs it contains. */
13413 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
13414 }
13415 /* NOTE: This will be NULL if unable to open the file. */
13416 dwo_file = (struct dwo_file *) *dwo_file_slot;
13417
13418 if (dwo_file != NULL)
13419 {
13420 struct dwo_unit *dwo_cutu = NULL;
13421
13422 if (is_debug_types && dwo_file->tus)
13423 {
13424 struct dwo_unit find_dwo_cutu;
13425
13426 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13427 find_dwo_cutu.signature = signature;
13428 dwo_cutu
13429 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
13430 }
13431 else if (!is_debug_types && dwo_file->cus)
13432 {
13433 struct dwo_unit find_dwo_cutu;
13434
13435 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13436 find_dwo_cutu.signature = signature;
13437 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus,
13438 &find_dwo_cutu);
13439 }
13440
13441 if (dwo_cutu != NULL)
13442 {
13443 if (dwarf_read_debug)
13444 {
13445 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
13446 kind, dwo_name, hex_string (signature),
13447 host_address_to_string (dwo_cutu));
13448 }
13449 return dwo_cutu;
13450 }
13451 }
13452 }
13453
13454 /* We didn't find it. This could mean a dwo_id mismatch, or
13455 someone deleted the DWO/DWP file, or the search path isn't set up
13456 correctly to find the file. */
13457
13458 if (dwarf_read_debug)
13459 {
13460 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
13461 kind, dwo_name, hex_string (signature));
13462 }
13463
13464 /* This is a warning and not a complaint because it can be caused by
13465 pilot error (e.g., user accidentally deleting the DWO). */
13466 {
13467 /* Print the name of the DWP file if we looked there, helps the user
13468 better diagnose the problem. */
13469 std::string dwp_text;
13470
13471 if (dwp_file != NULL)
13472 dwp_text = string_printf (" [in DWP file %s]",
13473 lbasename (dwp_file->name));
13474
13475 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
13476 " [in module %s]"),
13477 kind, dwo_name, hex_string (signature),
13478 dwp_text.c_str (),
13479 this_unit->is_debug_types ? "TU" : "CU",
13480 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
13481 }
13482 return NULL;
13483 }
13484
13485 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13486 See lookup_dwo_cutu_unit for details. */
13487
13488 static struct dwo_unit *
13489 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
13490 const char *dwo_name, const char *comp_dir,
13491 ULONGEST signature)
13492 {
13493 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
13494 }
13495
13496 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13497 See lookup_dwo_cutu_unit for details. */
13498
13499 static struct dwo_unit *
13500 lookup_dwo_type_unit (struct signatured_type *this_tu,
13501 const char *dwo_name, const char *comp_dir)
13502 {
13503 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
13504 }
13505
13506 /* Traversal function for queue_and_load_all_dwo_tus. */
13507
13508 static int
13509 queue_and_load_dwo_tu (void **slot, void *info)
13510 {
13511 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13512 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
13513 ULONGEST signature = dwo_unit->signature;
13514 struct signatured_type *sig_type =
13515 lookup_dwo_signatured_type (per_cu->cu, signature);
13516
13517 if (sig_type != NULL)
13518 {
13519 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13520
13521 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13522 a real dependency of PER_CU on SIG_TYPE. That is detected later
13523 while processing PER_CU. */
13524 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
13525 load_full_type_unit (sig_cu);
13526 per_cu->imported_symtabs_push (sig_cu);
13527 }
13528
13529 return 1;
13530 }
13531
13532 /* Queue all TUs contained in the DWO of PER_CU to be read in.
13533 The DWO may have the only definition of the type, though it may not be
13534 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13535 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13536
13537 static void
13538 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
13539 {
13540 struct dwo_unit *dwo_unit;
13541 struct dwo_file *dwo_file;
13542
13543 gdb_assert (!per_cu->is_debug_types);
13544 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
13545 gdb_assert (per_cu->cu != NULL);
13546
13547 dwo_unit = per_cu->cu->dwo_unit;
13548 gdb_assert (dwo_unit != NULL);
13549
13550 dwo_file = dwo_unit->dwo_file;
13551 if (dwo_file->tus != NULL)
13552 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
13553 }
13554
13555 /* Read in various DIEs. */
13556
13557 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
13558 Inherit only the children of the DW_AT_abstract_origin DIE not being
13559 already referenced by DW_AT_abstract_origin from the children of the
13560 current DIE. */
13561
13562 static void
13563 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13564 {
13565 struct die_info *child_die;
13566 sect_offset *offsetp;
13567 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13568 struct die_info *origin_die;
13569 /* Iterator of the ORIGIN_DIE children. */
13570 struct die_info *origin_child_die;
13571 struct attribute *attr;
13572 struct dwarf2_cu *origin_cu;
13573 struct pending **origin_previous_list_in_scope;
13574
13575 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13576 if (!attr)
13577 return;
13578
13579 /* Note that following die references may follow to a die in a
13580 different cu. */
13581
13582 origin_cu = cu;
13583 origin_die = follow_die_ref (die, attr, &origin_cu);
13584
13585 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13586 symbols in. */
13587 origin_previous_list_in_scope = origin_cu->list_in_scope;
13588 origin_cu->list_in_scope = cu->list_in_scope;
13589
13590 if (die->tag != origin_die->tag
13591 && !(die->tag == DW_TAG_inlined_subroutine
13592 && origin_die->tag == DW_TAG_subprogram))
13593 complaint (_("DIE %s and its abstract origin %s have different tags"),
13594 sect_offset_str (die->sect_off),
13595 sect_offset_str (origin_die->sect_off));
13596
13597 std::vector<sect_offset> offsets;
13598
13599 for (child_die = die->child;
13600 child_die && child_die->tag;
13601 child_die = sibling_die (child_die))
13602 {
13603 struct die_info *child_origin_die;
13604 struct dwarf2_cu *child_origin_cu;
13605
13606 /* We are trying to process concrete instance entries:
13607 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
13608 it's not relevant to our analysis here. i.e. detecting DIEs that are
13609 present in the abstract instance but not referenced in the concrete
13610 one. */
13611 if (child_die->tag == DW_TAG_call_site
13612 || child_die->tag == DW_TAG_GNU_call_site)
13613 continue;
13614
13615 /* For each CHILD_DIE, find the corresponding child of
13616 ORIGIN_DIE. If there is more than one layer of
13617 DW_AT_abstract_origin, follow them all; there shouldn't be,
13618 but GCC versions at least through 4.4 generate this (GCC PR
13619 40573). */
13620 child_origin_die = child_die;
13621 child_origin_cu = cu;
13622 while (1)
13623 {
13624 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13625 child_origin_cu);
13626 if (attr == NULL)
13627 break;
13628 child_origin_die = follow_die_ref (child_origin_die, attr,
13629 &child_origin_cu);
13630 }
13631
13632 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13633 counterpart may exist. */
13634 if (child_origin_die != child_die)
13635 {
13636 if (child_die->tag != child_origin_die->tag
13637 && !(child_die->tag == DW_TAG_inlined_subroutine
13638 && child_origin_die->tag == DW_TAG_subprogram))
13639 complaint (_("Child DIE %s and its abstract origin %s have "
13640 "different tags"),
13641 sect_offset_str (child_die->sect_off),
13642 sect_offset_str (child_origin_die->sect_off));
13643 if (child_origin_die->parent != origin_die)
13644 complaint (_("Child DIE %s and its abstract origin %s have "
13645 "different parents"),
13646 sect_offset_str (child_die->sect_off),
13647 sect_offset_str (child_origin_die->sect_off));
13648 else
13649 offsets.push_back (child_origin_die->sect_off);
13650 }
13651 }
13652 std::sort (offsets.begin (), offsets.end ());
13653 sect_offset *offsets_end = offsets.data () + offsets.size ();
13654 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
13655 if (offsetp[-1] == *offsetp)
13656 complaint (_("Multiple children of DIE %s refer "
13657 "to DIE %s as their abstract origin"),
13658 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
13659
13660 offsetp = offsets.data ();
13661 origin_child_die = origin_die->child;
13662 while (origin_child_die && origin_child_die->tag)
13663 {
13664 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
13665 while (offsetp < offsets_end
13666 && *offsetp < origin_child_die->sect_off)
13667 offsetp++;
13668 if (offsetp >= offsets_end
13669 || *offsetp > origin_child_die->sect_off)
13670 {
13671 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13672 Check whether we're already processing ORIGIN_CHILD_DIE.
13673 This can happen with mutually referenced abstract_origins.
13674 PR 16581. */
13675 if (!origin_child_die->in_process)
13676 process_die (origin_child_die, origin_cu);
13677 }
13678 origin_child_die = sibling_die (origin_child_die);
13679 }
13680 origin_cu->list_in_scope = origin_previous_list_in_scope;
13681
13682 if (cu != origin_cu)
13683 compute_delayed_physnames (origin_cu);
13684 }
13685
13686 static void
13687 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
13688 {
13689 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13690 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13691 struct context_stack *newobj;
13692 CORE_ADDR lowpc;
13693 CORE_ADDR highpc;
13694 struct die_info *child_die;
13695 struct attribute *attr, *call_line, *call_file;
13696 const char *name;
13697 CORE_ADDR baseaddr;
13698 struct block *block;
13699 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13700 std::vector<struct symbol *> template_args;
13701 struct template_symbol *templ_func = NULL;
13702
13703 if (inlined_func)
13704 {
13705 /* If we do not have call site information, we can't show the
13706 caller of this inlined function. That's too confusing, so
13707 only use the scope for local variables. */
13708 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13709 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13710 if (call_line == NULL || call_file == NULL)
13711 {
13712 read_lexical_block_scope (die, cu);
13713 return;
13714 }
13715 }
13716
13717 baseaddr = objfile->section_offsets[SECT_OFF_TEXT (objfile)];
13718
13719 name = dwarf2_name (die, cu);
13720
13721 /* Ignore functions with missing or empty names. These are actually
13722 illegal according to the DWARF standard. */
13723 if (name == NULL)
13724 {
13725 complaint (_("missing name for subprogram DIE at %s"),
13726 sect_offset_str (die->sect_off));
13727 return;
13728 }
13729
13730 /* Ignore functions with missing or invalid low and high pc attributes. */
13731 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
13732 <= PC_BOUNDS_INVALID)
13733 {
13734 attr = dwarf2_attr (die, DW_AT_external, cu);
13735 if (!attr || !DW_UNSND (attr))
13736 complaint (_("cannot get low and high bounds "
13737 "for subprogram DIE at %s"),
13738 sect_offset_str (die->sect_off));
13739 return;
13740 }
13741
13742 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13743 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13744
13745 /* If we have any template arguments, then we must allocate a
13746 different sort of symbol. */
13747 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
13748 {
13749 if (child_die->tag == DW_TAG_template_type_param
13750 || child_die->tag == DW_TAG_template_value_param)
13751 {
13752 templ_func = allocate_template_symbol (objfile);
13753 templ_func->subclass = SYMBOL_TEMPLATE;
13754 break;
13755 }
13756 }
13757
13758 newobj = cu->get_builder ()->push_context (0, lowpc);
13759 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13760 (struct symbol *) templ_func);
13761
13762 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
13763 set_objfile_main_name (objfile, newobj->name->linkage_name (),
13764 cu->language);
13765
13766 /* If there is a location expression for DW_AT_frame_base, record
13767 it. */
13768 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
13769 if (attr != nullptr)
13770 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
13771
13772 /* If there is a location for the static link, record it. */
13773 newobj->static_link = NULL;
13774 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13775 if (attr != nullptr)
13776 {
13777 newobj->static_link
13778 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
13779 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
13780 dwarf2_per_cu_addr_type (cu->per_cu));
13781 }
13782
13783 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
13784
13785 if (die->child != NULL)
13786 {
13787 child_die = die->child;
13788 while (child_die && child_die->tag)
13789 {
13790 if (child_die->tag == DW_TAG_template_type_param
13791 || child_die->tag == DW_TAG_template_value_param)
13792 {
13793 struct symbol *arg = new_symbol (child_die, NULL, cu);
13794
13795 if (arg != NULL)
13796 template_args.push_back (arg);
13797 }
13798 else
13799 process_die (child_die, cu);
13800 child_die = sibling_die (child_die);
13801 }
13802 }
13803
13804 inherit_abstract_dies (die, cu);
13805
13806 /* If we have a DW_AT_specification, we might need to import using
13807 directives from the context of the specification DIE. See the
13808 comment in determine_prefix. */
13809 if (cu->language == language_cplus
13810 && dwarf2_attr (die, DW_AT_specification, cu))
13811 {
13812 struct dwarf2_cu *spec_cu = cu;
13813 struct die_info *spec_die = die_specification (die, &spec_cu);
13814
13815 while (spec_die)
13816 {
13817 child_die = spec_die->child;
13818 while (child_die && child_die->tag)
13819 {
13820 if (child_die->tag == DW_TAG_imported_module)
13821 process_die (child_die, spec_cu);
13822 child_die = sibling_die (child_die);
13823 }
13824
13825 /* In some cases, GCC generates specification DIEs that
13826 themselves contain DW_AT_specification attributes. */
13827 spec_die = die_specification (spec_die, &spec_cu);
13828 }
13829 }
13830
13831 struct context_stack cstk = cu->get_builder ()->pop_context ();
13832 /* Make a block for the local symbols within. */
13833 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
13834 cstk.static_link, lowpc, highpc);
13835
13836 /* For C++, set the block's scope. */
13837 if ((cu->language == language_cplus
13838 || cu->language == language_fortran
13839 || cu->language == language_d
13840 || cu->language == language_rust)
13841 && cu->processing_has_namespace_info)
13842 block_set_scope (block, determine_prefix (die, cu),
13843 &objfile->objfile_obstack);
13844
13845 /* If we have address ranges, record them. */
13846 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13847
13848 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
13849
13850 /* Attach template arguments to function. */
13851 if (!template_args.empty ())
13852 {
13853 gdb_assert (templ_func != NULL);
13854
13855 templ_func->n_template_arguments = template_args.size ();
13856 templ_func->template_arguments
13857 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13858 templ_func->n_template_arguments);
13859 memcpy (templ_func->template_arguments,
13860 template_args.data (),
13861 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13862
13863 /* Make sure that the symtab is set on the new symbols. Even
13864 though they don't appear in this symtab directly, other parts
13865 of gdb assume that symbols do, and this is reasonably
13866 true. */
13867 for (symbol *sym : template_args)
13868 symbol_set_symtab (sym, symbol_symtab (templ_func));
13869 }
13870
13871 /* In C++, we can have functions nested inside functions (e.g., when
13872 a function declares a class that has methods). This means that
13873 when we finish processing a function scope, we may need to go
13874 back to building a containing block's symbol lists. */
13875 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13876 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13877
13878 /* If we've finished processing a top-level function, subsequent
13879 symbols go in the file symbol list. */
13880 if (cu->get_builder ()->outermost_context_p ())
13881 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
13882 }
13883
13884 /* Process all the DIES contained within a lexical block scope. Start
13885 a new scope, process the dies, and then close the scope. */
13886
13887 static void
13888 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13889 {
13890 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13891 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13892 CORE_ADDR lowpc, highpc;
13893 struct die_info *child_die;
13894 CORE_ADDR baseaddr;
13895
13896 baseaddr = objfile->section_offsets[SECT_OFF_TEXT (objfile)];
13897
13898 /* Ignore blocks with missing or invalid low and high pc attributes. */
13899 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13900 as multiple lexical blocks? Handling children in a sane way would
13901 be nasty. Might be easier to properly extend generic blocks to
13902 describe ranges. */
13903 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13904 {
13905 case PC_BOUNDS_NOT_PRESENT:
13906 /* DW_TAG_lexical_block has no attributes, process its children as if
13907 there was no wrapping by that DW_TAG_lexical_block.
13908 GCC does no longer produces such DWARF since GCC r224161. */
13909 for (child_die = die->child;
13910 child_die != NULL && child_die->tag;
13911 child_die = sibling_die (child_die))
13912 process_die (child_die, cu);
13913 return;
13914 case PC_BOUNDS_INVALID:
13915 return;
13916 }
13917 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13918 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13919
13920 cu->get_builder ()->push_context (0, lowpc);
13921 if (die->child != NULL)
13922 {
13923 child_die = die->child;
13924 while (child_die && child_die->tag)
13925 {
13926 process_die (child_die, cu);
13927 child_die = sibling_die (child_die);
13928 }
13929 }
13930 inherit_abstract_dies (die, cu);
13931 struct context_stack cstk = cu->get_builder ()->pop_context ();
13932
13933 if (*cu->get_builder ()->get_local_symbols () != NULL
13934 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13935 {
13936 struct block *block
13937 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13938 cstk.start_addr, highpc);
13939
13940 /* Note that recording ranges after traversing children, as we
13941 do here, means that recording a parent's ranges entails
13942 walking across all its children's ranges as they appear in
13943 the address map, which is quadratic behavior.
13944
13945 It would be nicer to record the parent's ranges before
13946 traversing its children, simply overriding whatever you find
13947 there. But since we don't even decide whether to create a
13948 block until after we've traversed its children, that's hard
13949 to do. */
13950 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13951 }
13952 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13953 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13954 }
13955
13956 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13957
13958 static void
13959 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13960 {
13961 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13962 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13963 CORE_ADDR pc, baseaddr;
13964 struct attribute *attr;
13965 struct call_site *call_site, call_site_local;
13966 void **slot;
13967 int nparams;
13968 struct die_info *child_die;
13969
13970 baseaddr = objfile->section_offsets[SECT_OFF_TEXT (objfile)];
13971
13972 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13973 if (attr == NULL)
13974 {
13975 /* This was a pre-DWARF-5 GNU extension alias
13976 for DW_AT_call_return_pc. */
13977 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13978 }
13979 if (!attr)
13980 {
13981 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13982 "DIE %s [in module %s]"),
13983 sect_offset_str (die->sect_off), objfile_name (objfile));
13984 return;
13985 }
13986 pc = attr_value_as_address (attr) + baseaddr;
13987 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13988
13989 if (cu->call_site_htab == NULL)
13990 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13991 NULL, &objfile->objfile_obstack,
13992 hashtab_obstack_allocate, NULL);
13993 call_site_local.pc = pc;
13994 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13995 if (*slot != NULL)
13996 {
13997 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13998 "DIE %s [in module %s]"),
13999 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
14000 objfile_name (objfile));
14001 return;
14002 }
14003
14004 /* Count parameters at the caller. */
14005
14006 nparams = 0;
14007 for (child_die = die->child; child_die && child_die->tag;
14008 child_die = sibling_die (child_die))
14009 {
14010 if (child_die->tag != DW_TAG_call_site_parameter
14011 && child_die->tag != DW_TAG_GNU_call_site_parameter)
14012 {
14013 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
14014 "DW_TAG_call_site child DIE %s [in module %s]"),
14015 child_die->tag, sect_offset_str (child_die->sect_off),
14016 objfile_name (objfile));
14017 continue;
14018 }
14019
14020 nparams++;
14021 }
14022
14023 call_site
14024 = ((struct call_site *)
14025 obstack_alloc (&objfile->objfile_obstack,
14026 sizeof (*call_site)
14027 + (sizeof (*call_site->parameter) * (nparams - 1))));
14028 *slot = call_site;
14029 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
14030 call_site->pc = pc;
14031
14032 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
14033 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
14034 {
14035 struct die_info *func_die;
14036
14037 /* Skip also over DW_TAG_inlined_subroutine. */
14038 for (func_die = die->parent;
14039 func_die && func_die->tag != DW_TAG_subprogram
14040 && func_die->tag != DW_TAG_subroutine_type;
14041 func_die = func_die->parent);
14042
14043 /* DW_AT_call_all_calls is a superset
14044 of DW_AT_call_all_tail_calls. */
14045 if (func_die
14046 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
14047 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
14048 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
14049 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
14050 {
14051 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
14052 not complete. But keep CALL_SITE for look ups via call_site_htab,
14053 both the initial caller containing the real return address PC and
14054 the final callee containing the current PC of a chain of tail
14055 calls do not need to have the tail call list complete. But any
14056 function candidate for a virtual tail call frame searched via
14057 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
14058 determined unambiguously. */
14059 }
14060 else
14061 {
14062 struct type *func_type = NULL;
14063
14064 if (func_die)
14065 func_type = get_die_type (func_die, cu);
14066 if (func_type != NULL)
14067 {
14068 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
14069
14070 /* Enlist this call site to the function. */
14071 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
14072 TYPE_TAIL_CALL_LIST (func_type) = call_site;
14073 }
14074 else
14075 complaint (_("Cannot find function owning DW_TAG_call_site "
14076 "DIE %s [in module %s]"),
14077 sect_offset_str (die->sect_off), objfile_name (objfile));
14078 }
14079 }
14080
14081 attr = dwarf2_attr (die, DW_AT_call_target, cu);
14082 if (attr == NULL)
14083 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
14084 if (attr == NULL)
14085 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
14086 if (attr == NULL)
14087 {
14088 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
14089 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14090 }
14091 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
14092 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
14093 /* Keep NULL DWARF_BLOCK. */;
14094 else if (attr_form_is_block (attr))
14095 {
14096 struct dwarf2_locexpr_baton *dlbaton;
14097
14098 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
14099 dlbaton->data = DW_BLOCK (attr)->data;
14100 dlbaton->size = DW_BLOCK (attr)->size;
14101 dlbaton->per_cu = cu->per_cu;
14102
14103 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
14104 }
14105 else if (attr_form_is_ref (attr))
14106 {
14107 struct dwarf2_cu *target_cu = cu;
14108 struct die_info *target_die;
14109
14110 target_die = follow_die_ref (die, attr, &target_cu);
14111 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
14112 if (die_is_declaration (target_die, target_cu))
14113 {
14114 const char *target_physname;
14115
14116 /* Prefer the mangled name; otherwise compute the demangled one. */
14117 target_physname = dw2_linkage_name (target_die, target_cu);
14118 if (target_physname == NULL)
14119 target_physname = dwarf2_physname (NULL, target_die, target_cu);
14120 if (target_physname == NULL)
14121 complaint (_("DW_AT_call_target target DIE has invalid "
14122 "physname, for referencing DIE %s [in module %s]"),
14123 sect_offset_str (die->sect_off), objfile_name (objfile));
14124 else
14125 SET_FIELD_PHYSNAME (call_site->target, target_physname);
14126 }
14127 else
14128 {
14129 CORE_ADDR lowpc;
14130
14131 /* DW_AT_entry_pc should be preferred. */
14132 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
14133 <= PC_BOUNDS_INVALID)
14134 complaint (_("DW_AT_call_target target DIE has invalid "
14135 "low pc, for referencing DIE %s [in module %s]"),
14136 sect_offset_str (die->sect_off), objfile_name (objfile));
14137 else
14138 {
14139 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14140 SET_FIELD_PHYSADDR (call_site->target, lowpc);
14141 }
14142 }
14143 }
14144 else
14145 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
14146 "block nor reference, for DIE %s [in module %s]"),
14147 sect_offset_str (die->sect_off), objfile_name (objfile));
14148
14149 call_site->per_cu = cu->per_cu;
14150
14151 for (child_die = die->child;
14152 child_die && child_die->tag;
14153 child_die = sibling_die (child_die))
14154 {
14155 struct call_site_parameter *parameter;
14156 struct attribute *loc, *origin;
14157
14158 if (child_die->tag != DW_TAG_call_site_parameter
14159 && child_die->tag != DW_TAG_GNU_call_site_parameter)
14160 {
14161 /* Already printed the complaint above. */
14162 continue;
14163 }
14164
14165 gdb_assert (call_site->parameter_count < nparams);
14166 parameter = &call_site->parameter[call_site->parameter_count];
14167
14168 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14169 specifies DW_TAG_formal_parameter. Value of the data assumed for the
14170 register is contained in DW_AT_call_value. */
14171
14172 loc = dwarf2_attr (child_die, DW_AT_location, cu);
14173 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14174 if (origin == NULL)
14175 {
14176 /* This was a pre-DWARF-5 GNU extension alias
14177 for DW_AT_call_parameter. */
14178 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14179 }
14180 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
14181 {
14182 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
14183
14184 sect_offset sect_off
14185 = (sect_offset) dwarf2_get_ref_die_offset (origin);
14186 if (!offset_in_cu_p (&cu->header, sect_off))
14187 {
14188 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14189 binding can be done only inside one CU. Such referenced DIE
14190 therefore cannot be even moved to DW_TAG_partial_unit. */
14191 complaint (_("DW_AT_call_parameter offset is not in CU for "
14192 "DW_TAG_call_site child DIE %s [in module %s]"),
14193 sect_offset_str (child_die->sect_off),
14194 objfile_name (objfile));
14195 continue;
14196 }
14197 parameter->u.param_cu_off
14198 = (cu_offset) (sect_off - cu->header.sect_off);
14199 }
14200 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
14201 {
14202 complaint (_("No DW_FORM_block* DW_AT_location for "
14203 "DW_TAG_call_site child DIE %s [in module %s]"),
14204 sect_offset_str (child_die->sect_off), objfile_name (objfile));
14205 continue;
14206 }
14207 else
14208 {
14209 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
14210 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
14211 if (parameter->u.dwarf_reg != -1)
14212 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
14213 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
14214 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
14215 &parameter->u.fb_offset))
14216 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14217 else
14218 {
14219 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
14220 "for DW_FORM_block* DW_AT_location is supported for "
14221 "DW_TAG_call_site child DIE %s "
14222 "[in module %s]"),
14223 sect_offset_str (child_die->sect_off),
14224 objfile_name (objfile));
14225 continue;
14226 }
14227 }
14228
14229 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14230 if (attr == NULL)
14231 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
14232 if (!attr_form_is_block (attr))
14233 {
14234 complaint (_("No DW_FORM_block* DW_AT_call_value for "
14235 "DW_TAG_call_site child DIE %s [in module %s]"),
14236 sect_offset_str (child_die->sect_off),
14237 objfile_name (objfile));
14238 continue;
14239 }
14240 parameter->value = DW_BLOCK (attr)->data;
14241 parameter->value_size = DW_BLOCK (attr)->size;
14242
14243 /* Parameters are not pre-cleared by memset above. */
14244 parameter->data_value = NULL;
14245 parameter->data_value_size = 0;
14246 call_site->parameter_count++;
14247
14248 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14249 if (attr == NULL)
14250 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
14251 if (attr != nullptr)
14252 {
14253 if (!attr_form_is_block (attr))
14254 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
14255 "DW_TAG_call_site child DIE %s [in module %s]"),
14256 sect_offset_str (child_die->sect_off),
14257 objfile_name (objfile));
14258 else
14259 {
14260 parameter->data_value = DW_BLOCK (attr)->data;
14261 parameter->data_value_size = DW_BLOCK (attr)->size;
14262 }
14263 }
14264 }
14265 }
14266
14267 /* Helper function for read_variable. If DIE represents a virtual
14268 table, then return the type of the concrete object that is
14269 associated with the virtual table. Otherwise, return NULL. */
14270
14271 static struct type *
14272 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14273 {
14274 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14275 if (attr == NULL)
14276 return NULL;
14277
14278 /* Find the type DIE. */
14279 struct die_info *type_die = NULL;
14280 struct dwarf2_cu *type_cu = cu;
14281
14282 if (attr_form_is_ref (attr))
14283 type_die = follow_die_ref (die, attr, &type_cu);
14284 if (type_die == NULL)
14285 return NULL;
14286
14287 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14288 return NULL;
14289 return die_containing_type (type_die, type_cu);
14290 }
14291
14292 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14293
14294 static void
14295 read_variable (struct die_info *die, struct dwarf2_cu *cu)
14296 {
14297 struct rust_vtable_symbol *storage = NULL;
14298
14299 if (cu->language == language_rust)
14300 {
14301 struct type *containing_type = rust_containing_type (die, cu);
14302
14303 if (containing_type != NULL)
14304 {
14305 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14306
14307 storage = new (&objfile->objfile_obstack) rust_vtable_symbol ();
14308 initialize_objfile_symbol (storage);
14309 storage->concrete_type = containing_type;
14310 storage->subclass = SYMBOL_RUST_VTABLE;
14311 }
14312 }
14313
14314 struct symbol *res = new_symbol (die, NULL, cu, storage);
14315 struct attribute *abstract_origin
14316 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14317 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
14318 if (res == NULL && loc && abstract_origin)
14319 {
14320 /* We have a variable without a name, but with a location and an abstract
14321 origin. This may be a concrete instance of an abstract variable
14322 referenced from an DW_OP_GNU_variable_value, so save it to find it back
14323 later. */
14324 struct dwarf2_cu *origin_cu = cu;
14325 struct die_info *origin_die
14326 = follow_die_ref (die, abstract_origin, &origin_cu);
14327 dwarf2_per_objfile *dpo = cu->per_cu->dwarf2_per_objfile;
14328 dpo->abstract_to_concrete[origin_die->sect_off].push_back (die->sect_off);
14329 }
14330 }
14331
14332 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14333 reading .debug_rnglists.
14334 Callback's type should be:
14335 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14336 Return true if the attributes are present and valid, otherwise,
14337 return false. */
14338
14339 template <typename Callback>
14340 static bool
14341 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
14342 Callback &&callback)
14343 {
14344 struct dwarf2_per_objfile *dwarf2_per_objfile
14345 = cu->per_cu->dwarf2_per_objfile;
14346 struct objfile *objfile = dwarf2_per_objfile->objfile;
14347 bfd *obfd = objfile->obfd;
14348 /* Base address selection entry. */
14349 CORE_ADDR base;
14350 int found_base;
14351 const gdb_byte *buffer;
14352 CORE_ADDR baseaddr;
14353 bool overflow = false;
14354
14355 found_base = cu->base_known;
14356 base = cu->base_address;
14357
14358 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
14359 if (offset >= dwarf2_per_objfile->rnglists.size)
14360 {
14361 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
14362 offset);
14363 return false;
14364 }
14365 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
14366
14367 baseaddr = objfile->section_offsets[SECT_OFF_TEXT (objfile)];
14368
14369 while (1)
14370 {
14371 /* Initialize it due to a false compiler warning. */
14372 CORE_ADDR range_beginning = 0, range_end = 0;
14373 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
14374 + dwarf2_per_objfile->rnglists.size);
14375 unsigned int bytes_read;
14376
14377 if (buffer == buf_end)
14378 {
14379 overflow = true;
14380 break;
14381 }
14382 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14383 switch (rlet)
14384 {
14385 case DW_RLE_end_of_list:
14386 break;
14387 case DW_RLE_base_address:
14388 if (buffer + cu->header.addr_size > buf_end)
14389 {
14390 overflow = true;
14391 break;
14392 }
14393 base = read_address (obfd, buffer, cu, &bytes_read);
14394 found_base = 1;
14395 buffer += bytes_read;
14396 break;
14397 case DW_RLE_start_length:
14398 if (buffer + cu->header.addr_size > buf_end)
14399 {
14400 overflow = true;
14401 break;
14402 }
14403 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14404 buffer += bytes_read;
14405 range_end = (range_beginning
14406 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14407 buffer += bytes_read;
14408 if (buffer > buf_end)
14409 {
14410 overflow = true;
14411 break;
14412 }
14413 break;
14414 case DW_RLE_offset_pair:
14415 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14416 buffer += bytes_read;
14417 if (buffer > buf_end)
14418 {
14419 overflow = true;
14420 break;
14421 }
14422 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14423 buffer += bytes_read;
14424 if (buffer > buf_end)
14425 {
14426 overflow = true;
14427 break;
14428 }
14429 break;
14430 case DW_RLE_start_end:
14431 if (buffer + 2 * cu->header.addr_size > buf_end)
14432 {
14433 overflow = true;
14434 break;
14435 }
14436 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14437 buffer += bytes_read;
14438 range_end = read_address (obfd, buffer, cu, &bytes_read);
14439 buffer += bytes_read;
14440 break;
14441 default:
14442 complaint (_("Invalid .debug_rnglists data (no base address)"));
14443 return false;
14444 }
14445 if (rlet == DW_RLE_end_of_list || overflow)
14446 break;
14447 if (rlet == DW_RLE_base_address)
14448 continue;
14449
14450 if (!found_base)
14451 {
14452 /* We have no valid base address for the ranges
14453 data. */
14454 complaint (_("Invalid .debug_rnglists data (no base address)"));
14455 return false;
14456 }
14457
14458 if (range_beginning > range_end)
14459 {
14460 /* Inverted range entries are invalid. */
14461 complaint (_("Invalid .debug_rnglists data (inverted range)"));
14462 return false;
14463 }
14464
14465 /* Empty range entries have no effect. */
14466 if (range_beginning == range_end)
14467 continue;
14468
14469 range_beginning += base;
14470 range_end += base;
14471
14472 /* A not-uncommon case of bad debug info.
14473 Don't pollute the addrmap with bad data. */
14474 if (range_beginning + baseaddr == 0
14475 && !dwarf2_per_objfile->has_section_at_zero)
14476 {
14477 complaint (_(".debug_rnglists entry has start address of zero"
14478 " [in module %s]"), objfile_name (objfile));
14479 continue;
14480 }
14481
14482 callback (range_beginning, range_end);
14483 }
14484
14485 if (overflow)
14486 {
14487 complaint (_("Offset %d is not terminated "
14488 "for DW_AT_ranges attribute"),
14489 offset);
14490 return false;
14491 }
14492
14493 return true;
14494 }
14495
14496 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14497 Callback's type should be:
14498 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14499 Return 1 if the attributes are present and valid, otherwise, return 0. */
14500
14501 template <typename Callback>
14502 static int
14503 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
14504 Callback &&callback)
14505 {
14506 struct dwarf2_per_objfile *dwarf2_per_objfile
14507 = cu->per_cu->dwarf2_per_objfile;
14508 struct objfile *objfile = dwarf2_per_objfile->objfile;
14509 struct comp_unit_head *cu_header = &cu->header;
14510 bfd *obfd = objfile->obfd;
14511 unsigned int addr_size = cu_header->addr_size;
14512 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14513 /* Base address selection entry. */
14514 CORE_ADDR base;
14515 int found_base;
14516 unsigned int dummy;
14517 const gdb_byte *buffer;
14518 CORE_ADDR baseaddr;
14519
14520 if (cu_header->version >= 5)
14521 return dwarf2_rnglists_process (offset, cu, callback);
14522
14523 found_base = cu->base_known;
14524 base = cu->base_address;
14525
14526 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
14527 if (offset >= dwarf2_per_objfile->ranges.size)
14528 {
14529 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
14530 offset);
14531 return 0;
14532 }
14533 buffer = dwarf2_per_objfile->ranges.buffer + offset;
14534
14535 baseaddr = objfile->section_offsets[SECT_OFF_TEXT (objfile)];
14536
14537 while (1)
14538 {
14539 CORE_ADDR range_beginning, range_end;
14540
14541 range_beginning = read_address (obfd, buffer, cu, &dummy);
14542 buffer += addr_size;
14543 range_end = read_address (obfd, buffer, cu, &dummy);
14544 buffer += addr_size;
14545 offset += 2 * addr_size;
14546
14547 /* An end of list marker is a pair of zero addresses. */
14548 if (range_beginning == 0 && range_end == 0)
14549 /* Found the end of list entry. */
14550 break;
14551
14552 /* Each base address selection entry is a pair of 2 values.
14553 The first is the largest possible address, the second is
14554 the base address. Check for a base address here. */
14555 if ((range_beginning & mask) == mask)
14556 {
14557 /* If we found the largest possible address, then we already
14558 have the base address in range_end. */
14559 base = range_end;
14560 found_base = 1;
14561 continue;
14562 }
14563
14564 if (!found_base)
14565 {
14566 /* We have no valid base address for the ranges
14567 data. */
14568 complaint (_("Invalid .debug_ranges data (no base address)"));
14569 return 0;
14570 }
14571
14572 if (range_beginning > range_end)
14573 {
14574 /* Inverted range entries are invalid. */
14575 complaint (_("Invalid .debug_ranges data (inverted range)"));
14576 return 0;
14577 }
14578
14579 /* Empty range entries have no effect. */
14580 if (range_beginning == range_end)
14581 continue;
14582
14583 range_beginning += base;
14584 range_end += base;
14585
14586 /* A not-uncommon case of bad debug info.
14587 Don't pollute the addrmap with bad data. */
14588 if (range_beginning + baseaddr == 0
14589 && !dwarf2_per_objfile->has_section_at_zero)
14590 {
14591 complaint (_(".debug_ranges entry has start address of zero"
14592 " [in module %s]"), objfile_name (objfile));
14593 continue;
14594 }
14595
14596 callback (range_beginning, range_end);
14597 }
14598
14599 return 1;
14600 }
14601
14602 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14603 Return 1 if the attributes are present and valid, otherwise, return 0.
14604 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14605
14606 static int
14607 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14608 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14609 struct partial_symtab *ranges_pst)
14610 {
14611 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14612 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14613 const CORE_ADDR baseaddr = objfile->section_offsets[SECT_OFF_TEXT (objfile)];
14614 int low_set = 0;
14615 CORE_ADDR low = 0;
14616 CORE_ADDR high = 0;
14617 int retval;
14618
14619 retval = dwarf2_ranges_process (offset, cu,
14620 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14621 {
14622 if (ranges_pst != NULL)
14623 {
14624 CORE_ADDR lowpc;
14625 CORE_ADDR highpc;
14626
14627 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14628 range_beginning + baseaddr)
14629 - baseaddr);
14630 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14631 range_end + baseaddr)
14632 - baseaddr);
14633 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
14634 lowpc, highpc - 1, ranges_pst);
14635 }
14636
14637 /* FIXME: This is recording everything as a low-high
14638 segment of consecutive addresses. We should have a
14639 data structure for discontiguous block ranges
14640 instead. */
14641 if (! low_set)
14642 {
14643 low = range_beginning;
14644 high = range_end;
14645 low_set = 1;
14646 }
14647 else
14648 {
14649 if (range_beginning < low)
14650 low = range_beginning;
14651 if (range_end > high)
14652 high = range_end;
14653 }
14654 });
14655 if (!retval)
14656 return 0;
14657
14658 if (! low_set)
14659 /* If the first entry is an end-of-list marker, the range
14660 describes an empty scope, i.e. no instructions. */
14661 return 0;
14662
14663 if (low_return)
14664 *low_return = low;
14665 if (high_return)
14666 *high_return = high;
14667 return 1;
14668 }
14669
14670 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
14671 definition for the return value. *LOWPC and *HIGHPC are set iff
14672 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
14673
14674 static enum pc_bounds_kind
14675 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
14676 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14677 struct partial_symtab *pst)
14678 {
14679 struct dwarf2_per_objfile *dwarf2_per_objfile
14680 = cu->per_cu->dwarf2_per_objfile;
14681 struct attribute *attr;
14682 struct attribute *attr_high;
14683 CORE_ADDR low = 0;
14684 CORE_ADDR high = 0;
14685 enum pc_bounds_kind ret;
14686
14687 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14688 if (attr_high)
14689 {
14690 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14691 if (attr != nullptr)
14692 {
14693 low = attr_value_as_address (attr);
14694 high = attr_value_as_address (attr_high);
14695 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14696 high += low;
14697 }
14698 else
14699 /* Found high w/o low attribute. */
14700 return PC_BOUNDS_INVALID;
14701
14702 /* Found consecutive range of addresses. */
14703 ret = PC_BOUNDS_HIGH_LOW;
14704 }
14705 else
14706 {
14707 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14708 if (attr != NULL)
14709 {
14710 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14711 We take advantage of the fact that DW_AT_ranges does not appear
14712 in DW_TAG_compile_unit of DWO files. */
14713 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14714 unsigned int ranges_offset = (DW_UNSND (attr)
14715 + (need_ranges_base
14716 ? cu->ranges_base
14717 : 0));
14718
14719 /* Value of the DW_AT_ranges attribute is the offset in the
14720 .debug_ranges section. */
14721 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
14722 return PC_BOUNDS_INVALID;
14723 /* Found discontinuous range of addresses. */
14724 ret = PC_BOUNDS_RANGES;
14725 }
14726 else
14727 return PC_BOUNDS_NOT_PRESENT;
14728 }
14729
14730 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
14731 if (high <= low)
14732 return PC_BOUNDS_INVALID;
14733
14734 /* When using the GNU linker, .gnu.linkonce. sections are used to
14735 eliminate duplicate copies of functions and vtables and such.
14736 The linker will arbitrarily choose one and discard the others.
14737 The AT_*_pc values for such functions refer to local labels in
14738 these sections. If the section from that file was discarded, the
14739 labels are not in the output, so the relocs get a value of 0.
14740 If this is a discarded function, mark the pc bounds as invalid,
14741 so that GDB will ignore it. */
14742 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
14743 return PC_BOUNDS_INVALID;
14744
14745 *lowpc = low;
14746 if (highpc)
14747 *highpc = high;
14748 return ret;
14749 }
14750
14751 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
14752 its low and high PC addresses. Do nothing if these addresses could not
14753 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14754 and HIGHPC to the high address if greater than HIGHPC. */
14755
14756 static void
14757 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14758 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14759 struct dwarf2_cu *cu)
14760 {
14761 CORE_ADDR low, high;
14762 struct die_info *child = die->child;
14763
14764 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
14765 {
14766 *lowpc = std::min (*lowpc, low);
14767 *highpc = std::max (*highpc, high);
14768 }
14769
14770 /* If the language does not allow nested subprograms (either inside
14771 subprograms or lexical blocks), we're done. */
14772 if (cu->language != language_ada)
14773 return;
14774
14775 /* Check all the children of the given DIE. If it contains nested
14776 subprograms, then check their pc bounds. Likewise, we need to
14777 check lexical blocks as well, as they may also contain subprogram
14778 definitions. */
14779 while (child && child->tag)
14780 {
14781 if (child->tag == DW_TAG_subprogram
14782 || child->tag == DW_TAG_lexical_block)
14783 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14784 child = sibling_die (child);
14785 }
14786 }
14787
14788 /* Get the low and high pc's represented by the scope DIE, and store
14789 them in *LOWPC and *HIGHPC. If the correct values can't be
14790 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14791
14792 static void
14793 get_scope_pc_bounds (struct die_info *die,
14794 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14795 struct dwarf2_cu *cu)
14796 {
14797 CORE_ADDR best_low = (CORE_ADDR) -1;
14798 CORE_ADDR best_high = (CORE_ADDR) 0;
14799 CORE_ADDR current_low, current_high;
14800
14801 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
14802 >= PC_BOUNDS_RANGES)
14803 {
14804 best_low = current_low;
14805 best_high = current_high;
14806 }
14807 else
14808 {
14809 struct die_info *child = die->child;
14810
14811 while (child && child->tag)
14812 {
14813 switch (child->tag) {
14814 case DW_TAG_subprogram:
14815 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14816 break;
14817 case DW_TAG_namespace:
14818 case DW_TAG_module:
14819 /* FIXME: carlton/2004-01-16: Should we do this for
14820 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14821 that current GCC's always emit the DIEs corresponding
14822 to definitions of methods of classes as children of a
14823 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14824 the DIEs giving the declarations, which could be
14825 anywhere). But I don't see any reason why the
14826 standards says that they have to be there. */
14827 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14828
14829 if (current_low != ((CORE_ADDR) -1))
14830 {
14831 best_low = std::min (best_low, current_low);
14832 best_high = std::max (best_high, current_high);
14833 }
14834 break;
14835 default:
14836 /* Ignore. */
14837 break;
14838 }
14839
14840 child = sibling_die (child);
14841 }
14842 }
14843
14844 *lowpc = best_low;
14845 *highpc = best_high;
14846 }
14847
14848 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14849 in DIE. */
14850
14851 static void
14852 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14853 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14854 {
14855 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14856 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14857 struct attribute *attr;
14858 struct attribute *attr_high;
14859
14860 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14861 if (attr_high)
14862 {
14863 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14864 if (attr != nullptr)
14865 {
14866 CORE_ADDR low = attr_value_as_address (attr);
14867 CORE_ADDR high = attr_value_as_address (attr_high);
14868
14869 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14870 high += low;
14871
14872 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14873 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14874 cu->get_builder ()->record_block_range (block, low, high - 1);
14875 }
14876 }
14877
14878 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14879 if (attr != nullptr)
14880 {
14881 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14882 We take advantage of the fact that DW_AT_ranges does not appear
14883 in DW_TAG_compile_unit of DWO files. */
14884 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14885
14886 /* The value of the DW_AT_ranges attribute is the offset of the
14887 address range list in the .debug_ranges section. */
14888 unsigned long offset = (DW_UNSND (attr)
14889 + (need_ranges_base ? cu->ranges_base : 0));
14890
14891 std::vector<blockrange> blockvec;
14892 dwarf2_ranges_process (offset, cu,
14893 [&] (CORE_ADDR start, CORE_ADDR end)
14894 {
14895 start += baseaddr;
14896 end += baseaddr;
14897 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14898 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14899 cu->get_builder ()->record_block_range (block, start, end - 1);
14900 blockvec.emplace_back (start, end);
14901 });
14902
14903 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
14904 }
14905 }
14906
14907 /* Check whether the producer field indicates either of GCC < 4.6, or the
14908 Intel C/C++ compiler, and cache the result in CU. */
14909
14910 static void
14911 check_producer (struct dwarf2_cu *cu)
14912 {
14913 int major, minor;
14914
14915 if (cu->producer == NULL)
14916 {
14917 /* For unknown compilers expect their behavior is DWARF version
14918 compliant.
14919
14920 GCC started to support .debug_types sections by -gdwarf-4 since
14921 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14922 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14923 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14924 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14925 }
14926 else if (producer_is_gcc (cu->producer, &major, &minor))
14927 {
14928 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14929 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14930 }
14931 else if (producer_is_icc (cu->producer, &major, &minor))
14932 {
14933 cu->producer_is_icc = true;
14934 cu->producer_is_icc_lt_14 = major < 14;
14935 }
14936 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14937 cu->producer_is_codewarrior = true;
14938 else
14939 {
14940 /* For other non-GCC compilers, expect their behavior is DWARF version
14941 compliant. */
14942 }
14943
14944 cu->checked_producer = true;
14945 }
14946
14947 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14948 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14949 during 4.6.0 experimental. */
14950
14951 static bool
14952 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14953 {
14954 if (!cu->checked_producer)
14955 check_producer (cu);
14956
14957 return cu->producer_is_gxx_lt_4_6;
14958 }
14959
14960
14961 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14962 with incorrect is_stmt attributes. */
14963
14964 static bool
14965 producer_is_codewarrior (struct dwarf2_cu *cu)
14966 {
14967 if (!cu->checked_producer)
14968 check_producer (cu);
14969
14970 return cu->producer_is_codewarrior;
14971 }
14972
14973 /* Return the default accessibility type if it is not overridden by
14974 DW_AT_accessibility. */
14975
14976 static enum dwarf_access_attribute
14977 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14978 {
14979 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14980 {
14981 /* The default DWARF 2 accessibility for members is public, the default
14982 accessibility for inheritance is private. */
14983
14984 if (die->tag != DW_TAG_inheritance)
14985 return DW_ACCESS_public;
14986 else
14987 return DW_ACCESS_private;
14988 }
14989 else
14990 {
14991 /* DWARF 3+ defines the default accessibility a different way. The same
14992 rules apply now for DW_TAG_inheritance as for the members and it only
14993 depends on the container kind. */
14994
14995 if (die->parent->tag == DW_TAG_class_type)
14996 return DW_ACCESS_private;
14997 else
14998 return DW_ACCESS_public;
14999 }
15000 }
15001
15002 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
15003 offset. If the attribute was not found return 0, otherwise return
15004 1. If it was found but could not properly be handled, set *OFFSET
15005 to 0. */
15006
15007 static int
15008 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
15009 LONGEST *offset)
15010 {
15011 struct attribute *attr;
15012
15013 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
15014 if (attr != NULL)
15015 {
15016 *offset = 0;
15017
15018 /* Note that we do not check for a section offset first here.
15019 This is because DW_AT_data_member_location is new in DWARF 4,
15020 so if we see it, we can assume that a constant form is really
15021 a constant and not a section offset. */
15022 if (attr_form_is_constant (attr))
15023 *offset = dwarf2_get_attr_constant_value (attr, 0);
15024 else if (attr_form_is_section_offset (attr))
15025 dwarf2_complex_location_expr_complaint ();
15026 else if (attr_form_is_block (attr))
15027 *offset = decode_locdesc (DW_BLOCK (attr), cu);
15028 else
15029 dwarf2_complex_location_expr_complaint ();
15030
15031 return 1;
15032 }
15033
15034 return 0;
15035 }
15036
15037 /* Add an aggregate field to the field list. */
15038
15039 static void
15040 dwarf2_add_field (struct field_info *fip, struct die_info *die,
15041 struct dwarf2_cu *cu)
15042 {
15043 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15044 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15045 struct nextfield *new_field;
15046 struct attribute *attr;
15047 struct field *fp;
15048 const char *fieldname = "";
15049
15050 if (die->tag == DW_TAG_inheritance)
15051 {
15052 fip->baseclasses.emplace_back ();
15053 new_field = &fip->baseclasses.back ();
15054 }
15055 else
15056 {
15057 fip->fields.emplace_back ();
15058 new_field = &fip->fields.back ();
15059 }
15060
15061 fip->nfields++;
15062
15063 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15064 if (attr != nullptr)
15065 new_field->accessibility = DW_UNSND (attr);
15066 else
15067 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
15068 if (new_field->accessibility != DW_ACCESS_public)
15069 fip->non_public_fields = 1;
15070
15071 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15072 if (attr != nullptr)
15073 new_field->virtuality = DW_UNSND (attr);
15074 else
15075 new_field->virtuality = DW_VIRTUALITY_none;
15076
15077 fp = &new_field->field;
15078
15079 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
15080 {
15081 LONGEST offset;
15082
15083 /* Data member other than a C++ static data member. */
15084
15085 /* Get type of field. */
15086 fp->type = die_type (die, cu);
15087
15088 SET_FIELD_BITPOS (*fp, 0);
15089
15090 /* Get bit size of field (zero if none). */
15091 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
15092 if (attr != nullptr)
15093 {
15094 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
15095 }
15096 else
15097 {
15098 FIELD_BITSIZE (*fp) = 0;
15099 }
15100
15101 /* Get bit offset of field. */
15102 if (handle_data_member_location (die, cu, &offset))
15103 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
15104 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
15105 if (attr != nullptr)
15106 {
15107 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
15108 {
15109 /* For big endian bits, the DW_AT_bit_offset gives the
15110 additional bit offset from the MSB of the containing
15111 anonymous object to the MSB of the field. We don't
15112 have to do anything special since we don't need to
15113 know the size of the anonymous object. */
15114 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
15115 }
15116 else
15117 {
15118 /* For little endian bits, compute the bit offset to the
15119 MSB of the anonymous object, subtract off the number of
15120 bits from the MSB of the field to the MSB of the
15121 object, and then subtract off the number of bits of
15122 the field itself. The result is the bit offset of
15123 the LSB of the field. */
15124 int anonymous_size;
15125 int bit_offset = DW_UNSND (attr);
15126
15127 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15128 if (attr != nullptr)
15129 {
15130 /* The size of the anonymous object containing
15131 the bit field is explicit, so use the
15132 indicated size (in bytes). */
15133 anonymous_size = DW_UNSND (attr);
15134 }
15135 else
15136 {
15137 /* The size of the anonymous object containing
15138 the bit field must be inferred from the type
15139 attribute of the data member containing the
15140 bit field. */
15141 anonymous_size = TYPE_LENGTH (fp->type);
15142 }
15143 SET_FIELD_BITPOS (*fp,
15144 (FIELD_BITPOS (*fp)
15145 + anonymous_size * bits_per_byte
15146 - bit_offset - FIELD_BITSIZE (*fp)));
15147 }
15148 }
15149 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15150 if (attr != NULL)
15151 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15152 + dwarf2_get_attr_constant_value (attr, 0)));
15153
15154 /* Get name of field. */
15155 fieldname = dwarf2_name (die, cu);
15156 if (fieldname == NULL)
15157 fieldname = "";
15158
15159 /* The name is already allocated along with this objfile, so we don't
15160 need to duplicate it for the type. */
15161 fp->name = fieldname;
15162
15163 /* Change accessibility for artificial fields (e.g. virtual table
15164 pointer or virtual base class pointer) to private. */
15165 if (dwarf2_attr (die, DW_AT_artificial, cu))
15166 {
15167 FIELD_ARTIFICIAL (*fp) = 1;
15168 new_field->accessibility = DW_ACCESS_private;
15169 fip->non_public_fields = 1;
15170 }
15171 }
15172 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
15173 {
15174 /* C++ static member. */
15175
15176 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15177 is a declaration, but all versions of G++ as of this writing
15178 (so through at least 3.2.1) incorrectly generate
15179 DW_TAG_variable tags. */
15180
15181 const char *physname;
15182
15183 /* Get name of field. */
15184 fieldname = dwarf2_name (die, cu);
15185 if (fieldname == NULL)
15186 return;
15187
15188 attr = dwarf2_attr (die, DW_AT_const_value, cu);
15189 if (attr
15190 /* Only create a symbol if this is an external value.
15191 new_symbol checks this and puts the value in the global symbol
15192 table, which we want. If it is not external, new_symbol
15193 will try to put the value in cu->list_in_scope which is wrong. */
15194 && dwarf2_flag_true_p (die, DW_AT_external, cu))
15195 {
15196 /* A static const member, not much different than an enum as far as
15197 we're concerned, except that we can support more types. */
15198 new_symbol (die, NULL, cu);
15199 }
15200
15201 /* Get physical name. */
15202 physname = dwarf2_physname (fieldname, die, cu);
15203
15204 /* The name is already allocated along with this objfile, so we don't
15205 need to duplicate it for the type. */
15206 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
15207 FIELD_TYPE (*fp) = die_type (die, cu);
15208 FIELD_NAME (*fp) = fieldname;
15209 }
15210 else if (die->tag == DW_TAG_inheritance)
15211 {
15212 LONGEST offset;
15213
15214 /* C++ base class field. */
15215 if (handle_data_member_location (die, cu, &offset))
15216 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
15217 FIELD_BITSIZE (*fp) = 0;
15218 FIELD_TYPE (*fp) = die_type (die, cu);
15219 FIELD_NAME (*fp) = TYPE_NAME (fp->type);
15220 }
15221 else if (die->tag == DW_TAG_variant_part)
15222 {
15223 /* process_structure_scope will treat this DIE as a union. */
15224 process_structure_scope (die, cu);
15225
15226 /* The variant part is relative to the start of the enclosing
15227 structure. */
15228 SET_FIELD_BITPOS (*fp, 0);
15229 fp->type = get_die_type (die, cu);
15230 fp->artificial = 1;
15231 fp->name = "<<variant>>";
15232
15233 /* Normally a DW_TAG_variant_part won't have a size, but our
15234 representation requires one, so set it to the maximum of the
15235 child sizes, being sure to account for the offset at which
15236 each child is seen. */
15237 if (TYPE_LENGTH (fp->type) == 0)
15238 {
15239 unsigned max = 0;
15240 for (int i = 0; i < TYPE_NFIELDS (fp->type); ++i)
15241 {
15242 unsigned len = ((TYPE_FIELD_BITPOS (fp->type, i) + 7) / 8
15243 + TYPE_LENGTH (TYPE_FIELD_TYPE (fp->type, i)));
15244 if (len > max)
15245 max = len;
15246 }
15247 TYPE_LENGTH (fp->type) = max;
15248 }
15249 }
15250 else
15251 gdb_assert_not_reached ("missing case in dwarf2_add_field");
15252 }
15253
15254 /* Can the type given by DIE define another type? */
15255
15256 static bool
15257 type_can_define_types (const struct die_info *die)
15258 {
15259 switch (die->tag)
15260 {
15261 case DW_TAG_typedef:
15262 case DW_TAG_class_type:
15263 case DW_TAG_structure_type:
15264 case DW_TAG_union_type:
15265 case DW_TAG_enumeration_type:
15266 return true;
15267
15268 default:
15269 return false;
15270 }
15271 }
15272
15273 /* Add a type definition defined in the scope of the FIP's class. */
15274
15275 static void
15276 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15277 struct dwarf2_cu *cu)
15278 {
15279 struct decl_field fp;
15280 memset (&fp, 0, sizeof (fp));
15281
15282 gdb_assert (type_can_define_types (die));
15283
15284 /* Get name of field. NULL is okay here, meaning an anonymous type. */
15285 fp.name = dwarf2_name (die, cu);
15286 fp.type = read_type_die (die, cu);
15287
15288 /* Save accessibility. */
15289 enum dwarf_access_attribute accessibility;
15290 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15291 if (attr != NULL)
15292 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15293 else
15294 accessibility = dwarf2_default_access_attribute (die, cu);
15295 switch (accessibility)
15296 {
15297 case DW_ACCESS_public:
15298 /* The assumed value if neither private nor protected. */
15299 break;
15300 case DW_ACCESS_private:
15301 fp.is_private = 1;
15302 break;
15303 case DW_ACCESS_protected:
15304 fp.is_protected = 1;
15305 break;
15306 default:
15307 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
15308 }
15309
15310 if (die->tag == DW_TAG_typedef)
15311 fip->typedef_field_list.push_back (fp);
15312 else
15313 fip->nested_types_list.push_back (fp);
15314 }
15315
15316 /* Create the vector of fields, and attach it to the type. */
15317
15318 static void
15319 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
15320 struct dwarf2_cu *cu)
15321 {
15322 int nfields = fip->nfields;
15323
15324 /* Record the field count, allocate space for the array of fields,
15325 and create blank accessibility bitfields if necessary. */
15326 TYPE_NFIELDS (type) = nfields;
15327 TYPE_FIELDS (type) = (struct field *)
15328 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
15329
15330 if (fip->non_public_fields && cu->language != language_ada)
15331 {
15332 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15333
15334 TYPE_FIELD_PRIVATE_BITS (type) =
15335 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15336 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15337
15338 TYPE_FIELD_PROTECTED_BITS (type) =
15339 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15340 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15341
15342 TYPE_FIELD_IGNORE_BITS (type) =
15343 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15344 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
15345 }
15346
15347 /* If the type has baseclasses, allocate and clear a bit vector for
15348 TYPE_FIELD_VIRTUAL_BITS. */
15349 if (!fip->baseclasses.empty () && cu->language != language_ada)
15350 {
15351 int num_bytes = B_BYTES (fip->baseclasses.size ());
15352 unsigned char *pointer;
15353
15354 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15355 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
15356 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
15357 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15358 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
15359 }
15360
15361 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
15362 {
15363 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
15364
15365 for (int index = 0; index < nfields; ++index)
15366 {
15367 struct nextfield &field = fip->fields[index];
15368
15369 if (field.variant.is_discriminant)
15370 di->discriminant_index = index;
15371 else if (field.variant.default_branch)
15372 di->default_index = index;
15373 else
15374 di->discriminants[index] = field.variant.discriminant_value;
15375 }
15376 }
15377
15378 /* Copy the saved-up fields into the field vector. */
15379 for (int i = 0; i < nfields; ++i)
15380 {
15381 struct nextfield &field
15382 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15383 : fip->fields[i - fip->baseclasses.size ()]);
15384
15385 TYPE_FIELD (type, i) = field.field;
15386 switch (field.accessibility)
15387 {
15388 case DW_ACCESS_private:
15389 if (cu->language != language_ada)
15390 SET_TYPE_FIELD_PRIVATE (type, i);
15391 break;
15392
15393 case DW_ACCESS_protected:
15394 if (cu->language != language_ada)
15395 SET_TYPE_FIELD_PROTECTED (type, i);
15396 break;
15397
15398 case DW_ACCESS_public:
15399 break;
15400
15401 default:
15402 /* Unknown accessibility. Complain and treat it as public. */
15403 {
15404 complaint (_("unsupported accessibility %d"),
15405 field.accessibility);
15406 }
15407 break;
15408 }
15409 if (i < fip->baseclasses.size ())
15410 {
15411 switch (field.virtuality)
15412 {
15413 case DW_VIRTUALITY_virtual:
15414 case DW_VIRTUALITY_pure_virtual:
15415 if (cu->language == language_ada)
15416 error (_("unexpected virtuality in component of Ada type"));
15417 SET_TYPE_FIELD_VIRTUAL (type, i);
15418 break;
15419 }
15420 }
15421 }
15422 }
15423
15424 /* Return true if this member function is a constructor, false
15425 otherwise. */
15426
15427 static int
15428 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15429 {
15430 const char *fieldname;
15431 const char *type_name;
15432 int len;
15433
15434 if (die->parent == NULL)
15435 return 0;
15436
15437 if (die->parent->tag != DW_TAG_structure_type
15438 && die->parent->tag != DW_TAG_union_type
15439 && die->parent->tag != DW_TAG_class_type)
15440 return 0;
15441
15442 fieldname = dwarf2_name (die, cu);
15443 type_name = dwarf2_name (die->parent, cu);
15444 if (fieldname == NULL || type_name == NULL)
15445 return 0;
15446
15447 len = strlen (fieldname);
15448 return (strncmp (fieldname, type_name, len) == 0
15449 && (type_name[len] == '\0' || type_name[len] == '<'));
15450 }
15451
15452 /* Check if the given VALUE is a recognized enum
15453 dwarf_defaulted_attribute constant according to DWARF5 spec,
15454 Table 7.24. */
15455
15456 static bool
15457 is_valid_DW_AT_defaulted (ULONGEST value)
15458 {
15459 switch (value)
15460 {
15461 case DW_DEFAULTED_no:
15462 case DW_DEFAULTED_in_class:
15463 case DW_DEFAULTED_out_of_class:
15464 return true;
15465 }
15466
15467 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
15468 return false;
15469 }
15470
15471 /* Add a member function to the proper fieldlist. */
15472
15473 static void
15474 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
15475 struct type *type, struct dwarf2_cu *cu)
15476 {
15477 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15478 struct attribute *attr;
15479 int i;
15480 struct fnfieldlist *flp = nullptr;
15481 struct fn_field *fnp;
15482 const char *fieldname;
15483 struct type *this_type;
15484 enum dwarf_access_attribute accessibility;
15485
15486 if (cu->language == language_ada)
15487 error (_("unexpected member function in Ada type"));
15488
15489 /* Get name of member function. */
15490 fieldname = dwarf2_name (die, cu);
15491 if (fieldname == NULL)
15492 return;
15493
15494 /* Look up member function name in fieldlist. */
15495 for (i = 0; i < fip->fnfieldlists.size (); i++)
15496 {
15497 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
15498 {
15499 flp = &fip->fnfieldlists[i];
15500 break;
15501 }
15502 }
15503
15504 /* Create a new fnfieldlist if necessary. */
15505 if (flp == nullptr)
15506 {
15507 fip->fnfieldlists.emplace_back ();
15508 flp = &fip->fnfieldlists.back ();
15509 flp->name = fieldname;
15510 i = fip->fnfieldlists.size () - 1;
15511 }
15512
15513 /* Create a new member function field and add it to the vector of
15514 fnfieldlists. */
15515 flp->fnfields.emplace_back ();
15516 fnp = &flp->fnfields.back ();
15517
15518 /* Delay processing of the physname until later. */
15519 if (cu->language == language_cplus)
15520 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15521 die, cu);
15522 else
15523 {
15524 const char *physname = dwarf2_physname (fieldname, die, cu);
15525 fnp->physname = physname ? physname : "";
15526 }
15527
15528 fnp->type = alloc_type (objfile);
15529 this_type = read_type_die (die, cu);
15530 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
15531 {
15532 int nparams = TYPE_NFIELDS (this_type);
15533
15534 /* TYPE is the domain of this method, and THIS_TYPE is the type
15535 of the method itself (TYPE_CODE_METHOD). */
15536 smash_to_method_type (fnp->type, type,
15537 TYPE_TARGET_TYPE (this_type),
15538 TYPE_FIELDS (this_type),
15539 TYPE_NFIELDS (this_type),
15540 TYPE_VARARGS (this_type));
15541
15542 /* Handle static member functions.
15543 Dwarf2 has no clean way to discern C++ static and non-static
15544 member functions. G++ helps GDB by marking the first
15545 parameter for non-static member functions (which is the this
15546 pointer) as artificial. We obtain this information from
15547 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15548 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15549 fnp->voffset = VOFFSET_STATIC;
15550 }
15551 else
15552 complaint (_("member function type missing for '%s'"),
15553 dwarf2_full_name (fieldname, die, cu));
15554
15555 /* Get fcontext from DW_AT_containing_type if present. */
15556 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15557 fnp->fcontext = die_containing_type (die, cu);
15558
15559 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15560 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15561
15562 /* Get accessibility. */
15563 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15564 if (attr != nullptr)
15565 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15566 else
15567 accessibility = dwarf2_default_access_attribute (die, cu);
15568 switch (accessibility)
15569 {
15570 case DW_ACCESS_private:
15571 fnp->is_private = 1;
15572 break;
15573 case DW_ACCESS_protected:
15574 fnp->is_protected = 1;
15575 break;
15576 }
15577
15578 /* Check for artificial methods. */
15579 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15580 if (attr && DW_UNSND (attr) != 0)
15581 fnp->is_artificial = 1;
15582
15583 /* Check for defaulted methods. */
15584 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
15585 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
15586 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
15587
15588 /* Check for deleted methods. */
15589 attr = dwarf2_attr (die, DW_AT_deleted, cu);
15590 if (attr != nullptr && DW_UNSND (attr) != 0)
15591 fnp->is_deleted = 1;
15592
15593 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15594
15595 /* Get index in virtual function table if it is a virtual member
15596 function. For older versions of GCC, this is an offset in the
15597 appropriate virtual table, as specified by DW_AT_containing_type.
15598 For everyone else, it is an expression to be evaluated relative
15599 to the object address. */
15600
15601 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15602 if (attr != nullptr)
15603 {
15604 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
15605 {
15606 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15607 {
15608 /* Old-style GCC. */
15609 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15610 }
15611 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15612 || (DW_BLOCK (attr)->size > 1
15613 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15614 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15615 {
15616 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15617 if ((fnp->voffset % cu->header.addr_size) != 0)
15618 dwarf2_complex_location_expr_complaint ();
15619 else
15620 fnp->voffset /= cu->header.addr_size;
15621 fnp->voffset += 2;
15622 }
15623 else
15624 dwarf2_complex_location_expr_complaint ();
15625
15626 if (!fnp->fcontext)
15627 {
15628 /* If there is no `this' field and no DW_AT_containing_type,
15629 we cannot actually find a base class context for the
15630 vtable! */
15631 if (TYPE_NFIELDS (this_type) == 0
15632 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15633 {
15634 complaint (_("cannot determine context for virtual member "
15635 "function \"%s\" (offset %s)"),
15636 fieldname, sect_offset_str (die->sect_off));
15637 }
15638 else
15639 {
15640 fnp->fcontext
15641 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15642 }
15643 }
15644 }
15645 else if (attr_form_is_section_offset (attr))
15646 {
15647 dwarf2_complex_location_expr_complaint ();
15648 }
15649 else
15650 {
15651 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15652 fieldname);
15653 }
15654 }
15655 else
15656 {
15657 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15658 if (attr && DW_UNSND (attr))
15659 {
15660 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15661 complaint (_("Member function \"%s\" (offset %s) is virtual "
15662 "but the vtable offset is not specified"),
15663 fieldname, sect_offset_str (die->sect_off));
15664 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15665 TYPE_CPLUS_DYNAMIC (type) = 1;
15666 }
15667 }
15668 }
15669
15670 /* Create the vector of member function fields, and attach it to the type. */
15671
15672 static void
15673 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15674 struct dwarf2_cu *cu)
15675 {
15676 if (cu->language == language_ada)
15677 error (_("unexpected member functions in Ada type"));
15678
15679 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15680 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15681 TYPE_ALLOC (type,
15682 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
15683
15684 for (int i = 0; i < fip->fnfieldlists.size (); i++)
15685 {
15686 struct fnfieldlist &nf = fip->fnfieldlists[i];
15687 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15688
15689 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15690 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
15691 fn_flp->fn_fields = (struct fn_field *)
15692 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15693
15694 for (int k = 0; k < nf.fnfields.size (); ++k)
15695 fn_flp->fn_fields[k] = nf.fnfields[k];
15696 }
15697
15698 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
15699 }
15700
15701 /* Returns non-zero if NAME is the name of a vtable member in CU's
15702 language, zero otherwise. */
15703 static int
15704 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15705 {
15706 static const char vptr[] = "_vptr";
15707
15708 /* Look for the C++ form of the vtable. */
15709 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15710 return 1;
15711
15712 return 0;
15713 }
15714
15715 /* GCC outputs unnamed structures that are really pointers to member
15716 functions, with the ABI-specified layout. If TYPE describes
15717 such a structure, smash it into a member function type.
15718
15719 GCC shouldn't do this; it should just output pointer to member DIEs.
15720 This is GCC PR debug/28767. */
15721
15722 static void
15723 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15724 {
15725 struct type *pfn_type, *self_type, *new_type;
15726
15727 /* Check for a structure with no name and two children. */
15728 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
15729 return;
15730
15731 /* Check for __pfn and __delta members. */
15732 if (TYPE_FIELD_NAME (type, 0) == NULL
15733 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15734 || TYPE_FIELD_NAME (type, 1) == NULL
15735 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15736 return;
15737
15738 /* Find the type of the method. */
15739 pfn_type = TYPE_FIELD_TYPE (type, 0);
15740 if (pfn_type == NULL
15741 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
15742 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
15743 return;
15744
15745 /* Look for the "this" argument. */
15746 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15747 if (TYPE_NFIELDS (pfn_type) == 0
15748 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
15749 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
15750 return;
15751
15752 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
15753 new_type = alloc_type (objfile);
15754 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15755 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
15756 TYPE_VARARGS (pfn_type));
15757 smash_to_methodptr_type (type, new_type);
15758 }
15759
15760 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
15761 appropriate error checking and issuing complaints if there is a
15762 problem. */
15763
15764 static ULONGEST
15765 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15766 {
15767 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15768
15769 if (attr == nullptr)
15770 return 0;
15771
15772 if (!attr_form_is_constant (attr))
15773 {
15774 complaint (_("DW_AT_alignment must have constant form"
15775 " - DIE at %s [in module %s]"),
15776 sect_offset_str (die->sect_off),
15777 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15778 return 0;
15779 }
15780
15781 ULONGEST align;
15782 if (attr->form == DW_FORM_sdata)
15783 {
15784 LONGEST val = DW_SND (attr);
15785 if (val < 0)
15786 {
15787 complaint (_("DW_AT_alignment value must not be negative"
15788 " - DIE at %s [in module %s]"),
15789 sect_offset_str (die->sect_off),
15790 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15791 return 0;
15792 }
15793 align = val;
15794 }
15795 else
15796 align = DW_UNSND (attr);
15797
15798 if (align == 0)
15799 {
15800 complaint (_("DW_AT_alignment value must not be zero"
15801 " - DIE at %s [in module %s]"),
15802 sect_offset_str (die->sect_off),
15803 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15804 return 0;
15805 }
15806 if ((align & (align - 1)) != 0)
15807 {
15808 complaint (_("DW_AT_alignment value must be a power of 2"
15809 " - DIE at %s [in module %s]"),
15810 sect_offset_str (die->sect_off),
15811 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15812 return 0;
15813 }
15814
15815 return align;
15816 }
15817
15818 /* If the DIE has a DW_AT_alignment attribute, use its value to set
15819 the alignment for TYPE. */
15820
15821 static void
15822 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15823 struct type *type)
15824 {
15825 if (!set_type_align (type, get_alignment (cu, die)))
15826 complaint (_("DW_AT_alignment value too large"
15827 " - DIE at %s [in module %s]"),
15828 sect_offset_str (die->sect_off),
15829 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15830 }
15831
15832 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15833 constant for a type, according to DWARF5 spec, Table 5.5. */
15834
15835 static bool
15836 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15837 {
15838 switch (value)
15839 {
15840 case DW_CC_normal:
15841 case DW_CC_pass_by_reference:
15842 case DW_CC_pass_by_value:
15843 return true;
15844
15845 default:
15846 complaint (_("unrecognized DW_AT_calling_convention value "
15847 "(%s) for a type"), pulongest (value));
15848 return false;
15849 }
15850 }
15851
15852 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15853 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15854 also according to GNU-specific values (see include/dwarf2.h). */
15855
15856 static bool
15857 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15858 {
15859 switch (value)
15860 {
15861 case DW_CC_normal:
15862 case DW_CC_program:
15863 case DW_CC_nocall:
15864 return true;
15865
15866 case DW_CC_GNU_renesas_sh:
15867 case DW_CC_GNU_borland_fastcall_i386:
15868 case DW_CC_GDB_IBM_OpenCL:
15869 return true;
15870
15871 default:
15872 complaint (_("unrecognized DW_AT_calling_convention value "
15873 "(%s) for a subroutine"), pulongest (value));
15874 return false;
15875 }
15876 }
15877
15878 /* Called when we find the DIE that starts a structure or union scope
15879 (definition) to create a type for the structure or union. Fill in
15880 the type's name and general properties; the members will not be
15881 processed until process_structure_scope. A symbol table entry for
15882 the type will also not be done until process_structure_scope (assuming
15883 the type has a name).
15884
15885 NOTE: we need to call these functions regardless of whether or not the
15886 DIE has a DW_AT_name attribute, since it might be an anonymous
15887 structure or union. This gets the type entered into our set of
15888 user defined types. */
15889
15890 static struct type *
15891 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15892 {
15893 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15894 struct type *type;
15895 struct attribute *attr;
15896 const char *name;
15897
15898 /* If the definition of this type lives in .debug_types, read that type.
15899 Don't follow DW_AT_specification though, that will take us back up
15900 the chain and we want to go down. */
15901 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
15902 if (attr != nullptr)
15903 {
15904 type = get_DW_AT_signature_type (die, attr, cu);
15905
15906 /* The type's CU may not be the same as CU.
15907 Ensure TYPE is recorded with CU in die_type_hash. */
15908 return set_die_type (die, type, cu);
15909 }
15910
15911 type = alloc_type (objfile);
15912 INIT_CPLUS_SPECIFIC (type);
15913
15914 name = dwarf2_name (die, cu);
15915 if (name != NULL)
15916 {
15917 if (cu->language == language_cplus
15918 || cu->language == language_d
15919 || cu->language == language_rust)
15920 {
15921 const char *full_name = dwarf2_full_name (name, die, cu);
15922
15923 /* dwarf2_full_name might have already finished building the DIE's
15924 type. If so, there is no need to continue. */
15925 if (get_die_type (die, cu) != NULL)
15926 return get_die_type (die, cu);
15927
15928 TYPE_NAME (type) = full_name;
15929 }
15930 else
15931 {
15932 /* The name is already allocated along with this objfile, so
15933 we don't need to duplicate it for the type. */
15934 TYPE_NAME (type) = name;
15935 }
15936 }
15937
15938 if (die->tag == DW_TAG_structure_type)
15939 {
15940 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15941 }
15942 else if (die->tag == DW_TAG_union_type)
15943 {
15944 TYPE_CODE (type) = TYPE_CODE_UNION;
15945 }
15946 else if (die->tag == DW_TAG_variant_part)
15947 {
15948 TYPE_CODE (type) = TYPE_CODE_UNION;
15949 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
15950 }
15951 else
15952 {
15953 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15954 }
15955
15956 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15957 TYPE_DECLARED_CLASS (type) = 1;
15958
15959 /* Store the calling convention in the type if it's available in
15960 the die. Otherwise the calling convention remains set to
15961 the default value DW_CC_normal. */
15962 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15963 if (attr != nullptr
15964 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15965 {
15966 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15967 TYPE_CPLUS_CALLING_CONVENTION (type)
15968 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15969 }
15970
15971 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15972 if (attr != nullptr)
15973 {
15974 if (attr_form_is_constant (attr))
15975 TYPE_LENGTH (type) = DW_UNSND (attr);
15976 else
15977 {
15978 /* For the moment, dynamic type sizes are not supported
15979 by GDB's struct type. The actual size is determined
15980 on-demand when resolving the type of a given object,
15981 so set the type's length to zero for now. Otherwise,
15982 we record an expression as the length, and that expression
15983 could lead to a very large value, which could eventually
15984 lead to us trying to allocate that much memory when creating
15985 a value of that type. */
15986 TYPE_LENGTH (type) = 0;
15987 }
15988 }
15989 else
15990 {
15991 TYPE_LENGTH (type) = 0;
15992 }
15993
15994 maybe_set_alignment (cu, die, type);
15995
15996 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15997 {
15998 /* ICC<14 does not output the required DW_AT_declaration on
15999 incomplete types, but gives them a size of zero. */
16000 TYPE_STUB (type) = 1;
16001 }
16002 else
16003 TYPE_STUB_SUPPORTED (type) = 1;
16004
16005 if (die_is_declaration (die, cu))
16006 TYPE_STUB (type) = 1;
16007 else if (attr == NULL && die->child == NULL
16008 && producer_is_realview (cu->producer))
16009 /* RealView does not output the required DW_AT_declaration
16010 on incomplete types. */
16011 TYPE_STUB (type) = 1;
16012
16013 /* We need to add the type field to the die immediately so we don't
16014 infinitely recurse when dealing with pointers to the structure
16015 type within the structure itself. */
16016 set_die_type (die, type, cu);
16017
16018 /* set_die_type should be already done. */
16019 set_descriptive_type (type, die, cu);
16020
16021 return type;
16022 }
16023
16024 /* A helper for process_structure_scope that handles a single member
16025 DIE. */
16026
16027 static void
16028 handle_struct_member_die (struct die_info *child_die, struct type *type,
16029 struct field_info *fi,
16030 std::vector<struct symbol *> *template_args,
16031 struct dwarf2_cu *cu)
16032 {
16033 if (child_die->tag == DW_TAG_member
16034 || child_die->tag == DW_TAG_variable
16035 || child_die->tag == DW_TAG_variant_part)
16036 {
16037 /* NOTE: carlton/2002-11-05: A C++ static data member
16038 should be a DW_TAG_member that is a declaration, but
16039 all versions of G++ as of this writing (so through at
16040 least 3.2.1) incorrectly generate DW_TAG_variable
16041 tags for them instead. */
16042 dwarf2_add_field (fi, child_die, cu);
16043 }
16044 else if (child_die->tag == DW_TAG_subprogram)
16045 {
16046 /* Rust doesn't have member functions in the C++ sense.
16047 However, it does emit ordinary functions as children
16048 of a struct DIE. */
16049 if (cu->language == language_rust)
16050 read_func_scope (child_die, cu);
16051 else
16052 {
16053 /* C++ member function. */
16054 dwarf2_add_member_fn (fi, child_die, type, cu);
16055 }
16056 }
16057 else if (child_die->tag == DW_TAG_inheritance)
16058 {
16059 /* C++ base class field. */
16060 dwarf2_add_field (fi, child_die, cu);
16061 }
16062 else if (type_can_define_types (child_die))
16063 dwarf2_add_type_defn (fi, child_die, cu);
16064 else if (child_die->tag == DW_TAG_template_type_param
16065 || child_die->tag == DW_TAG_template_value_param)
16066 {
16067 struct symbol *arg = new_symbol (child_die, NULL, cu);
16068
16069 if (arg != NULL)
16070 template_args->push_back (arg);
16071 }
16072 else if (child_die->tag == DW_TAG_variant)
16073 {
16074 /* In a variant we want to get the discriminant and also add a
16075 field for our sole member child. */
16076 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
16077
16078 for (die_info *variant_child = child_die->child;
16079 variant_child != NULL;
16080 variant_child = sibling_die (variant_child))
16081 {
16082 if (variant_child->tag == DW_TAG_member)
16083 {
16084 handle_struct_member_die (variant_child, type, fi,
16085 template_args, cu);
16086 /* Only handle the one. */
16087 break;
16088 }
16089 }
16090
16091 /* We don't handle this but we might as well report it if we see
16092 it. */
16093 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
16094 complaint (_("DW_AT_discr_list is not supported yet"
16095 " - DIE at %s [in module %s]"),
16096 sect_offset_str (child_die->sect_off),
16097 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16098
16099 /* The first field was just added, so we can stash the
16100 discriminant there. */
16101 gdb_assert (!fi->fields.empty ());
16102 if (discr == NULL)
16103 fi->fields.back ().variant.default_branch = true;
16104 else
16105 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
16106 }
16107 }
16108
16109 /* Finish creating a structure or union type, including filling in
16110 its members and creating a symbol for it. */
16111
16112 static void
16113 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
16114 {
16115 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16116 struct die_info *child_die;
16117 struct type *type;
16118
16119 type = get_die_type (die, cu);
16120 if (type == NULL)
16121 type = read_structure_type (die, cu);
16122
16123 /* When reading a DW_TAG_variant_part, we need to notice when we
16124 read the discriminant member, so we can record it later in the
16125 discriminant_info. */
16126 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
16127 sect_offset discr_offset {};
16128 bool has_template_parameters = false;
16129
16130 if (is_variant_part)
16131 {
16132 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
16133 if (discr == NULL)
16134 {
16135 /* Maybe it's a univariant form, an extension we support.
16136 In this case arrange not to check the offset. */
16137 is_variant_part = false;
16138 }
16139 else if (attr_form_is_ref (discr))
16140 {
16141 struct dwarf2_cu *target_cu = cu;
16142 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
16143
16144 discr_offset = target_die->sect_off;
16145 }
16146 else
16147 {
16148 complaint (_("DW_AT_discr does not have DIE reference form"
16149 " - DIE at %s [in module %s]"),
16150 sect_offset_str (die->sect_off),
16151 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16152 is_variant_part = false;
16153 }
16154 }
16155
16156 if (die->child != NULL && ! die_is_declaration (die, cu))
16157 {
16158 struct field_info fi;
16159 std::vector<struct symbol *> template_args;
16160
16161 child_die = die->child;
16162
16163 while (child_die && child_die->tag)
16164 {
16165 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
16166
16167 if (is_variant_part && discr_offset == child_die->sect_off)
16168 fi.fields.back ().variant.is_discriminant = true;
16169
16170 child_die = sibling_die (child_die);
16171 }
16172
16173 /* Attach template arguments to type. */
16174 if (!template_args.empty ())
16175 {
16176 has_template_parameters = true;
16177 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16178 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
16179 TYPE_TEMPLATE_ARGUMENTS (type)
16180 = XOBNEWVEC (&objfile->objfile_obstack,
16181 struct symbol *,
16182 TYPE_N_TEMPLATE_ARGUMENTS (type));
16183 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
16184 template_args.data (),
16185 (TYPE_N_TEMPLATE_ARGUMENTS (type)
16186 * sizeof (struct symbol *)));
16187 }
16188
16189 /* Attach fields and member functions to the type. */
16190 if (fi.nfields)
16191 dwarf2_attach_fields_to_type (&fi, type, cu);
16192 if (!fi.fnfieldlists.empty ())
16193 {
16194 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
16195
16196 /* Get the type which refers to the base class (possibly this
16197 class itself) which contains the vtable pointer for the current
16198 class from the DW_AT_containing_type attribute. This use of
16199 DW_AT_containing_type is a GNU extension. */
16200
16201 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
16202 {
16203 struct type *t = die_containing_type (die, cu);
16204
16205 set_type_vptr_basetype (type, t);
16206 if (type == t)
16207 {
16208 int i;
16209
16210 /* Our own class provides vtbl ptr. */
16211 for (i = TYPE_NFIELDS (t) - 1;
16212 i >= TYPE_N_BASECLASSES (t);
16213 --i)
16214 {
16215 const char *fieldname = TYPE_FIELD_NAME (t, i);
16216
16217 if (is_vtable_name (fieldname, cu))
16218 {
16219 set_type_vptr_fieldno (type, i);
16220 break;
16221 }
16222 }
16223
16224 /* Complain if virtual function table field not found. */
16225 if (i < TYPE_N_BASECLASSES (t))
16226 complaint (_("virtual function table pointer "
16227 "not found when defining class '%s'"),
16228 TYPE_NAME (type) ? TYPE_NAME (type) : "");
16229 }
16230 else
16231 {
16232 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
16233 }
16234 }
16235 else if (cu->producer
16236 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
16237 {
16238 /* The IBM XLC compiler does not provide direct indication
16239 of the containing type, but the vtable pointer is
16240 always named __vfp. */
16241
16242 int i;
16243
16244 for (i = TYPE_NFIELDS (type) - 1;
16245 i >= TYPE_N_BASECLASSES (type);
16246 --i)
16247 {
16248 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16249 {
16250 set_type_vptr_fieldno (type, i);
16251 set_type_vptr_basetype (type, type);
16252 break;
16253 }
16254 }
16255 }
16256 }
16257
16258 /* Copy fi.typedef_field_list linked list elements content into the
16259 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
16260 if (!fi.typedef_field_list.empty ())
16261 {
16262 int count = fi.typedef_field_list.size ();
16263
16264 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16265 TYPE_TYPEDEF_FIELD_ARRAY (type)
16266 = ((struct decl_field *)
16267 TYPE_ALLOC (type,
16268 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
16269 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
16270
16271 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
16272 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
16273 }
16274
16275 /* Copy fi.nested_types_list linked list elements content into the
16276 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
16277 if (!fi.nested_types_list.empty () && cu->language != language_ada)
16278 {
16279 int count = fi.nested_types_list.size ();
16280
16281 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16282 TYPE_NESTED_TYPES_ARRAY (type)
16283 = ((struct decl_field *)
16284 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
16285 TYPE_NESTED_TYPES_COUNT (type) = count;
16286
16287 for (int i = 0; i < fi.nested_types_list.size (); ++i)
16288 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
16289 }
16290 }
16291
16292 quirk_gcc_member_function_pointer (type, objfile);
16293 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
16294 cu->rust_unions.push_back (type);
16295
16296 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16297 snapshots) has been known to create a die giving a declaration
16298 for a class that has, as a child, a die giving a definition for a
16299 nested class. So we have to process our children even if the
16300 current die is a declaration. Normally, of course, a declaration
16301 won't have any children at all. */
16302
16303 child_die = die->child;
16304
16305 while (child_die != NULL && child_die->tag)
16306 {
16307 if (child_die->tag == DW_TAG_member
16308 || child_die->tag == DW_TAG_variable
16309 || child_die->tag == DW_TAG_inheritance
16310 || child_die->tag == DW_TAG_template_value_param
16311 || child_die->tag == DW_TAG_template_type_param)
16312 {
16313 /* Do nothing. */
16314 }
16315 else
16316 process_die (child_die, cu);
16317
16318 child_die = sibling_die (child_die);
16319 }
16320
16321 /* Do not consider external references. According to the DWARF standard,
16322 these DIEs are identified by the fact that they have no byte_size
16323 attribute, and a declaration attribute. */
16324 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16325 || !die_is_declaration (die, cu))
16326 {
16327 struct symbol *sym = new_symbol (die, type, cu);
16328
16329 if (has_template_parameters)
16330 {
16331 struct symtab *symtab;
16332 if (sym != nullptr)
16333 symtab = symbol_symtab (sym);
16334 else if (cu->line_header != nullptr)
16335 {
16336 /* Any related symtab will do. */
16337 symtab
16338 = cu->line_header->file_names ()[0].symtab;
16339 }
16340 else
16341 {
16342 symtab = nullptr;
16343 complaint (_("could not find suitable "
16344 "symtab for template parameter"
16345 " - DIE at %s [in module %s]"),
16346 sect_offset_str (die->sect_off),
16347 objfile_name (objfile));
16348 }
16349
16350 if (symtab != nullptr)
16351 {
16352 /* Make sure that the symtab is set on the new symbols.
16353 Even though they don't appear in this symtab directly,
16354 other parts of gdb assume that symbols do, and this is
16355 reasonably true. */
16356 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
16357 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
16358 }
16359 }
16360 }
16361 }
16362
16363 /* Assuming DIE is an enumeration type, and TYPE is its associated type,
16364 update TYPE using some information only available in DIE's children. */
16365
16366 static void
16367 update_enumeration_type_from_children (struct die_info *die,
16368 struct type *type,
16369 struct dwarf2_cu *cu)
16370 {
16371 struct die_info *child_die;
16372 int unsigned_enum = 1;
16373 int flag_enum = 1;
16374 ULONGEST mask = 0;
16375
16376 auto_obstack obstack;
16377
16378 for (child_die = die->child;
16379 child_die != NULL && child_die->tag;
16380 child_die = sibling_die (child_die))
16381 {
16382 struct attribute *attr;
16383 LONGEST value;
16384 const gdb_byte *bytes;
16385 struct dwarf2_locexpr_baton *baton;
16386 const char *name;
16387
16388 if (child_die->tag != DW_TAG_enumerator)
16389 continue;
16390
16391 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16392 if (attr == NULL)
16393 continue;
16394
16395 name = dwarf2_name (child_die, cu);
16396 if (name == NULL)
16397 name = "<anonymous enumerator>";
16398
16399 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16400 &value, &bytes, &baton);
16401 if (value < 0)
16402 {
16403 unsigned_enum = 0;
16404 flag_enum = 0;
16405 }
16406 else if ((mask & value) != 0)
16407 flag_enum = 0;
16408 else
16409 mask |= value;
16410
16411 /* If we already know that the enum type is neither unsigned, nor
16412 a flag type, no need to look at the rest of the enumerates. */
16413 if (!unsigned_enum && !flag_enum)
16414 break;
16415 }
16416
16417 if (unsigned_enum)
16418 TYPE_UNSIGNED (type) = 1;
16419 if (flag_enum)
16420 TYPE_FLAG_ENUM (type) = 1;
16421 }
16422
16423 /* Given a DW_AT_enumeration_type die, set its type. We do not
16424 complete the type's fields yet, or create any symbols. */
16425
16426 static struct type *
16427 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
16428 {
16429 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16430 struct type *type;
16431 struct attribute *attr;
16432 const char *name;
16433
16434 /* If the definition of this type lives in .debug_types, read that type.
16435 Don't follow DW_AT_specification though, that will take us back up
16436 the chain and we want to go down. */
16437 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
16438 if (attr != nullptr)
16439 {
16440 type = get_DW_AT_signature_type (die, attr, cu);
16441
16442 /* The type's CU may not be the same as CU.
16443 Ensure TYPE is recorded with CU in die_type_hash. */
16444 return set_die_type (die, type, cu);
16445 }
16446
16447 type = alloc_type (objfile);
16448
16449 TYPE_CODE (type) = TYPE_CODE_ENUM;
16450 name = dwarf2_full_name (NULL, die, cu);
16451 if (name != NULL)
16452 TYPE_NAME (type) = name;
16453
16454 attr = dwarf2_attr (die, DW_AT_type, cu);
16455 if (attr != NULL)
16456 {
16457 struct type *underlying_type = die_type (die, cu);
16458
16459 TYPE_TARGET_TYPE (type) = underlying_type;
16460 }
16461
16462 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16463 if (attr != nullptr)
16464 {
16465 TYPE_LENGTH (type) = DW_UNSND (attr);
16466 }
16467 else
16468 {
16469 TYPE_LENGTH (type) = 0;
16470 }
16471
16472 maybe_set_alignment (cu, die, type);
16473
16474 /* The enumeration DIE can be incomplete. In Ada, any type can be
16475 declared as private in the package spec, and then defined only
16476 inside the package body. Such types are known as Taft Amendment
16477 Types. When another package uses such a type, an incomplete DIE
16478 may be generated by the compiler. */
16479 if (die_is_declaration (die, cu))
16480 TYPE_STUB (type) = 1;
16481
16482 /* Finish the creation of this type by using the enum's children.
16483 We must call this even when the underlying type has been provided
16484 so that we can determine if we're looking at a "flag" enum. */
16485 update_enumeration_type_from_children (die, type, cu);
16486
16487 /* If this type has an underlying type that is not a stub, then we
16488 may use its attributes. We always use the "unsigned" attribute
16489 in this situation, because ordinarily we guess whether the type
16490 is unsigned -- but the guess can be wrong and the underlying type
16491 can tell us the reality. However, we defer to a local size
16492 attribute if one exists, because this lets the compiler override
16493 the underlying type if needed. */
16494 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16495 {
16496 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
16497 if (TYPE_LENGTH (type) == 0)
16498 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
16499 if (TYPE_RAW_ALIGN (type) == 0
16500 && TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)) != 0)
16501 set_type_align (type, TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)));
16502 }
16503
16504 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16505
16506 return set_die_type (die, type, cu);
16507 }
16508
16509 /* Given a pointer to a die which begins an enumeration, process all
16510 the dies that define the members of the enumeration, and create the
16511 symbol for the enumeration type.
16512
16513 NOTE: We reverse the order of the element list. */
16514
16515 static void
16516 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16517 {
16518 struct type *this_type;
16519
16520 this_type = get_die_type (die, cu);
16521 if (this_type == NULL)
16522 this_type = read_enumeration_type (die, cu);
16523
16524 if (die->child != NULL)
16525 {
16526 struct die_info *child_die;
16527 struct symbol *sym;
16528 std::vector<struct field> fields;
16529 const char *name;
16530
16531 child_die = die->child;
16532 while (child_die && child_die->tag)
16533 {
16534 if (child_die->tag != DW_TAG_enumerator)
16535 {
16536 process_die (child_die, cu);
16537 }
16538 else
16539 {
16540 name = dwarf2_name (child_die, cu);
16541 if (name)
16542 {
16543 sym = new_symbol (child_die, this_type, cu);
16544
16545 fields.emplace_back ();
16546 struct field &field = fields.back ();
16547
16548 FIELD_NAME (field) = sym->linkage_name ();
16549 FIELD_TYPE (field) = NULL;
16550 SET_FIELD_ENUMVAL (field, SYMBOL_VALUE (sym));
16551 FIELD_BITSIZE (field) = 0;
16552 }
16553 }
16554
16555 child_die = sibling_die (child_die);
16556 }
16557
16558 if (!fields.empty ())
16559 {
16560 TYPE_NFIELDS (this_type) = fields.size ();
16561 TYPE_FIELDS (this_type) = (struct field *)
16562 TYPE_ALLOC (this_type, sizeof (struct field) * fields.size ());
16563 memcpy (TYPE_FIELDS (this_type), fields.data (),
16564 sizeof (struct field) * fields.size ());
16565 }
16566 }
16567
16568 /* If we are reading an enum from a .debug_types unit, and the enum
16569 is a declaration, and the enum is not the signatured type in the
16570 unit, then we do not want to add a symbol for it. Adding a
16571 symbol would in some cases obscure the true definition of the
16572 enum, giving users an incomplete type when the definition is
16573 actually available. Note that we do not want to do this for all
16574 enums which are just declarations, because C++0x allows forward
16575 enum declarations. */
16576 if (cu->per_cu->is_debug_types
16577 && die_is_declaration (die, cu))
16578 {
16579 struct signatured_type *sig_type;
16580
16581 sig_type = (struct signatured_type *) cu->per_cu;
16582 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16583 if (sig_type->type_offset_in_section != die->sect_off)
16584 return;
16585 }
16586
16587 new_symbol (die, this_type, cu);
16588 }
16589
16590 /* Extract all information from a DW_TAG_array_type DIE and put it in
16591 the DIE's type field. For now, this only handles one dimensional
16592 arrays. */
16593
16594 static struct type *
16595 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
16596 {
16597 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16598 struct die_info *child_die;
16599 struct type *type;
16600 struct type *element_type, *range_type, *index_type;
16601 struct attribute *attr;
16602 const char *name;
16603 struct dynamic_prop *byte_stride_prop = NULL;
16604 unsigned int bit_stride = 0;
16605
16606 element_type = die_type (die, cu);
16607
16608 /* The die_type call above may have already set the type for this DIE. */
16609 type = get_die_type (die, cu);
16610 if (type)
16611 return type;
16612
16613 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16614 if (attr != NULL)
16615 {
16616 int stride_ok;
16617 struct type *prop_type
16618 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
16619
16620 byte_stride_prop
16621 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16622 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16623 prop_type);
16624 if (!stride_ok)
16625 {
16626 complaint (_("unable to read array DW_AT_byte_stride "
16627 " - DIE at %s [in module %s]"),
16628 sect_offset_str (die->sect_off),
16629 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16630 /* Ignore this attribute. We will likely not be able to print
16631 arrays of this type correctly, but there is little we can do
16632 to help if we cannot read the attribute's value. */
16633 byte_stride_prop = NULL;
16634 }
16635 }
16636
16637 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16638 if (attr != NULL)
16639 bit_stride = DW_UNSND (attr);
16640
16641 /* Irix 6.2 native cc creates array types without children for
16642 arrays with unspecified length. */
16643 if (die->child == NULL)
16644 {
16645 index_type = objfile_type (objfile)->builtin_int;
16646 range_type = create_static_range_type (NULL, index_type, 0, -1);
16647 type = create_array_type_with_stride (NULL, element_type, range_type,
16648 byte_stride_prop, bit_stride);
16649 return set_die_type (die, type, cu);
16650 }
16651
16652 std::vector<struct type *> range_types;
16653 child_die = die->child;
16654 while (child_die && child_die->tag)
16655 {
16656 if (child_die->tag == DW_TAG_subrange_type)
16657 {
16658 struct type *child_type = read_type_die (child_die, cu);
16659
16660 if (child_type != NULL)
16661 {
16662 /* The range type was succesfully read. Save it for the
16663 array type creation. */
16664 range_types.push_back (child_type);
16665 }
16666 }
16667 child_die = sibling_die (child_die);
16668 }
16669
16670 /* Dwarf2 dimensions are output from left to right, create the
16671 necessary array types in backwards order. */
16672
16673 type = element_type;
16674
16675 if (read_array_order (die, cu) == DW_ORD_col_major)
16676 {
16677 int i = 0;
16678
16679 while (i < range_types.size ())
16680 type = create_array_type_with_stride (NULL, type, range_types[i++],
16681 byte_stride_prop, bit_stride);
16682 }
16683 else
16684 {
16685 size_t ndim = range_types.size ();
16686 while (ndim-- > 0)
16687 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16688 byte_stride_prop, bit_stride);
16689 }
16690
16691 /* Understand Dwarf2 support for vector types (like they occur on
16692 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16693 array type. This is not part of the Dwarf2/3 standard yet, but a
16694 custom vendor extension. The main difference between a regular
16695 array and the vector variant is that vectors are passed by value
16696 to functions. */
16697 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
16698 if (attr != nullptr)
16699 make_vector_type (type);
16700
16701 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16702 implementation may choose to implement triple vectors using this
16703 attribute. */
16704 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16705 if (attr != nullptr)
16706 {
16707 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16708 TYPE_LENGTH (type) = DW_UNSND (attr);
16709 else
16710 complaint (_("DW_AT_byte_size for array type smaller "
16711 "than the total size of elements"));
16712 }
16713
16714 name = dwarf2_name (die, cu);
16715 if (name)
16716 TYPE_NAME (type) = name;
16717
16718 maybe_set_alignment (cu, die, type);
16719
16720 /* Install the type in the die. */
16721 set_die_type (die, type, cu);
16722
16723 /* set_die_type should be already done. */
16724 set_descriptive_type (type, die, cu);
16725
16726 return type;
16727 }
16728
16729 static enum dwarf_array_dim_ordering
16730 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
16731 {
16732 struct attribute *attr;
16733
16734 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16735
16736 if (attr != nullptr)
16737 return (enum dwarf_array_dim_ordering) DW_SND (attr);
16738
16739 /* GNU F77 is a special case, as at 08/2004 array type info is the
16740 opposite order to the dwarf2 specification, but data is still
16741 laid out as per normal fortran.
16742
16743 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16744 version checking. */
16745
16746 if (cu->language == language_fortran
16747 && cu->producer && strstr (cu->producer, "GNU F77"))
16748 {
16749 return DW_ORD_row_major;
16750 }
16751
16752 switch (cu->language_defn->la_array_ordering)
16753 {
16754 case array_column_major:
16755 return DW_ORD_col_major;
16756 case array_row_major:
16757 default:
16758 return DW_ORD_row_major;
16759 };
16760 }
16761
16762 /* Extract all information from a DW_TAG_set_type DIE and put it in
16763 the DIE's type field. */
16764
16765 static struct type *
16766 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16767 {
16768 struct type *domain_type, *set_type;
16769 struct attribute *attr;
16770
16771 domain_type = die_type (die, cu);
16772
16773 /* The die_type call above may have already set the type for this DIE. */
16774 set_type = get_die_type (die, cu);
16775 if (set_type)
16776 return set_type;
16777
16778 set_type = create_set_type (NULL, domain_type);
16779
16780 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16781 if (attr != nullptr)
16782 TYPE_LENGTH (set_type) = DW_UNSND (attr);
16783
16784 maybe_set_alignment (cu, die, set_type);
16785
16786 return set_die_type (die, set_type, cu);
16787 }
16788
16789 /* A helper for read_common_block that creates a locexpr baton.
16790 SYM is the symbol which we are marking as computed.
16791 COMMON_DIE is the DIE for the common block.
16792 COMMON_LOC is the location expression attribute for the common
16793 block itself.
16794 MEMBER_LOC is the location expression attribute for the particular
16795 member of the common block that we are processing.
16796 CU is the CU from which the above come. */
16797
16798 static void
16799 mark_common_block_symbol_computed (struct symbol *sym,
16800 struct die_info *common_die,
16801 struct attribute *common_loc,
16802 struct attribute *member_loc,
16803 struct dwarf2_cu *cu)
16804 {
16805 struct dwarf2_per_objfile *dwarf2_per_objfile
16806 = cu->per_cu->dwarf2_per_objfile;
16807 struct objfile *objfile = dwarf2_per_objfile->objfile;
16808 struct dwarf2_locexpr_baton *baton;
16809 gdb_byte *ptr;
16810 unsigned int cu_off;
16811 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
16812 LONGEST offset = 0;
16813
16814 gdb_assert (common_loc && member_loc);
16815 gdb_assert (attr_form_is_block (common_loc));
16816 gdb_assert (attr_form_is_block (member_loc)
16817 || attr_form_is_constant (member_loc));
16818
16819 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
16820 baton->per_cu = cu->per_cu;
16821 gdb_assert (baton->per_cu);
16822
16823 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16824
16825 if (attr_form_is_constant (member_loc))
16826 {
16827 offset = dwarf2_get_attr_constant_value (member_loc, 0);
16828 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16829 }
16830 else
16831 baton->size += DW_BLOCK (member_loc)->size;
16832
16833 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
16834 baton->data = ptr;
16835
16836 *ptr++ = DW_OP_call4;
16837 cu_off = common_die->sect_off - cu->per_cu->sect_off;
16838 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16839 ptr += 4;
16840
16841 if (attr_form_is_constant (member_loc))
16842 {
16843 *ptr++ = DW_OP_addr;
16844 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16845 ptr += cu->header.addr_size;
16846 }
16847 else
16848 {
16849 /* We have to copy the data here, because DW_OP_call4 will only
16850 use a DW_AT_location attribute. */
16851 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16852 ptr += DW_BLOCK (member_loc)->size;
16853 }
16854
16855 *ptr++ = DW_OP_plus;
16856 gdb_assert (ptr - baton->data == baton->size);
16857
16858 SYMBOL_LOCATION_BATON (sym) = baton;
16859 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
16860 }
16861
16862 /* Create appropriate locally-scoped variables for all the
16863 DW_TAG_common_block entries. Also create a struct common_block
16864 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16865 is used to separate the common blocks name namespace from regular
16866 variable names. */
16867
16868 static void
16869 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
16870 {
16871 struct attribute *attr;
16872
16873 attr = dwarf2_attr (die, DW_AT_location, cu);
16874 if (attr != nullptr)
16875 {
16876 /* Support the .debug_loc offsets. */
16877 if (attr_form_is_block (attr))
16878 {
16879 /* Ok. */
16880 }
16881 else if (attr_form_is_section_offset (attr))
16882 {
16883 dwarf2_complex_location_expr_complaint ();
16884 attr = NULL;
16885 }
16886 else
16887 {
16888 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16889 "common block member");
16890 attr = NULL;
16891 }
16892 }
16893
16894 if (die->child != NULL)
16895 {
16896 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16897 struct die_info *child_die;
16898 size_t n_entries = 0, size;
16899 struct common_block *common_block;
16900 struct symbol *sym;
16901
16902 for (child_die = die->child;
16903 child_die && child_die->tag;
16904 child_die = sibling_die (child_die))
16905 ++n_entries;
16906
16907 size = (sizeof (struct common_block)
16908 + (n_entries - 1) * sizeof (struct symbol *));
16909 common_block
16910 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16911 size);
16912 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16913 common_block->n_entries = 0;
16914
16915 for (child_die = die->child;
16916 child_die && child_die->tag;
16917 child_die = sibling_die (child_die))
16918 {
16919 /* Create the symbol in the DW_TAG_common_block block in the current
16920 symbol scope. */
16921 sym = new_symbol (child_die, NULL, cu);
16922 if (sym != NULL)
16923 {
16924 struct attribute *member_loc;
16925
16926 common_block->contents[common_block->n_entries++] = sym;
16927
16928 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16929 cu);
16930 if (member_loc)
16931 {
16932 /* GDB has handled this for a long time, but it is
16933 not specified by DWARF. It seems to have been
16934 emitted by gfortran at least as recently as:
16935 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16936 complaint (_("Variable in common block has "
16937 "DW_AT_data_member_location "
16938 "- DIE at %s [in module %s]"),
16939 sect_offset_str (child_die->sect_off),
16940 objfile_name (objfile));
16941
16942 if (attr_form_is_section_offset (member_loc))
16943 dwarf2_complex_location_expr_complaint ();
16944 else if (attr_form_is_constant (member_loc)
16945 || attr_form_is_block (member_loc))
16946 {
16947 if (attr != nullptr)
16948 mark_common_block_symbol_computed (sym, die, attr,
16949 member_loc, cu);
16950 }
16951 else
16952 dwarf2_complex_location_expr_complaint ();
16953 }
16954 }
16955 }
16956
16957 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16958 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
16959 }
16960 }
16961
16962 /* Create a type for a C++ namespace. */
16963
16964 static struct type *
16965 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16966 {
16967 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16968 const char *previous_prefix, *name;
16969 int is_anonymous;
16970 struct type *type;
16971
16972 /* For extensions, reuse the type of the original namespace. */
16973 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16974 {
16975 struct die_info *ext_die;
16976 struct dwarf2_cu *ext_cu = cu;
16977
16978 ext_die = dwarf2_extension (die, &ext_cu);
16979 type = read_type_die (ext_die, ext_cu);
16980
16981 /* EXT_CU may not be the same as CU.
16982 Ensure TYPE is recorded with CU in die_type_hash. */
16983 return set_die_type (die, type, cu);
16984 }
16985
16986 name = namespace_name (die, &is_anonymous, cu);
16987
16988 /* Now build the name of the current namespace. */
16989
16990 previous_prefix = determine_prefix (die, cu);
16991 if (previous_prefix[0] != '\0')
16992 name = typename_concat (&objfile->objfile_obstack,
16993 previous_prefix, name, 0, cu);
16994
16995 /* Create the type. */
16996 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
16997
16998 return set_die_type (die, type, cu);
16999 }
17000
17001 /* Read a namespace scope. */
17002
17003 static void
17004 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
17005 {
17006 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17007 int is_anonymous;
17008
17009 /* Add a symbol associated to this if we haven't seen the namespace
17010 before. Also, add a using directive if it's an anonymous
17011 namespace. */
17012
17013 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
17014 {
17015 struct type *type;
17016
17017 type = read_type_die (die, cu);
17018 new_symbol (die, type, cu);
17019
17020 namespace_name (die, &is_anonymous, cu);
17021 if (is_anonymous)
17022 {
17023 const char *previous_prefix = determine_prefix (die, cu);
17024
17025 std::vector<const char *> excludes;
17026 add_using_directive (using_directives (cu),
17027 previous_prefix, TYPE_NAME (type), NULL,
17028 NULL, excludes, 0, &objfile->objfile_obstack);
17029 }
17030 }
17031
17032 if (die->child != NULL)
17033 {
17034 struct die_info *child_die = die->child;
17035
17036 while (child_die && child_die->tag)
17037 {
17038 process_die (child_die, cu);
17039 child_die = sibling_die (child_die);
17040 }
17041 }
17042 }
17043
17044 /* Read a Fortran module as type. This DIE can be only a declaration used for
17045 imported module. Still we need that type as local Fortran "use ... only"
17046 declaration imports depend on the created type in determine_prefix. */
17047
17048 static struct type *
17049 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
17050 {
17051 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17052 const char *module_name;
17053 struct type *type;
17054
17055 module_name = dwarf2_name (die, cu);
17056 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
17057
17058 return set_die_type (die, type, cu);
17059 }
17060
17061 /* Read a Fortran module. */
17062
17063 static void
17064 read_module (struct die_info *die, struct dwarf2_cu *cu)
17065 {
17066 struct die_info *child_die = die->child;
17067 struct type *type;
17068
17069 type = read_type_die (die, cu);
17070 new_symbol (die, type, cu);
17071
17072 while (child_die && child_die->tag)
17073 {
17074 process_die (child_die, cu);
17075 child_die = sibling_die (child_die);
17076 }
17077 }
17078
17079 /* Return the name of the namespace represented by DIE. Set
17080 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
17081 namespace. */
17082
17083 static const char *
17084 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
17085 {
17086 struct die_info *current_die;
17087 const char *name = NULL;
17088
17089 /* Loop through the extensions until we find a name. */
17090
17091 for (current_die = die;
17092 current_die != NULL;
17093 current_die = dwarf2_extension (die, &cu))
17094 {
17095 /* We don't use dwarf2_name here so that we can detect the absence
17096 of a name -> anonymous namespace. */
17097 name = dwarf2_string_attr (die, DW_AT_name, cu);
17098
17099 if (name != NULL)
17100 break;
17101 }
17102
17103 /* Is it an anonymous namespace? */
17104
17105 *is_anonymous = (name == NULL);
17106 if (*is_anonymous)
17107 name = CP_ANONYMOUS_NAMESPACE_STR;
17108
17109 return name;
17110 }
17111
17112 /* Extract all information from a DW_TAG_pointer_type DIE and add to
17113 the user defined type vector. */
17114
17115 static struct type *
17116 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
17117 {
17118 struct gdbarch *gdbarch
17119 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
17120 struct comp_unit_head *cu_header = &cu->header;
17121 struct type *type;
17122 struct attribute *attr_byte_size;
17123 struct attribute *attr_address_class;
17124 int byte_size, addr_class;
17125 struct type *target_type;
17126
17127 target_type = die_type (die, cu);
17128
17129 /* The die_type call above may have already set the type for this DIE. */
17130 type = get_die_type (die, cu);
17131 if (type)
17132 return type;
17133
17134 type = lookup_pointer_type (target_type);
17135
17136 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
17137 if (attr_byte_size)
17138 byte_size = DW_UNSND (attr_byte_size);
17139 else
17140 byte_size = cu_header->addr_size;
17141
17142 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
17143 if (attr_address_class)
17144 addr_class = DW_UNSND (attr_address_class);
17145 else
17146 addr_class = DW_ADDR_none;
17147
17148 ULONGEST alignment = get_alignment (cu, die);
17149
17150 /* If the pointer size, alignment, or address class is different
17151 than the default, create a type variant marked as such and set
17152 the length accordingly. */
17153 if (TYPE_LENGTH (type) != byte_size
17154 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
17155 && alignment != TYPE_RAW_ALIGN (type))
17156 || addr_class != DW_ADDR_none)
17157 {
17158 if (gdbarch_address_class_type_flags_p (gdbarch))
17159 {
17160 int type_flags;
17161
17162 type_flags = gdbarch_address_class_type_flags
17163 (gdbarch, byte_size, addr_class);
17164 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17165 == 0);
17166 type = make_type_with_address_space (type, type_flags);
17167 }
17168 else if (TYPE_LENGTH (type) != byte_size)
17169 {
17170 complaint (_("invalid pointer size %d"), byte_size);
17171 }
17172 else if (TYPE_RAW_ALIGN (type) != alignment)
17173 {
17174 complaint (_("Invalid DW_AT_alignment"
17175 " - DIE at %s [in module %s]"),
17176 sect_offset_str (die->sect_off),
17177 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17178 }
17179 else
17180 {
17181 /* Should we also complain about unhandled address classes? */
17182 }
17183 }
17184
17185 TYPE_LENGTH (type) = byte_size;
17186 set_type_align (type, alignment);
17187 return set_die_type (die, type, cu);
17188 }
17189
17190 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17191 the user defined type vector. */
17192
17193 static struct type *
17194 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
17195 {
17196 struct type *type;
17197 struct type *to_type;
17198 struct type *domain;
17199
17200 to_type = die_type (die, cu);
17201 domain = die_containing_type (die, cu);
17202
17203 /* The calls above may have already set the type for this DIE. */
17204 type = get_die_type (die, cu);
17205 if (type)
17206 return type;
17207
17208 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
17209 type = lookup_methodptr_type (to_type);
17210 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
17211 {
17212 struct type *new_type
17213 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
17214
17215 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
17216 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
17217 TYPE_VARARGS (to_type));
17218 type = lookup_methodptr_type (new_type);
17219 }
17220 else
17221 type = lookup_memberptr_type (to_type, domain);
17222
17223 return set_die_type (die, type, cu);
17224 }
17225
17226 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
17227 the user defined type vector. */
17228
17229 static struct type *
17230 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
17231 enum type_code refcode)
17232 {
17233 struct comp_unit_head *cu_header = &cu->header;
17234 struct type *type, *target_type;
17235 struct attribute *attr;
17236
17237 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17238
17239 target_type = die_type (die, cu);
17240
17241 /* The die_type call above may have already set the type for this DIE. */
17242 type = get_die_type (die, cu);
17243 if (type)
17244 return type;
17245
17246 type = lookup_reference_type (target_type, refcode);
17247 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17248 if (attr != nullptr)
17249 {
17250 TYPE_LENGTH (type) = DW_UNSND (attr);
17251 }
17252 else
17253 {
17254 TYPE_LENGTH (type) = cu_header->addr_size;
17255 }
17256 maybe_set_alignment (cu, die, type);
17257 return set_die_type (die, type, cu);
17258 }
17259
17260 /* Add the given cv-qualifiers to the element type of the array. GCC
17261 outputs DWARF type qualifiers that apply to an array, not the
17262 element type. But GDB relies on the array element type to carry
17263 the cv-qualifiers. This mimics section 6.7.3 of the C99
17264 specification. */
17265
17266 static struct type *
17267 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17268 struct type *base_type, int cnst, int voltl)
17269 {
17270 struct type *el_type, *inner_array;
17271
17272 base_type = copy_type (base_type);
17273 inner_array = base_type;
17274
17275 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
17276 {
17277 TYPE_TARGET_TYPE (inner_array) =
17278 copy_type (TYPE_TARGET_TYPE (inner_array));
17279 inner_array = TYPE_TARGET_TYPE (inner_array);
17280 }
17281
17282 el_type = TYPE_TARGET_TYPE (inner_array);
17283 cnst |= TYPE_CONST (el_type);
17284 voltl |= TYPE_VOLATILE (el_type);
17285 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17286
17287 return set_die_type (die, base_type, cu);
17288 }
17289
17290 static struct type *
17291 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
17292 {
17293 struct type *base_type, *cv_type;
17294
17295 base_type = die_type (die, cu);
17296
17297 /* The die_type call above may have already set the type for this DIE. */
17298 cv_type = get_die_type (die, cu);
17299 if (cv_type)
17300 return cv_type;
17301
17302 /* In case the const qualifier is applied to an array type, the element type
17303 is so qualified, not the array type (section 6.7.3 of C99). */
17304 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17305 return add_array_cv_type (die, cu, base_type, 1, 0);
17306
17307 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17308 return set_die_type (die, cv_type, cu);
17309 }
17310
17311 static struct type *
17312 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
17313 {
17314 struct type *base_type, *cv_type;
17315
17316 base_type = die_type (die, cu);
17317
17318 /* The die_type call above may have already set the type for this DIE. */
17319 cv_type = get_die_type (die, cu);
17320 if (cv_type)
17321 return cv_type;
17322
17323 /* In case the volatile qualifier is applied to an array type, the
17324 element type is so qualified, not the array type (section 6.7.3
17325 of C99). */
17326 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17327 return add_array_cv_type (die, cu, base_type, 0, 1);
17328
17329 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17330 return set_die_type (die, cv_type, cu);
17331 }
17332
17333 /* Handle DW_TAG_restrict_type. */
17334
17335 static struct type *
17336 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17337 {
17338 struct type *base_type, *cv_type;
17339
17340 base_type = die_type (die, cu);
17341
17342 /* The die_type call above may have already set the type for this DIE. */
17343 cv_type = get_die_type (die, cu);
17344 if (cv_type)
17345 return cv_type;
17346
17347 cv_type = make_restrict_type (base_type);
17348 return set_die_type (die, cv_type, cu);
17349 }
17350
17351 /* Handle DW_TAG_atomic_type. */
17352
17353 static struct type *
17354 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17355 {
17356 struct type *base_type, *cv_type;
17357
17358 base_type = die_type (die, cu);
17359
17360 /* The die_type call above may have already set the type for this DIE. */
17361 cv_type = get_die_type (die, cu);
17362 if (cv_type)
17363 return cv_type;
17364
17365 cv_type = make_atomic_type (base_type);
17366 return set_die_type (die, cv_type, cu);
17367 }
17368
17369 /* Extract all information from a DW_TAG_string_type DIE and add to
17370 the user defined type vector. It isn't really a user defined type,
17371 but it behaves like one, with other DIE's using an AT_user_def_type
17372 attribute to reference it. */
17373
17374 static struct type *
17375 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
17376 {
17377 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17378 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17379 struct type *type, *range_type, *index_type, *char_type;
17380 struct attribute *attr;
17381 struct dynamic_prop prop;
17382 bool length_is_constant = true;
17383 LONGEST length;
17384
17385 /* There are a couple of places where bit sizes might be made use of
17386 when parsing a DW_TAG_string_type, however, no producer that we know
17387 of make use of these. Handling bit sizes that are a multiple of the
17388 byte size is easy enough, but what about other bit sizes? Lets deal
17389 with that problem when we have to. Warn about these attributes being
17390 unsupported, then parse the type and ignore them like we always
17391 have. */
17392 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
17393 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
17394 {
17395 static bool warning_printed = false;
17396 if (!warning_printed)
17397 {
17398 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
17399 "currently supported on DW_TAG_string_type."));
17400 warning_printed = true;
17401 }
17402 }
17403
17404 attr = dwarf2_attr (die, DW_AT_string_length, cu);
17405 if (attr != nullptr && !attr_form_is_constant (attr))
17406 {
17407 /* The string length describes the location at which the length of
17408 the string can be found. The size of the length field can be
17409 specified with one of the attributes below. */
17410 struct type *prop_type;
17411 struct attribute *len
17412 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
17413 if (len == nullptr)
17414 len = dwarf2_attr (die, DW_AT_byte_size, cu);
17415 if (len != nullptr && attr_form_is_constant (len))
17416 {
17417 /* Pass 0 as the default as we know this attribute is constant
17418 and the default value will not be returned. */
17419 LONGEST sz = dwarf2_get_attr_constant_value (len, 0);
17420 prop_type = dwarf2_per_cu_int_type (cu->per_cu, sz, true);
17421 }
17422 else
17423 {
17424 /* If the size is not specified then we assume it is the size of
17425 an address on this target. */
17426 prop_type = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, true);
17427 }
17428
17429 /* Convert the attribute into a dynamic property. */
17430 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17431 length = 1;
17432 else
17433 length_is_constant = false;
17434 }
17435 else if (attr != nullptr)
17436 {
17437 /* This DW_AT_string_length just contains the length with no
17438 indirection. There's no need to create a dynamic property in this
17439 case. Pass 0 for the default value as we know it will not be
17440 returned in this case. */
17441 length = dwarf2_get_attr_constant_value (attr, 0);
17442 }
17443 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
17444 {
17445 /* We don't currently support non-constant byte sizes for strings. */
17446 length = dwarf2_get_attr_constant_value (attr, 1);
17447 }
17448 else
17449 {
17450 /* Use 1 as a fallback length if we have nothing else. */
17451 length = 1;
17452 }
17453
17454 index_type = objfile_type (objfile)->builtin_int;
17455 if (length_is_constant)
17456 range_type = create_static_range_type (NULL, index_type, 1, length);
17457 else
17458 {
17459 struct dynamic_prop low_bound;
17460
17461 low_bound.kind = PROP_CONST;
17462 low_bound.data.const_val = 1;
17463 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17464 }
17465 char_type = language_string_char_type (cu->language_defn, gdbarch);
17466 type = create_string_type (NULL, char_type, range_type);
17467
17468 return set_die_type (die, type, cu);
17469 }
17470
17471 /* Assuming that DIE corresponds to a function, returns nonzero
17472 if the function is prototyped. */
17473
17474 static int
17475 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17476 {
17477 struct attribute *attr;
17478
17479 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17480 if (attr && (DW_UNSND (attr) != 0))
17481 return 1;
17482
17483 /* The DWARF standard implies that the DW_AT_prototyped attribute
17484 is only meaningful for C, but the concept also extends to other
17485 languages that allow unprototyped functions (Eg: Objective C).
17486 For all other languages, assume that functions are always
17487 prototyped. */
17488 if (cu->language != language_c
17489 && cu->language != language_objc
17490 && cu->language != language_opencl)
17491 return 1;
17492
17493 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17494 prototyped and unprototyped functions; default to prototyped,
17495 since that is more common in modern code (and RealView warns
17496 about unprototyped functions). */
17497 if (producer_is_realview (cu->producer))
17498 return 1;
17499
17500 return 0;
17501 }
17502
17503 /* Handle DIES due to C code like:
17504
17505 struct foo
17506 {
17507 int (*funcp)(int a, long l);
17508 int b;
17509 };
17510
17511 ('funcp' generates a DW_TAG_subroutine_type DIE). */
17512
17513 static struct type *
17514 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
17515 {
17516 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17517 struct type *type; /* Type that this function returns. */
17518 struct type *ftype; /* Function that returns above type. */
17519 struct attribute *attr;
17520
17521 type = die_type (die, cu);
17522
17523 /* The die_type call above may have already set the type for this DIE. */
17524 ftype = get_die_type (die, cu);
17525 if (ftype)
17526 return ftype;
17527
17528 ftype = lookup_function_type (type);
17529
17530 if (prototyped_function_p (die, cu))
17531 TYPE_PROTOTYPED (ftype) = 1;
17532
17533 /* Store the calling convention in the type if it's available in
17534 the subroutine die. Otherwise set the calling convention to
17535 the default value DW_CC_normal. */
17536 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
17537 if (attr != nullptr
17538 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
17539 TYPE_CALLING_CONVENTION (ftype)
17540 = (enum dwarf_calling_convention) (DW_UNSND (attr));
17541 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17542 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17543 else
17544 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
17545
17546 /* Record whether the function returns normally to its caller or not
17547 if the DWARF producer set that information. */
17548 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17549 if (attr && (DW_UNSND (attr) != 0))
17550 TYPE_NO_RETURN (ftype) = 1;
17551
17552 /* We need to add the subroutine type to the die immediately so
17553 we don't infinitely recurse when dealing with parameters
17554 declared as the same subroutine type. */
17555 set_die_type (die, ftype, cu);
17556
17557 if (die->child != NULL)
17558 {
17559 struct type *void_type = objfile_type (objfile)->builtin_void;
17560 struct die_info *child_die;
17561 int nparams, iparams;
17562
17563 /* Count the number of parameters.
17564 FIXME: GDB currently ignores vararg functions, but knows about
17565 vararg member functions. */
17566 nparams = 0;
17567 child_die = die->child;
17568 while (child_die && child_die->tag)
17569 {
17570 if (child_die->tag == DW_TAG_formal_parameter)
17571 nparams++;
17572 else if (child_die->tag == DW_TAG_unspecified_parameters)
17573 TYPE_VARARGS (ftype) = 1;
17574 child_die = sibling_die (child_die);
17575 }
17576
17577 /* Allocate storage for parameters and fill them in. */
17578 TYPE_NFIELDS (ftype) = nparams;
17579 TYPE_FIELDS (ftype) = (struct field *)
17580 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
17581
17582 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17583 even if we error out during the parameters reading below. */
17584 for (iparams = 0; iparams < nparams; iparams++)
17585 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17586
17587 iparams = 0;
17588 child_die = die->child;
17589 while (child_die && child_die->tag)
17590 {
17591 if (child_die->tag == DW_TAG_formal_parameter)
17592 {
17593 struct type *arg_type;
17594
17595 /* DWARF version 2 has no clean way to discern C++
17596 static and non-static member functions. G++ helps
17597 GDB by marking the first parameter for non-static
17598 member functions (which is the this pointer) as
17599 artificial. We pass this information to
17600 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17601
17602 DWARF version 3 added DW_AT_object_pointer, which GCC
17603 4.5 does not yet generate. */
17604 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
17605 if (attr != nullptr)
17606 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17607 else
17608 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
17609 arg_type = die_type (child_die, cu);
17610
17611 /* RealView does not mark THIS as const, which the testsuite
17612 expects. GCC marks THIS as const in method definitions,
17613 but not in the class specifications (GCC PR 43053). */
17614 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17615 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17616 {
17617 int is_this = 0;
17618 struct dwarf2_cu *arg_cu = cu;
17619 const char *name = dwarf2_name (child_die, cu);
17620
17621 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17622 if (attr != nullptr)
17623 {
17624 /* If the compiler emits this, use it. */
17625 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17626 is_this = 1;
17627 }
17628 else if (name && strcmp (name, "this") == 0)
17629 /* Function definitions will have the argument names. */
17630 is_this = 1;
17631 else if (name == NULL && iparams == 0)
17632 /* Declarations may not have the names, so like
17633 elsewhere in GDB, assume an artificial first
17634 argument is "this". */
17635 is_this = 1;
17636
17637 if (is_this)
17638 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17639 arg_type, 0);
17640 }
17641
17642 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
17643 iparams++;
17644 }
17645 child_die = sibling_die (child_die);
17646 }
17647 }
17648
17649 return ftype;
17650 }
17651
17652 static struct type *
17653 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
17654 {
17655 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17656 const char *name = NULL;
17657 struct type *this_type, *target_type;
17658
17659 name = dwarf2_full_name (NULL, die, cu);
17660 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17661 TYPE_TARGET_STUB (this_type) = 1;
17662 set_die_type (die, this_type, cu);
17663 target_type = die_type (die, cu);
17664 if (target_type != this_type)
17665 TYPE_TARGET_TYPE (this_type) = target_type;
17666 else
17667 {
17668 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17669 spec and cause infinite loops in GDB. */
17670 complaint (_("Self-referential DW_TAG_typedef "
17671 "- DIE at %s [in module %s]"),
17672 sect_offset_str (die->sect_off), objfile_name (objfile));
17673 TYPE_TARGET_TYPE (this_type) = NULL;
17674 }
17675 return this_type;
17676 }
17677
17678 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17679 (which may be different from NAME) to the architecture back-end to allow
17680 it to guess the correct format if necessary. */
17681
17682 static struct type *
17683 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17684 const char *name_hint, enum bfd_endian byte_order)
17685 {
17686 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17687 const struct floatformat **format;
17688 struct type *type;
17689
17690 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17691 if (format)
17692 type = init_float_type (objfile, bits, name, format, byte_order);
17693 else
17694 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17695
17696 return type;
17697 }
17698
17699 /* Allocate an integer type of size BITS and name NAME. */
17700
17701 static struct type *
17702 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17703 int bits, int unsigned_p, const char *name)
17704 {
17705 struct type *type;
17706
17707 /* Versions of Intel's C Compiler generate an integer type called "void"
17708 instead of using DW_TAG_unspecified_type. This has been seen on
17709 at least versions 14, 17, and 18. */
17710 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17711 && strcmp (name, "void") == 0)
17712 type = objfile_type (objfile)->builtin_void;
17713 else
17714 type = init_integer_type (objfile, bits, unsigned_p, name);
17715
17716 return type;
17717 }
17718
17719 /* Initialise and return a floating point type of size BITS suitable for
17720 use as a component of a complex number. The NAME_HINT is passed through
17721 when initialising the floating point type and is the name of the complex
17722 type.
17723
17724 As DWARF doesn't currently provide an explicit name for the components
17725 of a complex number, but it can be helpful to have these components
17726 named, we try to select a suitable name based on the size of the
17727 component. */
17728 static struct type *
17729 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17730 struct objfile *objfile,
17731 int bits, const char *name_hint,
17732 enum bfd_endian byte_order)
17733 {
17734 gdbarch *gdbarch = get_objfile_arch (objfile);
17735 struct type *tt = nullptr;
17736
17737 /* Try to find a suitable floating point builtin type of size BITS.
17738 We're going to use the name of this type as the name for the complex
17739 target type that we are about to create. */
17740 switch (cu->language)
17741 {
17742 case language_fortran:
17743 switch (bits)
17744 {
17745 case 32:
17746 tt = builtin_f_type (gdbarch)->builtin_real;
17747 break;
17748 case 64:
17749 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17750 break;
17751 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17752 case 128:
17753 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17754 break;
17755 }
17756 break;
17757 default:
17758 switch (bits)
17759 {
17760 case 32:
17761 tt = builtin_type (gdbarch)->builtin_float;
17762 break;
17763 case 64:
17764 tt = builtin_type (gdbarch)->builtin_double;
17765 break;
17766 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17767 case 128:
17768 tt = builtin_type (gdbarch)->builtin_long_double;
17769 break;
17770 }
17771 break;
17772 }
17773
17774 /* If the type we found doesn't match the size we were looking for, then
17775 pretend we didn't find a type at all, the complex target type we
17776 create will then be nameless. */
17777 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
17778 tt = nullptr;
17779
17780 const char *name = (tt == nullptr) ? nullptr : TYPE_NAME (tt);
17781 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
17782 }
17783
17784 /* Find a representation of a given base type and install
17785 it in the TYPE field of the die. */
17786
17787 static struct type *
17788 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
17789 {
17790 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17791 struct type *type;
17792 struct attribute *attr;
17793 int encoding = 0, bits = 0;
17794 const char *name;
17795 gdbarch *arch;
17796
17797 attr = dwarf2_attr (die, DW_AT_encoding, cu);
17798 if (attr != nullptr)
17799 encoding = DW_UNSND (attr);
17800 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17801 if (attr != nullptr)
17802 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
17803 name = dwarf2_name (die, cu);
17804 if (!name)
17805 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
17806
17807 arch = get_objfile_arch (objfile);
17808 enum bfd_endian byte_order = gdbarch_byte_order (arch);
17809
17810 attr = dwarf2_attr (die, DW_AT_endianity, cu);
17811 if (attr)
17812 {
17813 int endianity = DW_UNSND (attr);
17814
17815 switch (endianity)
17816 {
17817 case DW_END_big:
17818 byte_order = BFD_ENDIAN_BIG;
17819 break;
17820 case DW_END_little:
17821 byte_order = BFD_ENDIAN_LITTLE;
17822 break;
17823 default:
17824 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
17825 break;
17826 }
17827 }
17828
17829 switch (encoding)
17830 {
17831 case DW_ATE_address:
17832 /* Turn DW_ATE_address into a void * pointer. */
17833 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
17834 type = init_pointer_type (objfile, bits, name, type);
17835 break;
17836 case DW_ATE_boolean:
17837 type = init_boolean_type (objfile, bits, 1, name);
17838 break;
17839 case DW_ATE_complex_float:
17840 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
17841 byte_order);
17842 type = init_complex_type (objfile, name, type);
17843 break;
17844 case DW_ATE_decimal_float:
17845 type = init_decfloat_type (objfile, bits, name);
17846 break;
17847 case DW_ATE_float:
17848 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
17849 break;
17850 case DW_ATE_signed:
17851 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17852 break;
17853 case DW_ATE_unsigned:
17854 if (cu->language == language_fortran
17855 && name
17856 && startswith (name, "character("))
17857 type = init_character_type (objfile, bits, 1, name);
17858 else
17859 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17860 break;
17861 case DW_ATE_signed_char:
17862 if (cu->language == language_ada || cu->language == language_m2
17863 || cu->language == language_pascal
17864 || cu->language == language_fortran)
17865 type = init_character_type (objfile, bits, 0, name);
17866 else
17867 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17868 break;
17869 case DW_ATE_unsigned_char:
17870 if (cu->language == language_ada || cu->language == language_m2
17871 || cu->language == language_pascal
17872 || cu->language == language_fortran
17873 || cu->language == language_rust)
17874 type = init_character_type (objfile, bits, 1, name);
17875 else
17876 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17877 break;
17878 case DW_ATE_UTF:
17879 {
17880 if (bits == 16)
17881 type = builtin_type (arch)->builtin_char16;
17882 else if (bits == 32)
17883 type = builtin_type (arch)->builtin_char32;
17884 else
17885 {
17886 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
17887 bits);
17888 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17889 }
17890 return set_die_type (die, type, cu);
17891 }
17892 break;
17893
17894 default:
17895 complaint (_("unsupported DW_AT_encoding: '%s'"),
17896 dwarf_type_encoding_name (encoding));
17897 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17898 break;
17899 }
17900
17901 if (name && strcmp (name, "char") == 0)
17902 TYPE_NOSIGN (type) = 1;
17903
17904 maybe_set_alignment (cu, die, type);
17905
17906 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
17907
17908 return set_die_type (die, type, cu);
17909 }
17910
17911 /* Parse dwarf attribute if it's a block, reference or constant and put the
17912 resulting value of the attribute into struct bound_prop.
17913 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17914
17915 static int
17916 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17917 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17918 struct type *default_type)
17919 {
17920 struct dwarf2_property_baton *baton;
17921 struct obstack *obstack
17922 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
17923
17924 gdb_assert (default_type != NULL);
17925
17926 if (attr == NULL || prop == NULL)
17927 return 0;
17928
17929 if (attr_form_is_block (attr))
17930 {
17931 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17932 baton->property_type = default_type;
17933 baton->locexpr.per_cu = cu->per_cu;
17934 baton->locexpr.size = DW_BLOCK (attr)->size;
17935 baton->locexpr.data = DW_BLOCK (attr)->data;
17936 switch (attr->name)
17937 {
17938 case DW_AT_string_length:
17939 baton->locexpr.is_reference = true;
17940 break;
17941 default:
17942 baton->locexpr.is_reference = false;
17943 break;
17944 }
17945 prop->data.baton = baton;
17946 prop->kind = PROP_LOCEXPR;
17947 gdb_assert (prop->data.baton != NULL);
17948 }
17949 else if (attr_form_is_ref (attr))
17950 {
17951 struct dwarf2_cu *target_cu = cu;
17952 struct die_info *target_die;
17953 struct attribute *target_attr;
17954
17955 target_die = follow_die_ref (die, attr, &target_cu);
17956 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17957 if (target_attr == NULL)
17958 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17959 target_cu);
17960 if (target_attr == NULL)
17961 return 0;
17962
17963 switch (target_attr->name)
17964 {
17965 case DW_AT_location:
17966 if (attr_form_is_section_offset (target_attr))
17967 {
17968 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17969 baton->property_type = die_type (target_die, target_cu);
17970 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17971 prop->data.baton = baton;
17972 prop->kind = PROP_LOCLIST;
17973 gdb_assert (prop->data.baton != NULL);
17974 }
17975 else if (attr_form_is_block (target_attr))
17976 {
17977 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17978 baton->property_type = die_type (target_die, target_cu);
17979 baton->locexpr.per_cu = cu->per_cu;
17980 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17981 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17982 baton->locexpr.is_reference = true;
17983 prop->data.baton = baton;
17984 prop->kind = PROP_LOCEXPR;
17985 gdb_assert (prop->data.baton != NULL);
17986 }
17987 else
17988 {
17989 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17990 "dynamic property");
17991 return 0;
17992 }
17993 break;
17994 case DW_AT_data_member_location:
17995 {
17996 LONGEST offset;
17997
17998 if (!handle_data_member_location (target_die, target_cu,
17999 &offset))
18000 return 0;
18001
18002 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18003 baton->property_type = read_type_die (target_die->parent,
18004 target_cu);
18005 baton->offset_info.offset = offset;
18006 baton->offset_info.type = die_type (target_die, target_cu);
18007 prop->data.baton = baton;
18008 prop->kind = PROP_ADDR_OFFSET;
18009 break;
18010 }
18011 }
18012 }
18013 else if (attr_form_is_constant (attr))
18014 {
18015 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
18016 prop->kind = PROP_CONST;
18017 }
18018 else
18019 {
18020 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
18021 dwarf2_name (die, cu));
18022 return 0;
18023 }
18024
18025 return 1;
18026 }
18027
18028 /* Find an integer type SIZE_IN_BYTES bytes in size and return it.
18029 UNSIGNED_P controls if the integer is unsigned or not. */
18030
18031 static struct type *
18032 dwarf2_per_cu_int_type (struct dwarf2_per_cu_data *per_cu,
18033 int size_in_bytes, bool unsigned_p)
18034 {
18035 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
18036 struct type *int_type;
18037
18038 /* Helper macro to examine the various builtin types. */
18039 #define TRY_TYPE(F) \
18040 int_type = (unsigned_p \
18041 ? objfile_type (objfile)->builtin_unsigned_ ## F \
18042 : objfile_type (objfile)->builtin_ ## F); \
18043 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
18044 return int_type
18045
18046 TRY_TYPE (char);
18047 TRY_TYPE (short);
18048 TRY_TYPE (int);
18049 TRY_TYPE (long);
18050 TRY_TYPE (long_long);
18051
18052 #undef TRY_TYPE
18053
18054 gdb_assert_not_reached ("unable to find suitable integer type");
18055 }
18056
18057 /* Find an integer type the same size as the address size given in the
18058 compilation unit header for PER_CU. UNSIGNED_P controls if the integer
18059 is unsigned or not. */
18060
18061 static struct type *
18062 dwarf2_per_cu_addr_sized_int_type (struct dwarf2_per_cu_data *per_cu,
18063 bool unsigned_p)
18064 {
18065 int addr_size = dwarf2_per_cu_addr_size (per_cu);
18066 return dwarf2_per_cu_int_type (per_cu, addr_size, unsigned_p);
18067 }
18068
18069 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
18070 present (which is valid) then compute the default type based on the
18071 compilation units address size. */
18072
18073 static struct type *
18074 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
18075 {
18076 struct type *index_type = die_type (die, cu);
18077
18078 /* Dwarf-2 specifications explicitly allows to create subrange types
18079 without specifying a base type.
18080 In that case, the base type must be set to the type of
18081 the lower bound, upper bound or count, in that order, if any of these
18082 three attributes references an object that has a type.
18083 If no base type is found, the Dwarf-2 specifications say that
18084 a signed integer type of size equal to the size of an address should
18085 be used.
18086 For the following C code: `extern char gdb_int [];'
18087 GCC produces an empty range DIE.
18088 FIXME: muller/2010-05-28: Possible references to object for low bound,
18089 high bound or count are not yet handled by this code. */
18090 if (TYPE_CODE (index_type) == TYPE_CODE_VOID)
18091 index_type = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
18092
18093 return index_type;
18094 }
18095
18096 /* Read the given DW_AT_subrange DIE. */
18097
18098 static struct type *
18099 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
18100 {
18101 struct type *base_type, *orig_base_type;
18102 struct type *range_type;
18103 struct attribute *attr;
18104 struct dynamic_prop low, high;
18105 int low_default_is_valid;
18106 int high_bound_is_count = 0;
18107 const char *name;
18108 ULONGEST negative_mask;
18109
18110 orig_base_type = read_subrange_index_type (die, cu);
18111
18112 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
18113 whereas the real type might be. So, we use ORIG_BASE_TYPE when
18114 creating the range type, but we use the result of check_typedef
18115 when examining properties of the type. */
18116 base_type = check_typedef (orig_base_type);
18117
18118 /* The die_type call above may have already set the type for this DIE. */
18119 range_type = get_die_type (die, cu);
18120 if (range_type)
18121 return range_type;
18122
18123 low.kind = PROP_CONST;
18124 high.kind = PROP_CONST;
18125 high.data.const_val = 0;
18126
18127 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
18128 omitting DW_AT_lower_bound. */
18129 switch (cu->language)
18130 {
18131 case language_c:
18132 case language_cplus:
18133 low.data.const_val = 0;
18134 low_default_is_valid = 1;
18135 break;
18136 case language_fortran:
18137 low.data.const_val = 1;
18138 low_default_is_valid = 1;
18139 break;
18140 case language_d:
18141 case language_objc:
18142 case language_rust:
18143 low.data.const_val = 0;
18144 low_default_is_valid = (cu->header.version >= 4);
18145 break;
18146 case language_ada:
18147 case language_m2:
18148 case language_pascal:
18149 low.data.const_val = 1;
18150 low_default_is_valid = (cu->header.version >= 4);
18151 break;
18152 default:
18153 low.data.const_val = 0;
18154 low_default_is_valid = 0;
18155 break;
18156 }
18157
18158 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
18159 if (attr != nullptr)
18160 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
18161 else if (!low_default_is_valid)
18162 complaint (_("Missing DW_AT_lower_bound "
18163 "- DIE at %s [in module %s]"),
18164 sect_offset_str (die->sect_off),
18165 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
18166
18167 struct attribute *attr_ub, *attr_count;
18168 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
18169 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
18170 {
18171 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
18172 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
18173 {
18174 /* If bounds are constant do the final calculation here. */
18175 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
18176 high.data.const_val = low.data.const_val + high.data.const_val - 1;
18177 else
18178 high_bound_is_count = 1;
18179 }
18180 else
18181 {
18182 if (attr_ub != NULL)
18183 complaint (_("Unresolved DW_AT_upper_bound "
18184 "- DIE at %s [in module %s]"),
18185 sect_offset_str (die->sect_off),
18186 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
18187 if (attr_count != NULL)
18188 complaint (_("Unresolved DW_AT_count "
18189 "- DIE at %s [in module %s]"),
18190 sect_offset_str (die->sect_off),
18191 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
18192 }
18193 }
18194
18195 LONGEST bias = 0;
18196 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
18197 if (bias_attr != nullptr && attr_form_is_constant (bias_attr))
18198 bias = dwarf2_get_attr_constant_value (bias_attr, 0);
18199
18200 /* Normally, the DWARF producers are expected to use a signed
18201 constant form (Eg. DW_FORM_sdata) to express negative bounds.
18202 But this is unfortunately not always the case, as witnessed
18203 with GCC, for instance, where the ambiguous DW_FORM_dataN form
18204 is used instead. To work around that ambiguity, we treat
18205 the bounds as signed, and thus sign-extend their values, when
18206 the base type is signed. */
18207 negative_mask =
18208 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
18209 if (low.kind == PROP_CONST
18210 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
18211 low.data.const_val |= negative_mask;
18212 if (high.kind == PROP_CONST
18213 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
18214 high.data.const_val |= negative_mask;
18215
18216 /* Check for bit and byte strides. */
18217 struct dynamic_prop byte_stride_prop;
18218 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
18219 if (attr_byte_stride != nullptr)
18220 {
18221 struct type *prop_type
18222 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
18223 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
18224 prop_type);
18225 }
18226
18227 struct dynamic_prop bit_stride_prop;
18228 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
18229 if (attr_bit_stride != nullptr)
18230 {
18231 /* It only makes sense to have either a bit or byte stride. */
18232 if (attr_byte_stride != nullptr)
18233 {
18234 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
18235 "- DIE at %s [in module %s]"),
18236 sect_offset_str (die->sect_off),
18237 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
18238 attr_bit_stride = nullptr;
18239 }
18240 else
18241 {
18242 struct type *prop_type
18243 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
18244 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
18245 prop_type);
18246 }
18247 }
18248
18249 if (attr_byte_stride != nullptr
18250 || attr_bit_stride != nullptr)
18251 {
18252 bool byte_stride_p = (attr_byte_stride != nullptr);
18253 struct dynamic_prop *stride
18254 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
18255
18256 range_type
18257 = create_range_type_with_stride (NULL, orig_base_type, &low,
18258 &high, bias, stride, byte_stride_p);
18259 }
18260 else
18261 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
18262
18263 if (high_bound_is_count)
18264 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
18265
18266 /* Ada expects an empty array on no boundary attributes. */
18267 if (attr == NULL && cu->language != language_ada)
18268 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
18269
18270 name = dwarf2_name (die, cu);
18271 if (name)
18272 TYPE_NAME (range_type) = name;
18273
18274 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
18275 if (attr != nullptr)
18276 TYPE_LENGTH (range_type) = DW_UNSND (attr);
18277
18278 maybe_set_alignment (cu, die, range_type);
18279
18280 set_die_type (die, range_type, cu);
18281
18282 /* set_die_type should be already done. */
18283 set_descriptive_type (range_type, die, cu);
18284
18285 return range_type;
18286 }
18287
18288 static struct type *
18289 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
18290 {
18291 struct type *type;
18292
18293 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
18294 NULL);
18295 TYPE_NAME (type) = dwarf2_name (die, cu);
18296
18297 /* In Ada, an unspecified type is typically used when the description
18298 of the type is deferred to a different unit. When encountering
18299 such a type, we treat it as a stub, and try to resolve it later on,
18300 when needed. */
18301 if (cu->language == language_ada)
18302 TYPE_STUB (type) = 1;
18303
18304 return set_die_type (die, type, cu);
18305 }
18306
18307 /* Read a single die and all its descendents. Set the die's sibling
18308 field to NULL; set other fields in the die correctly, and set all
18309 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
18310 location of the info_ptr after reading all of those dies. PARENT
18311 is the parent of the die in question. */
18312
18313 static struct die_info *
18314 read_die_and_children (const struct die_reader_specs *reader,
18315 const gdb_byte *info_ptr,
18316 const gdb_byte **new_info_ptr,
18317 struct die_info *parent)
18318 {
18319 struct die_info *die;
18320 const gdb_byte *cur_ptr;
18321 int has_children;
18322
18323 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
18324 if (die == NULL)
18325 {
18326 *new_info_ptr = cur_ptr;
18327 return NULL;
18328 }
18329 store_in_ref_table (die, reader->cu);
18330
18331 if (has_children)
18332 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
18333 else
18334 {
18335 die->child = NULL;
18336 *new_info_ptr = cur_ptr;
18337 }
18338
18339 die->sibling = NULL;
18340 die->parent = parent;
18341 return die;
18342 }
18343
18344 /* Read a die, all of its descendents, and all of its siblings; set
18345 all of the fields of all of the dies correctly. Arguments are as
18346 in read_die_and_children. */
18347
18348 static struct die_info *
18349 read_die_and_siblings_1 (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 *first_die, *last_sibling;
18355 const gdb_byte *cur_ptr;
18356
18357 cur_ptr = info_ptr;
18358 first_die = last_sibling = NULL;
18359
18360 while (1)
18361 {
18362 struct die_info *die
18363 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
18364
18365 if (die == NULL)
18366 {
18367 *new_info_ptr = cur_ptr;
18368 return first_die;
18369 }
18370
18371 if (!first_die)
18372 first_die = die;
18373 else
18374 last_sibling->sibling = die;
18375
18376 last_sibling = die;
18377 }
18378 }
18379
18380 /* Read a die, all of its descendents, and all of its siblings; set
18381 all of the fields of all of the dies correctly. Arguments are as
18382 in read_die_and_children.
18383 This the main entry point for reading a DIE and all its children. */
18384
18385 static struct die_info *
18386 read_die_and_siblings (const struct die_reader_specs *reader,
18387 const gdb_byte *info_ptr,
18388 const gdb_byte **new_info_ptr,
18389 struct die_info *parent)
18390 {
18391 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
18392 new_info_ptr, parent);
18393
18394 if (dwarf_die_debug)
18395 {
18396 fprintf_unfiltered (gdb_stdlog,
18397 "Read die from %s@0x%x of %s:\n",
18398 get_section_name (reader->die_section),
18399 (unsigned) (info_ptr - reader->die_section->buffer),
18400 bfd_get_filename (reader->abfd));
18401 dump_die (die, dwarf_die_debug);
18402 }
18403
18404 return die;
18405 }
18406
18407 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
18408 attributes.
18409 The caller is responsible for filling in the extra attributes
18410 and updating (*DIEP)->num_attrs.
18411 Set DIEP to point to a newly allocated die with its information,
18412 except for its child, sibling, and parent fields.
18413 Set HAS_CHILDREN to tell whether the die has children or not. */
18414
18415 static const gdb_byte *
18416 read_full_die_1 (const struct die_reader_specs *reader,
18417 struct die_info **diep, const gdb_byte *info_ptr,
18418 int *has_children, int num_extra_attrs)
18419 {
18420 unsigned int abbrev_number, bytes_read, i;
18421 struct abbrev_info *abbrev;
18422 struct die_info *die;
18423 struct dwarf2_cu *cu = reader->cu;
18424 bfd *abfd = reader->abfd;
18425
18426 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
18427 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18428 info_ptr += bytes_read;
18429 if (!abbrev_number)
18430 {
18431 *diep = NULL;
18432 *has_children = 0;
18433 return info_ptr;
18434 }
18435
18436 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
18437 if (!abbrev)
18438 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18439 abbrev_number,
18440 bfd_get_filename (abfd));
18441
18442 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
18443 die->sect_off = sect_off;
18444 die->tag = abbrev->tag;
18445 die->abbrev = abbrev_number;
18446
18447 /* Make the result usable.
18448 The caller needs to update num_attrs after adding the extra
18449 attributes. */
18450 die->num_attrs = abbrev->num_attrs;
18451
18452 for (i = 0; i < abbrev->num_attrs; ++i)
18453 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18454 info_ptr);
18455
18456 *diep = die;
18457 *has_children = abbrev->has_children;
18458 return info_ptr;
18459 }
18460
18461 /* Read a die and all its attributes.
18462 Set DIEP to point to a newly allocated die with its information,
18463 except for its child, sibling, and parent fields.
18464 Set HAS_CHILDREN to tell whether the die has children or not. */
18465
18466 static const gdb_byte *
18467 read_full_die (const struct die_reader_specs *reader,
18468 struct die_info **diep, const gdb_byte *info_ptr,
18469 int *has_children)
18470 {
18471 const gdb_byte *result;
18472
18473 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
18474
18475 if (dwarf_die_debug)
18476 {
18477 fprintf_unfiltered (gdb_stdlog,
18478 "Read die from %s@0x%x of %s:\n",
18479 get_section_name (reader->die_section),
18480 (unsigned) (info_ptr - reader->die_section->buffer),
18481 bfd_get_filename (reader->abfd));
18482 dump_die (*diep, dwarf_die_debug);
18483 }
18484
18485 return result;
18486 }
18487 \f
18488 /* Abbreviation tables.
18489
18490 In DWARF version 2, the description of the debugging information is
18491 stored in a separate .debug_abbrev section. Before we read any
18492 dies from a section we read in all abbreviations and install them
18493 in a hash table. */
18494
18495 /* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
18496
18497 struct abbrev_info *
18498 abbrev_table::alloc_abbrev ()
18499 {
18500 struct abbrev_info *abbrev;
18501
18502 abbrev = XOBNEW (&abbrev_obstack, struct abbrev_info);
18503 memset (abbrev, 0, sizeof (struct abbrev_info));
18504
18505 return abbrev;
18506 }
18507
18508 /* Add an abbreviation to the table. */
18509
18510 void
18511 abbrev_table::add_abbrev (unsigned int abbrev_number,
18512 struct abbrev_info *abbrev)
18513 {
18514 unsigned int hash_number;
18515
18516 hash_number = abbrev_number % ABBREV_HASH_SIZE;
18517 abbrev->next = m_abbrevs[hash_number];
18518 m_abbrevs[hash_number] = abbrev;
18519 }
18520
18521 /* Look up an abbrev in the table.
18522 Returns NULL if the abbrev is not found. */
18523
18524 struct abbrev_info *
18525 abbrev_table::lookup_abbrev (unsigned int abbrev_number)
18526 {
18527 unsigned int hash_number;
18528 struct abbrev_info *abbrev;
18529
18530 hash_number = abbrev_number % ABBREV_HASH_SIZE;
18531 abbrev = m_abbrevs[hash_number];
18532
18533 while (abbrev)
18534 {
18535 if (abbrev->number == abbrev_number)
18536 return abbrev;
18537 abbrev = abbrev->next;
18538 }
18539 return NULL;
18540 }
18541
18542 /* Read in an abbrev table. */
18543
18544 static abbrev_table_up
18545 abbrev_table_read_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
18546 struct dwarf2_section_info *section,
18547 sect_offset sect_off)
18548 {
18549 struct objfile *objfile = dwarf2_per_objfile->objfile;
18550 bfd *abfd = get_section_bfd_owner (section);
18551 const gdb_byte *abbrev_ptr;
18552 struct abbrev_info *cur_abbrev;
18553 unsigned int abbrev_number, bytes_read, abbrev_name;
18554 unsigned int abbrev_form;
18555 std::vector<struct attr_abbrev> cur_attrs;
18556
18557 abbrev_table_up abbrev_table (new struct abbrev_table (sect_off));
18558
18559 dwarf2_read_section (objfile, section);
18560 abbrev_ptr = section->buffer + to_underlying (sect_off);
18561 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18562 abbrev_ptr += bytes_read;
18563
18564 /* Loop until we reach an abbrev number of 0. */
18565 while (abbrev_number)
18566 {
18567 cur_attrs.clear ();
18568 cur_abbrev = abbrev_table->alloc_abbrev ();
18569
18570 /* read in abbrev header */
18571 cur_abbrev->number = abbrev_number;
18572 cur_abbrev->tag
18573 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18574 abbrev_ptr += bytes_read;
18575 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
18576 abbrev_ptr += 1;
18577
18578 /* now read in declarations */
18579 for (;;)
18580 {
18581 LONGEST implicit_const;
18582
18583 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18584 abbrev_ptr += bytes_read;
18585 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18586 abbrev_ptr += bytes_read;
18587 if (abbrev_form == DW_FORM_implicit_const)
18588 {
18589 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
18590 &bytes_read);
18591 abbrev_ptr += bytes_read;
18592 }
18593 else
18594 {
18595 /* Initialize it due to a false compiler warning. */
18596 implicit_const = -1;
18597 }
18598
18599 if (abbrev_name == 0)
18600 break;
18601
18602 cur_attrs.emplace_back ();
18603 struct attr_abbrev &cur_attr = cur_attrs.back ();
18604 cur_attr.name = (enum dwarf_attribute) abbrev_name;
18605 cur_attr.form = (enum dwarf_form) abbrev_form;
18606 cur_attr.implicit_const = implicit_const;
18607 ++cur_abbrev->num_attrs;
18608 }
18609
18610 cur_abbrev->attrs =
18611 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
18612 cur_abbrev->num_attrs);
18613 memcpy (cur_abbrev->attrs, cur_attrs.data (),
18614 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
18615
18616 abbrev_table->add_abbrev (abbrev_number, cur_abbrev);
18617
18618 /* Get next abbreviation.
18619 Under Irix6 the abbreviations for a compilation unit are not
18620 always properly terminated with an abbrev number of 0.
18621 Exit loop if we encounter an abbreviation which we have
18622 already read (which means we are about to read the abbreviations
18623 for the next compile unit) or if the end of the abbreviation
18624 table is reached. */
18625 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
18626 break;
18627 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18628 abbrev_ptr += bytes_read;
18629 if (abbrev_table->lookup_abbrev (abbrev_number) != NULL)
18630 break;
18631 }
18632
18633 return abbrev_table;
18634 }
18635
18636 /* Returns nonzero if TAG represents a type that we might generate a partial
18637 symbol for. */
18638
18639 static int
18640 is_type_tag_for_partial (int tag)
18641 {
18642 switch (tag)
18643 {
18644 #if 0
18645 /* Some types that would be reasonable to generate partial symbols for,
18646 that we don't at present. */
18647 case DW_TAG_array_type:
18648 case DW_TAG_file_type:
18649 case DW_TAG_ptr_to_member_type:
18650 case DW_TAG_set_type:
18651 case DW_TAG_string_type:
18652 case DW_TAG_subroutine_type:
18653 #endif
18654 case DW_TAG_base_type:
18655 case DW_TAG_class_type:
18656 case DW_TAG_interface_type:
18657 case DW_TAG_enumeration_type:
18658 case DW_TAG_structure_type:
18659 case DW_TAG_subrange_type:
18660 case DW_TAG_typedef:
18661 case DW_TAG_union_type:
18662 return 1;
18663 default:
18664 return 0;
18665 }
18666 }
18667
18668 /* Load all DIEs that are interesting for partial symbols into memory. */
18669
18670 static struct partial_die_info *
18671 load_partial_dies (const struct die_reader_specs *reader,
18672 const gdb_byte *info_ptr, int building_psymtab)
18673 {
18674 struct dwarf2_cu *cu = reader->cu;
18675 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18676 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
18677 unsigned int bytes_read;
18678 unsigned int load_all = 0;
18679 int nesting_level = 1;
18680
18681 parent_die = NULL;
18682 last_die = NULL;
18683
18684 gdb_assert (cu->per_cu != NULL);
18685 if (cu->per_cu->load_all_dies)
18686 load_all = 1;
18687
18688 cu->partial_dies
18689 = htab_create_alloc_ex (cu->header.length / 12,
18690 partial_die_hash,
18691 partial_die_eq,
18692 NULL,
18693 &cu->comp_unit_obstack,
18694 hashtab_obstack_allocate,
18695 dummy_obstack_deallocate);
18696
18697 while (1)
18698 {
18699 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
18700
18701 /* A NULL abbrev means the end of a series of children. */
18702 if (abbrev == NULL)
18703 {
18704 if (--nesting_level == 0)
18705 return first_die;
18706
18707 info_ptr += bytes_read;
18708 last_die = parent_die;
18709 parent_die = parent_die->die_parent;
18710 continue;
18711 }
18712
18713 /* Check for template arguments. We never save these; if
18714 they're seen, we just mark the parent, and go on our way. */
18715 if (parent_die != NULL
18716 && cu->language == language_cplus
18717 && (abbrev->tag == DW_TAG_template_type_param
18718 || abbrev->tag == DW_TAG_template_value_param))
18719 {
18720 parent_die->has_template_arguments = 1;
18721
18722 if (!load_all)
18723 {
18724 /* We don't need a partial DIE for the template argument. */
18725 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18726 continue;
18727 }
18728 }
18729
18730 /* We only recurse into c++ subprograms looking for template arguments.
18731 Skip their other children. */
18732 if (!load_all
18733 && cu->language == language_cplus
18734 && parent_die != NULL
18735 && parent_die->tag == DW_TAG_subprogram)
18736 {
18737 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18738 continue;
18739 }
18740
18741 /* Check whether this DIE is interesting enough to save. Normally
18742 we would not be interested in members here, but there may be
18743 later variables referencing them via DW_AT_specification (for
18744 static members). */
18745 if (!load_all
18746 && !is_type_tag_for_partial (abbrev->tag)
18747 && abbrev->tag != DW_TAG_constant
18748 && abbrev->tag != DW_TAG_enumerator
18749 && abbrev->tag != DW_TAG_subprogram
18750 && abbrev->tag != DW_TAG_inlined_subroutine
18751 && abbrev->tag != DW_TAG_lexical_block
18752 && abbrev->tag != DW_TAG_variable
18753 && abbrev->tag != DW_TAG_namespace
18754 && abbrev->tag != DW_TAG_module
18755 && abbrev->tag != DW_TAG_member
18756 && abbrev->tag != DW_TAG_imported_unit
18757 && abbrev->tag != DW_TAG_imported_declaration)
18758 {
18759 /* Otherwise we skip to the next sibling, if any. */
18760 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18761 continue;
18762 }
18763
18764 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18765 abbrev);
18766
18767 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
18768
18769 /* This two-pass algorithm for processing partial symbols has a
18770 high cost in cache pressure. Thus, handle some simple cases
18771 here which cover the majority of C partial symbols. DIEs
18772 which neither have specification tags in them, nor could have
18773 specification tags elsewhere pointing at them, can simply be
18774 processed and discarded.
18775
18776 This segment is also optional; scan_partial_symbols and
18777 add_partial_symbol will handle these DIEs if we chain
18778 them in normally. When compilers which do not emit large
18779 quantities of duplicate debug information are more common,
18780 this code can probably be removed. */
18781
18782 /* Any complete simple types at the top level (pretty much all
18783 of them, for a language without namespaces), can be processed
18784 directly. */
18785 if (parent_die == NULL
18786 && pdi.has_specification == 0
18787 && pdi.is_declaration == 0
18788 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18789 || pdi.tag == DW_TAG_base_type
18790 || pdi.tag == DW_TAG_subrange_type))
18791 {
18792 if (building_psymtab && pdi.name != NULL)
18793 add_psymbol_to_list (pdi.name, false,
18794 VAR_DOMAIN, LOC_TYPEDEF, -1,
18795 psymbol_placement::STATIC,
18796 0, cu->language, objfile);
18797 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18798 continue;
18799 }
18800
18801 /* The exception for DW_TAG_typedef with has_children above is
18802 a workaround of GCC PR debug/47510. In the case of this complaint
18803 type_name_or_error will error on such types later.
18804
18805 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18806 it could not find the child DIEs referenced later, this is checked
18807 above. In correct DWARF DW_TAG_typedef should have no children. */
18808
18809 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
18810 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
18811 "- DIE at %s [in module %s]"),
18812 sect_offset_str (pdi.sect_off), objfile_name (objfile));
18813
18814 /* If we're at the second level, and we're an enumerator, and
18815 our parent has no specification (meaning possibly lives in a
18816 namespace elsewhere), then we can add the partial symbol now
18817 instead of queueing it. */
18818 if (pdi.tag == DW_TAG_enumerator
18819 && parent_die != NULL
18820 && parent_die->die_parent == NULL
18821 && parent_die->tag == DW_TAG_enumeration_type
18822 && parent_die->has_specification == 0)
18823 {
18824 if (pdi.name == NULL)
18825 complaint (_("malformed enumerator DIE ignored"));
18826 else if (building_psymtab)
18827 add_psymbol_to_list (pdi.name, false,
18828 VAR_DOMAIN, LOC_CONST, -1,
18829 cu->language == language_cplus
18830 ? psymbol_placement::GLOBAL
18831 : psymbol_placement::STATIC,
18832 0, cu->language, objfile);
18833
18834 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18835 continue;
18836 }
18837
18838 struct partial_die_info *part_die
18839 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
18840
18841 /* We'll save this DIE so link it in. */
18842 part_die->die_parent = parent_die;
18843 part_die->die_sibling = NULL;
18844 part_die->die_child = NULL;
18845
18846 if (last_die && last_die == parent_die)
18847 last_die->die_child = part_die;
18848 else if (last_die)
18849 last_die->die_sibling = part_die;
18850
18851 last_die = part_die;
18852
18853 if (first_die == NULL)
18854 first_die = part_die;
18855
18856 /* Maybe add the DIE to the hash table. Not all DIEs that we
18857 find interesting need to be in the hash table, because we
18858 also have the parent/sibling/child chains; only those that we
18859 might refer to by offset later during partial symbol reading.
18860
18861 For now this means things that might have be the target of a
18862 DW_AT_specification, DW_AT_abstract_origin, or
18863 DW_AT_extension. DW_AT_extension will refer only to
18864 namespaces; DW_AT_abstract_origin refers to functions (and
18865 many things under the function DIE, but we do not recurse
18866 into function DIEs during partial symbol reading) and
18867 possibly variables as well; DW_AT_specification refers to
18868 declarations. Declarations ought to have the DW_AT_declaration
18869 flag. It happens that GCC forgets to put it in sometimes, but
18870 only for functions, not for types.
18871
18872 Adding more things than necessary to the hash table is harmless
18873 except for the performance cost. Adding too few will result in
18874 wasted time in find_partial_die, when we reread the compilation
18875 unit with load_all_dies set. */
18876
18877 if (load_all
18878 || abbrev->tag == DW_TAG_constant
18879 || abbrev->tag == DW_TAG_subprogram
18880 || abbrev->tag == DW_TAG_variable
18881 || abbrev->tag == DW_TAG_namespace
18882 || part_die->is_declaration)
18883 {
18884 void **slot;
18885
18886 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
18887 to_underlying (part_die->sect_off),
18888 INSERT);
18889 *slot = part_die;
18890 }
18891
18892 /* For some DIEs we want to follow their children (if any). For C
18893 we have no reason to follow the children of structures; for other
18894 languages we have to, so that we can get at method physnames
18895 to infer fully qualified class names, for DW_AT_specification,
18896 and for C++ template arguments. For C++, we also look one level
18897 inside functions to find template arguments (if the name of the
18898 function does not already contain the template arguments).
18899
18900 For Ada and Fortran, we need to scan the children of subprograms
18901 and lexical blocks as well because these languages allow the
18902 definition of nested entities that could be interesting for the
18903 debugger, such as nested subprograms for instance. */
18904 if (last_die->has_children
18905 && (load_all
18906 || last_die->tag == DW_TAG_namespace
18907 || last_die->tag == DW_TAG_module
18908 || last_die->tag == DW_TAG_enumeration_type
18909 || (cu->language == language_cplus
18910 && last_die->tag == DW_TAG_subprogram
18911 && (last_die->name == NULL
18912 || strchr (last_die->name, '<') == NULL))
18913 || (cu->language != language_c
18914 && (last_die->tag == DW_TAG_class_type
18915 || last_die->tag == DW_TAG_interface_type
18916 || last_die->tag == DW_TAG_structure_type
18917 || last_die->tag == DW_TAG_union_type))
18918 || ((cu->language == language_ada
18919 || cu->language == language_fortran)
18920 && (last_die->tag == DW_TAG_subprogram
18921 || last_die->tag == DW_TAG_lexical_block))))
18922 {
18923 nesting_level++;
18924 parent_die = last_die;
18925 continue;
18926 }
18927
18928 /* Otherwise we skip to the next sibling, if any. */
18929 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
18930
18931 /* Back to the top, do it again. */
18932 }
18933 }
18934
18935 partial_die_info::partial_die_info (sect_offset sect_off_,
18936 struct abbrev_info *abbrev)
18937 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18938 {
18939 }
18940
18941 /* Read a minimal amount of information into the minimal die structure.
18942 INFO_PTR should point just after the initial uleb128 of a DIE. */
18943
18944 const gdb_byte *
18945 partial_die_info::read (const struct die_reader_specs *reader,
18946 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
18947 {
18948 struct dwarf2_cu *cu = reader->cu;
18949 struct dwarf2_per_objfile *dwarf2_per_objfile
18950 = cu->per_cu->dwarf2_per_objfile;
18951 unsigned int i;
18952 int has_low_pc_attr = 0;
18953 int has_high_pc_attr = 0;
18954 int high_pc_relative = 0;
18955
18956 for (i = 0; i < abbrev.num_attrs; ++i)
18957 {
18958 struct attribute attr;
18959
18960 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
18961
18962 /* Store the data if it is of an attribute we want to keep in a
18963 partial symbol table. */
18964 switch (attr.name)
18965 {
18966 case DW_AT_name:
18967 switch (tag)
18968 {
18969 case DW_TAG_compile_unit:
18970 case DW_TAG_partial_unit:
18971 case DW_TAG_type_unit:
18972 /* Compilation units have a DW_AT_name that is a filename, not
18973 a source language identifier. */
18974 case DW_TAG_enumeration_type:
18975 case DW_TAG_enumerator:
18976 /* These tags always have simple identifiers already; no need
18977 to canonicalize them. */
18978 name = DW_STRING (&attr);
18979 break;
18980 default:
18981 {
18982 struct objfile *objfile = dwarf2_per_objfile->objfile;
18983
18984 name
18985 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
18986 &objfile->per_bfd->storage_obstack);
18987 }
18988 break;
18989 }
18990 break;
18991 case DW_AT_linkage_name:
18992 case DW_AT_MIPS_linkage_name:
18993 /* Note that both forms of linkage name might appear. We
18994 assume they will be the same, and we only store the last
18995 one we see. */
18996 linkage_name = DW_STRING (&attr);
18997 break;
18998 case DW_AT_low_pc:
18999 has_low_pc_attr = 1;
19000 lowpc = attr_value_as_address (&attr);
19001 break;
19002 case DW_AT_high_pc:
19003 has_high_pc_attr = 1;
19004 highpc = attr_value_as_address (&attr);
19005 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
19006 high_pc_relative = 1;
19007 break;
19008 case DW_AT_location:
19009 /* Support the .debug_loc offsets. */
19010 if (attr_form_is_block (&attr))
19011 {
19012 d.locdesc = DW_BLOCK (&attr);
19013 }
19014 else if (attr_form_is_section_offset (&attr))
19015 {
19016 dwarf2_complex_location_expr_complaint ();
19017 }
19018 else
19019 {
19020 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
19021 "partial symbol information");
19022 }
19023 break;
19024 case DW_AT_external:
19025 is_external = DW_UNSND (&attr);
19026 break;
19027 case DW_AT_declaration:
19028 is_declaration = DW_UNSND (&attr);
19029 break;
19030 case DW_AT_type:
19031 has_type = 1;
19032 break;
19033 case DW_AT_abstract_origin:
19034 case DW_AT_specification:
19035 case DW_AT_extension:
19036 has_specification = 1;
19037 spec_offset = dwarf2_get_ref_die_offset (&attr);
19038 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
19039 || cu->per_cu->is_dwz);
19040 break;
19041 case DW_AT_sibling:
19042 /* Ignore absolute siblings, they might point outside of
19043 the current compile unit. */
19044 if (attr.form == DW_FORM_ref_addr)
19045 complaint (_("ignoring absolute DW_AT_sibling"));
19046 else
19047 {
19048 const gdb_byte *buffer = reader->buffer;
19049 sect_offset off = dwarf2_get_ref_die_offset (&attr);
19050 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
19051
19052 if (sibling_ptr < info_ptr)
19053 complaint (_("DW_AT_sibling points backwards"));
19054 else if (sibling_ptr > reader->buffer_end)
19055 dwarf2_section_buffer_overflow_complaint (reader->die_section);
19056 else
19057 sibling = sibling_ptr;
19058 }
19059 break;
19060 case DW_AT_byte_size:
19061 has_byte_size = 1;
19062 break;
19063 case DW_AT_const_value:
19064 has_const_value = 1;
19065 break;
19066 case DW_AT_calling_convention:
19067 /* DWARF doesn't provide a way to identify a program's source-level
19068 entry point. DW_AT_calling_convention attributes are only meant
19069 to describe functions' calling conventions.
19070
19071 However, because it's a necessary piece of information in
19072 Fortran, and before DWARF 4 DW_CC_program was the only
19073 piece of debugging information whose definition refers to
19074 a 'main program' at all, several compilers marked Fortran
19075 main programs with DW_CC_program --- even when those
19076 functions use the standard calling conventions.
19077
19078 Although DWARF now specifies a way to provide this
19079 information, we support this practice for backward
19080 compatibility. */
19081 if (DW_UNSND (&attr) == DW_CC_program
19082 && cu->language == language_fortran)
19083 main_subprogram = 1;
19084 break;
19085 case DW_AT_inline:
19086 if (DW_UNSND (&attr) == DW_INL_inlined
19087 || DW_UNSND (&attr) == DW_INL_declared_inlined)
19088 may_be_inlined = 1;
19089 break;
19090
19091 case DW_AT_import:
19092 if (tag == DW_TAG_imported_unit)
19093 {
19094 d.sect_off = dwarf2_get_ref_die_offset (&attr);
19095 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
19096 || cu->per_cu->is_dwz);
19097 }
19098 break;
19099
19100 case DW_AT_main_subprogram:
19101 main_subprogram = DW_UNSND (&attr);
19102 break;
19103
19104 case DW_AT_ranges:
19105 {
19106 /* It would be nice to reuse dwarf2_get_pc_bounds here,
19107 but that requires a full DIE, so instead we just
19108 reimplement it. */
19109 int need_ranges_base = tag != DW_TAG_compile_unit;
19110 unsigned int ranges_offset = (DW_UNSND (&attr)
19111 + (need_ranges_base
19112 ? cu->ranges_base
19113 : 0));
19114
19115 /* Value of the DW_AT_ranges attribute is the offset in the
19116 .debug_ranges section. */
19117 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
19118 nullptr))
19119 has_pc_info = 1;
19120 }
19121 break;
19122
19123 default:
19124 break;
19125 }
19126 }
19127
19128 /* For Ada, if both the name and the linkage name appear, we prefer
19129 the latter. This lets "catch exception" work better, regardless
19130 of the order in which the name and linkage name were emitted.
19131 Really, though, this is just a workaround for the fact that gdb
19132 doesn't store both the name and the linkage name. */
19133 if (cu->language == language_ada && linkage_name != nullptr)
19134 name = linkage_name;
19135
19136 if (high_pc_relative)
19137 highpc += lowpc;
19138
19139 if (has_low_pc_attr && has_high_pc_attr)
19140 {
19141 /* When using the GNU linker, .gnu.linkonce. sections are used to
19142 eliminate duplicate copies of functions and vtables and such.
19143 The linker will arbitrarily choose one and discard the others.
19144 The AT_*_pc values for such functions refer to local labels in
19145 these sections. If the section from that file was discarded, the
19146 labels are not in the output, so the relocs get a value of 0.
19147 If this is a discarded function, mark the pc bounds as invalid,
19148 so that GDB will ignore it. */
19149 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
19150 {
19151 struct objfile *objfile = dwarf2_per_objfile->objfile;
19152 struct gdbarch *gdbarch = get_objfile_arch (objfile);
19153
19154 complaint (_("DW_AT_low_pc %s is zero "
19155 "for DIE at %s [in module %s]"),
19156 paddress (gdbarch, lowpc),
19157 sect_offset_str (sect_off),
19158 objfile_name (objfile));
19159 }
19160 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
19161 else if (lowpc >= highpc)
19162 {
19163 struct objfile *objfile = dwarf2_per_objfile->objfile;
19164 struct gdbarch *gdbarch = get_objfile_arch (objfile);
19165
19166 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
19167 "for DIE at %s [in module %s]"),
19168 paddress (gdbarch, lowpc),
19169 paddress (gdbarch, highpc),
19170 sect_offset_str (sect_off),
19171 objfile_name (objfile));
19172 }
19173 else
19174 has_pc_info = 1;
19175 }
19176
19177 return info_ptr;
19178 }
19179
19180 /* Find a cached partial DIE at OFFSET in CU. */
19181
19182 struct partial_die_info *
19183 dwarf2_cu::find_partial_die (sect_offset sect_off)
19184 {
19185 struct partial_die_info *lookup_die = NULL;
19186 struct partial_die_info part_die (sect_off);
19187
19188 lookup_die = ((struct partial_die_info *)
19189 htab_find_with_hash (partial_dies, &part_die,
19190 to_underlying (sect_off)));
19191
19192 return lookup_die;
19193 }
19194
19195 /* Find a partial DIE at OFFSET, which may or may not be in CU,
19196 except in the case of .debug_types DIEs which do not reference
19197 outside their CU (they do however referencing other types via
19198 DW_FORM_ref_sig8). */
19199
19200 static const struct cu_partial_die_info
19201 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
19202 {
19203 struct dwarf2_per_objfile *dwarf2_per_objfile
19204 = cu->per_cu->dwarf2_per_objfile;
19205 struct objfile *objfile = dwarf2_per_objfile->objfile;
19206 struct dwarf2_per_cu_data *per_cu = NULL;
19207 struct partial_die_info *pd = NULL;
19208
19209 if (offset_in_dwz == cu->per_cu->is_dwz
19210 && offset_in_cu_p (&cu->header, sect_off))
19211 {
19212 pd = cu->find_partial_die (sect_off);
19213 if (pd != NULL)
19214 return { cu, pd };
19215 /* We missed recording what we needed.
19216 Load all dies and try again. */
19217 per_cu = cu->per_cu;
19218 }
19219 else
19220 {
19221 /* TUs don't reference other CUs/TUs (except via type signatures). */
19222 if (cu->per_cu->is_debug_types)
19223 {
19224 error (_("Dwarf Error: Type Unit at offset %s contains"
19225 " external reference to offset %s [in module %s].\n"),
19226 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
19227 bfd_get_filename (objfile->obfd));
19228 }
19229 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
19230 dwarf2_per_objfile);
19231
19232 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
19233 load_partial_comp_unit (per_cu);
19234
19235 per_cu->cu->last_used = 0;
19236 pd = per_cu->cu->find_partial_die (sect_off);
19237 }
19238
19239 /* If we didn't find it, and not all dies have been loaded,
19240 load them all and try again. */
19241
19242 if (pd == NULL && per_cu->load_all_dies == 0)
19243 {
19244 per_cu->load_all_dies = 1;
19245
19246 /* This is nasty. When we reread the DIEs, somewhere up the call chain
19247 THIS_CU->cu may already be in use. So we can't just free it and
19248 replace its DIEs with the ones we read in. Instead, we leave those
19249 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
19250 and clobber THIS_CU->cu->partial_dies with the hash table for the new
19251 set. */
19252 load_partial_comp_unit (per_cu);
19253
19254 pd = per_cu->cu->find_partial_die (sect_off);
19255 }
19256
19257 if (pd == NULL)
19258 internal_error (__FILE__, __LINE__,
19259 _("could not find partial DIE %s "
19260 "in cache [from module %s]\n"),
19261 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
19262 return { per_cu->cu, pd };
19263 }
19264
19265 /* See if we can figure out if the class lives in a namespace. We do
19266 this by looking for a member function; its demangled name will
19267 contain namespace info, if there is any. */
19268
19269 static void
19270 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
19271 struct dwarf2_cu *cu)
19272 {
19273 /* NOTE: carlton/2003-10-07: Getting the info this way changes
19274 what template types look like, because the demangler
19275 frequently doesn't give the same name as the debug info. We
19276 could fix this by only using the demangled name to get the
19277 prefix (but see comment in read_structure_type). */
19278
19279 struct partial_die_info *real_pdi;
19280 struct partial_die_info *child_pdi;
19281
19282 /* If this DIE (this DIE's specification, if any) has a parent, then
19283 we should not do this. We'll prepend the parent's fully qualified
19284 name when we create the partial symbol. */
19285
19286 real_pdi = struct_pdi;
19287 while (real_pdi->has_specification)
19288 {
19289 auto res = find_partial_die (real_pdi->spec_offset,
19290 real_pdi->spec_is_dwz, cu);
19291 real_pdi = res.pdi;
19292 cu = res.cu;
19293 }
19294
19295 if (real_pdi->die_parent != NULL)
19296 return;
19297
19298 for (child_pdi = struct_pdi->die_child;
19299 child_pdi != NULL;
19300 child_pdi = child_pdi->die_sibling)
19301 {
19302 if (child_pdi->tag == DW_TAG_subprogram
19303 && child_pdi->linkage_name != NULL)
19304 {
19305 gdb::unique_xmalloc_ptr<char> actual_class_name
19306 (language_class_name_from_physname (cu->language_defn,
19307 child_pdi->linkage_name));
19308 if (actual_class_name != NULL)
19309 {
19310 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19311 struct_pdi->name
19312 = obstack_strdup (&objfile->per_bfd->storage_obstack,
19313 actual_class_name.get ());
19314 }
19315 break;
19316 }
19317 }
19318 }
19319
19320 void
19321 partial_die_info::fixup (struct dwarf2_cu *cu)
19322 {
19323 /* Once we've fixed up a die, there's no point in doing so again.
19324 This also avoids a memory leak if we were to call
19325 guess_partial_die_structure_name multiple times. */
19326 if (fixup_called)
19327 return;
19328
19329 /* If we found a reference attribute and the DIE has no name, try
19330 to find a name in the referred to DIE. */
19331
19332 if (name == NULL && has_specification)
19333 {
19334 struct partial_die_info *spec_die;
19335
19336 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
19337 spec_die = res.pdi;
19338 cu = res.cu;
19339
19340 spec_die->fixup (cu);
19341
19342 if (spec_die->name)
19343 {
19344 name = spec_die->name;
19345
19346 /* Copy DW_AT_external attribute if it is set. */
19347 if (spec_die->is_external)
19348 is_external = spec_die->is_external;
19349 }
19350 }
19351
19352 /* Set default names for some unnamed DIEs. */
19353
19354 if (name == NULL && tag == DW_TAG_namespace)
19355 name = CP_ANONYMOUS_NAMESPACE_STR;
19356
19357 /* If there is no parent die to provide a namespace, and there are
19358 children, see if we can determine the namespace from their linkage
19359 name. */
19360 if (cu->language == language_cplus
19361 && !cu->per_cu->dwarf2_per_objfile->types.empty ()
19362 && die_parent == NULL
19363 && has_children
19364 && (tag == DW_TAG_class_type
19365 || tag == DW_TAG_structure_type
19366 || tag == DW_TAG_union_type))
19367 guess_partial_die_structure_name (this, cu);
19368
19369 /* GCC might emit a nameless struct or union that has a linkage
19370 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
19371 if (name == NULL
19372 && (tag == DW_TAG_class_type
19373 || tag == DW_TAG_interface_type
19374 || tag == DW_TAG_structure_type
19375 || tag == DW_TAG_union_type)
19376 && linkage_name != NULL)
19377 {
19378 gdb::unique_xmalloc_ptr<char> demangled
19379 (gdb_demangle (linkage_name, DMGL_TYPES));
19380 if (demangled != nullptr)
19381 {
19382 const char *base;
19383
19384 /* Strip any leading namespaces/classes, keep only the base name.
19385 DW_AT_name for named DIEs does not contain the prefixes. */
19386 base = strrchr (demangled.get (), ':');
19387 if (base && base > demangled.get () && base[-1] == ':')
19388 base++;
19389 else
19390 base = demangled.get ();
19391
19392 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19393 name = obstack_strdup (&objfile->per_bfd->storage_obstack, base);
19394 }
19395 }
19396
19397 fixup_called = 1;
19398 }
19399
19400 /* Read an attribute value described by an attribute form. */
19401
19402 static const gdb_byte *
19403 read_attribute_value (const struct die_reader_specs *reader,
19404 struct attribute *attr, unsigned form,
19405 LONGEST implicit_const, const gdb_byte *info_ptr)
19406 {
19407 struct dwarf2_cu *cu = reader->cu;
19408 struct dwarf2_per_objfile *dwarf2_per_objfile
19409 = cu->per_cu->dwarf2_per_objfile;
19410 struct objfile *objfile = dwarf2_per_objfile->objfile;
19411 struct gdbarch *gdbarch = get_objfile_arch (objfile);
19412 bfd *abfd = reader->abfd;
19413 struct comp_unit_head *cu_header = &cu->header;
19414 unsigned int bytes_read;
19415 struct dwarf_block *blk;
19416
19417 attr->form = (enum dwarf_form) form;
19418 switch (form)
19419 {
19420 case DW_FORM_ref_addr:
19421 if (cu->header.version == 2)
19422 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
19423 else
19424 DW_UNSND (attr) = read_offset (abfd, info_ptr,
19425 &cu->header, &bytes_read);
19426 info_ptr += bytes_read;
19427 break;
19428 case DW_FORM_GNU_ref_alt:
19429 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19430 info_ptr += bytes_read;
19431 break;
19432 case DW_FORM_addr:
19433 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
19434 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
19435 info_ptr += bytes_read;
19436 break;
19437 case DW_FORM_block2:
19438 blk = dwarf_alloc_block (cu);
19439 blk->size = read_2_bytes (abfd, info_ptr);
19440 info_ptr += 2;
19441 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19442 info_ptr += blk->size;
19443 DW_BLOCK (attr) = blk;
19444 break;
19445 case DW_FORM_block4:
19446 blk = dwarf_alloc_block (cu);
19447 blk->size = read_4_bytes (abfd, info_ptr);
19448 info_ptr += 4;
19449 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19450 info_ptr += blk->size;
19451 DW_BLOCK (attr) = blk;
19452 break;
19453 case DW_FORM_data2:
19454 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
19455 info_ptr += 2;
19456 break;
19457 case DW_FORM_data4:
19458 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
19459 info_ptr += 4;
19460 break;
19461 case DW_FORM_data8:
19462 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
19463 info_ptr += 8;
19464 break;
19465 case DW_FORM_data16:
19466 blk = dwarf_alloc_block (cu);
19467 blk->size = 16;
19468 blk->data = read_n_bytes (abfd, info_ptr, 16);
19469 info_ptr += 16;
19470 DW_BLOCK (attr) = blk;
19471 break;
19472 case DW_FORM_sec_offset:
19473 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19474 info_ptr += bytes_read;
19475 break;
19476 case DW_FORM_string:
19477 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
19478 DW_STRING_IS_CANONICAL (attr) = 0;
19479 info_ptr += bytes_read;
19480 break;
19481 case DW_FORM_strp:
19482 if (!cu->per_cu->is_dwz)
19483 {
19484 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
19485 abfd, info_ptr, cu_header,
19486 &bytes_read);
19487 DW_STRING_IS_CANONICAL (attr) = 0;
19488 info_ptr += bytes_read;
19489 break;
19490 }
19491 /* FALLTHROUGH */
19492 case DW_FORM_line_strp:
19493 if (!cu->per_cu->is_dwz)
19494 {
19495 DW_STRING (attr) = read_indirect_line_string (dwarf2_per_objfile,
19496 abfd, info_ptr,
19497 cu_header, &bytes_read);
19498 DW_STRING_IS_CANONICAL (attr) = 0;
19499 info_ptr += bytes_read;
19500 break;
19501 }
19502 /* FALLTHROUGH */
19503 case DW_FORM_GNU_strp_alt:
19504 {
19505 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
19506 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
19507 &bytes_read);
19508
19509 DW_STRING (attr) = read_indirect_string_from_dwz (objfile,
19510 dwz, str_offset);
19511 DW_STRING_IS_CANONICAL (attr) = 0;
19512 info_ptr += bytes_read;
19513 }
19514 break;
19515 case DW_FORM_exprloc:
19516 case DW_FORM_block:
19517 blk = dwarf_alloc_block (cu);
19518 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19519 info_ptr += bytes_read;
19520 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19521 info_ptr += blk->size;
19522 DW_BLOCK (attr) = blk;
19523 break;
19524 case DW_FORM_block1:
19525 blk = dwarf_alloc_block (cu);
19526 blk->size = read_1_byte (abfd, info_ptr);
19527 info_ptr += 1;
19528 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19529 info_ptr += blk->size;
19530 DW_BLOCK (attr) = blk;
19531 break;
19532 case DW_FORM_data1:
19533 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19534 info_ptr += 1;
19535 break;
19536 case DW_FORM_flag:
19537 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19538 info_ptr += 1;
19539 break;
19540 case DW_FORM_flag_present:
19541 DW_UNSND (attr) = 1;
19542 break;
19543 case DW_FORM_sdata:
19544 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19545 info_ptr += bytes_read;
19546 break;
19547 case DW_FORM_udata:
19548 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19549 info_ptr += bytes_read;
19550 break;
19551 case DW_FORM_ref1:
19552 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19553 + read_1_byte (abfd, info_ptr));
19554 info_ptr += 1;
19555 break;
19556 case DW_FORM_ref2:
19557 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19558 + read_2_bytes (abfd, info_ptr));
19559 info_ptr += 2;
19560 break;
19561 case DW_FORM_ref4:
19562 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19563 + read_4_bytes (abfd, info_ptr));
19564 info_ptr += 4;
19565 break;
19566 case DW_FORM_ref8:
19567 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19568 + read_8_bytes (abfd, info_ptr));
19569 info_ptr += 8;
19570 break;
19571 case DW_FORM_ref_sig8:
19572 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
19573 info_ptr += 8;
19574 break;
19575 case DW_FORM_ref_udata:
19576 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19577 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
19578 info_ptr += bytes_read;
19579 break;
19580 case DW_FORM_indirect:
19581 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19582 info_ptr += bytes_read;
19583 if (form == DW_FORM_implicit_const)
19584 {
19585 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19586 info_ptr += bytes_read;
19587 }
19588 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19589 info_ptr);
19590 break;
19591 case DW_FORM_implicit_const:
19592 DW_SND (attr) = implicit_const;
19593 break;
19594 case DW_FORM_addrx:
19595 case DW_FORM_GNU_addr_index:
19596 if (reader->dwo_file == NULL)
19597 {
19598 /* For now flag a hard error.
19599 Later we can turn this into a complaint. */
19600 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19601 dwarf_form_name (form),
19602 bfd_get_filename (abfd));
19603 }
19604 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
19605 info_ptr += bytes_read;
19606 break;
19607 case DW_FORM_strx:
19608 case DW_FORM_strx1:
19609 case DW_FORM_strx2:
19610 case DW_FORM_strx3:
19611 case DW_FORM_strx4:
19612 case DW_FORM_GNU_str_index:
19613 if (reader->dwo_file == NULL)
19614 {
19615 /* For now flag a hard error.
19616 Later we can turn this into a complaint if warranted. */
19617 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19618 dwarf_form_name (form),
19619 bfd_get_filename (abfd));
19620 }
19621 {
19622 ULONGEST str_index;
19623 if (form == DW_FORM_strx1)
19624 {
19625 str_index = read_1_byte (abfd, info_ptr);
19626 info_ptr += 1;
19627 }
19628 else if (form == DW_FORM_strx2)
19629 {
19630 str_index = read_2_bytes (abfd, info_ptr);
19631 info_ptr += 2;
19632 }
19633 else if (form == DW_FORM_strx3)
19634 {
19635 str_index = read_3_bytes (abfd, info_ptr);
19636 info_ptr += 3;
19637 }
19638 else if (form == DW_FORM_strx4)
19639 {
19640 str_index = read_4_bytes (abfd, info_ptr);
19641 info_ptr += 4;
19642 }
19643 else
19644 {
19645 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19646 info_ptr += bytes_read;
19647 }
19648 DW_STRING (attr) = read_str_index (reader, str_index);
19649 DW_STRING_IS_CANONICAL (attr) = 0;
19650 }
19651 break;
19652 default:
19653 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
19654 dwarf_form_name (form),
19655 bfd_get_filename (abfd));
19656 }
19657
19658 /* Super hack. */
19659 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
19660 attr->form = DW_FORM_GNU_ref_alt;
19661
19662 /* We have seen instances where the compiler tried to emit a byte
19663 size attribute of -1 which ended up being encoded as an unsigned
19664 0xffffffff. Although 0xffffffff is technically a valid size value,
19665 an object of this size seems pretty unlikely so we can relatively
19666 safely treat these cases as if the size attribute was invalid and
19667 treat them as zero by default. */
19668 if (attr->name == DW_AT_byte_size
19669 && form == DW_FORM_data4
19670 && DW_UNSND (attr) >= 0xffffffff)
19671 {
19672 complaint
19673 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19674 hex_string (DW_UNSND (attr)));
19675 DW_UNSND (attr) = 0;
19676 }
19677
19678 return info_ptr;
19679 }
19680
19681 /* Read an attribute described by an abbreviated attribute. */
19682
19683 static const gdb_byte *
19684 read_attribute (const struct die_reader_specs *reader,
19685 struct attribute *attr, struct attr_abbrev *abbrev,
19686 const gdb_byte *info_ptr)
19687 {
19688 attr->name = abbrev->name;
19689 return read_attribute_value (reader, attr, abbrev->form,
19690 abbrev->implicit_const, info_ptr);
19691 }
19692
19693 /* Read dwarf information from a buffer. */
19694
19695 static unsigned int
19696 read_1_byte (bfd *abfd, const gdb_byte *buf)
19697 {
19698 return bfd_get_8 (abfd, buf);
19699 }
19700
19701 static int
19702 read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
19703 {
19704 return bfd_get_signed_8 (abfd, buf);
19705 }
19706
19707 static unsigned int
19708 read_2_bytes (bfd *abfd, const gdb_byte *buf)
19709 {
19710 return bfd_get_16 (abfd, buf);
19711 }
19712
19713 static int
19714 read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
19715 {
19716 return bfd_get_signed_16 (abfd, buf);
19717 }
19718
19719 static unsigned int
19720 read_3_bytes (bfd *abfd, const gdb_byte *buf)
19721 {
19722 unsigned int result = 0;
19723 for (int i = 0; i < 3; ++i)
19724 {
19725 unsigned char byte = bfd_get_8 (abfd, buf);
19726 buf++;
19727 result |= ((unsigned int) byte << (i * 8));
19728 }
19729 return result;
19730 }
19731
19732 static unsigned int
19733 read_4_bytes (bfd *abfd, const gdb_byte *buf)
19734 {
19735 return bfd_get_32 (abfd, buf);
19736 }
19737
19738 static int
19739 read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
19740 {
19741 return bfd_get_signed_32 (abfd, buf);
19742 }
19743
19744 static ULONGEST
19745 read_8_bytes (bfd *abfd, const gdb_byte *buf)
19746 {
19747 return bfd_get_64 (abfd, buf);
19748 }
19749
19750 static CORE_ADDR
19751 read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
19752 unsigned int *bytes_read)
19753 {
19754 struct comp_unit_head *cu_header = &cu->header;
19755 CORE_ADDR retval = 0;
19756
19757 if (cu_header->signed_addr_p)
19758 {
19759 switch (cu_header->addr_size)
19760 {
19761 case 2:
19762 retval = bfd_get_signed_16 (abfd, buf);
19763 break;
19764 case 4:
19765 retval = bfd_get_signed_32 (abfd, buf);
19766 break;
19767 case 8:
19768 retval = bfd_get_signed_64 (abfd, buf);
19769 break;
19770 default:
19771 internal_error (__FILE__, __LINE__,
19772 _("read_address: bad switch, signed [in module %s]"),
19773 bfd_get_filename (abfd));
19774 }
19775 }
19776 else
19777 {
19778 switch (cu_header->addr_size)
19779 {
19780 case 2:
19781 retval = bfd_get_16 (abfd, buf);
19782 break;
19783 case 4:
19784 retval = bfd_get_32 (abfd, buf);
19785 break;
19786 case 8:
19787 retval = bfd_get_64 (abfd, buf);
19788 break;
19789 default:
19790 internal_error (__FILE__, __LINE__,
19791 _("read_address: bad switch, "
19792 "unsigned [in module %s]"),
19793 bfd_get_filename (abfd));
19794 }
19795 }
19796
19797 *bytes_read = cu_header->addr_size;
19798 return retval;
19799 }
19800
19801 /* Read the initial length from a section. The (draft) DWARF 3
19802 specification allows the initial length to take up either 4 bytes
19803 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
19804 bytes describe the length and all offsets will be 8 bytes in length
19805 instead of 4.
19806
19807 An older, non-standard 64-bit format is also handled by this
19808 function. The older format in question stores the initial length
19809 as an 8-byte quantity without an escape value. Lengths greater
19810 than 2^32 aren't very common which means that the initial 4 bytes
19811 is almost always zero. Since a length value of zero doesn't make
19812 sense for the 32-bit format, this initial zero can be considered to
19813 be an escape value which indicates the presence of the older 64-bit
19814 format. As written, the code can't detect (old format) lengths
19815 greater than 4GB. If it becomes necessary to handle lengths
19816 somewhat larger than 4GB, we could allow other small values (such
19817 as the non-sensical values of 1, 2, and 3) to also be used as
19818 escape values indicating the presence of the old format.
19819
19820 The value returned via bytes_read should be used to increment the
19821 relevant pointer after calling read_initial_length().
19822
19823 [ Note: read_initial_length() and read_offset() are based on the
19824 document entitled "DWARF Debugging Information Format", revision
19825 3, draft 8, dated November 19, 2001. This document was obtained
19826 from:
19827
19828 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
19829
19830 This document is only a draft and is subject to change. (So beware.)
19831
19832 Details regarding the older, non-standard 64-bit format were
19833 determined empirically by examining 64-bit ELF files produced by
19834 the SGI toolchain on an IRIX 6.5 machine.
19835
19836 - Kevin, July 16, 2002
19837 ] */
19838
19839 static LONGEST
19840 read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
19841 {
19842 LONGEST length = bfd_get_32 (abfd, buf);
19843
19844 if (length == 0xffffffff)
19845 {
19846 length = bfd_get_64 (abfd, buf + 4);
19847 *bytes_read = 12;
19848 }
19849 else if (length == 0)
19850 {
19851 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
19852 length = bfd_get_64 (abfd, buf);
19853 *bytes_read = 8;
19854 }
19855 else
19856 {
19857 *bytes_read = 4;
19858 }
19859
19860 return length;
19861 }
19862
19863 /* Cover function for read_initial_length.
19864 Returns the length of the object at BUF, and stores the size of the
19865 initial length in *BYTES_READ and stores the size that offsets will be in
19866 *OFFSET_SIZE.
19867 If the initial length size is not equivalent to that specified in
19868 CU_HEADER then issue a complaint.
19869 This is useful when reading non-comp-unit headers. */
19870
19871 static LONGEST
19872 read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
19873 const struct comp_unit_head *cu_header,
19874 unsigned int *bytes_read,
19875 unsigned int *offset_size)
19876 {
19877 LONGEST length = read_initial_length (abfd, buf, bytes_read);
19878
19879 gdb_assert (cu_header->initial_length_size == 4
19880 || cu_header->initial_length_size == 8
19881 || cu_header->initial_length_size == 12);
19882
19883 if (cu_header->initial_length_size != *bytes_read)
19884 complaint (_("intermixed 32-bit and 64-bit DWARF sections"));
19885
19886 *offset_size = (*bytes_read == 4) ? 4 : 8;
19887 return length;
19888 }
19889
19890 /* Read an offset from the data stream. The size of the offset is
19891 given by cu_header->offset_size. */
19892
19893 static LONGEST
19894 read_offset (bfd *abfd, const gdb_byte *buf,
19895 const struct comp_unit_head *cu_header,
19896 unsigned int *bytes_read)
19897 {
19898 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
19899
19900 *bytes_read = cu_header->offset_size;
19901 return offset;
19902 }
19903
19904 /* Read an offset from the data stream. */
19905
19906 static LONGEST
19907 read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
19908 {
19909 LONGEST retval = 0;
19910
19911 switch (offset_size)
19912 {
19913 case 4:
19914 retval = bfd_get_32 (abfd, buf);
19915 break;
19916 case 8:
19917 retval = bfd_get_64 (abfd, buf);
19918 break;
19919 default:
19920 internal_error (__FILE__, __LINE__,
19921 _("read_offset_1: bad switch [in module %s]"),
19922 bfd_get_filename (abfd));
19923 }
19924
19925 return retval;
19926 }
19927
19928 static const gdb_byte *
19929 read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
19930 {
19931 /* If the size of a host char is 8 bits, we can return a pointer
19932 to the buffer, otherwise we have to copy the data to a buffer
19933 allocated on the temporary obstack. */
19934 gdb_assert (HOST_CHAR_BIT == 8);
19935 return buf;
19936 }
19937
19938 static const char *
19939 read_direct_string (bfd *abfd, const gdb_byte *buf,
19940 unsigned int *bytes_read_ptr)
19941 {
19942 /* If the size of a host char is 8 bits, we can return a pointer
19943 to the string, otherwise we have to copy the string to a buffer
19944 allocated on the temporary obstack. */
19945 gdb_assert (HOST_CHAR_BIT == 8);
19946 if (*buf == '\0')
19947 {
19948 *bytes_read_ptr = 1;
19949 return NULL;
19950 }
19951 *bytes_read_ptr = strlen ((const char *) buf) + 1;
19952 return (const char *) buf;
19953 }
19954
19955 /* Return pointer to string at section SECT offset STR_OFFSET with error
19956 reporting strings FORM_NAME and SECT_NAME. */
19957
19958 static const char *
19959 read_indirect_string_at_offset_from (struct objfile *objfile,
19960 bfd *abfd, LONGEST str_offset,
19961 struct dwarf2_section_info *sect,
19962 const char *form_name,
19963 const char *sect_name)
19964 {
19965 dwarf2_read_section (objfile, sect);
19966 if (sect->buffer == NULL)
19967 error (_("%s used without %s section [in module %s]"),
19968 form_name, sect_name, bfd_get_filename (abfd));
19969 if (str_offset >= sect->size)
19970 error (_("%s pointing outside of %s section [in module %s]"),
19971 form_name, sect_name, bfd_get_filename (abfd));
19972 gdb_assert (HOST_CHAR_BIT == 8);
19973 if (sect->buffer[str_offset] == '\0')
19974 return NULL;
19975 return (const char *) (sect->buffer + str_offset);
19976 }
19977
19978 /* Return pointer to string at .debug_str offset STR_OFFSET. */
19979
19980 static const char *
19981 read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19982 bfd *abfd, LONGEST str_offset)
19983 {
19984 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19985 abfd, str_offset,
19986 &dwarf2_per_objfile->str,
19987 "DW_FORM_strp", ".debug_str");
19988 }
19989
19990 /* Return pointer to string at .debug_line_str offset STR_OFFSET. */
19991
19992 static const char *
19993 read_indirect_line_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19994 bfd *abfd, LONGEST str_offset)
19995 {
19996 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19997 abfd, str_offset,
19998 &dwarf2_per_objfile->line_str,
19999 "DW_FORM_line_strp",
20000 ".debug_line_str");
20001 }
20002
20003 /* Read a string at offset STR_OFFSET in the .debug_str section from
20004 the .dwz file DWZ. Throw an error if the offset is too large. If
20005 the string consists of a single NUL byte, return NULL; otherwise
20006 return a pointer to the string. */
20007
20008 static const char *
20009 read_indirect_string_from_dwz (struct objfile *objfile, struct dwz_file *dwz,
20010 LONGEST str_offset)
20011 {
20012 dwarf2_read_section (objfile, &dwz->str);
20013
20014 if (dwz->str.buffer == NULL)
20015 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
20016 "section [in module %s]"),
20017 bfd_get_filename (dwz->dwz_bfd.get ()));
20018 if (str_offset >= dwz->str.size)
20019 error (_("DW_FORM_GNU_strp_alt pointing outside of "
20020 ".debug_str section [in module %s]"),
20021 bfd_get_filename (dwz->dwz_bfd.get ()));
20022 gdb_assert (HOST_CHAR_BIT == 8);
20023 if (dwz->str.buffer[str_offset] == '\0')
20024 return NULL;
20025 return (const char *) (dwz->str.buffer + str_offset);
20026 }
20027
20028 /* Return pointer to string at .debug_str offset as read from BUF.
20029 BUF is assumed to be in a compilation unit described by CU_HEADER.
20030 Return *BYTES_READ_PTR count of bytes read from BUF. */
20031
20032 static const char *
20033 read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
20034 const gdb_byte *buf,
20035 const struct comp_unit_head *cu_header,
20036 unsigned int *bytes_read_ptr)
20037 {
20038 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
20039
20040 return read_indirect_string_at_offset (dwarf2_per_objfile, abfd, str_offset);
20041 }
20042
20043 /* Return pointer to string at .debug_line_str offset as read from BUF.
20044 BUF is assumed to be in a compilation unit described by CU_HEADER.
20045 Return *BYTES_READ_PTR count of bytes read from BUF. */
20046
20047 static const char *
20048 read_indirect_line_string (struct dwarf2_per_objfile *dwarf2_per_objfile,
20049 bfd *abfd, const gdb_byte *buf,
20050 const struct comp_unit_head *cu_header,
20051 unsigned int *bytes_read_ptr)
20052 {
20053 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
20054
20055 return read_indirect_line_string_at_offset (dwarf2_per_objfile, abfd,
20056 str_offset);
20057 }
20058
20059 ULONGEST
20060 read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
20061 unsigned int *bytes_read_ptr)
20062 {
20063 ULONGEST result;
20064 unsigned int num_read;
20065 int shift;
20066 unsigned char byte;
20067
20068 result = 0;
20069 shift = 0;
20070 num_read = 0;
20071 while (1)
20072 {
20073 byte = bfd_get_8 (abfd, buf);
20074 buf++;
20075 num_read++;
20076 result |= ((ULONGEST) (byte & 127) << shift);
20077 if ((byte & 128) == 0)
20078 {
20079 break;
20080 }
20081 shift += 7;
20082 }
20083 *bytes_read_ptr = num_read;
20084 return result;
20085 }
20086
20087 static LONGEST
20088 read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
20089 unsigned int *bytes_read_ptr)
20090 {
20091 ULONGEST result;
20092 int shift, num_read;
20093 unsigned char byte;
20094
20095 result = 0;
20096 shift = 0;
20097 num_read = 0;
20098 while (1)
20099 {
20100 byte = bfd_get_8 (abfd, buf);
20101 buf++;
20102 num_read++;
20103 result |= ((ULONGEST) (byte & 127) << shift);
20104 shift += 7;
20105 if ((byte & 128) == 0)
20106 {
20107 break;
20108 }
20109 }
20110 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
20111 result |= -(((ULONGEST) 1) << shift);
20112 *bytes_read_ptr = num_read;
20113 return result;
20114 }
20115
20116 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
20117 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
20118 ADDR_SIZE is the size of addresses from the CU header. */
20119
20120 static CORE_ADDR
20121 read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
20122 unsigned int addr_index, ULONGEST addr_base, int addr_size)
20123 {
20124 struct objfile *objfile = dwarf2_per_objfile->objfile;
20125 bfd *abfd = objfile->obfd;
20126 const gdb_byte *info_ptr;
20127
20128 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
20129 if (dwarf2_per_objfile->addr.buffer == NULL)
20130 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
20131 objfile_name (objfile));
20132 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
20133 error (_("DW_FORM_addr_index pointing outside of "
20134 ".debug_addr section [in module %s]"),
20135 objfile_name (objfile));
20136 info_ptr = (dwarf2_per_objfile->addr.buffer
20137 + addr_base + addr_index * addr_size);
20138 if (addr_size == 4)
20139 return bfd_get_32 (abfd, info_ptr);
20140 else
20141 return bfd_get_64 (abfd, info_ptr);
20142 }
20143
20144 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
20145
20146 static CORE_ADDR
20147 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
20148 {
20149 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
20150 cu->addr_base, cu->header.addr_size);
20151 }
20152
20153 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
20154
20155 static CORE_ADDR
20156 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
20157 unsigned int *bytes_read)
20158 {
20159 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
20160 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
20161
20162 return read_addr_index (cu, addr_index);
20163 }
20164
20165 /* Data structure to pass results from dwarf2_read_addr_index_reader
20166 back to dwarf2_read_addr_index. */
20167
20168 struct dwarf2_read_addr_index_data
20169 {
20170 ULONGEST addr_base;
20171 int addr_size;
20172 };
20173
20174 /* die_reader_func for dwarf2_read_addr_index. */
20175
20176 static void
20177 dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
20178 const gdb_byte *info_ptr,
20179 struct die_info *comp_unit_die,
20180 int has_children,
20181 void *data)
20182 {
20183 struct dwarf2_cu *cu = reader->cu;
20184 struct dwarf2_read_addr_index_data *aidata =
20185 (struct dwarf2_read_addr_index_data *) data;
20186
20187 aidata->addr_base = cu->addr_base;
20188 aidata->addr_size = cu->header.addr_size;
20189 }
20190
20191 /* Given an index in .debug_addr, fetch the value.
20192 NOTE: This can be called during dwarf expression evaluation,
20193 long after the debug information has been read, and thus per_cu->cu
20194 may no longer exist. */
20195
20196 CORE_ADDR
20197 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
20198 unsigned int addr_index)
20199 {
20200 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
20201 struct dwarf2_cu *cu = per_cu->cu;
20202 ULONGEST addr_base;
20203 int addr_size;
20204
20205 /* We need addr_base and addr_size.
20206 If we don't have PER_CU->cu, we have to get it.
20207 Nasty, but the alternative is storing the needed info in PER_CU,
20208 which at this point doesn't seem justified: it's not clear how frequently
20209 it would get used and it would increase the size of every PER_CU.
20210 Entry points like dwarf2_per_cu_addr_size do a similar thing
20211 so we're not in uncharted territory here.
20212 Alas we need to be a bit more complicated as addr_base is contained
20213 in the DIE.
20214
20215 We don't need to read the entire CU(/TU).
20216 We just need the header and top level die.
20217
20218 IWBN to use the aging mechanism to let us lazily later discard the CU.
20219 For now we skip this optimization. */
20220
20221 if (cu != NULL)
20222 {
20223 addr_base = cu->addr_base;
20224 addr_size = cu->header.addr_size;
20225 }
20226 else
20227 {
20228 struct dwarf2_read_addr_index_data aidata;
20229
20230 /* Note: We can't use init_cutu_and_read_dies_simple here,
20231 we need addr_base. */
20232 init_cutu_and_read_dies (per_cu, NULL, 0, 0, false,
20233 dwarf2_read_addr_index_reader, &aidata);
20234 addr_base = aidata.addr_base;
20235 addr_size = aidata.addr_size;
20236 }
20237
20238 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
20239 addr_size);
20240 }
20241
20242 /* Given a DW_FORM_GNU_str_index or DW_FORM_strx, fetch the string.
20243 This is only used by the Fission support. */
20244
20245 static const char *
20246 read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
20247 {
20248 struct dwarf2_cu *cu = reader->cu;
20249 struct dwarf2_per_objfile *dwarf2_per_objfile
20250 = cu->per_cu->dwarf2_per_objfile;
20251 struct objfile *objfile = dwarf2_per_objfile->objfile;
20252 const char *objf_name = objfile_name (objfile);
20253 bfd *abfd = objfile->obfd;
20254 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
20255 struct dwarf2_section_info *str_offsets_section =
20256 &reader->dwo_file->sections.str_offsets;
20257 const gdb_byte *info_ptr;
20258 ULONGEST str_offset;
20259 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
20260
20261 dwarf2_read_section (objfile, str_section);
20262 dwarf2_read_section (objfile, str_offsets_section);
20263 if (str_section->buffer == NULL)
20264 error (_("%s used without .debug_str.dwo section"
20265 " in CU at offset %s [in module %s]"),
20266 form_name, sect_offset_str (cu->header.sect_off), objf_name);
20267 if (str_offsets_section->buffer == NULL)
20268 error (_("%s used without .debug_str_offsets.dwo section"
20269 " in CU at offset %s [in module %s]"),
20270 form_name, sect_offset_str (cu->header.sect_off), objf_name);
20271 if (str_index * cu->header.offset_size >= str_offsets_section->size)
20272 error (_("%s pointing outside of .debug_str_offsets.dwo"
20273 " section in CU at offset %s [in module %s]"),
20274 form_name, sect_offset_str (cu->header.sect_off), objf_name);
20275 info_ptr = (str_offsets_section->buffer
20276 + str_index * cu->header.offset_size);
20277 if (cu->header.offset_size == 4)
20278 str_offset = bfd_get_32 (abfd, info_ptr);
20279 else
20280 str_offset = bfd_get_64 (abfd, info_ptr);
20281 if (str_offset >= str_section->size)
20282 error (_("Offset from %s pointing outside of"
20283 " .debug_str.dwo section in CU at offset %s [in module %s]"),
20284 form_name, sect_offset_str (cu->header.sect_off), objf_name);
20285 return (const char *) (str_section->buffer + str_offset);
20286 }
20287
20288 /* Return the length of an LEB128 number in BUF. */
20289
20290 static int
20291 leb128_size (const gdb_byte *buf)
20292 {
20293 const gdb_byte *begin = buf;
20294 gdb_byte byte;
20295
20296 while (1)
20297 {
20298 byte = *buf++;
20299 if ((byte & 128) == 0)
20300 return buf - begin;
20301 }
20302 }
20303
20304 static void
20305 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
20306 {
20307 switch (lang)
20308 {
20309 case DW_LANG_C89:
20310 case DW_LANG_C99:
20311 case DW_LANG_C11:
20312 case DW_LANG_C:
20313 case DW_LANG_UPC:
20314 cu->language = language_c;
20315 break;
20316 case DW_LANG_Java:
20317 case DW_LANG_C_plus_plus:
20318 case DW_LANG_C_plus_plus_11:
20319 case DW_LANG_C_plus_plus_14:
20320 cu->language = language_cplus;
20321 break;
20322 case DW_LANG_D:
20323 cu->language = language_d;
20324 break;
20325 case DW_LANG_Fortran77:
20326 case DW_LANG_Fortran90:
20327 case DW_LANG_Fortran95:
20328 case DW_LANG_Fortran03:
20329 case DW_LANG_Fortran08:
20330 cu->language = language_fortran;
20331 break;
20332 case DW_LANG_Go:
20333 cu->language = language_go;
20334 break;
20335 case DW_LANG_Mips_Assembler:
20336 cu->language = language_asm;
20337 break;
20338 case DW_LANG_Ada83:
20339 case DW_LANG_Ada95:
20340 cu->language = language_ada;
20341 break;
20342 case DW_LANG_Modula2:
20343 cu->language = language_m2;
20344 break;
20345 case DW_LANG_Pascal83:
20346 cu->language = language_pascal;
20347 break;
20348 case DW_LANG_ObjC:
20349 cu->language = language_objc;
20350 break;
20351 case DW_LANG_Rust:
20352 case DW_LANG_Rust_old:
20353 cu->language = language_rust;
20354 break;
20355 case DW_LANG_Cobol74:
20356 case DW_LANG_Cobol85:
20357 default:
20358 cu->language = language_minimal;
20359 break;
20360 }
20361 cu->language_defn = language_def (cu->language);
20362 }
20363
20364 /* Return the named attribute or NULL if not there. */
20365
20366 static struct attribute *
20367 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20368 {
20369 for (;;)
20370 {
20371 unsigned int i;
20372 struct attribute *spec = NULL;
20373
20374 for (i = 0; i < die->num_attrs; ++i)
20375 {
20376 if (die->attrs[i].name == name)
20377 return &die->attrs[i];
20378 if (die->attrs[i].name == DW_AT_specification
20379 || die->attrs[i].name == DW_AT_abstract_origin)
20380 spec = &die->attrs[i];
20381 }
20382
20383 if (!spec)
20384 break;
20385
20386 die = follow_die_ref (die, spec, &cu);
20387 }
20388
20389 return NULL;
20390 }
20391
20392 /* Return the named attribute or NULL if not there,
20393 but do not follow DW_AT_specification, etc.
20394 This is for use in contexts where we're reading .debug_types dies.
20395 Following DW_AT_specification, DW_AT_abstract_origin will take us
20396 back up the chain, and we want to go down. */
20397
20398 static struct attribute *
20399 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
20400 {
20401 unsigned int i;
20402
20403 for (i = 0; i < die->num_attrs; ++i)
20404 if (die->attrs[i].name == name)
20405 return &die->attrs[i];
20406
20407 return NULL;
20408 }
20409
20410 /* Return the string associated with a string-typed attribute, or NULL if it
20411 is either not found or is of an incorrect type. */
20412
20413 static const char *
20414 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20415 {
20416 struct attribute *attr;
20417 const char *str = NULL;
20418
20419 attr = dwarf2_attr (die, name, cu);
20420
20421 if (attr != NULL)
20422 {
20423 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
20424 || attr->form == DW_FORM_string
20425 || attr->form == DW_FORM_strx
20426 || attr->form == DW_FORM_strx1
20427 || attr->form == DW_FORM_strx2
20428 || attr->form == DW_FORM_strx3
20429 || attr->form == DW_FORM_strx4
20430 || attr->form == DW_FORM_GNU_str_index
20431 || attr->form == DW_FORM_GNU_strp_alt)
20432 str = DW_STRING (attr);
20433 else
20434 complaint (_("string type expected for attribute %s for "
20435 "DIE at %s in module %s"),
20436 dwarf_attr_name (name), sect_offset_str (die->sect_off),
20437 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
20438 }
20439
20440 return str;
20441 }
20442
20443 /* Return the dwo name or NULL if not present. If present, it is in either
20444 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
20445 static const char *
20446 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
20447 {
20448 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
20449 if (dwo_name == nullptr)
20450 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
20451 return dwo_name;
20452 }
20453
20454 /* Return non-zero iff the attribute NAME is defined for the given DIE,
20455 and holds a non-zero value. This function should only be used for
20456 DW_FORM_flag or DW_FORM_flag_present attributes. */
20457
20458 static int
20459 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
20460 {
20461 struct attribute *attr = dwarf2_attr (die, name, cu);
20462
20463 return (attr && DW_UNSND (attr));
20464 }
20465
20466 static int
20467 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
20468 {
20469 /* A DIE is a declaration if it has a DW_AT_declaration attribute
20470 which value is non-zero. However, we have to be careful with
20471 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
20472 (via dwarf2_flag_true_p) follows this attribute. So we may
20473 end up accidently finding a declaration attribute that belongs
20474 to a different DIE referenced by the specification attribute,
20475 even though the given DIE does not have a declaration attribute. */
20476 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
20477 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
20478 }
20479
20480 /* Return the die giving the specification for DIE, if there is
20481 one. *SPEC_CU is the CU containing DIE on input, and the CU
20482 containing the return value on output. If there is no
20483 specification, but there is an abstract origin, that is
20484 returned. */
20485
20486 static struct die_info *
20487 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
20488 {
20489 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
20490 *spec_cu);
20491
20492 if (spec_attr == NULL)
20493 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
20494
20495 if (spec_attr == NULL)
20496 return NULL;
20497 else
20498 return follow_die_ref (die, spec_attr, spec_cu);
20499 }
20500
20501 /* Stub for free_line_header to match void * callback types. */
20502
20503 static void
20504 free_line_header_voidp (void *arg)
20505 {
20506 struct line_header *lh = (struct line_header *) arg;
20507
20508 delete lh;
20509 }
20510
20511 void
20512 line_header::add_include_dir (const char *include_dir)
20513 {
20514 if (dwarf_line_debug >= 2)
20515 {
20516 size_t new_size;
20517 if (version >= 5)
20518 new_size = m_include_dirs.size ();
20519 else
20520 new_size = m_include_dirs.size () + 1;
20521 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
20522 new_size, include_dir);
20523 }
20524 m_include_dirs.push_back (include_dir);
20525 }
20526
20527 void
20528 line_header::add_file_name (const char *name,
20529 dir_index d_index,
20530 unsigned int mod_time,
20531 unsigned int length)
20532 {
20533 if (dwarf_line_debug >= 2)
20534 {
20535 size_t new_size;
20536 if (version >= 5)
20537 new_size = file_names_size ();
20538 else
20539 new_size = file_names_size () + 1;
20540 fprintf_unfiltered (gdb_stdlog, "Adding file %zu: %s\n",
20541 new_size, name);
20542 }
20543 m_file_names.emplace_back (name, d_index, mod_time, length);
20544 }
20545
20546 /* A convenience function to find the proper .debug_line section for a CU. */
20547
20548 static struct dwarf2_section_info *
20549 get_debug_line_section (struct dwarf2_cu *cu)
20550 {
20551 struct dwarf2_section_info *section;
20552 struct dwarf2_per_objfile *dwarf2_per_objfile
20553 = cu->per_cu->dwarf2_per_objfile;
20554
20555 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
20556 DWO file. */
20557 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20558 section = &cu->dwo_unit->dwo_file->sections.line;
20559 else if (cu->per_cu->is_dwz)
20560 {
20561 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
20562
20563 section = &dwz->line;
20564 }
20565 else
20566 section = &dwarf2_per_objfile->line;
20567
20568 return section;
20569 }
20570
20571 /* Read directory or file name entry format, starting with byte of
20572 format count entries, ULEB128 pairs of entry formats, ULEB128 of
20573 entries count and the entries themselves in the described entry
20574 format. */
20575
20576 static void
20577 read_formatted_entries (struct dwarf2_per_objfile *dwarf2_per_objfile,
20578 bfd *abfd, const gdb_byte **bufp,
20579 struct line_header *lh,
20580 const struct comp_unit_head *cu_header,
20581 void (*callback) (struct line_header *lh,
20582 const char *name,
20583 dir_index d_index,
20584 unsigned int mod_time,
20585 unsigned int length))
20586 {
20587 gdb_byte format_count, formati;
20588 ULONGEST data_count, datai;
20589 const gdb_byte *buf = *bufp;
20590 const gdb_byte *format_header_data;
20591 unsigned int bytes_read;
20592
20593 format_count = read_1_byte (abfd, buf);
20594 buf += 1;
20595 format_header_data = buf;
20596 for (formati = 0; formati < format_count; formati++)
20597 {
20598 read_unsigned_leb128 (abfd, buf, &bytes_read);
20599 buf += bytes_read;
20600 read_unsigned_leb128 (abfd, buf, &bytes_read);
20601 buf += bytes_read;
20602 }
20603
20604 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
20605 buf += bytes_read;
20606 for (datai = 0; datai < data_count; datai++)
20607 {
20608 const gdb_byte *format = format_header_data;
20609 struct file_entry fe;
20610
20611 for (formati = 0; formati < format_count; formati++)
20612 {
20613 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
20614 format += bytes_read;
20615
20616 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
20617 format += bytes_read;
20618
20619 gdb::optional<const char *> string;
20620 gdb::optional<unsigned int> uint;
20621
20622 switch (form)
20623 {
20624 case DW_FORM_string:
20625 string.emplace (read_direct_string (abfd, buf, &bytes_read));
20626 buf += bytes_read;
20627 break;
20628
20629 case DW_FORM_line_strp:
20630 string.emplace (read_indirect_line_string (dwarf2_per_objfile,
20631 abfd, buf,
20632 cu_header,
20633 &bytes_read));
20634 buf += bytes_read;
20635 break;
20636
20637 case DW_FORM_data1:
20638 uint.emplace (read_1_byte (abfd, buf));
20639 buf += 1;
20640 break;
20641
20642 case DW_FORM_data2:
20643 uint.emplace (read_2_bytes (abfd, buf));
20644 buf += 2;
20645 break;
20646
20647 case DW_FORM_data4:
20648 uint.emplace (read_4_bytes (abfd, buf));
20649 buf += 4;
20650 break;
20651
20652 case DW_FORM_data8:
20653 uint.emplace (read_8_bytes (abfd, buf));
20654 buf += 8;
20655 break;
20656
20657 case DW_FORM_data16:
20658 /* This is used for MD5, but file_entry does not record MD5s. */
20659 buf += 16;
20660 break;
20661
20662 case DW_FORM_udata:
20663 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
20664 buf += bytes_read;
20665 break;
20666
20667 case DW_FORM_block:
20668 /* It is valid only for DW_LNCT_timestamp which is ignored by
20669 current GDB. */
20670 break;
20671 }
20672
20673 switch (content_type)
20674 {
20675 case DW_LNCT_path:
20676 if (string.has_value ())
20677 fe.name = *string;
20678 break;
20679 case DW_LNCT_directory_index:
20680 if (uint.has_value ())
20681 fe.d_index = (dir_index) *uint;
20682 break;
20683 case DW_LNCT_timestamp:
20684 if (uint.has_value ())
20685 fe.mod_time = *uint;
20686 break;
20687 case DW_LNCT_size:
20688 if (uint.has_value ())
20689 fe.length = *uint;
20690 break;
20691 case DW_LNCT_MD5:
20692 break;
20693 default:
20694 complaint (_("Unknown format content type %s"),
20695 pulongest (content_type));
20696 }
20697 }
20698
20699 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
20700 }
20701
20702 *bufp = buf;
20703 }
20704
20705 /* Read the statement program header starting at OFFSET in
20706 .debug_line, or .debug_line.dwo. Return a pointer
20707 to a struct line_header, allocated using xmalloc.
20708 Returns NULL if there is a problem reading the header, e.g., if it
20709 has a version we don't understand.
20710
20711 NOTE: the strings in the include directory and file name tables of
20712 the returned object point into the dwarf line section buffer,
20713 and must not be freed. */
20714
20715 static line_header_up
20716 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
20717 {
20718 const gdb_byte *line_ptr;
20719 unsigned int bytes_read, offset_size;
20720 int i;
20721 const char *cur_dir, *cur_file;
20722 struct dwarf2_section_info *section;
20723 bfd *abfd;
20724 struct dwarf2_per_objfile *dwarf2_per_objfile
20725 = cu->per_cu->dwarf2_per_objfile;
20726
20727 section = get_debug_line_section (cu);
20728 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
20729 if (section->buffer == NULL)
20730 {
20731 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20732 complaint (_("missing .debug_line.dwo section"));
20733 else
20734 complaint (_("missing .debug_line section"));
20735 return 0;
20736 }
20737
20738 /* We can't do this until we know the section is non-empty.
20739 Only then do we know we have such a section. */
20740 abfd = get_section_bfd_owner (section);
20741
20742 /* Make sure that at least there's room for the total_length field.
20743 That could be 12 bytes long, but we're just going to fudge that. */
20744 if (to_underlying (sect_off) + 4 >= section->size)
20745 {
20746 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20747 return 0;
20748 }
20749
20750 line_header_up lh (new line_header ());
20751
20752 lh->sect_off = sect_off;
20753 lh->offset_in_dwz = cu->per_cu->is_dwz;
20754
20755 line_ptr = section->buffer + to_underlying (sect_off);
20756
20757 /* Read in the header. */
20758 lh->total_length =
20759 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
20760 &bytes_read, &offset_size);
20761 line_ptr += bytes_read;
20762
20763 const gdb_byte *start_here = line_ptr;
20764
20765 if (line_ptr + lh->total_length > (section->buffer + section->size))
20766 {
20767 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20768 return 0;
20769 }
20770 lh->statement_program_end = start_here + lh->total_length;
20771 lh->version = read_2_bytes (abfd, line_ptr);
20772 line_ptr += 2;
20773 if (lh->version > 5)
20774 {
20775 /* This is a version we don't understand. The format could have
20776 changed in ways we don't handle properly so just punt. */
20777 complaint (_("unsupported version in .debug_line section"));
20778 return NULL;
20779 }
20780 if (lh->version >= 5)
20781 {
20782 gdb_byte segment_selector_size;
20783
20784 /* Skip address size. */
20785 read_1_byte (abfd, line_ptr);
20786 line_ptr += 1;
20787
20788 segment_selector_size = read_1_byte (abfd, line_ptr);
20789 line_ptr += 1;
20790 if (segment_selector_size != 0)
20791 {
20792 complaint (_("unsupported segment selector size %u "
20793 "in .debug_line section"),
20794 segment_selector_size);
20795 return NULL;
20796 }
20797 }
20798 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
20799 line_ptr += offset_size;
20800 lh->statement_program_start = line_ptr + lh->header_length;
20801 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
20802 line_ptr += 1;
20803 if (lh->version >= 4)
20804 {
20805 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
20806 line_ptr += 1;
20807 }
20808 else
20809 lh->maximum_ops_per_instruction = 1;
20810
20811 if (lh->maximum_ops_per_instruction == 0)
20812 {
20813 lh->maximum_ops_per_instruction = 1;
20814 complaint (_("invalid maximum_ops_per_instruction "
20815 "in `.debug_line' section"));
20816 }
20817
20818 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
20819 line_ptr += 1;
20820 lh->line_base = read_1_signed_byte (abfd, line_ptr);
20821 line_ptr += 1;
20822 lh->line_range = read_1_byte (abfd, line_ptr);
20823 line_ptr += 1;
20824 lh->opcode_base = read_1_byte (abfd, line_ptr);
20825 line_ptr += 1;
20826 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
20827
20828 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
20829 for (i = 1; i < lh->opcode_base; ++i)
20830 {
20831 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
20832 line_ptr += 1;
20833 }
20834
20835 if (lh->version >= 5)
20836 {
20837 /* Read directory table. */
20838 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20839 &cu->header,
20840 [] (struct line_header *header, const char *name,
20841 dir_index d_index, unsigned int mod_time,
20842 unsigned int length)
20843 {
20844 header->add_include_dir (name);
20845 });
20846
20847 /* Read file name table. */
20848 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20849 &cu->header,
20850 [] (struct line_header *header, const char *name,
20851 dir_index d_index, unsigned int mod_time,
20852 unsigned int length)
20853 {
20854 header->add_file_name (name, d_index, mod_time, length);
20855 });
20856 }
20857 else
20858 {
20859 /* Read directory table. */
20860 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20861 {
20862 line_ptr += bytes_read;
20863 lh->add_include_dir (cur_dir);
20864 }
20865 line_ptr += bytes_read;
20866
20867 /* Read file name table. */
20868 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20869 {
20870 unsigned int mod_time, length;
20871 dir_index d_index;
20872
20873 line_ptr += bytes_read;
20874 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20875 line_ptr += bytes_read;
20876 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20877 line_ptr += bytes_read;
20878 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20879 line_ptr += bytes_read;
20880
20881 lh->add_file_name (cur_file, d_index, mod_time, length);
20882 }
20883 line_ptr += bytes_read;
20884 }
20885
20886 if (line_ptr > (section->buffer + section->size))
20887 complaint (_("line number info header doesn't "
20888 "fit in `.debug_line' section"));
20889
20890 return lh;
20891 }
20892
20893 /* Subroutine of dwarf_decode_lines to simplify it.
20894 Return the file name of the psymtab for the given file_entry.
20895 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20896 If space for the result is malloc'd, *NAME_HOLDER will be set.
20897 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
20898
20899 static const char *
20900 psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
20901 const struct partial_symtab *pst,
20902 const char *comp_dir,
20903 gdb::unique_xmalloc_ptr<char> *name_holder)
20904 {
20905 const char *include_name = fe.name;
20906 const char *include_name_to_compare = include_name;
20907 const char *pst_filename;
20908 int file_is_pst;
20909
20910 const char *dir_name = fe.include_dir (lh);
20911
20912 gdb::unique_xmalloc_ptr<char> hold_compare;
20913 if (!IS_ABSOLUTE_PATH (include_name)
20914 && (dir_name != NULL || comp_dir != NULL))
20915 {
20916 /* Avoid creating a duplicate psymtab for PST.
20917 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20918 Before we do the comparison, however, we need to account
20919 for DIR_NAME and COMP_DIR.
20920 First prepend dir_name (if non-NULL). If we still don't
20921 have an absolute path prepend comp_dir (if non-NULL).
20922 However, the directory we record in the include-file's
20923 psymtab does not contain COMP_DIR (to match the
20924 corresponding symtab(s)).
20925
20926 Example:
20927
20928 bash$ cd /tmp
20929 bash$ gcc -g ./hello.c
20930 include_name = "hello.c"
20931 dir_name = "."
20932 DW_AT_comp_dir = comp_dir = "/tmp"
20933 DW_AT_name = "./hello.c"
20934
20935 */
20936
20937 if (dir_name != NULL)
20938 {
20939 name_holder->reset (concat (dir_name, SLASH_STRING,
20940 include_name, (char *) NULL));
20941 include_name = name_holder->get ();
20942 include_name_to_compare = include_name;
20943 }
20944 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20945 {
20946 hold_compare.reset (concat (comp_dir, SLASH_STRING,
20947 include_name, (char *) NULL));
20948 include_name_to_compare = hold_compare.get ();
20949 }
20950 }
20951
20952 pst_filename = pst->filename;
20953 gdb::unique_xmalloc_ptr<char> copied_name;
20954 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20955 {
20956 copied_name.reset (concat (pst->dirname, SLASH_STRING,
20957 pst_filename, (char *) NULL));
20958 pst_filename = copied_name.get ();
20959 }
20960
20961 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
20962
20963 if (file_is_pst)
20964 return NULL;
20965 return include_name;
20966 }
20967
20968 /* State machine to track the state of the line number program. */
20969
20970 class lnp_state_machine
20971 {
20972 public:
20973 /* Initialize a machine state for the start of a line number
20974 program. */
20975 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
20976 bool record_lines_p);
20977
20978 file_entry *current_file ()
20979 {
20980 /* lh->file_names is 0-based, but the file name numbers in the
20981 statement program are 1-based. */
20982 return m_line_header->file_name_at (m_file);
20983 }
20984
20985 /* Record the line in the state machine. END_SEQUENCE is true if
20986 we're processing the end of a sequence. */
20987 void record_line (bool end_sequence);
20988
20989 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
20990 nop-out rest of the lines in this sequence. */
20991 void check_line_address (struct dwarf2_cu *cu,
20992 const gdb_byte *line_ptr,
20993 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
20994
20995 void handle_set_discriminator (unsigned int discriminator)
20996 {
20997 m_discriminator = discriminator;
20998 m_line_has_non_zero_discriminator |= discriminator != 0;
20999 }
21000
21001 /* Handle DW_LNE_set_address. */
21002 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
21003 {
21004 m_op_index = 0;
21005 address += baseaddr;
21006 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
21007 }
21008
21009 /* Handle DW_LNS_advance_pc. */
21010 void handle_advance_pc (CORE_ADDR adjust);
21011
21012 /* Handle a special opcode. */
21013 void handle_special_opcode (unsigned char op_code);
21014
21015 /* Handle DW_LNS_advance_line. */
21016 void handle_advance_line (int line_delta)
21017 {
21018 advance_line (line_delta);
21019 }
21020
21021 /* Handle DW_LNS_set_file. */
21022 void handle_set_file (file_name_index file);
21023
21024 /* Handle DW_LNS_negate_stmt. */
21025 void handle_negate_stmt ()
21026 {
21027 m_is_stmt = !m_is_stmt;
21028 }
21029
21030 /* Handle DW_LNS_const_add_pc. */
21031 void handle_const_add_pc ();
21032
21033 /* Handle DW_LNS_fixed_advance_pc. */
21034 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
21035 {
21036 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21037 m_op_index = 0;
21038 }
21039
21040 /* Handle DW_LNS_copy. */
21041 void handle_copy ()
21042 {
21043 record_line (false);
21044 m_discriminator = 0;
21045 }
21046
21047 /* Handle DW_LNE_end_sequence. */
21048 void handle_end_sequence ()
21049 {
21050 m_currently_recording_lines = true;
21051 }
21052
21053 private:
21054 /* Advance the line by LINE_DELTA. */
21055 void advance_line (int line_delta)
21056 {
21057 m_line += line_delta;
21058
21059 if (line_delta != 0)
21060 m_line_has_non_zero_discriminator = m_discriminator != 0;
21061 }
21062
21063 struct dwarf2_cu *m_cu;
21064
21065 gdbarch *m_gdbarch;
21066
21067 /* True if we're recording lines.
21068 Otherwise we're building partial symtabs and are just interested in
21069 finding include files mentioned by the line number program. */
21070 bool m_record_lines_p;
21071
21072 /* The line number header. */
21073 line_header *m_line_header;
21074
21075 /* These are part of the standard DWARF line number state machine,
21076 and initialized according to the DWARF spec. */
21077
21078 unsigned char m_op_index = 0;
21079 /* The line table index of the current file. */
21080 file_name_index m_file = 1;
21081 unsigned int m_line = 1;
21082
21083 /* These are initialized in the constructor. */
21084
21085 CORE_ADDR m_address;
21086 bool m_is_stmt;
21087 unsigned int m_discriminator;
21088
21089 /* Additional bits of state we need to track. */
21090
21091 /* The last file that we called dwarf2_start_subfile for.
21092 This is only used for TLLs. */
21093 unsigned int m_last_file = 0;
21094 /* The last file a line number was recorded for. */
21095 struct subfile *m_last_subfile = NULL;
21096
21097 /* When true, record the lines we decode. */
21098 bool m_currently_recording_lines = false;
21099
21100 /* The last line number that was recorded, used to coalesce
21101 consecutive entries for the same line. This can happen, for
21102 example, when discriminators are present. PR 17276. */
21103 unsigned int m_last_line = 0;
21104 bool m_line_has_non_zero_discriminator = false;
21105 };
21106
21107 void
21108 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
21109 {
21110 CORE_ADDR addr_adj = (((m_op_index + adjust)
21111 / m_line_header->maximum_ops_per_instruction)
21112 * m_line_header->minimum_instruction_length);
21113 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21114 m_op_index = ((m_op_index + adjust)
21115 % m_line_header->maximum_ops_per_instruction);
21116 }
21117
21118 void
21119 lnp_state_machine::handle_special_opcode (unsigned char op_code)
21120 {
21121 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
21122 CORE_ADDR addr_adj = (((m_op_index
21123 + (adj_opcode / m_line_header->line_range))
21124 / m_line_header->maximum_ops_per_instruction)
21125 * m_line_header->minimum_instruction_length);
21126 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21127 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
21128 % m_line_header->maximum_ops_per_instruction);
21129
21130 int line_delta = (m_line_header->line_base
21131 + (adj_opcode % m_line_header->line_range));
21132 advance_line (line_delta);
21133 record_line (false);
21134 m_discriminator = 0;
21135 }
21136
21137 void
21138 lnp_state_machine::handle_set_file (file_name_index file)
21139 {
21140 m_file = file;
21141
21142 const file_entry *fe = current_file ();
21143 if (fe == NULL)
21144 dwarf2_debug_line_missing_file_complaint ();
21145 else if (m_record_lines_p)
21146 {
21147 const char *dir = fe->include_dir (m_line_header);
21148
21149 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
21150 m_line_has_non_zero_discriminator = m_discriminator != 0;
21151 dwarf2_start_subfile (m_cu, fe->name, dir);
21152 }
21153 }
21154
21155 void
21156 lnp_state_machine::handle_const_add_pc ()
21157 {
21158 CORE_ADDR adjust
21159 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
21160
21161 CORE_ADDR addr_adj
21162 = (((m_op_index + adjust)
21163 / m_line_header->maximum_ops_per_instruction)
21164 * m_line_header->minimum_instruction_length);
21165
21166 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21167 m_op_index = ((m_op_index + adjust)
21168 % m_line_header->maximum_ops_per_instruction);
21169 }
21170
21171 /* Return non-zero if we should add LINE to the line number table.
21172 LINE is the line to add, LAST_LINE is the last line that was added,
21173 LAST_SUBFILE is the subfile for LAST_LINE.
21174 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
21175 had a non-zero discriminator.
21176
21177 We have to be careful in the presence of discriminators.
21178 E.g., for this line:
21179
21180 for (i = 0; i < 100000; i++);
21181
21182 clang can emit four line number entries for that one line,
21183 each with a different discriminator.
21184 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
21185
21186 However, we want gdb to coalesce all four entries into one.
21187 Otherwise the user could stepi into the middle of the line and
21188 gdb would get confused about whether the pc really was in the
21189 middle of the line.
21190
21191 Things are further complicated by the fact that two consecutive
21192 line number entries for the same line is a heuristic used by gcc
21193 to denote the end of the prologue. So we can't just discard duplicate
21194 entries, we have to be selective about it. The heuristic we use is
21195 that we only collapse consecutive entries for the same line if at least
21196 one of those entries has a non-zero discriminator. PR 17276.
21197
21198 Note: Addresses in the line number state machine can never go backwards
21199 within one sequence, thus this coalescing is ok. */
21200
21201 static int
21202 dwarf_record_line_p (struct dwarf2_cu *cu,
21203 unsigned int line, unsigned int last_line,
21204 int line_has_non_zero_discriminator,
21205 struct subfile *last_subfile)
21206 {
21207 if (cu->get_builder ()->get_current_subfile () != last_subfile)
21208 return 1;
21209 if (line != last_line)
21210 return 1;
21211 /* Same line for the same file that we've seen already.
21212 As a last check, for pr 17276, only record the line if the line
21213 has never had a non-zero discriminator. */
21214 if (!line_has_non_zero_discriminator)
21215 return 1;
21216 return 0;
21217 }
21218
21219 /* Use the CU's builder to record line number LINE beginning at
21220 address ADDRESS in the line table of subfile SUBFILE. */
21221
21222 static void
21223 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
21224 unsigned int line, CORE_ADDR address,
21225 struct dwarf2_cu *cu)
21226 {
21227 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
21228
21229 if (dwarf_line_debug)
21230 {
21231 fprintf_unfiltered (gdb_stdlog,
21232 "Recording line %u, file %s, address %s\n",
21233 line, lbasename (subfile->name),
21234 paddress (gdbarch, address));
21235 }
21236
21237 if (cu != nullptr)
21238 cu->get_builder ()->record_line (subfile, line, addr);
21239 }
21240
21241 /* Subroutine of dwarf_decode_lines_1 to simplify it.
21242 Mark the end of a set of line number records.
21243 The arguments are the same as for dwarf_record_line_1.
21244 If SUBFILE is NULL the request is ignored. */
21245
21246 static void
21247 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
21248 CORE_ADDR address, struct dwarf2_cu *cu)
21249 {
21250 if (subfile == NULL)
21251 return;
21252
21253 if (dwarf_line_debug)
21254 {
21255 fprintf_unfiltered (gdb_stdlog,
21256 "Finishing current line, file %s, address %s\n",
21257 lbasename (subfile->name),
21258 paddress (gdbarch, address));
21259 }
21260
21261 dwarf_record_line_1 (gdbarch, subfile, 0, address, cu);
21262 }
21263
21264 void
21265 lnp_state_machine::record_line (bool end_sequence)
21266 {
21267 if (dwarf_line_debug)
21268 {
21269 fprintf_unfiltered (gdb_stdlog,
21270 "Processing actual line %u: file %u,"
21271 " address %s, is_stmt %u, discrim %u\n",
21272 m_line, m_file,
21273 paddress (m_gdbarch, m_address),
21274 m_is_stmt, m_discriminator);
21275 }
21276
21277 file_entry *fe = current_file ();
21278
21279 if (fe == NULL)
21280 dwarf2_debug_line_missing_file_complaint ();
21281 /* For now we ignore lines not starting on an instruction boundary.
21282 But not when processing end_sequence for compatibility with the
21283 previous version of the code. */
21284 else if (m_op_index == 0 || end_sequence)
21285 {
21286 fe->included_p = 1;
21287 if (m_record_lines_p && (producer_is_codewarrior (m_cu) || m_is_stmt))
21288 {
21289 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
21290 || end_sequence)
21291 {
21292 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
21293 m_currently_recording_lines ? m_cu : nullptr);
21294 }
21295
21296 if (!end_sequence)
21297 {
21298 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
21299 m_line_has_non_zero_discriminator,
21300 m_last_subfile))
21301 {
21302 buildsym_compunit *builder = m_cu->get_builder ();
21303 dwarf_record_line_1 (m_gdbarch,
21304 builder->get_current_subfile (),
21305 m_line, m_address,
21306 m_currently_recording_lines ? m_cu : nullptr);
21307 }
21308 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
21309 m_last_line = m_line;
21310 }
21311 }
21312 }
21313 }
21314
21315 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
21316 line_header *lh, bool record_lines_p)
21317 {
21318 m_cu = cu;
21319 m_gdbarch = arch;
21320 m_record_lines_p = record_lines_p;
21321 m_line_header = lh;
21322
21323 m_currently_recording_lines = true;
21324
21325 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
21326 was a line entry for it so that the backend has a chance to adjust it
21327 and also record it in case it needs it. This is currently used by MIPS
21328 code, cf. `mips_adjust_dwarf2_line'. */
21329 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
21330 m_is_stmt = lh->default_is_stmt;
21331 m_discriminator = 0;
21332 }
21333
21334 void
21335 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
21336 const gdb_byte *line_ptr,
21337 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
21338 {
21339 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
21340 the pc range of the CU. However, we restrict the test to only ADDRESS
21341 values of zero to preserve GDB's previous behaviour which is to handle
21342 the specific case of a function being GC'd by the linker. */
21343
21344 if (address == 0 && address < unrelocated_lowpc)
21345 {
21346 /* This line table is for a function which has been
21347 GCd by the linker. Ignore it. PR gdb/12528 */
21348
21349 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21350 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
21351
21352 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
21353 line_offset, objfile_name (objfile));
21354 m_currently_recording_lines = false;
21355 /* Note: m_currently_recording_lines is left as false until we see
21356 DW_LNE_end_sequence. */
21357 }
21358 }
21359
21360 /* Subroutine of dwarf_decode_lines to simplify it.
21361 Process the line number information in LH.
21362 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
21363 program in order to set included_p for every referenced header. */
21364
21365 static void
21366 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
21367 const int decode_for_pst_p, CORE_ADDR lowpc)
21368 {
21369 const gdb_byte *line_ptr, *extended_end;
21370 const gdb_byte *line_end;
21371 unsigned int bytes_read, extended_len;
21372 unsigned char op_code, extended_op;
21373 CORE_ADDR baseaddr;
21374 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21375 bfd *abfd = objfile->obfd;
21376 struct gdbarch *gdbarch = get_objfile_arch (objfile);
21377 /* True if we're recording line info (as opposed to building partial
21378 symtabs and just interested in finding include files mentioned by
21379 the line number program). */
21380 bool record_lines_p = !decode_for_pst_p;
21381
21382 baseaddr = objfile->section_offsets[SECT_OFF_TEXT (objfile)];
21383
21384 line_ptr = lh->statement_program_start;
21385 line_end = lh->statement_program_end;
21386
21387 /* Read the statement sequences until there's nothing left. */
21388 while (line_ptr < line_end)
21389 {
21390 /* The DWARF line number program state machine. Reset the state
21391 machine at the start of each sequence. */
21392 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
21393 bool end_sequence = false;
21394
21395 if (record_lines_p)
21396 {
21397 /* Start a subfile for the current file of the state
21398 machine. */
21399 const file_entry *fe = state_machine.current_file ();
21400
21401 if (fe != NULL)
21402 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
21403 }
21404
21405 /* Decode the table. */
21406 while (line_ptr < line_end && !end_sequence)
21407 {
21408 op_code = read_1_byte (abfd, line_ptr);
21409 line_ptr += 1;
21410
21411 if (op_code >= lh->opcode_base)
21412 {
21413 /* Special opcode. */
21414 state_machine.handle_special_opcode (op_code);
21415 }
21416 else switch (op_code)
21417 {
21418 case DW_LNS_extended_op:
21419 extended_len = read_unsigned_leb128 (abfd, line_ptr,
21420 &bytes_read);
21421 line_ptr += bytes_read;
21422 extended_end = line_ptr + extended_len;
21423 extended_op = read_1_byte (abfd, line_ptr);
21424 line_ptr += 1;
21425 switch (extended_op)
21426 {
21427 case DW_LNE_end_sequence:
21428 state_machine.handle_end_sequence ();
21429 end_sequence = true;
21430 break;
21431 case DW_LNE_set_address:
21432 {
21433 CORE_ADDR address
21434 = read_address (abfd, line_ptr, cu, &bytes_read);
21435 line_ptr += bytes_read;
21436
21437 state_machine.check_line_address (cu, line_ptr,
21438 lowpc - baseaddr, address);
21439 state_machine.handle_set_address (baseaddr, address);
21440 }
21441 break;
21442 case DW_LNE_define_file:
21443 {
21444 const char *cur_file;
21445 unsigned int mod_time, length;
21446 dir_index dindex;
21447
21448 cur_file = read_direct_string (abfd, line_ptr,
21449 &bytes_read);
21450 line_ptr += bytes_read;
21451 dindex = (dir_index)
21452 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21453 line_ptr += bytes_read;
21454 mod_time =
21455 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21456 line_ptr += bytes_read;
21457 length =
21458 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21459 line_ptr += bytes_read;
21460 lh->add_file_name (cur_file, dindex, mod_time, length);
21461 }
21462 break;
21463 case DW_LNE_set_discriminator:
21464 {
21465 /* The discriminator is not interesting to the
21466 debugger; just ignore it. We still need to
21467 check its value though:
21468 if there are consecutive entries for the same
21469 (non-prologue) line we want to coalesce them.
21470 PR 17276. */
21471 unsigned int discr
21472 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21473 line_ptr += bytes_read;
21474
21475 state_machine.handle_set_discriminator (discr);
21476 }
21477 break;
21478 default:
21479 complaint (_("mangled .debug_line section"));
21480 return;
21481 }
21482 /* Make sure that we parsed the extended op correctly. If e.g.
21483 we expected a different address size than the producer used,
21484 we may have read the wrong number of bytes. */
21485 if (line_ptr != extended_end)
21486 {
21487 complaint (_("mangled .debug_line section"));
21488 return;
21489 }
21490 break;
21491 case DW_LNS_copy:
21492 state_machine.handle_copy ();
21493 break;
21494 case DW_LNS_advance_pc:
21495 {
21496 CORE_ADDR adjust
21497 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21498 line_ptr += bytes_read;
21499
21500 state_machine.handle_advance_pc (adjust);
21501 }
21502 break;
21503 case DW_LNS_advance_line:
21504 {
21505 int line_delta
21506 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
21507 line_ptr += bytes_read;
21508
21509 state_machine.handle_advance_line (line_delta);
21510 }
21511 break;
21512 case DW_LNS_set_file:
21513 {
21514 file_name_index file
21515 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21516 &bytes_read);
21517 line_ptr += bytes_read;
21518
21519 state_machine.handle_set_file (file);
21520 }
21521 break;
21522 case DW_LNS_set_column:
21523 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21524 line_ptr += bytes_read;
21525 break;
21526 case DW_LNS_negate_stmt:
21527 state_machine.handle_negate_stmt ();
21528 break;
21529 case DW_LNS_set_basic_block:
21530 break;
21531 /* Add to the address register of the state machine the
21532 address increment value corresponding to special opcode
21533 255. I.e., this value is scaled by the minimum
21534 instruction length since special opcode 255 would have
21535 scaled the increment. */
21536 case DW_LNS_const_add_pc:
21537 state_machine.handle_const_add_pc ();
21538 break;
21539 case DW_LNS_fixed_advance_pc:
21540 {
21541 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
21542 line_ptr += 2;
21543
21544 state_machine.handle_fixed_advance_pc (addr_adj);
21545 }
21546 break;
21547 default:
21548 {
21549 /* Unknown standard opcode, ignore it. */
21550 int i;
21551
21552 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
21553 {
21554 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21555 line_ptr += bytes_read;
21556 }
21557 }
21558 }
21559 }
21560
21561 if (!end_sequence)
21562 dwarf2_debug_line_missing_end_sequence_complaint ();
21563
21564 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21565 in which case we still finish recording the last line). */
21566 state_machine.record_line (true);
21567 }
21568 }
21569
21570 /* Decode the Line Number Program (LNP) for the given line_header
21571 structure and CU. The actual information extracted and the type
21572 of structures created from the LNP depends on the value of PST.
21573
21574 1. If PST is NULL, then this procedure uses the data from the program
21575 to create all necessary symbol tables, and their linetables.
21576
21577 2. If PST is not NULL, this procedure reads the program to determine
21578 the list of files included by the unit represented by PST, and
21579 builds all the associated partial symbol tables.
21580
21581 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21582 It is used for relative paths in the line table.
21583 NOTE: When processing partial symtabs (pst != NULL),
21584 comp_dir == pst->dirname.
21585
21586 NOTE: It is important that psymtabs have the same file name (via strcmp)
21587 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21588 symtab we don't use it in the name of the psymtabs we create.
21589 E.g. expand_line_sal requires this when finding psymtabs to expand.
21590 A good testcase for this is mb-inline.exp.
21591
21592 LOWPC is the lowest address in CU (or 0 if not known).
21593
21594 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21595 for its PC<->lines mapping information. Otherwise only the filename
21596 table is read in. */
21597
21598 static void
21599 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
21600 struct dwarf2_cu *cu, struct partial_symtab *pst,
21601 CORE_ADDR lowpc, int decode_mapping)
21602 {
21603 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21604 const int decode_for_pst_p = (pst != NULL);
21605
21606 if (decode_mapping)
21607 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
21608
21609 if (decode_for_pst_p)
21610 {
21611 /* Now that we're done scanning the Line Header Program, we can
21612 create the psymtab of each included file. */
21613 for (auto &file_entry : lh->file_names ())
21614 if (file_entry.included_p == 1)
21615 {
21616 gdb::unique_xmalloc_ptr<char> name_holder;
21617 const char *include_name =
21618 psymtab_include_file_name (lh, file_entry, pst,
21619 comp_dir, &name_holder);
21620 if (include_name != NULL)
21621 dwarf2_create_include_psymtab (include_name, pst, objfile);
21622 }
21623 }
21624 else
21625 {
21626 /* Make sure a symtab is created for every file, even files
21627 which contain only variables (i.e. no code with associated
21628 line numbers). */
21629 buildsym_compunit *builder = cu->get_builder ();
21630 struct compunit_symtab *cust = builder->get_compunit_symtab ();
21631
21632 for (auto &fe : lh->file_names ())
21633 {
21634 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
21635 if (builder->get_current_subfile ()->symtab == NULL)
21636 {
21637 builder->get_current_subfile ()->symtab
21638 = allocate_symtab (cust,
21639 builder->get_current_subfile ()->name);
21640 }
21641 fe.symtab = builder->get_current_subfile ()->symtab;
21642 }
21643 }
21644 }
21645
21646 /* Start a subfile for DWARF. FILENAME is the name of the file and
21647 DIRNAME the name of the source directory which contains FILENAME
21648 or NULL if not known.
21649 This routine tries to keep line numbers from identical absolute and
21650 relative file names in a common subfile.
21651
21652 Using the `list' example from the GDB testsuite, which resides in
21653 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21654 of /srcdir/list0.c yields the following debugging information for list0.c:
21655
21656 DW_AT_name: /srcdir/list0.c
21657 DW_AT_comp_dir: /compdir
21658 files.files[0].name: list0.h
21659 files.files[0].dir: /srcdir
21660 files.files[1].name: list0.c
21661 files.files[1].dir: /srcdir
21662
21663 The line number information for list0.c has to end up in a single
21664 subfile, so that `break /srcdir/list0.c:1' works as expected.
21665 start_subfile will ensure that this happens provided that we pass the
21666 concatenation of files.files[1].dir and files.files[1].name as the
21667 subfile's name. */
21668
21669 static void
21670 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
21671 const char *dirname)
21672 {
21673 gdb::unique_xmalloc_ptr<char> copy;
21674
21675 /* In order not to lose the line information directory,
21676 we concatenate it to the filename when it makes sense.
21677 Note that the Dwarf3 standard says (speaking of filenames in line
21678 information): ``The directory index is ignored for file names
21679 that represent full path names''. Thus ignoring dirname in the
21680 `else' branch below isn't an issue. */
21681
21682 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
21683 {
21684 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
21685 filename = copy.get ();
21686 }
21687
21688 cu->get_builder ()->start_subfile (filename);
21689 }
21690
21691 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
21692 buildsym_compunit constructor. */
21693
21694 struct compunit_symtab *
21695 dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
21696 CORE_ADDR low_pc)
21697 {
21698 gdb_assert (m_builder == nullptr);
21699
21700 m_builder.reset (new struct buildsym_compunit
21701 (per_cu->dwarf2_per_objfile->objfile,
21702 name, comp_dir, language, low_pc));
21703
21704 list_in_scope = get_builder ()->get_file_symbols ();
21705
21706 get_builder ()->record_debugformat (xstrprintf ("DWARF %d", this->header.version));
21707 get_builder ()->record_producer (producer);
21708
21709 processing_has_namespace_info = false;
21710
21711 return get_builder ()->get_compunit_symtab ();
21712 }
21713
21714 static void
21715 var_decode_location (struct attribute *attr, struct symbol *sym,
21716 struct dwarf2_cu *cu)
21717 {
21718 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21719 struct comp_unit_head *cu_header = &cu->header;
21720
21721 /* NOTE drow/2003-01-30: There used to be a comment and some special
21722 code here to turn a symbol with DW_AT_external and a
21723 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21724 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21725 with some versions of binutils) where shared libraries could have
21726 relocations against symbols in their debug information - the
21727 minimal symbol would have the right address, but the debug info
21728 would not. It's no longer necessary, because we will explicitly
21729 apply relocations when we read in the debug information now. */
21730
21731 /* A DW_AT_location attribute with no contents indicates that a
21732 variable has been optimized away. */
21733 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
21734 {
21735 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21736 return;
21737 }
21738
21739 /* Handle one degenerate form of location expression specially, to
21740 preserve GDB's previous behavior when section offsets are
21741 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
21742 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
21743
21744 if (attr_form_is_block (attr)
21745 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
21746 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
21747 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
21748 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
21749 && (DW_BLOCK (attr)->size
21750 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
21751 {
21752 unsigned int dummy;
21753
21754 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
21755 SET_SYMBOL_VALUE_ADDRESS (sym,
21756 read_address (objfile->obfd,
21757 DW_BLOCK (attr)->data + 1,
21758 cu, &dummy));
21759 else
21760 SET_SYMBOL_VALUE_ADDRESS
21761 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
21762 &dummy));
21763 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
21764 fixup_symbol_section (sym, objfile);
21765 SET_SYMBOL_VALUE_ADDRESS
21766 (sym,
21767 SYMBOL_VALUE_ADDRESS (sym)
21768 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
21769 return;
21770 }
21771
21772 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21773 expression evaluator, and use LOC_COMPUTED only when necessary
21774 (i.e. when the value of a register or memory location is
21775 referenced, or a thread-local block, etc.). Then again, it might
21776 not be worthwhile. I'm assuming that it isn't unless performance
21777 or memory numbers show me otherwise. */
21778
21779 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
21780
21781 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
21782 cu->has_loclist = true;
21783 }
21784
21785 /* Given a pointer to a DWARF information entry, figure out if we need
21786 to make a symbol table entry for it, and if so, create a new entry
21787 and return a pointer to it.
21788 If TYPE is NULL, determine symbol type from the die, otherwise
21789 used the passed type.
21790 If SPACE is not NULL, use it to hold the new symbol. If it is
21791 NULL, allocate a new symbol on the objfile's obstack. */
21792
21793 static struct symbol *
21794 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21795 struct symbol *space)
21796 {
21797 struct dwarf2_per_objfile *dwarf2_per_objfile
21798 = cu->per_cu->dwarf2_per_objfile;
21799 struct objfile *objfile = dwarf2_per_objfile->objfile;
21800 struct gdbarch *gdbarch = get_objfile_arch (objfile);
21801 struct symbol *sym = NULL;
21802 const char *name;
21803 struct attribute *attr = NULL;
21804 struct attribute *attr2 = NULL;
21805 CORE_ADDR baseaddr;
21806 struct pending **list_to_add = NULL;
21807
21808 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
21809
21810 baseaddr = objfile->section_offsets[SECT_OFF_TEXT (objfile)];
21811
21812 name = dwarf2_name (die, cu);
21813 if (name)
21814 {
21815 const char *linkagename;
21816 int suppress_add = 0;
21817
21818 if (space)
21819 sym = space;
21820 else
21821 sym = allocate_symbol (objfile);
21822 OBJSTAT (objfile, n_syms++);
21823
21824 /* Cache this symbol's name and the name's demangled form (if any). */
21825 sym->set_language (cu->language, &objfile->objfile_obstack);
21826 linkagename = dwarf2_physname (name, die, cu);
21827 sym->compute_and_set_names (linkagename, false, objfile->per_bfd);
21828
21829 /* Fortran does not have mangling standard and the mangling does differ
21830 between gfortran, iFort etc. */
21831 if (cu->language == language_fortran
21832 && symbol_get_demangled_name (sym) == NULL)
21833 symbol_set_demangled_name (sym,
21834 dwarf2_full_name (name, die, cu),
21835 NULL);
21836
21837 /* Default assumptions.
21838 Use the passed type or decode it from the die. */
21839 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21840 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21841 if (type != NULL)
21842 SYMBOL_TYPE (sym) = type;
21843 else
21844 SYMBOL_TYPE (sym) = die_type (die, cu);
21845 attr = dwarf2_attr (die,
21846 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21847 cu);
21848 if (attr != nullptr)
21849 {
21850 SYMBOL_LINE (sym) = DW_UNSND (attr);
21851 }
21852
21853 attr = dwarf2_attr (die,
21854 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21855 cu);
21856 if (attr != nullptr)
21857 {
21858 file_name_index file_index = (file_name_index) DW_UNSND (attr);
21859 struct file_entry *fe;
21860
21861 if (cu->line_header != NULL)
21862 fe = cu->line_header->file_name_at (file_index);
21863 else
21864 fe = NULL;
21865
21866 if (fe == NULL)
21867 complaint (_("file index out of range"));
21868 else
21869 symbol_set_symtab (sym, fe->symtab);
21870 }
21871
21872 switch (die->tag)
21873 {
21874 case DW_TAG_label:
21875 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
21876 if (attr != nullptr)
21877 {
21878 CORE_ADDR addr;
21879
21880 addr = attr_value_as_address (attr);
21881 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
21882 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
21883 }
21884 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21885 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
21886 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
21887 add_symbol_to_list (sym, cu->list_in_scope);
21888 break;
21889 case DW_TAG_subprogram:
21890 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21891 finish_block. */
21892 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21893 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21894 if ((attr2 && (DW_UNSND (attr2) != 0))
21895 || cu->language == language_ada
21896 || cu->language == language_fortran)
21897 {
21898 /* Subprograms marked external are stored as a global symbol.
21899 Ada and Fortran subprograms, whether marked external or
21900 not, are always stored as a global symbol, because we want
21901 to be able to access them globally. For instance, we want
21902 to be able to break on a nested subprogram without having
21903 to specify the context. */
21904 list_to_add = cu->get_builder ()->get_global_symbols ();
21905 }
21906 else
21907 {
21908 list_to_add = cu->list_in_scope;
21909 }
21910 break;
21911 case DW_TAG_inlined_subroutine:
21912 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21913 finish_block. */
21914 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21915 SYMBOL_INLINED (sym) = 1;
21916 list_to_add = cu->list_in_scope;
21917 break;
21918 case DW_TAG_template_value_param:
21919 suppress_add = 1;
21920 /* Fall through. */
21921 case DW_TAG_constant:
21922 case DW_TAG_variable:
21923 case DW_TAG_member:
21924 /* Compilation with minimal debug info may result in
21925 variables with missing type entries. Change the
21926 misleading `void' type to something sensible. */
21927 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
21928 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
21929
21930 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21931 /* In the case of DW_TAG_member, we should only be called for
21932 static const members. */
21933 if (die->tag == DW_TAG_member)
21934 {
21935 /* dwarf2_add_field uses die_is_declaration,
21936 so we do the same. */
21937 gdb_assert (die_is_declaration (die, cu));
21938 gdb_assert (attr);
21939 }
21940 if (attr != nullptr)
21941 {
21942 dwarf2_const_value (attr, sym, cu);
21943 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21944 if (!suppress_add)
21945 {
21946 if (attr2 && (DW_UNSND (attr2) != 0))
21947 list_to_add = cu->get_builder ()->get_global_symbols ();
21948 else
21949 list_to_add = cu->list_in_scope;
21950 }
21951 break;
21952 }
21953 attr = dwarf2_attr (die, DW_AT_location, cu);
21954 if (attr != nullptr)
21955 {
21956 var_decode_location (attr, sym, cu);
21957 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21958
21959 /* Fortran explicitly imports any global symbols to the local
21960 scope by DW_TAG_common_block. */
21961 if (cu->language == language_fortran && die->parent
21962 && die->parent->tag == DW_TAG_common_block)
21963 attr2 = NULL;
21964
21965 if (SYMBOL_CLASS (sym) == LOC_STATIC
21966 && SYMBOL_VALUE_ADDRESS (sym) == 0
21967 && !dwarf2_per_objfile->has_section_at_zero)
21968 {
21969 /* When a static variable is eliminated by the linker,
21970 the corresponding debug information is not stripped
21971 out, but the variable address is set to null;
21972 do not add such variables into symbol table. */
21973 }
21974 else if (attr2 && (DW_UNSND (attr2) != 0))
21975 {
21976 if (SYMBOL_CLASS (sym) == LOC_STATIC
21977 && (objfile->flags & OBJF_MAINLINE) == 0
21978 && dwarf2_per_objfile->can_copy)
21979 {
21980 /* A global static variable might be subject to
21981 copy relocation. We first check for a local
21982 minsym, though, because maybe the symbol was
21983 marked hidden, in which case this would not
21984 apply. */
21985 bound_minimal_symbol found
21986 = (lookup_minimal_symbol_linkage
21987 (sym->linkage_name (), objfile));
21988 if (found.minsym != nullptr)
21989 sym->maybe_copied = 1;
21990 }
21991
21992 /* A variable with DW_AT_external is never static,
21993 but it may be block-scoped. */
21994 list_to_add
21995 = ((cu->list_in_scope
21996 == cu->get_builder ()->get_file_symbols ())
21997 ? cu->get_builder ()->get_global_symbols ()
21998 : cu->list_in_scope);
21999 }
22000 else
22001 list_to_add = cu->list_in_scope;
22002 }
22003 else
22004 {
22005 /* We do not know the address of this symbol.
22006 If it is an external symbol and we have type information
22007 for it, enter the symbol as a LOC_UNRESOLVED symbol.
22008 The address of the variable will then be determined from
22009 the minimal symbol table whenever the variable is
22010 referenced. */
22011 attr2 = dwarf2_attr (die, DW_AT_external, cu);
22012
22013 /* Fortran explicitly imports any global symbols to the local
22014 scope by DW_TAG_common_block. */
22015 if (cu->language == language_fortran && die->parent
22016 && die->parent->tag == DW_TAG_common_block)
22017 {
22018 /* SYMBOL_CLASS doesn't matter here because
22019 read_common_block is going to reset it. */
22020 if (!suppress_add)
22021 list_to_add = cu->list_in_scope;
22022 }
22023 else if (attr2 && (DW_UNSND (attr2) != 0)
22024 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
22025 {
22026 /* A variable with DW_AT_external is never static, but it
22027 may be block-scoped. */
22028 list_to_add
22029 = ((cu->list_in_scope
22030 == cu->get_builder ()->get_file_symbols ())
22031 ? cu->get_builder ()->get_global_symbols ()
22032 : cu->list_in_scope);
22033
22034 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
22035 }
22036 else if (!die_is_declaration (die, cu))
22037 {
22038 /* Use the default LOC_OPTIMIZED_OUT class. */
22039 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
22040 if (!suppress_add)
22041 list_to_add = cu->list_in_scope;
22042 }
22043 }
22044 break;
22045 case DW_TAG_formal_parameter:
22046 {
22047 /* If we are inside a function, mark this as an argument. If
22048 not, we might be looking at an argument to an inlined function
22049 when we do not have enough information to show inlined frames;
22050 pretend it's a local variable in that case so that the user can
22051 still see it. */
22052 struct context_stack *curr
22053 = cu->get_builder ()->get_current_context_stack ();
22054 if (curr != nullptr && curr->name != nullptr)
22055 SYMBOL_IS_ARGUMENT (sym) = 1;
22056 attr = dwarf2_attr (die, DW_AT_location, cu);
22057 if (attr != nullptr)
22058 {
22059 var_decode_location (attr, sym, cu);
22060 }
22061 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22062 if (attr != nullptr)
22063 {
22064 dwarf2_const_value (attr, sym, cu);
22065 }
22066
22067 list_to_add = cu->list_in_scope;
22068 }
22069 break;
22070 case DW_TAG_unspecified_parameters:
22071 /* From varargs functions; gdb doesn't seem to have any
22072 interest in this information, so just ignore it for now.
22073 (FIXME?) */
22074 break;
22075 case DW_TAG_template_type_param:
22076 suppress_add = 1;
22077 /* Fall through. */
22078 case DW_TAG_class_type:
22079 case DW_TAG_interface_type:
22080 case DW_TAG_structure_type:
22081 case DW_TAG_union_type:
22082 case DW_TAG_set_type:
22083 case DW_TAG_enumeration_type:
22084 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22085 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
22086
22087 {
22088 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
22089 really ever be static objects: otherwise, if you try
22090 to, say, break of a class's method and you're in a file
22091 which doesn't mention that class, it won't work unless
22092 the check for all static symbols in lookup_symbol_aux
22093 saves you. See the OtherFileClass tests in
22094 gdb.c++/namespace.exp. */
22095
22096 if (!suppress_add)
22097 {
22098 buildsym_compunit *builder = cu->get_builder ();
22099 list_to_add
22100 = (cu->list_in_scope == builder->get_file_symbols ()
22101 && cu->language == language_cplus
22102 ? builder->get_global_symbols ()
22103 : cu->list_in_scope);
22104
22105 /* The semantics of C++ state that "struct foo {
22106 ... }" also defines a typedef for "foo". */
22107 if (cu->language == language_cplus
22108 || cu->language == language_ada
22109 || cu->language == language_d
22110 || cu->language == language_rust)
22111 {
22112 /* The symbol's name is already allocated along
22113 with this objfile, so we don't need to
22114 duplicate it for the type. */
22115 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
22116 TYPE_NAME (SYMBOL_TYPE (sym)) = sym->search_name ();
22117 }
22118 }
22119 }
22120 break;
22121 case DW_TAG_typedef:
22122 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22123 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
22124 list_to_add = cu->list_in_scope;
22125 break;
22126 case DW_TAG_base_type:
22127 case DW_TAG_subrange_type:
22128 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22129 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
22130 list_to_add = cu->list_in_scope;
22131 break;
22132 case DW_TAG_enumerator:
22133 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22134 if (attr != nullptr)
22135 {
22136 dwarf2_const_value (attr, sym, cu);
22137 }
22138 {
22139 /* NOTE: carlton/2003-11-10: See comment above in the
22140 DW_TAG_class_type, etc. block. */
22141
22142 list_to_add
22143 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
22144 && cu->language == language_cplus
22145 ? cu->get_builder ()->get_global_symbols ()
22146 : cu->list_in_scope);
22147 }
22148 break;
22149 case DW_TAG_imported_declaration:
22150 case DW_TAG_namespace:
22151 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22152 list_to_add = cu->get_builder ()->get_global_symbols ();
22153 break;
22154 case DW_TAG_module:
22155 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22156 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
22157 list_to_add = cu->get_builder ()->get_global_symbols ();
22158 break;
22159 case DW_TAG_common_block:
22160 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
22161 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
22162 add_symbol_to_list (sym, cu->list_in_scope);
22163 break;
22164 default:
22165 /* Not a tag we recognize. Hopefully we aren't processing
22166 trash data, but since we must specifically ignore things
22167 we don't recognize, there is nothing else we should do at
22168 this point. */
22169 complaint (_("unsupported tag: '%s'"),
22170 dwarf_tag_name (die->tag));
22171 break;
22172 }
22173
22174 if (suppress_add)
22175 {
22176 sym->hash_next = objfile->template_symbols;
22177 objfile->template_symbols = sym;
22178 list_to_add = NULL;
22179 }
22180
22181 if (list_to_add != NULL)
22182 add_symbol_to_list (sym, list_to_add);
22183
22184 /* For the benefit of old versions of GCC, check for anonymous
22185 namespaces based on the demangled name. */
22186 if (!cu->processing_has_namespace_info
22187 && cu->language == language_cplus)
22188 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
22189 }
22190 return (sym);
22191 }
22192
22193 /* Given an attr with a DW_FORM_dataN value in host byte order,
22194 zero-extend it as appropriate for the symbol's type. The DWARF
22195 standard (v4) is not entirely clear about the meaning of using
22196 DW_FORM_dataN for a constant with a signed type, where the type is
22197 wider than the data. The conclusion of a discussion on the DWARF
22198 list was that this is unspecified. We choose to always zero-extend
22199 because that is the interpretation long in use by GCC. */
22200
22201 static gdb_byte *
22202 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
22203 struct dwarf2_cu *cu, LONGEST *value, int bits)
22204 {
22205 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22206 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
22207 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
22208 LONGEST l = DW_UNSND (attr);
22209
22210 if (bits < sizeof (*value) * 8)
22211 {
22212 l &= ((LONGEST) 1 << bits) - 1;
22213 *value = l;
22214 }
22215 else if (bits == sizeof (*value) * 8)
22216 *value = l;
22217 else
22218 {
22219 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
22220 store_unsigned_integer (bytes, bits / 8, byte_order, l);
22221 return bytes;
22222 }
22223
22224 return NULL;
22225 }
22226
22227 /* Read a constant value from an attribute. Either set *VALUE, or if
22228 the value does not fit in *VALUE, set *BYTES - either already
22229 allocated on the objfile obstack, or newly allocated on OBSTACK,
22230 or, set *BATON, if we translated the constant to a location
22231 expression. */
22232
22233 static void
22234 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
22235 const char *name, struct obstack *obstack,
22236 struct dwarf2_cu *cu,
22237 LONGEST *value, const gdb_byte **bytes,
22238 struct dwarf2_locexpr_baton **baton)
22239 {
22240 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22241 struct comp_unit_head *cu_header = &cu->header;
22242 struct dwarf_block *blk;
22243 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
22244 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22245
22246 *value = 0;
22247 *bytes = NULL;
22248 *baton = NULL;
22249
22250 switch (attr->form)
22251 {
22252 case DW_FORM_addr:
22253 case DW_FORM_addrx:
22254 case DW_FORM_GNU_addr_index:
22255 {
22256 gdb_byte *data;
22257
22258 if (TYPE_LENGTH (type) != cu_header->addr_size)
22259 dwarf2_const_value_length_mismatch_complaint (name,
22260 cu_header->addr_size,
22261 TYPE_LENGTH (type));
22262 /* Symbols of this form are reasonably rare, so we just
22263 piggyback on the existing location code rather than writing
22264 a new implementation of symbol_computed_ops. */
22265 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
22266 (*baton)->per_cu = cu->per_cu;
22267 gdb_assert ((*baton)->per_cu);
22268
22269 (*baton)->size = 2 + cu_header->addr_size;
22270 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
22271 (*baton)->data = data;
22272
22273 data[0] = DW_OP_addr;
22274 store_unsigned_integer (&data[1], cu_header->addr_size,
22275 byte_order, DW_ADDR (attr));
22276 data[cu_header->addr_size + 1] = DW_OP_stack_value;
22277 }
22278 break;
22279 case DW_FORM_string:
22280 case DW_FORM_strp:
22281 case DW_FORM_strx:
22282 case DW_FORM_GNU_str_index:
22283 case DW_FORM_GNU_strp_alt:
22284 /* DW_STRING is already allocated on the objfile obstack, point
22285 directly to it. */
22286 *bytes = (const gdb_byte *) DW_STRING (attr);
22287 break;
22288 case DW_FORM_block1:
22289 case DW_FORM_block2:
22290 case DW_FORM_block4:
22291 case DW_FORM_block:
22292 case DW_FORM_exprloc:
22293 case DW_FORM_data16:
22294 blk = DW_BLOCK (attr);
22295 if (TYPE_LENGTH (type) != blk->size)
22296 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
22297 TYPE_LENGTH (type));
22298 *bytes = blk->data;
22299 break;
22300
22301 /* The DW_AT_const_value attributes are supposed to carry the
22302 symbol's value "represented as it would be on the target
22303 architecture." By the time we get here, it's already been
22304 converted to host endianness, so we just need to sign- or
22305 zero-extend it as appropriate. */
22306 case DW_FORM_data1:
22307 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
22308 break;
22309 case DW_FORM_data2:
22310 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
22311 break;
22312 case DW_FORM_data4:
22313 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
22314 break;
22315 case DW_FORM_data8:
22316 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
22317 break;
22318
22319 case DW_FORM_sdata:
22320 case DW_FORM_implicit_const:
22321 *value = DW_SND (attr);
22322 break;
22323
22324 case DW_FORM_udata:
22325 *value = DW_UNSND (attr);
22326 break;
22327
22328 default:
22329 complaint (_("unsupported const value attribute form: '%s'"),
22330 dwarf_form_name (attr->form));
22331 *value = 0;
22332 break;
22333 }
22334 }
22335
22336
22337 /* Copy constant value from an attribute to a symbol. */
22338
22339 static void
22340 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
22341 struct dwarf2_cu *cu)
22342 {
22343 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22344 LONGEST value;
22345 const gdb_byte *bytes;
22346 struct dwarf2_locexpr_baton *baton;
22347
22348 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
22349 sym->print_name (),
22350 &objfile->objfile_obstack, cu,
22351 &value, &bytes, &baton);
22352
22353 if (baton != NULL)
22354 {
22355 SYMBOL_LOCATION_BATON (sym) = baton;
22356 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
22357 }
22358 else if (bytes != NULL)
22359 {
22360 SYMBOL_VALUE_BYTES (sym) = bytes;
22361 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
22362 }
22363 else
22364 {
22365 SYMBOL_VALUE (sym) = value;
22366 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
22367 }
22368 }
22369
22370 /* Return the type of the die in question using its DW_AT_type attribute. */
22371
22372 static struct type *
22373 die_type (struct die_info *die, struct dwarf2_cu *cu)
22374 {
22375 struct attribute *type_attr;
22376
22377 type_attr = dwarf2_attr (die, DW_AT_type, cu);
22378 if (!type_attr)
22379 {
22380 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22381 /* A missing DW_AT_type represents a void type. */
22382 return objfile_type (objfile)->builtin_void;
22383 }
22384
22385 return lookup_die_type (die, type_attr, cu);
22386 }
22387
22388 /* True iff CU's producer generates GNAT Ada auxiliary information
22389 that allows to find parallel types through that information instead
22390 of having to do expensive parallel lookups by type name. */
22391
22392 static int
22393 need_gnat_info (struct dwarf2_cu *cu)
22394 {
22395 /* Assume that the Ada compiler was GNAT, which always produces
22396 the auxiliary information. */
22397 return (cu->language == language_ada);
22398 }
22399
22400 /* Return the auxiliary type of the die in question using its
22401 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
22402 attribute is not present. */
22403
22404 static struct type *
22405 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
22406 {
22407 struct attribute *type_attr;
22408
22409 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
22410 if (!type_attr)
22411 return NULL;
22412
22413 return lookup_die_type (die, type_attr, cu);
22414 }
22415
22416 /* If DIE has a descriptive_type attribute, then set the TYPE's
22417 descriptive type accordingly. */
22418
22419 static void
22420 set_descriptive_type (struct type *type, struct die_info *die,
22421 struct dwarf2_cu *cu)
22422 {
22423 struct type *descriptive_type = die_descriptive_type (die, cu);
22424
22425 if (descriptive_type)
22426 {
22427 ALLOCATE_GNAT_AUX_TYPE (type);
22428 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
22429 }
22430 }
22431
22432 /* Return the containing type of the die in question using its
22433 DW_AT_containing_type attribute. */
22434
22435 static struct type *
22436 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
22437 {
22438 struct attribute *type_attr;
22439 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22440
22441 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
22442 if (!type_attr)
22443 error (_("Dwarf Error: Problem turning containing type into gdb type "
22444 "[in module %s]"), objfile_name (objfile));
22445
22446 return lookup_die_type (die, type_attr, cu);
22447 }
22448
22449 /* Return an error marker type to use for the ill formed type in DIE/CU. */
22450
22451 static struct type *
22452 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22453 {
22454 struct dwarf2_per_objfile *dwarf2_per_objfile
22455 = cu->per_cu->dwarf2_per_objfile;
22456 struct objfile *objfile = dwarf2_per_objfile->objfile;
22457 char *saved;
22458
22459 std::string message
22460 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
22461 objfile_name (objfile),
22462 sect_offset_str (cu->header.sect_off),
22463 sect_offset_str (die->sect_off));
22464 saved = obstack_strdup (&objfile->objfile_obstack, message);
22465
22466 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
22467 }
22468
22469 /* Look up the type of DIE in CU using its type attribute ATTR.
22470 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22471 DW_AT_containing_type.
22472 If there is no type substitute an error marker. */
22473
22474 static struct type *
22475 lookup_die_type (struct die_info *die, const struct attribute *attr,
22476 struct dwarf2_cu *cu)
22477 {
22478 struct dwarf2_per_objfile *dwarf2_per_objfile
22479 = cu->per_cu->dwarf2_per_objfile;
22480 struct objfile *objfile = dwarf2_per_objfile->objfile;
22481 struct type *this_type;
22482
22483 gdb_assert (attr->name == DW_AT_type
22484 || attr->name == DW_AT_GNAT_descriptive_type
22485 || attr->name == DW_AT_containing_type);
22486
22487 /* First see if we have it cached. */
22488
22489 if (attr->form == DW_FORM_GNU_ref_alt)
22490 {
22491 struct dwarf2_per_cu_data *per_cu;
22492 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
22493
22494 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
22495 dwarf2_per_objfile);
22496 this_type = get_die_type_at_offset (sect_off, per_cu);
22497 }
22498 else if (attr_form_is_ref (attr))
22499 {
22500 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
22501
22502 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
22503 }
22504 else if (attr->form == DW_FORM_ref_sig8)
22505 {
22506 ULONGEST signature = DW_SIGNATURE (attr);
22507
22508 return get_signatured_type (die, signature, cu);
22509 }
22510 else
22511 {
22512 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
22513 " at %s [in module %s]"),
22514 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
22515 objfile_name (objfile));
22516 return build_error_marker_type (cu, die);
22517 }
22518
22519 /* If not cached we need to read it in. */
22520
22521 if (this_type == NULL)
22522 {
22523 struct die_info *type_die = NULL;
22524 struct dwarf2_cu *type_cu = cu;
22525
22526 if (attr_form_is_ref (attr))
22527 type_die = follow_die_ref (die, attr, &type_cu);
22528 if (type_die == NULL)
22529 return build_error_marker_type (cu, die);
22530 /* If we find the type now, it's probably because the type came
22531 from an inter-CU reference and the type's CU got expanded before
22532 ours. */
22533 this_type = read_type_die (type_die, type_cu);
22534 }
22535
22536 /* If we still don't have a type use an error marker. */
22537
22538 if (this_type == NULL)
22539 return build_error_marker_type (cu, die);
22540
22541 return this_type;
22542 }
22543
22544 /* Return the type in DIE, CU.
22545 Returns NULL for invalid types.
22546
22547 This first does a lookup in die_type_hash,
22548 and only reads the die in if necessary.
22549
22550 NOTE: This can be called when reading in partial or full symbols. */
22551
22552 static struct type *
22553 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
22554 {
22555 struct type *this_type;
22556
22557 this_type = get_die_type (die, cu);
22558 if (this_type)
22559 return this_type;
22560
22561 return read_type_die_1 (die, cu);
22562 }
22563
22564 /* Read the type in DIE, CU.
22565 Returns NULL for invalid types. */
22566
22567 static struct type *
22568 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22569 {
22570 struct type *this_type = NULL;
22571
22572 switch (die->tag)
22573 {
22574 case DW_TAG_class_type:
22575 case DW_TAG_interface_type:
22576 case DW_TAG_structure_type:
22577 case DW_TAG_union_type:
22578 this_type = read_structure_type (die, cu);
22579 break;
22580 case DW_TAG_enumeration_type:
22581 this_type = read_enumeration_type (die, cu);
22582 break;
22583 case DW_TAG_subprogram:
22584 case DW_TAG_subroutine_type:
22585 case DW_TAG_inlined_subroutine:
22586 this_type = read_subroutine_type (die, cu);
22587 break;
22588 case DW_TAG_array_type:
22589 this_type = read_array_type (die, cu);
22590 break;
22591 case DW_TAG_set_type:
22592 this_type = read_set_type (die, cu);
22593 break;
22594 case DW_TAG_pointer_type:
22595 this_type = read_tag_pointer_type (die, cu);
22596 break;
22597 case DW_TAG_ptr_to_member_type:
22598 this_type = read_tag_ptr_to_member_type (die, cu);
22599 break;
22600 case DW_TAG_reference_type:
22601 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22602 break;
22603 case DW_TAG_rvalue_reference_type:
22604 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
22605 break;
22606 case DW_TAG_const_type:
22607 this_type = read_tag_const_type (die, cu);
22608 break;
22609 case DW_TAG_volatile_type:
22610 this_type = read_tag_volatile_type (die, cu);
22611 break;
22612 case DW_TAG_restrict_type:
22613 this_type = read_tag_restrict_type (die, cu);
22614 break;
22615 case DW_TAG_string_type:
22616 this_type = read_tag_string_type (die, cu);
22617 break;
22618 case DW_TAG_typedef:
22619 this_type = read_typedef (die, cu);
22620 break;
22621 case DW_TAG_subrange_type:
22622 this_type = read_subrange_type (die, cu);
22623 break;
22624 case DW_TAG_base_type:
22625 this_type = read_base_type (die, cu);
22626 break;
22627 case DW_TAG_unspecified_type:
22628 this_type = read_unspecified_type (die, cu);
22629 break;
22630 case DW_TAG_namespace:
22631 this_type = read_namespace_type (die, cu);
22632 break;
22633 case DW_TAG_module:
22634 this_type = read_module_type (die, cu);
22635 break;
22636 case DW_TAG_atomic_type:
22637 this_type = read_tag_atomic_type (die, cu);
22638 break;
22639 default:
22640 complaint (_("unexpected tag in read_type_die: '%s'"),
22641 dwarf_tag_name (die->tag));
22642 break;
22643 }
22644
22645 return this_type;
22646 }
22647
22648 /* See if we can figure out if the class lives in a namespace. We do
22649 this by looking for a member function; its demangled name will
22650 contain namespace info, if there is any.
22651 Return the computed name or NULL.
22652 Space for the result is allocated on the objfile's obstack.
22653 This is the full-die version of guess_partial_die_structure_name.
22654 In this case we know DIE has no useful parent. */
22655
22656 static const char *
22657 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22658 {
22659 struct die_info *spec_die;
22660 struct dwarf2_cu *spec_cu;
22661 struct die_info *child;
22662 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22663
22664 spec_cu = cu;
22665 spec_die = die_specification (die, &spec_cu);
22666 if (spec_die != NULL)
22667 {
22668 die = spec_die;
22669 cu = spec_cu;
22670 }
22671
22672 for (child = die->child;
22673 child != NULL;
22674 child = child->sibling)
22675 {
22676 if (child->tag == DW_TAG_subprogram)
22677 {
22678 const char *linkage_name = dw2_linkage_name (child, cu);
22679
22680 if (linkage_name != NULL)
22681 {
22682 gdb::unique_xmalloc_ptr<char> actual_name
22683 (language_class_name_from_physname (cu->language_defn,
22684 linkage_name));
22685 const char *name = NULL;
22686
22687 if (actual_name != NULL)
22688 {
22689 const char *die_name = dwarf2_name (die, cu);
22690
22691 if (die_name != NULL
22692 && strcmp (die_name, actual_name.get ()) != 0)
22693 {
22694 /* Strip off the class name from the full name.
22695 We want the prefix. */
22696 int die_name_len = strlen (die_name);
22697 int actual_name_len = strlen (actual_name.get ());
22698 const char *ptr = actual_name.get ();
22699
22700 /* Test for '::' as a sanity check. */
22701 if (actual_name_len > die_name_len + 2
22702 && ptr[actual_name_len - die_name_len - 1] == ':')
22703 name = obstack_strndup (
22704 &objfile->per_bfd->storage_obstack,
22705 ptr, actual_name_len - die_name_len - 2);
22706 }
22707 }
22708 return name;
22709 }
22710 }
22711 }
22712
22713 return NULL;
22714 }
22715
22716 /* GCC might emit a nameless typedef that has a linkage name. Determine the
22717 prefix part in such case. See
22718 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22719
22720 static const char *
22721 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22722 {
22723 struct attribute *attr;
22724 const char *base;
22725
22726 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22727 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22728 return NULL;
22729
22730 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
22731 return NULL;
22732
22733 attr = dw2_linkage_name_attr (die, cu);
22734 if (attr == NULL || DW_STRING (attr) == NULL)
22735 return NULL;
22736
22737 /* dwarf2_name had to be already called. */
22738 gdb_assert (DW_STRING_IS_CANONICAL (attr));
22739
22740 /* Strip the base name, keep any leading namespaces/classes. */
22741 base = strrchr (DW_STRING (attr), ':');
22742 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
22743 return "";
22744
22745 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22746 return obstack_strndup (&objfile->per_bfd->storage_obstack,
22747 DW_STRING (attr),
22748 &base[-1] - DW_STRING (attr));
22749 }
22750
22751 /* Return the name of the namespace/class that DIE is defined within,
22752 or "" if we can't tell. The caller should not xfree the result.
22753
22754 For example, if we're within the method foo() in the following
22755 code:
22756
22757 namespace N {
22758 class C {
22759 void foo () {
22760 }
22761 };
22762 }
22763
22764 then determine_prefix on foo's die will return "N::C". */
22765
22766 static const char *
22767 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
22768 {
22769 struct dwarf2_per_objfile *dwarf2_per_objfile
22770 = cu->per_cu->dwarf2_per_objfile;
22771 struct die_info *parent, *spec_die;
22772 struct dwarf2_cu *spec_cu;
22773 struct type *parent_type;
22774 const char *retval;
22775
22776 if (cu->language != language_cplus
22777 && cu->language != language_fortran && cu->language != language_d
22778 && cu->language != language_rust)
22779 return "";
22780
22781 retval = anonymous_struct_prefix (die, cu);
22782 if (retval)
22783 return retval;
22784
22785 /* We have to be careful in the presence of DW_AT_specification.
22786 For example, with GCC 3.4, given the code
22787
22788 namespace N {
22789 void foo() {
22790 // Definition of N::foo.
22791 }
22792 }
22793
22794 then we'll have a tree of DIEs like this:
22795
22796 1: DW_TAG_compile_unit
22797 2: DW_TAG_namespace // N
22798 3: DW_TAG_subprogram // declaration of N::foo
22799 4: DW_TAG_subprogram // definition of N::foo
22800 DW_AT_specification // refers to die #3
22801
22802 Thus, when processing die #4, we have to pretend that we're in
22803 the context of its DW_AT_specification, namely the contex of die
22804 #3. */
22805 spec_cu = cu;
22806 spec_die = die_specification (die, &spec_cu);
22807 if (spec_die == NULL)
22808 parent = die->parent;
22809 else
22810 {
22811 parent = spec_die->parent;
22812 cu = spec_cu;
22813 }
22814
22815 if (parent == NULL)
22816 return "";
22817 else if (parent->building_fullname)
22818 {
22819 const char *name;
22820 const char *parent_name;
22821
22822 /* It has been seen on RealView 2.2 built binaries,
22823 DW_TAG_template_type_param types actually _defined_ as
22824 children of the parent class:
22825
22826 enum E {};
22827 template class <class Enum> Class{};
22828 Class<enum E> class_e;
22829
22830 1: DW_TAG_class_type (Class)
22831 2: DW_TAG_enumeration_type (E)
22832 3: DW_TAG_enumerator (enum1:0)
22833 3: DW_TAG_enumerator (enum2:1)
22834 ...
22835 2: DW_TAG_template_type_param
22836 DW_AT_type DW_FORM_ref_udata (E)
22837
22838 Besides being broken debug info, it can put GDB into an
22839 infinite loop. Consider:
22840
22841 When we're building the full name for Class<E>, we'll start
22842 at Class, and go look over its template type parameters,
22843 finding E. We'll then try to build the full name of E, and
22844 reach here. We're now trying to build the full name of E,
22845 and look over the parent DIE for containing scope. In the
22846 broken case, if we followed the parent DIE of E, we'd again
22847 find Class, and once again go look at its template type
22848 arguments, etc., etc. Simply don't consider such parent die
22849 as source-level parent of this die (it can't be, the language
22850 doesn't allow it), and break the loop here. */
22851 name = dwarf2_name (die, cu);
22852 parent_name = dwarf2_name (parent, cu);
22853 complaint (_("template param type '%s' defined within parent '%s'"),
22854 name ? name : "<unknown>",
22855 parent_name ? parent_name : "<unknown>");
22856 return "";
22857 }
22858 else
22859 switch (parent->tag)
22860 {
22861 case DW_TAG_namespace:
22862 parent_type = read_type_die (parent, cu);
22863 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22864 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22865 Work around this problem here. */
22866 if (cu->language == language_cplus
22867 && strcmp (TYPE_NAME (parent_type), "::") == 0)
22868 return "";
22869 /* We give a name to even anonymous namespaces. */
22870 return TYPE_NAME (parent_type);
22871 case DW_TAG_class_type:
22872 case DW_TAG_interface_type:
22873 case DW_TAG_structure_type:
22874 case DW_TAG_union_type:
22875 case DW_TAG_module:
22876 parent_type = read_type_die (parent, cu);
22877 if (TYPE_NAME (parent_type) != NULL)
22878 return TYPE_NAME (parent_type);
22879 else
22880 /* An anonymous structure is only allowed non-static data
22881 members; no typedefs, no member functions, et cetera.
22882 So it does not need a prefix. */
22883 return "";
22884 case DW_TAG_compile_unit:
22885 case DW_TAG_partial_unit:
22886 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22887 if (cu->language == language_cplus
22888 && !dwarf2_per_objfile->types.empty ()
22889 && die->child != NULL
22890 && (die->tag == DW_TAG_class_type
22891 || die->tag == DW_TAG_structure_type
22892 || die->tag == DW_TAG_union_type))
22893 {
22894 const char *name = guess_full_die_structure_name (die, cu);
22895 if (name != NULL)
22896 return name;
22897 }
22898 return "";
22899 case DW_TAG_subprogram:
22900 /* Nested subroutines in Fortran get a prefix with the name
22901 of the parent's subroutine. */
22902 if (cu->language == language_fortran)
22903 {
22904 if ((die->tag == DW_TAG_subprogram)
22905 && (dwarf2_name (parent, cu) != NULL))
22906 return dwarf2_name (parent, cu);
22907 }
22908 return determine_prefix (parent, cu);
22909 case DW_TAG_enumeration_type:
22910 parent_type = read_type_die (parent, cu);
22911 if (TYPE_DECLARED_CLASS (parent_type))
22912 {
22913 if (TYPE_NAME (parent_type) != NULL)
22914 return TYPE_NAME (parent_type);
22915 return "";
22916 }
22917 /* Fall through. */
22918 default:
22919 return determine_prefix (parent, cu);
22920 }
22921 }
22922
22923 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22924 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22925 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22926 an obconcat, otherwise allocate storage for the result. The CU argument is
22927 used to determine the language and hence, the appropriate separator. */
22928
22929 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
22930
22931 static char *
22932 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22933 int physname, struct dwarf2_cu *cu)
22934 {
22935 const char *lead = "";
22936 const char *sep;
22937
22938 if (suffix == NULL || suffix[0] == '\0'
22939 || prefix == NULL || prefix[0] == '\0')
22940 sep = "";
22941 else if (cu->language == language_d)
22942 {
22943 /* For D, the 'main' function could be defined in any module, but it
22944 should never be prefixed. */
22945 if (strcmp (suffix, "D main") == 0)
22946 {
22947 prefix = "";
22948 sep = "";
22949 }
22950 else
22951 sep = ".";
22952 }
22953 else if (cu->language == language_fortran && physname)
22954 {
22955 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22956 DW_AT_MIPS_linkage_name is preferred and used instead. */
22957
22958 lead = "__";
22959 sep = "_MOD_";
22960 }
22961 else
22962 sep = "::";
22963
22964 if (prefix == NULL)
22965 prefix = "";
22966 if (suffix == NULL)
22967 suffix = "";
22968
22969 if (obs == NULL)
22970 {
22971 char *retval
22972 = ((char *)
22973 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
22974
22975 strcpy (retval, lead);
22976 strcat (retval, prefix);
22977 strcat (retval, sep);
22978 strcat (retval, suffix);
22979 return retval;
22980 }
22981 else
22982 {
22983 /* We have an obstack. */
22984 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
22985 }
22986 }
22987
22988 /* Return sibling of die, NULL if no sibling. */
22989
22990 static struct die_info *
22991 sibling_die (struct die_info *die)
22992 {
22993 return die->sibling;
22994 }
22995
22996 /* Get name of a die, return NULL if not found. */
22997
22998 static const char *
22999 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
23000 struct obstack *obstack)
23001 {
23002 if (name && cu->language == language_cplus)
23003 {
23004 std::string canon_name = cp_canonicalize_string (name);
23005
23006 if (!canon_name.empty ())
23007 {
23008 if (canon_name != name)
23009 name = obstack_strdup (obstack, canon_name);
23010 }
23011 }
23012
23013 return name;
23014 }
23015
23016 /* Get name of a die, return NULL if not found.
23017 Anonymous namespaces are converted to their magic string. */
23018
23019 static const char *
23020 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
23021 {
23022 struct attribute *attr;
23023 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
23024
23025 attr = dwarf2_attr (die, DW_AT_name, cu);
23026 if ((!attr || !DW_STRING (attr))
23027 && die->tag != DW_TAG_namespace
23028 && die->tag != DW_TAG_class_type
23029 && die->tag != DW_TAG_interface_type
23030 && die->tag != DW_TAG_structure_type
23031 && die->tag != DW_TAG_union_type)
23032 return NULL;
23033
23034 switch (die->tag)
23035 {
23036 case DW_TAG_compile_unit:
23037 case DW_TAG_partial_unit:
23038 /* Compilation units have a DW_AT_name that is a filename, not
23039 a source language identifier. */
23040 case DW_TAG_enumeration_type:
23041 case DW_TAG_enumerator:
23042 /* These tags always have simple identifiers already; no need
23043 to canonicalize them. */
23044 return DW_STRING (attr);
23045
23046 case DW_TAG_namespace:
23047 if (attr != NULL && DW_STRING (attr) != NULL)
23048 return DW_STRING (attr);
23049 return CP_ANONYMOUS_NAMESPACE_STR;
23050
23051 case DW_TAG_class_type:
23052 case DW_TAG_interface_type:
23053 case DW_TAG_structure_type:
23054 case DW_TAG_union_type:
23055 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
23056 structures or unions. These were of the form "._%d" in GCC 4.1,
23057 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
23058 and GCC 4.4. We work around this problem by ignoring these. */
23059 if (attr && DW_STRING (attr)
23060 && (startswith (DW_STRING (attr), "._")
23061 || startswith (DW_STRING (attr), "<anonymous")))
23062 return NULL;
23063
23064 /* GCC might emit a nameless typedef that has a linkage name. See
23065 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
23066 if (!attr || DW_STRING (attr) == NULL)
23067 {
23068 attr = dw2_linkage_name_attr (die, cu);
23069 if (attr == NULL || DW_STRING (attr) == NULL)
23070 return NULL;
23071
23072 /* Avoid demangling DW_STRING (attr) the second time on a second
23073 call for the same DIE. */
23074 if (!DW_STRING_IS_CANONICAL (attr))
23075 {
23076 gdb::unique_xmalloc_ptr<char> demangled
23077 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
23078
23079 const char *base;
23080
23081 /* FIXME: we already did this for the partial symbol... */
23082 DW_STRING (attr)
23083 = obstack_strdup (&objfile->per_bfd->storage_obstack,
23084 demangled.get ());
23085 DW_STRING_IS_CANONICAL (attr) = 1;
23086
23087 /* Strip any leading namespaces/classes, keep only the base name.
23088 DW_AT_name for named DIEs does not contain the prefixes. */
23089 base = strrchr (DW_STRING (attr), ':');
23090 if (base && base > DW_STRING (attr) && base[-1] == ':')
23091 return &base[1];
23092 else
23093 return DW_STRING (attr);
23094 }
23095 }
23096 break;
23097
23098 default:
23099 break;
23100 }
23101
23102 if (!DW_STRING_IS_CANONICAL (attr))
23103 {
23104 DW_STRING (attr)
23105 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
23106 &objfile->per_bfd->storage_obstack);
23107 DW_STRING_IS_CANONICAL (attr) = 1;
23108 }
23109 return DW_STRING (attr);
23110 }
23111
23112 /* Return the die that this die in an extension of, or NULL if there
23113 is none. *EXT_CU is the CU containing DIE on input, and the CU
23114 containing the return value on output. */
23115
23116 static struct die_info *
23117 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
23118 {
23119 struct attribute *attr;
23120
23121 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
23122 if (attr == NULL)
23123 return NULL;
23124
23125 return follow_die_ref (die, attr, ext_cu);
23126 }
23127
23128 /* A convenience function that returns an "unknown" DWARF name,
23129 including the value of V. STR is the name of the entity being
23130 printed, e.g., "TAG". */
23131
23132 static const char *
23133 dwarf_unknown (const char *str, unsigned v)
23134 {
23135 char *cell = get_print_cell ();
23136 xsnprintf (cell, PRINT_CELL_SIZE, "DW_%s_<unknown: %u>", str, v);
23137 return cell;
23138 }
23139
23140 /* Convert a DIE tag into its string name. */
23141
23142 static const char *
23143 dwarf_tag_name (unsigned tag)
23144 {
23145 const char *name = get_DW_TAG_name (tag);
23146
23147 if (name == NULL)
23148 return dwarf_unknown ("TAG", tag);
23149
23150 return name;
23151 }
23152
23153 /* Convert a DWARF attribute code into its string name. */
23154
23155 static const char *
23156 dwarf_attr_name (unsigned attr)
23157 {
23158 const char *name;
23159
23160 #ifdef MIPS /* collides with DW_AT_HP_block_index */
23161 if (attr == DW_AT_MIPS_fde)
23162 return "DW_AT_MIPS_fde";
23163 #else
23164 if (attr == DW_AT_HP_block_index)
23165 return "DW_AT_HP_block_index";
23166 #endif
23167
23168 name = get_DW_AT_name (attr);
23169
23170 if (name == NULL)
23171 return dwarf_unknown ("AT", attr);
23172
23173 return name;
23174 }
23175
23176 /* Convert a unit type to corresponding DW_UT name. */
23177
23178 static const char *
23179 dwarf_unit_type_name (int unit_type) {
23180 switch (unit_type)
23181 {
23182 case 0x01:
23183 return "DW_UT_compile (0x01)";
23184 case 0x02:
23185 return "DW_UT_type (0x02)";
23186 case 0x03:
23187 return "DW_UT_partial (0x03)";
23188 case 0x04:
23189 return "DW_UT_skeleton (0x04)";
23190 case 0x05:
23191 return "DW_UT_split_compile (0x05)";
23192 case 0x06:
23193 return "DW_UT_split_type (0x06)";
23194 case 0x80:
23195 return "DW_UT_lo_user (0x80)";
23196 case 0xff:
23197 return "DW_UT_hi_user (0xff)";
23198 default:
23199 return nullptr;
23200 }
23201 }
23202
23203 /* Convert a DWARF value form code into its string name. */
23204
23205 static const char *
23206 dwarf_form_name (unsigned form)
23207 {
23208 const char *name = get_DW_FORM_name (form);
23209
23210 if (name == NULL)
23211 return dwarf_unknown ("FORM", form);
23212
23213 return name;
23214 }
23215
23216 static const char *
23217 dwarf_bool_name (unsigned mybool)
23218 {
23219 if (mybool)
23220 return "TRUE";
23221 else
23222 return "FALSE";
23223 }
23224
23225 /* Convert a DWARF type code into its string name. */
23226
23227 static const char *
23228 dwarf_type_encoding_name (unsigned enc)
23229 {
23230 const char *name = get_DW_ATE_name (enc);
23231
23232 if (name == NULL)
23233 return dwarf_unknown ("ATE", enc);
23234
23235 return name;
23236 }
23237
23238 static void
23239 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
23240 {
23241 unsigned int i;
23242
23243 print_spaces (indent, f);
23244 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
23245 dwarf_tag_name (die->tag), die->abbrev,
23246 sect_offset_str (die->sect_off));
23247
23248 if (die->parent != NULL)
23249 {
23250 print_spaces (indent, f);
23251 fprintf_unfiltered (f, " parent at offset: %s\n",
23252 sect_offset_str (die->parent->sect_off));
23253 }
23254
23255 print_spaces (indent, f);
23256 fprintf_unfiltered (f, " has children: %s\n",
23257 dwarf_bool_name (die->child != NULL));
23258
23259 print_spaces (indent, f);
23260 fprintf_unfiltered (f, " attributes:\n");
23261
23262 for (i = 0; i < die->num_attrs; ++i)
23263 {
23264 print_spaces (indent, f);
23265 fprintf_unfiltered (f, " %s (%s) ",
23266 dwarf_attr_name (die->attrs[i].name),
23267 dwarf_form_name (die->attrs[i].form));
23268
23269 switch (die->attrs[i].form)
23270 {
23271 case DW_FORM_addr:
23272 case DW_FORM_addrx:
23273 case DW_FORM_GNU_addr_index:
23274 fprintf_unfiltered (f, "address: ");
23275 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
23276 break;
23277 case DW_FORM_block2:
23278 case DW_FORM_block4:
23279 case DW_FORM_block:
23280 case DW_FORM_block1:
23281 fprintf_unfiltered (f, "block: size %s",
23282 pulongest (DW_BLOCK (&die->attrs[i])->size));
23283 break;
23284 case DW_FORM_exprloc:
23285 fprintf_unfiltered (f, "expression: size %s",
23286 pulongest (DW_BLOCK (&die->attrs[i])->size));
23287 break;
23288 case DW_FORM_data16:
23289 fprintf_unfiltered (f, "constant of 16 bytes");
23290 break;
23291 case DW_FORM_ref_addr:
23292 fprintf_unfiltered (f, "ref address: ");
23293 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
23294 break;
23295 case DW_FORM_GNU_ref_alt:
23296 fprintf_unfiltered (f, "alt ref address: ");
23297 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
23298 break;
23299 case DW_FORM_ref1:
23300 case DW_FORM_ref2:
23301 case DW_FORM_ref4:
23302 case DW_FORM_ref8:
23303 case DW_FORM_ref_udata:
23304 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
23305 (long) (DW_UNSND (&die->attrs[i])));
23306 break;
23307 case DW_FORM_data1:
23308 case DW_FORM_data2:
23309 case DW_FORM_data4:
23310 case DW_FORM_data8:
23311 case DW_FORM_udata:
23312 case DW_FORM_sdata:
23313 fprintf_unfiltered (f, "constant: %s",
23314 pulongest (DW_UNSND (&die->attrs[i])));
23315 break;
23316 case DW_FORM_sec_offset:
23317 fprintf_unfiltered (f, "section offset: %s",
23318 pulongest (DW_UNSND (&die->attrs[i])));
23319 break;
23320 case DW_FORM_ref_sig8:
23321 fprintf_unfiltered (f, "signature: %s",
23322 hex_string (DW_SIGNATURE (&die->attrs[i])));
23323 break;
23324 case DW_FORM_string:
23325 case DW_FORM_strp:
23326 case DW_FORM_line_strp:
23327 case DW_FORM_strx:
23328 case DW_FORM_GNU_str_index:
23329 case DW_FORM_GNU_strp_alt:
23330 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
23331 DW_STRING (&die->attrs[i])
23332 ? DW_STRING (&die->attrs[i]) : "",
23333 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
23334 break;
23335 case DW_FORM_flag:
23336 if (DW_UNSND (&die->attrs[i]))
23337 fprintf_unfiltered (f, "flag: TRUE");
23338 else
23339 fprintf_unfiltered (f, "flag: FALSE");
23340 break;
23341 case DW_FORM_flag_present:
23342 fprintf_unfiltered (f, "flag: TRUE");
23343 break;
23344 case DW_FORM_indirect:
23345 /* The reader will have reduced the indirect form to
23346 the "base form" so this form should not occur. */
23347 fprintf_unfiltered (f,
23348 "unexpected attribute form: DW_FORM_indirect");
23349 break;
23350 case DW_FORM_implicit_const:
23351 fprintf_unfiltered (f, "constant: %s",
23352 plongest (DW_SND (&die->attrs[i])));
23353 break;
23354 default:
23355 fprintf_unfiltered (f, "unsupported attribute form: %d.",
23356 die->attrs[i].form);
23357 break;
23358 }
23359 fprintf_unfiltered (f, "\n");
23360 }
23361 }
23362
23363 static void
23364 dump_die_for_error (struct die_info *die)
23365 {
23366 dump_die_shallow (gdb_stderr, 0, die);
23367 }
23368
23369 static void
23370 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
23371 {
23372 int indent = level * 4;
23373
23374 gdb_assert (die != NULL);
23375
23376 if (level >= max_level)
23377 return;
23378
23379 dump_die_shallow (f, indent, die);
23380
23381 if (die->child != NULL)
23382 {
23383 print_spaces (indent, f);
23384 fprintf_unfiltered (f, " Children:");
23385 if (level + 1 < max_level)
23386 {
23387 fprintf_unfiltered (f, "\n");
23388 dump_die_1 (f, level + 1, max_level, die->child);
23389 }
23390 else
23391 {
23392 fprintf_unfiltered (f,
23393 " [not printed, max nesting level reached]\n");
23394 }
23395 }
23396
23397 if (die->sibling != NULL && level > 0)
23398 {
23399 dump_die_1 (f, level, max_level, die->sibling);
23400 }
23401 }
23402
23403 /* This is called from the pdie macro in gdbinit.in.
23404 It's not static so gcc will keep a copy callable from gdb. */
23405
23406 void
23407 dump_die (struct die_info *die, int max_level)
23408 {
23409 dump_die_1 (gdb_stdlog, 0, max_level, die);
23410 }
23411
23412 static void
23413 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
23414 {
23415 void **slot;
23416
23417 slot = htab_find_slot_with_hash (cu->die_hash, die,
23418 to_underlying (die->sect_off),
23419 INSERT);
23420
23421 *slot = die;
23422 }
23423
23424 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
23425 required kind. */
23426
23427 static sect_offset
23428 dwarf2_get_ref_die_offset (const struct attribute *attr)
23429 {
23430 if (attr_form_is_ref (attr))
23431 return (sect_offset) DW_UNSND (attr);
23432
23433 complaint (_("unsupported die ref attribute form: '%s'"),
23434 dwarf_form_name (attr->form));
23435 return {};
23436 }
23437
23438 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
23439 * the value held by the attribute is not constant. */
23440
23441 static LONGEST
23442 dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
23443 {
23444 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
23445 return DW_SND (attr);
23446 else if (attr->form == DW_FORM_udata
23447 || attr->form == DW_FORM_data1
23448 || attr->form == DW_FORM_data2
23449 || attr->form == DW_FORM_data4
23450 || attr->form == DW_FORM_data8)
23451 return DW_UNSND (attr);
23452 else
23453 {
23454 /* For DW_FORM_data16 see attr_form_is_constant. */
23455 complaint (_("Attribute value is not a constant (%s)"),
23456 dwarf_form_name (attr->form));
23457 return default_value;
23458 }
23459 }
23460
23461 /* Follow reference or signature attribute ATTR of SRC_DIE.
23462 On entry *REF_CU is the CU of SRC_DIE.
23463 On exit *REF_CU is the CU of the result. */
23464
23465 static struct die_info *
23466 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
23467 struct dwarf2_cu **ref_cu)
23468 {
23469 struct die_info *die;
23470
23471 if (attr_form_is_ref (attr))
23472 die = follow_die_ref (src_die, attr, ref_cu);
23473 else if (attr->form == DW_FORM_ref_sig8)
23474 die = follow_die_sig (src_die, attr, ref_cu);
23475 else
23476 {
23477 dump_die_for_error (src_die);
23478 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
23479 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23480 }
23481
23482 return die;
23483 }
23484
23485 /* Follow reference OFFSET.
23486 On entry *REF_CU is the CU of the source die referencing OFFSET.
23487 On exit *REF_CU is the CU of the result.
23488 Returns NULL if OFFSET is invalid. */
23489
23490 static struct die_info *
23491 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
23492 struct dwarf2_cu **ref_cu)
23493 {
23494 struct die_info temp_die;
23495 struct dwarf2_cu *target_cu, *cu = *ref_cu;
23496 struct dwarf2_per_objfile *dwarf2_per_objfile
23497 = cu->per_cu->dwarf2_per_objfile;
23498
23499 gdb_assert (cu->per_cu != NULL);
23500
23501 target_cu = cu;
23502
23503 if (cu->per_cu->is_debug_types)
23504 {
23505 /* .debug_types CUs cannot reference anything outside their CU.
23506 If they need to, they have to reference a signatured type via
23507 DW_FORM_ref_sig8. */
23508 if (!offset_in_cu_p (&cu->header, sect_off))
23509 return NULL;
23510 }
23511 else if (offset_in_dwz != cu->per_cu->is_dwz
23512 || !offset_in_cu_p (&cu->header, sect_off))
23513 {
23514 struct dwarf2_per_cu_data *per_cu;
23515
23516 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
23517 dwarf2_per_objfile);
23518
23519 /* If necessary, add it to the queue and load its DIEs. */
23520 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
23521 load_full_comp_unit (per_cu, false, cu->language);
23522
23523 target_cu = per_cu->cu;
23524 }
23525 else if (cu->dies == NULL)
23526 {
23527 /* We're loading full DIEs during partial symbol reading. */
23528 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
23529 load_full_comp_unit (cu->per_cu, false, language_minimal);
23530 }
23531
23532 *ref_cu = target_cu;
23533 temp_die.sect_off = sect_off;
23534
23535 if (target_cu != cu)
23536 target_cu->ancestor = cu;
23537
23538 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
23539 &temp_die,
23540 to_underlying (sect_off));
23541 }
23542
23543 /* Follow reference attribute ATTR of SRC_DIE.
23544 On entry *REF_CU is the CU of SRC_DIE.
23545 On exit *REF_CU is the CU of the result. */
23546
23547 static struct die_info *
23548 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
23549 struct dwarf2_cu **ref_cu)
23550 {
23551 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
23552 struct dwarf2_cu *cu = *ref_cu;
23553 struct die_info *die;
23554
23555 die = follow_die_offset (sect_off,
23556 (attr->form == DW_FORM_GNU_ref_alt
23557 || cu->per_cu->is_dwz),
23558 ref_cu);
23559 if (!die)
23560 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
23561 "at %s [in module %s]"),
23562 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
23563 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
23564
23565 return die;
23566 }
23567
23568 /* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
23569 Returned value is intended for DW_OP_call*. Returned
23570 dwarf2_locexpr_baton->data has lifetime of
23571 PER_CU->DWARF2_PER_OBJFILE->OBJFILE. */
23572
23573 struct dwarf2_locexpr_baton
23574 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
23575 struct dwarf2_per_cu_data *per_cu,
23576 CORE_ADDR (*get_frame_pc) (void *baton),
23577 void *baton, bool resolve_abstract_p)
23578 {
23579 struct dwarf2_cu *cu;
23580 struct die_info *die;
23581 struct attribute *attr;
23582 struct dwarf2_locexpr_baton retval;
23583 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
23584 struct objfile *objfile = dwarf2_per_objfile->objfile;
23585
23586 if (per_cu->cu == NULL)
23587 load_cu (per_cu, false);
23588 cu = per_cu->cu;
23589 if (cu == NULL)
23590 {
23591 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23592 Instead just throw an error, not much else we can do. */
23593 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23594 sect_offset_str (sect_off), objfile_name (objfile));
23595 }
23596
23597 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23598 if (!die)
23599 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23600 sect_offset_str (sect_off), objfile_name (objfile));
23601
23602 attr = dwarf2_attr (die, DW_AT_location, cu);
23603 if (!attr && resolve_abstract_p
23604 && (dwarf2_per_objfile->abstract_to_concrete.find (die->sect_off)
23605 != dwarf2_per_objfile->abstract_to_concrete.end ()))
23606 {
23607 CORE_ADDR pc = (*get_frame_pc) (baton);
23608 CORE_ADDR baseaddr = objfile->section_offsets[SECT_OFF_TEXT (objfile)];
23609 struct gdbarch *gdbarch = get_objfile_arch (objfile);
23610
23611 for (const auto &cand_off
23612 : dwarf2_per_objfile->abstract_to_concrete[die->sect_off])
23613 {
23614 struct dwarf2_cu *cand_cu = cu;
23615 struct die_info *cand
23616 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
23617 if (!cand
23618 || !cand->parent
23619 || cand->parent->tag != DW_TAG_subprogram)
23620 continue;
23621
23622 CORE_ADDR pc_low, pc_high;
23623 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
23624 if (pc_low == ((CORE_ADDR) -1))
23625 continue;
23626 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
23627 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
23628 if (!(pc_low <= pc && pc < pc_high))
23629 continue;
23630
23631 die = cand;
23632 attr = dwarf2_attr (die, DW_AT_location, cu);
23633 break;
23634 }
23635 }
23636
23637 if (!attr)
23638 {
23639 /* DWARF: "If there is no such attribute, then there is no effect.".
23640 DATA is ignored if SIZE is 0. */
23641
23642 retval.data = NULL;
23643 retval.size = 0;
23644 }
23645 else if (attr_form_is_section_offset (attr))
23646 {
23647 struct dwarf2_loclist_baton loclist_baton;
23648 CORE_ADDR pc = (*get_frame_pc) (baton);
23649 size_t size;
23650
23651 fill_in_loclist_baton (cu, &loclist_baton, attr);
23652
23653 retval.data = dwarf2_find_location_expression (&loclist_baton,
23654 &size, pc);
23655 retval.size = size;
23656 }
23657 else
23658 {
23659 if (!attr_form_is_block (attr))
23660 error (_("Dwarf Error: DIE at %s referenced in module %s "
23661 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
23662 sect_offset_str (sect_off), objfile_name (objfile));
23663
23664 retval.data = DW_BLOCK (attr)->data;
23665 retval.size = DW_BLOCK (attr)->size;
23666 }
23667 retval.per_cu = cu->per_cu;
23668
23669 age_cached_comp_units (dwarf2_per_objfile);
23670
23671 return retval;
23672 }
23673
23674 /* Like dwarf2_fetch_die_loc_sect_off, but take a CU
23675 offset. */
23676
23677 struct dwarf2_locexpr_baton
23678 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
23679 struct dwarf2_per_cu_data *per_cu,
23680 CORE_ADDR (*get_frame_pc) (void *baton),
23681 void *baton)
23682 {
23683 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
23684
23685 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
23686 }
23687
23688 /* Write a constant of a given type as target-ordered bytes into
23689 OBSTACK. */
23690
23691 static const gdb_byte *
23692 write_constant_as_bytes (struct obstack *obstack,
23693 enum bfd_endian byte_order,
23694 struct type *type,
23695 ULONGEST value,
23696 LONGEST *len)
23697 {
23698 gdb_byte *result;
23699
23700 *len = TYPE_LENGTH (type);
23701 result = (gdb_byte *) obstack_alloc (obstack, *len);
23702 store_unsigned_integer (result, *len, byte_order, value);
23703
23704 return result;
23705 }
23706
23707 /* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
23708 pointer to the constant bytes and set LEN to the length of the
23709 data. If memory is needed, allocate it on OBSTACK. If the DIE
23710 does not have a DW_AT_const_value, return NULL. */
23711
23712 const gdb_byte *
23713 dwarf2_fetch_constant_bytes (sect_offset sect_off,
23714 struct dwarf2_per_cu_data *per_cu,
23715 struct obstack *obstack,
23716 LONGEST *len)
23717 {
23718 struct dwarf2_cu *cu;
23719 struct die_info *die;
23720 struct attribute *attr;
23721 const gdb_byte *result = NULL;
23722 struct type *type;
23723 LONGEST value;
23724 enum bfd_endian byte_order;
23725 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
23726
23727 if (per_cu->cu == NULL)
23728 load_cu (per_cu, false);
23729 cu = per_cu->cu;
23730 if (cu == NULL)
23731 {
23732 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23733 Instead just throw an error, not much else we can do. */
23734 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23735 sect_offset_str (sect_off), objfile_name (objfile));
23736 }
23737
23738 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23739 if (!die)
23740 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23741 sect_offset_str (sect_off), objfile_name (objfile));
23742
23743 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23744 if (attr == NULL)
23745 return NULL;
23746
23747 byte_order = (bfd_big_endian (objfile->obfd)
23748 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23749
23750 switch (attr->form)
23751 {
23752 case DW_FORM_addr:
23753 case DW_FORM_addrx:
23754 case DW_FORM_GNU_addr_index:
23755 {
23756 gdb_byte *tem;
23757
23758 *len = cu->header.addr_size;
23759 tem = (gdb_byte *) obstack_alloc (obstack, *len);
23760 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
23761 result = tem;
23762 }
23763 break;
23764 case DW_FORM_string:
23765 case DW_FORM_strp:
23766 case DW_FORM_strx:
23767 case DW_FORM_GNU_str_index:
23768 case DW_FORM_GNU_strp_alt:
23769 /* DW_STRING is already allocated on the objfile obstack, point
23770 directly to it. */
23771 result = (const gdb_byte *) DW_STRING (attr);
23772 *len = strlen (DW_STRING (attr));
23773 break;
23774 case DW_FORM_block1:
23775 case DW_FORM_block2:
23776 case DW_FORM_block4:
23777 case DW_FORM_block:
23778 case DW_FORM_exprloc:
23779 case DW_FORM_data16:
23780 result = DW_BLOCK (attr)->data;
23781 *len = DW_BLOCK (attr)->size;
23782 break;
23783
23784 /* The DW_AT_const_value attributes are supposed to carry the
23785 symbol's value "represented as it would be on the target
23786 architecture." By the time we get here, it's already been
23787 converted to host endianness, so we just need to sign- or
23788 zero-extend it as appropriate. */
23789 case DW_FORM_data1:
23790 type = die_type (die, cu);
23791 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23792 if (result == NULL)
23793 result = write_constant_as_bytes (obstack, byte_order,
23794 type, value, len);
23795 break;
23796 case DW_FORM_data2:
23797 type = die_type (die, cu);
23798 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23799 if (result == NULL)
23800 result = write_constant_as_bytes (obstack, byte_order,
23801 type, value, len);
23802 break;
23803 case DW_FORM_data4:
23804 type = die_type (die, cu);
23805 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23806 if (result == NULL)
23807 result = write_constant_as_bytes (obstack, byte_order,
23808 type, value, len);
23809 break;
23810 case DW_FORM_data8:
23811 type = die_type (die, cu);
23812 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23813 if (result == NULL)
23814 result = write_constant_as_bytes (obstack, byte_order,
23815 type, value, len);
23816 break;
23817
23818 case DW_FORM_sdata:
23819 case DW_FORM_implicit_const:
23820 type = die_type (die, cu);
23821 result = write_constant_as_bytes (obstack, byte_order,
23822 type, DW_SND (attr), len);
23823 break;
23824
23825 case DW_FORM_udata:
23826 type = die_type (die, cu);
23827 result = write_constant_as_bytes (obstack, byte_order,
23828 type, DW_UNSND (attr), len);
23829 break;
23830
23831 default:
23832 complaint (_("unsupported const value attribute form: '%s'"),
23833 dwarf_form_name (attr->form));
23834 break;
23835 }
23836
23837 return result;
23838 }
23839
23840 /* Return the type of the die at OFFSET in PER_CU. Return NULL if no
23841 valid type for this die is found. */
23842
23843 struct type *
23844 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
23845 struct dwarf2_per_cu_data *per_cu)
23846 {
23847 struct dwarf2_cu *cu;
23848 struct die_info *die;
23849
23850 if (per_cu->cu == NULL)
23851 load_cu (per_cu, false);
23852 cu = per_cu->cu;
23853 if (!cu)
23854 return NULL;
23855
23856 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23857 if (!die)
23858 return NULL;
23859
23860 return die_type (die, cu);
23861 }
23862
23863 /* Return the type of the DIE at DIE_OFFSET in the CU named by
23864 PER_CU. */
23865
23866 struct type *
23867 dwarf2_get_die_type (cu_offset die_offset,
23868 struct dwarf2_per_cu_data *per_cu)
23869 {
23870 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
23871 return get_die_type_at_offset (die_offset_sect, per_cu);
23872 }
23873
23874 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
23875 On entry *REF_CU is the CU of SRC_DIE.
23876 On exit *REF_CU is the CU of the result.
23877 Returns NULL if the referenced DIE isn't found. */
23878
23879 static struct die_info *
23880 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23881 struct dwarf2_cu **ref_cu)
23882 {
23883 struct die_info temp_die;
23884 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
23885 struct die_info *die;
23886
23887 /* While it might be nice to assert sig_type->type == NULL here,
23888 we can get here for DW_AT_imported_declaration where we need
23889 the DIE not the type. */
23890
23891 /* If necessary, add it to the queue and load its DIEs. */
23892
23893 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
23894 read_signatured_type (sig_type);
23895
23896 sig_cu = sig_type->per_cu.cu;
23897 gdb_assert (sig_cu != NULL);
23898 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23899 temp_die.sect_off = sig_type->type_offset_in_section;
23900 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
23901 to_underlying (temp_die.sect_off));
23902 if (die)
23903 {
23904 struct dwarf2_per_objfile *dwarf2_per_objfile
23905 = (*ref_cu)->per_cu->dwarf2_per_objfile;
23906
23907 /* For .gdb_index version 7 keep track of included TUs.
23908 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23909 if (dwarf2_per_objfile->index_table != NULL
23910 && dwarf2_per_objfile->index_table->version <= 7)
23911 {
23912 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
23913 }
23914
23915 *ref_cu = sig_cu;
23916 if (sig_cu != cu)
23917 sig_cu->ancestor = cu;
23918
23919 return die;
23920 }
23921
23922 return NULL;
23923 }
23924
23925 /* Follow signatured type referenced by ATTR in SRC_DIE.
23926 On entry *REF_CU is the CU of SRC_DIE.
23927 On exit *REF_CU is the CU of the result.
23928 The result is the DIE of the type.
23929 If the referenced type cannot be found an error is thrown. */
23930
23931 static struct die_info *
23932 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
23933 struct dwarf2_cu **ref_cu)
23934 {
23935 ULONGEST signature = DW_SIGNATURE (attr);
23936 struct signatured_type *sig_type;
23937 struct die_info *die;
23938
23939 gdb_assert (attr->form == DW_FORM_ref_sig8);
23940
23941 sig_type = lookup_signatured_type (*ref_cu, signature);
23942 /* sig_type will be NULL if the signatured type is missing from
23943 the debug info. */
23944 if (sig_type == NULL)
23945 {
23946 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23947 " from DIE at %s [in module %s]"),
23948 hex_string (signature), sect_offset_str (src_die->sect_off),
23949 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23950 }
23951
23952 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23953 if (die == NULL)
23954 {
23955 dump_die_for_error (src_die);
23956 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23957 " from DIE at %s [in module %s]"),
23958 hex_string (signature), sect_offset_str (src_die->sect_off),
23959 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23960 }
23961
23962 return die;
23963 }
23964
23965 /* Get the type specified by SIGNATURE referenced in DIE/CU,
23966 reading in and processing the type unit if necessary. */
23967
23968 static struct type *
23969 get_signatured_type (struct die_info *die, ULONGEST signature,
23970 struct dwarf2_cu *cu)
23971 {
23972 struct dwarf2_per_objfile *dwarf2_per_objfile
23973 = cu->per_cu->dwarf2_per_objfile;
23974 struct signatured_type *sig_type;
23975 struct dwarf2_cu *type_cu;
23976 struct die_info *type_die;
23977 struct type *type;
23978
23979 sig_type = lookup_signatured_type (cu, signature);
23980 /* sig_type will be NULL if the signatured type is missing from
23981 the debug info. */
23982 if (sig_type == NULL)
23983 {
23984 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23985 " from DIE at %s [in module %s]"),
23986 hex_string (signature), sect_offset_str (die->sect_off),
23987 objfile_name (dwarf2_per_objfile->objfile));
23988 return build_error_marker_type (cu, die);
23989 }
23990
23991 /* If we already know the type we're done. */
23992 if (sig_type->type != NULL)
23993 return sig_type->type;
23994
23995 type_cu = cu;
23996 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23997 if (type_die != NULL)
23998 {
23999 /* N.B. We need to call get_die_type to ensure only one type for this DIE
24000 is created. This is important, for example, because for c++ classes
24001 we need TYPE_NAME set which is only done by new_symbol. Blech. */
24002 type = read_type_die (type_die, type_cu);
24003 if (type == NULL)
24004 {
24005 complaint (_("Dwarf Error: Cannot build signatured type %s"
24006 " referenced 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 type = build_error_marker_type (cu, die);
24010 }
24011 }
24012 else
24013 {
24014 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
24015 " from DIE at %s [in module %s]"),
24016 hex_string (signature), sect_offset_str (die->sect_off),
24017 objfile_name (dwarf2_per_objfile->objfile));
24018 type = build_error_marker_type (cu, die);
24019 }
24020 sig_type->type = type;
24021
24022 return type;
24023 }
24024
24025 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
24026 reading in and processing the type unit if necessary. */
24027
24028 static struct type *
24029 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
24030 struct dwarf2_cu *cu) /* ARI: editCase function */
24031 {
24032 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
24033 if (attr_form_is_ref (attr))
24034 {
24035 struct dwarf2_cu *type_cu = cu;
24036 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
24037
24038 return read_type_die (type_die, type_cu);
24039 }
24040 else if (attr->form == DW_FORM_ref_sig8)
24041 {
24042 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
24043 }
24044 else
24045 {
24046 struct dwarf2_per_objfile *dwarf2_per_objfile
24047 = cu->per_cu->dwarf2_per_objfile;
24048
24049 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
24050 " at %s [in module %s]"),
24051 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
24052 objfile_name (dwarf2_per_objfile->objfile));
24053 return build_error_marker_type (cu, die);
24054 }
24055 }
24056
24057 /* Load the DIEs associated with type unit PER_CU into memory. */
24058
24059 static void
24060 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
24061 {
24062 struct signatured_type *sig_type;
24063
24064 /* Caller is responsible for ensuring type_unit_groups don't get here. */
24065 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
24066
24067 /* We have the per_cu, but we need the signatured_type.
24068 Fortunately this is an easy translation. */
24069 gdb_assert (per_cu->is_debug_types);
24070 sig_type = (struct signatured_type *) per_cu;
24071
24072 gdb_assert (per_cu->cu == NULL);
24073
24074 read_signatured_type (sig_type);
24075
24076 gdb_assert (per_cu->cu != NULL);
24077 }
24078
24079 /* die_reader_func for read_signatured_type.
24080 This is identical to load_full_comp_unit_reader,
24081 but is kept separate for now. */
24082
24083 static void
24084 read_signatured_type_reader (const struct die_reader_specs *reader,
24085 const gdb_byte *info_ptr,
24086 struct die_info *comp_unit_die,
24087 int has_children,
24088 void *data)
24089 {
24090 struct dwarf2_cu *cu = reader->cu;
24091
24092 gdb_assert (cu->die_hash == NULL);
24093 cu->die_hash =
24094 htab_create_alloc_ex (cu->header.length / 12,
24095 die_hash,
24096 die_eq,
24097 NULL,
24098 &cu->comp_unit_obstack,
24099 hashtab_obstack_allocate,
24100 dummy_obstack_deallocate);
24101
24102 if (has_children)
24103 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
24104 &info_ptr, comp_unit_die);
24105 cu->dies = comp_unit_die;
24106 /* comp_unit_die is not stored in die_hash, no need. */
24107
24108 /* We try not to read any attributes in this function, because not
24109 all CUs needed for references have been loaded yet, and symbol
24110 table processing isn't initialized. But we have to set the CU language,
24111 or we won't be able to build types correctly.
24112 Similarly, if we do not read the producer, we can not apply
24113 producer-specific interpretation. */
24114 prepare_one_comp_unit (cu, cu->dies, language_minimal);
24115 }
24116
24117 /* Read in a signatured type and build its CU and DIEs.
24118 If the type is a stub for the real type in a DWO file,
24119 read in the real type from the DWO file as well. */
24120
24121 static void
24122 read_signatured_type (struct signatured_type *sig_type)
24123 {
24124 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
24125
24126 gdb_assert (per_cu->is_debug_types);
24127 gdb_assert (per_cu->cu == NULL);
24128
24129 init_cutu_and_read_dies (per_cu, NULL, 0, 1, false,
24130 read_signatured_type_reader, NULL);
24131 sig_type->per_cu.tu_read = 1;
24132 }
24133
24134 /* Decode simple location descriptions.
24135 Given a pointer to a dwarf block that defines a location, compute
24136 the location and return the value.
24137
24138 NOTE drow/2003-11-18: This function is called in two situations
24139 now: for the address of static or global variables (partial symbols
24140 only) and for offsets into structures which are expected to be
24141 (more or less) constant. The partial symbol case should go away,
24142 and only the constant case should remain. That will let this
24143 function complain more accurately. A few special modes are allowed
24144 without complaint for global variables (for instance, global
24145 register values and thread-local values).
24146
24147 A location description containing no operations indicates that the
24148 object is optimized out. The return value is 0 for that case.
24149 FIXME drow/2003-11-16: No callers check for this case any more; soon all
24150 callers will only want a very basic result and this can become a
24151 complaint.
24152
24153 Note that stack[0] is unused except as a default error return. */
24154
24155 static CORE_ADDR
24156 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
24157 {
24158 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
24159 size_t i;
24160 size_t size = blk->size;
24161 const gdb_byte *data = blk->data;
24162 CORE_ADDR stack[64];
24163 int stacki;
24164 unsigned int bytes_read, unsnd;
24165 gdb_byte op;
24166
24167 i = 0;
24168 stacki = 0;
24169 stack[stacki] = 0;
24170 stack[++stacki] = 0;
24171
24172 while (i < size)
24173 {
24174 op = data[i++];
24175 switch (op)
24176 {
24177 case DW_OP_lit0:
24178 case DW_OP_lit1:
24179 case DW_OP_lit2:
24180 case DW_OP_lit3:
24181 case DW_OP_lit4:
24182 case DW_OP_lit5:
24183 case DW_OP_lit6:
24184 case DW_OP_lit7:
24185 case DW_OP_lit8:
24186 case DW_OP_lit9:
24187 case DW_OP_lit10:
24188 case DW_OP_lit11:
24189 case DW_OP_lit12:
24190 case DW_OP_lit13:
24191 case DW_OP_lit14:
24192 case DW_OP_lit15:
24193 case DW_OP_lit16:
24194 case DW_OP_lit17:
24195 case DW_OP_lit18:
24196 case DW_OP_lit19:
24197 case DW_OP_lit20:
24198 case DW_OP_lit21:
24199 case DW_OP_lit22:
24200 case DW_OP_lit23:
24201 case DW_OP_lit24:
24202 case DW_OP_lit25:
24203 case DW_OP_lit26:
24204 case DW_OP_lit27:
24205 case DW_OP_lit28:
24206 case DW_OP_lit29:
24207 case DW_OP_lit30:
24208 case DW_OP_lit31:
24209 stack[++stacki] = op - DW_OP_lit0;
24210 break;
24211
24212 case DW_OP_reg0:
24213 case DW_OP_reg1:
24214 case DW_OP_reg2:
24215 case DW_OP_reg3:
24216 case DW_OP_reg4:
24217 case DW_OP_reg5:
24218 case DW_OP_reg6:
24219 case DW_OP_reg7:
24220 case DW_OP_reg8:
24221 case DW_OP_reg9:
24222 case DW_OP_reg10:
24223 case DW_OP_reg11:
24224 case DW_OP_reg12:
24225 case DW_OP_reg13:
24226 case DW_OP_reg14:
24227 case DW_OP_reg15:
24228 case DW_OP_reg16:
24229 case DW_OP_reg17:
24230 case DW_OP_reg18:
24231 case DW_OP_reg19:
24232 case DW_OP_reg20:
24233 case DW_OP_reg21:
24234 case DW_OP_reg22:
24235 case DW_OP_reg23:
24236 case DW_OP_reg24:
24237 case DW_OP_reg25:
24238 case DW_OP_reg26:
24239 case DW_OP_reg27:
24240 case DW_OP_reg28:
24241 case DW_OP_reg29:
24242 case DW_OP_reg30:
24243 case DW_OP_reg31:
24244 stack[++stacki] = op - DW_OP_reg0;
24245 if (i < size)
24246 dwarf2_complex_location_expr_complaint ();
24247 break;
24248
24249 case DW_OP_regx:
24250 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
24251 i += bytes_read;
24252 stack[++stacki] = unsnd;
24253 if (i < size)
24254 dwarf2_complex_location_expr_complaint ();
24255 break;
24256
24257 case DW_OP_addr:
24258 stack[++stacki] = read_address (objfile->obfd, &data[i],
24259 cu, &bytes_read);
24260 i += bytes_read;
24261 break;
24262
24263 case DW_OP_const1u:
24264 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
24265 i += 1;
24266 break;
24267
24268 case DW_OP_const1s:
24269 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
24270 i += 1;
24271 break;
24272
24273 case DW_OP_const2u:
24274 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
24275 i += 2;
24276 break;
24277
24278 case DW_OP_const2s:
24279 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
24280 i += 2;
24281 break;
24282
24283 case DW_OP_const4u:
24284 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
24285 i += 4;
24286 break;
24287
24288 case DW_OP_const4s:
24289 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
24290 i += 4;
24291 break;
24292
24293 case DW_OP_const8u:
24294 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
24295 i += 8;
24296 break;
24297
24298 case DW_OP_constu:
24299 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
24300 &bytes_read);
24301 i += bytes_read;
24302 break;
24303
24304 case DW_OP_consts:
24305 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
24306 i += bytes_read;
24307 break;
24308
24309 case DW_OP_dup:
24310 stack[stacki + 1] = stack[stacki];
24311 stacki++;
24312 break;
24313
24314 case DW_OP_plus:
24315 stack[stacki - 1] += stack[stacki];
24316 stacki--;
24317 break;
24318
24319 case DW_OP_plus_uconst:
24320 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
24321 &bytes_read);
24322 i += bytes_read;
24323 break;
24324
24325 case DW_OP_minus:
24326 stack[stacki - 1] -= stack[stacki];
24327 stacki--;
24328 break;
24329
24330 case DW_OP_deref:
24331 /* If we're not the last op, then we definitely can't encode
24332 this using GDB's address_class enum. This is valid for partial
24333 global symbols, although the variable's address will be bogus
24334 in the psymtab. */
24335 if (i < size)
24336 dwarf2_complex_location_expr_complaint ();
24337 break;
24338
24339 case DW_OP_GNU_push_tls_address:
24340 case DW_OP_form_tls_address:
24341 /* The top of the stack has the offset from the beginning
24342 of the thread control block at which the variable is located. */
24343 /* Nothing should follow this operator, so the top of stack would
24344 be returned. */
24345 /* This is valid for partial global symbols, but the variable's
24346 address will be bogus in the psymtab. Make it always at least
24347 non-zero to not look as a variable garbage collected by linker
24348 which have DW_OP_addr 0. */
24349 if (i < size)
24350 dwarf2_complex_location_expr_complaint ();
24351 stack[stacki]++;
24352 break;
24353
24354 case DW_OP_GNU_uninit:
24355 break;
24356
24357 case DW_OP_addrx:
24358 case DW_OP_GNU_addr_index:
24359 case DW_OP_GNU_const_index:
24360 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
24361 &bytes_read);
24362 i += bytes_read;
24363 break;
24364
24365 default:
24366 {
24367 const char *name = get_DW_OP_name (op);
24368
24369 if (name)
24370 complaint (_("unsupported stack op: '%s'"),
24371 name);
24372 else
24373 complaint (_("unsupported stack op: '%02x'"),
24374 op);
24375 }
24376
24377 return (stack[stacki]);
24378 }
24379
24380 /* Enforce maximum stack depth of SIZE-1 to avoid writing
24381 outside of the allocated space. Also enforce minimum>0. */
24382 if (stacki >= ARRAY_SIZE (stack) - 1)
24383 {
24384 complaint (_("location description stack overflow"));
24385 return 0;
24386 }
24387
24388 if (stacki <= 0)
24389 {
24390 complaint (_("location description stack underflow"));
24391 return 0;
24392 }
24393 }
24394 return (stack[stacki]);
24395 }
24396
24397 /* memory allocation interface */
24398
24399 static struct dwarf_block *
24400 dwarf_alloc_block (struct dwarf2_cu *cu)
24401 {
24402 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
24403 }
24404
24405 static struct die_info *
24406 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
24407 {
24408 struct die_info *die;
24409 size_t size = sizeof (struct die_info);
24410
24411 if (num_attrs > 1)
24412 size += (num_attrs - 1) * sizeof (struct attribute);
24413
24414 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
24415 memset (die, 0, sizeof (struct die_info));
24416 return (die);
24417 }
24418
24419 \f
24420 /* Macro support. */
24421
24422 /* Return file name relative to the compilation directory of file number I in
24423 *LH's file name table. The result is allocated using xmalloc; the caller is
24424 responsible for freeing it. */
24425
24426 static char *
24427 file_file_name (int file, struct line_header *lh)
24428 {
24429 /* Is the file number a valid index into the line header's file name
24430 table? Remember that file numbers start with one, not zero. */
24431 if (lh->is_valid_file_index (file))
24432 {
24433 const file_entry *fe = lh->file_name_at (file);
24434
24435 if (!IS_ABSOLUTE_PATH (fe->name))
24436 {
24437 const char *dir = fe->include_dir (lh);
24438 if (dir != NULL)
24439 return concat (dir, SLASH_STRING, fe->name, (char *) NULL);
24440 }
24441 return xstrdup (fe->name);
24442 }
24443 else
24444 {
24445 /* The compiler produced a bogus file number. We can at least
24446 record the macro definitions made in the file, even if we
24447 won't be able to find the file by name. */
24448 char fake_name[80];
24449
24450 xsnprintf (fake_name, sizeof (fake_name),
24451 "<bad macro file number %d>", file);
24452
24453 complaint (_("bad file number in macro information (%d)"),
24454 file);
24455
24456 return xstrdup (fake_name);
24457 }
24458 }
24459
24460 /* Return the full name of file number I in *LH's file name table.
24461 Use COMP_DIR as the name of the current directory of the
24462 compilation. The result is allocated using xmalloc; the caller is
24463 responsible for freeing it. */
24464 static char *
24465 file_full_name (int file, struct line_header *lh, const char *comp_dir)
24466 {
24467 /* Is the file number a valid index into the line header's file name
24468 table? Remember that file numbers start with one, not zero. */
24469 if (lh->is_valid_file_index (file))
24470 {
24471 char *relative = file_file_name (file, lh);
24472
24473 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
24474 return relative;
24475 return reconcat (relative, comp_dir, SLASH_STRING,
24476 relative, (char *) NULL);
24477 }
24478 else
24479 return file_file_name (file, lh);
24480 }
24481
24482
24483 static struct macro_source_file *
24484 macro_start_file (struct dwarf2_cu *cu,
24485 int file, int line,
24486 struct macro_source_file *current_file,
24487 struct line_header *lh)
24488 {
24489 /* File name relative to the compilation directory of this source file. */
24490 char *file_name = file_file_name (file, lh);
24491
24492 if (! current_file)
24493 {
24494 /* Note: We don't create a macro table for this compilation unit
24495 at all until we actually get a filename. */
24496 struct macro_table *macro_table = cu->get_builder ()->get_macro_table ();
24497
24498 /* If we have no current file, then this must be the start_file
24499 directive for the compilation unit's main source file. */
24500 current_file = macro_set_main (macro_table, file_name);
24501 macro_define_special (macro_table);
24502 }
24503 else
24504 current_file = macro_include (current_file, line, file_name);
24505
24506 xfree (file_name);
24507
24508 return current_file;
24509 }
24510
24511 static const char *
24512 consume_improper_spaces (const char *p, const char *body)
24513 {
24514 if (*p == ' ')
24515 {
24516 complaint (_("macro definition contains spaces "
24517 "in formal argument list:\n`%s'"),
24518 body);
24519
24520 while (*p == ' ')
24521 p++;
24522 }
24523
24524 return p;
24525 }
24526
24527
24528 static void
24529 parse_macro_definition (struct macro_source_file *file, int line,
24530 const char *body)
24531 {
24532 const char *p;
24533
24534 /* The body string takes one of two forms. For object-like macro
24535 definitions, it should be:
24536
24537 <macro name> " " <definition>
24538
24539 For function-like macro definitions, it should be:
24540
24541 <macro name> "() " <definition>
24542 or
24543 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
24544
24545 Spaces may appear only where explicitly indicated, and in the
24546 <definition>.
24547
24548 The Dwarf 2 spec says that an object-like macro's name is always
24549 followed by a space, but versions of GCC around March 2002 omit
24550 the space when the macro's definition is the empty string.
24551
24552 The Dwarf 2 spec says that there should be no spaces between the
24553 formal arguments in a function-like macro's formal argument list,
24554 but versions of GCC around March 2002 include spaces after the
24555 commas. */
24556
24557
24558 /* Find the extent of the macro name. The macro name is terminated
24559 by either a space or null character (for an object-like macro) or
24560 an opening paren (for a function-like macro). */
24561 for (p = body; *p; p++)
24562 if (*p == ' ' || *p == '(')
24563 break;
24564
24565 if (*p == ' ' || *p == '\0')
24566 {
24567 /* It's an object-like macro. */
24568 int name_len = p - body;
24569 std::string name (body, name_len);
24570 const char *replacement;
24571
24572 if (*p == ' ')
24573 replacement = body + name_len + 1;
24574 else
24575 {
24576 dwarf2_macro_malformed_definition_complaint (body);
24577 replacement = body + name_len;
24578 }
24579
24580 macro_define_object (file, line, name.c_str (), replacement);
24581 }
24582 else if (*p == '(')
24583 {
24584 /* It's a function-like macro. */
24585 std::string name (body, p - body);
24586 int argc = 0;
24587 int argv_size = 1;
24588 char **argv = XNEWVEC (char *, argv_size);
24589
24590 p++;
24591
24592 p = consume_improper_spaces (p, body);
24593
24594 /* Parse the formal argument list. */
24595 while (*p && *p != ')')
24596 {
24597 /* Find the extent of the current argument name. */
24598 const char *arg_start = p;
24599
24600 while (*p && *p != ',' && *p != ')' && *p != ' ')
24601 p++;
24602
24603 if (! *p || p == arg_start)
24604 dwarf2_macro_malformed_definition_complaint (body);
24605 else
24606 {
24607 /* Make sure argv has room for the new argument. */
24608 if (argc >= argv_size)
24609 {
24610 argv_size *= 2;
24611 argv = XRESIZEVEC (char *, argv, argv_size);
24612 }
24613
24614 argv[argc++] = savestring (arg_start, p - arg_start);
24615 }
24616
24617 p = consume_improper_spaces (p, body);
24618
24619 /* Consume the comma, if present. */
24620 if (*p == ',')
24621 {
24622 p++;
24623
24624 p = consume_improper_spaces (p, body);
24625 }
24626 }
24627
24628 if (*p == ')')
24629 {
24630 p++;
24631
24632 if (*p == ' ')
24633 /* Perfectly formed definition, no complaints. */
24634 macro_define_function (file, line, name.c_str (),
24635 argc, (const char **) argv,
24636 p + 1);
24637 else if (*p == '\0')
24638 {
24639 /* Complain, but do define it. */
24640 dwarf2_macro_malformed_definition_complaint (body);
24641 macro_define_function (file, line, name.c_str (),
24642 argc, (const char **) argv,
24643 p);
24644 }
24645 else
24646 /* Just complain. */
24647 dwarf2_macro_malformed_definition_complaint (body);
24648 }
24649 else
24650 /* Just complain. */
24651 dwarf2_macro_malformed_definition_complaint (body);
24652
24653 {
24654 int i;
24655
24656 for (i = 0; i < argc; i++)
24657 xfree (argv[i]);
24658 }
24659 xfree (argv);
24660 }
24661 else
24662 dwarf2_macro_malformed_definition_complaint (body);
24663 }
24664
24665 /* Skip some bytes from BYTES according to the form given in FORM.
24666 Returns the new pointer. */
24667
24668 static const gdb_byte *
24669 skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
24670 enum dwarf_form form,
24671 unsigned int offset_size,
24672 struct dwarf2_section_info *section)
24673 {
24674 unsigned int bytes_read;
24675
24676 switch (form)
24677 {
24678 case DW_FORM_data1:
24679 case DW_FORM_flag:
24680 ++bytes;
24681 break;
24682
24683 case DW_FORM_data2:
24684 bytes += 2;
24685 break;
24686
24687 case DW_FORM_data4:
24688 bytes += 4;
24689 break;
24690
24691 case DW_FORM_data8:
24692 bytes += 8;
24693 break;
24694
24695 case DW_FORM_data16:
24696 bytes += 16;
24697 break;
24698
24699 case DW_FORM_string:
24700 read_direct_string (abfd, bytes, &bytes_read);
24701 bytes += bytes_read;
24702 break;
24703
24704 case DW_FORM_sec_offset:
24705 case DW_FORM_strp:
24706 case DW_FORM_GNU_strp_alt:
24707 bytes += offset_size;
24708 break;
24709
24710 case DW_FORM_block:
24711 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
24712 bytes += bytes_read;
24713 break;
24714
24715 case DW_FORM_block1:
24716 bytes += 1 + read_1_byte (abfd, bytes);
24717 break;
24718 case DW_FORM_block2:
24719 bytes += 2 + read_2_bytes (abfd, bytes);
24720 break;
24721 case DW_FORM_block4:
24722 bytes += 4 + read_4_bytes (abfd, bytes);
24723 break;
24724
24725 case DW_FORM_addrx:
24726 case DW_FORM_sdata:
24727 case DW_FORM_strx:
24728 case DW_FORM_udata:
24729 case DW_FORM_GNU_addr_index:
24730 case DW_FORM_GNU_str_index:
24731 bytes = gdb_skip_leb128 (bytes, buffer_end);
24732 if (bytes == NULL)
24733 {
24734 dwarf2_section_buffer_overflow_complaint (section);
24735 return NULL;
24736 }
24737 break;
24738
24739 case DW_FORM_implicit_const:
24740 break;
24741
24742 default:
24743 {
24744 complaint (_("invalid form 0x%x in `%s'"),
24745 form, get_section_name (section));
24746 return NULL;
24747 }
24748 }
24749
24750 return bytes;
24751 }
24752
24753 /* A helper for dwarf_decode_macros that handles skipping an unknown
24754 opcode. Returns an updated pointer to the macro data buffer; or,
24755 on error, issues a complaint and returns NULL. */
24756
24757 static const gdb_byte *
24758 skip_unknown_opcode (unsigned int opcode,
24759 const gdb_byte **opcode_definitions,
24760 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
24761 bfd *abfd,
24762 unsigned int offset_size,
24763 struct dwarf2_section_info *section)
24764 {
24765 unsigned int bytes_read, i;
24766 unsigned long arg;
24767 const gdb_byte *defn;
24768
24769 if (opcode_definitions[opcode] == NULL)
24770 {
24771 complaint (_("unrecognized DW_MACFINO opcode 0x%x"),
24772 opcode);
24773 return NULL;
24774 }
24775
24776 defn = opcode_definitions[opcode];
24777 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
24778 defn += bytes_read;
24779
24780 for (i = 0; i < arg; ++i)
24781 {
24782 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
24783 (enum dwarf_form) defn[i], offset_size,
24784 section);
24785 if (mac_ptr == NULL)
24786 {
24787 /* skip_form_bytes already issued the complaint. */
24788 return NULL;
24789 }
24790 }
24791
24792 return mac_ptr;
24793 }
24794
24795 /* A helper function which parses the header of a macro section.
24796 If the macro section is the extended (for now called "GNU") type,
24797 then this updates *OFFSET_SIZE. Returns a pointer to just after
24798 the header, or issues a complaint and returns NULL on error. */
24799
24800 static const gdb_byte *
24801 dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
24802 bfd *abfd,
24803 const gdb_byte *mac_ptr,
24804 unsigned int *offset_size,
24805 int section_is_gnu)
24806 {
24807 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
24808
24809 if (section_is_gnu)
24810 {
24811 unsigned int version, flags;
24812
24813 version = read_2_bytes (abfd, mac_ptr);
24814 if (version != 4 && version != 5)
24815 {
24816 complaint (_("unrecognized version `%d' in .debug_macro section"),
24817 version);
24818 return NULL;
24819 }
24820 mac_ptr += 2;
24821
24822 flags = read_1_byte (abfd, mac_ptr);
24823 ++mac_ptr;
24824 *offset_size = (flags & 1) ? 8 : 4;
24825
24826 if ((flags & 2) != 0)
24827 /* We don't need the line table offset. */
24828 mac_ptr += *offset_size;
24829
24830 /* Vendor opcode descriptions. */
24831 if ((flags & 4) != 0)
24832 {
24833 unsigned int i, count;
24834
24835 count = read_1_byte (abfd, mac_ptr);
24836 ++mac_ptr;
24837 for (i = 0; i < count; ++i)
24838 {
24839 unsigned int opcode, bytes_read;
24840 unsigned long arg;
24841
24842 opcode = read_1_byte (abfd, mac_ptr);
24843 ++mac_ptr;
24844 opcode_definitions[opcode] = mac_ptr;
24845 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24846 mac_ptr += bytes_read;
24847 mac_ptr += arg;
24848 }
24849 }
24850 }
24851
24852 return mac_ptr;
24853 }
24854
24855 /* A helper for dwarf_decode_macros that handles the GNU extensions,
24856 including DW_MACRO_import. */
24857
24858 static void
24859 dwarf_decode_macro_bytes (struct dwarf2_cu *cu,
24860 bfd *abfd,
24861 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
24862 struct macro_source_file *current_file,
24863 struct line_header *lh,
24864 struct dwarf2_section_info *section,
24865 int section_is_gnu, int section_is_dwz,
24866 unsigned int offset_size,
24867 htab_t include_hash)
24868 {
24869 struct dwarf2_per_objfile *dwarf2_per_objfile
24870 = cu->per_cu->dwarf2_per_objfile;
24871 struct objfile *objfile = dwarf2_per_objfile->objfile;
24872 enum dwarf_macro_record_type macinfo_type;
24873 int at_commandline;
24874 const gdb_byte *opcode_definitions[256];
24875
24876 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24877 &offset_size, section_is_gnu);
24878 if (mac_ptr == NULL)
24879 {
24880 /* We already issued a complaint. */
24881 return;
24882 }
24883
24884 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
24885 GDB is still reading the definitions from command line. First
24886 DW_MACINFO_start_file will need to be ignored as it was already executed
24887 to create CURRENT_FILE for the main source holding also the command line
24888 definitions. On first met DW_MACINFO_start_file this flag is reset to
24889 normally execute all the remaining DW_MACINFO_start_file macinfos. */
24890
24891 at_commandline = 1;
24892
24893 do
24894 {
24895 /* Do we at least have room for a macinfo type byte? */
24896 if (mac_ptr >= mac_end)
24897 {
24898 dwarf2_section_buffer_overflow_complaint (section);
24899 break;
24900 }
24901
24902 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
24903 mac_ptr++;
24904
24905 /* Note that we rely on the fact that the corresponding GNU and
24906 DWARF constants are the same. */
24907 DIAGNOSTIC_PUSH
24908 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
24909 switch (macinfo_type)
24910 {
24911 /* A zero macinfo type indicates the end of the macro
24912 information. */
24913 case 0:
24914 break;
24915
24916 case DW_MACRO_define:
24917 case DW_MACRO_undef:
24918 case DW_MACRO_define_strp:
24919 case DW_MACRO_undef_strp:
24920 case DW_MACRO_define_sup:
24921 case DW_MACRO_undef_sup:
24922 {
24923 unsigned int bytes_read;
24924 int line;
24925 const char *body;
24926 int is_define;
24927
24928 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24929 mac_ptr += bytes_read;
24930
24931 if (macinfo_type == DW_MACRO_define
24932 || macinfo_type == DW_MACRO_undef)
24933 {
24934 body = read_direct_string (abfd, mac_ptr, &bytes_read);
24935 mac_ptr += bytes_read;
24936 }
24937 else
24938 {
24939 LONGEST str_offset;
24940
24941 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
24942 mac_ptr += offset_size;
24943
24944 if (macinfo_type == DW_MACRO_define_sup
24945 || macinfo_type == DW_MACRO_undef_sup
24946 || section_is_dwz)
24947 {
24948 struct dwz_file *dwz
24949 = dwarf2_get_dwz_file (dwarf2_per_objfile);
24950
24951 body = read_indirect_string_from_dwz (objfile,
24952 dwz, str_offset);
24953 }
24954 else
24955 body = read_indirect_string_at_offset (dwarf2_per_objfile,
24956 abfd, str_offset);
24957 }
24958
24959 is_define = (macinfo_type == DW_MACRO_define
24960 || macinfo_type == DW_MACRO_define_strp
24961 || macinfo_type == DW_MACRO_define_sup);
24962 if (! current_file)
24963 {
24964 /* DWARF violation as no main source is present. */
24965 complaint (_("debug info with no main source gives macro %s "
24966 "on line %d: %s"),
24967 is_define ? _("definition") : _("undefinition"),
24968 line, body);
24969 break;
24970 }
24971 if ((line == 0 && !at_commandline)
24972 || (line != 0 && at_commandline))
24973 complaint (_("debug info gives %s macro %s with %s line %d: %s"),
24974 at_commandline ? _("command-line") : _("in-file"),
24975 is_define ? _("definition") : _("undefinition"),
24976 line == 0 ? _("zero") : _("non-zero"), line, body);
24977
24978 if (body == NULL)
24979 {
24980 /* Fedora's rpm-build's "debugedit" binary
24981 corrupted .debug_macro sections.
24982
24983 For more info, see
24984 https://bugzilla.redhat.com/show_bug.cgi?id=1708786 */
24985 complaint (_("debug info gives %s invalid macro %s "
24986 "without body (corrupted?) at line %d "
24987 "on file %s"),
24988 at_commandline ? _("command-line") : _("in-file"),
24989 is_define ? _("definition") : _("undefinition"),
24990 line, current_file->filename);
24991 }
24992 else if (is_define)
24993 parse_macro_definition (current_file, line, body);
24994 else
24995 {
24996 gdb_assert (macinfo_type == DW_MACRO_undef
24997 || macinfo_type == DW_MACRO_undef_strp
24998 || macinfo_type == DW_MACRO_undef_sup);
24999 macro_undef (current_file, line, body);
25000 }
25001 }
25002 break;
25003
25004 case DW_MACRO_start_file:
25005 {
25006 unsigned int bytes_read;
25007 int line, file;
25008
25009 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25010 mac_ptr += bytes_read;
25011 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25012 mac_ptr += bytes_read;
25013
25014 if ((line == 0 && !at_commandline)
25015 || (line != 0 && at_commandline))
25016 complaint (_("debug info gives source %d included "
25017 "from %s at %s line %d"),
25018 file, at_commandline ? _("command-line") : _("file"),
25019 line == 0 ? _("zero") : _("non-zero"), line);
25020
25021 if (at_commandline)
25022 {
25023 /* This DW_MACRO_start_file was executed in the
25024 pass one. */
25025 at_commandline = 0;
25026 }
25027 else
25028 current_file = macro_start_file (cu, file, line, current_file,
25029 lh);
25030 }
25031 break;
25032
25033 case DW_MACRO_end_file:
25034 if (! current_file)
25035 complaint (_("macro debug info has an unmatched "
25036 "`close_file' directive"));
25037 else
25038 {
25039 current_file = current_file->included_by;
25040 if (! current_file)
25041 {
25042 enum dwarf_macro_record_type next_type;
25043
25044 /* GCC circa March 2002 doesn't produce the zero
25045 type byte marking the end of the compilation
25046 unit. Complain if it's not there, but exit no
25047 matter what. */
25048
25049 /* Do we at least have room for a macinfo type byte? */
25050 if (mac_ptr >= mac_end)
25051 {
25052 dwarf2_section_buffer_overflow_complaint (section);
25053 return;
25054 }
25055
25056 /* We don't increment mac_ptr here, so this is just
25057 a look-ahead. */
25058 next_type
25059 = (enum dwarf_macro_record_type) read_1_byte (abfd,
25060 mac_ptr);
25061 if (next_type != 0)
25062 complaint (_("no terminating 0-type entry for "
25063 "macros in `.debug_macinfo' section"));
25064
25065 return;
25066 }
25067 }
25068 break;
25069
25070 case DW_MACRO_import:
25071 case DW_MACRO_import_sup:
25072 {
25073 LONGEST offset;
25074 void **slot;
25075 bfd *include_bfd = abfd;
25076 struct dwarf2_section_info *include_section = section;
25077 const gdb_byte *include_mac_end = mac_end;
25078 int is_dwz = section_is_dwz;
25079 const gdb_byte *new_mac_ptr;
25080
25081 offset = read_offset_1 (abfd, mac_ptr, offset_size);
25082 mac_ptr += offset_size;
25083
25084 if (macinfo_type == DW_MACRO_import_sup)
25085 {
25086 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
25087
25088 dwarf2_read_section (objfile, &dwz->macro);
25089
25090 include_section = &dwz->macro;
25091 include_bfd = get_section_bfd_owner (include_section);
25092 include_mac_end = dwz->macro.buffer + dwz->macro.size;
25093 is_dwz = 1;
25094 }
25095
25096 new_mac_ptr = include_section->buffer + offset;
25097 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
25098
25099 if (*slot != NULL)
25100 {
25101 /* This has actually happened; see
25102 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
25103 complaint (_("recursive DW_MACRO_import in "
25104 ".debug_macro section"));
25105 }
25106 else
25107 {
25108 *slot = (void *) new_mac_ptr;
25109
25110 dwarf_decode_macro_bytes (cu, include_bfd, new_mac_ptr,
25111 include_mac_end, current_file, lh,
25112 section, section_is_gnu, is_dwz,
25113 offset_size, include_hash);
25114
25115 htab_remove_elt (include_hash, (void *) new_mac_ptr);
25116 }
25117 }
25118 break;
25119
25120 case DW_MACINFO_vendor_ext:
25121 if (!section_is_gnu)
25122 {
25123 unsigned int bytes_read;
25124
25125 /* This reads the constant, but since we don't recognize
25126 any vendor extensions, we ignore it. */
25127 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25128 mac_ptr += bytes_read;
25129 read_direct_string (abfd, mac_ptr, &bytes_read);
25130 mac_ptr += bytes_read;
25131
25132 /* We don't recognize any vendor extensions. */
25133 break;
25134 }
25135 /* FALLTHROUGH */
25136
25137 default:
25138 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
25139 mac_ptr, mac_end, abfd, offset_size,
25140 section);
25141 if (mac_ptr == NULL)
25142 return;
25143 break;
25144 }
25145 DIAGNOSTIC_POP
25146 } while (macinfo_type != 0);
25147 }
25148
25149 static void
25150 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
25151 int section_is_gnu)
25152 {
25153 struct dwarf2_per_objfile *dwarf2_per_objfile
25154 = cu->per_cu->dwarf2_per_objfile;
25155 struct objfile *objfile = dwarf2_per_objfile->objfile;
25156 struct line_header *lh = cu->line_header;
25157 bfd *abfd;
25158 const gdb_byte *mac_ptr, *mac_end;
25159 struct macro_source_file *current_file = 0;
25160 enum dwarf_macro_record_type macinfo_type;
25161 unsigned int offset_size = cu->header.offset_size;
25162 const gdb_byte *opcode_definitions[256];
25163 void **slot;
25164 struct dwarf2_section_info *section;
25165 const char *section_name;
25166
25167 if (cu->dwo_unit != NULL)
25168 {
25169 if (section_is_gnu)
25170 {
25171 section = &cu->dwo_unit->dwo_file->sections.macro;
25172 section_name = ".debug_macro.dwo";
25173 }
25174 else
25175 {
25176 section = &cu->dwo_unit->dwo_file->sections.macinfo;
25177 section_name = ".debug_macinfo.dwo";
25178 }
25179 }
25180 else
25181 {
25182 if (section_is_gnu)
25183 {
25184 section = &dwarf2_per_objfile->macro;
25185 section_name = ".debug_macro";
25186 }
25187 else
25188 {
25189 section = &dwarf2_per_objfile->macinfo;
25190 section_name = ".debug_macinfo";
25191 }
25192 }
25193
25194 dwarf2_read_section (objfile, section);
25195 if (section->buffer == NULL)
25196 {
25197 complaint (_("missing %s section"), section_name);
25198 return;
25199 }
25200 abfd = get_section_bfd_owner (section);
25201
25202 /* First pass: Find the name of the base filename.
25203 This filename is needed in order to process all macros whose definition
25204 (or undefinition) comes from the command line. These macros are defined
25205 before the first DW_MACINFO_start_file entry, and yet still need to be
25206 associated to the base file.
25207
25208 To determine the base file name, we scan the macro definitions until we
25209 reach the first DW_MACINFO_start_file entry. We then initialize
25210 CURRENT_FILE accordingly so that any macro definition found before the
25211 first DW_MACINFO_start_file can still be associated to the base file. */
25212
25213 mac_ptr = section->buffer + offset;
25214 mac_end = section->buffer + section->size;
25215
25216 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
25217 &offset_size, section_is_gnu);
25218 if (mac_ptr == NULL)
25219 {
25220 /* We already issued a complaint. */
25221 return;
25222 }
25223
25224 do
25225 {
25226 /* Do we at least have room for a macinfo type byte? */
25227 if (mac_ptr >= mac_end)
25228 {
25229 /* Complaint is printed during the second pass as GDB will probably
25230 stop the first pass earlier upon finding
25231 DW_MACINFO_start_file. */
25232 break;
25233 }
25234
25235 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
25236 mac_ptr++;
25237
25238 /* Note that we rely on the fact that the corresponding GNU and
25239 DWARF constants are the same. */
25240 DIAGNOSTIC_PUSH
25241 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
25242 switch (macinfo_type)
25243 {
25244 /* A zero macinfo type indicates the end of the macro
25245 information. */
25246 case 0:
25247 break;
25248
25249 case DW_MACRO_define:
25250 case DW_MACRO_undef:
25251 /* Only skip the data by MAC_PTR. */
25252 {
25253 unsigned int bytes_read;
25254
25255 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25256 mac_ptr += bytes_read;
25257 read_direct_string (abfd, mac_ptr, &bytes_read);
25258 mac_ptr += bytes_read;
25259 }
25260 break;
25261
25262 case DW_MACRO_start_file:
25263 {
25264 unsigned int bytes_read;
25265 int line, file;
25266
25267 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25268 mac_ptr += bytes_read;
25269 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25270 mac_ptr += bytes_read;
25271
25272 current_file = macro_start_file (cu, file, line, current_file, lh);
25273 }
25274 break;
25275
25276 case DW_MACRO_end_file:
25277 /* No data to skip by MAC_PTR. */
25278 break;
25279
25280 case DW_MACRO_define_strp:
25281 case DW_MACRO_undef_strp:
25282 case DW_MACRO_define_sup:
25283 case DW_MACRO_undef_sup:
25284 {
25285 unsigned int bytes_read;
25286
25287 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25288 mac_ptr += bytes_read;
25289 mac_ptr += offset_size;
25290 }
25291 break;
25292
25293 case DW_MACRO_import:
25294 case DW_MACRO_import_sup:
25295 /* Note that, according to the spec, a transparent include
25296 chain cannot call DW_MACRO_start_file. So, we can just
25297 skip this opcode. */
25298 mac_ptr += offset_size;
25299 break;
25300
25301 case DW_MACINFO_vendor_ext:
25302 /* Only skip the data by MAC_PTR. */
25303 if (!section_is_gnu)
25304 {
25305 unsigned int bytes_read;
25306
25307 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25308 mac_ptr += bytes_read;
25309 read_direct_string (abfd, mac_ptr, &bytes_read);
25310 mac_ptr += bytes_read;
25311 }
25312 /* FALLTHROUGH */
25313
25314 default:
25315 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
25316 mac_ptr, mac_end, abfd, offset_size,
25317 section);
25318 if (mac_ptr == NULL)
25319 return;
25320 break;
25321 }
25322 DIAGNOSTIC_POP
25323 } while (macinfo_type != 0 && current_file == NULL);
25324
25325 /* Second pass: Process all entries.
25326
25327 Use the AT_COMMAND_LINE flag to determine whether we are still processing
25328 command-line macro definitions/undefinitions. This flag is unset when we
25329 reach the first DW_MACINFO_start_file entry. */
25330
25331 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
25332 htab_eq_pointer,
25333 NULL, xcalloc, xfree));
25334 mac_ptr = section->buffer + offset;
25335 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
25336 *slot = (void *) mac_ptr;
25337 dwarf_decode_macro_bytes (cu, abfd, mac_ptr, mac_end,
25338 current_file, lh, section,
25339 section_is_gnu, 0, offset_size,
25340 include_hash.get ());
25341 }
25342
25343 /* Check if the attribute's form is a DW_FORM_block*
25344 if so return true else false. */
25345
25346 static int
25347 attr_form_is_block (const struct attribute *attr)
25348 {
25349 return (attr == NULL ? 0 :
25350 attr->form == DW_FORM_block1
25351 || attr->form == DW_FORM_block2
25352 || attr->form == DW_FORM_block4
25353 || attr->form == DW_FORM_block
25354 || attr->form == DW_FORM_exprloc);
25355 }
25356
25357 /* Return non-zero if ATTR's value is a section offset --- classes
25358 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
25359 You may use DW_UNSND (attr) to retrieve such offsets.
25360
25361 Section 7.5.4, "Attribute Encodings", explains that no attribute
25362 may have a value that belongs to more than one of these classes; it
25363 would be ambiguous if we did, because we use the same forms for all
25364 of them. */
25365
25366 static int
25367 attr_form_is_section_offset (const struct attribute *attr)
25368 {
25369 return (attr->form == DW_FORM_data4
25370 || attr->form == DW_FORM_data8
25371 || attr->form == DW_FORM_sec_offset);
25372 }
25373
25374 /* Return non-zero if ATTR's value falls in the 'constant' class, or
25375 zero otherwise. When this function returns true, you can apply
25376 dwarf2_get_attr_constant_value to it.
25377
25378 However, note that for some attributes you must check
25379 attr_form_is_section_offset before using this test. DW_FORM_data4
25380 and DW_FORM_data8 are members of both the constant class, and of
25381 the classes that contain offsets into other debug sections
25382 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
25383 that, if an attribute's can be either a constant or one of the
25384 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
25385 taken as section offsets, not constants.
25386
25387 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
25388 cannot handle that. */
25389
25390 static int
25391 attr_form_is_constant (const struct attribute *attr)
25392 {
25393 switch (attr->form)
25394 {
25395 case DW_FORM_sdata:
25396 case DW_FORM_udata:
25397 case DW_FORM_data1:
25398 case DW_FORM_data2:
25399 case DW_FORM_data4:
25400 case DW_FORM_data8:
25401 case DW_FORM_implicit_const:
25402 return 1;
25403 default:
25404 return 0;
25405 }
25406 }
25407
25408
25409 /* DW_ADDR is always stored already as sect_offset; despite for the forms
25410 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
25411
25412 static int
25413 attr_form_is_ref (const struct attribute *attr)
25414 {
25415 switch (attr->form)
25416 {
25417 case DW_FORM_ref_addr:
25418 case DW_FORM_ref1:
25419 case DW_FORM_ref2:
25420 case DW_FORM_ref4:
25421 case DW_FORM_ref8:
25422 case DW_FORM_ref_udata:
25423 case DW_FORM_GNU_ref_alt:
25424 return 1;
25425 default:
25426 return 0;
25427 }
25428 }
25429
25430 /* Return the .debug_loc section to use for CU.
25431 For DWO files use .debug_loc.dwo. */
25432
25433 static struct dwarf2_section_info *
25434 cu_debug_loc_section (struct dwarf2_cu *cu)
25435 {
25436 struct dwarf2_per_objfile *dwarf2_per_objfile
25437 = cu->per_cu->dwarf2_per_objfile;
25438
25439 if (cu->dwo_unit)
25440 {
25441 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
25442
25443 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
25444 }
25445 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
25446 : &dwarf2_per_objfile->loc);
25447 }
25448
25449 /* A helper function that fills in a dwarf2_loclist_baton. */
25450
25451 static void
25452 fill_in_loclist_baton (struct dwarf2_cu *cu,
25453 struct dwarf2_loclist_baton *baton,
25454 const struct attribute *attr)
25455 {
25456 struct dwarf2_per_objfile *dwarf2_per_objfile
25457 = cu->per_cu->dwarf2_per_objfile;
25458 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
25459
25460 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
25461
25462 baton->per_cu = cu->per_cu;
25463 gdb_assert (baton->per_cu);
25464 /* We don't know how long the location list is, but make sure we
25465 don't run off the edge of the section. */
25466 baton->size = section->size - DW_UNSND (attr);
25467 baton->data = section->buffer + DW_UNSND (attr);
25468 baton->base_address = cu->base_address;
25469 baton->from_dwo = cu->dwo_unit != NULL;
25470 }
25471
25472 static void
25473 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
25474 struct dwarf2_cu *cu, int is_block)
25475 {
25476 struct dwarf2_per_objfile *dwarf2_per_objfile
25477 = cu->per_cu->dwarf2_per_objfile;
25478 struct objfile *objfile = dwarf2_per_objfile->objfile;
25479 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
25480
25481 if (attr_form_is_section_offset (attr)
25482 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
25483 the section. If so, fall through to the complaint in the
25484 other branch. */
25485 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
25486 {
25487 struct dwarf2_loclist_baton *baton;
25488
25489 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
25490
25491 fill_in_loclist_baton (cu, baton, attr);
25492
25493 if (cu->base_known == 0)
25494 complaint (_("Location list used without "
25495 "specifying the CU base address."));
25496
25497 SYMBOL_ACLASS_INDEX (sym) = (is_block
25498 ? dwarf2_loclist_block_index
25499 : dwarf2_loclist_index);
25500 SYMBOL_LOCATION_BATON (sym) = baton;
25501 }
25502 else
25503 {
25504 struct dwarf2_locexpr_baton *baton;
25505
25506 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
25507 baton->per_cu = cu->per_cu;
25508 gdb_assert (baton->per_cu);
25509
25510 if (attr_form_is_block (attr))
25511 {
25512 /* Note that we're just copying the block's data pointer
25513 here, not the actual data. We're still pointing into the
25514 info_buffer for SYM's objfile; right now we never release
25515 that buffer, but when we do clean up properly this may
25516 need to change. */
25517 baton->size = DW_BLOCK (attr)->size;
25518 baton->data = DW_BLOCK (attr)->data;
25519 }
25520 else
25521 {
25522 dwarf2_invalid_attrib_class_complaint ("location description",
25523 sym->natural_name ());
25524 baton->size = 0;
25525 }
25526
25527 SYMBOL_ACLASS_INDEX (sym) = (is_block
25528 ? dwarf2_locexpr_block_index
25529 : dwarf2_locexpr_index);
25530 SYMBOL_LOCATION_BATON (sym) = baton;
25531 }
25532 }
25533
25534 /* Return the OBJFILE associated with the compilation unit CU. If CU
25535 came from a separate debuginfo file, then the master objfile is
25536 returned. */
25537
25538 struct objfile *
25539 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
25540 {
25541 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
25542
25543 /* Return the master objfile, so that we can report and look up the
25544 correct file containing this variable. */
25545 if (objfile->separate_debug_objfile_backlink)
25546 objfile = objfile->separate_debug_objfile_backlink;
25547
25548 return objfile;
25549 }
25550
25551 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
25552 (CU_HEADERP is unused in such case) or prepare a temporary copy at
25553 CU_HEADERP first. */
25554
25555 static const struct comp_unit_head *
25556 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
25557 struct dwarf2_per_cu_data *per_cu)
25558 {
25559 const gdb_byte *info_ptr;
25560
25561 if (per_cu->cu)
25562 return &per_cu->cu->header;
25563
25564 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
25565
25566 memset (cu_headerp, 0, sizeof (*cu_headerp));
25567 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
25568 rcuh_kind::COMPILE);
25569
25570 return cu_headerp;
25571 }
25572
25573 /* Return the address size given in the compilation unit header for CU. */
25574
25575 int
25576 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
25577 {
25578 struct comp_unit_head cu_header_local;
25579 const struct comp_unit_head *cu_headerp;
25580
25581 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25582
25583 return cu_headerp->addr_size;
25584 }
25585
25586 /* Return the offset size given in the compilation unit header for CU. */
25587
25588 int
25589 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
25590 {
25591 struct comp_unit_head cu_header_local;
25592 const struct comp_unit_head *cu_headerp;
25593
25594 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25595
25596 return cu_headerp->offset_size;
25597 }
25598
25599 /* See its dwarf2loc.h declaration. */
25600
25601 int
25602 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
25603 {
25604 struct comp_unit_head cu_header_local;
25605 const struct comp_unit_head *cu_headerp;
25606
25607 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25608
25609 if (cu_headerp->version == 2)
25610 return cu_headerp->addr_size;
25611 else
25612 return cu_headerp->offset_size;
25613 }
25614
25615 /* Return the text offset of the CU. The returned offset comes from
25616 this CU's objfile. If this objfile came from a separate debuginfo
25617 file, then the offset may be different from the corresponding
25618 offset in the parent objfile. */
25619
25620 CORE_ADDR
25621 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
25622 {
25623 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
25624
25625 return objfile->section_offsets[SECT_OFF_TEXT (objfile)];
25626 }
25627
25628 /* Return a type that is a generic pointer type, the size of which matches
25629 the address size given in the compilation unit header for PER_CU. */
25630 static struct type *
25631 dwarf2_per_cu_addr_type (struct dwarf2_per_cu_data *per_cu)
25632 {
25633 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
25634 struct type *void_type = objfile_type (objfile)->builtin_void;
25635 struct type *addr_type = lookup_pointer_type (void_type);
25636 int addr_size = dwarf2_per_cu_addr_size (per_cu);
25637
25638 if (TYPE_LENGTH (addr_type) == addr_size)
25639 return addr_type;
25640
25641 addr_type
25642 = dwarf2_per_cu_addr_sized_int_type (per_cu, TYPE_UNSIGNED (addr_type));
25643 return addr_type;
25644 }
25645
25646 /* Return DWARF version number of PER_CU. */
25647
25648 short
25649 dwarf2_version (struct dwarf2_per_cu_data *per_cu)
25650 {
25651 return per_cu->dwarf_version;
25652 }
25653
25654 /* Locate the .debug_info compilation unit from CU's objfile which contains
25655 the DIE at OFFSET. Raises an error on failure. */
25656
25657 static struct dwarf2_per_cu_data *
25658 dwarf2_find_containing_comp_unit (sect_offset sect_off,
25659 unsigned int offset_in_dwz,
25660 struct dwarf2_per_objfile *dwarf2_per_objfile)
25661 {
25662 struct dwarf2_per_cu_data *this_cu;
25663 int low, high;
25664
25665 low = 0;
25666 high = dwarf2_per_objfile->all_comp_units.size () - 1;
25667 while (high > low)
25668 {
25669 struct dwarf2_per_cu_data *mid_cu;
25670 int mid = low + (high - low) / 2;
25671
25672 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
25673 if (mid_cu->is_dwz > offset_in_dwz
25674 || (mid_cu->is_dwz == offset_in_dwz
25675 && mid_cu->sect_off + mid_cu->length >= sect_off))
25676 high = mid;
25677 else
25678 low = mid + 1;
25679 }
25680 gdb_assert (low == high);
25681 this_cu = dwarf2_per_objfile->all_comp_units[low];
25682 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
25683 {
25684 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
25685 error (_("Dwarf Error: could not find partial DIE containing "
25686 "offset %s [in module %s]"),
25687 sect_offset_str (sect_off),
25688 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
25689
25690 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
25691 <= sect_off);
25692 return dwarf2_per_objfile->all_comp_units[low-1];
25693 }
25694 else
25695 {
25696 if (low == dwarf2_per_objfile->all_comp_units.size () - 1
25697 && sect_off >= this_cu->sect_off + this_cu->length)
25698 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
25699 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
25700 return this_cu;
25701 }
25702 }
25703
25704 /* Initialize dwarf2_cu CU, owned by PER_CU. */
25705
25706 dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
25707 : per_cu (per_cu_),
25708 mark (false),
25709 has_loclist (false),
25710 checked_producer (false),
25711 producer_is_gxx_lt_4_6 (false),
25712 producer_is_gcc_lt_4_3 (false),
25713 producer_is_icc (false),
25714 producer_is_icc_lt_14 (false),
25715 producer_is_codewarrior (false),
25716 processing_has_namespace_info (false)
25717 {
25718 per_cu->cu = this;
25719 }
25720
25721 /* Destroy a dwarf2_cu. */
25722
25723 dwarf2_cu::~dwarf2_cu ()
25724 {
25725 per_cu->cu = NULL;
25726 }
25727
25728 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
25729
25730 static void
25731 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
25732 enum language pretend_language)
25733 {
25734 struct attribute *attr;
25735
25736 /* Set the language we're debugging. */
25737 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
25738 if (attr != nullptr)
25739 set_cu_language (DW_UNSND (attr), cu);
25740 else
25741 {
25742 cu->language = pretend_language;
25743 cu->language_defn = language_def (cu->language);
25744 }
25745
25746 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
25747 }
25748
25749 /* Increase the age counter on each cached compilation unit, and free
25750 any that are too old. */
25751
25752 static void
25753 age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
25754 {
25755 struct dwarf2_per_cu_data *per_cu, **last_chain;
25756
25757 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
25758 per_cu = dwarf2_per_objfile->read_in_chain;
25759 while (per_cu != NULL)
25760 {
25761 per_cu->cu->last_used ++;
25762 if (per_cu->cu->last_used <= dwarf_max_cache_age)
25763 dwarf2_mark (per_cu->cu);
25764 per_cu = per_cu->cu->read_in_chain;
25765 }
25766
25767 per_cu = dwarf2_per_objfile->read_in_chain;
25768 last_chain = &dwarf2_per_objfile->read_in_chain;
25769 while (per_cu != NULL)
25770 {
25771 struct dwarf2_per_cu_data *next_cu;
25772
25773 next_cu = per_cu->cu->read_in_chain;
25774
25775 if (!per_cu->cu->mark)
25776 {
25777 delete per_cu->cu;
25778 *last_chain = next_cu;
25779 }
25780 else
25781 last_chain = &per_cu->cu->read_in_chain;
25782
25783 per_cu = next_cu;
25784 }
25785 }
25786
25787 /* Remove a single compilation unit from the cache. */
25788
25789 static void
25790 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
25791 {
25792 struct dwarf2_per_cu_data *per_cu, **last_chain;
25793 struct dwarf2_per_objfile *dwarf2_per_objfile
25794 = target_per_cu->dwarf2_per_objfile;
25795
25796 per_cu = dwarf2_per_objfile->read_in_chain;
25797 last_chain = &dwarf2_per_objfile->read_in_chain;
25798 while (per_cu != NULL)
25799 {
25800 struct dwarf2_per_cu_data *next_cu;
25801
25802 next_cu = per_cu->cu->read_in_chain;
25803
25804 if (per_cu == target_per_cu)
25805 {
25806 delete per_cu->cu;
25807 per_cu->cu = NULL;
25808 *last_chain = next_cu;
25809 break;
25810 }
25811 else
25812 last_chain = &per_cu->cu->read_in_chain;
25813
25814 per_cu = next_cu;
25815 }
25816 }
25817
25818 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25819 We store these in a hash table separate from the DIEs, and preserve them
25820 when the DIEs are flushed out of cache.
25821
25822 The CU "per_cu" pointer is needed because offset alone is not enough to
25823 uniquely identify the type. A file may have multiple .debug_types sections,
25824 or the type may come from a DWO file. Furthermore, while it's more logical
25825 to use per_cu->section+offset, with Fission the section with the data is in
25826 the DWO file but we don't know that section at the point we need it.
25827 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25828 because we can enter the lookup routine, get_die_type_at_offset, from
25829 outside this file, and thus won't necessarily have PER_CU->cu.
25830 Fortunately, PER_CU is stable for the life of the objfile. */
25831
25832 struct dwarf2_per_cu_offset_and_type
25833 {
25834 const struct dwarf2_per_cu_data *per_cu;
25835 sect_offset sect_off;
25836 struct type *type;
25837 };
25838
25839 /* Hash function for a dwarf2_per_cu_offset_and_type. */
25840
25841 static hashval_t
25842 per_cu_offset_and_type_hash (const void *item)
25843 {
25844 const struct dwarf2_per_cu_offset_and_type *ofs
25845 = (const struct dwarf2_per_cu_offset_and_type *) item;
25846
25847 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
25848 }
25849
25850 /* Equality function for a dwarf2_per_cu_offset_and_type. */
25851
25852 static int
25853 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
25854 {
25855 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25856 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25857 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25858 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
25859
25860 return (ofs_lhs->per_cu == ofs_rhs->per_cu
25861 && ofs_lhs->sect_off == ofs_rhs->sect_off);
25862 }
25863
25864 /* Set the type associated with DIE to TYPE. Save it in CU's hash
25865 table if necessary. For convenience, return TYPE.
25866
25867 The DIEs reading must have careful ordering to:
25868 * Not cause infinite loops trying to read in DIEs as a prerequisite for
25869 reading current DIE.
25870 * Not trying to dereference contents of still incompletely read in types
25871 while reading in other DIEs.
25872 * Enable referencing still incompletely read in types just by a pointer to
25873 the type without accessing its fields.
25874
25875 Therefore caller should follow these rules:
25876 * Try to fetch any prerequisite types we may need to build this DIE type
25877 before building the type and calling set_die_type.
25878 * After building type call set_die_type for current DIE as soon as
25879 possible before fetching more types to complete the current type.
25880 * Make the type as complete as possible before fetching more types. */
25881
25882 static struct type *
25883 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
25884 {
25885 struct dwarf2_per_objfile *dwarf2_per_objfile
25886 = cu->per_cu->dwarf2_per_objfile;
25887 struct dwarf2_per_cu_offset_and_type **slot, ofs;
25888 struct objfile *objfile = dwarf2_per_objfile->objfile;
25889 struct attribute *attr;
25890 struct dynamic_prop prop;
25891
25892 /* For Ada types, make sure that the gnat-specific data is always
25893 initialized (if not already set). There are a few types where
25894 we should not be doing so, because the type-specific area is
25895 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25896 where the type-specific area is used to store the floatformat).
25897 But this is not a problem, because the gnat-specific information
25898 is actually not needed for these types. */
25899 if (need_gnat_info (cu)
25900 && TYPE_CODE (type) != TYPE_CODE_FUNC
25901 && TYPE_CODE (type) != TYPE_CODE_FLT
25902 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
25903 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
25904 && TYPE_CODE (type) != TYPE_CODE_METHOD
25905 && !HAVE_GNAT_AUX_INFO (type))
25906 INIT_GNAT_SPECIFIC (type);
25907
25908 /* Read DW_AT_allocated and set in type. */
25909 attr = dwarf2_attr (die, DW_AT_allocated, cu);
25910 if (attr_form_is_block (attr))
25911 {
25912 struct type *prop_type
25913 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
25914 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
25915 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
25916 }
25917 else if (attr != NULL)
25918 {
25919 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
25920 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25921 sect_offset_str (die->sect_off));
25922 }
25923
25924 /* Read DW_AT_associated and set in type. */
25925 attr = dwarf2_attr (die, DW_AT_associated, cu);
25926 if (attr_form_is_block (attr))
25927 {
25928 struct type *prop_type
25929 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
25930 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
25931 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
25932 }
25933 else if (attr != NULL)
25934 {
25935 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
25936 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25937 sect_offset_str (die->sect_off));
25938 }
25939
25940 /* Read DW_AT_data_location and set in type. */
25941 attr = dwarf2_attr (die, DW_AT_data_location, cu);
25942 if (attr_to_dynamic_prop (attr, die, cu, &prop,
25943 dwarf2_per_cu_addr_type (cu->per_cu)))
25944 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
25945
25946 if (dwarf2_per_objfile->die_type_hash == NULL)
25947 {
25948 dwarf2_per_objfile->die_type_hash =
25949 htab_create_alloc_ex (127,
25950 per_cu_offset_and_type_hash,
25951 per_cu_offset_and_type_eq,
25952 NULL,
25953 &objfile->objfile_obstack,
25954 hashtab_obstack_allocate,
25955 dummy_obstack_deallocate);
25956 }
25957
25958 ofs.per_cu = cu->per_cu;
25959 ofs.sect_off = die->sect_off;
25960 ofs.type = type;
25961 slot = (struct dwarf2_per_cu_offset_and_type **)
25962 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
25963 if (*slot)
25964 complaint (_("A problem internal to GDB: DIE %s has type already set"),
25965 sect_offset_str (die->sect_off));
25966 *slot = XOBNEW (&objfile->objfile_obstack,
25967 struct dwarf2_per_cu_offset_and_type);
25968 **slot = ofs;
25969 return type;
25970 }
25971
25972 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
25973 or return NULL if the die does not have a saved type. */
25974
25975 static struct type *
25976 get_die_type_at_offset (sect_offset sect_off,
25977 struct dwarf2_per_cu_data *per_cu)
25978 {
25979 struct dwarf2_per_cu_offset_and_type *slot, ofs;
25980 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
25981
25982 if (dwarf2_per_objfile->die_type_hash == NULL)
25983 return NULL;
25984
25985 ofs.per_cu = per_cu;
25986 ofs.sect_off = sect_off;
25987 slot = ((struct dwarf2_per_cu_offset_and_type *)
25988 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
25989 if (slot)
25990 return slot->type;
25991 else
25992 return NULL;
25993 }
25994
25995 /* Look up the type for DIE in CU in die_type_hash,
25996 or return NULL if DIE does not have a saved type. */
25997
25998 static struct type *
25999 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
26000 {
26001 return get_die_type_at_offset (die->sect_off, cu->per_cu);
26002 }
26003
26004 /* Add a dependence relationship from CU to REF_PER_CU. */
26005
26006 static void
26007 dwarf2_add_dependence (struct dwarf2_cu *cu,
26008 struct dwarf2_per_cu_data *ref_per_cu)
26009 {
26010 void **slot;
26011
26012 if (cu->dependencies == NULL)
26013 cu->dependencies
26014 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
26015 NULL, &cu->comp_unit_obstack,
26016 hashtab_obstack_allocate,
26017 dummy_obstack_deallocate);
26018
26019 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
26020 if (*slot == NULL)
26021 *slot = ref_per_cu;
26022 }
26023
26024 /* Subroutine of dwarf2_mark to pass to htab_traverse.
26025 Set the mark field in every compilation unit in the
26026 cache that we must keep because we are keeping CU. */
26027
26028 static int
26029 dwarf2_mark_helper (void **slot, void *data)
26030 {
26031 struct dwarf2_per_cu_data *per_cu;
26032
26033 per_cu = (struct dwarf2_per_cu_data *) *slot;
26034
26035 /* cu->dependencies references may not yet have been ever read if QUIT aborts
26036 reading of the chain. As such dependencies remain valid it is not much
26037 useful to track and undo them during QUIT cleanups. */
26038 if (per_cu->cu == NULL)
26039 return 1;
26040
26041 if (per_cu->cu->mark)
26042 return 1;
26043 per_cu->cu->mark = true;
26044
26045 if (per_cu->cu->dependencies != NULL)
26046 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
26047
26048 return 1;
26049 }
26050
26051 /* Set the mark field in CU and in every other compilation unit in the
26052 cache that we must keep because we are keeping CU. */
26053
26054 static void
26055 dwarf2_mark (struct dwarf2_cu *cu)
26056 {
26057 if (cu->mark)
26058 return;
26059 cu->mark = true;
26060 if (cu->dependencies != NULL)
26061 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
26062 }
26063
26064 static void
26065 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
26066 {
26067 while (per_cu)
26068 {
26069 per_cu->cu->mark = false;
26070 per_cu = per_cu->cu->read_in_chain;
26071 }
26072 }
26073
26074 /* Trivial hash function for partial_die_info: the hash value of a DIE
26075 is its offset in .debug_info for this objfile. */
26076
26077 static hashval_t
26078 partial_die_hash (const void *item)
26079 {
26080 const struct partial_die_info *part_die
26081 = (const struct partial_die_info *) item;
26082
26083 return to_underlying (part_die->sect_off);
26084 }
26085
26086 /* Trivial comparison function for partial_die_info structures: two DIEs
26087 are equal if they have the same offset. */
26088
26089 static int
26090 partial_die_eq (const void *item_lhs, const void *item_rhs)
26091 {
26092 const struct partial_die_info *part_die_lhs
26093 = (const struct partial_die_info *) item_lhs;
26094 const struct partial_die_info *part_die_rhs
26095 = (const struct partial_die_info *) item_rhs;
26096
26097 return part_die_lhs->sect_off == part_die_rhs->sect_off;
26098 }
26099
26100 struct cmd_list_element *set_dwarf_cmdlist;
26101 struct cmd_list_element *show_dwarf_cmdlist;
26102
26103 static void
26104 set_dwarf_cmd (const char *args, int from_tty)
26105 {
26106 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
26107 gdb_stdout);
26108 }
26109
26110 static void
26111 show_dwarf_cmd (const char *args, int from_tty)
26112 {
26113 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
26114 }
26115
26116 bool dwarf_always_disassemble;
26117
26118 static void
26119 show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
26120 struct cmd_list_element *c, const char *value)
26121 {
26122 fprintf_filtered (file,
26123 _("Whether to always disassemble "
26124 "DWARF expressions is %s.\n"),
26125 value);
26126 }
26127
26128 static void
26129 show_check_physname (struct ui_file *file, int from_tty,
26130 struct cmd_list_element *c, const char *value)
26131 {
26132 fprintf_filtered (file,
26133 _("Whether to check \"physname\" is %s.\n"),
26134 value);
26135 }
26136
26137 void
26138 _initialize_dwarf2_read (void)
26139 {
26140 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
26141 Set DWARF specific variables.\n\
26142 Configure DWARF variables such as the cache size."),
26143 &set_dwarf_cmdlist, "maintenance set dwarf ",
26144 0/*allow-unknown*/, &maintenance_set_cmdlist);
26145
26146 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
26147 Show DWARF specific variables.\n\
26148 Show DWARF variables such as the cache size."),
26149 &show_dwarf_cmdlist, "maintenance show dwarf ",
26150 0/*allow-unknown*/, &maintenance_show_cmdlist);
26151
26152 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
26153 &dwarf_max_cache_age, _("\
26154 Set the upper bound on the age of cached DWARF compilation units."), _("\
26155 Show the upper bound on the age of cached DWARF compilation units."), _("\
26156 A higher limit means that cached compilation units will be stored\n\
26157 in memory longer, and more total memory will be used. Zero disables\n\
26158 caching, which can slow down startup."),
26159 NULL,
26160 show_dwarf_max_cache_age,
26161 &set_dwarf_cmdlist,
26162 &show_dwarf_cmdlist);
26163
26164 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
26165 &dwarf_always_disassemble, _("\
26166 Set whether `info address' always disassembles DWARF expressions."), _("\
26167 Show whether `info address' always disassembles DWARF expressions."), _("\
26168 When enabled, DWARF expressions are always printed in an assembly-like\n\
26169 syntax. When disabled, expressions will be printed in a more\n\
26170 conversational style, when possible."),
26171 NULL,
26172 show_dwarf_always_disassemble,
26173 &set_dwarf_cmdlist,
26174 &show_dwarf_cmdlist);
26175
26176 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
26177 Set debugging of the DWARF reader."), _("\
26178 Show debugging of the DWARF reader."), _("\
26179 When enabled (non-zero), debugging messages are printed during DWARF\n\
26180 reading and symtab expansion. A value of 1 (one) provides basic\n\
26181 information. A value greater than 1 provides more verbose information."),
26182 NULL,
26183 NULL,
26184 &setdebuglist, &showdebuglist);
26185
26186 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
26187 Set debugging of the DWARF DIE reader."), _("\
26188 Show debugging of the DWARF DIE reader."), _("\
26189 When enabled (non-zero), DIEs are dumped after they are read in.\n\
26190 The value is the maximum depth to print."),
26191 NULL,
26192 NULL,
26193 &setdebuglist, &showdebuglist);
26194
26195 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
26196 Set debugging of the dwarf line reader."), _("\
26197 Show debugging of the dwarf line reader."), _("\
26198 When enabled (non-zero), line number entries are dumped as they are read in.\n\
26199 A value of 1 (one) provides basic information.\n\
26200 A value greater than 1 provides more verbose information."),
26201 NULL,
26202 NULL,
26203 &setdebuglist, &showdebuglist);
26204
26205 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
26206 Set cross-checking of \"physname\" code against demangler."), _("\
26207 Show cross-checking of \"physname\" code against demangler."), _("\
26208 When enabled, GDB's internal \"physname\" code is checked against\n\
26209 the demangler."),
26210 NULL, show_check_physname,
26211 &setdebuglist, &showdebuglist);
26212
26213 add_setshow_boolean_cmd ("use-deprecated-index-sections",
26214 no_class, &use_deprecated_index_sections, _("\
26215 Set whether to use deprecated gdb_index sections."), _("\
26216 Show whether to use deprecated gdb_index sections."), _("\
26217 When enabled, deprecated .gdb_index sections are used anyway.\n\
26218 Normally they are ignored either because of a missing feature or\n\
26219 performance issue.\n\
26220 Warning: This option must be enabled before gdb reads the file."),
26221 NULL,
26222 NULL,
26223 &setlist, &showlist);
26224
26225 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
26226 &dwarf2_locexpr_funcs);
26227 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
26228 &dwarf2_loclist_funcs);
26229
26230 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
26231 &dwarf2_block_frame_base_locexpr_funcs);
26232 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
26233 &dwarf2_block_frame_base_loclist_funcs);
26234
26235 #if GDB_SELF_TEST
26236 selftests::register_test ("dw2_expand_symtabs_matching",
26237 selftests::dw2_expand_symtabs_matching::run_test);
26238 #endif
26239 }
This page took 0.597225 seconds and 4 git commands to generate.