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 #ifndef ATTR_ALLOC_CHUNK
1344 #define ATTR_ALLOC_CHUNK 4
1345 #endif
1346
1347 /* Allocate fields for structs, unions and enums in this size. */
1348 #ifndef DW_FIELD_ALLOC_CHUNK
1349 #define DW_FIELD_ALLOC_CHUNK 4
1350 #endif
1351
1352 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1353 but this would require a corresponding change in unpack_field_as_long
1354 and friends. */
1355 static int bits_per_byte = 8;
1356
1357 /* When reading a variant or variant part, we track a bit more
1358 information about the field, and store it in an object of this
1359 type. */
1360
1361 struct variant_field
1362 {
1363 /* If we see a DW_TAG_variant, then this will be the discriminant
1364 value. */
1365 ULONGEST discriminant_value;
1366 /* If we see a DW_TAG_variant, then this will be set if this is the
1367 default branch. */
1368 bool default_branch;
1369 /* While reading a DW_TAG_variant_part, this will be set if this
1370 field is the discriminant. */
1371 bool is_discriminant;
1372 };
1373
1374 struct nextfield
1375 {
1376 int accessibility = 0;
1377 int virtuality = 0;
1378 /* Extra information to describe a variant or variant part. */
1379 struct variant_field variant {};
1380 struct field field {};
1381 };
1382
1383 struct fnfieldlist
1384 {
1385 const char *name = nullptr;
1386 std::vector<struct fn_field> fnfields;
1387 };
1388
1389 /* The routines that read and process dies for a C struct or C++ class
1390 pass lists of data member fields and lists of member function fields
1391 in an instance of a field_info structure, as defined below. */
1392 struct field_info
1393 {
1394 /* List of data member and baseclasses fields. */
1395 std::vector<struct nextfield> fields;
1396 std::vector<struct nextfield> baseclasses;
1397
1398 /* Number of fields (including baseclasses). */
1399 int nfields = 0;
1400
1401 /* Set if the accessibility of one of the fields is not public. */
1402 int non_public_fields = 0;
1403
1404 /* Member function fieldlist array, contains name of possibly overloaded
1405 member function, number of overloaded member functions and a pointer
1406 to the head of the member function field chain. */
1407 std::vector<struct fnfieldlist> fnfieldlists;
1408
1409 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1410 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1411 std::vector<struct decl_field> typedef_field_list;
1412
1413 /* Nested types defined by this class and the number of elements in this
1414 list. */
1415 std::vector<struct decl_field> nested_types_list;
1416 };
1417
1418 /* One item on the queue of compilation units to read in full symbols
1419 for. */
1420 struct dwarf2_queue_item
1421 {
1422 struct dwarf2_per_cu_data *per_cu;
1423 enum language pretend_language;
1424 struct dwarf2_queue_item *next;
1425 };
1426
1427 /* The current queue. */
1428 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1429
1430 /* Loaded secondary compilation units are kept in memory until they
1431 have not been referenced for the processing of this many
1432 compilation units. Set this to zero to disable caching. Cache
1433 sizes of up to at least twenty will improve startup time for
1434 typical inter-CU-reference binaries, at an obvious memory cost. */
1435 static int dwarf_max_cache_age = 5;
1436 static void
1437 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1438 struct cmd_list_element *c, const char *value)
1439 {
1440 fprintf_filtered (file, _("The upper bound on the age of cached "
1441 "DWARF compilation units is %s.\n"),
1442 value);
1443 }
1444 \f
1445 /* local function prototypes */
1446
1447 static const char *get_section_name (const struct dwarf2_section_info *);
1448
1449 static const char *get_section_file_name (const struct dwarf2_section_info *);
1450
1451 static void dwarf2_find_base_address (struct die_info *die,
1452 struct dwarf2_cu *cu);
1453
1454 static struct partial_symtab *create_partial_symtab
1455 (struct dwarf2_per_cu_data *per_cu, const char *name);
1456
1457 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1458 const gdb_byte *info_ptr,
1459 struct die_info *type_unit_die,
1460 int has_children, void *data);
1461
1462 static void dwarf2_build_psymtabs_hard
1463 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1464
1465 static void scan_partial_symbols (struct partial_die_info *,
1466 CORE_ADDR *, CORE_ADDR *,
1467 int, struct dwarf2_cu *);
1468
1469 static void add_partial_symbol (struct partial_die_info *,
1470 struct dwarf2_cu *);
1471
1472 static void add_partial_namespace (struct partial_die_info *pdi,
1473 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1474 int set_addrmap, struct dwarf2_cu *cu);
1475
1476 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1477 CORE_ADDR *highpc, int set_addrmap,
1478 struct dwarf2_cu *cu);
1479
1480 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1481 struct dwarf2_cu *cu);
1482
1483 static void add_partial_subprogram (struct partial_die_info *pdi,
1484 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1485 int need_pc, struct dwarf2_cu *cu);
1486
1487 static void dwarf2_read_symtab (struct partial_symtab *,
1488 struct objfile *);
1489
1490 static void psymtab_to_symtab_1 (struct partial_symtab *);
1491
1492 static abbrev_table_up abbrev_table_read_table
1493 (struct dwarf2_per_objfile *dwarf2_per_objfile, struct dwarf2_section_info *,
1494 sect_offset);
1495
1496 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1497
1498 static struct partial_die_info *load_partial_dies
1499 (const struct die_reader_specs *, const gdb_byte *, int);
1500
1501 /* A pair of partial_die_info and compilation unit. */
1502 struct cu_partial_die_info
1503 {
1504 /* The compilation unit of the partial_die_info. */
1505 struct dwarf2_cu *cu;
1506 /* A partial_die_info. */
1507 struct partial_die_info *pdi;
1508
1509 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1510 : cu (cu),
1511 pdi (pdi)
1512 { /* Nothing. */ }
1513
1514 private:
1515 cu_partial_die_info () = delete;
1516 };
1517
1518 static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1519 struct dwarf2_cu *);
1520
1521 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1522 struct attribute *, struct attr_abbrev *,
1523 const gdb_byte *);
1524
1525 static unsigned int read_1_byte (bfd *, const gdb_byte *);
1526
1527 static int read_1_signed_byte (bfd *, const gdb_byte *);
1528
1529 static unsigned int read_2_bytes (bfd *, const gdb_byte *);
1530
1531 /* Read the next three bytes (little-endian order) as an unsigned integer. */
1532 static unsigned int read_3_bytes (bfd *, const gdb_byte *);
1533
1534 static unsigned int read_4_bytes (bfd *, const gdb_byte *);
1535
1536 static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
1537
1538 static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
1539 unsigned int *);
1540
1541 static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
1542
1543 static LONGEST read_checked_initial_length_and_offset
1544 (bfd *, const gdb_byte *, const struct comp_unit_head *,
1545 unsigned int *, unsigned int *);
1546
1547 static LONGEST read_offset (bfd *, const gdb_byte *,
1548 const struct comp_unit_head *,
1549 unsigned int *);
1550
1551 static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
1552
1553 static sect_offset read_abbrev_offset
1554 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1555 struct dwarf2_section_info *, sect_offset);
1556
1557 static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
1558
1559 static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
1560
1561 static const char *read_indirect_string
1562 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1563 const struct comp_unit_head *, unsigned int *);
1564
1565 static const char *read_indirect_line_string
1566 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1567 const struct comp_unit_head *, unsigned int *);
1568
1569 static const char *read_indirect_string_at_offset
1570 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
1571 LONGEST str_offset);
1572
1573 static const char *read_indirect_string_from_dwz
1574 (struct objfile *objfile, struct dwz_file *, LONGEST);
1575
1576 static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
1577
1578 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1579 const gdb_byte *,
1580 unsigned int *);
1581
1582 static const char *read_str_index (const struct die_reader_specs *reader,
1583 ULONGEST str_index);
1584
1585 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1586
1587 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1588 struct dwarf2_cu *);
1589
1590 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1591 unsigned int);
1592
1593 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1594 struct dwarf2_cu *cu);
1595
1596 static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1597
1598 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1599 struct dwarf2_cu *cu);
1600
1601 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1602
1603 static struct die_info *die_specification (struct die_info *die,
1604 struct dwarf2_cu **);
1605
1606 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1607 struct dwarf2_cu *cu);
1608
1609 static void dwarf_decode_lines (struct line_header *, const char *,
1610 struct dwarf2_cu *, struct partial_symtab *,
1611 CORE_ADDR, int decode_mapping);
1612
1613 static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1614 const char *);
1615
1616 static struct symbol *new_symbol (struct die_info *, struct type *,
1617 struct dwarf2_cu *, struct symbol * = NULL);
1618
1619 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1620 struct dwarf2_cu *);
1621
1622 static void dwarf2_const_value_attr (const struct attribute *attr,
1623 struct type *type,
1624 const char *name,
1625 struct obstack *obstack,
1626 struct dwarf2_cu *cu, LONGEST *value,
1627 const gdb_byte **bytes,
1628 struct dwarf2_locexpr_baton **baton);
1629
1630 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1631
1632 static int need_gnat_info (struct dwarf2_cu *);
1633
1634 static struct type *die_descriptive_type (struct die_info *,
1635 struct dwarf2_cu *);
1636
1637 static void set_descriptive_type (struct type *, struct die_info *,
1638 struct dwarf2_cu *);
1639
1640 static struct type *die_containing_type (struct die_info *,
1641 struct dwarf2_cu *);
1642
1643 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1644 struct dwarf2_cu *);
1645
1646 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1647
1648 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1649
1650 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1651
1652 static char *typename_concat (struct obstack *obs, const char *prefix,
1653 const char *suffix, int physname,
1654 struct dwarf2_cu *cu);
1655
1656 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1657
1658 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1659
1660 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1661
1662 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1663
1664 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1665
1666 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1667
1668 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1669 struct dwarf2_cu *, struct partial_symtab *);
1670
1671 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1672 values. Keep the items ordered with increasing constraints compliance. */
1673 enum pc_bounds_kind
1674 {
1675 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1676 PC_BOUNDS_NOT_PRESENT,
1677
1678 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1679 were present but they do not form a valid range of PC addresses. */
1680 PC_BOUNDS_INVALID,
1681
1682 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1683 PC_BOUNDS_RANGES,
1684
1685 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1686 PC_BOUNDS_HIGH_LOW,
1687 };
1688
1689 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1690 CORE_ADDR *, CORE_ADDR *,
1691 struct dwarf2_cu *,
1692 struct partial_symtab *);
1693
1694 static void get_scope_pc_bounds (struct die_info *,
1695 CORE_ADDR *, CORE_ADDR *,
1696 struct dwarf2_cu *);
1697
1698 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1699 CORE_ADDR, struct dwarf2_cu *);
1700
1701 static void dwarf2_add_field (struct field_info *, struct die_info *,
1702 struct dwarf2_cu *);
1703
1704 static void dwarf2_attach_fields_to_type (struct field_info *,
1705 struct type *, struct dwarf2_cu *);
1706
1707 static void dwarf2_add_member_fn (struct field_info *,
1708 struct die_info *, struct type *,
1709 struct dwarf2_cu *);
1710
1711 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1712 struct type *,
1713 struct dwarf2_cu *);
1714
1715 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1716
1717 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1718
1719 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1720
1721 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1722
1723 static struct using_direct **using_directives (struct dwarf2_cu *cu);
1724
1725 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1726
1727 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1728
1729 static struct type *read_module_type (struct die_info *die,
1730 struct dwarf2_cu *cu);
1731
1732 static const char *namespace_name (struct die_info *die,
1733 int *is_anonymous, struct dwarf2_cu *);
1734
1735 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1736
1737 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1738
1739 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1740 struct dwarf2_cu *);
1741
1742 static struct die_info *read_die_and_siblings_1
1743 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1744 struct die_info *);
1745
1746 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1747 const gdb_byte *info_ptr,
1748 const gdb_byte **new_info_ptr,
1749 struct die_info *parent);
1750
1751 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1752 struct die_info **, const gdb_byte *,
1753 int *, int);
1754
1755 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1756 struct die_info **, const gdb_byte *,
1757 int *);
1758
1759 static void process_die (struct die_info *, struct dwarf2_cu *);
1760
1761 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1762 struct obstack *);
1763
1764 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1765
1766 static const char *dwarf2_full_name (const char *name,
1767 struct die_info *die,
1768 struct dwarf2_cu *cu);
1769
1770 static const char *dwarf2_physname (const char *name, struct die_info *die,
1771 struct dwarf2_cu *cu);
1772
1773 static struct die_info *dwarf2_extension (struct die_info *die,
1774 struct dwarf2_cu **);
1775
1776 static const char *dwarf_tag_name (unsigned int);
1777
1778 static const char *dwarf_attr_name (unsigned int);
1779
1780 static const char *dwarf_unit_type_name (int unit_type);
1781
1782 static const char *dwarf_form_name (unsigned int);
1783
1784 static const char *dwarf_bool_name (unsigned int);
1785
1786 static const char *dwarf_type_encoding_name (unsigned int);
1787
1788 static struct die_info *sibling_die (struct die_info *);
1789
1790 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1791
1792 static void dump_die_for_error (struct die_info *);
1793
1794 static void dump_die_1 (struct ui_file *, int level, int max_level,
1795 struct die_info *);
1796
1797 /*static*/ void dump_die (struct die_info *, int max_level);
1798
1799 static void store_in_ref_table (struct die_info *,
1800 struct dwarf2_cu *);
1801
1802 static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
1803
1804 static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
1805
1806 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1807 const struct attribute *,
1808 struct dwarf2_cu **);
1809
1810 static struct die_info *follow_die_ref (struct die_info *,
1811 const struct attribute *,
1812 struct dwarf2_cu **);
1813
1814 static struct die_info *follow_die_sig (struct die_info *,
1815 const struct attribute *,
1816 struct dwarf2_cu **);
1817
1818 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1819 struct dwarf2_cu *);
1820
1821 static struct type *get_DW_AT_signature_type (struct die_info *,
1822 const struct attribute *,
1823 struct dwarf2_cu *);
1824
1825 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1826
1827 static void read_signatured_type (struct signatured_type *);
1828
1829 static int attr_to_dynamic_prop (const struct attribute *attr,
1830 struct die_info *die, struct dwarf2_cu *cu,
1831 struct dynamic_prop *prop, struct type *type);
1832
1833 /* memory allocation interface */
1834
1835 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1836
1837 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1838
1839 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1840
1841 static int attr_form_is_block (const struct attribute *);
1842
1843 static int attr_form_is_section_offset (const struct attribute *);
1844
1845 static int attr_form_is_constant (const struct attribute *);
1846
1847 static int attr_form_is_ref (const struct attribute *);
1848
1849 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1850 struct dwarf2_loclist_baton *baton,
1851 const struct attribute *attr);
1852
1853 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1854 struct symbol *sym,
1855 struct dwarf2_cu *cu,
1856 int is_block);
1857
1858 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1859 const gdb_byte *info_ptr,
1860 struct abbrev_info *abbrev);
1861
1862 static hashval_t partial_die_hash (const void *item);
1863
1864 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1865
1866 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1867 (sect_offset sect_off, unsigned int offset_in_dwz,
1868 struct dwarf2_per_objfile *dwarf2_per_objfile);
1869
1870 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1871 struct die_info *comp_unit_die,
1872 enum language pretend_language);
1873
1874 static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1875
1876 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1877
1878 static struct type *set_die_type (struct die_info *, struct type *,
1879 struct dwarf2_cu *);
1880
1881 static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1882
1883 static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1884
1885 static void load_full_comp_unit (struct dwarf2_per_cu_data *, bool,
1886 enum language);
1887
1888 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1889 enum language);
1890
1891 static void process_full_type_unit (struct dwarf2_per_cu_data *,
1892 enum language);
1893
1894 static void dwarf2_add_dependence (struct dwarf2_cu *,
1895 struct dwarf2_per_cu_data *);
1896
1897 static void dwarf2_mark (struct dwarf2_cu *);
1898
1899 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1900
1901 static struct type *get_die_type_at_offset (sect_offset,
1902 struct dwarf2_per_cu_data *);
1903
1904 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1905
1906 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1907 enum language pretend_language);
1908
1909 static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
1910
1911 static struct type *dwarf2_per_cu_addr_type (struct dwarf2_per_cu_data *per_cu);
1912 static struct type *dwarf2_per_cu_addr_sized_int_type
1913 (struct dwarf2_per_cu_data *per_cu, bool unsigned_p);
1914 static struct type *dwarf2_per_cu_int_type
1915 (struct dwarf2_per_cu_data *per_cu, int size_in_bytes,
1916 bool unsigned_p);
1917
1918 /* Class, the destructor of which frees all allocated queue entries. This
1919 will only have work to do if an error was thrown while processing the
1920 dwarf. If no error was thrown then the queue entries should have all
1921 been processed, and freed, as we went along. */
1922
1923 class dwarf2_queue_guard
1924 {
1925 public:
1926 dwarf2_queue_guard () = default;
1927
1928 /* Free any entries remaining on the queue. There should only be
1929 entries left if we hit an error while processing the dwarf. */
1930 ~dwarf2_queue_guard ()
1931 {
1932 struct dwarf2_queue_item *item, *last;
1933
1934 item = dwarf2_queue;
1935 while (item)
1936 {
1937 /* Anything still marked queued is likely to be in an
1938 inconsistent state, so discard it. */
1939 if (item->per_cu->queued)
1940 {
1941 if (item->per_cu->cu != NULL)
1942 free_one_cached_comp_unit (item->per_cu);
1943 item->per_cu->queued = 0;
1944 }
1945
1946 last = item;
1947 item = item->next;
1948 xfree (last);
1949 }
1950
1951 dwarf2_queue = dwarf2_queue_tail = NULL;
1952 }
1953 };
1954
1955 /* The return type of find_file_and_directory. Note, the enclosed
1956 string pointers are only valid while this object is valid. */
1957
1958 struct file_and_directory
1959 {
1960 /* The filename. This is never NULL. */
1961 const char *name;
1962
1963 /* The compilation directory. NULL if not known. If we needed to
1964 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1965 points directly to the DW_AT_comp_dir string attribute owned by
1966 the obstack that owns the DIE. */
1967 const char *comp_dir;
1968
1969 /* If we needed to build a new string for comp_dir, this is what
1970 owns the storage. */
1971 std::string comp_dir_storage;
1972 };
1973
1974 static file_and_directory find_file_and_directory (struct die_info *die,
1975 struct dwarf2_cu *cu);
1976
1977 static char *file_full_name (int file, struct line_header *lh,
1978 const char *comp_dir);
1979
1980 /* Expected enum dwarf_unit_type for read_comp_unit_head. */
1981 enum class rcuh_kind { COMPILE, TYPE };
1982
1983 static const gdb_byte *read_and_check_comp_unit_head
1984 (struct dwarf2_per_objfile* dwarf2_per_objfile,
1985 struct comp_unit_head *header,
1986 struct dwarf2_section_info *section,
1987 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
1988 rcuh_kind section_kind);
1989
1990 static void init_cutu_and_read_dies
1991 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
1992 int use_existing_cu, int keep, bool skip_partial,
1993 die_reader_func_ftype *die_reader_func, void *data);
1994
1995 static void init_cutu_and_read_dies_simple
1996 (struct dwarf2_per_cu_data *this_cu,
1997 die_reader_func_ftype *die_reader_func, void *data);
1998
1999 static htab_t allocate_signatured_type_table (struct objfile *objfile);
2000
2001 static htab_t allocate_dwo_unit_table (struct objfile *objfile);
2002
2003 static struct dwo_unit *lookup_dwo_unit_in_dwp
2004 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2005 struct dwp_file *dwp_file, const char *comp_dir,
2006 ULONGEST signature, int is_debug_types);
2007
2008 static struct dwp_file *get_dwp_file
2009 (struct dwarf2_per_objfile *dwarf2_per_objfile);
2010
2011 static struct dwo_unit *lookup_dwo_comp_unit
2012 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
2013
2014 static struct dwo_unit *lookup_dwo_type_unit
2015 (struct signatured_type *, const char *, const char *);
2016
2017 static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
2018
2019 /* A unique pointer to a dwo_file. */
2020
2021 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
2022
2023 static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
2024
2025 static void check_producer (struct dwarf2_cu *cu);
2026
2027 static void free_line_header_voidp (void *arg);
2028 \f
2029 /* Various complaints about symbol reading that don't abort the process. */
2030
2031 static void
2032 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2033 {
2034 complaint (_("statement list doesn't fit in .debug_line section"));
2035 }
2036
2037 static void
2038 dwarf2_debug_line_missing_file_complaint (void)
2039 {
2040 complaint (_(".debug_line section has line data without a file"));
2041 }
2042
2043 static void
2044 dwarf2_debug_line_missing_end_sequence_complaint (void)
2045 {
2046 complaint (_(".debug_line section has line "
2047 "program sequence without an end"));
2048 }
2049
2050 static void
2051 dwarf2_complex_location_expr_complaint (void)
2052 {
2053 complaint (_("location expression too complex"));
2054 }
2055
2056 static void
2057 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
2058 int arg3)
2059 {
2060 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
2061 arg1, arg2, arg3);
2062 }
2063
2064 static void
2065 dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2066 {
2067 complaint (_("debug info runs off end of %s section"
2068 " [in module %s]"),
2069 get_section_name (section),
2070 get_section_file_name (section));
2071 }
2072
2073 static void
2074 dwarf2_macro_malformed_definition_complaint (const char *arg1)
2075 {
2076 complaint (_("macro debug info contains a "
2077 "malformed macro definition:\n`%s'"),
2078 arg1);
2079 }
2080
2081 static void
2082 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
2083 {
2084 complaint (_("invalid attribute class or form for '%s' in '%s'"),
2085 arg1, arg2);
2086 }
2087
2088 /* Hash function for line_header_hash. */
2089
2090 static hashval_t
2091 line_header_hash (const struct line_header *ofs)
2092 {
2093 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
2094 }
2095
2096 /* Hash function for htab_create_alloc_ex for line_header_hash. */
2097
2098 static hashval_t
2099 line_header_hash_voidp (const void *item)
2100 {
2101 const struct line_header *ofs = (const struct line_header *) item;
2102
2103 return line_header_hash (ofs);
2104 }
2105
2106 /* Equality function for line_header_hash. */
2107
2108 static int
2109 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
2110 {
2111 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
2112 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
2113
2114 return (ofs_lhs->sect_off == ofs_rhs->sect_off
2115 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
2116 }
2117
2118 \f
2119
2120 /* Read the given attribute value as an address, taking the attribute's
2121 form into account. */
2122
2123 static CORE_ADDR
2124 attr_value_as_address (struct attribute *attr)
2125 {
2126 CORE_ADDR addr;
2127
2128 if (attr->form != DW_FORM_addr && attr->form != DW_FORM_addrx
2129 && attr->form != DW_FORM_GNU_addr_index)
2130 {
2131 /* Aside from a few clearly defined exceptions, attributes that
2132 contain an address must always be in DW_FORM_addr form.
2133 Unfortunately, some compilers happen to be violating this
2134 requirement by encoding addresses using other forms, such
2135 as DW_FORM_data4 for example. For those broken compilers,
2136 we try to do our best, without any guarantee of success,
2137 to interpret the address correctly. It would also be nice
2138 to generate a complaint, but that would require us to maintain
2139 a list of legitimate cases where a non-address form is allowed,
2140 as well as update callers to pass in at least the CU's DWARF
2141 version. This is more overhead than what we're willing to
2142 expand for a pretty rare case. */
2143 addr = DW_UNSND (attr);
2144 }
2145 else
2146 addr = DW_ADDR (attr);
2147
2148 return addr;
2149 }
2150
2151 /* See declaration. */
2152
2153 dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
2154 const dwarf2_debug_sections *names,
2155 bool can_copy_)
2156 : objfile (objfile_),
2157 can_copy (can_copy_)
2158 {
2159 if (names == NULL)
2160 names = &dwarf2_elf_names;
2161
2162 bfd *obfd = objfile->obfd;
2163
2164 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
2165 locate_sections (obfd, sec, *names);
2166 }
2167
2168 dwarf2_per_objfile::~dwarf2_per_objfile ()
2169 {
2170 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
2171 free_cached_comp_units ();
2172
2173 if (quick_file_names_table)
2174 htab_delete (quick_file_names_table);
2175
2176 if (line_header_hash)
2177 htab_delete (line_header_hash);
2178
2179 for (dwarf2_per_cu_data *per_cu : all_comp_units)
2180 per_cu->imported_symtabs_free ();
2181
2182 for (signatured_type *sig_type : all_type_units)
2183 sig_type->per_cu.imported_symtabs_free ();
2184
2185 /* Everything else should be on the objfile obstack. */
2186 }
2187
2188 /* See declaration. */
2189
2190 void
2191 dwarf2_per_objfile::free_cached_comp_units ()
2192 {
2193 dwarf2_per_cu_data *per_cu = read_in_chain;
2194 dwarf2_per_cu_data **last_chain = &read_in_chain;
2195 while (per_cu != NULL)
2196 {
2197 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
2198
2199 delete per_cu->cu;
2200 *last_chain = next_cu;
2201 per_cu = next_cu;
2202 }
2203 }
2204
2205 /* A helper class that calls free_cached_comp_units on
2206 destruction. */
2207
2208 class free_cached_comp_units
2209 {
2210 public:
2211
2212 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
2213 : m_per_objfile (per_objfile)
2214 {
2215 }
2216
2217 ~free_cached_comp_units ()
2218 {
2219 m_per_objfile->free_cached_comp_units ();
2220 }
2221
2222 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
2223
2224 private:
2225
2226 dwarf2_per_objfile *m_per_objfile;
2227 };
2228
2229 /* Try to locate the sections we need for DWARF 2 debugging
2230 information and return true if we have enough to do something.
2231 NAMES points to the dwarf2 section names, or is NULL if the standard
2232 ELF names are used. CAN_COPY is true for formats where symbol
2233 interposition is possible and so symbol values must follow copy
2234 relocation rules. */
2235
2236 int
2237 dwarf2_has_info (struct objfile *objfile,
2238 const struct dwarf2_debug_sections *names,
2239 bool can_copy)
2240 {
2241 if (objfile->flags & OBJF_READNEVER)
2242 return 0;
2243
2244 struct dwarf2_per_objfile *dwarf2_per_objfile
2245 = get_dwarf2_per_objfile (objfile);
2246
2247 if (dwarf2_per_objfile == NULL)
2248 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile,
2249 names,
2250 can_copy);
2251
2252 return (!dwarf2_per_objfile->info.is_virtual
2253 && dwarf2_per_objfile->info.s.section != NULL
2254 && !dwarf2_per_objfile->abbrev.is_virtual
2255 && dwarf2_per_objfile->abbrev.s.section != NULL);
2256 }
2257
2258 /* Return the containing section of virtual section SECTION. */
2259
2260 static struct dwarf2_section_info *
2261 get_containing_section (const struct dwarf2_section_info *section)
2262 {
2263 gdb_assert (section->is_virtual);
2264 return section->s.containing_section;
2265 }
2266
2267 /* Return the bfd owner of SECTION. */
2268
2269 static struct bfd *
2270 get_section_bfd_owner (const struct dwarf2_section_info *section)
2271 {
2272 if (section->is_virtual)
2273 {
2274 section = get_containing_section (section);
2275 gdb_assert (!section->is_virtual);
2276 }
2277 return section->s.section->owner;
2278 }
2279
2280 /* Return the bfd section of SECTION.
2281 Returns NULL if the section is not present. */
2282
2283 static asection *
2284 get_section_bfd_section (const struct dwarf2_section_info *section)
2285 {
2286 if (section->is_virtual)
2287 {
2288 section = get_containing_section (section);
2289 gdb_assert (!section->is_virtual);
2290 }
2291 return section->s.section;
2292 }
2293
2294 /* Return the name of SECTION. */
2295
2296 static const char *
2297 get_section_name (const struct dwarf2_section_info *section)
2298 {
2299 asection *sectp = get_section_bfd_section (section);
2300
2301 gdb_assert (sectp != NULL);
2302 return bfd_section_name (sectp);
2303 }
2304
2305 /* Return the name of the file SECTION is in. */
2306
2307 static const char *
2308 get_section_file_name (const struct dwarf2_section_info *section)
2309 {
2310 bfd *abfd = get_section_bfd_owner (section);
2311
2312 return bfd_get_filename (abfd);
2313 }
2314
2315 /* Return the id of SECTION.
2316 Returns 0 if SECTION doesn't exist. */
2317
2318 static int
2319 get_section_id (const struct dwarf2_section_info *section)
2320 {
2321 asection *sectp = get_section_bfd_section (section);
2322
2323 if (sectp == NULL)
2324 return 0;
2325 return sectp->id;
2326 }
2327
2328 /* Return the flags of SECTION.
2329 SECTION (or containing section if this is a virtual section) must exist. */
2330
2331 static int
2332 get_section_flags (const struct dwarf2_section_info *section)
2333 {
2334 asection *sectp = get_section_bfd_section (section);
2335
2336 gdb_assert (sectp != NULL);
2337 return bfd_section_flags (sectp);
2338 }
2339
2340 /* When loading sections, we look either for uncompressed section or for
2341 compressed section names. */
2342
2343 static int
2344 section_is_p (const char *section_name,
2345 const struct dwarf2_section_names *names)
2346 {
2347 if (names->normal != NULL
2348 && strcmp (section_name, names->normal) == 0)
2349 return 1;
2350 if (names->compressed != NULL
2351 && strcmp (section_name, names->compressed) == 0)
2352 return 1;
2353 return 0;
2354 }
2355
2356 /* See declaration. */
2357
2358 void
2359 dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
2360 const dwarf2_debug_sections &names)
2361 {
2362 flagword aflag = bfd_section_flags (sectp);
2363
2364 if ((aflag & SEC_HAS_CONTENTS) == 0)
2365 {
2366 }
2367 else if (elf_section_data (sectp)->this_hdr.sh_size
2368 > bfd_get_file_size (abfd))
2369 {
2370 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
2371 warning (_("Discarding section %s which has a section size (%s"
2372 ") larger than the file size [in module %s]"),
2373 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
2374 bfd_get_filename (abfd));
2375 }
2376 else if (section_is_p (sectp->name, &names.info))
2377 {
2378 this->info.s.section = sectp;
2379 this->info.size = bfd_section_size (sectp);
2380 }
2381 else if (section_is_p (sectp->name, &names.abbrev))
2382 {
2383 this->abbrev.s.section = sectp;
2384 this->abbrev.size = bfd_section_size (sectp);
2385 }
2386 else if (section_is_p (sectp->name, &names.line))
2387 {
2388 this->line.s.section = sectp;
2389 this->line.size = bfd_section_size (sectp);
2390 }
2391 else if (section_is_p (sectp->name, &names.loc))
2392 {
2393 this->loc.s.section = sectp;
2394 this->loc.size = bfd_section_size (sectp);
2395 }
2396 else if (section_is_p (sectp->name, &names.loclists))
2397 {
2398 this->loclists.s.section = sectp;
2399 this->loclists.size = bfd_section_size (sectp);
2400 }
2401 else if (section_is_p (sectp->name, &names.macinfo))
2402 {
2403 this->macinfo.s.section = sectp;
2404 this->macinfo.size = bfd_section_size (sectp);
2405 }
2406 else if (section_is_p (sectp->name, &names.macro))
2407 {
2408 this->macro.s.section = sectp;
2409 this->macro.size = bfd_section_size (sectp);
2410 }
2411 else if (section_is_p (sectp->name, &names.str))
2412 {
2413 this->str.s.section = sectp;
2414 this->str.size = bfd_section_size (sectp);
2415 }
2416 else if (section_is_p (sectp->name, &names.line_str))
2417 {
2418 this->line_str.s.section = sectp;
2419 this->line_str.size = bfd_section_size (sectp);
2420 }
2421 else if (section_is_p (sectp->name, &names.addr))
2422 {
2423 this->addr.s.section = sectp;
2424 this->addr.size = bfd_section_size (sectp);
2425 }
2426 else if (section_is_p (sectp->name, &names.frame))
2427 {
2428 this->frame.s.section = sectp;
2429 this->frame.size = bfd_section_size (sectp);
2430 }
2431 else if (section_is_p (sectp->name, &names.eh_frame))
2432 {
2433 this->eh_frame.s.section = sectp;
2434 this->eh_frame.size = bfd_section_size (sectp);
2435 }
2436 else if (section_is_p (sectp->name, &names.ranges))
2437 {
2438 this->ranges.s.section = sectp;
2439 this->ranges.size = bfd_section_size (sectp);
2440 }
2441 else if (section_is_p (sectp->name, &names.rnglists))
2442 {
2443 this->rnglists.s.section = sectp;
2444 this->rnglists.size = bfd_section_size (sectp);
2445 }
2446 else if (section_is_p (sectp->name, &names.types))
2447 {
2448 struct dwarf2_section_info type_section;
2449
2450 memset (&type_section, 0, sizeof (type_section));
2451 type_section.s.section = sectp;
2452 type_section.size = bfd_section_size (sectp);
2453
2454 this->types.push_back (type_section);
2455 }
2456 else if (section_is_p (sectp->name, &names.gdb_index))
2457 {
2458 this->gdb_index.s.section = sectp;
2459 this->gdb_index.size = bfd_section_size (sectp);
2460 }
2461 else if (section_is_p (sectp->name, &names.debug_names))
2462 {
2463 this->debug_names.s.section = sectp;
2464 this->debug_names.size = bfd_section_size (sectp);
2465 }
2466 else if (section_is_p (sectp->name, &names.debug_aranges))
2467 {
2468 this->debug_aranges.s.section = sectp;
2469 this->debug_aranges.size = bfd_section_size (sectp);
2470 }
2471
2472 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2473 && bfd_section_vma (sectp) == 0)
2474 this->has_section_at_zero = true;
2475 }
2476
2477 /* A helper function that decides whether a section is empty,
2478 or not present. */
2479
2480 static int
2481 dwarf2_section_empty_p (const struct dwarf2_section_info *section)
2482 {
2483 if (section->is_virtual)
2484 return section->size == 0;
2485 return section->s.section == NULL || section->size == 0;
2486 }
2487
2488 /* See dwarf2read.h. */
2489
2490 void
2491 dwarf2_read_section (struct objfile *objfile, dwarf2_section_info *info)
2492 {
2493 asection *sectp;
2494 bfd *abfd;
2495 gdb_byte *buf, *retbuf;
2496
2497 if (info->readin)
2498 return;
2499 info->buffer = NULL;
2500 info->readin = true;
2501
2502 if (dwarf2_section_empty_p (info))
2503 return;
2504
2505 sectp = get_section_bfd_section (info);
2506
2507 /* If this is a virtual section we need to read in the real one first. */
2508 if (info->is_virtual)
2509 {
2510 struct dwarf2_section_info *containing_section =
2511 get_containing_section (info);
2512
2513 gdb_assert (sectp != NULL);
2514 if ((sectp->flags & SEC_RELOC) != 0)
2515 {
2516 error (_("Dwarf Error: DWP format V2 with relocations is not"
2517 " supported in section %s [in module %s]"),
2518 get_section_name (info), get_section_file_name (info));
2519 }
2520 dwarf2_read_section (objfile, containing_section);
2521 /* Other code should have already caught virtual sections that don't
2522 fit. */
2523 gdb_assert (info->virtual_offset + info->size
2524 <= containing_section->size);
2525 /* If the real section is empty or there was a problem reading the
2526 section we shouldn't get here. */
2527 gdb_assert (containing_section->buffer != NULL);
2528 info->buffer = containing_section->buffer + info->virtual_offset;
2529 return;
2530 }
2531
2532 /* If the section has relocations, we must read it ourselves.
2533 Otherwise we attach it to the BFD. */
2534 if ((sectp->flags & SEC_RELOC) == 0)
2535 {
2536 info->buffer = gdb_bfd_map_section (sectp, &info->size);
2537 return;
2538 }
2539
2540 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, info->size);
2541 info->buffer = buf;
2542
2543 /* When debugging .o files, we may need to apply relocations; see
2544 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2545 We never compress sections in .o files, so we only need to
2546 try this when the section is not compressed. */
2547 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
2548 if (retbuf != NULL)
2549 {
2550 info->buffer = retbuf;
2551 return;
2552 }
2553
2554 abfd = get_section_bfd_owner (info);
2555 gdb_assert (abfd != NULL);
2556
2557 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2558 || bfd_bread (buf, info->size, abfd) != info->size)
2559 {
2560 error (_("Dwarf Error: Can't read DWARF data"
2561 " in section %s [in module %s]"),
2562 bfd_section_name (sectp), bfd_get_filename (abfd));
2563 }
2564 }
2565
2566 /* A helper function that returns the size of a section in a safe way.
2567 If you are positive that the section has been read before using the
2568 size, then it is safe to refer to the dwarf2_section_info object's
2569 "size" field directly. In other cases, you must call this
2570 function, because for compressed sections the size field is not set
2571 correctly until the section has been read. */
2572
2573 static bfd_size_type
2574 dwarf2_section_size (struct objfile *objfile,
2575 struct dwarf2_section_info *info)
2576 {
2577 if (!info->readin)
2578 dwarf2_read_section (objfile, info);
2579 return info->size;
2580 }
2581
2582 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2583 SECTION_NAME. */
2584
2585 void
2586 dwarf2_get_section_info (struct objfile *objfile,
2587 enum dwarf2_section_enum sect,
2588 asection **sectp, const gdb_byte **bufp,
2589 bfd_size_type *sizep)
2590 {
2591 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
2592 struct dwarf2_section_info *info;
2593
2594 /* We may see an objfile without any DWARF, in which case we just
2595 return nothing. */
2596 if (data == NULL)
2597 {
2598 *sectp = NULL;
2599 *bufp = NULL;
2600 *sizep = 0;
2601 return;
2602 }
2603 switch (sect)
2604 {
2605 case DWARF2_DEBUG_FRAME:
2606 info = &data->frame;
2607 break;
2608 case DWARF2_EH_FRAME:
2609 info = &data->eh_frame;
2610 break;
2611 default:
2612 gdb_assert_not_reached ("unexpected section");
2613 }
2614
2615 dwarf2_read_section (objfile, info);
2616
2617 *sectp = get_section_bfd_section (info);
2618 *bufp = info->buffer;
2619 *sizep = info->size;
2620 }
2621
2622 /* A helper function to find the sections for a .dwz file. */
2623
2624 static void
2625 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2626 {
2627 struct dwz_file *dwz_file = (struct dwz_file *) arg;
2628
2629 /* Note that we only support the standard ELF names, because .dwz
2630 is ELF-only (at the time of writing). */
2631 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2632 {
2633 dwz_file->abbrev.s.section = sectp;
2634 dwz_file->abbrev.size = bfd_section_size (sectp);
2635 }
2636 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2637 {
2638 dwz_file->info.s.section = sectp;
2639 dwz_file->info.size = bfd_section_size (sectp);
2640 }
2641 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2642 {
2643 dwz_file->str.s.section = sectp;
2644 dwz_file->str.size = bfd_section_size (sectp);
2645 }
2646 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2647 {
2648 dwz_file->line.s.section = sectp;
2649 dwz_file->line.size = bfd_section_size (sectp);
2650 }
2651 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2652 {
2653 dwz_file->macro.s.section = sectp;
2654 dwz_file->macro.size = bfd_section_size (sectp);
2655 }
2656 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2657 {
2658 dwz_file->gdb_index.s.section = sectp;
2659 dwz_file->gdb_index.size = bfd_section_size (sectp);
2660 }
2661 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2662 {
2663 dwz_file->debug_names.s.section = sectp;
2664 dwz_file->debug_names.size = bfd_section_size (sectp);
2665 }
2666 }
2667
2668 /* See dwarf2read.h. */
2669
2670 struct dwz_file *
2671 dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
2672 {
2673 const char *filename;
2674 bfd_size_type buildid_len_arg;
2675 size_t buildid_len;
2676 bfd_byte *buildid;
2677
2678 if (dwarf2_per_objfile->dwz_file != NULL)
2679 return dwarf2_per_objfile->dwz_file.get ();
2680
2681 bfd_set_error (bfd_error_no_error);
2682 gdb::unique_xmalloc_ptr<char> data
2683 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2684 &buildid_len_arg, &buildid));
2685 if (data == NULL)
2686 {
2687 if (bfd_get_error () == bfd_error_no_error)
2688 return NULL;
2689 error (_("could not read '.gnu_debugaltlink' section: %s"),
2690 bfd_errmsg (bfd_get_error ()));
2691 }
2692
2693 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
2694
2695 buildid_len = (size_t) buildid_len_arg;
2696
2697 filename = data.get ();
2698
2699 std::string abs_storage;
2700 if (!IS_ABSOLUTE_PATH (filename))
2701 {
2702 gdb::unique_xmalloc_ptr<char> abs
2703 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
2704
2705 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
2706 filename = abs_storage.c_str ();
2707 }
2708
2709 /* First try the file name given in the section. If that doesn't
2710 work, try to use the build-id instead. */
2711 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
2712 if (dwz_bfd != NULL)
2713 {
2714 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2715 dwz_bfd.reset (nullptr);
2716 }
2717
2718 if (dwz_bfd == NULL)
2719 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2720
2721 if (dwz_bfd == NULL)
2722 error (_("could not find '.gnu_debugaltlink' file for %s"),
2723 objfile_name (dwarf2_per_objfile->objfile));
2724
2725 std::unique_ptr<struct dwz_file> result
2726 (new struct dwz_file (std::move (dwz_bfd)));
2727
2728 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2729 result.get ());
2730
2731 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd,
2732 result->dwz_bfd.get ());
2733 dwarf2_per_objfile->dwz_file = std::move (result);
2734 return dwarf2_per_objfile->dwz_file.get ();
2735 }
2736 \f
2737 /* DWARF quick_symbols_functions support. */
2738
2739 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2740 unique line tables, so we maintain a separate table of all .debug_line
2741 derived entries to support the sharing.
2742 All the quick functions need is the list of file names. We discard the
2743 line_header when we're done and don't need to record it here. */
2744 struct quick_file_names
2745 {
2746 /* The data used to construct the hash key. */
2747 struct stmt_list_hash hash;
2748
2749 /* The number of entries in file_names, real_names. */
2750 unsigned int num_file_names;
2751
2752 /* The file names from the line table, after being run through
2753 file_full_name. */
2754 const char **file_names;
2755
2756 /* The file names from the line table after being run through
2757 gdb_realpath. These are computed lazily. */
2758 const char **real_names;
2759 };
2760
2761 /* When using the index (and thus not using psymtabs), each CU has an
2762 object of this type. This is used to hold information needed by
2763 the various "quick" methods. */
2764 struct dwarf2_per_cu_quick_data
2765 {
2766 /* The file table. This can be NULL if there was no file table
2767 or it's currently not read in.
2768 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2769 struct quick_file_names *file_names;
2770
2771 /* The corresponding symbol table. This is NULL if symbols for this
2772 CU have not yet been read. */
2773 struct compunit_symtab *compunit_symtab;
2774
2775 /* A temporary mark bit used when iterating over all CUs in
2776 expand_symtabs_matching. */
2777 unsigned int mark : 1;
2778
2779 /* True if we've tried to read the file table and found there isn't one.
2780 There will be no point in trying to read it again next time. */
2781 unsigned int no_file_data : 1;
2782 };
2783
2784 /* Utility hash function for a stmt_list_hash. */
2785
2786 static hashval_t
2787 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2788 {
2789 hashval_t v = 0;
2790
2791 if (stmt_list_hash->dwo_unit != NULL)
2792 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2793 v += to_underlying (stmt_list_hash->line_sect_off);
2794 return v;
2795 }
2796
2797 /* Utility equality function for a stmt_list_hash. */
2798
2799 static int
2800 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2801 const struct stmt_list_hash *rhs)
2802 {
2803 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2804 return 0;
2805 if (lhs->dwo_unit != NULL
2806 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2807 return 0;
2808
2809 return lhs->line_sect_off == rhs->line_sect_off;
2810 }
2811
2812 /* Hash function for a quick_file_names. */
2813
2814 static hashval_t
2815 hash_file_name_entry (const void *e)
2816 {
2817 const struct quick_file_names *file_data
2818 = (const struct quick_file_names *) e;
2819
2820 return hash_stmt_list_entry (&file_data->hash);
2821 }
2822
2823 /* Equality function for a quick_file_names. */
2824
2825 static int
2826 eq_file_name_entry (const void *a, const void *b)
2827 {
2828 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2829 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2830
2831 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2832 }
2833
2834 /* Delete function for a quick_file_names. */
2835
2836 static void
2837 delete_file_name_entry (void *e)
2838 {
2839 struct quick_file_names *file_data = (struct quick_file_names *) e;
2840 int i;
2841
2842 for (i = 0; i < file_data->num_file_names; ++i)
2843 {
2844 xfree ((void*) file_data->file_names[i]);
2845 if (file_data->real_names)
2846 xfree ((void*) file_data->real_names[i]);
2847 }
2848
2849 /* The space for the struct itself lives on objfile_obstack,
2850 so we don't free it here. */
2851 }
2852
2853 /* Create a quick_file_names hash table. */
2854
2855 static htab_t
2856 create_quick_file_names_table (unsigned int nr_initial_entries)
2857 {
2858 return htab_create_alloc (nr_initial_entries,
2859 hash_file_name_entry, eq_file_name_entry,
2860 delete_file_name_entry, xcalloc, xfree);
2861 }
2862
2863 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2864 have to be created afterwards. You should call age_cached_comp_units after
2865 processing PER_CU->CU. dw2_setup must have been already called. */
2866
2867 static void
2868 load_cu (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2869 {
2870 if (per_cu->is_debug_types)
2871 load_full_type_unit (per_cu);
2872 else
2873 load_full_comp_unit (per_cu, skip_partial, language_minimal);
2874
2875 if (per_cu->cu == NULL)
2876 return; /* Dummy CU. */
2877
2878 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2879 }
2880
2881 /* Read in the symbols for PER_CU. */
2882
2883 static void
2884 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2885 {
2886 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2887
2888 /* Skip type_unit_groups, reading the type units they contain
2889 is handled elsewhere. */
2890 if (IS_TYPE_UNIT_GROUP (per_cu))
2891 return;
2892
2893 /* The destructor of dwarf2_queue_guard frees any entries left on
2894 the queue. After this point we're guaranteed to leave this function
2895 with the dwarf queue empty. */
2896 dwarf2_queue_guard q_guard;
2897
2898 if (dwarf2_per_objfile->using_index
2899 ? per_cu->v.quick->compunit_symtab == NULL
2900 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2901 {
2902 queue_comp_unit (per_cu, language_minimal);
2903 load_cu (per_cu, skip_partial);
2904
2905 /* If we just loaded a CU from a DWO, and we're working with an index
2906 that may badly handle TUs, load all the TUs in that DWO as well.
2907 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2908 if (!per_cu->is_debug_types
2909 && per_cu->cu != NULL
2910 && per_cu->cu->dwo_unit != NULL
2911 && dwarf2_per_objfile->index_table != NULL
2912 && dwarf2_per_objfile->index_table->version <= 7
2913 /* DWP files aren't supported yet. */
2914 && get_dwp_file (dwarf2_per_objfile) == NULL)
2915 queue_and_load_all_dwo_tus (per_cu);
2916 }
2917
2918 process_queue (dwarf2_per_objfile);
2919
2920 /* Age the cache, releasing compilation units that have not
2921 been used recently. */
2922 age_cached_comp_units (dwarf2_per_objfile);
2923 }
2924
2925 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2926 the objfile from which this CU came. Returns the resulting symbol
2927 table. */
2928
2929 static struct compunit_symtab *
2930 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2931 {
2932 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2933
2934 gdb_assert (dwarf2_per_objfile->using_index);
2935 if (!per_cu->v.quick->compunit_symtab)
2936 {
2937 free_cached_comp_units freer (dwarf2_per_objfile);
2938 scoped_restore decrementer = increment_reading_symtab ();
2939 dw2_do_instantiate_symtab (per_cu, skip_partial);
2940 process_cu_includes (dwarf2_per_objfile);
2941 }
2942
2943 return per_cu->v.quick->compunit_symtab;
2944 }
2945
2946 /* See declaration. */
2947
2948 dwarf2_per_cu_data *
2949 dwarf2_per_objfile::get_cutu (int index)
2950 {
2951 if (index >= this->all_comp_units.size ())
2952 {
2953 index -= this->all_comp_units.size ();
2954 gdb_assert (index < this->all_type_units.size ());
2955 return &this->all_type_units[index]->per_cu;
2956 }
2957
2958 return this->all_comp_units[index];
2959 }
2960
2961 /* See declaration. */
2962
2963 dwarf2_per_cu_data *
2964 dwarf2_per_objfile::get_cu (int index)
2965 {
2966 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
2967
2968 return this->all_comp_units[index];
2969 }
2970
2971 /* See declaration. */
2972
2973 signatured_type *
2974 dwarf2_per_objfile::get_tu (int index)
2975 {
2976 gdb_assert (index >= 0 && index < this->all_type_units.size ());
2977
2978 return this->all_type_units[index];
2979 }
2980
2981 /* Return a new dwarf2_per_cu_data allocated on OBJFILE's
2982 objfile_obstack, and constructed with the specified field
2983 values. */
2984
2985 static dwarf2_per_cu_data *
2986 create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2987 struct dwarf2_section_info *section,
2988 int is_dwz,
2989 sect_offset sect_off, ULONGEST length)
2990 {
2991 struct objfile *objfile = dwarf2_per_objfile->objfile;
2992 dwarf2_per_cu_data *the_cu
2993 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2994 struct dwarf2_per_cu_data);
2995 the_cu->sect_off = sect_off;
2996 the_cu->length = length;
2997 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
2998 the_cu->section = section;
2999 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3000 struct dwarf2_per_cu_quick_data);
3001 the_cu->is_dwz = is_dwz;
3002 return the_cu;
3003 }
3004
3005 /* A helper for create_cus_from_index that handles a given list of
3006 CUs. */
3007
3008 static void
3009 create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
3010 const gdb_byte *cu_list, offset_type n_elements,
3011 struct dwarf2_section_info *section,
3012 int is_dwz)
3013 {
3014 for (offset_type i = 0; i < n_elements; i += 2)
3015 {
3016 gdb_static_assert (sizeof (ULONGEST) >= 8);
3017
3018 sect_offset sect_off
3019 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
3020 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
3021 cu_list += 2 * 8;
3022
3023 dwarf2_per_cu_data *per_cu
3024 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
3025 sect_off, length);
3026 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
3027 }
3028 }
3029
3030 /* Read the CU list from the mapped index, and use it to create all
3031 the CU objects for this objfile. */
3032
3033 static void
3034 create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
3035 const gdb_byte *cu_list, offset_type cu_list_elements,
3036 const gdb_byte *dwz_list, offset_type dwz_elements)
3037 {
3038 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
3039 dwarf2_per_objfile->all_comp_units.reserve
3040 ((cu_list_elements + dwz_elements) / 2);
3041
3042 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
3043 &dwarf2_per_objfile->info, 0);
3044
3045 if (dwz_elements == 0)
3046 return;
3047
3048 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3049 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
3050 &dwz->info, 1);
3051 }
3052
3053 /* Create the signatured type hash table from the index. */
3054
3055 static void
3056 create_signatured_type_table_from_index
3057 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3058 struct dwarf2_section_info *section,
3059 const gdb_byte *bytes,
3060 offset_type elements)
3061 {
3062 struct objfile *objfile = dwarf2_per_objfile->objfile;
3063
3064 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
3065 dwarf2_per_objfile->all_type_units.reserve (elements / 3);
3066
3067 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
3068
3069 for (offset_type i = 0; i < elements; i += 3)
3070 {
3071 struct signatured_type *sig_type;
3072 ULONGEST signature;
3073 void **slot;
3074 cu_offset type_offset_in_tu;
3075
3076 gdb_static_assert (sizeof (ULONGEST) >= 8);
3077 sect_offset sect_off
3078 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
3079 type_offset_in_tu
3080 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
3081 BFD_ENDIAN_LITTLE);
3082 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
3083 bytes += 3 * 8;
3084
3085 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3086 struct signatured_type);
3087 sig_type->signature = signature;
3088 sig_type->type_offset_in_tu = type_offset_in_tu;
3089 sig_type->per_cu.is_debug_types = 1;
3090 sig_type->per_cu.section = section;
3091 sig_type->per_cu.sect_off = sect_off;
3092 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3093 sig_type->per_cu.v.quick
3094 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3095 struct dwarf2_per_cu_quick_data);
3096
3097 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3098 *slot = sig_type;
3099
3100 dwarf2_per_objfile->all_type_units.push_back (sig_type);
3101 }
3102
3103 dwarf2_per_objfile->signatured_types = sig_types_hash;
3104 }
3105
3106 /* Create the signatured type hash table from .debug_names. */
3107
3108 static void
3109 create_signatured_type_table_from_debug_names
3110 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3111 const mapped_debug_names &map,
3112 struct dwarf2_section_info *section,
3113 struct dwarf2_section_info *abbrev_section)
3114 {
3115 struct objfile *objfile = dwarf2_per_objfile->objfile;
3116
3117 dwarf2_read_section (objfile, section);
3118 dwarf2_read_section (objfile, abbrev_section);
3119
3120 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
3121 dwarf2_per_objfile->all_type_units.reserve (map.tu_count);
3122
3123 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
3124
3125 for (uint32_t i = 0; i < map.tu_count; ++i)
3126 {
3127 struct signatured_type *sig_type;
3128 void **slot;
3129
3130 sect_offset sect_off
3131 = (sect_offset) (extract_unsigned_integer
3132 (map.tu_table_reordered + i * map.offset_size,
3133 map.offset_size,
3134 map.dwarf5_byte_order));
3135
3136 comp_unit_head cu_header;
3137 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
3138 abbrev_section,
3139 section->buffer + to_underlying (sect_off),
3140 rcuh_kind::TYPE);
3141
3142 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3143 struct signatured_type);
3144 sig_type->signature = cu_header.signature;
3145 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
3146 sig_type->per_cu.is_debug_types = 1;
3147 sig_type->per_cu.section = section;
3148 sig_type->per_cu.sect_off = sect_off;
3149 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3150 sig_type->per_cu.v.quick
3151 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3152 struct dwarf2_per_cu_quick_data);
3153
3154 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3155 *slot = sig_type;
3156
3157 dwarf2_per_objfile->all_type_units.push_back (sig_type);
3158 }
3159
3160 dwarf2_per_objfile->signatured_types = sig_types_hash;
3161 }
3162
3163 /* Read the address map data from the mapped index, and use it to
3164 populate the objfile's psymtabs_addrmap. */
3165
3166 static void
3167 create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
3168 struct mapped_index *index)
3169 {
3170 struct objfile *objfile = dwarf2_per_objfile->objfile;
3171 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3172 const gdb_byte *iter, *end;
3173 struct addrmap *mutable_map;
3174 CORE_ADDR baseaddr;
3175
3176 auto_obstack temp_obstack;
3177
3178 mutable_map = addrmap_create_mutable (&temp_obstack);
3179
3180 iter = index->address_table.data ();
3181 end = iter + index->address_table.size ();
3182
3183 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3184
3185 while (iter < end)
3186 {
3187 ULONGEST hi, lo, cu_index;
3188 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3189 iter += 8;
3190 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3191 iter += 8;
3192 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
3193 iter += 4;
3194
3195 if (lo > hi)
3196 {
3197 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
3198 hex_string (lo), hex_string (hi));
3199 continue;
3200 }
3201
3202 if (cu_index >= dwarf2_per_objfile->all_comp_units.size ())
3203 {
3204 complaint (_(".gdb_index address table has invalid CU number %u"),
3205 (unsigned) cu_index);
3206 continue;
3207 }
3208
3209 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
3210 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
3211 addrmap_set_empty (mutable_map, lo, hi - 1,
3212 dwarf2_per_objfile->get_cu (cu_index));
3213 }
3214
3215 objfile->partial_symtabs->psymtabs_addrmap
3216 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
3217 }
3218
3219 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
3220 populate the objfile's psymtabs_addrmap. */
3221
3222 static void
3223 create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
3224 struct dwarf2_section_info *section)
3225 {
3226 struct objfile *objfile = dwarf2_per_objfile->objfile;
3227 bfd *abfd = objfile->obfd;
3228 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3229 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
3230 SECT_OFF_TEXT (objfile));
3231
3232 auto_obstack temp_obstack;
3233 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
3234
3235 std::unordered_map<sect_offset,
3236 dwarf2_per_cu_data *,
3237 gdb::hash_enum<sect_offset>>
3238 debug_info_offset_to_per_cu;
3239 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3240 {
3241 const auto insertpair
3242 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
3243 if (!insertpair.second)
3244 {
3245 warning (_("Section .debug_aranges in %s has duplicate "
3246 "debug_info_offset %s, ignoring .debug_aranges."),
3247 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
3248 return;
3249 }
3250 }
3251
3252 dwarf2_read_section (objfile, section);
3253
3254 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
3255
3256 const gdb_byte *addr = section->buffer;
3257
3258 while (addr < section->buffer + section->size)
3259 {
3260 const gdb_byte *const entry_addr = addr;
3261 unsigned int bytes_read;
3262
3263 const LONGEST entry_length = read_initial_length (abfd, addr,
3264 &bytes_read);
3265 addr += bytes_read;
3266
3267 const gdb_byte *const entry_end = addr + entry_length;
3268 const bool dwarf5_is_dwarf64 = bytes_read != 4;
3269 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
3270 if (addr + entry_length > section->buffer + section->size)
3271 {
3272 warning (_("Section .debug_aranges in %s entry at offset %s "
3273 "length %s exceeds section length %s, "
3274 "ignoring .debug_aranges."),
3275 objfile_name (objfile),
3276 plongest (entry_addr - section->buffer),
3277 plongest (bytes_read + entry_length),
3278 pulongest (section->size));
3279 return;
3280 }
3281
3282 /* The version number. */
3283 const uint16_t version = read_2_bytes (abfd, addr);
3284 addr += 2;
3285 if (version != 2)
3286 {
3287 warning (_("Section .debug_aranges in %s entry at offset %s "
3288 "has unsupported version %d, ignoring .debug_aranges."),
3289 objfile_name (objfile),
3290 plongest (entry_addr - section->buffer), version);
3291 return;
3292 }
3293
3294 const uint64_t debug_info_offset
3295 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
3296 addr += offset_size;
3297 const auto per_cu_it
3298 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
3299 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
3300 {
3301 warning (_("Section .debug_aranges in %s entry at offset %s "
3302 "debug_info_offset %s does not exists, "
3303 "ignoring .debug_aranges."),
3304 objfile_name (objfile),
3305 plongest (entry_addr - section->buffer),
3306 pulongest (debug_info_offset));
3307 return;
3308 }
3309 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
3310
3311 const uint8_t address_size = *addr++;
3312 if (address_size < 1 || address_size > 8)
3313 {
3314 warning (_("Section .debug_aranges in %s entry at offset %s "
3315 "address_size %u is invalid, ignoring .debug_aranges."),
3316 objfile_name (objfile),
3317 plongest (entry_addr - section->buffer), address_size);
3318 return;
3319 }
3320
3321 const uint8_t segment_selector_size = *addr++;
3322 if (segment_selector_size != 0)
3323 {
3324 warning (_("Section .debug_aranges in %s entry at offset %s "
3325 "segment_selector_size %u is not supported, "
3326 "ignoring .debug_aranges."),
3327 objfile_name (objfile),
3328 plongest (entry_addr - section->buffer),
3329 segment_selector_size);
3330 return;
3331 }
3332
3333 /* Must pad to an alignment boundary that is twice the address
3334 size. It is undocumented by the DWARF standard but GCC does
3335 use it. */
3336 for (size_t padding = ((-(addr - section->buffer))
3337 & (2 * address_size - 1));
3338 padding > 0; padding--)
3339 if (*addr++ != 0)
3340 {
3341 warning (_("Section .debug_aranges in %s entry at offset %s "
3342 "padding is not zero, ignoring .debug_aranges."),
3343 objfile_name (objfile),
3344 plongest (entry_addr - section->buffer));
3345 return;
3346 }
3347
3348 for (;;)
3349 {
3350 if (addr + 2 * address_size > entry_end)
3351 {
3352 warning (_("Section .debug_aranges in %s entry at offset %s "
3353 "address list is not properly terminated, "
3354 "ignoring .debug_aranges."),
3355 objfile_name (objfile),
3356 plongest (entry_addr - section->buffer));
3357 return;
3358 }
3359 ULONGEST start = extract_unsigned_integer (addr, address_size,
3360 dwarf5_byte_order);
3361 addr += address_size;
3362 ULONGEST length = extract_unsigned_integer (addr, address_size,
3363 dwarf5_byte_order);
3364 addr += address_size;
3365 if (start == 0 && length == 0)
3366 break;
3367 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
3368 {
3369 /* Symbol was eliminated due to a COMDAT group. */
3370 continue;
3371 }
3372 ULONGEST end = start + length;
3373 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
3374 - baseaddr);
3375 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
3376 - baseaddr);
3377 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
3378 }
3379 }
3380
3381 objfile->partial_symtabs->psymtabs_addrmap
3382 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
3383 }
3384
3385 /* Find a slot in the mapped index INDEX for the object named NAME.
3386 If NAME is found, set *VEC_OUT to point to the CU vector in the
3387 constant pool and return true. If NAME cannot be found, return
3388 false. */
3389
3390 static bool
3391 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3392 offset_type **vec_out)
3393 {
3394 offset_type hash;
3395 offset_type slot, step;
3396 int (*cmp) (const char *, const char *);
3397
3398 gdb::unique_xmalloc_ptr<char> without_params;
3399 if (current_language->la_language == language_cplus
3400 || current_language->la_language == language_fortran
3401 || current_language->la_language == language_d)
3402 {
3403 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3404 not contain any. */
3405
3406 if (strchr (name, '(') != NULL)
3407 {
3408 without_params = cp_remove_params (name);
3409
3410 if (without_params != NULL)
3411 name = without_params.get ();
3412 }
3413 }
3414
3415 /* Index version 4 did not support case insensitive searches. But the
3416 indices for case insensitive languages are built in lowercase, therefore
3417 simulate our NAME being searched is also lowercased. */
3418 hash = mapped_index_string_hash ((index->version == 4
3419 && case_sensitivity == case_sensitive_off
3420 ? 5 : index->version),
3421 name);
3422
3423 slot = hash & (index->symbol_table.size () - 1);
3424 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
3425 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
3426
3427 for (;;)
3428 {
3429 const char *str;
3430
3431 const auto &bucket = index->symbol_table[slot];
3432 if (bucket.name == 0 && bucket.vec == 0)
3433 return false;
3434
3435 str = index->constant_pool + MAYBE_SWAP (bucket.name);
3436 if (!cmp (name, str))
3437 {
3438 *vec_out = (offset_type *) (index->constant_pool
3439 + MAYBE_SWAP (bucket.vec));
3440 return true;
3441 }
3442
3443 slot = (slot + step) & (index->symbol_table.size () - 1);
3444 }
3445 }
3446
3447 /* A helper function that reads the .gdb_index from BUFFER and fills
3448 in MAP. FILENAME is the name of the file containing the data;
3449 it is used for error reporting. DEPRECATED_OK is true if it is
3450 ok to use deprecated sections.
3451
3452 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3453 out parameters that are filled in with information about the CU and
3454 TU lists in the section.
3455
3456 Returns true if all went well, false otherwise. */
3457
3458 static bool
3459 read_gdb_index_from_buffer (struct objfile *objfile,
3460 const char *filename,
3461 bool deprecated_ok,
3462 gdb::array_view<const gdb_byte> buffer,
3463 struct mapped_index *map,
3464 const gdb_byte **cu_list,
3465 offset_type *cu_list_elements,
3466 const gdb_byte **types_list,
3467 offset_type *types_list_elements)
3468 {
3469 const gdb_byte *addr = &buffer[0];
3470
3471 /* Version check. */
3472 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
3473 /* Versions earlier than 3 emitted every copy of a psymbol. This
3474 causes the index to behave very poorly for certain requests. Version 3
3475 contained incomplete addrmap. So, it seems better to just ignore such
3476 indices. */
3477 if (version < 4)
3478 {
3479 static int warning_printed = 0;
3480 if (!warning_printed)
3481 {
3482 warning (_("Skipping obsolete .gdb_index section in %s."),
3483 filename);
3484 warning_printed = 1;
3485 }
3486 return 0;
3487 }
3488 /* Index version 4 uses a different hash function than index version
3489 5 and later.
3490
3491 Versions earlier than 6 did not emit psymbols for inlined
3492 functions. Using these files will cause GDB not to be able to
3493 set breakpoints on inlined functions by name, so we ignore these
3494 indices unless the user has done
3495 "set use-deprecated-index-sections on". */
3496 if (version < 6 && !deprecated_ok)
3497 {
3498 static int warning_printed = 0;
3499 if (!warning_printed)
3500 {
3501 warning (_("\
3502 Skipping deprecated .gdb_index section in %s.\n\
3503 Do \"set use-deprecated-index-sections on\" before the file is read\n\
3504 to use the section anyway."),
3505 filename);
3506 warning_printed = 1;
3507 }
3508 return 0;
3509 }
3510 /* Version 7 indices generated by gold refer to the CU for a symbol instead
3511 of the TU (for symbols coming from TUs),
3512 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3513 Plus gold-generated indices can have duplicate entries for global symbols,
3514 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3515 These are just performance bugs, and we can't distinguish gdb-generated
3516 indices from gold-generated ones, so issue no warning here. */
3517
3518 /* Indexes with higher version than the one supported by GDB may be no
3519 longer backward compatible. */
3520 if (version > 8)
3521 return 0;
3522
3523 map->version = version;
3524
3525 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
3526
3527 int i = 0;
3528 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3529 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3530 / 8);
3531 ++i;
3532
3533 *types_list = addr + MAYBE_SWAP (metadata[i]);
3534 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3535 - MAYBE_SWAP (metadata[i]))
3536 / 8);
3537 ++i;
3538
3539 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3540 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3541 map->address_table
3542 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
3543 ++i;
3544
3545 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3546 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3547 map->symbol_table
3548 = gdb::array_view<mapped_index::symbol_table_slot>
3549 ((mapped_index::symbol_table_slot *) symbol_table,
3550 (mapped_index::symbol_table_slot *) symbol_table_end);
3551
3552 ++i;
3553 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
3554
3555 return 1;
3556 }
3557
3558 /* Callback types for dwarf2_read_gdb_index. */
3559
3560 typedef gdb::function_view
3561 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_objfile *)>
3562 get_gdb_index_contents_ftype;
3563 typedef gdb::function_view
3564 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3565 get_gdb_index_contents_dwz_ftype;
3566
3567 /* Read .gdb_index. If everything went ok, initialize the "quick"
3568 elements of all the CUs and return 1. Otherwise, return 0. */
3569
3570 static int
3571 dwarf2_read_gdb_index
3572 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3573 get_gdb_index_contents_ftype get_gdb_index_contents,
3574 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
3575 {
3576 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3577 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3578 struct dwz_file *dwz;
3579 struct objfile *objfile = dwarf2_per_objfile->objfile;
3580
3581 gdb::array_view<const gdb_byte> main_index_contents
3582 = get_gdb_index_contents (objfile, dwarf2_per_objfile);
3583
3584 if (main_index_contents.empty ())
3585 return 0;
3586
3587 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3588 if (!read_gdb_index_from_buffer (objfile, objfile_name (objfile),
3589 use_deprecated_index_sections,
3590 main_index_contents, map.get (), &cu_list,
3591 &cu_list_elements, &types_list,
3592 &types_list_elements))
3593 return 0;
3594
3595 /* Don't use the index if it's empty. */
3596 if (map->symbol_table.empty ())
3597 return 0;
3598
3599 /* If there is a .dwz file, read it so we can get its CU list as
3600 well. */
3601 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3602 if (dwz != NULL)
3603 {
3604 struct mapped_index dwz_map;
3605 const gdb_byte *dwz_types_ignore;
3606 offset_type dwz_types_elements_ignore;
3607
3608 gdb::array_view<const gdb_byte> dwz_index_content
3609 = get_gdb_index_contents_dwz (objfile, dwz);
3610
3611 if (dwz_index_content.empty ())
3612 return 0;
3613
3614 if (!read_gdb_index_from_buffer (objfile,
3615 bfd_get_filename (dwz->dwz_bfd.get ()),
3616 1, dwz_index_content, &dwz_map,
3617 &dwz_list, &dwz_list_elements,
3618 &dwz_types_ignore,
3619 &dwz_types_elements_ignore))
3620 {
3621 warning (_("could not read '.gdb_index' section from %s; skipping"),
3622 bfd_get_filename (dwz->dwz_bfd.get ()));
3623 return 0;
3624 }
3625 }
3626
3627 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3628 dwz_list, dwz_list_elements);
3629
3630 if (types_list_elements)
3631 {
3632 /* We can only handle a single .debug_types when we have an
3633 index. */
3634 if (dwarf2_per_objfile->types.size () != 1)
3635 return 0;
3636
3637 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
3638
3639 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3640 types_list, types_list_elements);
3641 }
3642
3643 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
3644
3645 dwarf2_per_objfile->index_table = std::move (map);
3646 dwarf2_per_objfile->using_index = 1;
3647 dwarf2_per_objfile->quick_file_names_table =
3648 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
3649
3650 return 1;
3651 }
3652
3653 /* die_reader_func for dw2_get_file_names. */
3654
3655 static void
3656 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3657 const gdb_byte *info_ptr,
3658 struct die_info *comp_unit_die,
3659 int has_children,
3660 void *data)
3661 {
3662 struct dwarf2_cu *cu = reader->cu;
3663 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3664 struct dwarf2_per_objfile *dwarf2_per_objfile
3665 = cu->per_cu->dwarf2_per_objfile;
3666 struct objfile *objfile = dwarf2_per_objfile->objfile;
3667 struct dwarf2_per_cu_data *lh_cu;
3668 struct attribute *attr;
3669 void **slot;
3670 struct quick_file_names *qfn;
3671
3672 gdb_assert (! this_cu->is_debug_types);
3673
3674 /* Our callers never want to match partial units -- instead they
3675 will match the enclosing full CU. */
3676 if (comp_unit_die->tag == DW_TAG_partial_unit)
3677 {
3678 this_cu->v.quick->no_file_data = 1;
3679 return;
3680 }
3681
3682 lh_cu = this_cu;
3683 slot = NULL;
3684
3685 line_header_up lh;
3686 sect_offset line_offset {};
3687
3688 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3689 if (attr != nullptr)
3690 {
3691 struct quick_file_names find_entry;
3692
3693 line_offset = (sect_offset) DW_UNSND (attr);
3694
3695 /* We may have already read in this line header (TU line header sharing).
3696 If we have we're done. */
3697 find_entry.hash.dwo_unit = cu->dwo_unit;
3698 find_entry.hash.line_sect_off = line_offset;
3699 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3700 &find_entry, INSERT);
3701 if (*slot != NULL)
3702 {
3703 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3704 return;
3705 }
3706
3707 lh = dwarf_decode_line_header (line_offset, cu);
3708 }
3709 if (lh == NULL)
3710 {
3711 lh_cu->v.quick->no_file_data = 1;
3712 return;
3713 }
3714
3715 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
3716 qfn->hash.dwo_unit = cu->dwo_unit;
3717 qfn->hash.line_sect_off = line_offset;
3718 gdb_assert (slot != NULL);
3719 *slot = qfn;
3720
3721 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3722
3723 int offset = 0;
3724 if (strcmp (fnd.name, "<unknown>") != 0)
3725 ++offset;
3726
3727 qfn->num_file_names = offset + lh->file_names_size ();
3728 qfn->file_names =
3729 XOBNEWVEC (&objfile->objfile_obstack, const char *, qfn->num_file_names);
3730 if (offset != 0)
3731 qfn->file_names[0] = xstrdup (fnd.name);
3732 for (int i = 0; i < lh->file_names_size (); ++i)
3733 qfn->file_names[i + offset] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
3734 qfn->real_names = NULL;
3735
3736 lh_cu->v.quick->file_names = qfn;
3737 }
3738
3739 /* A helper for the "quick" functions which attempts to read the line
3740 table for THIS_CU. */
3741
3742 static struct quick_file_names *
3743 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
3744 {
3745 /* This should never be called for TUs. */
3746 gdb_assert (! this_cu->is_debug_types);
3747 /* Nor type unit groups. */
3748 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
3749
3750 if (this_cu->v.quick->file_names != NULL)
3751 return this_cu->v.quick->file_names;
3752 /* If we know there is no line data, no point in looking again. */
3753 if (this_cu->v.quick->no_file_data)
3754 return NULL;
3755
3756 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
3757
3758 if (this_cu->v.quick->no_file_data)
3759 return NULL;
3760 return this_cu->v.quick->file_names;
3761 }
3762
3763 /* A helper for the "quick" functions which computes and caches the
3764 real path for a given file name from the line table. */
3765
3766 static const char *
3767 dw2_get_real_path (struct objfile *objfile,
3768 struct quick_file_names *qfn, int index)
3769 {
3770 if (qfn->real_names == NULL)
3771 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
3772 qfn->num_file_names, const char *);
3773
3774 if (qfn->real_names[index] == NULL)
3775 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3776
3777 return qfn->real_names[index];
3778 }
3779
3780 static struct symtab *
3781 dw2_find_last_source_symtab (struct objfile *objfile)
3782 {
3783 struct dwarf2_per_objfile *dwarf2_per_objfile
3784 = get_dwarf2_per_objfile (objfile);
3785 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->all_comp_units.back ();
3786 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, false);
3787
3788 if (cust == NULL)
3789 return NULL;
3790
3791 return compunit_primary_filetab (cust);
3792 }
3793
3794 /* Traversal function for dw2_forget_cached_source_info. */
3795
3796 static int
3797 dw2_free_cached_file_names (void **slot, void *info)
3798 {
3799 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3800
3801 if (file_data->real_names)
3802 {
3803 int i;
3804
3805 for (i = 0; i < file_data->num_file_names; ++i)
3806 {
3807 xfree ((void*) file_data->real_names[i]);
3808 file_data->real_names[i] = NULL;
3809 }
3810 }
3811
3812 return 1;
3813 }
3814
3815 static void
3816 dw2_forget_cached_source_info (struct objfile *objfile)
3817 {
3818 struct dwarf2_per_objfile *dwarf2_per_objfile
3819 = get_dwarf2_per_objfile (objfile);
3820
3821 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3822 dw2_free_cached_file_names, NULL);
3823 }
3824
3825 /* Helper function for dw2_map_symtabs_matching_filename that expands
3826 the symtabs and calls the iterator. */
3827
3828 static int
3829 dw2_map_expand_apply (struct objfile *objfile,
3830 struct dwarf2_per_cu_data *per_cu,
3831 const char *name, const char *real_path,
3832 gdb::function_view<bool (symtab *)> callback)
3833 {
3834 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3835
3836 /* Don't visit already-expanded CUs. */
3837 if (per_cu->v.quick->compunit_symtab)
3838 return 0;
3839
3840 /* This may expand more than one symtab, and we want to iterate over
3841 all of them. */
3842 dw2_instantiate_symtab (per_cu, false);
3843
3844 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3845 last_made, callback);
3846 }
3847
3848 /* Implementation of the map_symtabs_matching_filename method. */
3849
3850 static bool
3851 dw2_map_symtabs_matching_filename
3852 (struct objfile *objfile, const char *name, const char *real_path,
3853 gdb::function_view<bool (symtab *)> callback)
3854 {
3855 const char *name_basename = lbasename (name);
3856 struct dwarf2_per_objfile *dwarf2_per_objfile
3857 = get_dwarf2_per_objfile (objfile);
3858
3859 /* The rule is CUs specify all the files, including those used by
3860 any TU, so there's no need to scan TUs here. */
3861
3862 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3863 {
3864 /* We only need to look at symtabs not already expanded. */
3865 if (per_cu->v.quick->compunit_symtab)
3866 continue;
3867
3868 quick_file_names *file_data = dw2_get_file_names (per_cu);
3869 if (file_data == NULL)
3870 continue;
3871
3872 for (int j = 0; j < file_data->num_file_names; ++j)
3873 {
3874 const char *this_name = file_data->file_names[j];
3875 const char *this_real_name;
3876
3877 if (compare_filenames_for_search (this_name, name))
3878 {
3879 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3880 callback))
3881 return true;
3882 continue;
3883 }
3884
3885 /* Before we invoke realpath, which can get expensive when many
3886 files are involved, do a quick comparison of the basenames. */
3887 if (! basenames_may_differ
3888 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3889 continue;
3890
3891 this_real_name = dw2_get_real_path (objfile, file_data, j);
3892 if (compare_filenames_for_search (this_real_name, name))
3893 {
3894 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3895 callback))
3896 return true;
3897 continue;
3898 }
3899
3900 if (real_path != NULL)
3901 {
3902 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3903 gdb_assert (IS_ABSOLUTE_PATH (name));
3904 if (this_real_name != NULL
3905 && FILENAME_CMP (real_path, this_real_name) == 0)
3906 {
3907 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3908 callback))
3909 return true;
3910 continue;
3911 }
3912 }
3913 }
3914 }
3915
3916 return false;
3917 }
3918
3919 /* Struct used to manage iterating over all CUs looking for a symbol. */
3920
3921 struct dw2_symtab_iterator
3922 {
3923 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3924 struct dwarf2_per_objfile *dwarf2_per_objfile;
3925 /* If set, only look for symbols that match that block. Valid values are
3926 GLOBAL_BLOCK and STATIC_BLOCK. */
3927 gdb::optional<block_enum> block_index;
3928 /* The kind of symbol we're looking for. */
3929 domain_enum domain;
3930 /* The list of CUs from the index entry of the symbol,
3931 or NULL if not found. */
3932 offset_type *vec;
3933 /* The next element in VEC to look at. */
3934 int next;
3935 /* The number of elements in VEC, or zero if there is no match. */
3936 int length;
3937 /* Have we seen a global version of the symbol?
3938 If so we can ignore all further global instances.
3939 This is to work around gold/15646, inefficient gold-generated
3940 indices. */
3941 int global_seen;
3942 };
3943
3944 /* Initialize the index symtab iterator ITER. */
3945
3946 static void
3947 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3948 struct dwarf2_per_objfile *dwarf2_per_objfile,
3949 gdb::optional<block_enum> block_index,
3950 domain_enum domain,
3951 const char *name)
3952 {
3953 iter->dwarf2_per_objfile = dwarf2_per_objfile;
3954 iter->block_index = block_index;
3955 iter->domain = domain;
3956 iter->next = 0;
3957 iter->global_seen = 0;
3958
3959 mapped_index *index = dwarf2_per_objfile->index_table.get ();
3960
3961 /* index is NULL if OBJF_READNOW. */
3962 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
3963 iter->length = MAYBE_SWAP (*iter->vec);
3964 else
3965 {
3966 iter->vec = NULL;
3967 iter->length = 0;
3968 }
3969 }
3970
3971 /* Return the next matching CU or NULL if there are no more. */
3972
3973 static struct dwarf2_per_cu_data *
3974 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3975 {
3976 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3977
3978 for ( ; iter->next < iter->length; ++iter->next)
3979 {
3980 offset_type cu_index_and_attrs =
3981 MAYBE_SWAP (iter->vec[iter->next + 1]);
3982 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3983 gdb_index_symbol_kind symbol_kind =
3984 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3985 /* Only check the symbol attributes if they're present.
3986 Indices prior to version 7 don't record them,
3987 and indices >= 7 may elide them for certain symbols
3988 (gold does this). */
3989 int attrs_valid =
3990 (dwarf2_per_objfile->index_table->version >= 7
3991 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3992
3993 /* Don't crash on bad data. */
3994 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
3995 + dwarf2_per_objfile->all_type_units.size ()))
3996 {
3997 complaint (_(".gdb_index entry has bad CU index"
3998 " [in module %s]"),
3999 objfile_name (dwarf2_per_objfile->objfile));
4000 continue;
4001 }
4002
4003 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
4004
4005 /* Skip if already read in. */
4006 if (per_cu->v.quick->compunit_symtab)
4007 continue;
4008
4009 /* Check static vs global. */
4010 if (attrs_valid)
4011 {
4012 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4013
4014 if (iter->block_index.has_value ())
4015 {
4016 bool want_static = *iter->block_index == STATIC_BLOCK;
4017
4018 if (is_static != want_static)
4019 continue;
4020 }
4021
4022 /* Work around gold/15646. */
4023 if (!is_static && iter->global_seen)
4024 continue;
4025 if (!is_static)
4026 iter->global_seen = 1;
4027 }
4028
4029 /* Only check the symbol's kind if it has one. */
4030 if (attrs_valid)
4031 {
4032 switch (iter->domain)
4033 {
4034 case VAR_DOMAIN:
4035 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
4036 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
4037 /* Some types are also in VAR_DOMAIN. */
4038 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4039 continue;
4040 break;
4041 case STRUCT_DOMAIN:
4042 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4043 continue;
4044 break;
4045 case LABEL_DOMAIN:
4046 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4047 continue;
4048 break;
4049 case MODULE_DOMAIN:
4050 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4051 continue;
4052 break;
4053 default:
4054 break;
4055 }
4056 }
4057
4058 ++iter->next;
4059 return per_cu;
4060 }
4061
4062 return NULL;
4063 }
4064
4065 static struct compunit_symtab *
4066 dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
4067 const char *name, domain_enum domain)
4068 {
4069 struct compunit_symtab *stab_best = NULL;
4070 struct dwarf2_per_objfile *dwarf2_per_objfile
4071 = get_dwarf2_per_objfile (objfile);
4072
4073 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
4074
4075 struct dw2_symtab_iterator iter;
4076 struct dwarf2_per_cu_data *per_cu;
4077
4078 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
4079
4080 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4081 {
4082 struct symbol *sym, *with_opaque = NULL;
4083 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
4084 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
4085 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
4086
4087 sym = block_find_symbol (block, name, domain,
4088 block_find_non_opaque_type_preferred,
4089 &with_opaque);
4090
4091 /* Some caution must be observed with overloaded functions
4092 and methods, since the index will not contain any overload
4093 information (but NAME might contain it). */
4094
4095 if (sym != NULL
4096 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
4097 return stab;
4098 if (with_opaque != NULL
4099 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
4100 stab_best = stab;
4101
4102 /* Keep looking through other CUs. */
4103 }
4104
4105 return stab_best;
4106 }
4107
4108 static void
4109 dw2_print_stats (struct objfile *objfile)
4110 {
4111 struct dwarf2_per_objfile *dwarf2_per_objfile
4112 = get_dwarf2_per_objfile (objfile);
4113 int total = (dwarf2_per_objfile->all_comp_units.size ()
4114 + dwarf2_per_objfile->all_type_units.size ());
4115 int count = 0;
4116
4117 for (int i = 0; i < total; ++i)
4118 {
4119 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
4120
4121 if (!per_cu->v.quick->compunit_symtab)
4122 ++count;
4123 }
4124 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
4125 printf_filtered (_(" Number of unread CUs: %d\n"), count);
4126 }
4127
4128 /* This dumps minimal information about the index.
4129 It is called via "mt print objfiles".
4130 One use is to verify .gdb_index has been loaded by the
4131 gdb.dwarf2/gdb-index.exp testcase. */
4132
4133 static void
4134 dw2_dump (struct objfile *objfile)
4135 {
4136 struct dwarf2_per_objfile *dwarf2_per_objfile
4137 = get_dwarf2_per_objfile (objfile);
4138
4139 gdb_assert (dwarf2_per_objfile->using_index);
4140 printf_filtered (".gdb_index:");
4141 if (dwarf2_per_objfile->index_table != NULL)
4142 {
4143 printf_filtered (" version %d\n",
4144 dwarf2_per_objfile->index_table->version);
4145 }
4146 else
4147 printf_filtered (" faked for \"readnow\"\n");
4148 printf_filtered ("\n");
4149 }
4150
4151 static void
4152 dw2_expand_symtabs_for_function (struct objfile *objfile,
4153 const char *func_name)
4154 {
4155 struct dwarf2_per_objfile *dwarf2_per_objfile
4156 = get_dwarf2_per_objfile (objfile);
4157
4158 struct dw2_symtab_iterator iter;
4159 struct dwarf2_per_cu_data *per_cu;
4160
4161 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
4162
4163 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4164 dw2_instantiate_symtab (per_cu, false);
4165
4166 }
4167
4168 static void
4169 dw2_expand_all_symtabs (struct objfile *objfile)
4170 {
4171 struct dwarf2_per_objfile *dwarf2_per_objfile
4172 = get_dwarf2_per_objfile (objfile);
4173 int total_units = (dwarf2_per_objfile->all_comp_units.size ()
4174 + dwarf2_per_objfile->all_type_units.size ());
4175
4176 for (int i = 0; i < total_units; ++i)
4177 {
4178 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
4179
4180 /* We don't want to directly expand a partial CU, because if we
4181 read it with the wrong language, then assertion failures can
4182 be triggered later on. See PR symtab/23010. So, tell
4183 dw2_instantiate_symtab to skip partial CUs -- any important
4184 partial CU will be read via DW_TAG_imported_unit anyway. */
4185 dw2_instantiate_symtab (per_cu, true);
4186 }
4187 }
4188
4189 static void
4190 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
4191 const char *fullname)
4192 {
4193 struct dwarf2_per_objfile *dwarf2_per_objfile
4194 = get_dwarf2_per_objfile (objfile);
4195
4196 /* We don't need to consider type units here.
4197 This is only called for examining code, e.g. expand_line_sal.
4198 There can be an order of magnitude (or more) more type units
4199 than comp units, and we avoid them if we can. */
4200
4201 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4202 {
4203 /* We only need to look at symtabs not already expanded. */
4204 if (per_cu->v.quick->compunit_symtab)
4205 continue;
4206
4207 quick_file_names *file_data = dw2_get_file_names (per_cu);
4208 if (file_data == NULL)
4209 continue;
4210
4211 for (int j = 0; j < file_data->num_file_names; ++j)
4212 {
4213 const char *this_fullname = file_data->file_names[j];
4214
4215 if (filename_cmp (this_fullname, fullname) == 0)
4216 {
4217 dw2_instantiate_symtab (per_cu, false);
4218 break;
4219 }
4220 }
4221 }
4222 }
4223
4224 static void
4225 dw2_map_matching_symbols
4226 (struct objfile *objfile,
4227 const lookup_name_info &name, domain_enum domain,
4228 int global,
4229 gdb::function_view<symbol_found_callback_ftype> callback,
4230 symbol_compare_ftype *ordered_compare)
4231 {
4232 /* Currently unimplemented; used for Ada. The function can be called if the
4233 current language is Ada for a non-Ada objfile using GNU index. As Ada
4234 does not look for non-Ada symbols this function should just return. */
4235 }
4236
4237 /* Starting from a search name, return the string that finds the upper
4238 bound of all strings that start with SEARCH_NAME in a sorted name
4239 list. Returns the empty string to indicate that the upper bound is
4240 the end of the list. */
4241
4242 static std::string
4243 make_sort_after_prefix_name (const char *search_name)
4244 {
4245 /* When looking to complete "func", we find the upper bound of all
4246 symbols that start with "func" by looking for where we'd insert
4247 the closest string that would follow "func" in lexicographical
4248 order. Usually, that's "func"-with-last-character-incremented,
4249 i.e. "fund". Mind non-ASCII characters, though. Usually those
4250 will be UTF-8 multi-byte sequences, but we can't be certain.
4251 Especially mind the 0xff character, which is a valid character in
4252 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
4253 rule out compilers allowing it in identifiers. Note that
4254 conveniently, strcmp/strcasecmp are specified to compare
4255 characters interpreted as unsigned char. So what we do is treat
4256 the whole string as a base 256 number composed of a sequence of
4257 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
4258 to 0, and carries 1 to the following more-significant position.
4259 If the very first character in SEARCH_NAME ends up incremented
4260 and carries/overflows, then the upper bound is the end of the
4261 list. The string after the empty string is also the empty
4262 string.
4263
4264 Some examples of this operation:
4265
4266 SEARCH_NAME => "+1" RESULT
4267
4268 "abc" => "abd"
4269 "ab\xff" => "ac"
4270 "\xff" "a" "\xff" => "\xff" "b"
4271 "\xff" => ""
4272 "\xff\xff" => ""
4273 "" => ""
4274
4275 Then, with these symbols for example:
4276
4277 func
4278 func1
4279 fund
4280
4281 completing "func" looks for symbols between "func" and
4282 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
4283 which finds "func" and "func1", but not "fund".
4284
4285 And with:
4286
4287 funcÿ (Latin1 'ÿ' [0xff])
4288 funcÿ1
4289 fund
4290
4291 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
4292 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
4293
4294 And with:
4295
4296 ÿÿ (Latin1 'ÿ' [0xff])
4297 ÿÿ1
4298
4299 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
4300 the end of the list.
4301 */
4302 std::string after = search_name;
4303 while (!after.empty () && (unsigned char) after.back () == 0xff)
4304 after.pop_back ();
4305 if (!after.empty ())
4306 after.back () = (unsigned char) after.back () + 1;
4307 return after;
4308 }
4309
4310 /* See declaration. */
4311
4312 std::pair<std::vector<name_component>::const_iterator,
4313 std::vector<name_component>::const_iterator>
4314 mapped_index_base::find_name_components_bounds
4315 (const lookup_name_info &lookup_name_without_params, language lang) const
4316 {
4317 auto *name_cmp
4318 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4319
4320 const char *lang_name
4321 = lookup_name_without_params.language_lookup_name (lang).c_str ();
4322
4323 /* Comparison function object for lower_bound that matches against a
4324 given symbol name. */
4325 auto lookup_compare_lower = [&] (const name_component &elem,
4326 const char *name)
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 (elem_name, name) < 0;
4331 };
4332
4333 /* Comparison function object for upper_bound that matches against a
4334 given symbol name. */
4335 auto lookup_compare_upper = [&] (const char *name,
4336 const name_component &elem)
4337 {
4338 const char *elem_qualified = this->symbol_name_at (elem.idx);
4339 const char *elem_name = elem_qualified + elem.name_offset;
4340 return name_cmp (name, elem_name) < 0;
4341 };
4342
4343 auto begin = this->name_components.begin ();
4344 auto end = this->name_components.end ();
4345
4346 /* Find the lower bound. */
4347 auto lower = [&] ()
4348 {
4349 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
4350 return begin;
4351 else
4352 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
4353 } ();
4354
4355 /* Find the upper bound. */
4356 auto upper = [&] ()
4357 {
4358 if (lookup_name_without_params.completion_mode ())
4359 {
4360 /* In completion mode, we want UPPER to point past all
4361 symbols names that have the same prefix. I.e., with
4362 these symbols, and completing "func":
4363
4364 function << lower bound
4365 function1
4366 other_function << upper bound
4367
4368 We find the upper bound by looking for the insertion
4369 point of "func"-with-last-character-incremented,
4370 i.e. "fund". */
4371 std::string after = make_sort_after_prefix_name (lang_name);
4372 if (after.empty ())
4373 return end;
4374 return std::lower_bound (lower, end, after.c_str (),
4375 lookup_compare_lower);
4376 }
4377 else
4378 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
4379 } ();
4380
4381 return {lower, upper};
4382 }
4383
4384 /* See declaration. */
4385
4386 void
4387 mapped_index_base::build_name_components ()
4388 {
4389 if (!this->name_components.empty ())
4390 return;
4391
4392 this->name_components_casing = case_sensitivity;
4393 auto *name_cmp
4394 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4395
4396 /* The code below only knows how to break apart components of C++
4397 symbol names (and other languages that use '::' as
4398 namespace/module separator) and Ada symbol names. */
4399 auto count = this->symbol_name_count ();
4400 for (offset_type idx = 0; idx < count; idx++)
4401 {
4402 if (this->symbol_name_slot_invalid (idx))
4403 continue;
4404
4405 const char *name = this->symbol_name_at (idx);
4406
4407 /* Add each name component to the name component table. */
4408 unsigned int previous_len = 0;
4409
4410 if (strstr (name, "::") != nullptr)
4411 {
4412 for (unsigned int current_len = cp_find_first_component (name);
4413 name[current_len] != '\0';
4414 current_len += cp_find_first_component (name + current_len))
4415 {
4416 gdb_assert (name[current_len] == ':');
4417 this->name_components.push_back ({previous_len, idx});
4418 /* Skip the '::'. */
4419 current_len += 2;
4420 previous_len = current_len;
4421 }
4422 }
4423 else
4424 {
4425 /* Handle the Ada encoded (aka mangled) form here. */
4426 for (const char *iter = strstr (name, "__");
4427 iter != nullptr;
4428 iter = strstr (iter, "__"))
4429 {
4430 this->name_components.push_back ({previous_len, idx});
4431 iter += 2;
4432 previous_len = iter - name;
4433 }
4434 }
4435
4436 this->name_components.push_back ({previous_len, idx});
4437 }
4438
4439 /* Sort name_components elements by name. */
4440 auto name_comp_compare = [&] (const name_component &left,
4441 const name_component &right)
4442 {
4443 const char *left_qualified = this->symbol_name_at (left.idx);
4444 const char *right_qualified = this->symbol_name_at (right.idx);
4445
4446 const char *left_name = left_qualified + left.name_offset;
4447 const char *right_name = right_qualified + right.name_offset;
4448
4449 return name_cmp (left_name, right_name) < 0;
4450 };
4451
4452 std::sort (this->name_components.begin (),
4453 this->name_components.end (),
4454 name_comp_compare);
4455 }
4456
4457 /* Helper for dw2_expand_symtabs_matching that works with a
4458 mapped_index_base instead of the containing objfile. This is split
4459 to a separate function in order to be able to unit test the
4460 name_components matching using a mock mapped_index_base. For each
4461 symbol name that matches, calls MATCH_CALLBACK, passing it the
4462 symbol's index in the mapped_index_base symbol table. */
4463
4464 static void
4465 dw2_expand_symtabs_matching_symbol
4466 (mapped_index_base &index,
4467 const lookup_name_info &lookup_name_in,
4468 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4469 enum search_domain kind,
4470 gdb::function_view<bool (offset_type)> match_callback)
4471 {
4472 lookup_name_info lookup_name_without_params
4473 = lookup_name_in.make_ignore_params ();
4474
4475 /* Build the symbol name component sorted vector, if we haven't
4476 yet. */
4477 index.build_name_components ();
4478
4479 /* The same symbol may appear more than once in the range though.
4480 E.g., if we're looking for symbols that complete "w", and we have
4481 a symbol named "w1::w2", we'll find the two name components for
4482 that same symbol in the range. To be sure we only call the
4483 callback once per symbol, we first collect the symbol name
4484 indexes that matched in a temporary vector and ignore
4485 duplicates. */
4486 std::vector<offset_type> matches;
4487
4488 struct name_and_matcher
4489 {
4490 symbol_name_matcher_ftype *matcher;
4491 const std::string &name;
4492
4493 bool operator== (const name_and_matcher &other) const
4494 {
4495 return matcher == other.matcher && name == other.name;
4496 }
4497 };
4498
4499 /* A vector holding all the different symbol name matchers, for all
4500 languages. */
4501 std::vector<name_and_matcher> matchers;
4502
4503 for (int i = 0; i < nr_languages; i++)
4504 {
4505 enum language lang_e = (enum language) i;
4506
4507 const language_defn *lang = language_def (lang_e);
4508 symbol_name_matcher_ftype *name_matcher
4509 = get_symbol_name_matcher (lang, lookup_name_without_params);
4510
4511 name_and_matcher key {
4512 name_matcher,
4513 lookup_name_without_params.language_lookup_name (lang_e)
4514 };
4515
4516 /* Don't insert the same comparison routine more than once.
4517 Note that we do this linear walk. This is not a problem in
4518 practice because the number of supported languages is
4519 low. */
4520 if (std::find (matchers.begin (), matchers.end (), key)
4521 != matchers.end ())
4522 continue;
4523 matchers.push_back (std::move (key));
4524
4525 auto bounds
4526 = index.find_name_components_bounds (lookup_name_without_params,
4527 lang_e);
4528
4529 /* Now for each symbol name in range, check to see if we have a name
4530 match, and if so, call the MATCH_CALLBACK callback. */
4531
4532 for (; bounds.first != bounds.second; ++bounds.first)
4533 {
4534 const char *qualified = index.symbol_name_at (bounds.first->idx);
4535
4536 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4537 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4538 continue;
4539
4540 matches.push_back (bounds.first->idx);
4541 }
4542 }
4543
4544 std::sort (matches.begin (), matches.end ());
4545
4546 /* Finally call the callback, once per match. */
4547 ULONGEST prev = -1;
4548 for (offset_type idx : matches)
4549 {
4550 if (prev != idx)
4551 {
4552 if (!match_callback (idx))
4553 break;
4554 prev = idx;
4555 }
4556 }
4557
4558 /* Above we use a type wider than idx's for 'prev', since 0 and
4559 (offset_type)-1 are both possible values. */
4560 static_assert (sizeof (prev) > sizeof (offset_type), "");
4561 }
4562
4563 #if GDB_SELF_TEST
4564
4565 namespace selftests { namespace dw2_expand_symtabs_matching {
4566
4567 /* A mock .gdb_index/.debug_names-like name index table, enough to
4568 exercise dw2_expand_symtabs_matching_symbol, which works with the
4569 mapped_index_base interface. Builds an index from the symbol list
4570 passed as parameter to the constructor. */
4571 class mock_mapped_index : public mapped_index_base
4572 {
4573 public:
4574 mock_mapped_index (gdb::array_view<const char *> symbols)
4575 : m_symbol_table (symbols)
4576 {}
4577
4578 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
4579
4580 /* Return the number of names in the symbol table. */
4581 size_t symbol_name_count () const override
4582 {
4583 return m_symbol_table.size ();
4584 }
4585
4586 /* Get the name of the symbol at IDX in the symbol table. */
4587 const char *symbol_name_at (offset_type idx) const override
4588 {
4589 return m_symbol_table[idx];
4590 }
4591
4592 private:
4593 gdb::array_view<const char *> m_symbol_table;
4594 };
4595
4596 /* Convenience function that converts a NULL pointer to a "<null>"
4597 string, to pass to print routines. */
4598
4599 static const char *
4600 string_or_null (const char *str)
4601 {
4602 return str != NULL ? str : "<null>";
4603 }
4604
4605 /* Check if a lookup_name_info built from
4606 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4607 index. EXPECTED_LIST is the list of expected matches, in expected
4608 matching order. If no match expected, then an empty list is
4609 specified. Returns true on success. On failure prints a warning
4610 indicating the file:line that failed, and returns false. */
4611
4612 static bool
4613 check_match (const char *file, int line,
4614 mock_mapped_index &mock_index,
4615 const char *name, symbol_name_match_type match_type,
4616 bool completion_mode,
4617 std::initializer_list<const char *> expected_list)
4618 {
4619 lookup_name_info lookup_name (name, match_type, completion_mode);
4620
4621 bool matched = true;
4622
4623 auto mismatch = [&] (const char *expected_str,
4624 const char *got)
4625 {
4626 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4627 "expected=\"%s\", got=\"%s\"\n"),
4628 file, line,
4629 (match_type == symbol_name_match_type::FULL
4630 ? "FULL" : "WILD"),
4631 name, string_or_null (expected_str), string_or_null (got));
4632 matched = false;
4633 };
4634
4635 auto expected_it = expected_list.begin ();
4636 auto expected_end = expected_list.end ();
4637
4638 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4639 NULL, ALL_DOMAIN,
4640 [&] (offset_type idx)
4641 {
4642 const char *matched_name = mock_index.symbol_name_at (idx);
4643 const char *expected_str
4644 = expected_it == expected_end ? NULL : *expected_it++;
4645
4646 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4647 mismatch (expected_str, matched_name);
4648 return true;
4649 });
4650
4651 const char *expected_str
4652 = expected_it == expected_end ? NULL : *expected_it++;
4653 if (expected_str != NULL)
4654 mismatch (expected_str, NULL);
4655
4656 return matched;
4657 }
4658
4659 /* The symbols added to the mock mapped_index for testing (in
4660 canonical form). */
4661 static const char *test_symbols[] = {
4662 "function",
4663 "std::bar",
4664 "std::zfunction",
4665 "std::zfunction2",
4666 "w1::w2",
4667 "ns::foo<char*>",
4668 "ns::foo<int>",
4669 "ns::foo<long>",
4670 "ns2::tmpl<int>::foo2",
4671 "(anonymous namespace)::A::B::C",
4672
4673 /* These are used to check that the increment-last-char in the
4674 matching algorithm for completion doesn't match "t1_fund" when
4675 completing "t1_func". */
4676 "t1_func",
4677 "t1_func1",
4678 "t1_fund",
4679 "t1_fund1",
4680
4681 /* A UTF-8 name with multi-byte sequences to make sure that
4682 cp-name-parser understands this as a single identifier ("função"
4683 is "function" in PT). */
4684 u8"u8função",
4685
4686 /* \377 (0xff) is Latin1 'ÿ'. */
4687 "yfunc\377",
4688
4689 /* \377 (0xff) is Latin1 'ÿ'. */
4690 "\377",
4691 "\377\377123",
4692
4693 /* A name with all sorts of complications. Starts with "z" to make
4694 it easier for the completion tests below. */
4695 #define Z_SYM_NAME \
4696 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4697 "::tuple<(anonymous namespace)::ui*, " \
4698 "std::default_delete<(anonymous namespace)::ui>, void>"
4699
4700 Z_SYM_NAME
4701 };
4702
4703 /* Returns true if the mapped_index_base::find_name_component_bounds
4704 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4705 in completion mode. */
4706
4707 static bool
4708 check_find_bounds_finds (mapped_index_base &index,
4709 const char *search_name,
4710 gdb::array_view<const char *> expected_syms)
4711 {
4712 lookup_name_info lookup_name (search_name,
4713 symbol_name_match_type::FULL, true);
4714
4715 auto bounds = index.find_name_components_bounds (lookup_name,
4716 language_cplus);
4717
4718 size_t distance = std::distance (bounds.first, bounds.second);
4719 if (distance != expected_syms.size ())
4720 return false;
4721
4722 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4723 {
4724 auto nc_elem = bounds.first + exp_elem;
4725 const char *qualified = index.symbol_name_at (nc_elem->idx);
4726 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4727 return false;
4728 }
4729
4730 return true;
4731 }
4732
4733 /* Test the lower-level mapped_index::find_name_component_bounds
4734 method. */
4735
4736 static void
4737 test_mapped_index_find_name_component_bounds ()
4738 {
4739 mock_mapped_index mock_index (test_symbols);
4740
4741 mock_index.build_name_components ();
4742
4743 /* Test the lower-level mapped_index::find_name_component_bounds
4744 method in completion mode. */
4745 {
4746 static const char *expected_syms[] = {
4747 "t1_func",
4748 "t1_func1",
4749 };
4750
4751 SELF_CHECK (check_find_bounds_finds (mock_index,
4752 "t1_func", expected_syms));
4753 }
4754
4755 /* Check that the increment-last-char in the name matching algorithm
4756 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4757 {
4758 static const char *expected_syms1[] = {
4759 "\377",
4760 "\377\377123",
4761 };
4762 SELF_CHECK (check_find_bounds_finds (mock_index,
4763 "\377", expected_syms1));
4764
4765 static const char *expected_syms2[] = {
4766 "\377\377123",
4767 };
4768 SELF_CHECK (check_find_bounds_finds (mock_index,
4769 "\377\377", expected_syms2));
4770 }
4771 }
4772
4773 /* Test dw2_expand_symtabs_matching_symbol. */
4774
4775 static void
4776 test_dw2_expand_symtabs_matching_symbol ()
4777 {
4778 mock_mapped_index mock_index (test_symbols);
4779
4780 /* We let all tests run until the end even if some fails, for debug
4781 convenience. */
4782 bool any_mismatch = false;
4783
4784 /* Create the expected symbols list (an initializer_list). Needed
4785 because lists have commas, and we need to pass them to CHECK,
4786 which is a macro. */
4787 #define EXPECT(...) { __VA_ARGS__ }
4788
4789 /* Wrapper for check_match that passes down the current
4790 __FILE__/__LINE__. */
4791 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4792 any_mismatch |= !check_match (__FILE__, __LINE__, \
4793 mock_index, \
4794 NAME, MATCH_TYPE, COMPLETION_MODE, \
4795 EXPECTED_LIST)
4796
4797 /* Identity checks. */
4798 for (const char *sym : test_symbols)
4799 {
4800 /* Should be able to match all existing symbols. */
4801 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4802 EXPECT (sym));
4803
4804 /* Should be able to match all existing symbols with
4805 parameters. */
4806 std::string with_params = std::string (sym) + "(int)";
4807 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4808 EXPECT (sym));
4809
4810 /* Should be able to match all existing symbols with
4811 parameters and qualifiers. */
4812 with_params = std::string (sym) + " ( int ) const";
4813 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4814 EXPECT (sym));
4815
4816 /* This should really find sym, but cp-name-parser.y doesn't
4817 know about lvalue/rvalue qualifiers yet. */
4818 with_params = std::string (sym) + " ( int ) &&";
4819 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4820 {});
4821 }
4822
4823 /* Check that the name matching algorithm for completion doesn't get
4824 confused with Latin1 'ÿ' / 0xff. */
4825 {
4826 static const char str[] = "\377";
4827 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4828 EXPECT ("\377", "\377\377123"));
4829 }
4830
4831 /* Check that the increment-last-char in the matching algorithm for
4832 completion doesn't match "t1_fund" when completing "t1_func". */
4833 {
4834 static const char str[] = "t1_func";
4835 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4836 EXPECT ("t1_func", "t1_func1"));
4837 }
4838
4839 /* Check that completion mode works at each prefix of the expected
4840 symbol name. */
4841 {
4842 static const char str[] = "function(int)";
4843 size_t len = strlen (str);
4844 std::string lookup;
4845
4846 for (size_t i = 1; i < len; i++)
4847 {
4848 lookup.assign (str, i);
4849 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4850 EXPECT ("function"));
4851 }
4852 }
4853
4854 /* While "w" is a prefix of both components, the match function
4855 should still only be called once. */
4856 {
4857 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4858 EXPECT ("w1::w2"));
4859 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4860 EXPECT ("w1::w2"));
4861 }
4862
4863 /* Same, with a "complicated" symbol. */
4864 {
4865 static const char str[] = Z_SYM_NAME;
4866 size_t len = strlen (str);
4867 std::string lookup;
4868
4869 for (size_t i = 1; i < len; i++)
4870 {
4871 lookup.assign (str, i);
4872 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4873 EXPECT (Z_SYM_NAME));
4874 }
4875 }
4876
4877 /* In FULL mode, an incomplete symbol doesn't match. */
4878 {
4879 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4880 {});
4881 }
4882
4883 /* A complete symbol with parameters matches any overload, since the
4884 index has no overload info. */
4885 {
4886 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4887 EXPECT ("std::zfunction", "std::zfunction2"));
4888 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4889 EXPECT ("std::zfunction", "std::zfunction2"));
4890 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4891 EXPECT ("std::zfunction", "std::zfunction2"));
4892 }
4893
4894 /* Check that whitespace is ignored appropriately. A symbol with a
4895 template argument list. */
4896 {
4897 static const char expected[] = "ns::foo<int>";
4898 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4899 EXPECT (expected));
4900 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4901 EXPECT (expected));
4902 }
4903
4904 /* Check that whitespace is ignored appropriately. A symbol with a
4905 template argument list that includes a pointer. */
4906 {
4907 static const char expected[] = "ns::foo<char*>";
4908 /* Try both completion and non-completion modes. */
4909 static const bool completion_mode[2] = {false, true};
4910 for (size_t i = 0; i < 2; i++)
4911 {
4912 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4913 completion_mode[i], EXPECT (expected));
4914 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4915 completion_mode[i], EXPECT (expected));
4916
4917 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4918 completion_mode[i], EXPECT (expected));
4919 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4920 completion_mode[i], EXPECT (expected));
4921 }
4922 }
4923
4924 {
4925 /* Check method qualifiers are ignored. */
4926 static const char expected[] = "ns::foo<char*>";
4927 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4928 symbol_name_match_type::FULL, true, EXPECT (expected));
4929 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4930 symbol_name_match_type::FULL, true, EXPECT (expected));
4931 CHECK_MATCH ("foo < char * > ( int ) const",
4932 symbol_name_match_type::WILD, true, EXPECT (expected));
4933 CHECK_MATCH ("foo < char * > ( int ) &&",
4934 symbol_name_match_type::WILD, true, EXPECT (expected));
4935 }
4936
4937 /* Test lookup names that don't match anything. */
4938 {
4939 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4940 {});
4941
4942 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4943 {});
4944 }
4945
4946 /* Some wild matching tests, exercising "(anonymous namespace)",
4947 which should not be confused with a parameter list. */
4948 {
4949 static const char *syms[] = {
4950 "A::B::C",
4951 "B::C",
4952 "C",
4953 "A :: B :: C ( int )",
4954 "B :: C ( int )",
4955 "C ( int )",
4956 };
4957
4958 for (const char *s : syms)
4959 {
4960 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4961 EXPECT ("(anonymous namespace)::A::B::C"));
4962 }
4963 }
4964
4965 {
4966 static const char expected[] = "ns2::tmpl<int>::foo2";
4967 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4968 EXPECT (expected));
4969 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4970 EXPECT (expected));
4971 }
4972
4973 SELF_CHECK (!any_mismatch);
4974
4975 #undef EXPECT
4976 #undef CHECK_MATCH
4977 }
4978
4979 static void
4980 run_test ()
4981 {
4982 test_mapped_index_find_name_component_bounds ();
4983 test_dw2_expand_symtabs_matching_symbol ();
4984 }
4985
4986 }} // namespace selftests::dw2_expand_symtabs_matching
4987
4988 #endif /* GDB_SELF_TEST */
4989
4990 /* If FILE_MATCHER is NULL or if PER_CU has
4991 dwarf2_per_cu_quick_data::MARK set (see
4992 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4993 EXPANSION_NOTIFY on it. */
4994
4995 static void
4996 dw2_expand_symtabs_matching_one
4997 (struct dwarf2_per_cu_data *per_cu,
4998 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4999 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
5000 {
5001 if (file_matcher == NULL || per_cu->v.quick->mark)
5002 {
5003 bool symtab_was_null
5004 = (per_cu->v.quick->compunit_symtab == NULL);
5005
5006 dw2_instantiate_symtab (per_cu, false);
5007
5008 if (expansion_notify != NULL
5009 && symtab_was_null
5010 && per_cu->v.quick->compunit_symtab != NULL)
5011 expansion_notify (per_cu->v.quick->compunit_symtab);
5012 }
5013 }
5014
5015 /* Helper for dw2_expand_matching symtabs. Called on each symbol
5016 matched, to expand corresponding CUs that were marked. IDX is the
5017 index of the symbol name that matched. */
5018
5019 static void
5020 dw2_expand_marked_cus
5021 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
5022 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5023 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5024 search_domain kind)
5025 {
5026 offset_type *vec, vec_len, vec_idx;
5027 bool global_seen = false;
5028 mapped_index &index = *dwarf2_per_objfile->index_table;
5029
5030 vec = (offset_type *) (index.constant_pool
5031 + MAYBE_SWAP (index.symbol_table[idx].vec));
5032 vec_len = MAYBE_SWAP (vec[0]);
5033 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
5034 {
5035 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
5036 /* This value is only valid for index versions >= 7. */
5037 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
5038 gdb_index_symbol_kind symbol_kind =
5039 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
5040 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
5041 /* Only check the symbol attributes if they're present.
5042 Indices prior to version 7 don't record them,
5043 and indices >= 7 may elide them for certain symbols
5044 (gold does this). */
5045 int attrs_valid =
5046 (index.version >= 7
5047 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
5048
5049 /* Work around gold/15646. */
5050 if (attrs_valid)
5051 {
5052 if (!is_static && global_seen)
5053 continue;
5054 if (!is_static)
5055 global_seen = true;
5056 }
5057
5058 /* Only check the symbol's kind if it has one. */
5059 if (attrs_valid)
5060 {
5061 switch (kind)
5062 {
5063 case VARIABLES_DOMAIN:
5064 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
5065 continue;
5066 break;
5067 case FUNCTIONS_DOMAIN:
5068 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
5069 continue;
5070 break;
5071 case TYPES_DOMAIN:
5072 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
5073 continue;
5074 break;
5075 case MODULES_DOMAIN:
5076 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
5077 continue;
5078 break;
5079 default:
5080 break;
5081 }
5082 }
5083
5084 /* Don't crash on bad data. */
5085 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
5086 + dwarf2_per_objfile->all_type_units.size ()))
5087 {
5088 complaint (_(".gdb_index entry has bad CU index"
5089 " [in module %s]"),
5090 objfile_name (dwarf2_per_objfile->objfile));
5091 continue;
5092 }
5093
5094 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
5095 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5096 expansion_notify);
5097 }
5098 }
5099
5100 /* If FILE_MATCHER is non-NULL, set all the
5101 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
5102 that match FILE_MATCHER. */
5103
5104 static void
5105 dw_expand_symtabs_matching_file_matcher
5106 (struct dwarf2_per_objfile *dwarf2_per_objfile,
5107 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
5108 {
5109 if (file_matcher == NULL)
5110 return;
5111
5112 objfile *const objfile = dwarf2_per_objfile->objfile;
5113
5114 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
5115 htab_eq_pointer,
5116 NULL, xcalloc, xfree));
5117 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
5118 htab_eq_pointer,
5119 NULL, xcalloc, xfree));
5120
5121 /* The rule is CUs specify all the files, including those used by
5122 any TU, so there's no need to scan TUs here. */
5123
5124 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5125 {
5126 QUIT;
5127
5128 per_cu->v.quick->mark = 0;
5129
5130 /* We only need to look at symtabs not already expanded. */
5131 if (per_cu->v.quick->compunit_symtab)
5132 continue;
5133
5134 quick_file_names *file_data = dw2_get_file_names (per_cu);
5135 if (file_data == NULL)
5136 continue;
5137
5138 if (htab_find (visited_not_found.get (), file_data) != NULL)
5139 continue;
5140 else if (htab_find (visited_found.get (), file_data) != NULL)
5141 {
5142 per_cu->v.quick->mark = 1;
5143 continue;
5144 }
5145
5146 for (int j = 0; j < file_data->num_file_names; ++j)
5147 {
5148 const char *this_real_name;
5149
5150 if (file_matcher (file_data->file_names[j], false))
5151 {
5152 per_cu->v.quick->mark = 1;
5153 break;
5154 }
5155
5156 /* Before we invoke realpath, which can get expensive when many
5157 files are involved, do a quick comparison of the basenames. */
5158 if (!basenames_may_differ
5159 && !file_matcher (lbasename (file_data->file_names[j]),
5160 true))
5161 continue;
5162
5163 this_real_name = dw2_get_real_path (objfile, file_data, j);
5164 if (file_matcher (this_real_name, false))
5165 {
5166 per_cu->v.quick->mark = 1;
5167 break;
5168 }
5169 }
5170
5171 void **slot = htab_find_slot (per_cu->v.quick->mark
5172 ? visited_found.get ()
5173 : visited_not_found.get (),
5174 file_data, INSERT);
5175 *slot = file_data;
5176 }
5177 }
5178
5179 static void
5180 dw2_expand_symtabs_matching
5181 (struct objfile *objfile,
5182 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5183 const lookup_name_info &lookup_name,
5184 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5185 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5186 enum search_domain kind)
5187 {
5188 struct dwarf2_per_objfile *dwarf2_per_objfile
5189 = get_dwarf2_per_objfile (objfile);
5190
5191 /* index_table is NULL if OBJF_READNOW. */
5192 if (!dwarf2_per_objfile->index_table)
5193 return;
5194
5195 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
5196
5197 mapped_index &index = *dwarf2_per_objfile->index_table;
5198
5199 dw2_expand_symtabs_matching_symbol (index, lookup_name,
5200 symbol_matcher,
5201 kind, [&] (offset_type idx)
5202 {
5203 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
5204 expansion_notify, kind);
5205 return true;
5206 });
5207 }
5208
5209 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
5210 symtab. */
5211
5212 static struct compunit_symtab *
5213 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
5214 CORE_ADDR pc)
5215 {
5216 int i;
5217
5218 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
5219 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
5220 return cust;
5221
5222 if (cust->includes == NULL)
5223 return NULL;
5224
5225 for (i = 0; cust->includes[i]; ++i)
5226 {
5227 struct compunit_symtab *s = cust->includes[i];
5228
5229 s = recursively_find_pc_sect_compunit_symtab (s, pc);
5230 if (s != NULL)
5231 return s;
5232 }
5233
5234 return NULL;
5235 }
5236
5237 static struct compunit_symtab *
5238 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
5239 struct bound_minimal_symbol msymbol,
5240 CORE_ADDR pc,
5241 struct obj_section *section,
5242 int warn_if_readin)
5243 {
5244 struct dwarf2_per_cu_data *data;
5245 struct compunit_symtab *result;
5246
5247 if (!objfile->partial_symtabs->psymtabs_addrmap)
5248 return NULL;
5249
5250 CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
5251 SECT_OFF_TEXT (objfile));
5252 data = (struct dwarf2_per_cu_data *) addrmap_find
5253 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
5254 if (!data)
5255 return NULL;
5256
5257 if (warn_if_readin && data->v.quick->compunit_symtab)
5258 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
5259 paddress (get_objfile_arch (objfile), pc));
5260
5261 result
5262 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data,
5263 false),
5264 pc);
5265 gdb_assert (result != NULL);
5266 return result;
5267 }
5268
5269 static void
5270 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
5271 void *data, int need_fullname)
5272 {
5273 struct dwarf2_per_objfile *dwarf2_per_objfile
5274 = get_dwarf2_per_objfile (objfile);
5275
5276 if (!dwarf2_per_objfile->filenames_cache)
5277 {
5278 dwarf2_per_objfile->filenames_cache.emplace ();
5279
5280 htab_up visited (htab_create_alloc (10,
5281 htab_hash_pointer, htab_eq_pointer,
5282 NULL, xcalloc, xfree));
5283
5284 /* The rule is CUs specify all the files, including those used
5285 by any TU, so there's no need to scan TUs here. We can
5286 ignore file names coming from already-expanded CUs. */
5287
5288 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5289 {
5290 if (per_cu->v.quick->compunit_symtab)
5291 {
5292 void **slot = htab_find_slot (visited.get (),
5293 per_cu->v.quick->file_names,
5294 INSERT);
5295
5296 *slot = per_cu->v.quick->file_names;
5297 }
5298 }
5299
5300 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5301 {
5302 /* We only need to look at symtabs not already expanded. */
5303 if (per_cu->v.quick->compunit_symtab)
5304 continue;
5305
5306 quick_file_names *file_data = dw2_get_file_names (per_cu);
5307 if (file_data == NULL)
5308 continue;
5309
5310 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
5311 if (*slot)
5312 {
5313 /* Already visited. */
5314 continue;
5315 }
5316 *slot = file_data;
5317
5318 for (int j = 0; j < file_data->num_file_names; ++j)
5319 {
5320 const char *filename = file_data->file_names[j];
5321 dwarf2_per_objfile->filenames_cache->seen (filename);
5322 }
5323 }
5324 }
5325
5326 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
5327 {
5328 gdb::unique_xmalloc_ptr<char> this_real_name;
5329
5330 if (need_fullname)
5331 this_real_name = gdb_realpath (filename);
5332 (*fun) (filename, this_real_name.get (), data);
5333 });
5334 }
5335
5336 static int
5337 dw2_has_symbols (struct objfile *objfile)
5338 {
5339 return 1;
5340 }
5341
5342 const struct quick_symbol_functions dwarf2_gdb_index_functions =
5343 {
5344 dw2_has_symbols,
5345 dw2_find_last_source_symtab,
5346 dw2_forget_cached_source_info,
5347 dw2_map_symtabs_matching_filename,
5348 dw2_lookup_symbol,
5349 dw2_print_stats,
5350 dw2_dump,
5351 dw2_expand_symtabs_for_function,
5352 dw2_expand_all_symtabs,
5353 dw2_expand_symtabs_with_fullname,
5354 dw2_map_matching_symbols,
5355 dw2_expand_symtabs_matching,
5356 dw2_find_pc_sect_compunit_symtab,
5357 NULL,
5358 dw2_map_symbol_filenames
5359 };
5360
5361 /* DWARF-5 debug_names reader. */
5362
5363 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5364 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5365
5366 /* A helper function that reads the .debug_names section in SECTION
5367 and fills in MAP. FILENAME is the name of the file containing the
5368 section; it is used for error reporting.
5369
5370 Returns true if all went well, false otherwise. */
5371
5372 static bool
5373 read_debug_names_from_section (struct objfile *objfile,
5374 const char *filename,
5375 struct dwarf2_section_info *section,
5376 mapped_debug_names &map)
5377 {
5378 if (dwarf2_section_empty_p (section))
5379 return false;
5380
5381 /* Older elfutils strip versions could keep the section in the main
5382 executable while splitting it for the separate debug info file. */
5383 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
5384 return false;
5385
5386 dwarf2_read_section (objfile, section);
5387
5388 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
5389
5390 const gdb_byte *addr = section->buffer;
5391
5392 bfd *const abfd = get_section_bfd_owner (section);
5393
5394 unsigned int bytes_read;
5395 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5396 addr += bytes_read;
5397
5398 map.dwarf5_is_dwarf64 = bytes_read != 4;
5399 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5400 if (bytes_read + length != section->size)
5401 {
5402 /* There may be multiple per-CU indices. */
5403 warning (_("Section .debug_names in %s length %s does not match "
5404 "section length %s, ignoring .debug_names."),
5405 filename, plongest (bytes_read + length),
5406 pulongest (section->size));
5407 return false;
5408 }
5409
5410 /* The version number. */
5411 uint16_t version = read_2_bytes (abfd, addr);
5412 addr += 2;
5413 if (version != 5)
5414 {
5415 warning (_("Section .debug_names in %s has unsupported version %d, "
5416 "ignoring .debug_names."),
5417 filename, version);
5418 return false;
5419 }
5420
5421 /* Padding. */
5422 uint16_t padding = read_2_bytes (abfd, addr);
5423 addr += 2;
5424 if (padding != 0)
5425 {
5426 warning (_("Section .debug_names in %s has unsupported padding %d, "
5427 "ignoring .debug_names."),
5428 filename, padding);
5429 return false;
5430 }
5431
5432 /* comp_unit_count - The number of CUs in the CU list. */
5433 map.cu_count = read_4_bytes (abfd, addr);
5434 addr += 4;
5435
5436 /* local_type_unit_count - The number of TUs in the local TU
5437 list. */
5438 map.tu_count = read_4_bytes (abfd, addr);
5439 addr += 4;
5440
5441 /* foreign_type_unit_count - The number of TUs in the foreign TU
5442 list. */
5443 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5444 addr += 4;
5445 if (foreign_tu_count != 0)
5446 {
5447 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5448 "ignoring .debug_names."),
5449 filename, static_cast<unsigned long> (foreign_tu_count));
5450 return false;
5451 }
5452
5453 /* bucket_count - The number of hash buckets in the hash lookup
5454 table. */
5455 map.bucket_count = read_4_bytes (abfd, addr);
5456 addr += 4;
5457
5458 /* name_count - The number of unique names in the index. */
5459 map.name_count = read_4_bytes (abfd, addr);
5460 addr += 4;
5461
5462 /* abbrev_table_size - The size in bytes of the abbreviations
5463 table. */
5464 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5465 addr += 4;
5466
5467 /* augmentation_string_size - The size in bytes of the augmentation
5468 string. This value is rounded up to a multiple of 4. */
5469 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5470 addr += 4;
5471 map.augmentation_is_gdb = ((augmentation_string_size
5472 == sizeof (dwarf5_augmentation))
5473 && memcmp (addr, dwarf5_augmentation,
5474 sizeof (dwarf5_augmentation)) == 0);
5475 augmentation_string_size += (-augmentation_string_size) & 3;
5476 addr += augmentation_string_size;
5477
5478 /* List of CUs */
5479 map.cu_table_reordered = addr;
5480 addr += map.cu_count * map.offset_size;
5481
5482 /* List of Local TUs */
5483 map.tu_table_reordered = addr;
5484 addr += map.tu_count * map.offset_size;
5485
5486 /* Hash Lookup Table */
5487 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5488 addr += map.bucket_count * 4;
5489 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5490 addr += map.name_count * 4;
5491
5492 /* Name Table */
5493 map.name_table_string_offs_reordered = addr;
5494 addr += map.name_count * map.offset_size;
5495 map.name_table_entry_offs_reordered = addr;
5496 addr += map.name_count * map.offset_size;
5497
5498 const gdb_byte *abbrev_table_start = addr;
5499 for (;;)
5500 {
5501 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5502 addr += bytes_read;
5503 if (index_num == 0)
5504 break;
5505
5506 const auto insertpair
5507 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5508 if (!insertpair.second)
5509 {
5510 warning (_("Section .debug_names in %s has duplicate index %s, "
5511 "ignoring .debug_names."),
5512 filename, pulongest (index_num));
5513 return false;
5514 }
5515 mapped_debug_names::index_val &indexval = insertpair.first->second;
5516 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5517 addr += bytes_read;
5518
5519 for (;;)
5520 {
5521 mapped_debug_names::index_val::attr attr;
5522 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5523 addr += bytes_read;
5524 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5525 addr += bytes_read;
5526 if (attr.form == DW_FORM_implicit_const)
5527 {
5528 attr.implicit_const = read_signed_leb128 (abfd, addr,
5529 &bytes_read);
5530 addr += bytes_read;
5531 }
5532 if (attr.dw_idx == 0 && attr.form == 0)
5533 break;
5534 indexval.attr_vec.push_back (std::move (attr));
5535 }
5536 }
5537 if (addr != abbrev_table_start + abbrev_table_size)
5538 {
5539 warning (_("Section .debug_names in %s has abbreviation_table "
5540 "of size %s vs. written as %u, ignoring .debug_names."),
5541 filename, plongest (addr - abbrev_table_start),
5542 abbrev_table_size);
5543 return false;
5544 }
5545 map.entry_pool = addr;
5546
5547 return true;
5548 }
5549
5550 /* A helper for create_cus_from_debug_names that handles the MAP's CU
5551 list. */
5552
5553 static void
5554 create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
5555 const mapped_debug_names &map,
5556 dwarf2_section_info &section,
5557 bool is_dwz)
5558 {
5559 sect_offset sect_off_prev;
5560 for (uint32_t i = 0; i <= map.cu_count; ++i)
5561 {
5562 sect_offset sect_off_next;
5563 if (i < map.cu_count)
5564 {
5565 sect_off_next
5566 = (sect_offset) (extract_unsigned_integer
5567 (map.cu_table_reordered + i * map.offset_size,
5568 map.offset_size,
5569 map.dwarf5_byte_order));
5570 }
5571 else
5572 sect_off_next = (sect_offset) section.size;
5573 if (i >= 1)
5574 {
5575 const ULONGEST length = sect_off_next - sect_off_prev;
5576 dwarf2_per_cu_data *per_cu
5577 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
5578 sect_off_prev, length);
5579 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
5580 }
5581 sect_off_prev = sect_off_next;
5582 }
5583 }
5584
5585 /* Read the CU list from the mapped index, and use it to create all
5586 the CU objects for this dwarf2_per_objfile. */
5587
5588 static void
5589 create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
5590 const mapped_debug_names &map,
5591 const mapped_debug_names &dwz_map)
5592 {
5593 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
5594 dwarf2_per_objfile->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
5595
5596 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5597 dwarf2_per_objfile->info,
5598 false /* is_dwz */);
5599
5600 if (dwz_map.cu_count == 0)
5601 return;
5602
5603 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5604 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
5605 true /* is_dwz */);
5606 }
5607
5608 /* Read .debug_names. If everything went ok, initialize the "quick"
5609 elements of all the CUs and return true. Otherwise, return false. */
5610
5611 static bool
5612 dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
5613 {
5614 std::unique_ptr<mapped_debug_names> map
5615 (new mapped_debug_names (dwarf2_per_objfile));
5616 mapped_debug_names dwz_map (dwarf2_per_objfile);
5617 struct objfile *objfile = dwarf2_per_objfile->objfile;
5618
5619 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5620 &dwarf2_per_objfile->debug_names,
5621 *map))
5622 return false;
5623
5624 /* Don't use the index if it's empty. */
5625 if (map->name_count == 0)
5626 return false;
5627
5628 /* If there is a .dwz file, read it so we can get its CU list as
5629 well. */
5630 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5631 if (dwz != NULL)
5632 {
5633 if (!read_debug_names_from_section (objfile,
5634 bfd_get_filename (dwz->dwz_bfd.get ()),
5635 &dwz->debug_names, dwz_map))
5636 {
5637 warning (_("could not read '.debug_names' section from %s; skipping"),
5638 bfd_get_filename (dwz->dwz_bfd.get ()));
5639 return false;
5640 }
5641 }
5642
5643 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
5644
5645 if (map->tu_count != 0)
5646 {
5647 /* We can only handle a single .debug_types when we have an
5648 index. */
5649 if (dwarf2_per_objfile->types.size () != 1)
5650 return false;
5651
5652 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
5653
5654 create_signatured_type_table_from_debug_names
5655 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->abbrev);
5656 }
5657
5658 create_addrmap_from_aranges (dwarf2_per_objfile,
5659 &dwarf2_per_objfile->debug_aranges);
5660
5661 dwarf2_per_objfile->debug_names_table = std::move (map);
5662 dwarf2_per_objfile->using_index = 1;
5663 dwarf2_per_objfile->quick_file_names_table =
5664 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
5665
5666 return true;
5667 }
5668
5669 /* Type used to manage iterating over all CUs looking for a symbol for
5670 .debug_names. */
5671
5672 class dw2_debug_names_iterator
5673 {
5674 public:
5675 dw2_debug_names_iterator (const mapped_debug_names &map,
5676 gdb::optional<block_enum> block_index,
5677 domain_enum domain,
5678 const char *name)
5679 : m_map (map), m_block_index (block_index), m_domain (domain),
5680 m_addr (find_vec_in_debug_names (map, name))
5681 {}
5682
5683 dw2_debug_names_iterator (const mapped_debug_names &map,
5684 search_domain search, uint32_t namei)
5685 : m_map (map),
5686 m_search (search),
5687 m_addr (find_vec_in_debug_names (map, namei))
5688 {}
5689
5690 dw2_debug_names_iterator (const mapped_debug_names &map,
5691 block_enum block_index, domain_enum domain,
5692 uint32_t namei)
5693 : m_map (map), m_block_index (block_index), m_domain (domain),
5694 m_addr (find_vec_in_debug_names (map, namei))
5695 {}
5696
5697 /* Return the next matching CU or NULL if there are no more. */
5698 dwarf2_per_cu_data *next ();
5699
5700 private:
5701 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5702 const char *name);
5703 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5704 uint32_t namei);
5705
5706 /* The internalized form of .debug_names. */
5707 const mapped_debug_names &m_map;
5708
5709 /* If set, only look for symbols that match that block. Valid values are
5710 GLOBAL_BLOCK and STATIC_BLOCK. */
5711 const gdb::optional<block_enum> m_block_index;
5712
5713 /* The kind of symbol we're looking for. */
5714 const domain_enum m_domain = UNDEF_DOMAIN;
5715 const search_domain m_search = ALL_DOMAIN;
5716
5717 /* The list of CUs from the index entry of the symbol, or NULL if
5718 not found. */
5719 const gdb_byte *m_addr;
5720 };
5721
5722 const char *
5723 mapped_debug_names::namei_to_name (uint32_t namei) const
5724 {
5725 const ULONGEST namei_string_offs
5726 = extract_unsigned_integer ((name_table_string_offs_reordered
5727 + namei * offset_size),
5728 offset_size,
5729 dwarf5_byte_order);
5730 return read_indirect_string_at_offset
5731 (dwarf2_per_objfile, dwarf2_per_objfile->objfile->obfd, namei_string_offs);
5732 }
5733
5734 /* Find a slot in .debug_names for the object named NAME. If NAME is
5735 found, return pointer to its pool data. If NAME cannot be found,
5736 return NULL. */
5737
5738 const gdb_byte *
5739 dw2_debug_names_iterator::find_vec_in_debug_names
5740 (const mapped_debug_names &map, const char *name)
5741 {
5742 int (*cmp) (const char *, const char *);
5743
5744 gdb::unique_xmalloc_ptr<char> without_params;
5745 if (current_language->la_language == language_cplus
5746 || current_language->la_language == language_fortran
5747 || current_language->la_language == language_d)
5748 {
5749 /* NAME is already canonical. Drop any qualifiers as
5750 .debug_names does not contain any. */
5751
5752 if (strchr (name, '(') != NULL)
5753 {
5754 without_params = cp_remove_params (name);
5755 if (without_params != NULL)
5756 name = without_params.get ();
5757 }
5758 }
5759
5760 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5761
5762 const uint32_t full_hash = dwarf5_djb_hash (name);
5763 uint32_t namei
5764 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5765 (map.bucket_table_reordered
5766 + (full_hash % map.bucket_count)), 4,
5767 map.dwarf5_byte_order);
5768 if (namei == 0)
5769 return NULL;
5770 --namei;
5771 if (namei >= map.name_count)
5772 {
5773 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5774 "[in module %s]"),
5775 namei, map.name_count,
5776 objfile_name (map.dwarf2_per_objfile->objfile));
5777 return NULL;
5778 }
5779
5780 for (;;)
5781 {
5782 const uint32_t namei_full_hash
5783 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5784 (map.hash_table_reordered + namei), 4,
5785 map.dwarf5_byte_order);
5786 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5787 return NULL;
5788
5789 if (full_hash == namei_full_hash)
5790 {
5791 const char *const namei_string = map.namei_to_name (namei);
5792
5793 #if 0 /* An expensive sanity check. */
5794 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5795 {
5796 complaint (_("Wrong .debug_names hash for string at index %u "
5797 "[in module %s]"),
5798 namei, objfile_name (dwarf2_per_objfile->objfile));
5799 return NULL;
5800 }
5801 #endif
5802
5803 if (cmp (namei_string, name) == 0)
5804 {
5805 const ULONGEST namei_entry_offs
5806 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5807 + namei * map.offset_size),
5808 map.offset_size, map.dwarf5_byte_order);
5809 return map.entry_pool + namei_entry_offs;
5810 }
5811 }
5812
5813 ++namei;
5814 if (namei >= map.name_count)
5815 return NULL;
5816 }
5817 }
5818
5819 const gdb_byte *
5820 dw2_debug_names_iterator::find_vec_in_debug_names
5821 (const mapped_debug_names &map, uint32_t namei)
5822 {
5823 if (namei >= map.name_count)
5824 {
5825 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5826 "[in module %s]"),
5827 namei, map.name_count,
5828 objfile_name (map.dwarf2_per_objfile->objfile));
5829 return NULL;
5830 }
5831
5832 const ULONGEST namei_entry_offs
5833 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5834 + namei * map.offset_size),
5835 map.offset_size, map.dwarf5_byte_order);
5836 return map.entry_pool + namei_entry_offs;
5837 }
5838
5839 /* See dw2_debug_names_iterator. */
5840
5841 dwarf2_per_cu_data *
5842 dw2_debug_names_iterator::next ()
5843 {
5844 if (m_addr == NULL)
5845 return NULL;
5846
5847 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5848 struct objfile *objfile = dwarf2_per_objfile->objfile;
5849 bfd *const abfd = objfile->obfd;
5850
5851 again:
5852
5853 unsigned int bytes_read;
5854 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5855 m_addr += bytes_read;
5856 if (abbrev == 0)
5857 return NULL;
5858
5859 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5860 if (indexval_it == m_map.abbrev_map.cend ())
5861 {
5862 complaint (_("Wrong .debug_names undefined abbrev code %s "
5863 "[in module %s]"),
5864 pulongest (abbrev), objfile_name (objfile));
5865 return NULL;
5866 }
5867 const mapped_debug_names::index_val &indexval = indexval_it->second;
5868 enum class symbol_linkage {
5869 unknown,
5870 static_,
5871 extern_,
5872 } symbol_linkage_ = symbol_linkage::unknown;
5873 dwarf2_per_cu_data *per_cu = NULL;
5874 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5875 {
5876 ULONGEST ull;
5877 switch (attr.form)
5878 {
5879 case DW_FORM_implicit_const:
5880 ull = attr.implicit_const;
5881 break;
5882 case DW_FORM_flag_present:
5883 ull = 1;
5884 break;
5885 case DW_FORM_udata:
5886 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5887 m_addr += bytes_read;
5888 break;
5889 default:
5890 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5891 dwarf_form_name (attr.form),
5892 objfile_name (objfile));
5893 return NULL;
5894 }
5895 switch (attr.dw_idx)
5896 {
5897 case DW_IDX_compile_unit:
5898 /* Don't crash on bad data. */
5899 if (ull >= dwarf2_per_objfile->all_comp_units.size ())
5900 {
5901 complaint (_(".debug_names entry has bad CU index %s"
5902 " [in module %s]"),
5903 pulongest (ull),
5904 objfile_name (dwarf2_per_objfile->objfile));
5905 continue;
5906 }
5907 per_cu = dwarf2_per_objfile->get_cutu (ull);
5908 break;
5909 case DW_IDX_type_unit:
5910 /* Don't crash on bad data. */
5911 if (ull >= dwarf2_per_objfile->all_type_units.size ())
5912 {
5913 complaint (_(".debug_names entry has bad TU index %s"
5914 " [in module %s]"),
5915 pulongest (ull),
5916 objfile_name (dwarf2_per_objfile->objfile));
5917 continue;
5918 }
5919 per_cu = &dwarf2_per_objfile->get_tu (ull)->per_cu;
5920 break;
5921 case DW_IDX_GNU_internal:
5922 if (!m_map.augmentation_is_gdb)
5923 break;
5924 symbol_linkage_ = symbol_linkage::static_;
5925 break;
5926 case DW_IDX_GNU_external:
5927 if (!m_map.augmentation_is_gdb)
5928 break;
5929 symbol_linkage_ = symbol_linkage::extern_;
5930 break;
5931 }
5932 }
5933
5934 /* Skip if already read in. */
5935 if (per_cu->v.quick->compunit_symtab)
5936 goto again;
5937
5938 /* Check static vs global. */
5939 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
5940 {
5941 const bool want_static = *m_block_index == STATIC_BLOCK;
5942 const bool symbol_is_static =
5943 symbol_linkage_ == symbol_linkage::static_;
5944 if (want_static != symbol_is_static)
5945 goto again;
5946 }
5947
5948 /* Match dw2_symtab_iter_next, symbol_kind
5949 and debug_names::psymbol_tag. */
5950 switch (m_domain)
5951 {
5952 case VAR_DOMAIN:
5953 switch (indexval.dwarf_tag)
5954 {
5955 case DW_TAG_variable:
5956 case DW_TAG_subprogram:
5957 /* Some types are also in VAR_DOMAIN. */
5958 case DW_TAG_typedef:
5959 case DW_TAG_structure_type:
5960 break;
5961 default:
5962 goto again;
5963 }
5964 break;
5965 case STRUCT_DOMAIN:
5966 switch (indexval.dwarf_tag)
5967 {
5968 case DW_TAG_typedef:
5969 case DW_TAG_structure_type:
5970 break;
5971 default:
5972 goto again;
5973 }
5974 break;
5975 case LABEL_DOMAIN:
5976 switch (indexval.dwarf_tag)
5977 {
5978 case 0:
5979 case DW_TAG_variable:
5980 break;
5981 default:
5982 goto again;
5983 }
5984 break;
5985 case MODULE_DOMAIN:
5986 switch (indexval.dwarf_tag)
5987 {
5988 case DW_TAG_module:
5989 break;
5990 default:
5991 goto again;
5992 }
5993 break;
5994 default:
5995 break;
5996 }
5997
5998 /* Match dw2_expand_symtabs_matching, symbol_kind and
5999 debug_names::psymbol_tag. */
6000 switch (m_search)
6001 {
6002 case VARIABLES_DOMAIN:
6003 switch (indexval.dwarf_tag)
6004 {
6005 case DW_TAG_variable:
6006 break;
6007 default:
6008 goto again;
6009 }
6010 break;
6011 case FUNCTIONS_DOMAIN:
6012 switch (indexval.dwarf_tag)
6013 {
6014 case DW_TAG_subprogram:
6015 break;
6016 default:
6017 goto again;
6018 }
6019 break;
6020 case TYPES_DOMAIN:
6021 switch (indexval.dwarf_tag)
6022 {
6023 case DW_TAG_typedef:
6024 case DW_TAG_structure_type:
6025 break;
6026 default:
6027 goto again;
6028 }
6029 break;
6030 case MODULES_DOMAIN:
6031 switch (indexval.dwarf_tag)
6032 {
6033 case DW_TAG_module:
6034 break;
6035 default:
6036 goto again;
6037 }
6038 default:
6039 break;
6040 }
6041
6042 return per_cu;
6043 }
6044
6045 static struct compunit_symtab *
6046 dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
6047 const char *name, domain_enum domain)
6048 {
6049 struct dwarf2_per_objfile *dwarf2_per_objfile
6050 = get_dwarf2_per_objfile (objfile);
6051
6052 const auto &mapp = dwarf2_per_objfile->debug_names_table;
6053 if (!mapp)
6054 {
6055 /* index is NULL if OBJF_READNOW. */
6056 return NULL;
6057 }
6058 const auto &map = *mapp;
6059
6060 dw2_debug_names_iterator iter (map, block_index, domain, name);
6061
6062 struct compunit_symtab *stab_best = NULL;
6063 struct dwarf2_per_cu_data *per_cu;
6064 while ((per_cu = iter.next ()) != NULL)
6065 {
6066 struct symbol *sym, *with_opaque = NULL;
6067 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
6068 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
6069 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
6070
6071 sym = block_find_symbol (block, name, domain,
6072 block_find_non_opaque_type_preferred,
6073 &with_opaque);
6074
6075 /* Some caution must be observed with overloaded functions and
6076 methods, since the index will not contain any overload
6077 information (but NAME might contain it). */
6078
6079 if (sym != NULL
6080 && strcmp_iw (sym->search_name (), name) == 0)
6081 return stab;
6082 if (with_opaque != NULL
6083 && strcmp_iw (with_opaque->search_name (), name) == 0)
6084 stab_best = stab;
6085
6086 /* Keep looking through other CUs. */
6087 }
6088
6089 return stab_best;
6090 }
6091
6092 /* This dumps minimal information about .debug_names. It is called
6093 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
6094 uses this to verify that .debug_names has been loaded. */
6095
6096 static void
6097 dw2_debug_names_dump (struct objfile *objfile)
6098 {
6099 struct dwarf2_per_objfile *dwarf2_per_objfile
6100 = get_dwarf2_per_objfile (objfile);
6101
6102 gdb_assert (dwarf2_per_objfile->using_index);
6103 printf_filtered (".debug_names:");
6104 if (dwarf2_per_objfile->debug_names_table)
6105 printf_filtered (" exists\n");
6106 else
6107 printf_filtered (" faked for \"readnow\"\n");
6108 printf_filtered ("\n");
6109 }
6110
6111 static void
6112 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
6113 const char *func_name)
6114 {
6115 struct dwarf2_per_objfile *dwarf2_per_objfile
6116 = get_dwarf2_per_objfile (objfile);
6117
6118 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
6119 if (dwarf2_per_objfile->debug_names_table)
6120 {
6121 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6122
6123 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
6124
6125 struct dwarf2_per_cu_data *per_cu;
6126 while ((per_cu = iter.next ()) != NULL)
6127 dw2_instantiate_symtab (per_cu, false);
6128 }
6129 }
6130
6131 static void
6132 dw2_debug_names_map_matching_symbols
6133 (struct objfile *objfile,
6134 const lookup_name_info &name, domain_enum domain,
6135 int global,
6136 gdb::function_view<symbol_found_callback_ftype> callback,
6137 symbol_compare_ftype *ordered_compare)
6138 {
6139 struct dwarf2_per_objfile *dwarf2_per_objfile
6140 = get_dwarf2_per_objfile (objfile);
6141
6142 /* debug_names_table is NULL if OBJF_READNOW. */
6143 if (!dwarf2_per_objfile->debug_names_table)
6144 return;
6145
6146 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6147 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
6148
6149 const char *match_name = name.ada ().lookup_name ().c_str ();
6150 auto matcher = [&] (const char *symname)
6151 {
6152 if (ordered_compare == nullptr)
6153 return true;
6154 return ordered_compare (symname, match_name) == 0;
6155 };
6156
6157 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
6158 [&] (offset_type namei)
6159 {
6160 /* The name was matched, now expand corresponding CUs that were
6161 marked. */
6162 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
6163
6164 struct dwarf2_per_cu_data *per_cu;
6165 while ((per_cu = iter.next ()) != NULL)
6166 dw2_expand_symtabs_matching_one (per_cu, nullptr, nullptr);
6167 return true;
6168 });
6169
6170 /* It's a shame we couldn't do this inside the
6171 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
6172 that have already been expanded. Instead, this loop matches what
6173 the psymtab code does. */
6174 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
6175 {
6176 struct compunit_symtab *cust = per_cu->v.quick->compunit_symtab;
6177 if (cust != nullptr)
6178 {
6179 const struct block *block
6180 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
6181 if (!iterate_over_symbols_terminated (block, name,
6182 domain, callback))
6183 break;
6184 }
6185 }
6186 }
6187
6188 static void
6189 dw2_debug_names_expand_symtabs_matching
6190 (struct objfile *objfile,
6191 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
6192 const lookup_name_info &lookup_name,
6193 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
6194 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
6195 enum search_domain kind)
6196 {
6197 struct dwarf2_per_objfile *dwarf2_per_objfile
6198 = get_dwarf2_per_objfile (objfile);
6199
6200 /* debug_names_table is NULL if OBJF_READNOW. */
6201 if (!dwarf2_per_objfile->debug_names_table)
6202 return;
6203
6204 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
6205
6206 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6207
6208 dw2_expand_symtabs_matching_symbol (map, lookup_name,
6209 symbol_matcher,
6210 kind, [&] (offset_type namei)
6211 {
6212 /* The name was matched, now expand corresponding CUs that were
6213 marked. */
6214 dw2_debug_names_iterator iter (map, kind, namei);
6215
6216 struct dwarf2_per_cu_data *per_cu;
6217 while ((per_cu = iter.next ()) != NULL)
6218 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
6219 expansion_notify);
6220 return true;
6221 });
6222 }
6223
6224 const struct quick_symbol_functions dwarf2_debug_names_functions =
6225 {
6226 dw2_has_symbols,
6227 dw2_find_last_source_symtab,
6228 dw2_forget_cached_source_info,
6229 dw2_map_symtabs_matching_filename,
6230 dw2_debug_names_lookup_symbol,
6231 dw2_print_stats,
6232 dw2_debug_names_dump,
6233 dw2_debug_names_expand_symtabs_for_function,
6234 dw2_expand_all_symtabs,
6235 dw2_expand_symtabs_with_fullname,
6236 dw2_debug_names_map_matching_symbols,
6237 dw2_debug_names_expand_symtabs_matching,
6238 dw2_find_pc_sect_compunit_symtab,
6239 NULL,
6240 dw2_map_symbol_filenames
6241 };
6242
6243 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
6244 to either a dwarf2_per_objfile or dwz_file object. */
6245
6246 template <typename T>
6247 static gdb::array_view<const gdb_byte>
6248 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
6249 {
6250 dwarf2_section_info *section = &section_owner->gdb_index;
6251
6252 if (dwarf2_section_empty_p (section))
6253 return {};
6254
6255 /* Older elfutils strip versions could keep the section in the main
6256 executable while splitting it for the separate debug info file. */
6257 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
6258 return {};
6259
6260 dwarf2_read_section (obj, section);
6261
6262 /* dwarf2_section_info::size is a bfd_size_type, while
6263 gdb::array_view works with size_t. On 32-bit hosts, with
6264 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
6265 is 32-bit. So we need an explicit narrowing conversion here.
6266 This is fine, because it's impossible to allocate or mmap an
6267 array/buffer larger than what size_t can represent. */
6268 return gdb::make_array_view (section->buffer, section->size);
6269 }
6270
6271 /* Lookup the index cache for the contents of the index associated to
6272 DWARF2_OBJ. */
6273
6274 static gdb::array_view<const gdb_byte>
6275 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_objfile *dwarf2_obj)
6276 {
6277 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
6278 if (build_id == nullptr)
6279 return {};
6280
6281 return global_index_cache.lookup_gdb_index (build_id,
6282 &dwarf2_obj->index_cache_res);
6283 }
6284
6285 /* Same as the above, but for DWZ. */
6286
6287 static gdb::array_view<const gdb_byte>
6288 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
6289 {
6290 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
6291 if (build_id == nullptr)
6292 return {};
6293
6294 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
6295 }
6296
6297 /* See symfile.h. */
6298
6299 bool
6300 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
6301 {
6302 struct dwarf2_per_objfile *dwarf2_per_objfile
6303 = get_dwarf2_per_objfile (objfile);
6304
6305 /* If we're about to read full symbols, don't bother with the
6306 indices. In this case we also don't care if some other debug
6307 format is making psymtabs, because they are all about to be
6308 expanded anyway. */
6309 if ((objfile->flags & OBJF_READNOW))
6310 {
6311 dwarf2_per_objfile->using_index = 1;
6312 create_all_comp_units (dwarf2_per_objfile);
6313 create_all_type_units (dwarf2_per_objfile);
6314 dwarf2_per_objfile->quick_file_names_table
6315 = create_quick_file_names_table
6316 (dwarf2_per_objfile->all_comp_units.size ());
6317
6318 for (int i = 0; i < (dwarf2_per_objfile->all_comp_units.size ()
6319 + dwarf2_per_objfile->all_type_units.size ()); ++i)
6320 {
6321 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
6322
6323 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6324 struct dwarf2_per_cu_quick_data);
6325 }
6326
6327 /* Return 1 so that gdb sees the "quick" functions. However,
6328 these functions will be no-ops because we will have expanded
6329 all symtabs. */
6330 *index_kind = dw_index_kind::GDB_INDEX;
6331 return true;
6332 }
6333
6334 if (dwarf2_read_debug_names (dwarf2_per_objfile))
6335 {
6336 *index_kind = dw_index_kind::DEBUG_NAMES;
6337 return true;
6338 }
6339
6340 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
6341 get_gdb_index_contents_from_section<struct dwarf2_per_objfile>,
6342 get_gdb_index_contents_from_section<dwz_file>))
6343 {
6344 *index_kind = dw_index_kind::GDB_INDEX;
6345 return true;
6346 }
6347
6348 /* ... otherwise, try to find the index in the index cache. */
6349 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
6350 get_gdb_index_contents_from_cache,
6351 get_gdb_index_contents_from_cache_dwz))
6352 {
6353 global_index_cache.hit ();
6354 *index_kind = dw_index_kind::GDB_INDEX;
6355 return true;
6356 }
6357
6358 global_index_cache.miss ();
6359 return false;
6360 }
6361
6362 \f
6363
6364 /* Build a partial symbol table. */
6365
6366 void
6367 dwarf2_build_psymtabs (struct objfile *objfile)
6368 {
6369 struct dwarf2_per_objfile *dwarf2_per_objfile
6370 = get_dwarf2_per_objfile (objfile);
6371
6372 init_psymbol_list (objfile, 1024);
6373
6374 try
6375 {
6376 /* This isn't really ideal: all the data we allocate on the
6377 objfile's obstack is still uselessly kept around. However,
6378 freeing it seems unsafe. */
6379 psymtab_discarder psymtabs (objfile);
6380 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
6381 psymtabs.keep ();
6382
6383 /* (maybe) store an index in the cache. */
6384 global_index_cache.store (dwarf2_per_objfile);
6385 }
6386 catch (const gdb_exception_error &except)
6387 {
6388 exception_print (gdb_stderr, except);
6389 }
6390 }
6391
6392 /* Return the total length of the CU described by HEADER. */
6393
6394 static unsigned int
6395 get_cu_length (const struct comp_unit_head *header)
6396 {
6397 return header->initial_length_size + header->length;
6398 }
6399
6400 /* Return TRUE if SECT_OFF is within CU_HEADER. */
6401
6402 static inline bool
6403 offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
6404 {
6405 sect_offset bottom = cu_header->sect_off;
6406 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
6407
6408 return sect_off >= bottom && sect_off < top;
6409 }
6410
6411 /* Find the base address of the compilation unit for range lists and
6412 location lists. It will normally be specified by DW_AT_low_pc.
6413 In DWARF-3 draft 4, the base address could be overridden by
6414 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6415 compilation units with discontinuous ranges. */
6416
6417 static void
6418 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6419 {
6420 struct attribute *attr;
6421
6422 cu->base_known = 0;
6423 cu->base_address = 0;
6424
6425 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6426 if (attr != nullptr)
6427 {
6428 cu->base_address = attr_value_as_address (attr);
6429 cu->base_known = 1;
6430 }
6431 else
6432 {
6433 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6434 if (attr != nullptr)
6435 {
6436 cu->base_address = attr_value_as_address (attr);
6437 cu->base_known = 1;
6438 }
6439 }
6440 }
6441
6442 /* Read in the comp unit header information from the debug_info at info_ptr.
6443 Use rcuh_kind::COMPILE as the default type if not known by the caller.
6444 NOTE: This leaves members offset, first_die_offset to be filled in
6445 by the caller. */
6446
6447 static const gdb_byte *
6448 read_comp_unit_head (struct comp_unit_head *cu_header,
6449 const gdb_byte *info_ptr,
6450 struct dwarf2_section_info *section,
6451 rcuh_kind section_kind)
6452 {
6453 int signed_addr;
6454 unsigned int bytes_read;
6455 const char *filename = get_section_file_name (section);
6456 bfd *abfd = get_section_bfd_owner (section);
6457
6458 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
6459 cu_header->initial_length_size = bytes_read;
6460 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
6461 info_ptr += bytes_read;
6462 cu_header->version = read_2_bytes (abfd, info_ptr);
6463 if (cu_header->version < 2 || cu_header->version > 5)
6464 error (_("Dwarf Error: wrong version in compilation unit header "
6465 "(is %d, should be 2, 3, 4 or 5) [in module %s]"),
6466 cu_header->version, filename);
6467 info_ptr += 2;
6468 if (cu_header->version < 5)
6469 switch (section_kind)
6470 {
6471 case rcuh_kind::COMPILE:
6472 cu_header->unit_type = DW_UT_compile;
6473 break;
6474 case rcuh_kind::TYPE:
6475 cu_header->unit_type = DW_UT_type;
6476 break;
6477 default:
6478 internal_error (__FILE__, __LINE__,
6479 _("read_comp_unit_head: invalid section_kind"));
6480 }
6481 else
6482 {
6483 cu_header->unit_type = static_cast<enum dwarf_unit_type>
6484 (read_1_byte (abfd, info_ptr));
6485 info_ptr += 1;
6486 switch (cu_header->unit_type)
6487 {
6488 case DW_UT_compile:
6489 case DW_UT_partial:
6490 case DW_UT_skeleton:
6491 case DW_UT_split_compile:
6492 if (section_kind != rcuh_kind::COMPILE)
6493 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6494 "(is %s, should be %s) [in module %s]"),
6495 dwarf_unit_type_name (cu_header->unit_type),
6496 dwarf_unit_type_name (DW_UT_type), filename);
6497 break;
6498 case DW_UT_type:
6499 case DW_UT_split_type:
6500 section_kind = rcuh_kind::TYPE;
6501 break;
6502 default:
6503 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6504 "(is %#04x, should be one of: %s, %s, %s, %s or %s) "
6505 "[in module %s]"), cu_header->unit_type,
6506 dwarf_unit_type_name (DW_UT_compile),
6507 dwarf_unit_type_name (DW_UT_skeleton),
6508 dwarf_unit_type_name (DW_UT_split_compile),
6509 dwarf_unit_type_name (DW_UT_type),
6510 dwarf_unit_type_name (DW_UT_split_type), filename);
6511 }
6512
6513 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6514 info_ptr += 1;
6515 }
6516 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
6517 cu_header,
6518 &bytes_read);
6519 info_ptr += bytes_read;
6520 if (cu_header->version < 5)
6521 {
6522 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6523 info_ptr += 1;
6524 }
6525 signed_addr = bfd_get_sign_extend_vma (abfd);
6526 if (signed_addr < 0)
6527 internal_error (__FILE__, __LINE__,
6528 _("read_comp_unit_head: dwarf from non elf file"));
6529 cu_header->signed_addr_p = signed_addr;
6530
6531 bool header_has_signature = section_kind == rcuh_kind::TYPE
6532 || cu_header->unit_type == DW_UT_skeleton
6533 || cu_header->unit_type == DW_UT_split_compile;
6534
6535 if (header_has_signature)
6536 {
6537 cu_header->signature = read_8_bytes (abfd, info_ptr);
6538 info_ptr += 8;
6539 }
6540
6541 if (section_kind == rcuh_kind::TYPE)
6542 {
6543 LONGEST type_offset;
6544 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
6545 info_ptr += bytes_read;
6546 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
6547 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
6548 error (_("Dwarf Error: Too big type_offset in compilation unit "
6549 "header (is %s) [in module %s]"), plongest (type_offset),
6550 filename);
6551 }
6552
6553 return info_ptr;
6554 }
6555
6556 /* Helper function that returns the proper abbrev section for
6557 THIS_CU. */
6558
6559 static struct dwarf2_section_info *
6560 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6561 {
6562 struct dwarf2_section_info *abbrev;
6563 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6564
6565 if (this_cu->is_dwz)
6566 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
6567 else
6568 abbrev = &dwarf2_per_objfile->abbrev;
6569
6570 return abbrev;
6571 }
6572
6573 /* Subroutine of read_and_check_comp_unit_head and
6574 read_and_check_type_unit_head to simplify them.
6575 Perform various error checking on the header. */
6576
6577 static void
6578 error_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6579 struct comp_unit_head *header,
6580 struct dwarf2_section_info *section,
6581 struct dwarf2_section_info *abbrev_section)
6582 {
6583 const char *filename = get_section_file_name (section);
6584
6585 if (to_underlying (header->abbrev_sect_off)
6586 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
6587 error (_("Dwarf Error: bad offset (%s) in compilation unit header "
6588 "(offset %s + 6) [in module %s]"),
6589 sect_offset_str (header->abbrev_sect_off),
6590 sect_offset_str (header->sect_off),
6591 filename);
6592
6593 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
6594 avoid potential 32-bit overflow. */
6595 if (((ULONGEST) header->sect_off + get_cu_length (header))
6596 > section->size)
6597 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
6598 "(offset %s + 0) [in module %s]"),
6599 header->length, sect_offset_str (header->sect_off),
6600 filename);
6601 }
6602
6603 /* Read in a CU/TU header and perform some basic error checking.
6604 The contents of the header are stored in HEADER.
6605 The result is a pointer to the start of the first DIE. */
6606
6607 static const gdb_byte *
6608 read_and_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6609 struct comp_unit_head *header,
6610 struct dwarf2_section_info *section,
6611 struct dwarf2_section_info *abbrev_section,
6612 const gdb_byte *info_ptr,
6613 rcuh_kind section_kind)
6614 {
6615 const gdb_byte *beg_of_comp_unit = info_ptr;
6616
6617 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
6618
6619 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
6620
6621 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
6622
6623 error_check_comp_unit_head (dwarf2_per_objfile, header, section,
6624 abbrev_section);
6625
6626 return info_ptr;
6627 }
6628
6629 /* Fetch the abbreviation table offset from a comp or type unit header. */
6630
6631 static sect_offset
6632 read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6633 struct dwarf2_section_info *section,
6634 sect_offset sect_off)
6635 {
6636 bfd *abfd = get_section_bfd_owner (section);
6637 const gdb_byte *info_ptr;
6638 unsigned int initial_length_size, offset_size;
6639 uint16_t version;
6640
6641 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
6642 info_ptr = section->buffer + to_underlying (sect_off);
6643 read_initial_length (abfd, info_ptr, &initial_length_size);
6644 offset_size = initial_length_size == 4 ? 4 : 8;
6645 info_ptr += initial_length_size;
6646
6647 version = read_2_bytes (abfd, info_ptr);
6648 info_ptr += 2;
6649 if (version >= 5)
6650 {
6651 /* Skip unit type and address size. */
6652 info_ptr += 2;
6653 }
6654
6655 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
6656 }
6657
6658 /* Allocate a new partial symtab for file named NAME and mark this new
6659 partial symtab as being an include of PST. */
6660
6661 static void
6662 dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
6663 struct objfile *objfile)
6664 {
6665 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
6666
6667 if (!IS_ABSOLUTE_PATH (subpst->filename))
6668 {
6669 /* It shares objfile->objfile_obstack. */
6670 subpst->dirname = pst->dirname;
6671 }
6672
6673 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
6674 subpst->dependencies[0] = pst;
6675 subpst->number_of_dependencies = 1;
6676
6677 subpst->read_symtab = pst->read_symtab;
6678
6679 /* No private part is necessary for include psymtabs. This property
6680 can be used to differentiate between such include psymtabs and
6681 the regular ones. */
6682 subpst->read_symtab_private = NULL;
6683 }
6684
6685 /* Read the Line Number Program data and extract the list of files
6686 included by the source file represented by PST. Build an include
6687 partial symtab for each of these included files. */
6688
6689 static void
6690 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
6691 struct die_info *die,
6692 struct partial_symtab *pst)
6693 {
6694 line_header_up lh;
6695 struct attribute *attr;
6696
6697 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6698 if (attr != nullptr)
6699 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
6700 if (lh == NULL)
6701 return; /* No linetable, so no includes. */
6702
6703 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6704 that we pass in the raw text_low here; that is ok because we're
6705 only decoding the line table to make include partial symtabs, and
6706 so the addresses aren't really used. */
6707 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
6708 pst->raw_text_low (), 1);
6709 }
6710
6711 static hashval_t
6712 hash_signatured_type (const void *item)
6713 {
6714 const struct signatured_type *sig_type
6715 = (const struct signatured_type *) item;
6716
6717 /* This drops the top 32 bits of the signature, but is ok for a hash. */
6718 return sig_type->signature;
6719 }
6720
6721 static int
6722 eq_signatured_type (const void *item_lhs, const void *item_rhs)
6723 {
6724 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6725 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
6726
6727 return lhs->signature == rhs->signature;
6728 }
6729
6730 /* Allocate a hash table for signatured types. */
6731
6732 static htab_t
6733 allocate_signatured_type_table (struct objfile *objfile)
6734 {
6735 return htab_create_alloc_ex (41,
6736 hash_signatured_type,
6737 eq_signatured_type,
6738 NULL,
6739 &objfile->objfile_obstack,
6740 hashtab_obstack_allocate,
6741 dummy_obstack_deallocate);
6742 }
6743
6744 /* A helper function to add a signatured type CU to a table. */
6745
6746 static int
6747 add_signatured_type_cu_to_table (void **slot, void *datum)
6748 {
6749 struct signatured_type *sigt = (struct signatured_type *) *slot;
6750 std::vector<signatured_type *> *all_type_units
6751 = (std::vector<signatured_type *> *) datum;
6752
6753 all_type_units->push_back (sigt);
6754
6755 return 1;
6756 }
6757
6758 /* A helper for create_debug_types_hash_table. Read types from SECTION
6759 and fill them into TYPES_HTAB. It will process only type units,
6760 therefore DW_UT_type. */
6761
6762 static void
6763 create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6764 struct dwo_file *dwo_file,
6765 dwarf2_section_info *section, htab_t &types_htab,
6766 rcuh_kind section_kind)
6767 {
6768 struct objfile *objfile = dwarf2_per_objfile->objfile;
6769 struct dwarf2_section_info *abbrev_section;
6770 bfd *abfd;
6771 const gdb_byte *info_ptr, *end_ptr;
6772
6773 abbrev_section = (dwo_file != NULL
6774 ? &dwo_file->sections.abbrev
6775 : &dwarf2_per_objfile->abbrev);
6776
6777 if (dwarf_read_debug)
6778 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6779 get_section_name (section),
6780 get_section_file_name (abbrev_section));
6781
6782 dwarf2_read_section (objfile, section);
6783 info_ptr = section->buffer;
6784
6785 if (info_ptr == NULL)
6786 return;
6787
6788 /* We can't set abfd until now because the section may be empty or
6789 not present, in which case the bfd is unknown. */
6790 abfd = get_section_bfd_owner (section);
6791
6792 /* We don't use init_cutu_and_read_dies_simple, or some such, here
6793 because we don't need to read any dies: the signature is in the
6794 header. */
6795
6796 end_ptr = info_ptr + section->size;
6797 while (info_ptr < end_ptr)
6798 {
6799 struct signatured_type *sig_type;
6800 struct dwo_unit *dwo_tu;
6801 void **slot;
6802 const gdb_byte *ptr = info_ptr;
6803 struct comp_unit_head header;
6804 unsigned int length;
6805
6806 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6807
6808 /* Initialize it due to a false compiler warning. */
6809 header.signature = -1;
6810 header.type_cu_offset_in_tu = (cu_offset) -1;
6811
6812 /* We need to read the type's signature in order to build the hash
6813 table, but we don't need anything else just yet. */
6814
6815 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
6816 abbrev_section, ptr, section_kind);
6817
6818 length = get_cu_length (&header);
6819
6820 /* Skip dummy type units. */
6821 if (ptr >= info_ptr + length
6822 || peek_abbrev_code (abfd, ptr) == 0
6823 || header.unit_type != DW_UT_type)
6824 {
6825 info_ptr += length;
6826 continue;
6827 }
6828
6829 if (types_htab == NULL)
6830 {
6831 if (dwo_file)
6832 types_htab = allocate_dwo_unit_table (objfile);
6833 else
6834 types_htab = allocate_signatured_type_table (objfile);
6835 }
6836
6837 if (dwo_file)
6838 {
6839 sig_type = NULL;
6840 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6841 struct dwo_unit);
6842 dwo_tu->dwo_file = dwo_file;
6843 dwo_tu->signature = header.signature;
6844 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6845 dwo_tu->section = section;
6846 dwo_tu->sect_off = sect_off;
6847 dwo_tu->length = length;
6848 }
6849 else
6850 {
6851 /* N.B.: type_offset is not usable if this type uses a DWO file.
6852 The real type_offset is in the DWO file. */
6853 dwo_tu = NULL;
6854 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6855 struct signatured_type);
6856 sig_type->signature = header.signature;
6857 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6858 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6859 sig_type->per_cu.is_debug_types = 1;
6860 sig_type->per_cu.section = section;
6861 sig_type->per_cu.sect_off = sect_off;
6862 sig_type->per_cu.length = length;
6863 }
6864
6865 slot = htab_find_slot (types_htab,
6866 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6867 INSERT);
6868 gdb_assert (slot != NULL);
6869 if (*slot != NULL)
6870 {
6871 sect_offset dup_sect_off;
6872
6873 if (dwo_file)
6874 {
6875 const struct dwo_unit *dup_tu
6876 = (const struct dwo_unit *) *slot;
6877
6878 dup_sect_off = dup_tu->sect_off;
6879 }
6880 else
6881 {
6882 const struct signatured_type *dup_tu
6883 = (const struct signatured_type *) *slot;
6884
6885 dup_sect_off = dup_tu->per_cu.sect_off;
6886 }
6887
6888 complaint (_("debug type entry at offset %s is duplicate to"
6889 " the entry at offset %s, signature %s"),
6890 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
6891 hex_string (header.signature));
6892 }
6893 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
6894
6895 if (dwarf_read_debug > 1)
6896 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6897 sect_offset_str (sect_off),
6898 hex_string (header.signature));
6899
6900 info_ptr += length;
6901 }
6902 }
6903
6904 /* Create the hash table of all entries in the .debug_types
6905 (or .debug_types.dwo) section(s).
6906 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6907 otherwise it is NULL.
6908
6909 The result is a pointer to the hash table or NULL if there are no types.
6910
6911 Note: This function processes DWO files only, not DWP files. */
6912
6913 static void
6914 create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6915 struct dwo_file *dwo_file,
6916 gdb::array_view<dwarf2_section_info> type_sections,
6917 htab_t &types_htab)
6918 {
6919 for (dwarf2_section_info &section : type_sections)
6920 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
6921 types_htab, rcuh_kind::TYPE);
6922 }
6923
6924 /* Create the hash table of all entries in the .debug_types section,
6925 and initialize all_type_units.
6926 The result is zero if there is an error (e.g. missing .debug_types section),
6927 otherwise non-zero. */
6928
6929 static int
6930 create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6931 {
6932 htab_t types_htab = NULL;
6933
6934 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6935 &dwarf2_per_objfile->info, types_htab,
6936 rcuh_kind::COMPILE);
6937 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6938 dwarf2_per_objfile->types, types_htab);
6939 if (types_htab == NULL)
6940 {
6941 dwarf2_per_objfile->signatured_types = NULL;
6942 return 0;
6943 }
6944
6945 dwarf2_per_objfile->signatured_types = types_htab;
6946
6947 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
6948 dwarf2_per_objfile->all_type_units.reserve (htab_elements (types_htab));
6949
6950 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table,
6951 &dwarf2_per_objfile->all_type_units);
6952
6953 return 1;
6954 }
6955
6956 /* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6957 If SLOT is non-NULL, it is the entry to use in the hash table.
6958 Otherwise we find one. */
6959
6960 static struct signatured_type *
6961 add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6962 void **slot)
6963 {
6964 struct objfile *objfile = dwarf2_per_objfile->objfile;
6965
6966 if (dwarf2_per_objfile->all_type_units.size ()
6967 == dwarf2_per_objfile->all_type_units.capacity ())
6968 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
6969
6970 signatured_type *sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6971 struct signatured_type);
6972
6973 dwarf2_per_objfile->all_type_units.push_back (sig_type);
6974 sig_type->signature = sig;
6975 sig_type->per_cu.is_debug_types = 1;
6976 if (dwarf2_per_objfile->using_index)
6977 {
6978 sig_type->per_cu.v.quick =
6979 OBSTACK_ZALLOC (&objfile->objfile_obstack,
6980 struct dwarf2_per_cu_quick_data);
6981 }
6982
6983 if (slot == NULL)
6984 {
6985 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
6986 sig_type, INSERT);
6987 }
6988 gdb_assert (*slot == NULL);
6989 *slot = sig_type;
6990 /* The rest of sig_type must be filled in by the caller. */
6991 return sig_type;
6992 }
6993
6994 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6995 Fill in SIG_ENTRY with DWO_ENTRY. */
6996
6997 static void
6998 fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
6999 struct signatured_type *sig_entry,
7000 struct dwo_unit *dwo_entry)
7001 {
7002 /* Make sure we're not clobbering something we don't expect to. */
7003 gdb_assert (! sig_entry->per_cu.queued);
7004 gdb_assert (sig_entry->per_cu.cu == NULL);
7005 if (dwarf2_per_objfile->using_index)
7006 {
7007 gdb_assert (sig_entry->per_cu.v.quick != NULL);
7008 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
7009 }
7010 else
7011 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
7012 gdb_assert (sig_entry->signature == dwo_entry->signature);
7013 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
7014 gdb_assert (sig_entry->type_unit_group == NULL);
7015 gdb_assert (sig_entry->dwo_unit == NULL);
7016
7017 sig_entry->per_cu.section = dwo_entry->section;
7018 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7019 sig_entry->per_cu.length = dwo_entry->length;
7020 sig_entry->per_cu.reading_dwo_directly = 1;
7021 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
7022 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
7023 sig_entry->dwo_unit = dwo_entry;
7024 }
7025
7026 /* Subroutine of lookup_signatured_type.
7027 If we haven't read the TU yet, create the signatured_type data structure
7028 for a TU to be read in directly from a DWO file, bypassing the stub.
7029 This is the "Stay in DWO Optimization": When there is no DWP file and we're
7030 using .gdb_index, then when reading a CU we want to stay in the DWO file
7031 containing that CU. Otherwise we could end up reading several other DWO
7032 files (due to comdat folding) to process the transitive closure of all the
7033 mentioned TUs, and that can be slow. The current DWO file will have every
7034 type signature that it needs.
7035 We only do this for .gdb_index because in the psymtab case we already have
7036 to read all the DWOs to build the type unit groups. */
7037
7038 static struct signatured_type *
7039 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7040 {
7041 struct dwarf2_per_objfile *dwarf2_per_objfile
7042 = cu->per_cu->dwarf2_per_objfile;
7043 struct objfile *objfile = dwarf2_per_objfile->objfile;
7044 struct dwo_file *dwo_file;
7045 struct dwo_unit find_dwo_entry, *dwo_entry;
7046 struct signatured_type find_sig_entry, *sig_entry;
7047 void **slot;
7048
7049 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7050
7051 /* If TU skeletons have been removed then we may not have read in any
7052 TUs yet. */
7053 if (dwarf2_per_objfile->signatured_types == NULL)
7054 {
7055 dwarf2_per_objfile->signatured_types
7056 = allocate_signatured_type_table (objfile);
7057 }
7058
7059 /* We only ever need to read in one copy of a signatured type.
7060 Use the global signatured_types array to do our own comdat-folding
7061 of types. If this is the first time we're reading this TU, and
7062 the TU has an entry in .gdb_index, replace the recorded data from
7063 .gdb_index with this TU. */
7064
7065 find_sig_entry.signature = sig;
7066 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7067 &find_sig_entry, INSERT);
7068 sig_entry = (struct signatured_type *) *slot;
7069
7070 /* We can get here with the TU already read, *or* in the process of being
7071 read. Don't reassign the global entry to point to this DWO if that's
7072 the case. Also note that if the TU is already being read, it may not
7073 have come from a DWO, the program may be a mix of Fission-compiled
7074 code and non-Fission-compiled code. */
7075
7076 /* Have we already tried to read this TU?
7077 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7078 needn't exist in the global table yet). */
7079 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
7080 return sig_entry;
7081
7082 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
7083 dwo_unit of the TU itself. */
7084 dwo_file = cu->dwo_unit->dwo_file;
7085
7086 /* Ok, this is the first time we're reading this TU. */
7087 if (dwo_file->tus == NULL)
7088 return NULL;
7089 find_dwo_entry.signature = sig;
7090 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
7091 if (dwo_entry == NULL)
7092 return NULL;
7093
7094 /* If the global table doesn't have an entry for this TU, add one. */
7095 if (sig_entry == NULL)
7096 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
7097
7098 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
7099 sig_entry->per_cu.tu_read = 1;
7100 return sig_entry;
7101 }
7102
7103 /* Subroutine of lookup_signatured_type.
7104 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
7105 then try the DWP file. If the TU stub (skeleton) has been removed then
7106 it won't be in .gdb_index. */
7107
7108 static struct signatured_type *
7109 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7110 {
7111 struct dwarf2_per_objfile *dwarf2_per_objfile
7112 = cu->per_cu->dwarf2_per_objfile;
7113 struct objfile *objfile = dwarf2_per_objfile->objfile;
7114 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
7115 struct dwo_unit *dwo_entry;
7116 struct signatured_type find_sig_entry, *sig_entry;
7117 void **slot;
7118
7119 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7120 gdb_assert (dwp_file != NULL);
7121
7122 /* If TU skeletons have been removed then we may not have read in any
7123 TUs yet. */
7124 if (dwarf2_per_objfile->signatured_types == NULL)
7125 {
7126 dwarf2_per_objfile->signatured_types
7127 = allocate_signatured_type_table (objfile);
7128 }
7129
7130 find_sig_entry.signature = sig;
7131 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7132 &find_sig_entry, INSERT);
7133 sig_entry = (struct signatured_type *) *slot;
7134
7135 /* Have we already tried to read this TU?
7136 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7137 needn't exist in the global table yet). */
7138 if (sig_entry != NULL)
7139 return sig_entry;
7140
7141 if (dwp_file->tus == NULL)
7142 return NULL;
7143 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
7144 sig, 1 /* is_debug_types */);
7145 if (dwo_entry == NULL)
7146 return NULL;
7147
7148 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
7149 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
7150
7151 return sig_entry;
7152 }
7153
7154 /* Lookup a signature based type for DW_FORM_ref_sig8.
7155 Returns NULL if signature SIG is not present in the table.
7156 It is up to the caller to complain about this. */
7157
7158 static struct signatured_type *
7159 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7160 {
7161 struct dwarf2_per_objfile *dwarf2_per_objfile
7162 = cu->per_cu->dwarf2_per_objfile;
7163
7164 if (cu->dwo_unit
7165 && dwarf2_per_objfile->using_index)
7166 {
7167 /* We're in a DWO/DWP file, and we're using .gdb_index.
7168 These cases require special processing. */
7169 if (get_dwp_file (dwarf2_per_objfile) == NULL)
7170 return lookup_dwo_signatured_type (cu, sig);
7171 else
7172 return lookup_dwp_signatured_type (cu, sig);
7173 }
7174 else
7175 {
7176 struct signatured_type find_entry, *entry;
7177
7178 if (dwarf2_per_objfile->signatured_types == NULL)
7179 return NULL;
7180 find_entry.signature = sig;
7181 entry = ((struct signatured_type *)
7182 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
7183 return entry;
7184 }
7185 }
7186 \f
7187 /* Low level DIE reading support. */
7188
7189 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
7190
7191 static void
7192 init_cu_die_reader (struct die_reader_specs *reader,
7193 struct dwarf2_cu *cu,
7194 struct dwarf2_section_info *section,
7195 struct dwo_file *dwo_file,
7196 struct abbrev_table *abbrev_table)
7197 {
7198 gdb_assert (section->readin && section->buffer != NULL);
7199 reader->abfd = get_section_bfd_owner (section);
7200 reader->cu = cu;
7201 reader->dwo_file = dwo_file;
7202 reader->die_section = section;
7203 reader->buffer = section->buffer;
7204 reader->buffer_end = section->buffer + section->size;
7205 reader->comp_dir = NULL;
7206 reader->abbrev_table = abbrev_table;
7207 }
7208
7209 /* Subroutine of init_cutu_and_read_dies to simplify it.
7210 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
7211 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
7212 already.
7213
7214 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
7215 from it to the DIE in the DWO. If NULL we are skipping the stub.
7216 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
7217 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
7218 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
7219 STUB_COMP_DIR may be non-NULL.
7220 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
7221 are filled in with the info of the DIE from the DWO file.
7222 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
7223 from the dwo. Since *RESULT_READER references this abbrev table, it must be
7224 kept around for at least as long as *RESULT_READER.
7225
7226 The result is non-zero if a valid (non-dummy) DIE was found. */
7227
7228 static int
7229 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
7230 struct dwo_unit *dwo_unit,
7231 struct die_info *stub_comp_unit_die,
7232 const char *stub_comp_dir,
7233 struct die_reader_specs *result_reader,
7234 const gdb_byte **result_info_ptr,
7235 struct die_info **result_comp_unit_die,
7236 int *result_has_children,
7237 abbrev_table_up *result_dwo_abbrev_table)
7238 {
7239 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7240 struct objfile *objfile = dwarf2_per_objfile->objfile;
7241 struct dwarf2_cu *cu = this_cu->cu;
7242 bfd *abfd;
7243 const gdb_byte *begin_info_ptr, *info_ptr;
7244 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
7245 int i,num_extra_attrs;
7246 struct dwarf2_section_info *dwo_abbrev_section;
7247 struct attribute *attr;
7248 struct die_info *comp_unit_die;
7249
7250 /* At most one of these may be provided. */
7251 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
7252
7253 /* These attributes aren't processed until later:
7254 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
7255 DW_AT_comp_dir is used now, to find the DWO file, but it is also
7256 referenced later. However, these attributes are found in the stub
7257 which we won't have later. In order to not impose this complication
7258 on the rest of the code, we read them here and copy them to the
7259 DWO CU/TU die. */
7260
7261 stmt_list = NULL;
7262 low_pc = NULL;
7263 high_pc = NULL;
7264 ranges = NULL;
7265 comp_dir = NULL;
7266
7267 if (stub_comp_unit_die != NULL)
7268 {
7269 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
7270 DWO file. */
7271 if (! this_cu->is_debug_types)
7272 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
7273 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
7274 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
7275 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
7276 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
7277
7278 /* There should be a DW_AT_addr_base attribute here (if needed).
7279 We need the value before we can process DW_FORM_GNU_addr_index
7280 or DW_FORM_addrx. */
7281 cu->addr_base = 0;
7282 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
7283 if (attr != nullptr)
7284 cu->addr_base = DW_UNSND (attr);
7285
7286 /* There should be a DW_AT_ranges_base attribute here (if needed).
7287 We need the value before we can process DW_AT_ranges. */
7288 cu->ranges_base = 0;
7289 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
7290 if (attr != nullptr)
7291 cu->ranges_base = DW_UNSND (attr);
7292 }
7293 else if (stub_comp_dir != NULL)
7294 {
7295 /* Reconstruct the comp_dir attribute to simplify the code below. */
7296 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
7297 comp_dir->name = DW_AT_comp_dir;
7298 comp_dir->form = DW_FORM_string;
7299 DW_STRING_IS_CANONICAL (comp_dir) = 0;
7300 DW_STRING (comp_dir) = stub_comp_dir;
7301 }
7302
7303 /* Set up for reading the DWO CU/TU. */
7304 cu->dwo_unit = dwo_unit;
7305 dwarf2_section_info *section = dwo_unit->section;
7306 dwarf2_read_section (objfile, section);
7307 abfd = get_section_bfd_owner (section);
7308 begin_info_ptr = info_ptr = (section->buffer
7309 + to_underlying (dwo_unit->sect_off));
7310 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
7311
7312 if (this_cu->is_debug_types)
7313 {
7314 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
7315
7316 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7317 &cu->header, section,
7318 dwo_abbrev_section,
7319 info_ptr, rcuh_kind::TYPE);
7320 /* This is not an assert because it can be caused by bad debug info. */
7321 if (sig_type->signature != cu->header.signature)
7322 {
7323 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
7324 " TU at offset %s [in module %s]"),
7325 hex_string (sig_type->signature),
7326 hex_string (cu->header.signature),
7327 sect_offset_str (dwo_unit->sect_off),
7328 bfd_get_filename (abfd));
7329 }
7330 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7331 /* For DWOs coming from DWP files, we don't know the CU length
7332 nor the type's offset in the TU until now. */
7333 dwo_unit->length = get_cu_length (&cu->header);
7334 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
7335
7336 /* Establish the type offset that can be used to lookup the type.
7337 For DWO files, we don't know it until now. */
7338 sig_type->type_offset_in_section
7339 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
7340 }
7341 else
7342 {
7343 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7344 &cu->header, section,
7345 dwo_abbrev_section,
7346 info_ptr, rcuh_kind::COMPILE);
7347 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7348 /* For DWOs coming from DWP files, we don't know the CU length
7349 until now. */
7350 dwo_unit->length = get_cu_length (&cu->header);
7351 }
7352
7353 *result_dwo_abbrev_table
7354 = abbrev_table_read_table (dwarf2_per_objfile, dwo_abbrev_section,
7355 cu->header.abbrev_sect_off);
7356 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
7357 result_dwo_abbrev_table->get ());
7358
7359 /* Read in the die, but leave space to copy over the attributes
7360 from the stub. This has the benefit of simplifying the rest of
7361 the code - all the work to maintain the illusion of a single
7362 DW_TAG_{compile,type}_unit DIE is done here. */
7363 num_extra_attrs = ((stmt_list != NULL)
7364 + (low_pc != NULL)
7365 + (high_pc != NULL)
7366 + (ranges != NULL)
7367 + (comp_dir != NULL));
7368 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
7369 result_has_children, num_extra_attrs);
7370
7371 /* Copy over the attributes from the stub to the DIE we just read in. */
7372 comp_unit_die = *result_comp_unit_die;
7373 i = comp_unit_die->num_attrs;
7374 if (stmt_list != NULL)
7375 comp_unit_die->attrs[i++] = *stmt_list;
7376 if (low_pc != NULL)
7377 comp_unit_die->attrs[i++] = *low_pc;
7378 if (high_pc != NULL)
7379 comp_unit_die->attrs[i++] = *high_pc;
7380 if (ranges != NULL)
7381 comp_unit_die->attrs[i++] = *ranges;
7382 if (comp_dir != NULL)
7383 comp_unit_die->attrs[i++] = *comp_dir;
7384 comp_unit_die->num_attrs += num_extra_attrs;
7385
7386 if (dwarf_die_debug)
7387 {
7388 fprintf_unfiltered (gdb_stdlog,
7389 "Read die from %s@0x%x of %s:\n",
7390 get_section_name (section),
7391 (unsigned) (begin_info_ptr - section->buffer),
7392 bfd_get_filename (abfd));
7393 dump_die (comp_unit_die, dwarf_die_debug);
7394 }
7395
7396 /* Save the comp_dir attribute. If there is no DWP file then we'll read
7397 TUs by skipping the stub and going directly to the entry in the DWO file.
7398 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
7399 to get it via circuitous means. Blech. */
7400 if (comp_dir != NULL)
7401 result_reader->comp_dir = DW_STRING (comp_dir);
7402
7403 /* Skip dummy compilation units. */
7404 if (info_ptr >= begin_info_ptr + dwo_unit->length
7405 || peek_abbrev_code (abfd, info_ptr) == 0)
7406 return 0;
7407
7408 *result_info_ptr = info_ptr;
7409 return 1;
7410 }
7411
7412 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
7413 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
7414 signature is part of the header. */
7415 static gdb::optional<ULONGEST>
7416 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
7417 {
7418 if (cu->header.version >= 5)
7419 return cu->header.signature;
7420 struct attribute *attr;
7421 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7422 if (attr == nullptr)
7423 return gdb::optional<ULONGEST> ();
7424 return DW_UNSND (attr);
7425 }
7426
7427 /* Subroutine of init_cutu_and_read_dies to simplify it.
7428 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
7429 Returns NULL if the specified DWO unit cannot be found. */
7430
7431 static struct dwo_unit *
7432 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
7433 struct die_info *comp_unit_die)
7434 {
7435 struct dwarf2_cu *cu = this_cu->cu;
7436 struct dwo_unit *dwo_unit;
7437 const char *comp_dir, *dwo_name;
7438
7439 gdb_assert (cu != NULL);
7440
7441 /* Yeah, we look dwo_name up again, but it simplifies the code. */
7442 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7443 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7444
7445 if (this_cu->is_debug_types)
7446 {
7447 struct signatured_type *sig_type;
7448
7449 /* Since this_cu is the first member of struct signatured_type,
7450 we can go from a pointer to one to a pointer to the other. */
7451 sig_type = (struct signatured_type *) this_cu;
7452 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
7453 }
7454 else
7455 {
7456 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
7457 if (!signature.has_value ())
7458 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7459 " [in module %s]"),
7460 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
7461 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
7462 *signature);
7463 }
7464
7465 return dwo_unit;
7466 }
7467
7468 /* Subroutine of init_cutu_and_read_dies to simplify it.
7469 See it for a description of the parameters.
7470 Read a TU directly from a DWO file, bypassing the stub. */
7471
7472 static void
7473 init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
7474 int use_existing_cu, int keep,
7475 die_reader_func_ftype *die_reader_func,
7476 void *data)
7477 {
7478 std::unique_ptr<dwarf2_cu> new_cu;
7479 struct signatured_type *sig_type;
7480 struct die_reader_specs reader;
7481 const gdb_byte *info_ptr;
7482 struct die_info *comp_unit_die;
7483 int has_children;
7484 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7485
7486 /* Verify we can do the following downcast, and that we have the
7487 data we need. */
7488 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7489 sig_type = (struct signatured_type *) this_cu;
7490 gdb_assert (sig_type->dwo_unit != NULL);
7491
7492 if (use_existing_cu && this_cu->cu != NULL)
7493 {
7494 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
7495 /* There's no need to do the rereading_dwo_cu handling that
7496 init_cutu_and_read_dies does since we don't read the stub. */
7497 }
7498 else
7499 {
7500 /* If !use_existing_cu, this_cu->cu must be NULL. */
7501 gdb_assert (this_cu->cu == NULL);
7502 new_cu.reset (new dwarf2_cu (this_cu));
7503 }
7504
7505 /* A future optimization, if needed, would be to use an existing
7506 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7507 could share abbrev tables. */
7508
7509 /* The abbreviation table used by READER, this must live at least as long as
7510 READER. */
7511 abbrev_table_up dwo_abbrev_table;
7512
7513 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
7514 NULL /* stub_comp_unit_die */,
7515 sig_type->dwo_unit->dwo_file->comp_dir,
7516 &reader, &info_ptr,
7517 &comp_unit_die, &has_children,
7518 &dwo_abbrev_table) == 0)
7519 {
7520 /* Dummy die. */
7521 return;
7522 }
7523
7524 /* All the "real" work is done here. */
7525 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7526
7527 /* This duplicates the code in init_cutu_and_read_dies,
7528 but the alternative is making the latter more complex.
7529 This function is only for the special case of using DWO files directly:
7530 no point in overly complicating the general case just to handle this. */
7531 if (new_cu != NULL && keep)
7532 {
7533 /* Link this CU into read_in_chain. */
7534 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7535 dwarf2_per_objfile->read_in_chain = this_cu;
7536 /* The chain owns it now. */
7537 new_cu.release ();
7538 }
7539 }
7540
7541 /* Initialize a CU (or TU) and read its DIEs.
7542 If the CU defers to a DWO file, read the DWO file as well.
7543
7544 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7545 Otherwise the table specified in the comp unit header is read in and used.
7546 This is an optimization for when we already have the abbrev table.
7547
7548 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
7549 Otherwise, a new CU is allocated with xmalloc.
7550
7551 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
7552 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
7553
7554 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
7555 linker) then DIE_READER_FUNC will not get called. */
7556
7557 static void
7558 init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
7559 struct abbrev_table *abbrev_table,
7560 int use_existing_cu, int keep,
7561 bool skip_partial,
7562 die_reader_func_ftype *die_reader_func,
7563 void *data)
7564 {
7565 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7566 struct objfile *objfile = dwarf2_per_objfile->objfile;
7567 struct dwarf2_section_info *section = this_cu->section;
7568 bfd *abfd = get_section_bfd_owner (section);
7569 struct dwarf2_cu *cu;
7570 const gdb_byte *begin_info_ptr, *info_ptr;
7571 struct die_reader_specs reader;
7572 struct die_info *comp_unit_die;
7573 int has_children;
7574 struct signatured_type *sig_type = NULL;
7575 struct dwarf2_section_info *abbrev_section;
7576 /* Non-zero if CU currently points to a DWO file and we need to
7577 reread it. When this happens we need to reread the skeleton die
7578 before we can reread the DWO file (this only applies to CUs, not TUs). */
7579 int rereading_dwo_cu = 0;
7580
7581 if (dwarf_die_debug)
7582 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7583 this_cu->is_debug_types ? "type" : "comp",
7584 sect_offset_str (this_cu->sect_off));
7585
7586 if (use_existing_cu)
7587 gdb_assert (keep);
7588
7589 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7590 file (instead of going through the stub), short-circuit all of this. */
7591 if (this_cu->reading_dwo_directly)
7592 {
7593 /* Narrow down the scope of possibilities to have to understand. */
7594 gdb_assert (this_cu->is_debug_types);
7595 gdb_assert (abbrev_table == NULL);
7596 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
7597 die_reader_func, data);
7598 return;
7599 }
7600
7601 /* This is cheap if the section is already read in. */
7602 dwarf2_read_section (objfile, section);
7603
7604 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7605
7606 abbrev_section = get_abbrev_section_for_cu (this_cu);
7607
7608 std::unique_ptr<dwarf2_cu> new_cu;
7609 if (use_existing_cu && this_cu->cu != NULL)
7610 {
7611 cu = this_cu->cu;
7612 /* If this CU is from a DWO file we need to start over, we need to
7613 refetch the attributes from the skeleton CU.
7614 This could be optimized by retrieving those attributes from when we
7615 were here the first time: the previous comp_unit_die was stored in
7616 comp_unit_obstack. But there's no data yet that we need this
7617 optimization. */
7618 if (cu->dwo_unit != NULL)
7619 rereading_dwo_cu = 1;
7620 }
7621 else
7622 {
7623 /* If !use_existing_cu, this_cu->cu must be NULL. */
7624 gdb_assert (this_cu->cu == NULL);
7625 new_cu.reset (new dwarf2_cu (this_cu));
7626 cu = new_cu.get ();
7627 }
7628
7629 /* Get the header. */
7630 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
7631 {
7632 /* We already have the header, there's no need to read it in again. */
7633 info_ptr += to_underlying (cu->header.first_die_cu_offset);
7634 }
7635 else
7636 {
7637 if (this_cu->is_debug_types)
7638 {
7639 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7640 &cu->header, section,
7641 abbrev_section, info_ptr,
7642 rcuh_kind::TYPE);
7643
7644 /* Since per_cu is the first member of struct signatured_type,
7645 we can go from a pointer to one to a pointer to the other. */
7646 sig_type = (struct signatured_type *) this_cu;
7647 gdb_assert (sig_type->signature == cu->header.signature);
7648 gdb_assert (sig_type->type_offset_in_tu
7649 == cu->header.type_cu_offset_in_tu);
7650 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7651
7652 /* LENGTH has not been set yet for type units if we're
7653 using .gdb_index. */
7654 this_cu->length = get_cu_length (&cu->header);
7655
7656 /* Establish the type offset that can be used to lookup the type. */
7657 sig_type->type_offset_in_section =
7658 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
7659
7660 this_cu->dwarf_version = cu->header.version;
7661 }
7662 else
7663 {
7664 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7665 &cu->header, section,
7666 abbrev_section,
7667 info_ptr,
7668 rcuh_kind::COMPILE);
7669
7670 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7671 gdb_assert (this_cu->length == get_cu_length (&cu->header));
7672 this_cu->dwarf_version = cu->header.version;
7673 }
7674 }
7675
7676 /* Skip dummy compilation units. */
7677 if (info_ptr >= begin_info_ptr + this_cu->length
7678 || peek_abbrev_code (abfd, info_ptr) == 0)
7679 return;
7680
7681 /* If we don't have them yet, read the abbrevs for this compilation unit.
7682 And if we need to read them now, make sure they're freed when we're
7683 done (own the table through ABBREV_TABLE_HOLDER). */
7684 abbrev_table_up abbrev_table_holder;
7685 if (abbrev_table != NULL)
7686 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7687 else
7688 {
7689 abbrev_table_holder
7690 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7691 cu->header.abbrev_sect_off);
7692 abbrev_table = abbrev_table_holder.get ();
7693 }
7694
7695 /* Read the top level CU/TU die. */
7696 init_cu_die_reader (&reader, cu, section, NULL, abbrev_table);
7697 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
7698
7699 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
7700 return;
7701
7702 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
7703 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7704 table from the DWO file and pass the ownership over to us. It will be
7705 referenced from READER, so we must make sure to free it after we're done
7706 with READER.
7707
7708 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7709 DWO CU, that this test will fail (the attribute will not be present). */
7710 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7711 abbrev_table_up dwo_abbrev_table;
7712 if (dwo_name != nullptr)
7713 {
7714 struct dwo_unit *dwo_unit;
7715 struct die_info *dwo_comp_unit_die;
7716
7717 if (has_children)
7718 {
7719 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
7720 " has children (offset %s) [in module %s]"),
7721 sect_offset_str (this_cu->sect_off),
7722 bfd_get_filename (abfd));
7723 }
7724 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
7725 if (dwo_unit != NULL)
7726 {
7727 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
7728 comp_unit_die, NULL,
7729 &reader, &info_ptr,
7730 &dwo_comp_unit_die, &has_children,
7731 &dwo_abbrev_table) == 0)
7732 {
7733 /* Dummy die. */
7734 return;
7735 }
7736 comp_unit_die = dwo_comp_unit_die;
7737 }
7738 else
7739 {
7740 /* Yikes, we couldn't find the rest of the DIE, we only have
7741 the stub. A complaint has already been logged. There's
7742 not much more we can do except pass on the stub DIE to
7743 die_reader_func. We don't want to throw an error on bad
7744 debug info. */
7745 }
7746 }
7747
7748 /* All of the above is setup for this call. Yikes. */
7749 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7750
7751 /* Done, clean up. */
7752 if (new_cu != NULL && keep)
7753 {
7754 /* Link this CU into read_in_chain. */
7755 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7756 dwarf2_per_objfile->read_in_chain = this_cu;
7757 /* The chain owns it now. */
7758 new_cu.release ();
7759 }
7760 }
7761
7762 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
7763 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed
7764 to have already done the lookup to find the DWO file).
7765
7766 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
7767 THIS_CU->is_debug_types, but nothing else.
7768
7769 We fill in THIS_CU->length.
7770
7771 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
7772 linker) then DIE_READER_FUNC will not get called.
7773
7774 THIS_CU->cu is always freed when done.
7775 This is done in order to not leave THIS_CU->cu in a state where we have
7776 to care whether it refers to the "main" CU or the DWO CU. */
7777
7778 static void
7779 init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
7780 struct dwo_file *dwo_file,
7781 die_reader_func_ftype *die_reader_func,
7782 void *data)
7783 {
7784 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7785 struct objfile *objfile = dwarf2_per_objfile->objfile;
7786 struct dwarf2_section_info *section = this_cu->section;
7787 bfd *abfd = get_section_bfd_owner (section);
7788 struct dwarf2_section_info *abbrev_section;
7789 const gdb_byte *begin_info_ptr, *info_ptr;
7790 struct die_reader_specs reader;
7791 struct die_info *comp_unit_die;
7792 int has_children;
7793
7794 if (dwarf_die_debug)
7795 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7796 this_cu->is_debug_types ? "type" : "comp",
7797 sect_offset_str (this_cu->sect_off));
7798
7799 gdb_assert (this_cu->cu == NULL);
7800
7801 abbrev_section = (dwo_file != NULL
7802 ? &dwo_file->sections.abbrev
7803 : get_abbrev_section_for_cu (this_cu));
7804
7805 /* This is cheap if the section is already read in. */
7806 dwarf2_read_section (objfile, section);
7807
7808 struct dwarf2_cu cu (this_cu);
7809
7810 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7811 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7812 &cu.header, section,
7813 abbrev_section, info_ptr,
7814 (this_cu->is_debug_types
7815 ? rcuh_kind::TYPE
7816 : rcuh_kind::COMPILE));
7817
7818 this_cu->length = get_cu_length (&cu.header);
7819
7820 /* Skip dummy compilation units. */
7821 if (info_ptr >= begin_info_ptr + this_cu->length
7822 || peek_abbrev_code (abfd, info_ptr) == 0)
7823 return;
7824
7825 abbrev_table_up abbrev_table
7826 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7827 cu.header.abbrev_sect_off);
7828
7829 init_cu_die_reader (&reader, &cu, section, dwo_file, abbrev_table.get ());
7830 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
7831
7832 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7833 }
7834
7835 /* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
7836 does not lookup the specified DWO file.
7837 This cannot be used to read DWO files.
7838
7839 THIS_CU->cu is always freed when done.
7840 This is done in order to not leave THIS_CU->cu in a state where we have
7841 to care whether it refers to the "main" CU or the DWO CU.
7842 We can revisit this if the data shows there's a performance issue. */
7843
7844 static void
7845 init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
7846 die_reader_func_ftype *die_reader_func,
7847 void *data)
7848 {
7849 init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
7850 }
7851 \f
7852 /* Type Unit Groups.
7853
7854 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7855 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7856 so that all types coming from the same compilation (.o file) are grouped
7857 together. A future step could be to put the types in the same symtab as
7858 the CU the types ultimately came from. */
7859
7860 static hashval_t
7861 hash_type_unit_group (const void *item)
7862 {
7863 const struct type_unit_group *tu_group
7864 = (const struct type_unit_group *) item;
7865
7866 return hash_stmt_list_entry (&tu_group->hash);
7867 }
7868
7869 static int
7870 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
7871 {
7872 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7873 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
7874
7875 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
7876 }
7877
7878 /* Allocate a hash table for type unit groups. */
7879
7880 static htab_t
7881 allocate_type_unit_groups_table (struct objfile *objfile)
7882 {
7883 return htab_create_alloc_ex (3,
7884 hash_type_unit_group,
7885 eq_type_unit_group,
7886 NULL,
7887 &objfile->objfile_obstack,
7888 hashtab_obstack_allocate,
7889 dummy_obstack_deallocate);
7890 }
7891
7892 /* Type units that don't have DW_AT_stmt_list are grouped into their own
7893 partial symtabs. We combine several TUs per psymtab to not let the size
7894 of any one psymtab grow too big. */
7895 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7896 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
7897
7898 /* Helper routine for get_type_unit_group.
7899 Create the type_unit_group object used to hold one or more TUs. */
7900
7901 static struct type_unit_group *
7902 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
7903 {
7904 struct dwarf2_per_objfile *dwarf2_per_objfile
7905 = cu->per_cu->dwarf2_per_objfile;
7906 struct objfile *objfile = dwarf2_per_objfile->objfile;
7907 struct dwarf2_per_cu_data *per_cu;
7908 struct type_unit_group *tu_group;
7909
7910 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7911 struct type_unit_group);
7912 per_cu = &tu_group->per_cu;
7913 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
7914
7915 if (dwarf2_per_objfile->using_index)
7916 {
7917 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7918 struct dwarf2_per_cu_quick_data);
7919 }
7920 else
7921 {
7922 unsigned int line_offset = to_underlying (line_offset_struct);
7923 struct partial_symtab *pst;
7924 std::string name;
7925
7926 /* Give the symtab a useful name for debug purposes. */
7927 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7928 name = string_printf ("<type_units_%d>",
7929 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7930 else
7931 name = string_printf ("<type_units_at_0x%x>", line_offset);
7932
7933 pst = create_partial_symtab (per_cu, name.c_str ());
7934 pst->anonymous = 1;
7935 }
7936
7937 tu_group->hash.dwo_unit = cu->dwo_unit;
7938 tu_group->hash.line_sect_off = line_offset_struct;
7939
7940 return tu_group;
7941 }
7942
7943 /* Look up the type_unit_group for type unit CU, and create it if necessary.
7944 STMT_LIST is a DW_AT_stmt_list attribute. */
7945
7946 static struct type_unit_group *
7947 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7948 {
7949 struct dwarf2_per_objfile *dwarf2_per_objfile
7950 = cu->per_cu->dwarf2_per_objfile;
7951 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7952 struct type_unit_group *tu_group;
7953 void **slot;
7954 unsigned int line_offset;
7955 struct type_unit_group type_unit_group_for_lookup;
7956
7957 if (dwarf2_per_objfile->type_unit_groups == NULL)
7958 {
7959 dwarf2_per_objfile->type_unit_groups =
7960 allocate_type_unit_groups_table (dwarf2_per_objfile->objfile);
7961 }
7962
7963 /* Do we need to create a new group, or can we use an existing one? */
7964
7965 if (stmt_list)
7966 {
7967 line_offset = DW_UNSND (stmt_list);
7968 ++tu_stats->nr_symtab_sharers;
7969 }
7970 else
7971 {
7972 /* Ugh, no stmt_list. Rare, but we have to handle it.
7973 We can do various things here like create one group per TU or
7974 spread them over multiple groups to split up the expansion work.
7975 To avoid worst case scenarios (too many groups or too large groups)
7976 we, umm, group them in bunches. */
7977 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7978 | (tu_stats->nr_stmt_less_type_units
7979 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7980 ++tu_stats->nr_stmt_less_type_units;
7981 }
7982
7983 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7984 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7985 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
7986 &type_unit_group_for_lookup, INSERT);
7987 if (*slot != NULL)
7988 {
7989 tu_group = (struct type_unit_group *) *slot;
7990 gdb_assert (tu_group != NULL);
7991 }
7992 else
7993 {
7994 sect_offset line_offset_struct = (sect_offset) line_offset;
7995 tu_group = create_type_unit_group (cu, line_offset_struct);
7996 *slot = tu_group;
7997 ++tu_stats->nr_symtabs;
7998 }
7999
8000 return tu_group;
8001 }
8002 \f
8003 /* Partial symbol tables. */
8004
8005 /* Create a psymtab named NAME and assign it to PER_CU.
8006
8007 The caller must fill in the following details:
8008 dirname, textlow, texthigh. */
8009
8010 static struct partial_symtab *
8011 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
8012 {
8013 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
8014 struct partial_symtab *pst;
8015
8016 pst = start_psymtab_common (objfile, name, 0);
8017
8018 pst->psymtabs_addrmap_supported = 1;
8019
8020 /* This is the glue that links PST into GDB's symbol API. */
8021 pst->read_symtab_private = per_cu;
8022 pst->read_symtab = dwarf2_read_symtab;
8023 per_cu->v.psymtab = pst;
8024
8025 return pst;
8026 }
8027
8028 /* The DATA object passed to process_psymtab_comp_unit_reader has this
8029 type. */
8030
8031 struct process_psymtab_comp_unit_data
8032 {
8033 /* True if we are reading a DW_TAG_partial_unit. */
8034
8035 int want_partial_unit;
8036
8037 /* The "pretend" language that is used if the CU doesn't declare a
8038 language. */
8039
8040 enum language pretend_language;
8041 };
8042
8043 /* die_reader_func for process_psymtab_comp_unit. */
8044
8045 static void
8046 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
8047 const gdb_byte *info_ptr,
8048 struct die_info *comp_unit_die,
8049 int has_children,
8050 void *data)
8051 {
8052 struct dwarf2_cu *cu = reader->cu;
8053 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
8054 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8055 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8056 CORE_ADDR baseaddr;
8057 CORE_ADDR best_lowpc = 0, best_highpc = 0;
8058 struct partial_symtab *pst;
8059 enum pc_bounds_kind cu_bounds_kind;
8060 const char *filename;
8061 struct process_psymtab_comp_unit_data *info
8062 = (struct process_psymtab_comp_unit_data *) data;
8063
8064 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
8065 return;
8066
8067 gdb_assert (! per_cu->is_debug_types);
8068
8069 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
8070
8071 /* Allocate a new partial symbol table structure. */
8072 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
8073 if (filename == NULL)
8074 filename = "";
8075
8076 pst = create_partial_symtab (per_cu, filename);
8077
8078 /* This must be done before calling dwarf2_build_include_psymtabs. */
8079 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
8080
8081 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8082
8083 dwarf2_find_base_address (comp_unit_die, cu);
8084
8085 /* Possibly set the default values of LOWPC and HIGHPC from
8086 `DW_AT_ranges'. */
8087 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
8088 &best_highpc, cu, pst);
8089 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
8090 {
8091 CORE_ADDR low
8092 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
8093 - baseaddr);
8094 CORE_ADDR high
8095 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
8096 - baseaddr - 1);
8097 /* Store the contiguous range if it is not empty; it can be
8098 empty for CUs with no code. */
8099 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8100 low, high, pst);
8101 }
8102
8103 /* Check if comp unit has_children.
8104 If so, read the rest of the partial symbols from this comp unit.
8105 If not, there's no more debug_info for this comp unit. */
8106 if (has_children)
8107 {
8108 struct partial_die_info *first_die;
8109 CORE_ADDR lowpc, highpc;
8110
8111 lowpc = ((CORE_ADDR) -1);
8112 highpc = ((CORE_ADDR) 0);
8113
8114 first_die = load_partial_dies (reader, info_ptr, 1);
8115
8116 scan_partial_symbols (first_die, &lowpc, &highpc,
8117 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
8118
8119 /* If we didn't find a lowpc, set it to highpc to avoid
8120 complaints from `maint check'. */
8121 if (lowpc == ((CORE_ADDR) -1))
8122 lowpc = highpc;
8123
8124 /* If the compilation unit didn't have an explicit address range,
8125 then use the information extracted from its child dies. */
8126 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
8127 {
8128 best_lowpc = lowpc;
8129 best_highpc = highpc;
8130 }
8131 }
8132 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
8133 best_lowpc + baseaddr)
8134 - baseaddr);
8135 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
8136 best_highpc + baseaddr)
8137 - baseaddr);
8138
8139 end_psymtab_common (objfile, pst);
8140
8141 if (!cu->per_cu->imported_symtabs_empty ())
8142 {
8143 int i;
8144 int len = cu->per_cu->imported_symtabs_size ();
8145
8146 /* Fill in 'dependencies' here; we fill in 'users' in a
8147 post-pass. */
8148 pst->number_of_dependencies = len;
8149 pst->dependencies
8150 = objfile->partial_symtabs->allocate_dependencies (len);
8151 for (i = 0; i < len; ++i)
8152 {
8153 pst->dependencies[i]
8154 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
8155 }
8156
8157 cu->per_cu->imported_symtabs_free ();
8158 }
8159
8160 /* Get the list of files included in the current compilation unit,
8161 and build a psymtab for each of them. */
8162 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
8163
8164 if (dwarf_read_debug)
8165 fprintf_unfiltered (gdb_stdlog,
8166 "Psymtab for %s unit @%s: %s - %s"
8167 ", %d global, %d static syms\n",
8168 per_cu->is_debug_types ? "type" : "comp",
8169 sect_offset_str (per_cu->sect_off),
8170 paddress (gdbarch, pst->text_low (objfile)),
8171 paddress (gdbarch, pst->text_high (objfile)),
8172 pst->n_global_syms, pst->n_static_syms);
8173 }
8174
8175 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8176 Process compilation unit THIS_CU for a psymtab. */
8177
8178 static void
8179 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
8180 int want_partial_unit,
8181 enum language pretend_language)
8182 {
8183 /* If this compilation unit was already read in, free the
8184 cached copy in order to read it in again. This is
8185 necessary because we skipped some symbols when we first
8186 read in the compilation unit (see load_partial_dies).
8187 This problem could be avoided, but the benefit is unclear. */
8188 if (this_cu->cu != NULL)
8189 free_one_cached_comp_unit (this_cu);
8190
8191 if (this_cu->is_debug_types)
8192 init_cutu_and_read_dies (this_cu, NULL, 0, 0, false,
8193 build_type_psymtabs_reader, NULL);
8194 else
8195 {
8196 process_psymtab_comp_unit_data info;
8197 info.want_partial_unit = want_partial_unit;
8198 info.pretend_language = pretend_language;
8199 init_cutu_and_read_dies (this_cu, NULL, 0, 0, false,
8200 process_psymtab_comp_unit_reader, &info);
8201 }
8202
8203 /* Age out any secondary CUs. */
8204 age_cached_comp_units (this_cu->dwarf2_per_objfile);
8205 }
8206
8207 /* Reader function for build_type_psymtabs. */
8208
8209 static void
8210 build_type_psymtabs_reader (const struct die_reader_specs *reader,
8211 const gdb_byte *info_ptr,
8212 struct die_info *type_unit_die,
8213 int has_children,
8214 void *data)
8215 {
8216 struct dwarf2_per_objfile *dwarf2_per_objfile
8217 = reader->cu->per_cu->dwarf2_per_objfile;
8218 struct objfile *objfile = dwarf2_per_objfile->objfile;
8219 struct dwarf2_cu *cu = reader->cu;
8220 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8221 struct signatured_type *sig_type;
8222 struct type_unit_group *tu_group;
8223 struct attribute *attr;
8224 struct partial_die_info *first_die;
8225 CORE_ADDR lowpc, highpc;
8226 struct partial_symtab *pst;
8227
8228 gdb_assert (data == NULL);
8229 gdb_assert (per_cu->is_debug_types);
8230 sig_type = (struct signatured_type *) per_cu;
8231
8232 if (! has_children)
8233 return;
8234
8235 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
8236 tu_group = get_type_unit_group (cu, attr);
8237
8238 if (tu_group->tus == nullptr)
8239 tu_group->tus = new std::vector<signatured_type *>;
8240 tu_group->tus->push_back (sig_type);
8241
8242 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
8243 pst = create_partial_symtab (per_cu, "");
8244 pst->anonymous = 1;
8245
8246 first_die = load_partial_dies (reader, info_ptr, 1);
8247
8248 lowpc = (CORE_ADDR) -1;
8249 highpc = (CORE_ADDR) 0;
8250 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
8251
8252 end_psymtab_common (objfile, pst);
8253 }
8254
8255 /* Struct used to sort TUs by their abbreviation table offset. */
8256
8257 struct tu_abbrev_offset
8258 {
8259 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
8260 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
8261 {}
8262
8263 signatured_type *sig_type;
8264 sect_offset abbrev_offset;
8265 };
8266
8267 /* Helper routine for build_type_psymtabs_1, passed to std::sort. */
8268
8269 static bool
8270 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
8271 const struct tu_abbrev_offset &b)
8272 {
8273 return a.abbrev_offset < b.abbrev_offset;
8274 }
8275
8276 /* Efficiently read all the type units.
8277 This does the bulk of the work for build_type_psymtabs.
8278
8279 The efficiency is because we sort TUs by the abbrev table they use and
8280 only read each abbrev table once. In one program there are 200K TUs
8281 sharing 8K abbrev tables.
8282
8283 The main purpose of this function is to support building the
8284 dwarf2_per_objfile->type_unit_groups table.
8285 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
8286 can collapse the search space by grouping them by stmt_list.
8287 The savings can be significant, in the same program from above the 200K TUs
8288 share 8K stmt_list tables.
8289
8290 FUNC is expected to call get_type_unit_group, which will create the
8291 struct type_unit_group if necessary and add it to
8292 dwarf2_per_objfile->type_unit_groups. */
8293
8294 static void
8295 build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
8296 {
8297 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8298 abbrev_table_up abbrev_table;
8299 sect_offset abbrev_offset;
8300
8301 /* It's up to the caller to not call us multiple times. */
8302 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
8303
8304 if (dwarf2_per_objfile->all_type_units.empty ())
8305 return;
8306
8307 /* TUs typically share abbrev tables, and there can be way more TUs than
8308 abbrev tables. Sort by abbrev table to reduce the number of times we
8309 read each abbrev table in.
8310 Alternatives are to punt or to maintain a cache of abbrev tables.
8311 This is simpler and efficient enough for now.
8312
8313 Later we group TUs by their DW_AT_stmt_list value (as this defines the
8314 symtab to use). Typically TUs with the same abbrev offset have the same
8315 stmt_list value too so in practice this should work well.
8316
8317 The basic algorithm here is:
8318
8319 sort TUs by abbrev table
8320 for each TU with same abbrev table:
8321 read abbrev table if first user
8322 read TU top level DIE
8323 [IWBN if DWO skeletons had DW_AT_stmt_list]
8324 call FUNC */
8325
8326 if (dwarf_read_debug)
8327 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
8328
8329 /* Sort in a separate table to maintain the order of all_type_units
8330 for .gdb_index: TU indices directly index all_type_units. */
8331 std::vector<tu_abbrev_offset> sorted_by_abbrev;
8332 sorted_by_abbrev.reserve (dwarf2_per_objfile->all_type_units.size ());
8333
8334 for (signatured_type *sig_type : dwarf2_per_objfile->all_type_units)
8335 sorted_by_abbrev.emplace_back
8336 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
8337 sig_type->per_cu.section,
8338 sig_type->per_cu.sect_off));
8339
8340 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
8341 sort_tu_by_abbrev_offset);
8342
8343 abbrev_offset = (sect_offset) ~(unsigned) 0;
8344
8345 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
8346 {
8347 /* Switch to the next abbrev table if necessary. */
8348 if (abbrev_table == NULL
8349 || tu.abbrev_offset != abbrev_offset)
8350 {
8351 abbrev_offset = tu.abbrev_offset;
8352 abbrev_table =
8353 abbrev_table_read_table (dwarf2_per_objfile,
8354 &dwarf2_per_objfile->abbrev,
8355 abbrev_offset);
8356 ++tu_stats->nr_uniq_abbrev_tables;
8357 }
8358
8359 init_cutu_and_read_dies (&tu.sig_type->per_cu, abbrev_table.get (),
8360 0, 0, false, build_type_psymtabs_reader, NULL);
8361 }
8362 }
8363
8364 /* Print collected type unit statistics. */
8365
8366 static void
8367 print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
8368 {
8369 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8370
8371 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
8372 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
8373 dwarf2_per_objfile->all_type_units.size ());
8374 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
8375 tu_stats->nr_uniq_abbrev_tables);
8376 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
8377 tu_stats->nr_symtabs);
8378 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
8379 tu_stats->nr_symtab_sharers);
8380 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
8381 tu_stats->nr_stmt_less_type_units);
8382 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
8383 tu_stats->nr_all_type_units_reallocs);
8384 }
8385
8386 /* Traversal function for build_type_psymtabs. */
8387
8388 static int
8389 build_type_psymtab_dependencies (void **slot, void *info)
8390 {
8391 struct dwarf2_per_objfile *dwarf2_per_objfile
8392 = (struct dwarf2_per_objfile *) info;
8393 struct objfile *objfile = dwarf2_per_objfile->objfile;
8394 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
8395 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
8396 struct partial_symtab *pst = per_cu->v.psymtab;
8397 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
8398 int i;
8399
8400 gdb_assert (len > 0);
8401 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
8402
8403 pst->number_of_dependencies = len;
8404 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
8405 for (i = 0; i < len; ++i)
8406 {
8407 struct signatured_type *iter = tu_group->tus->at (i);
8408 gdb_assert (iter->per_cu.is_debug_types);
8409 pst->dependencies[i] = iter->per_cu.v.psymtab;
8410 iter->type_unit_group = tu_group;
8411 }
8412
8413 delete tu_group->tus;
8414 tu_group->tus = nullptr;
8415
8416 return 1;
8417 }
8418
8419 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8420 Build partial symbol tables for the .debug_types comp-units. */
8421
8422 static void
8423 build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
8424 {
8425 if (! create_all_type_units (dwarf2_per_objfile))
8426 return;
8427
8428 build_type_psymtabs_1 (dwarf2_per_objfile);
8429 }
8430
8431 /* Traversal function for process_skeletonless_type_unit.
8432 Read a TU in a DWO file and build partial symbols for it. */
8433
8434 static int
8435 process_skeletonless_type_unit (void **slot, void *info)
8436 {
8437 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
8438 struct dwarf2_per_objfile *dwarf2_per_objfile
8439 = (struct dwarf2_per_objfile *) info;
8440 struct signatured_type find_entry, *entry;
8441
8442 /* If this TU doesn't exist in the global table, add it and read it in. */
8443
8444 if (dwarf2_per_objfile->signatured_types == NULL)
8445 {
8446 dwarf2_per_objfile->signatured_types
8447 = allocate_signatured_type_table (dwarf2_per_objfile->objfile);
8448 }
8449
8450 find_entry.signature = dwo_unit->signature;
8451 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
8452 INSERT);
8453 /* If we've already seen this type there's nothing to do. What's happening
8454 is we're doing our own version of comdat-folding here. */
8455 if (*slot != NULL)
8456 return 1;
8457
8458 /* This does the job that create_all_type_units would have done for
8459 this TU. */
8460 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
8461 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
8462 *slot = entry;
8463
8464 /* This does the job that build_type_psymtabs_1 would have done. */
8465 init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0, false,
8466 build_type_psymtabs_reader, NULL);
8467
8468 return 1;
8469 }
8470
8471 /* Traversal function for process_skeletonless_type_units. */
8472
8473 static int
8474 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8475 {
8476 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8477
8478 if (dwo_file->tus != NULL)
8479 {
8480 htab_traverse_noresize (dwo_file->tus,
8481 process_skeletonless_type_unit, info);
8482 }
8483
8484 return 1;
8485 }
8486
8487 /* Scan all TUs of DWO files, verifying we've processed them.
8488 This is needed in case a TU was emitted without its skeleton.
8489 Note: This can't be done until we know what all the DWO files are. */
8490
8491 static void
8492 process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
8493 {
8494 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
8495 if (get_dwp_file (dwarf2_per_objfile) == NULL
8496 && dwarf2_per_objfile->dwo_files != NULL)
8497 {
8498 htab_traverse_noresize (dwarf2_per_objfile->dwo_files.get (),
8499 process_dwo_file_for_skeletonless_type_units,
8500 dwarf2_per_objfile);
8501 }
8502 }
8503
8504 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
8505
8506 static void
8507 set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
8508 {
8509 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
8510 {
8511 struct partial_symtab *pst = per_cu->v.psymtab;
8512
8513 if (pst == NULL)
8514 continue;
8515
8516 for (int j = 0; j < pst->number_of_dependencies; ++j)
8517 {
8518 /* Set the 'user' field only if it is not already set. */
8519 if (pst->dependencies[j]->user == NULL)
8520 pst->dependencies[j]->user = pst;
8521 }
8522 }
8523 }
8524
8525 /* Build the partial symbol table by doing a quick pass through the
8526 .debug_info and .debug_abbrev sections. */
8527
8528 static void
8529 dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
8530 {
8531 struct objfile *objfile = dwarf2_per_objfile->objfile;
8532
8533 if (dwarf_read_debug)
8534 {
8535 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
8536 objfile_name (objfile));
8537 }
8538
8539 dwarf2_per_objfile->reading_partial_symbols = 1;
8540
8541 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
8542
8543 /* Any cached compilation units will be linked by the per-objfile
8544 read_in_chain. Make sure to free them when we're done. */
8545 free_cached_comp_units freer (dwarf2_per_objfile);
8546
8547 build_type_psymtabs (dwarf2_per_objfile);
8548
8549 create_all_comp_units (dwarf2_per_objfile);
8550
8551 /* Create a temporary address map on a temporary obstack. We later
8552 copy this to the final obstack. */
8553 auto_obstack temp_obstack;
8554
8555 scoped_restore save_psymtabs_addrmap
8556 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
8557 addrmap_create_mutable (&temp_obstack));
8558
8559 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
8560 process_psymtab_comp_unit (per_cu, 0, language_minimal);
8561
8562 /* This has to wait until we read the CUs, we need the list of DWOs. */
8563 process_skeletonless_type_units (dwarf2_per_objfile);
8564
8565 /* Now that all TUs have been processed we can fill in the dependencies. */
8566 if (dwarf2_per_objfile->type_unit_groups != NULL)
8567 {
8568 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
8569 build_type_psymtab_dependencies, dwarf2_per_objfile);
8570 }
8571
8572 if (dwarf_read_debug)
8573 print_tu_stats (dwarf2_per_objfile);
8574
8575 set_partial_user (dwarf2_per_objfile);
8576
8577 objfile->partial_symtabs->psymtabs_addrmap
8578 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
8579 objfile->partial_symtabs->obstack ());
8580 /* At this point we want to keep the address map. */
8581 save_psymtabs_addrmap.release ();
8582
8583 if (dwarf_read_debug)
8584 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
8585 objfile_name (objfile));
8586 }
8587
8588 /* die_reader_func for load_partial_comp_unit. */
8589
8590 static void
8591 load_partial_comp_unit_reader (const struct die_reader_specs *reader,
8592 const gdb_byte *info_ptr,
8593 struct die_info *comp_unit_die,
8594 int has_children,
8595 void *data)
8596 {
8597 struct dwarf2_cu *cu = reader->cu;
8598
8599 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
8600
8601 /* Check if comp unit has_children.
8602 If so, read the rest of the partial symbols from this comp unit.
8603 If not, there's no more debug_info for this comp unit. */
8604 if (has_children)
8605 load_partial_dies (reader, info_ptr, 0);
8606 }
8607
8608 /* Load the partial DIEs for a secondary CU into memory.
8609 This is also used when rereading a primary CU with load_all_dies. */
8610
8611 static void
8612 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
8613 {
8614 init_cutu_and_read_dies (this_cu, NULL, 1, 1, false,
8615 load_partial_comp_unit_reader, NULL);
8616 }
8617
8618 static void
8619 read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
8620 struct dwarf2_section_info *section,
8621 struct dwarf2_section_info *abbrev_section,
8622 unsigned int is_dwz)
8623 {
8624 const gdb_byte *info_ptr;
8625 struct objfile *objfile = dwarf2_per_objfile->objfile;
8626
8627 if (dwarf_read_debug)
8628 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
8629 get_section_name (section),
8630 get_section_file_name (section));
8631
8632 dwarf2_read_section (objfile, section);
8633
8634 info_ptr = section->buffer;
8635
8636 while (info_ptr < section->buffer + section->size)
8637 {
8638 struct dwarf2_per_cu_data *this_cu;
8639
8640 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
8641
8642 comp_unit_head cu_header;
8643 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
8644 abbrev_section, info_ptr,
8645 rcuh_kind::COMPILE);
8646
8647 /* Save the compilation unit for later lookup. */
8648 if (cu_header.unit_type != DW_UT_type)
8649 {
8650 this_cu = XOBNEW (&objfile->objfile_obstack,
8651 struct dwarf2_per_cu_data);
8652 memset (this_cu, 0, sizeof (*this_cu));
8653 }
8654 else
8655 {
8656 auto sig_type = XOBNEW (&objfile->objfile_obstack,
8657 struct signatured_type);
8658 memset (sig_type, 0, sizeof (*sig_type));
8659 sig_type->signature = cu_header.signature;
8660 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8661 this_cu = &sig_type->per_cu;
8662 }
8663 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
8664 this_cu->sect_off = sect_off;
8665 this_cu->length = cu_header.length + cu_header.initial_length_size;
8666 this_cu->is_dwz = is_dwz;
8667 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8668 this_cu->section = section;
8669
8670 dwarf2_per_objfile->all_comp_units.push_back (this_cu);
8671
8672 info_ptr = info_ptr + this_cu->length;
8673 }
8674 }
8675
8676 /* Create a list of all compilation units in OBJFILE.
8677 This is only done for -readnow and building partial symtabs. */
8678
8679 static void
8680 create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
8681 {
8682 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
8683 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
8684 &dwarf2_per_objfile->abbrev, 0);
8685
8686 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
8687 if (dwz != NULL)
8688 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
8689 1);
8690 }
8691
8692 /* Process all loaded DIEs for compilation unit CU, starting at
8693 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
8694 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
8695 DW_AT_ranges). See the comments of add_partial_subprogram on how
8696 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
8697
8698 static void
8699 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
8700 CORE_ADDR *highpc, int set_addrmap,
8701 struct dwarf2_cu *cu)
8702 {
8703 struct partial_die_info *pdi;
8704
8705 /* Now, march along the PDI's, descending into ones which have
8706 interesting children but skipping the children of the other ones,
8707 until we reach the end of the compilation unit. */
8708
8709 pdi = first_die;
8710
8711 while (pdi != NULL)
8712 {
8713 pdi->fixup (cu);
8714
8715 /* Anonymous namespaces or modules have no name but have interesting
8716 children, so we need to look at them. Ditto for anonymous
8717 enums. */
8718
8719 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
8720 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
8721 || pdi->tag == DW_TAG_imported_unit
8722 || pdi->tag == DW_TAG_inlined_subroutine)
8723 {
8724 switch (pdi->tag)
8725 {
8726 case DW_TAG_subprogram:
8727 case DW_TAG_inlined_subroutine:
8728 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8729 break;
8730 case DW_TAG_constant:
8731 case DW_TAG_variable:
8732 case DW_TAG_typedef:
8733 case DW_TAG_union_type:
8734 if (!pdi->is_declaration)
8735 {
8736 add_partial_symbol (pdi, cu);
8737 }
8738 break;
8739 case DW_TAG_class_type:
8740 case DW_TAG_interface_type:
8741 case DW_TAG_structure_type:
8742 if (!pdi->is_declaration)
8743 {
8744 add_partial_symbol (pdi, cu);
8745 }
8746 if ((cu->language == language_rust
8747 || cu->language == language_cplus) && pdi->has_children)
8748 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8749 set_addrmap, cu);
8750 break;
8751 case DW_TAG_enumeration_type:
8752 if (!pdi->is_declaration)
8753 add_partial_enumeration (pdi, cu);
8754 break;
8755 case DW_TAG_base_type:
8756 case DW_TAG_subrange_type:
8757 /* File scope base type definitions are added to the partial
8758 symbol table. */
8759 add_partial_symbol (pdi, cu);
8760 break;
8761 case DW_TAG_namespace:
8762 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
8763 break;
8764 case DW_TAG_module:
8765 if (!pdi->is_declaration)
8766 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
8767 break;
8768 case DW_TAG_imported_unit:
8769 {
8770 struct dwarf2_per_cu_data *per_cu;
8771
8772 /* For now we don't handle imported units in type units. */
8773 if (cu->per_cu->is_debug_types)
8774 {
8775 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8776 " supported in type units [in module %s]"),
8777 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
8778 }
8779
8780 per_cu = dwarf2_find_containing_comp_unit
8781 (pdi->d.sect_off, pdi->is_dwz,
8782 cu->per_cu->dwarf2_per_objfile);
8783
8784 /* Go read the partial unit, if needed. */
8785 if (per_cu->v.psymtab == NULL)
8786 process_psymtab_comp_unit (per_cu, 1, cu->language);
8787
8788 cu->per_cu->imported_symtabs_push (per_cu);
8789 }
8790 break;
8791 case DW_TAG_imported_declaration:
8792 add_partial_symbol (pdi, cu);
8793 break;
8794 default:
8795 break;
8796 }
8797 }
8798
8799 /* If the die has a sibling, skip to the sibling. */
8800
8801 pdi = pdi->die_sibling;
8802 }
8803 }
8804
8805 /* Functions used to compute the fully scoped name of a partial DIE.
8806
8807 Normally, this is simple. For C++, the parent DIE's fully scoped
8808 name is concatenated with "::" and the partial DIE's name.
8809 Enumerators are an exception; they use the scope of their parent
8810 enumeration type, i.e. the name of the enumeration type is not
8811 prepended to the enumerator.
8812
8813 There are two complexities. One is DW_AT_specification; in this
8814 case "parent" means the parent of the target of the specification,
8815 instead of the direct parent of the DIE. The other is compilers
8816 which do not emit DW_TAG_namespace; in this case we try to guess
8817 the fully qualified name of structure types from their members'
8818 linkage names. This must be done using the DIE's children rather
8819 than the children of any DW_AT_specification target. We only need
8820 to do this for structures at the top level, i.e. if the target of
8821 any DW_AT_specification (if any; otherwise the DIE itself) does not
8822 have a parent. */
8823
8824 /* Compute the scope prefix associated with PDI's parent, in
8825 compilation unit CU. The result will be allocated on CU's
8826 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8827 field. NULL is returned if no prefix is necessary. */
8828 static const char *
8829 partial_die_parent_scope (struct partial_die_info *pdi,
8830 struct dwarf2_cu *cu)
8831 {
8832 const char *grandparent_scope;
8833 struct partial_die_info *parent, *real_pdi;
8834
8835 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8836 then this means the parent of the specification DIE. */
8837
8838 real_pdi = pdi;
8839 while (real_pdi->has_specification)
8840 {
8841 auto res = find_partial_die (real_pdi->spec_offset,
8842 real_pdi->spec_is_dwz, cu);
8843 real_pdi = res.pdi;
8844 cu = res.cu;
8845 }
8846
8847 parent = real_pdi->die_parent;
8848 if (parent == NULL)
8849 return NULL;
8850
8851 if (parent->scope_set)
8852 return parent->scope;
8853
8854 parent->fixup (cu);
8855
8856 grandparent_scope = partial_die_parent_scope (parent, cu);
8857
8858 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8859 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8860 Work around this problem here. */
8861 if (cu->language == language_cplus
8862 && parent->tag == DW_TAG_namespace
8863 && strcmp (parent->name, "::") == 0
8864 && grandparent_scope == NULL)
8865 {
8866 parent->scope = NULL;
8867 parent->scope_set = 1;
8868 return NULL;
8869 }
8870
8871 /* Nested subroutines in Fortran get a prefix. */
8872 if (pdi->tag == DW_TAG_enumerator)
8873 /* Enumerators should not get the name of the enumeration as a prefix. */
8874 parent->scope = grandparent_scope;
8875 else if (parent->tag == DW_TAG_namespace
8876 || parent->tag == DW_TAG_module
8877 || parent->tag == DW_TAG_structure_type
8878 || parent->tag == DW_TAG_class_type
8879 || parent->tag == DW_TAG_interface_type
8880 || parent->tag == DW_TAG_union_type
8881 || parent->tag == DW_TAG_enumeration_type
8882 || (cu->language == language_fortran
8883 && parent->tag == DW_TAG_subprogram
8884 && pdi->tag == DW_TAG_subprogram))
8885 {
8886 if (grandparent_scope == NULL)
8887 parent->scope = parent->name;
8888 else
8889 parent->scope = typename_concat (&cu->comp_unit_obstack,
8890 grandparent_scope,
8891 parent->name, 0, cu);
8892 }
8893 else
8894 {
8895 /* FIXME drow/2004-04-01: What should we be doing with
8896 function-local names? For partial symbols, we should probably be
8897 ignoring them. */
8898 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8899 dwarf_tag_name (parent->tag),
8900 sect_offset_str (pdi->sect_off));
8901 parent->scope = grandparent_scope;
8902 }
8903
8904 parent->scope_set = 1;
8905 return parent->scope;
8906 }
8907
8908 /* Return the fully scoped name associated with PDI, from compilation unit
8909 CU. The result will be allocated with malloc. */
8910
8911 static char *
8912 partial_die_full_name (struct partial_die_info *pdi,
8913 struct dwarf2_cu *cu)
8914 {
8915 const char *parent_scope;
8916
8917 /* If this is a template instantiation, we can not work out the
8918 template arguments from partial DIEs. So, unfortunately, we have
8919 to go through the full DIEs. At least any work we do building
8920 types here will be reused if full symbols are loaded later. */
8921 if (pdi->has_template_arguments)
8922 {
8923 pdi->fixup (cu);
8924
8925 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8926 {
8927 struct die_info *die;
8928 struct attribute attr;
8929 struct dwarf2_cu *ref_cu = cu;
8930
8931 /* DW_FORM_ref_addr is using section offset. */
8932 attr.name = (enum dwarf_attribute) 0;
8933 attr.form = DW_FORM_ref_addr;
8934 attr.u.unsnd = to_underlying (pdi->sect_off);
8935 die = follow_die_ref (NULL, &attr, &ref_cu);
8936
8937 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8938 }
8939 }
8940
8941 parent_scope = partial_die_parent_scope (pdi, cu);
8942 if (parent_scope == NULL)
8943 return NULL;
8944 else
8945 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
8946 }
8947
8948 static void
8949 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8950 {
8951 struct dwarf2_per_objfile *dwarf2_per_objfile
8952 = cu->per_cu->dwarf2_per_objfile;
8953 struct objfile *objfile = dwarf2_per_objfile->objfile;
8954 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8955 CORE_ADDR addr = 0;
8956 const char *actual_name = NULL;
8957 CORE_ADDR baseaddr;
8958 char *built_actual_name;
8959
8960 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8961
8962 built_actual_name = partial_die_full_name (pdi, cu);
8963 if (built_actual_name != NULL)
8964 actual_name = built_actual_name;
8965
8966 if (actual_name == NULL)
8967 actual_name = pdi->name;
8968
8969 switch (pdi->tag)
8970 {
8971 case DW_TAG_inlined_subroutine:
8972 case DW_TAG_subprogram:
8973 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8974 - baseaddr);
8975 if (pdi->is_external
8976 || cu->language == language_ada
8977 || (cu->language == language_fortran
8978 && pdi->die_parent != NULL
8979 && pdi->die_parent->tag == DW_TAG_subprogram))
8980 {
8981 /* Normally, only "external" DIEs are part of the global scope.
8982 But in Ada and Fortran, we want to be able to access nested
8983 procedures globally. So all Ada and Fortran subprograms are
8984 stored in the global scope. */
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::GLOBAL,
8990 addr,
8991 cu->language, objfile);
8992 }
8993 else
8994 {
8995 add_psymbol_to_list (actual_name,
8996 built_actual_name != NULL,
8997 VAR_DOMAIN, LOC_BLOCK,
8998 SECT_OFF_TEXT (objfile),
8999 psymbol_placement::STATIC,
9000 addr, cu->language, objfile);
9001 }
9002
9003 if (pdi->main_subprogram && actual_name != NULL)
9004 set_objfile_main_name (objfile, actual_name, cu->language);
9005 break;
9006 case DW_TAG_constant:
9007 add_psymbol_to_list (actual_name,
9008 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
9009 -1, (pdi->is_external
9010 ? psymbol_placement::GLOBAL
9011 : psymbol_placement::STATIC),
9012 0, cu->language, objfile);
9013 break;
9014 case DW_TAG_variable:
9015 if (pdi->d.locdesc)
9016 addr = decode_locdesc (pdi->d.locdesc, cu);
9017
9018 if (pdi->d.locdesc
9019 && addr == 0
9020 && !dwarf2_per_objfile->has_section_at_zero)
9021 {
9022 /* A global or static variable may also have been stripped
9023 out by the linker if unused, in which case its address
9024 will be nullified; do not add such variables into partial
9025 symbol table then. */
9026 }
9027 else if (pdi->is_external)
9028 {
9029 /* Global Variable.
9030 Don't enter into the minimal symbol tables as there is
9031 a minimal symbol table entry from the ELF symbols already.
9032 Enter into partial symbol table if it has a location
9033 descriptor or a type.
9034 If the location descriptor is missing, new_symbol will create
9035 a LOC_UNRESOLVED symbol, the address of the variable will then
9036 be determined from the minimal symbol table whenever the variable
9037 is referenced.
9038 The address for the partial symbol table entry is not
9039 used by GDB, but it comes in handy for debugging partial symbol
9040 table building. */
9041
9042 if (pdi->d.locdesc || pdi->has_type)
9043 add_psymbol_to_list (actual_name,
9044 built_actual_name != NULL,
9045 VAR_DOMAIN, LOC_STATIC,
9046 SECT_OFF_TEXT (objfile),
9047 psymbol_placement::GLOBAL,
9048 addr, cu->language, objfile);
9049 }
9050 else
9051 {
9052 int has_loc = pdi->d.locdesc != NULL;
9053
9054 /* Static Variable. Skip symbols whose value we cannot know (those
9055 without location descriptors or constant values). */
9056 if (!has_loc && !pdi->has_const_value)
9057 {
9058 xfree (built_actual_name);
9059 return;
9060 }
9061
9062 add_psymbol_to_list (actual_name,
9063 built_actual_name != NULL,
9064 VAR_DOMAIN, LOC_STATIC,
9065 SECT_OFF_TEXT (objfile),
9066 psymbol_placement::STATIC,
9067 has_loc ? addr : 0,
9068 cu->language, objfile);
9069 }
9070 break;
9071 case DW_TAG_typedef:
9072 case DW_TAG_base_type:
9073 case DW_TAG_subrange_type:
9074 add_psymbol_to_list (actual_name,
9075 built_actual_name != NULL,
9076 VAR_DOMAIN, LOC_TYPEDEF, -1,
9077 psymbol_placement::STATIC,
9078 0, cu->language, objfile);
9079 break;
9080 case DW_TAG_imported_declaration:
9081 case DW_TAG_namespace:
9082 add_psymbol_to_list (actual_name,
9083 built_actual_name != NULL,
9084 VAR_DOMAIN, LOC_TYPEDEF, -1,
9085 psymbol_placement::GLOBAL,
9086 0, cu->language, objfile);
9087 break;
9088 case DW_TAG_module:
9089 /* With Fortran 77 there might be a "BLOCK DATA" module
9090 available without any name. If so, we skip the module as it
9091 doesn't bring any value. */
9092 if (actual_name != nullptr)
9093 add_psymbol_to_list (actual_name,
9094 built_actual_name != NULL,
9095 MODULE_DOMAIN, LOC_TYPEDEF, -1,
9096 psymbol_placement::GLOBAL,
9097 0, cu->language, objfile);
9098 break;
9099 case DW_TAG_class_type:
9100 case DW_TAG_interface_type:
9101 case DW_TAG_structure_type:
9102 case DW_TAG_union_type:
9103 case DW_TAG_enumeration_type:
9104 /* Skip external references. The DWARF standard says in the section
9105 about "Structure, Union, and Class Type Entries": "An incomplete
9106 structure, union or class type is represented by a structure,
9107 union or class entry that does not have a byte size attribute
9108 and that has a DW_AT_declaration attribute." */
9109 if (!pdi->has_byte_size && pdi->is_declaration)
9110 {
9111 xfree (built_actual_name);
9112 return;
9113 }
9114
9115 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
9116 static vs. global. */
9117 add_psymbol_to_list (actual_name,
9118 built_actual_name != NULL,
9119 STRUCT_DOMAIN, LOC_TYPEDEF, -1,
9120 cu->language == language_cplus
9121 ? psymbol_placement::GLOBAL
9122 : psymbol_placement::STATIC,
9123 0, cu->language, objfile);
9124
9125 break;
9126 case DW_TAG_enumerator:
9127 add_psymbol_to_list (actual_name,
9128 built_actual_name != NULL,
9129 VAR_DOMAIN, LOC_CONST, -1,
9130 cu->language == language_cplus
9131 ? psymbol_placement::GLOBAL
9132 : psymbol_placement::STATIC,
9133 0, cu->language, objfile);
9134 break;
9135 default:
9136 break;
9137 }
9138
9139 xfree (built_actual_name);
9140 }
9141
9142 /* Read a partial die corresponding to a namespace; also, add a symbol
9143 corresponding to that namespace to the symbol table. NAMESPACE is
9144 the name of the enclosing namespace. */
9145
9146 static void
9147 add_partial_namespace (struct partial_die_info *pdi,
9148 CORE_ADDR *lowpc, CORE_ADDR *highpc,
9149 int set_addrmap, struct dwarf2_cu *cu)
9150 {
9151 /* Add a symbol for the namespace. */
9152
9153 add_partial_symbol (pdi, cu);
9154
9155 /* Now scan partial symbols in that namespace. */
9156
9157 if (pdi->has_children)
9158 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
9159 }
9160
9161 /* Read a partial die corresponding to a Fortran module. */
9162
9163 static void
9164 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
9165 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
9166 {
9167 /* Add a symbol for the namespace. */
9168
9169 add_partial_symbol (pdi, cu);
9170
9171 /* Now scan partial symbols in that module. */
9172
9173 if (pdi->has_children)
9174 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
9175 }
9176
9177 /* Read a partial die corresponding to a subprogram or an inlined
9178 subprogram and create a partial symbol for that subprogram.
9179 When the CU language allows it, this routine also defines a partial
9180 symbol for each nested subprogram that this subprogram contains.
9181 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
9182 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
9183
9184 PDI may also be a lexical block, in which case we simply search
9185 recursively for subprograms defined inside that lexical block.
9186 Again, this is only performed when the CU language allows this
9187 type of definitions. */
9188
9189 static void
9190 add_partial_subprogram (struct partial_die_info *pdi,
9191 CORE_ADDR *lowpc, CORE_ADDR *highpc,
9192 int set_addrmap, struct dwarf2_cu *cu)
9193 {
9194 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
9195 {
9196 if (pdi->has_pc_info)
9197 {
9198 if (pdi->lowpc < *lowpc)
9199 *lowpc = pdi->lowpc;
9200 if (pdi->highpc > *highpc)
9201 *highpc = pdi->highpc;
9202 if (set_addrmap)
9203 {
9204 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9205 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9206 CORE_ADDR baseaddr;
9207 CORE_ADDR this_highpc;
9208 CORE_ADDR this_lowpc;
9209
9210 baseaddr = ANOFFSET (objfile->section_offsets,
9211 SECT_OFF_TEXT (objfile));
9212 this_lowpc
9213 = (gdbarch_adjust_dwarf2_addr (gdbarch,
9214 pdi->lowpc + baseaddr)
9215 - baseaddr);
9216 this_highpc
9217 = (gdbarch_adjust_dwarf2_addr (gdbarch,
9218 pdi->highpc + baseaddr)
9219 - baseaddr);
9220 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
9221 this_lowpc, this_highpc - 1,
9222 cu->per_cu->v.psymtab);
9223 }
9224 }
9225
9226 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
9227 {
9228 if (!pdi->is_declaration)
9229 /* Ignore subprogram DIEs that do not have a name, they are
9230 illegal. Do not emit a complaint at this point, we will
9231 do so when we convert this psymtab into a symtab. */
9232 if (pdi->name)
9233 add_partial_symbol (pdi, cu);
9234 }
9235 }
9236
9237 if (! pdi->has_children)
9238 return;
9239
9240 if (cu->language == language_ada || cu->language == language_fortran)
9241 {
9242 pdi = pdi->die_child;
9243 while (pdi != NULL)
9244 {
9245 pdi->fixup (cu);
9246 if (pdi->tag == DW_TAG_subprogram
9247 || pdi->tag == DW_TAG_inlined_subroutine
9248 || pdi->tag == DW_TAG_lexical_block)
9249 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
9250 pdi = pdi->die_sibling;
9251 }
9252 }
9253 }
9254
9255 /* Read a partial die corresponding to an enumeration type. */
9256
9257 static void
9258 add_partial_enumeration (struct partial_die_info *enum_pdi,
9259 struct dwarf2_cu *cu)
9260 {
9261 struct partial_die_info *pdi;
9262
9263 if (enum_pdi->name != NULL)
9264 add_partial_symbol (enum_pdi, cu);
9265
9266 pdi = enum_pdi->die_child;
9267 while (pdi)
9268 {
9269 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
9270 complaint (_("malformed enumerator DIE ignored"));
9271 else
9272 add_partial_symbol (pdi, cu);
9273 pdi = pdi->die_sibling;
9274 }
9275 }
9276
9277 /* Return the initial uleb128 in the die at INFO_PTR. */
9278
9279 static unsigned int
9280 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
9281 {
9282 unsigned int bytes_read;
9283
9284 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9285 }
9286
9287 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
9288 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
9289
9290 Return the corresponding abbrev, or NULL if the number is zero (indicating
9291 an empty DIE). In either case *BYTES_READ will be set to the length of
9292 the initial number. */
9293
9294 static struct abbrev_info *
9295 peek_die_abbrev (const die_reader_specs &reader,
9296 const gdb_byte *info_ptr, unsigned int *bytes_read)
9297 {
9298 dwarf2_cu *cu = reader.cu;
9299 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
9300 unsigned int abbrev_number
9301 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
9302
9303 if (abbrev_number == 0)
9304 return NULL;
9305
9306 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
9307 if (!abbrev)
9308 {
9309 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9310 " at offset %s [in module %s]"),
9311 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9312 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
9313 }
9314
9315 return abbrev;
9316 }
9317
9318 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9319 Returns a pointer to the end of a series of DIEs, terminated by an empty
9320 DIE. Any children of the skipped DIEs will also be skipped. */
9321
9322 static const gdb_byte *
9323 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
9324 {
9325 while (1)
9326 {
9327 unsigned int bytes_read;
9328 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
9329
9330 if (abbrev == NULL)
9331 return info_ptr + bytes_read;
9332 else
9333 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
9334 }
9335 }
9336
9337 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9338 INFO_PTR should point just after the initial uleb128 of a DIE, and the
9339 abbrev corresponding to that skipped uleb128 should be passed in
9340 ABBREV. Returns a pointer to this DIE's sibling, skipping any
9341 children. */
9342
9343 static const gdb_byte *
9344 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
9345 struct abbrev_info *abbrev)
9346 {
9347 unsigned int bytes_read;
9348 struct attribute attr;
9349 bfd *abfd = reader->abfd;
9350 struct dwarf2_cu *cu = reader->cu;
9351 const gdb_byte *buffer = reader->buffer;
9352 const gdb_byte *buffer_end = reader->buffer_end;
9353 unsigned int form, i;
9354
9355 for (i = 0; i < abbrev->num_attrs; i++)
9356 {
9357 /* The only abbrev we care about is DW_AT_sibling. */
9358 if (abbrev->attrs[i].name == DW_AT_sibling)
9359 {
9360 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
9361 if (attr.form == DW_FORM_ref_addr)
9362 complaint (_("ignoring absolute DW_AT_sibling"));
9363 else
9364 {
9365 sect_offset off = dwarf2_get_ref_die_offset (&attr);
9366 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
9367
9368 if (sibling_ptr < info_ptr)
9369 complaint (_("DW_AT_sibling points backwards"));
9370 else if (sibling_ptr > reader->buffer_end)
9371 dwarf2_section_buffer_overflow_complaint (reader->die_section);
9372 else
9373 return sibling_ptr;
9374 }
9375 }
9376
9377 /* If it isn't DW_AT_sibling, skip this attribute. */
9378 form = abbrev->attrs[i].form;
9379 skip_attribute:
9380 switch (form)
9381 {
9382 case DW_FORM_ref_addr:
9383 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
9384 and later it is offset sized. */
9385 if (cu->header.version == 2)
9386 info_ptr += cu->header.addr_size;
9387 else
9388 info_ptr += cu->header.offset_size;
9389 break;
9390 case DW_FORM_GNU_ref_alt:
9391 info_ptr += cu->header.offset_size;
9392 break;
9393 case DW_FORM_addr:
9394 info_ptr += cu->header.addr_size;
9395 break;
9396 case DW_FORM_data1:
9397 case DW_FORM_ref1:
9398 case DW_FORM_flag:
9399 case DW_FORM_strx1:
9400 info_ptr += 1;
9401 break;
9402 case DW_FORM_flag_present:
9403 case DW_FORM_implicit_const:
9404 break;
9405 case DW_FORM_data2:
9406 case DW_FORM_ref2:
9407 case DW_FORM_strx2:
9408 info_ptr += 2;
9409 break;
9410 case DW_FORM_strx3:
9411 info_ptr += 3;
9412 break;
9413 case DW_FORM_data4:
9414 case DW_FORM_ref4:
9415 case DW_FORM_strx4:
9416 info_ptr += 4;
9417 break;
9418 case DW_FORM_data8:
9419 case DW_FORM_ref8:
9420 case DW_FORM_ref_sig8:
9421 info_ptr += 8;
9422 break;
9423 case DW_FORM_data16:
9424 info_ptr += 16;
9425 break;
9426 case DW_FORM_string:
9427 read_direct_string (abfd, info_ptr, &bytes_read);
9428 info_ptr += bytes_read;
9429 break;
9430 case DW_FORM_sec_offset:
9431 case DW_FORM_strp:
9432 case DW_FORM_GNU_strp_alt:
9433 info_ptr += cu->header.offset_size;
9434 break;
9435 case DW_FORM_exprloc:
9436 case DW_FORM_block:
9437 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9438 info_ptr += bytes_read;
9439 break;
9440 case DW_FORM_block1:
9441 info_ptr += 1 + read_1_byte (abfd, info_ptr);
9442 break;
9443 case DW_FORM_block2:
9444 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
9445 break;
9446 case DW_FORM_block4:
9447 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
9448 break;
9449 case DW_FORM_addrx:
9450 case DW_FORM_strx:
9451 case DW_FORM_sdata:
9452 case DW_FORM_udata:
9453 case DW_FORM_ref_udata:
9454 case DW_FORM_GNU_addr_index:
9455 case DW_FORM_GNU_str_index:
9456 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
9457 break;
9458 case DW_FORM_indirect:
9459 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9460 info_ptr += bytes_read;
9461 /* We need to continue parsing from here, so just go back to
9462 the top. */
9463 goto skip_attribute;
9464
9465 default:
9466 error (_("Dwarf Error: Cannot handle %s "
9467 "in DWARF reader [in module %s]"),
9468 dwarf_form_name (form),
9469 bfd_get_filename (abfd));
9470 }
9471 }
9472
9473 if (abbrev->has_children)
9474 return skip_children (reader, info_ptr);
9475 else
9476 return info_ptr;
9477 }
9478
9479 /* Locate ORIG_PDI's sibling.
9480 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
9481
9482 static const gdb_byte *
9483 locate_pdi_sibling (const struct die_reader_specs *reader,
9484 struct partial_die_info *orig_pdi,
9485 const gdb_byte *info_ptr)
9486 {
9487 /* Do we know the sibling already? */
9488
9489 if (orig_pdi->sibling)
9490 return orig_pdi->sibling;
9491
9492 /* Are there any children to deal with? */
9493
9494 if (!orig_pdi->has_children)
9495 return info_ptr;
9496
9497 /* Skip the children the long way. */
9498
9499 return skip_children (reader, info_ptr);
9500 }
9501
9502 /* Expand this partial symbol table into a full symbol table. SELF is
9503 not NULL. */
9504
9505 static void
9506 dwarf2_read_symtab (struct partial_symtab *self,
9507 struct objfile *objfile)
9508 {
9509 struct dwarf2_per_objfile *dwarf2_per_objfile
9510 = get_dwarf2_per_objfile (objfile);
9511
9512 if (self->readin)
9513 {
9514 warning (_("bug: psymtab for %s is already read in."),
9515 self->filename);
9516 }
9517 else
9518 {
9519 if (info_verbose)
9520 {
9521 printf_filtered (_("Reading in symbols for %s..."),
9522 self->filename);
9523 gdb_flush (gdb_stdout);
9524 }
9525
9526 /* If this psymtab is constructed from a debug-only objfile, the
9527 has_section_at_zero flag will not necessarily be correct. We
9528 can get the correct value for this flag by looking at the data
9529 associated with the (presumably stripped) associated objfile. */
9530 if (objfile->separate_debug_objfile_backlink)
9531 {
9532 struct dwarf2_per_objfile *dpo_backlink
9533 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
9534
9535 dwarf2_per_objfile->has_section_at_zero
9536 = dpo_backlink->has_section_at_zero;
9537 }
9538
9539 dwarf2_per_objfile->reading_partial_symbols = 0;
9540
9541 psymtab_to_symtab_1 (self);
9542
9543 /* Finish up the debug error message. */
9544 if (info_verbose)
9545 printf_filtered (_("done.\n"));
9546 }
9547
9548 process_cu_includes (dwarf2_per_objfile);
9549 }
9550 \f
9551 /* Reading in full CUs. */
9552
9553 /* Add PER_CU to the queue. */
9554
9555 static void
9556 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
9557 enum language pretend_language)
9558 {
9559 struct dwarf2_queue_item *item;
9560
9561 per_cu->queued = 1;
9562 item = XNEW (struct dwarf2_queue_item);
9563 item->per_cu = per_cu;
9564 item->pretend_language = pretend_language;
9565 item->next = NULL;
9566
9567 if (dwarf2_queue == NULL)
9568 dwarf2_queue = item;
9569 else
9570 dwarf2_queue_tail->next = item;
9571
9572 dwarf2_queue_tail = item;
9573 }
9574
9575 /* If PER_CU is not yet queued, add it to the queue.
9576 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9577 dependency.
9578 The result is non-zero if PER_CU was queued, otherwise the result is zero
9579 meaning either PER_CU is already queued or it is already loaded.
9580
9581 N.B. There is an invariant here that if a CU is queued then it is loaded.
9582 The caller is required to load PER_CU if we return non-zero. */
9583
9584 static int
9585 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
9586 struct dwarf2_per_cu_data *per_cu,
9587 enum language pretend_language)
9588 {
9589 /* We may arrive here during partial symbol reading, if we need full
9590 DIEs to process an unusual case (e.g. template arguments). Do
9591 not queue PER_CU, just tell our caller to load its DIEs. */
9592 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
9593 {
9594 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
9595 return 1;
9596 return 0;
9597 }
9598
9599 /* Mark the dependence relation so that we don't flush PER_CU
9600 too early. */
9601 if (dependent_cu != NULL)
9602 dwarf2_add_dependence (dependent_cu, per_cu);
9603
9604 /* If it's already on the queue, we have nothing to do. */
9605 if (per_cu->queued)
9606 return 0;
9607
9608 /* If the compilation unit is already loaded, just mark it as
9609 used. */
9610 if (per_cu->cu != NULL)
9611 {
9612 per_cu->cu->last_used = 0;
9613 return 0;
9614 }
9615
9616 /* Add it to the queue. */
9617 queue_comp_unit (per_cu, pretend_language);
9618
9619 return 1;
9620 }
9621
9622 /* Process the queue. */
9623
9624 static void
9625 process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
9626 {
9627 struct dwarf2_queue_item *item, *next_item;
9628
9629 if (dwarf_read_debug)
9630 {
9631 fprintf_unfiltered (gdb_stdlog,
9632 "Expanding one or more symtabs of objfile %s ...\n",
9633 objfile_name (dwarf2_per_objfile->objfile));
9634 }
9635
9636 /* The queue starts out with one item, but following a DIE reference
9637 may load a new CU, adding it to the end of the queue. */
9638 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
9639 {
9640 if ((dwarf2_per_objfile->using_index
9641 ? !item->per_cu->v.quick->compunit_symtab
9642 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
9643 /* Skip dummy CUs. */
9644 && item->per_cu->cu != NULL)
9645 {
9646 struct dwarf2_per_cu_data *per_cu = item->per_cu;
9647 unsigned int debug_print_threshold;
9648 char buf[100];
9649
9650 if (per_cu->is_debug_types)
9651 {
9652 struct signatured_type *sig_type =
9653 (struct signatured_type *) per_cu;
9654
9655 sprintf (buf, "TU %s at offset %s",
9656 hex_string (sig_type->signature),
9657 sect_offset_str (per_cu->sect_off));
9658 /* There can be 100s of TUs.
9659 Only print them in verbose mode. */
9660 debug_print_threshold = 2;
9661 }
9662 else
9663 {
9664 sprintf (buf, "CU at offset %s",
9665 sect_offset_str (per_cu->sect_off));
9666 debug_print_threshold = 1;
9667 }
9668
9669 if (dwarf_read_debug >= debug_print_threshold)
9670 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
9671
9672 if (per_cu->is_debug_types)
9673 process_full_type_unit (per_cu, item->pretend_language);
9674 else
9675 process_full_comp_unit (per_cu, item->pretend_language);
9676
9677 if (dwarf_read_debug >= debug_print_threshold)
9678 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
9679 }
9680
9681 item->per_cu->queued = 0;
9682 next_item = item->next;
9683 xfree (item);
9684 }
9685
9686 dwarf2_queue_tail = NULL;
9687
9688 if (dwarf_read_debug)
9689 {
9690 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
9691 objfile_name (dwarf2_per_objfile->objfile));
9692 }
9693 }
9694
9695 /* Read in full symbols for PST, and anything it depends on. */
9696
9697 static void
9698 psymtab_to_symtab_1 (struct partial_symtab *pst)
9699 {
9700 struct dwarf2_per_cu_data *per_cu;
9701 int i;
9702
9703 if (pst->readin)
9704 return;
9705
9706 for (i = 0; i < pst->number_of_dependencies; i++)
9707 if (!pst->dependencies[i]->readin
9708 && pst->dependencies[i]->user == NULL)
9709 {
9710 /* Inform about additional files that need to be read in. */
9711 if (info_verbose)
9712 {
9713 /* FIXME: i18n: Need to make this a single string. */
9714 fputs_filtered (" ", gdb_stdout);
9715 wrap_here ("");
9716 fputs_filtered ("and ", gdb_stdout);
9717 wrap_here ("");
9718 printf_filtered ("%s...", pst->dependencies[i]->filename);
9719 wrap_here (""); /* Flush output. */
9720 gdb_flush (gdb_stdout);
9721 }
9722 psymtab_to_symtab_1 (pst->dependencies[i]);
9723 }
9724
9725 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
9726
9727 if (per_cu == NULL)
9728 {
9729 /* It's an include file, no symbols to read for it.
9730 Everything is in the parent symtab. */
9731 pst->readin = 1;
9732 return;
9733 }
9734
9735 dw2_do_instantiate_symtab (per_cu, false);
9736 }
9737
9738 /* Trivial hash function for die_info: the hash value of a DIE
9739 is its offset in .debug_info for this objfile. */
9740
9741 static hashval_t
9742 die_hash (const void *item)
9743 {
9744 const struct die_info *die = (const struct die_info *) item;
9745
9746 return to_underlying (die->sect_off);
9747 }
9748
9749 /* Trivial comparison function for die_info structures: two DIEs
9750 are equal if they have the same offset. */
9751
9752 static int
9753 die_eq (const void *item_lhs, const void *item_rhs)
9754 {
9755 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9756 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
9757
9758 return die_lhs->sect_off == die_rhs->sect_off;
9759 }
9760
9761 /* die_reader_func for load_full_comp_unit.
9762 This is identical to read_signatured_type_reader,
9763 but is kept separate for now. */
9764
9765 static void
9766 load_full_comp_unit_reader (const struct die_reader_specs *reader,
9767 const gdb_byte *info_ptr,
9768 struct die_info *comp_unit_die,
9769 int has_children,
9770 void *data)
9771 {
9772 struct dwarf2_cu *cu = reader->cu;
9773 enum language *language_ptr = (enum language *) data;
9774
9775 gdb_assert (cu->die_hash == NULL);
9776 cu->die_hash =
9777 htab_create_alloc_ex (cu->header.length / 12,
9778 die_hash,
9779 die_eq,
9780 NULL,
9781 &cu->comp_unit_obstack,
9782 hashtab_obstack_allocate,
9783 dummy_obstack_deallocate);
9784
9785 if (has_children)
9786 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
9787 &info_ptr, comp_unit_die);
9788 cu->dies = comp_unit_die;
9789 /* comp_unit_die is not stored in die_hash, no need. */
9790
9791 /* We try not to read any attributes in this function, because not
9792 all CUs needed for references have been loaded yet, and symbol
9793 table processing isn't initialized. But we have to set the CU language,
9794 or we won't be able to build types correctly.
9795 Similarly, if we do not read the producer, we can not apply
9796 producer-specific interpretation. */
9797 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
9798 }
9799
9800 /* Load the DIEs associated with PER_CU into memory. */
9801
9802 static void
9803 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
9804 bool skip_partial,
9805 enum language pretend_language)
9806 {
9807 gdb_assert (! this_cu->is_debug_types);
9808
9809 init_cutu_and_read_dies (this_cu, NULL, 1, 1, skip_partial,
9810 load_full_comp_unit_reader, &pretend_language);
9811 }
9812
9813 /* Add a DIE to the delayed physname list. */
9814
9815 static void
9816 add_to_method_list (struct type *type, int fnfield_index, int index,
9817 const char *name, struct die_info *die,
9818 struct dwarf2_cu *cu)
9819 {
9820 struct delayed_method_info mi;
9821 mi.type = type;
9822 mi.fnfield_index = fnfield_index;
9823 mi.index = index;
9824 mi.name = name;
9825 mi.die = die;
9826 cu->method_list.push_back (mi);
9827 }
9828
9829 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9830 "const" / "volatile". If so, decrements LEN by the length of the
9831 modifier and return true. Otherwise return false. */
9832
9833 template<size_t N>
9834 static bool
9835 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9836 {
9837 size_t mod_len = sizeof (mod) - 1;
9838 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9839 {
9840 len -= mod_len;
9841 return true;
9842 }
9843 return false;
9844 }
9845
9846 /* Compute the physnames of any methods on the CU's method list.
9847
9848 The computation of method physnames is delayed in order to avoid the
9849 (bad) condition that one of the method's formal parameters is of an as yet
9850 incomplete type. */
9851
9852 static void
9853 compute_delayed_physnames (struct dwarf2_cu *cu)
9854 {
9855 /* Only C++ delays computing physnames. */
9856 if (cu->method_list.empty ())
9857 return;
9858 gdb_assert (cu->language == language_cplus);
9859
9860 for (const delayed_method_info &mi : cu->method_list)
9861 {
9862 const char *physname;
9863 struct fn_fieldlist *fn_flp
9864 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9865 physname = dwarf2_physname (mi.name, mi.die, cu);
9866 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
9867 = physname ? physname : "";
9868
9869 /* Since there's no tag to indicate whether a method is a
9870 const/volatile overload, extract that information out of the
9871 demangled name. */
9872 if (physname != NULL)
9873 {
9874 size_t len = strlen (physname);
9875
9876 while (1)
9877 {
9878 if (physname[len] == ')') /* shortcut */
9879 break;
9880 else if (check_modifier (physname, len, " const"))
9881 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
9882 else if (check_modifier (physname, len, " volatile"))
9883 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
9884 else
9885 break;
9886 }
9887 }
9888 }
9889
9890 /* The list is no longer needed. */
9891 cu->method_list.clear ();
9892 }
9893
9894 /* Go objects should be embedded in a DW_TAG_module DIE,
9895 and it's not clear if/how imported objects will appear.
9896 To keep Go support simple until that's worked out,
9897 go back through what we've read and create something usable.
9898 We could do this while processing each DIE, and feels kinda cleaner,
9899 but that way is more invasive.
9900 This is to, for example, allow the user to type "p var" or "b main"
9901 without having to specify the package name, and allow lookups
9902 of module.object to work in contexts that use the expression
9903 parser. */
9904
9905 static void
9906 fixup_go_packaging (struct dwarf2_cu *cu)
9907 {
9908 char *package_name = NULL;
9909 struct pending *list;
9910 int i;
9911
9912 for (list = *cu->get_builder ()->get_global_symbols ();
9913 list != NULL;
9914 list = list->next)
9915 {
9916 for (i = 0; i < list->nsyms; ++i)
9917 {
9918 struct symbol *sym = list->symbol[i];
9919
9920 if (sym->language () == language_go
9921 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9922 {
9923 char *this_package_name = go_symbol_package_name (sym);
9924
9925 if (this_package_name == NULL)
9926 continue;
9927 if (package_name == NULL)
9928 package_name = this_package_name;
9929 else
9930 {
9931 struct objfile *objfile
9932 = cu->per_cu->dwarf2_per_objfile->objfile;
9933 if (strcmp (package_name, this_package_name) != 0)
9934 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
9935 (symbol_symtab (sym) != NULL
9936 ? symtab_to_filename_for_display
9937 (symbol_symtab (sym))
9938 : objfile_name (objfile)),
9939 this_package_name, package_name);
9940 xfree (this_package_name);
9941 }
9942 }
9943 }
9944 }
9945
9946 if (package_name != NULL)
9947 {
9948 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9949 const char *saved_package_name
9950 = obstack_strdup (&objfile->per_bfd->storage_obstack, package_name);
9951 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9952 saved_package_name);
9953 struct symbol *sym;
9954
9955 sym = allocate_symbol (objfile);
9956 sym->set_language (language_go, &objfile->objfile_obstack);
9957 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
9958 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9959 e.g., "main" finds the "main" module and not C's main(). */
9960 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9961 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9962 SYMBOL_TYPE (sym) = type;
9963
9964 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
9965
9966 xfree (package_name);
9967 }
9968 }
9969
9970 /* Allocate a fully-qualified name consisting of the two parts on the
9971 obstack. */
9972
9973 static const char *
9974 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9975 {
9976 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9977 }
9978
9979 /* A helper that allocates a struct discriminant_info to attach to a
9980 union type. */
9981
9982 static struct discriminant_info *
9983 alloc_discriminant_info (struct type *type, int discriminant_index,
9984 int default_index)
9985 {
9986 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9987 gdb_assert (discriminant_index == -1
9988 || (discriminant_index >= 0
9989 && discriminant_index < TYPE_NFIELDS (type)));
9990 gdb_assert (default_index == -1
9991 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
9992
9993 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
9994
9995 struct discriminant_info *disc
9996 = ((struct discriminant_info *)
9997 TYPE_ZALLOC (type,
9998 offsetof (struct discriminant_info, discriminants)
9999 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
10000 disc->default_index = default_index;
10001 disc->discriminant_index = discriminant_index;
10002
10003 struct dynamic_prop prop;
10004 prop.kind = PROP_UNDEFINED;
10005 prop.data.baton = disc;
10006
10007 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
10008
10009 return disc;
10010 }
10011
10012 /* Some versions of rustc emitted enums in an unusual way.
10013
10014 Ordinary enums were emitted as unions. The first element of each
10015 structure in the union was named "RUST$ENUM$DISR". This element
10016 held the discriminant.
10017
10018 These versions of Rust also implemented the "non-zero"
10019 optimization. When the enum had two values, and one is empty and
10020 the other holds a pointer that cannot be zero, the pointer is used
10021 as the discriminant, with a zero value meaning the empty variant.
10022 Here, the union's first member is of the form
10023 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
10024 where the fieldnos are the indices of the fields that should be
10025 traversed in order to find the field (which may be several fields deep)
10026 and the variantname is the name of the variant of the case when the
10027 field is zero.
10028
10029 This function recognizes whether TYPE is of one of these forms,
10030 and, if so, smashes it to be a variant type. */
10031
10032 static void
10033 quirk_rust_enum (struct type *type, struct objfile *objfile)
10034 {
10035 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
10036
10037 /* We don't need to deal with empty enums. */
10038 if (TYPE_NFIELDS (type) == 0)
10039 return;
10040
10041 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
10042 if (TYPE_NFIELDS (type) == 1
10043 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
10044 {
10045 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
10046
10047 /* Decode the field name to find the offset of the
10048 discriminant. */
10049 ULONGEST bit_offset = 0;
10050 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
10051 while (name[0] >= '0' && name[0] <= '9')
10052 {
10053 char *tail;
10054 unsigned long index = strtoul (name, &tail, 10);
10055 name = tail;
10056 if (*name != '$'
10057 || index >= TYPE_NFIELDS (field_type)
10058 || (TYPE_FIELD_LOC_KIND (field_type, index)
10059 != FIELD_LOC_KIND_BITPOS))
10060 {
10061 complaint (_("Could not parse Rust enum encoding string \"%s\""
10062 "[in module %s]"),
10063 TYPE_FIELD_NAME (type, 0),
10064 objfile_name (objfile));
10065 return;
10066 }
10067 ++name;
10068
10069 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
10070 field_type = TYPE_FIELD_TYPE (field_type, index);
10071 }
10072
10073 /* Make a union to hold the variants. */
10074 struct type *union_type = alloc_type (objfile);
10075 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10076 TYPE_NFIELDS (union_type) = 3;
10077 TYPE_FIELDS (union_type)
10078 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
10079 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
10080 set_type_align (union_type, TYPE_RAW_ALIGN (type));
10081
10082 /* Put the discriminant must at index 0. */
10083 TYPE_FIELD_TYPE (union_type, 0) = field_type;
10084 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10085 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10086 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
10087
10088 /* The order of fields doesn't really matter, so put the real
10089 field at index 1 and the data-less field at index 2. */
10090 struct discriminant_info *disc
10091 = alloc_discriminant_info (union_type, 0, 1);
10092 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
10093 TYPE_FIELD_NAME (union_type, 1)
10094 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
10095 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
10096 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
10097 TYPE_FIELD_NAME (union_type, 1));
10098
10099 const char *dataless_name
10100 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
10101 name);
10102 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
10103 dataless_name);
10104 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
10105 /* NAME points into the original discriminant name, which
10106 already has the correct lifetime. */
10107 TYPE_FIELD_NAME (union_type, 2) = name;
10108 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
10109 disc->discriminants[2] = 0;
10110
10111 /* Smash this type to be a structure type. We have to do this
10112 because the type has already been recorded. */
10113 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10114 TYPE_NFIELDS (type) = 1;
10115 TYPE_FIELDS (type)
10116 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
10117
10118 /* Install the variant part. */
10119 TYPE_FIELD_TYPE (type, 0) = union_type;
10120 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10121 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10122 }
10123 /* A union with a single anonymous field is probably an old-style
10124 univariant enum. */
10125 else if (TYPE_NFIELDS (type) == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
10126 {
10127 /* Smash this type to be a structure type. We have to do this
10128 because the type has already been recorded. */
10129 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10130
10131 /* Make a union to hold the variants. */
10132 struct type *union_type = alloc_type (objfile);
10133 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10134 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
10135 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
10136 set_type_align (union_type, TYPE_RAW_ALIGN (type));
10137 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
10138
10139 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
10140 const char *variant_name
10141 = rust_last_path_segment (TYPE_NAME (field_type));
10142 TYPE_FIELD_NAME (union_type, 0) = variant_name;
10143 TYPE_NAME (field_type)
10144 = rust_fully_qualify (&objfile->objfile_obstack,
10145 TYPE_NAME (type), variant_name);
10146
10147 /* Install the union in the outer struct type. */
10148 TYPE_NFIELDS (type) = 1;
10149 TYPE_FIELDS (type)
10150 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
10151 TYPE_FIELD_TYPE (type, 0) = union_type;
10152 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10153 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10154
10155 alloc_discriminant_info (union_type, -1, 0);
10156 }
10157 else
10158 {
10159 struct type *disr_type = nullptr;
10160 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
10161 {
10162 disr_type = TYPE_FIELD_TYPE (type, i);
10163
10164 if (TYPE_CODE (disr_type) != TYPE_CODE_STRUCT)
10165 {
10166 /* All fields of a true enum will be structs. */
10167 return;
10168 }
10169 else if (TYPE_NFIELDS (disr_type) == 0)
10170 {
10171 /* Could be data-less variant, so keep going. */
10172 disr_type = nullptr;
10173 }
10174 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
10175 "RUST$ENUM$DISR") != 0)
10176 {
10177 /* Not a Rust enum. */
10178 return;
10179 }
10180 else
10181 {
10182 /* Found one. */
10183 break;
10184 }
10185 }
10186
10187 /* If we got here without a discriminant, then it's probably
10188 just a union. */
10189 if (disr_type == nullptr)
10190 return;
10191
10192 /* Smash this type to be a structure type. We have to do this
10193 because the type has already been recorded. */
10194 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10195
10196 /* Make a union to hold the variants. */
10197 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
10198 struct type *union_type = alloc_type (objfile);
10199 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10200 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
10201 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
10202 set_type_align (union_type, TYPE_RAW_ALIGN (type));
10203 TYPE_FIELDS (union_type)
10204 = (struct field *) TYPE_ZALLOC (union_type,
10205 (TYPE_NFIELDS (union_type)
10206 * sizeof (struct field)));
10207
10208 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
10209 TYPE_NFIELDS (type) * sizeof (struct field));
10210
10211 /* Install the discriminant at index 0 in the union. */
10212 TYPE_FIELD (union_type, 0) = *disr_field;
10213 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10214 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10215
10216 /* Install the union in the outer struct type. */
10217 TYPE_FIELD_TYPE (type, 0) = union_type;
10218 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10219 TYPE_NFIELDS (type) = 1;
10220
10221 /* Set the size and offset of the union type. */
10222 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10223
10224 /* We need a way to find the correct discriminant given a
10225 variant name. For convenience we build a map here. */
10226 struct type *enum_type = FIELD_TYPE (*disr_field);
10227 std::unordered_map<std::string, ULONGEST> discriminant_map;
10228 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
10229 {
10230 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
10231 {
10232 const char *name
10233 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
10234 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
10235 }
10236 }
10237
10238 int n_fields = TYPE_NFIELDS (union_type);
10239 struct discriminant_info *disc
10240 = alloc_discriminant_info (union_type, 0, -1);
10241 /* Skip the discriminant here. */
10242 for (int i = 1; i < n_fields; ++i)
10243 {
10244 /* Find the final word in the name of this variant's type.
10245 That name can be used to look up the correct
10246 discriminant. */
10247 const char *variant_name
10248 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
10249 i)));
10250
10251 auto iter = discriminant_map.find (variant_name);
10252 if (iter != discriminant_map.end ())
10253 disc->discriminants[i] = iter->second;
10254
10255 /* Remove the discriminant field, if it exists. */
10256 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
10257 if (TYPE_NFIELDS (sub_type) > 0)
10258 {
10259 --TYPE_NFIELDS (sub_type);
10260 ++TYPE_FIELDS (sub_type);
10261 }
10262 TYPE_FIELD_NAME (union_type, i) = variant_name;
10263 TYPE_NAME (sub_type)
10264 = rust_fully_qualify (&objfile->objfile_obstack,
10265 TYPE_NAME (type), variant_name);
10266 }
10267 }
10268 }
10269
10270 /* Rewrite some Rust unions to be structures with variants parts. */
10271
10272 static void
10273 rust_union_quirks (struct dwarf2_cu *cu)
10274 {
10275 gdb_assert (cu->language == language_rust);
10276 for (type *type_ : cu->rust_unions)
10277 quirk_rust_enum (type_, cu->per_cu->dwarf2_per_objfile->objfile);
10278 /* We don't need this any more. */
10279 cu->rust_unions.clear ();
10280 }
10281
10282 /* Return the symtab for PER_CU. This works properly regardless of
10283 whether we're using the index or psymtabs. */
10284
10285 static struct compunit_symtab *
10286 get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
10287 {
10288 return (per_cu->dwarf2_per_objfile->using_index
10289 ? per_cu->v.quick->compunit_symtab
10290 : per_cu->v.psymtab->compunit_symtab);
10291 }
10292
10293 /* A helper function for computing the list of all symbol tables
10294 included by PER_CU. */
10295
10296 static void
10297 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
10298 htab_t all_children, htab_t all_type_symtabs,
10299 struct dwarf2_per_cu_data *per_cu,
10300 struct compunit_symtab *immediate_parent)
10301 {
10302 void **slot;
10303 struct compunit_symtab *cust;
10304
10305 slot = htab_find_slot (all_children, per_cu, INSERT);
10306 if (*slot != NULL)
10307 {
10308 /* This inclusion and its children have been processed. */
10309 return;
10310 }
10311
10312 *slot = per_cu;
10313 /* Only add a CU if it has a symbol table. */
10314 cust = get_compunit_symtab (per_cu);
10315 if (cust != NULL)
10316 {
10317 /* If this is a type unit only add its symbol table if we haven't
10318 seen it yet (type unit per_cu's can share symtabs). */
10319 if (per_cu->is_debug_types)
10320 {
10321 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
10322 if (*slot == NULL)
10323 {
10324 *slot = cust;
10325 result->push_back (cust);
10326 if (cust->user == NULL)
10327 cust->user = immediate_parent;
10328 }
10329 }
10330 else
10331 {
10332 result->push_back (cust);
10333 if (cust->user == NULL)
10334 cust->user = immediate_parent;
10335 }
10336 }
10337
10338 if (!per_cu->imported_symtabs_empty ())
10339 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
10340 {
10341 recursively_compute_inclusions (result, all_children,
10342 all_type_symtabs, ptr, cust);
10343 }
10344 }
10345
10346 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
10347 PER_CU. */
10348
10349 static void
10350 compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
10351 {
10352 gdb_assert (! per_cu->is_debug_types);
10353
10354 if (!per_cu->imported_symtabs_empty ())
10355 {
10356 int len;
10357 std::vector<compunit_symtab *> result_symtabs;
10358 htab_t all_children, all_type_symtabs;
10359 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
10360
10361 /* If we don't have a symtab, we can just skip this case. */
10362 if (cust == NULL)
10363 return;
10364
10365 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10366 NULL, xcalloc, xfree);
10367 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10368 NULL, xcalloc, xfree);
10369
10370 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
10371 {
10372 recursively_compute_inclusions (&result_symtabs, all_children,
10373 all_type_symtabs, ptr, cust);
10374 }
10375
10376 /* Now we have a transitive closure of all the included symtabs. */
10377 len = result_symtabs.size ();
10378 cust->includes
10379 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
10380 struct compunit_symtab *, len + 1);
10381 memcpy (cust->includes, result_symtabs.data (),
10382 len * sizeof (compunit_symtab *));
10383 cust->includes[len] = NULL;
10384
10385 htab_delete (all_children);
10386 htab_delete (all_type_symtabs);
10387 }
10388 }
10389
10390 /* Compute the 'includes' field for the symtabs of all the CUs we just
10391 read. */
10392
10393 static void
10394 process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
10395 {
10396 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->just_read_cus)
10397 {
10398 if (! iter->is_debug_types)
10399 compute_compunit_symtab_includes (iter);
10400 }
10401
10402 dwarf2_per_objfile->just_read_cus.clear ();
10403 }
10404
10405 /* Generate full symbol information for PER_CU, whose DIEs have
10406 already been loaded into memory. */
10407
10408 static void
10409 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
10410 enum language pretend_language)
10411 {
10412 struct dwarf2_cu *cu = per_cu->cu;
10413 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10414 struct objfile *objfile = dwarf2_per_objfile->objfile;
10415 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10416 CORE_ADDR lowpc, highpc;
10417 struct compunit_symtab *cust;
10418 CORE_ADDR baseaddr;
10419 struct block *static_block;
10420 CORE_ADDR addr;
10421
10422 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10423
10424 /* Clear the list here in case something was left over. */
10425 cu->method_list.clear ();
10426
10427 cu->language = pretend_language;
10428 cu->language_defn = language_def (cu->language);
10429
10430 /* Do line number decoding in read_file_scope () */
10431 process_die (cu->dies, cu);
10432
10433 /* For now fudge the Go package. */
10434 if (cu->language == language_go)
10435 fixup_go_packaging (cu);
10436
10437 /* Now that we have processed all the DIEs in the CU, all the types
10438 should be complete, and it should now be safe to compute all of the
10439 physnames. */
10440 compute_delayed_physnames (cu);
10441
10442 if (cu->language == language_rust)
10443 rust_union_quirks (cu);
10444
10445 /* Some compilers don't define a DW_AT_high_pc attribute for the
10446 compilation unit. If the DW_AT_high_pc is missing, synthesize
10447 it, by scanning the DIE's below the compilation unit. */
10448 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
10449
10450 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
10451 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
10452
10453 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10454 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10455 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10456 addrmap to help ensure it has an accurate map of pc values belonging to
10457 this comp unit. */
10458 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10459
10460 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
10461 SECT_OFF_TEXT (objfile),
10462 0);
10463
10464 if (cust != NULL)
10465 {
10466 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
10467
10468 /* Set symtab language to language from DW_AT_language. If the
10469 compilation is from a C file generated by language preprocessors, do
10470 not set the language if it was already deduced by start_subfile. */
10471 if (!(cu->language == language_c
10472 && COMPUNIT_FILETABS (cust)->language != language_unknown))
10473 COMPUNIT_FILETABS (cust)->language = cu->language;
10474
10475 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10476 produce DW_AT_location with location lists but it can be possibly
10477 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10478 there were bugs in prologue debug info, fixed later in GCC-4.5
10479 by "unwind info for epilogues" patch (which is not directly related).
10480
10481 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10482 needed, it would be wrong due to missing DW_AT_producer there.
10483
10484 Still one can confuse GDB by using non-standard GCC compilation
10485 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
10486 */
10487 if (cu->has_loclist && gcc_4_minor >= 5)
10488 cust->locations_valid = 1;
10489
10490 if (gcc_4_minor >= 5)
10491 cust->epilogue_unwind_valid = 1;
10492
10493 cust->call_site_htab = cu->call_site_htab;
10494 }
10495
10496 if (dwarf2_per_objfile->using_index)
10497 per_cu->v.quick->compunit_symtab = cust;
10498 else
10499 {
10500 struct partial_symtab *pst = per_cu->v.psymtab;
10501 pst->compunit_symtab = cust;
10502 pst->readin = 1;
10503 }
10504
10505 /* Push it for inclusion processing later. */
10506 dwarf2_per_objfile->just_read_cus.push_back (per_cu);
10507
10508 /* Not needed any more. */
10509 cu->reset_builder ();
10510 }
10511
10512 /* Generate full symbol information for type unit PER_CU, whose DIEs have
10513 already been loaded into memory. */
10514
10515 static void
10516 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
10517 enum language pretend_language)
10518 {
10519 struct dwarf2_cu *cu = per_cu->cu;
10520 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10521 struct objfile *objfile = dwarf2_per_objfile->objfile;
10522 struct compunit_symtab *cust;
10523 struct signatured_type *sig_type;
10524
10525 gdb_assert (per_cu->is_debug_types);
10526 sig_type = (struct signatured_type *) per_cu;
10527
10528 /* Clear the list here in case something was left over. */
10529 cu->method_list.clear ();
10530
10531 cu->language = pretend_language;
10532 cu->language_defn = language_def (cu->language);
10533
10534 /* The symbol tables are set up in read_type_unit_scope. */
10535 process_die (cu->dies, cu);
10536
10537 /* For now fudge the Go package. */
10538 if (cu->language == language_go)
10539 fixup_go_packaging (cu);
10540
10541 /* Now that we have processed all the DIEs in the CU, all the types
10542 should be complete, and it should now be safe to compute all of the
10543 physnames. */
10544 compute_delayed_physnames (cu);
10545
10546 if (cu->language == language_rust)
10547 rust_union_quirks (cu);
10548
10549 /* TUs share symbol tables.
10550 If this is the first TU to use this symtab, complete the construction
10551 of it with end_expandable_symtab. Otherwise, complete the addition of
10552 this TU's symbols to the existing symtab. */
10553 if (sig_type->type_unit_group->compunit_symtab == NULL)
10554 {
10555 buildsym_compunit *builder = cu->get_builder ();
10556 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
10557 sig_type->type_unit_group->compunit_symtab = cust;
10558
10559 if (cust != NULL)
10560 {
10561 /* Set symtab language to language from DW_AT_language. If the
10562 compilation is from a C file generated by language preprocessors,
10563 do not set the language if it was already deduced by
10564 start_subfile. */
10565 if (!(cu->language == language_c
10566 && COMPUNIT_FILETABS (cust)->language != language_c))
10567 COMPUNIT_FILETABS (cust)->language = cu->language;
10568 }
10569 }
10570 else
10571 {
10572 cu->get_builder ()->augment_type_symtab ();
10573 cust = sig_type->type_unit_group->compunit_symtab;
10574 }
10575
10576 if (dwarf2_per_objfile->using_index)
10577 per_cu->v.quick->compunit_symtab = cust;
10578 else
10579 {
10580 struct partial_symtab *pst = per_cu->v.psymtab;
10581 pst->compunit_symtab = cust;
10582 pst->readin = 1;
10583 }
10584
10585 /* Not needed any more. */
10586 cu->reset_builder ();
10587 }
10588
10589 /* Process an imported unit DIE. */
10590
10591 static void
10592 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10593 {
10594 struct attribute *attr;
10595
10596 /* For now we don't handle imported units in type units. */
10597 if (cu->per_cu->is_debug_types)
10598 {
10599 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10600 " supported in type units [in module %s]"),
10601 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
10602 }
10603
10604 attr = dwarf2_attr (die, DW_AT_import, cu);
10605 if (attr != NULL)
10606 {
10607 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10608 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10609 dwarf2_per_cu_data *per_cu
10610 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
10611 cu->per_cu->dwarf2_per_objfile);
10612
10613 /* If necessary, add it to the queue and load its DIEs. */
10614 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
10615 load_full_comp_unit (per_cu, false, cu->language);
10616
10617 cu->per_cu->imported_symtabs_push (per_cu);
10618 }
10619 }
10620
10621 /* RAII object that represents a process_die scope: i.e.,
10622 starts/finishes processing a DIE. */
10623 class process_die_scope
10624 {
10625 public:
10626 process_die_scope (die_info *die, dwarf2_cu *cu)
10627 : m_die (die), m_cu (cu)
10628 {
10629 /* We should only be processing DIEs not already in process. */
10630 gdb_assert (!m_die->in_process);
10631 m_die->in_process = true;
10632 }
10633
10634 ~process_die_scope ()
10635 {
10636 m_die->in_process = false;
10637
10638 /* If we're done processing the DIE for the CU that owns the line
10639 header, we don't need the line header anymore. */
10640 if (m_cu->line_header_die_owner == m_die)
10641 {
10642 delete m_cu->line_header;
10643 m_cu->line_header = NULL;
10644 m_cu->line_header_die_owner = NULL;
10645 }
10646 }
10647
10648 private:
10649 die_info *m_die;
10650 dwarf2_cu *m_cu;
10651 };
10652
10653 /* Process a die and its children. */
10654
10655 static void
10656 process_die (struct die_info *die, struct dwarf2_cu *cu)
10657 {
10658 process_die_scope scope (die, cu);
10659
10660 switch (die->tag)
10661 {
10662 case DW_TAG_padding:
10663 break;
10664 case DW_TAG_compile_unit:
10665 case DW_TAG_partial_unit:
10666 read_file_scope (die, cu);
10667 break;
10668 case DW_TAG_type_unit:
10669 read_type_unit_scope (die, cu);
10670 break;
10671 case DW_TAG_subprogram:
10672 /* Nested subprograms in Fortran get a prefix. */
10673 if (cu->language == language_fortran
10674 && die->parent != NULL
10675 && die->parent->tag == DW_TAG_subprogram)
10676 cu->processing_has_namespace_info = true;
10677 /* Fall through. */
10678 case DW_TAG_inlined_subroutine:
10679 read_func_scope (die, cu);
10680 break;
10681 case DW_TAG_lexical_block:
10682 case DW_TAG_try_block:
10683 case DW_TAG_catch_block:
10684 read_lexical_block_scope (die, cu);
10685 break;
10686 case DW_TAG_call_site:
10687 case DW_TAG_GNU_call_site:
10688 read_call_site_scope (die, cu);
10689 break;
10690 case DW_TAG_class_type:
10691 case DW_TAG_interface_type:
10692 case DW_TAG_structure_type:
10693 case DW_TAG_union_type:
10694 process_structure_scope (die, cu);
10695 break;
10696 case DW_TAG_enumeration_type:
10697 process_enumeration_scope (die, cu);
10698 break;
10699
10700 /* These dies have a type, but processing them does not create
10701 a symbol or recurse to process the children. Therefore we can
10702 read them on-demand through read_type_die. */
10703 case DW_TAG_subroutine_type:
10704 case DW_TAG_set_type:
10705 case DW_TAG_array_type:
10706 case DW_TAG_pointer_type:
10707 case DW_TAG_ptr_to_member_type:
10708 case DW_TAG_reference_type:
10709 case DW_TAG_rvalue_reference_type:
10710 case DW_TAG_string_type:
10711 break;
10712
10713 case DW_TAG_base_type:
10714 case DW_TAG_subrange_type:
10715 case DW_TAG_typedef:
10716 /* Add a typedef symbol for the type definition, if it has a
10717 DW_AT_name. */
10718 new_symbol (die, read_type_die (die, cu), cu);
10719 break;
10720 case DW_TAG_common_block:
10721 read_common_block (die, cu);
10722 break;
10723 case DW_TAG_common_inclusion:
10724 break;
10725 case DW_TAG_namespace:
10726 cu->processing_has_namespace_info = true;
10727 read_namespace (die, cu);
10728 break;
10729 case DW_TAG_module:
10730 cu->processing_has_namespace_info = true;
10731 read_module (die, cu);
10732 break;
10733 case DW_TAG_imported_declaration:
10734 cu->processing_has_namespace_info = true;
10735 if (read_namespace_alias (die, cu))
10736 break;
10737 /* The declaration is not a global namespace alias. */
10738 /* Fall through. */
10739 case DW_TAG_imported_module:
10740 cu->processing_has_namespace_info = true;
10741 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10742 || cu->language != language_fortran))
10743 complaint (_("Tag '%s' has unexpected children"),
10744 dwarf_tag_name (die->tag));
10745 read_import_statement (die, cu);
10746 break;
10747
10748 case DW_TAG_imported_unit:
10749 process_imported_unit_die (die, cu);
10750 break;
10751
10752 case DW_TAG_variable:
10753 read_variable (die, cu);
10754 break;
10755
10756 default:
10757 new_symbol (die, NULL, cu);
10758 break;
10759 }
10760 }
10761 \f
10762 /* DWARF name computation. */
10763
10764 /* A helper function for dwarf2_compute_name which determines whether DIE
10765 needs to have the name of the scope prepended to the name listed in the
10766 die. */
10767
10768 static int
10769 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10770 {
10771 struct attribute *attr;
10772
10773 switch (die->tag)
10774 {
10775 case DW_TAG_namespace:
10776 case DW_TAG_typedef:
10777 case DW_TAG_class_type:
10778 case DW_TAG_interface_type:
10779 case DW_TAG_structure_type:
10780 case DW_TAG_union_type:
10781 case DW_TAG_enumeration_type:
10782 case DW_TAG_enumerator:
10783 case DW_TAG_subprogram:
10784 case DW_TAG_inlined_subroutine:
10785 case DW_TAG_member:
10786 case DW_TAG_imported_declaration:
10787 return 1;
10788
10789 case DW_TAG_variable:
10790 case DW_TAG_constant:
10791 /* We only need to prefix "globally" visible variables. These include
10792 any variable marked with DW_AT_external or any variable that
10793 lives in a namespace. [Variables in anonymous namespaces
10794 require prefixing, but they are not DW_AT_external.] */
10795
10796 if (dwarf2_attr (die, DW_AT_specification, cu))
10797 {
10798 struct dwarf2_cu *spec_cu = cu;
10799
10800 return die_needs_namespace (die_specification (die, &spec_cu),
10801 spec_cu);
10802 }
10803
10804 attr = dwarf2_attr (die, DW_AT_external, cu);
10805 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10806 && die->parent->tag != DW_TAG_module)
10807 return 0;
10808 /* A variable in a lexical block of some kind does not need a
10809 namespace, even though in C++ such variables may be external
10810 and have a mangled name. */
10811 if (die->parent->tag == DW_TAG_lexical_block
10812 || die->parent->tag == DW_TAG_try_block
10813 || die->parent->tag == DW_TAG_catch_block
10814 || die->parent->tag == DW_TAG_subprogram)
10815 return 0;
10816 return 1;
10817
10818 default:
10819 return 0;
10820 }
10821 }
10822
10823 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10824 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10825 defined for the given DIE. */
10826
10827 static struct attribute *
10828 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10829 {
10830 struct attribute *attr;
10831
10832 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10833 if (attr == NULL)
10834 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10835
10836 return attr;
10837 }
10838
10839 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10840 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10841 defined for the given DIE. */
10842
10843 static const char *
10844 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10845 {
10846 const char *linkage_name;
10847
10848 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10849 if (linkage_name == NULL)
10850 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10851
10852 return linkage_name;
10853 }
10854
10855 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
10856 compute the physname for the object, which include a method's:
10857 - formal parameters (C++),
10858 - receiver type (Go),
10859
10860 The term "physname" is a bit confusing.
10861 For C++, for example, it is the demangled name.
10862 For Go, for example, it's the mangled name.
10863
10864 For Ada, return the DIE's linkage name rather than the fully qualified
10865 name. PHYSNAME is ignored..
10866
10867 The result is allocated on the objfile_obstack and canonicalized. */
10868
10869 static const char *
10870 dwarf2_compute_name (const char *name,
10871 struct die_info *die, struct dwarf2_cu *cu,
10872 int physname)
10873 {
10874 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10875
10876 if (name == NULL)
10877 name = dwarf2_name (die, cu);
10878
10879 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10880 but otherwise compute it by typename_concat inside GDB.
10881 FIXME: Actually this is not really true, or at least not always true.
10882 It's all very confusing. compute_and_set_names doesn't try to demangle
10883 Fortran names because there is no mangling standard. So new_symbol
10884 will set the demangled name to the result of dwarf2_full_name, and it is
10885 the demangled name that GDB uses if it exists. */
10886 if (cu->language == language_ada
10887 || (cu->language == language_fortran && physname))
10888 {
10889 /* For Ada unit, we prefer the linkage name over the name, as
10890 the former contains the exported name, which the user expects
10891 to be able to reference. Ideally, we want the user to be able
10892 to reference this entity using either natural or linkage name,
10893 but we haven't started looking at this enhancement yet. */
10894 const char *linkage_name = dw2_linkage_name (die, cu);
10895
10896 if (linkage_name != NULL)
10897 return linkage_name;
10898 }
10899
10900 /* These are the only languages we know how to qualify names in. */
10901 if (name != NULL
10902 && (cu->language == language_cplus
10903 || cu->language == language_fortran || cu->language == language_d
10904 || cu->language == language_rust))
10905 {
10906 if (die_needs_namespace (die, cu))
10907 {
10908 const char *prefix;
10909 const char *canonical_name = NULL;
10910
10911 string_file buf;
10912
10913 prefix = determine_prefix (die, cu);
10914 if (*prefix != '\0')
10915 {
10916 char *prefixed_name = typename_concat (NULL, prefix, name,
10917 physname, cu);
10918
10919 buf.puts (prefixed_name);
10920 xfree (prefixed_name);
10921 }
10922 else
10923 buf.puts (name);
10924
10925 /* Template parameters may be specified in the DIE's DW_AT_name, or
10926 as children with DW_TAG_template_type_param or
10927 DW_TAG_value_type_param. If the latter, add them to the name
10928 here. If the name already has template parameters, then
10929 skip this step; some versions of GCC emit both, and
10930 it is more efficient to use the pre-computed name.
10931
10932 Something to keep in mind about this process: it is very
10933 unlikely, or in some cases downright impossible, to produce
10934 something that will match the mangled name of a function.
10935 If the definition of the function has the same debug info,
10936 we should be able to match up with it anyway. But fallbacks
10937 using the minimal symbol, for instance to find a method
10938 implemented in a stripped copy of libstdc++, will not work.
10939 If we do not have debug info for the definition, we will have to
10940 match them up some other way.
10941
10942 When we do name matching there is a related problem with function
10943 templates; two instantiated function templates are allowed to
10944 differ only by their return types, which we do not add here. */
10945
10946 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10947 {
10948 struct attribute *attr;
10949 struct die_info *child;
10950 int first = 1;
10951
10952 die->building_fullname = 1;
10953
10954 for (child = die->child; child != NULL; child = child->sibling)
10955 {
10956 struct type *type;
10957 LONGEST value;
10958 const gdb_byte *bytes;
10959 struct dwarf2_locexpr_baton *baton;
10960 struct value *v;
10961
10962 if (child->tag != DW_TAG_template_type_param
10963 && child->tag != DW_TAG_template_value_param)
10964 continue;
10965
10966 if (first)
10967 {
10968 buf.puts ("<");
10969 first = 0;
10970 }
10971 else
10972 buf.puts (", ");
10973
10974 attr = dwarf2_attr (child, DW_AT_type, cu);
10975 if (attr == NULL)
10976 {
10977 complaint (_("template parameter missing DW_AT_type"));
10978 buf.puts ("UNKNOWN_TYPE");
10979 continue;
10980 }
10981 type = die_type (child, cu);
10982
10983 if (child->tag == DW_TAG_template_type_param)
10984 {
10985 c_print_type (type, "", &buf, -1, 0, cu->language,
10986 &type_print_raw_options);
10987 continue;
10988 }
10989
10990 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10991 if (attr == NULL)
10992 {
10993 complaint (_("template parameter missing "
10994 "DW_AT_const_value"));
10995 buf.puts ("UNKNOWN_VALUE");
10996 continue;
10997 }
10998
10999 dwarf2_const_value_attr (attr, type, name,
11000 &cu->comp_unit_obstack, cu,
11001 &value, &bytes, &baton);
11002
11003 if (TYPE_NOSIGN (type))
11004 /* GDB prints characters as NUMBER 'CHAR'. If that's
11005 changed, this can use value_print instead. */
11006 c_printchar (value, type, &buf);
11007 else
11008 {
11009 struct value_print_options opts;
11010
11011 if (baton != NULL)
11012 v = dwarf2_evaluate_loc_desc (type, NULL,
11013 baton->data,
11014 baton->size,
11015 baton->per_cu);
11016 else if (bytes != NULL)
11017 {
11018 v = allocate_value (type);
11019 memcpy (value_contents_writeable (v), bytes,
11020 TYPE_LENGTH (type));
11021 }
11022 else
11023 v = value_from_longest (type, value);
11024
11025 /* Specify decimal so that we do not depend on
11026 the radix. */
11027 get_formatted_print_options (&opts, 'd');
11028 opts.raw = 1;
11029 value_print (v, &buf, &opts);
11030 release_value (v);
11031 }
11032 }
11033
11034 die->building_fullname = 0;
11035
11036 if (!first)
11037 {
11038 /* Close the argument list, with a space if necessary
11039 (nested templates). */
11040 if (!buf.empty () && buf.string ().back () == '>')
11041 buf.puts (" >");
11042 else
11043 buf.puts (">");
11044 }
11045 }
11046
11047 /* For C++ methods, append formal parameter type
11048 information, if PHYSNAME. */
11049
11050 if (physname && die->tag == DW_TAG_subprogram
11051 && cu->language == language_cplus)
11052 {
11053 struct type *type = read_type_die (die, cu);
11054
11055 c_type_print_args (type, &buf, 1, cu->language,
11056 &type_print_raw_options);
11057
11058 if (cu->language == language_cplus)
11059 {
11060 /* Assume that an artificial first parameter is
11061 "this", but do not crash if it is not. RealView
11062 marks unnamed (and thus unused) parameters as
11063 artificial; there is no way to differentiate
11064 the two cases. */
11065 if (TYPE_NFIELDS (type) > 0
11066 && TYPE_FIELD_ARTIFICIAL (type, 0)
11067 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
11068 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
11069 0))))
11070 buf.puts (" const");
11071 }
11072 }
11073
11074 const std::string &intermediate_name = buf.string ();
11075
11076 if (cu->language == language_cplus)
11077 canonical_name
11078 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
11079 &objfile->per_bfd->storage_obstack);
11080
11081 /* If we only computed INTERMEDIATE_NAME, or if
11082 INTERMEDIATE_NAME is already canonical, then we need to
11083 copy it to the appropriate obstack. */
11084 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
11085 name = obstack_strdup (&objfile->per_bfd->storage_obstack,
11086 intermediate_name);
11087 else
11088 name = canonical_name;
11089 }
11090 }
11091
11092 return name;
11093 }
11094
11095 /* Return the fully qualified name of DIE, based on its DW_AT_name.
11096 If scope qualifiers are appropriate they will be added. The result
11097 will be allocated on the storage_obstack, or NULL if the DIE does
11098 not have a name. NAME may either be from a previous call to
11099 dwarf2_name or NULL.
11100
11101 The output string will be canonicalized (if C++). */
11102
11103 static const char *
11104 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
11105 {
11106 return dwarf2_compute_name (name, die, cu, 0);
11107 }
11108
11109 /* Construct a physname for the given DIE in CU. NAME may either be
11110 from a previous call to dwarf2_name or NULL. The result will be
11111 allocated on the objfile_objstack or NULL if the DIE does not have a
11112 name.
11113
11114 The output string will be canonicalized (if C++). */
11115
11116 static const char *
11117 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
11118 {
11119 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
11120 const char *retval, *mangled = NULL, *canon = NULL;
11121 int need_copy = 1;
11122
11123 /* In this case dwarf2_compute_name is just a shortcut not building anything
11124 on its own. */
11125 if (!die_needs_namespace (die, cu))
11126 return dwarf2_compute_name (name, die, cu, 1);
11127
11128 mangled = dw2_linkage_name (die, cu);
11129
11130 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
11131 See https://github.com/rust-lang/rust/issues/32925. */
11132 if (cu->language == language_rust && mangled != NULL
11133 && strchr (mangled, '{') != NULL)
11134 mangled = NULL;
11135
11136 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
11137 has computed. */
11138 gdb::unique_xmalloc_ptr<char> demangled;
11139 if (mangled != NULL)
11140 {
11141
11142 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
11143 {
11144 /* Do nothing (do not demangle the symbol name). */
11145 }
11146 else if (cu->language == language_go)
11147 {
11148 /* This is a lie, but we already lie to the caller new_symbol.
11149 new_symbol assumes we return the mangled name.
11150 This just undoes that lie until things are cleaned up. */
11151 }
11152 else
11153 {
11154 /* Use DMGL_RET_DROP for C++ template functions to suppress
11155 their return type. It is easier for GDB users to search
11156 for such functions as `name(params)' than `long name(params)'.
11157 In such case the minimal symbol names do not match the full
11158 symbol names but for template functions there is never a need
11159 to look up their definition from their declaration so
11160 the only disadvantage remains the minimal symbol variant
11161 `long name(params)' does not have the proper inferior type. */
11162 demangled.reset (gdb_demangle (mangled,
11163 (DMGL_PARAMS | DMGL_ANSI
11164 | DMGL_RET_DROP)));
11165 }
11166 if (demangled)
11167 canon = demangled.get ();
11168 else
11169 {
11170 canon = mangled;
11171 need_copy = 0;
11172 }
11173 }
11174
11175 if (canon == NULL || check_physname)
11176 {
11177 const char *physname = dwarf2_compute_name (name, die, cu, 1);
11178
11179 if (canon != NULL && strcmp (physname, canon) != 0)
11180 {
11181 /* It may not mean a bug in GDB. The compiler could also
11182 compute DW_AT_linkage_name incorrectly. But in such case
11183 GDB would need to be bug-to-bug compatible. */
11184
11185 complaint (_("Computed physname <%s> does not match demangled <%s> "
11186 "(from linkage <%s>) - DIE at %s [in module %s]"),
11187 physname, canon, mangled, sect_offset_str (die->sect_off),
11188 objfile_name (objfile));
11189
11190 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
11191 is available here - over computed PHYSNAME. It is safer
11192 against both buggy GDB and buggy compilers. */
11193
11194 retval = canon;
11195 }
11196 else
11197 {
11198 retval = physname;
11199 need_copy = 0;
11200 }
11201 }
11202 else
11203 retval = canon;
11204
11205 if (need_copy)
11206 retval = obstack_strdup (&objfile->per_bfd->storage_obstack, retval);
11207
11208 return retval;
11209 }
11210
11211 /* Inspect DIE in CU for a namespace alias. If one exists, record
11212 a new symbol for it.
11213
11214 Returns 1 if a namespace alias was recorded, 0 otherwise. */
11215
11216 static int
11217 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
11218 {
11219 struct attribute *attr;
11220
11221 /* If the die does not have a name, this is not a namespace
11222 alias. */
11223 attr = dwarf2_attr (die, DW_AT_name, cu);
11224 if (attr != NULL)
11225 {
11226 int num;
11227 struct die_info *d = die;
11228 struct dwarf2_cu *imported_cu = cu;
11229
11230 /* If the compiler has nested DW_AT_imported_declaration DIEs,
11231 keep inspecting DIEs until we hit the underlying import. */
11232 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
11233 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
11234 {
11235 attr = dwarf2_attr (d, DW_AT_import, cu);
11236 if (attr == NULL)
11237 break;
11238
11239 d = follow_die_ref (d, attr, &imported_cu);
11240 if (d->tag != DW_TAG_imported_declaration)
11241 break;
11242 }
11243
11244 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
11245 {
11246 complaint (_("DIE at %s has too many recursively imported "
11247 "declarations"), sect_offset_str (d->sect_off));
11248 return 0;
11249 }
11250
11251 if (attr != NULL)
11252 {
11253 struct type *type;
11254 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
11255
11256 type = get_die_type_at_offset (sect_off, cu->per_cu);
11257 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
11258 {
11259 /* This declaration is a global namespace alias. Add
11260 a symbol for it whose type is the aliased namespace. */
11261 new_symbol (die, type, cu);
11262 return 1;
11263 }
11264 }
11265 }
11266
11267 return 0;
11268 }
11269
11270 /* Return the using directives repository (global or local?) to use in the
11271 current context for CU.
11272
11273 For Ada, imported declarations can materialize renamings, which *may* be
11274 global. However it is impossible (for now?) in DWARF to distinguish
11275 "external" imported declarations and "static" ones. As all imported
11276 declarations seem to be static in all other languages, make them all CU-wide
11277 global only in Ada. */
11278
11279 static struct using_direct **
11280 using_directives (struct dwarf2_cu *cu)
11281 {
11282 if (cu->language == language_ada
11283 && cu->get_builder ()->outermost_context_p ())
11284 return cu->get_builder ()->get_global_using_directives ();
11285 else
11286 return cu->get_builder ()->get_local_using_directives ();
11287 }
11288
11289 /* Read the import statement specified by the given die and record it. */
11290
11291 static void
11292 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
11293 {
11294 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
11295 struct attribute *import_attr;
11296 struct die_info *imported_die, *child_die;
11297 struct dwarf2_cu *imported_cu;
11298 const char *imported_name;
11299 const char *imported_name_prefix;
11300 const char *canonical_name;
11301 const char *import_alias;
11302 const char *imported_declaration = NULL;
11303 const char *import_prefix;
11304 std::vector<const char *> excludes;
11305
11306 import_attr = dwarf2_attr (die, DW_AT_import, cu);
11307 if (import_attr == NULL)
11308 {
11309 complaint (_("Tag '%s' has no DW_AT_import"),
11310 dwarf_tag_name (die->tag));
11311 return;
11312 }
11313
11314 imported_cu = cu;
11315 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
11316 imported_name = dwarf2_name (imported_die, imported_cu);
11317 if (imported_name == NULL)
11318 {
11319 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
11320
11321 The import in the following code:
11322 namespace A
11323 {
11324 typedef int B;
11325 }
11326
11327 int main ()
11328 {
11329 using A::B;
11330 B b;
11331 return b;
11332 }
11333
11334 ...
11335 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
11336 <52> DW_AT_decl_file : 1
11337 <53> DW_AT_decl_line : 6
11338 <54> DW_AT_import : <0x75>
11339 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
11340 <59> DW_AT_name : B
11341 <5b> DW_AT_decl_file : 1
11342 <5c> DW_AT_decl_line : 2
11343 <5d> DW_AT_type : <0x6e>
11344 ...
11345 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
11346 <76> DW_AT_byte_size : 4
11347 <77> DW_AT_encoding : 5 (signed)
11348
11349 imports the wrong die ( 0x75 instead of 0x58 ).
11350 This case will be ignored until the gcc bug is fixed. */
11351 return;
11352 }
11353
11354 /* Figure out the local name after import. */
11355 import_alias = dwarf2_name (die, cu);
11356
11357 /* Figure out where the statement is being imported to. */
11358 import_prefix = determine_prefix (die, cu);
11359
11360 /* Figure out what the scope of the imported die is and prepend it
11361 to the name of the imported die. */
11362 imported_name_prefix = determine_prefix (imported_die, imported_cu);
11363
11364 if (imported_die->tag != DW_TAG_namespace
11365 && imported_die->tag != DW_TAG_module)
11366 {
11367 imported_declaration = imported_name;
11368 canonical_name = imported_name_prefix;
11369 }
11370 else if (strlen (imported_name_prefix) > 0)
11371 canonical_name = obconcat (&objfile->objfile_obstack,
11372 imported_name_prefix,
11373 (cu->language == language_d ? "." : "::"),
11374 imported_name, (char *) NULL);
11375 else
11376 canonical_name = imported_name;
11377
11378 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
11379 for (child_die = die->child; child_die && child_die->tag;
11380 child_die = sibling_die (child_die))
11381 {
11382 /* DWARF-4: A Fortran use statement with a “rename list” may be
11383 represented by an imported module entry with an import attribute
11384 referring to the module and owned entries corresponding to those
11385 entities that are renamed as part of being imported. */
11386
11387 if (child_die->tag != DW_TAG_imported_declaration)
11388 {
11389 complaint (_("child DW_TAG_imported_declaration expected "
11390 "- DIE at %s [in module %s]"),
11391 sect_offset_str (child_die->sect_off),
11392 objfile_name (objfile));
11393 continue;
11394 }
11395
11396 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
11397 if (import_attr == NULL)
11398 {
11399 complaint (_("Tag '%s' has no DW_AT_import"),
11400 dwarf_tag_name (child_die->tag));
11401 continue;
11402 }
11403
11404 imported_cu = cu;
11405 imported_die = follow_die_ref_or_sig (child_die, import_attr,
11406 &imported_cu);
11407 imported_name = dwarf2_name (imported_die, imported_cu);
11408 if (imported_name == NULL)
11409 {
11410 complaint (_("child DW_TAG_imported_declaration has unknown "
11411 "imported name - DIE at %s [in module %s]"),
11412 sect_offset_str (child_die->sect_off),
11413 objfile_name (objfile));
11414 continue;
11415 }
11416
11417 excludes.push_back (imported_name);
11418
11419 process_die (child_die, cu);
11420 }
11421
11422 add_using_directive (using_directives (cu),
11423 import_prefix,
11424 canonical_name,
11425 import_alias,
11426 imported_declaration,
11427 excludes,
11428 0,
11429 &objfile->objfile_obstack);
11430 }
11431
11432 /* ICC<14 does not output the required DW_AT_declaration on incomplete
11433 types, but gives them a size of zero. Starting with version 14,
11434 ICC is compatible with GCC. */
11435
11436 static bool
11437 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
11438 {
11439 if (!cu->checked_producer)
11440 check_producer (cu);
11441
11442 return cu->producer_is_icc_lt_14;
11443 }
11444
11445 /* ICC generates a DW_AT_type for C void functions. This was observed on
11446 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
11447 which says that void functions should not have a DW_AT_type. */
11448
11449 static bool
11450 producer_is_icc (struct dwarf2_cu *cu)
11451 {
11452 if (!cu->checked_producer)
11453 check_producer (cu);
11454
11455 return cu->producer_is_icc;
11456 }
11457
11458 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11459 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11460 this, it was first present in GCC release 4.3.0. */
11461
11462 static bool
11463 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11464 {
11465 if (!cu->checked_producer)
11466 check_producer (cu);
11467
11468 return cu->producer_is_gcc_lt_4_3;
11469 }
11470
11471 static file_and_directory
11472 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
11473 {
11474 file_and_directory res;
11475
11476 /* Find the filename. Do not use dwarf2_name here, since the filename
11477 is not a source language identifier. */
11478 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11479 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
11480
11481 if (res.comp_dir == NULL
11482 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11483 && IS_ABSOLUTE_PATH (res.name))
11484 {
11485 res.comp_dir_storage = ldirname (res.name);
11486 if (!res.comp_dir_storage.empty ())
11487 res.comp_dir = res.comp_dir_storage.c_str ();
11488 }
11489 if (res.comp_dir != NULL)
11490 {
11491 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11492 directory, get rid of it. */
11493 const char *cp = strchr (res.comp_dir, ':');
11494
11495 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11496 res.comp_dir = cp + 1;
11497 }
11498
11499 if (res.name == NULL)
11500 res.name = "<unknown>";
11501
11502 return res;
11503 }
11504
11505 /* Handle DW_AT_stmt_list for a compilation unit.
11506 DIE is the DW_TAG_compile_unit die for CU.
11507 COMP_DIR is the compilation directory. LOWPC is passed to
11508 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
11509
11510 static void
11511 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
11512 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
11513 {
11514 struct dwarf2_per_objfile *dwarf2_per_objfile
11515 = cu->per_cu->dwarf2_per_objfile;
11516 struct objfile *objfile = dwarf2_per_objfile->objfile;
11517 struct attribute *attr;
11518 struct line_header line_header_local;
11519 hashval_t line_header_local_hash;
11520 void **slot;
11521 int decode_mapping;
11522
11523 gdb_assert (! cu->per_cu->is_debug_types);
11524
11525 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
11526 if (attr == NULL)
11527 return;
11528
11529 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11530
11531 /* The line header hash table is only created if needed (it exists to
11532 prevent redundant reading of the line table for partial_units).
11533 If we're given a partial_unit, we'll need it. If we're given a
11534 compile_unit, then use the line header hash table if it's already
11535 created, but don't create one just yet. */
11536
11537 if (dwarf2_per_objfile->line_header_hash == NULL
11538 && die->tag == DW_TAG_partial_unit)
11539 {
11540 dwarf2_per_objfile->line_header_hash
11541 = htab_create_alloc_ex (127, line_header_hash_voidp,
11542 line_header_eq_voidp,
11543 free_line_header_voidp,
11544 &objfile->objfile_obstack,
11545 hashtab_obstack_allocate,
11546 dummy_obstack_deallocate);
11547 }
11548
11549 line_header_local.sect_off = line_offset;
11550 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11551 line_header_local_hash = line_header_hash (&line_header_local);
11552 if (dwarf2_per_objfile->line_header_hash != NULL)
11553 {
11554 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11555 &line_header_local,
11556 line_header_local_hash, NO_INSERT);
11557
11558 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11559 is not present in *SLOT (since if there is something in *SLOT then
11560 it will be for a partial_unit). */
11561 if (die->tag == DW_TAG_partial_unit && slot != NULL)
11562 {
11563 gdb_assert (*slot != NULL);
11564 cu->line_header = (struct line_header *) *slot;
11565 return;
11566 }
11567 }
11568
11569 /* dwarf_decode_line_header does not yet provide sufficient information.
11570 We always have to call also dwarf_decode_lines for it. */
11571 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11572 if (lh == NULL)
11573 return;
11574
11575 cu->line_header = lh.release ();
11576 cu->line_header_die_owner = die;
11577
11578 if (dwarf2_per_objfile->line_header_hash == NULL)
11579 slot = NULL;
11580 else
11581 {
11582 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11583 &line_header_local,
11584 line_header_local_hash, INSERT);
11585 gdb_assert (slot != NULL);
11586 }
11587 if (slot != NULL && *slot == NULL)
11588 {
11589 /* This newly decoded line number information unit will be owned
11590 by line_header_hash hash table. */
11591 *slot = cu->line_header;
11592 cu->line_header_die_owner = NULL;
11593 }
11594 else
11595 {
11596 /* We cannot free any current entry in (*slot) as that struct line_header
11597 may be already used by multiple CUs. Create only temporary decoded
11598 line_header for this CU - it may happen at most once for each line
11599 number information unit. And if we're not using line_header_hash
11600 then this is what we want as well. */
11601 gdb_assert (die->tag != DW_TAG_partial_unit);
11602 }
11603 decode_mapping = (die->tag != DW_TAG_partial_unit);
11604 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11605 decode_mapping);
11606
11607 }
11608
11609 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
11610
11611 static void
11612 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
11613 {
11614 struct dwarf2_per_objfile *dwarf2_per_objfile
11615 = cu->per_cu->dwarf2_per_objfile;
11616 struct objfile *objfile = dwarf2_per_objfile->objfile;
11617 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11618 CORE_ADDR lowpc = ((CORE_ADDR) -1);
11619 CORE_ADDR highpc = ((CORE_ADDR) 0);
11620 struct attribute *attr;
11621 struct die_info *child_die;
11622 CORE_ADDR baseaddr;
11623
11624 prepare_one_comp_unit (cu, die, cu->language);
11625 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11626
11627 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
11628
11629 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11630 from finish_block. */
11631 if (lowpc == ((CORE_ADDR) -1))
11632 lowpc = highpc;
11633 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11634
11635 file_and_directory fnd = find_file_and_directory (die, cu);
11636
11637 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11638 standardised yet. As a workaround for the language detection we fall
11639 back to the DW_AT_producer string. */
11640 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11641 cu->language = language_opencl;
11642
11643 /* Similar hack for Go. */
11644 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11645 set_cu_language (DW_LANG_Go, cu);
11646
11647 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
11648
11649 /* Decode line number information if present. We do this before
11650 processing child DIEs, so that the line header table is available
11651 for DW_AT_decl_file. */
11652 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
11653
11654 /* Process all dies in compilation unit. */
11655 if (die->child != NULL)
11656 {
11657 child_die = die->child;
11658 while (child_die && child_die->tag)
11659 {
11660 process_die (child_die, cu);
11661 child_die = sibling_die (child_die);
11662 }
11663 }
11664
11665 /* Decode macro information, if present. Dwarf 2 macro information
11666 refers to information in the line number info statement program
11667 header, so we can only read it if we've read the header
11668 successfully. */
11669 attr = dwarf2_attr (die, DW_AT_macros, cu);
11670 if (attr == NULL)
11671 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
11672 if (attr && cu->line_header)
11673 {
11674 if (dwarf2_attr (die, DW_AT_macro_info, cu))
11675 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
11676
11677 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
11678 }
11679 else
11680 {
11681 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11682 if (attr && cu->line_header)
11683 {
11684 unsigned int macro_offset = DW_UNSND (attr);
11685
11686 dwarf_decode_macros (cu, macro_offset, 0);
11687 }
11688 }
11689 }
11690
11691 void
11692 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
11693 {
11694 struct type_unit_group *tu_group;
11695 int first_time;
11696 struct attribute *attr;
11697 unsigned int i;
11698 struct signatured_type *sig_type;
11699
11700 gdb_assert (per_cu->is_debug_types);
11701 sig_type = (struct signatured_type *) per_cu;
11702
11703 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
11704
11705 /* If we're using .gdb_index (includes -readnow) then
11706 per_cu->type_unit_group may not have been set up yet. */
11707 if (sig_type->type_unit_group == NULL)
11708 sig_type->type_unit_group = get_type_unit_group (this, attr);
11709 tu_group = sig_type->type_unit_group;
11710
11711 /* If we've already processed this stmt_list there's no real need to
11712 do it again, we could fake it and just recreate the part we need
11713 (file name,index -> symtab mapping). If data shows this optimization
11714 is useful we can do it then. */
11715 first_time = tu_group->compunit_symtab == NULL;
11716
11717 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11718 debug info. */
11719 line_header_up lh;
11720 if (attr != NULL)
11721 {
11722 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11723 lh = dwarf_decode_line_header (line_offset, this);
11724 }
11725 if (lh == NULL)
11726 {
11727 if (first_time)
11728 start_symtab ("", NULL, 0);
11729 else
11730 {
11731 gdb_assert (tu_group->symtabs == NULL);
11732 gdb_assert (m_builder == nullptr);
11733 struct compunit_symtab *cust = tu_group->compunit_symtab;
11734 m_builder.reset (new struct buildsym_compunit
11735 (COMPUNIT_OBJFILE (cust), "",
11736 COMPUNIT_DIRNAME (cust),
11737 compunit_language (cust),
11738 0, cust));
11739 }
11740 return;
11741 }
11742
11743 line_header = lh.release ();
11744 line_header_die_owner = die;
11745
11746 if (first_time)
11747 {
11748 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
11749
11750 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11751 still initializing it, and our caller (a few levels up)
11752 process_full_type_unit still needs to know if this is the first
11753 time. */
11754
11755 tu_group->num_symtabs = line_header->file_names_size ();
11756 tu_group->symtabs = XNEWVEC (struct symtab *,
11757 line_header->file_names_size ());
11758
11759 auto &file_names = line_header->file_names ();
11760 for (i = 0; i < file_names.size (); ++i)
11761 {
11762 file_entry &fe = file_names[i];
11763 dwarf2_start_subfile (this, fe.name,
11764 fe.include_dir (line_header));
11765 buildsym_compunit *b = get_builder ();
11766 if (b->get_current_subfile ()->symtab == NULL)
11767 {
11768 /* NOTE: start_subfile will recognize when it's been
11769 passed a file it has already seen. So we can't
11770 assume there's a simple mapping from
11771 cu->line_header->file_names to subfiles, plus
11772 cu->line_header->file_names may contain dups. */
11773 b->get_current_subfile ()->symtab
11774 = allocate_symtab (cust, b->get_current_subfile ()->name);
11775 }
11776
11777 fe.symtab = b->get_current_subfile ()->symtab;
11778 tu_group->symtabs[i] = fe.symtab;
11779 }
11780 }
11781 else
11782 {
11783 gdb_assert (m_builder == nullptr);
11784 struct compunit_symtab *cust = tu_group->compunit_symtab;
11785 m_builder.reset (new struct buildsym_compunit
11786 (COMPUNIT_OBJFILE (cust), "",
11787 COMPUNIT_DIRNAME (cust),
11788 compunit_language (cust),
11789 0, cust));
11790
11791 auto &file_names = line_header->file_names ();
11792 for (i = 0; i < file_names.size (); ++i)
11793 {
11794 file_entry &fe = file_names[i];
11795 fe.symtab = tu_group->symtabs[i];
11796 }
11797 }
11798
11799 /* The main symtab is allocated last. Type units don't have DW_AT_name
11800 so they don't have a "real" (so to speak) symtab anyway.
11801 There is later code that will assign the main symtab to all symbols
11802 that don't have one. We need to handle the case of a symbol with a
11803 missing symtab (DW_AT_decl_file) anyway. */
11804 }
11805
11806 /* Process DW_TAG_type_unit.
11807 For TUs we want to skip the first top level sibling if it's not the
11808 actual type being defined by this TU. In this case the first top
11809 level sibling is there to provide context only. */
11810
11811 static void
11812 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11813 {
11814 struct die_info *child_die;
11815
11816 prepare_one_comp_unit (cu, die, language_minimal);
11817
11818 /* Initialize (or reinitialize) the machinery for building symtabs.
11819 We do this before processing child DIEs, so that the line header table
11820 is available for DW_AT_decl_file. */
11821 cu->setup_type_unit_groups (die);
11822
11823 if (die->child != NULL)
11824 {
11825 child_die = die->child;
11826 while (child_die && child_die->tag)
11827 {
11828 process_die (child_die, cu);
11829 child_die = sibling_die (child_die);
11830 }
11831 }
11832 }
11833 \f
11834 /* DWO/DWP files.
11835
11836 http://gcc.gnu.org/wiki/DebugFission
11837 http://gcc.gnu.org/wiki/DebugFissionDWP
11838
11839 To simplify handling of both DWO files ("object" files with the DWARF info)
11840 and DWP files (a file with the DWOs packaged up into one file), we treat
11841 DWP files as having a collection of virtual DWO files. */
11842
11843 static hashval_t
11844 hash_dwo_file (const void *item)
11845 {
11846 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
11847 hashval_t hash;
11848
11849 hash = htab_hash_string (dwo_file->dwo_name);
11850 if (dwo_file->comp_dir != NULL)
11851 hash += htab_hash_string (dwo_file->comp_dir);
11852 return hash;
11853 }
11854
11855 static int
11856 eq_dwo_file (const void *item_lhs, const void *item_rhs)
11857 {
11858 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11859 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
11860
11861 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11862 return 0;
11863 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11864 return lhs->comp_dir == rhs->comp_dir;
11865 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
11866 }
11867
11868 /* Allocate a hash table for DWO files. */
11869
11870 static htab_up
11871 allocate_dwo_file_hash_table (struct objfile *objfile)
11872 {
11873 auto delete_dwo_file = [] (void *item)
11874 {
11875 struct dwo_file *dwo_file = (struct dwo_file *) item;
11876
11877 delete dwo_file;
11878 };
11879
11880 return htab_up (htab_create_alloc_ex (41,
11881 hash_dwo_file,
11882 eq_dwo_file,
11883 delete_dwo_file,
11884 &objfile->objfile_obstack,
11885 hashtab_obstack_allocate,
11886 dummy_obstack_deallocate));
11887 }
11888
11889 /* Lookup DWO file DWO_NAME. */
11890
11891 static void **
11892 lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11893 const char *dwo_name,
11894 const char *comp_dir)
11895 {
11896 struct dwo_file find_entry;
11897 void **slot;
11898
11899 if (dwarf2_per_objfile->dwo_files == NULL)
11900 dwarf2_per_objfile->dwo_files
11901 = allocate_dwo_file_hash_table (dwarf2_per_objfile->objfile);
11902
11903 find_entry.dwo_name = dwo_name;
11904 find_entry.comp_dir = comp_dir;
11905 slot = htab_find_slot (dwarf2_per_objfile->dwo_files.get (), &find_entry,
11906 INSERT);
11907
11908 return slot;
11909 }
11910
11911 static hashval_t
11912 hash_dwo_unit (const void *item)
11913 {
11914 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11915
11916 /* This drops the top 32 bits of the id, but is ok for a hash. */
11917 return dwo_unit->signature;
11918 }
11919
11920 static int
11921 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11922 {
11923 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11924 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11925
11926 /* The signature is assumed to be unique within the DWO file.
11927 So while object file CU dwo_id's always have the value zero,
11928 that's OK, assuming each object file DWO file has only one CU,
11929 and that's the rule for now. */
11930 return lhs->signature == rhs->signature;
11931 }
11932
11933 /* Allocate a hash table for DWO CUs,TUs.
11934 There is one of these tables for each of CUs,TUs for each DWO file. */
11935
11936 static htab_t
11937 allocate_dwo_unit_table (struct objfile *objfile)
11938 {
11939 /* Start out with a pretty small number.
11940 Generally DWO files contain only one CU and maybe some TUs. */
11941 return htab_create_alloc_ex (3,
11942 hash_dwo_unit,
11943 eq_dwo_unit,
11944 NULL,
11945 &objfile->objfile_obstack,
11946 hashtab_obstack_allocate,
11947 dummy_obstack_deallocate);
11948 }
11949
11950 /* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
11951
11952 struct create_dwo_cu_data
11953 {
11954 struct dwo_file *dwo_file;
11955 struct dwo_unit dwo_unit;
11956 };
11957
11958 /* die_reader_func for create_dwo_cu. */
11959
11960 static void
11961 create_dwo_cu_reader (const struct die_reader_specs *reader,
11962 const gdb_byte *info_ptr,
11963 struct die_info *comp_unit_die,
11964 int has_children,
11965 void *datap)
11966 {
11967 struct dwarf2_cu *cu = reader->cu;
11968 sect_offset sect_off = cu->per_cu->sect_off;
11969 struct dwarf2_section_info *section = cu->per_cu->section;
11970 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
11971 struct dwo_file *dwo_file = data->dwo_file;
11972 struct dwo_unit *dwo_unit = &data->dwo_unit;
11973
11974 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11975 if (!signature.has_value ())
11976 {
11977 complaint (_("Dwarf Error: debug entry at offset %s is missing"
11978 " its dwo_id [in module %s]"),
11979 sect_offset_str (sect_off), dwo_file->dwo_name);
11980 return;
11981 }
11982
11983 dwo_unit->dwo_file = dwo_file;
11984 dwo_unit->signature = *signature;
11985 dwo_unit->section = section;
11986 dwo_unit->sect_off = sect_off;
11987 dwo_unit->length = cu->per_cu->length;
11988
11989 if (dwarf_read_debug)
11990 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11991 sect_offset_str (sect_off),
11992 hex_string (dwo_unit->signature));
11993 }
11994
11995 /* Create the dwo_units for the CUs in a DWO_FILE.
11996 Note: This function processes DWO files only, not DWP files. */
11997
11998 static void
11999 create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
12000 struct dwo_file &dwo_file, dwarf2_section_info &section,
12001 htab_t &cus_htab)
12002 {
12003 struct objfile *objfile = dwarf2_per_objfile->objfile;
12004 const gdb_byte *info_ptr, *end_ptr;
12005
12006 dwarf2_read_section (objfile, &section);
12007 info_ptr = section.buffer;
12008
12009 if (info_ptr == NULL)
12010 return;
12011
12012 if (dwarf_read_debug)
12013 {
12014 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
12015 get_section_name (&section),
12016 get_section_file_name (&section));
12017 }
12018
12019 end_ptr = info_ptr + section.size;
12020 while (info_ptr < end_ptr)
12021 {
12022 struct dwarf2_per_cu_data per_cu;
12023 struct create_dwo_cu_data create_dwo_cu_data;
12024 struct dwo_unit *dwo_unit;
12025 void **slot;
12026 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
12027
12028 memset (&create_dwo_cu_data.dwo_unit, 0,
12029 sizeof (create_dwo_cu_data.dwo_unit));
12030 memset (&per_cu, 0, sizeof (per_cu));
12031 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
12032 per_cu.is_debug_types = 0;
12033 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
12034 per_cu.section = &section;
12035 create_dwo_cu_data.dwo_file = &dwo_file;
12036
12037 init_cutu_and_read_dies_no_follow (
12038 &per_cu, &dwo_file, create_dwo_cu_reader, &create_dwo_cu_data);
12039 info_ptr += per_cu.length;
12040
12041 // If the unit could not be parsed, skip it.
12042 if (create_dwo_cu_data.dwo_unit.dwo_file == NULL)
12043 continue;
12044
12045 if (cus_htab == NULL)
12046 cus_htab = allocate_dwo_unit_table (objfile);
12047
12048 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12049 *dwo_unit = create_dwo_cu_data.dwo_unit;
12050 slot = htab_find_slot (cus_htab, dwo_unit, INSERT);
12051 gdb_assert (slot != NULL);
12052 if (*slot != NULL)
12053 {
12054 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
12055 sect_offset dup_sect_off = dup_cu->sect_off;
12056
12057 complaint (_("debug cu entry at offset %s is duplicate to"
12058 " the entry at offset %s, signature %s"),
12059 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
12060 hex_string (dwo_unit->signature));
12061 }
12062 *slot = (void *)dwo_unit;
12063 }
12064 }
12065
12066 /* DWP file .debug_{cu,tu}_index section format:
12067 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
12068
12069 DWP Version 1:
12070
12071 Both index sections have the same format, and serve to map a 64-bit
12072 signature to a set of section numbers. Each section begins with a header,
12073 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
12074 indexes, and a pool of 32-bit section numbers. The index sections will be
12075 aligned at 8-byte boundaries in the file.
12076
12077 The index section header consists of:
12078
12079 V, 32 bit version number
12080 -, 32 bits unused
12081 N, 32 bit number of compilation units or type units in the index
12082 M, 32 bit number of slots in the hash table
12083
12084 Numbers are recorded using the byte order of the application binary.
12085
12086 The hash table begins at offset 16 in the section, and consists of an array
12087 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
12088 order of the application binary). Unused slots in the hash table are 0.
12089 (We rely on the extreme unlikeliness of a signature being exactly 0.)
12090
12091 The parallel table begins immediately after the hash table
12092 (at offset 16 + 8 * M from the beginning of the section), and consists of an
12093 array of 32-bit indexes (using the byte order of the application binary),
12094 corresponding 1-1 with slots in the hash table. Each entry in the parallel
12095 table contains a 32-bit index into the pool of section numbers. For unused
12096 hash table slots, the corresponding entry in the parallel table will be 0.
12097
12098 The pool of section numbers begins immediately following the hash table
12099 (at offset 16 + 12 * M from the beginning of the section). The pool of
12100 section numbers consists of an array of 32-bit words (using the byte order
12101 of the application binary). Each item in the array is indexed starting
12102 from 0. The hash table entry provides the index of the first section
12103 number in the set. Additional section numbers in the set follow, and the
12104 set is terminated by a 0 entry (section number 0 is not used in ELF).
12105
12106 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
12107 section must be the first entry in the set, and the .debug_abbrev.dwo must
12108 be the second entry. Other members of the set may follow in any order.
12109
12110 ---
12111
12112 DWP Version 2:
12113
12114 DWP Version 2 combines all the .debug_info, etc. sections into one,
12115 and the entries in the index tables are now offsets into these sections.
12116 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
12117 section.
12118
12119 Index Section Contents:
12120 Header
12121 Hash Table of Signatures dwp_hash_table.hash_table
12122 Parallel Table of Indices dwp_hash_table.unit_table
12123 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
12124 Table of Section Sizes dwp_hash_table.v2.sizes
12125
12126 The index section header consists of:
12127
12128 V, 32 bit version number
12129 L, 32 bit number of columns in the table of section offsets
12130 N, 32 bit number of compilation units or type units in the index
12131 M, 32 bit number of slots in the hash table
12132
12133 Numbers are recorded using the byte order of the application binary.
12134
12135 The hash table has the same format as version 1.
12136 The parallel table of indices has the same format as version 1,
12137 except that the entries are origin-1 indices into the table of sections
12138 offsets and the table of section sizes.
12139
12140 The table of offsets begins immediately following the parallel table
12141 (at offset 16 + 12 * M from the beginning of the section). The table is
12142 a two-dimensional array of 32-bit words (using the byte order of the
12143 application binary), with L columns and N+1 rows, in row-major order.
12144 Each row in the array is indexed starting from 0. The first row provides
12145 a key to the remaining rows: each column in this row provides an identifier
12146 for a debug section, and the offsets in the same column of subsequent rows
12147 refer to that section. The section identifiers are:
12148
12149 DW_SECT_INFO 1 .debug_info.dwo
12150 DW_SECT_TYPES 2 .debug_types.dwo
12151 DW_SECT_ABBREV 3 .debug_abbrev.dwo
12152 DW_SECT_LINE 4 .debug_line.dwo
12153 DW_SECT_LOC 5 .debug_loc.dwo
12154 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
12155 DW_SECT_MACINFO 7 .debug_macinfo.dwo
12156 DW_SECT_MACRO 8 .debug_macro.dwo
12157
12158 The offsets provided by the CU and TU index sections are the base offsets
12159 for the contributions made by each CU or TU to the corresponding section
12160 in the package file. Each CU and TU header contains an abbrev_offset
12161 field, used to find the abbreviations table for that CU or TU within the
12162 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
12163 be interpreted as relative to the base offset given in the index section.
12164 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
12165 should be interpreted as relative to the base offset for .debug_line.dwo,
12166 and offsets into other debug sections obtained from DWARF attributes should
12167 also be interpreted as relative to the corresponding base offset.
12168
12169 The table of sizes begins immediately following the table of offsets.
12170 Like the table of offsets, it is a two-dimensional array of 32-bit words,
12171 with L columns and N rows, in row-major order. Each row in the array is
12172 indexed starting from 1 (row 0 is shared by the two tables).
12173
12174 ---
12175
12176 Hash table lookup is handled the same in version 1 and 2:
12177
12178 We assume that N and M will not exceed 2^32 - 1.
12179 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
12180
12181 Given a 64-bit compilation unit signature or a type signature S, an entry
12182 in the hash table is located as follows:
12183
12184 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
12185 the low-order k bits all set to 1.
12186
12187 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
12188
12189 3) If the hash table entry at index H matches the signature, use that
12190 entry. If the hash table entry at index H is unused (all zeroes),
12191 terminate the search: the signature is not present in the table.
12192
12193 4) Let H = (H + H') modulo M. Repeat at Step 3.
12194
12195 Because M > N and H' and M are relatively prime, the search is guaranteed
12196 to stop at an unused slot or find the match. */
12197
12198 /* Create a hash table to map DWO IDs to their CU/TU entry in
12199 .debug_{info,types}.dwo in DWP_FILE.
12200 Returns NULL if there isn't one.
12201 Note: This function processes DWP files only, not DWO files. */
12202
12203 static struct dwp_hash_table *
12204 create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
12205 struct dwp_file *dwp_file, int is_debug_types)
12206 {
12207 struct objfile *objfile = dwarf2_per_objfile->objfile;
12208 bfd *dbfd = dwp_file->dbfd.get ();
12209 const gdb_byte *index_ptr, *index_end;
12210 struct dwarf2_section_info *index;
12211 uint32_t version, nr_columns, nr_units, nr_slots;
12212 struct dwp_hash_table *htab;
12213
12214 if (is_debug_types)
12215 index = &dwp_file->sections.tu_index;
12216 else
12217 index = &dwp_file->sections.cu_index;
12218
12219 if (dwarf2_section_empty_p (index))
12220 return NULL;
12221 dwarf2_read_section (objfile, index);
12222
12223 index_ptr = index->buffer;
12224 index_end = index_ptr + index->size;
12225
12226 version = read_4_bytes (dbfd, index_ptr);
12227 index_ptr += 4;
12228 if (version == 2)
12229 nr_columns = read_4_bytes (dbfd, index_ptr);
12230 else
12231 nr_columns = 0;
12232 index_ptr += 4;
12233 nr_units = read_4_bytes (dbfd, index_ptr);
12234 index_ptr += 4;
12235 nr_slots = read_4_bytes (dbfd, index_ptr);
12236 index_ptr += 4;
12237
12238 if (version != 1 && version != 2)
12239 {
12240 error (_("Dwarf Error: unsupported DWP file version (%s)"
12241 " [in module %s]"),
12242 pulongest (version), dwp_file->name);
12243 }
12244 if (nr_slots != (nr_slots & -nr_slots))
12245 {
12246 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
12247 " is not power of 2 [in module %s]"),
12248 pulongest (nr_slots), dwp_file->name);
12249 }
12250
12251 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
12252 htab->version = version;
12253 htab->nr_columns = nr_columns;
12254 htab->nr_units = nr_units;
12255 htab->nr_slots = nr_slots;
12256 htab->hash_table = index_ptr;
12257 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
12258
12259 /* Exit early if the table is empty. */
12260 if (nr_slots == 0 || nr_units == 0
12261 || (version == 2 && nr_columns == 0))
12262 {
12263 /* All must be zero. */
12264 if (nr_slots != 0 || nr_units != 0
12265 || (version == 2 && nr_columns != 0))
12266 {
12267 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
12268 " all zero [in modules %s]"),
12269 dwp_file->name);
12270 }
12271 return htab;
12272 }
12273
12274 if (version == 1)
12275 {
12276 htab->section_pool.v1.indices =
12277 htab->unit_table + sizeof (uint32_t) * nr_slots;
12278 /* It's harder to decide whether the section is too small in v1.
12279 V1 is deprecated anyway so we punt. */
12280 }
12281 else
12282 {
12283 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
12284 int *ids = htab->section_pool.v2.section_ids;
12285 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
12286 /* Reverse map for error checking. */
12287 int ids_seen[DW_SECT_MAX + 1];
12288 int i;
12289
12290 if (nr_columns < 2)
12291 {
12292 error (_("Dwarf Error: bad DWP hash table, too few columns"
12293 " in section table [in module %s]"),
12294 dwp_file->name);
12295 }
12296 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
12297 {
12298 error (_("Dwarf Error: bad DWP hash table, too many columns"
12299 " in section table [in module %s]"),
12300 dwp_file->name);
12301 }
12302 memset (ids, 255, sizeof_ids);
12303 memset (ids_seen, 255, sizeof (ids_seen));
12304 for (i = 0; i < nr_columns; ++i)
12305 {
12306 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
12307
12308 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
12309 {
12310 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
12311 " in section table [in module %s]"),
12312 id, dwp_file->name);
12313 }
12314 if (ids_seen[id] != -1)
12315 {
12316 error (_("Dwarf Error: bad DWP hash table, duplicate section"
12317 " id %d in section table [in module %s]"),
12318 id, dwp_file->name);
12319 }
12320 ids_seen[id] = i;
12321 ids[i] = id;
12322 }
12323 /* Must have exactly one info or types section. */
12324 if (((ids_seen[DW_SECT_INFO] != -1)
12325 + (ids_seen[DW_SECT_TYPES] != -1))
12326 != 1)
12327 {
12328 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
12329 " DWO info/types section [in module %s]"),
12330 dwp_file->name);
12331 }
12332 /* Must have an abbrev section. */
12333 if (ids_seen[DW_SECT_ABBREV] == -1)
12334 {
12335 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
12336 " section [in module %s]"),
12337 dwp_file->name);
12338 }
12339 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
12340 htab->section_pool.v2.sizes =
12341 htab->section_pool.v2.offsets + (sizeof (uint32_t)
12342 * nr_units * nr_columns);
12343 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
12344 * nr_units * nr_columns))
12345 > index_end)
12346 {
12347 error (_("Dwarf Error: DWP index section is corrupt (too small)"
12348 " [in module %s]"),
12349 dwp_file->name);
12350 }
12351 }
12352
12353 return htab;
12354 }
12355
12356 /* Update SECTIONS with the data from SECTP.
12357
12358 This function is like the other "locate" section routines that are
12359 passed to bfd_map_over_sections, but in this context the sections to
12360 read comes from the DWP V1 hash table, not the full ELF section table.
12361
12362 The result is non-zero for success, or zero if an error was found. */
12363
12364 static int
12365 locate_v1_virtual_dwo_sections (asection *sectp,
12366 struct virtual_v1_dwo_sections *sections)
12367 {
12368 const struct dwop_section_names *names = &dwop_section_names;
12369
12370 if (section_is_p (sectp->name, &names->abbrev_dwo))
12371 {
12372 /* There can be only one. */
12373 if (sections->abbrev.s.section != NULL)
12374 return 0;
12375 sections->abbrev.s.section = sectp;
12376 sections->abbrev.size = bfd_section_size (sectp);
12377 }
12378 else if (section_is_p (sectp->name, &names->info_dwo)
12379 || section_is_p (sectp->name, &names->types_dwo))
12380 {
12381 /* There can be only one. */
12382 if (sections->info_or_types.s.section != NULL)
12383 return 0;
12384 sections->info_or_types.s.section = sectp;
12385 sections->info_or_types.size = bfd_section_size (sectp);
12386 }
12387 else if (section_is_p (sectp->name, &names->line_dwo))
12388 {
12389 /* There can be only one. */
12390 if (sections->line.s.section != NULL)
12391 return 0;
12392 sections->line.s.section = sectp;
12393 sections->line.size = bfd_section_size (sectp);
12394 }
12395 else if (section_is_p (sectp->name, &names->loc_dwo))
12396 {
12397 /* There can be only one. */
12398 if (sections->loc.s.section != NULL)
12399 return 0;
12400 sections->loc.s.section = sectp;
12401 sections->loc.size = bfd_section_size (sectp);
12402 }
12403 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12404 {
12405 /* There can be only one. */
12406 if (sections->macinfo.s.section != NULL)
12407 return 0;
12408 sections->macinfo.s.section = sectp;
12409 sections->macinfo.size = bfd_section_size (sectp);
12410 }
12411 else if (section_is_p (sectp->name, &names->macro_dwo))
12412 {
12413 /* There can be only one. */
12414 if (sections->macro.s.section != NULL)
12415 return 0;
12416 sections->macro.s.section = sectp;
12417 sections->macro.size = bfd_section_size (sectp);
12418 }
12419 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12420 {
12421 /* There can be only one. */
12422 if (sections->str_offsets.s.section != NULL)
12423 return 0;
12424 sections->str_offsets.s.section = sectp;
12425 sections->str_offsets.size = bfd_section_size (sectp);
12426 }
12427 else
12428 {
12429 /* No other kind of section is valid. */
12430 return 0;
12431 }
12432
12433 return 1;
12434 }
12435
12436 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12437 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12438 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12439 This is for DWP version 1 files. */
12440
12441 static struct dwo_unit *
12442 create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12443 struct dwp_file *dwp_file,
12444 uint32_t unit_index,
12445 const char *comp_dir,
12446 ULONGEST signature, int is_debug_types)
12447 {
12448 struct objfile *objfile = dwarf2_per_objfile->objfile;
12449 const struct dwp_hash_table *dwp_htab =
12450 is_debug_types ? dwp_file->tus : dwp_file->cus;
12451 bfd *dbfd = dwp_file->dbfd.get ();
12452 const char *kind = is_debug_types ? "TU" : "CU";
12453 struct dwo_file *dwo_file;
12454 struct dwo_unit *dwo_unit;
12455 struct virtual_v1_dwo_sections sections;
12456 void **dwo_file_slot;
12457 int i;
12458
12459 gdb_assert (dwp_file->version == 1);
12460
12461 if (dwarf_read_debug)
12462 {
12463 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
12464 kind,
12465 pulongest (unit_index), hex_string (signature),
12466 dwp_file->name);
12467 }
12468
12469 /* Fetch the sections of this DWO unit.
12470 Put a limit on the number of sections we look for so that bad data
12471 doesn't cause us to loop forever. */
12472
12473 #define MAX_NR_V1_DWO_SECTIONS \
12474 (1 /* .debug_info or .debug_types */ \
12475 + 1 /* .debug_abbrev */ \
12476 + 1 /* .debug_line */ \
12477 + 1 /* .debug_loc */ \
12478 + 1 /* .debug_str_offsets */ \
12479 + 1 /* .debug_macro or .debug_macinfo */ \
12480 + 1 /* trailing zero */)
12481
12482 memset (&sections, 0, sizeof (sections));
12483
12484 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
12485 {
12486 asection *sectp;
12487 uint32_t section_nr =
12488 read_4_bytes (dbfd,
12489 dwp_htab->section_pool.v1.indices
12490 + (unit_index + i) * sizeof (uint32_t));
12491
12492 if (section_nr == 0)
12493 break;
12494 if (section_nr >= dwp_file->num_sections)
12495 {
12496 error (_("Dwarf Error: bad DWP hash table, section number too large"
12497 " [in module %s]"),
12498 dwp_file->name);
12499 }
12500
12501 sectp = dwp_file->elf_sections[section_nr];
12502 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
12503 {
12504 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12505 " [in module %s]"),
12506 dwp_file->name);
12507 }
12508 }
12509
12510 if (i < 2
12511 || dwarf2_section_empty_p (&sections.info_or_types)
12512 || dwarf2_section_empty_p (&sections.abbrev))
12513 {
12514 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12515 " [in module %s]"),
12516 dwp_file->name);
12517 }
12518 if (i == MAX_NR_V1_DWO_SECTIONS)
12519 {
12520 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12521 " [in module %s]"),
12522 dwp_file->name);
12523 }
12524
12525 /* It's easier for the rest of the code if we fake a struct dwo_file and
12526 have dwo_unit "live" in that. At least for now.
12527
12528 The DWP file can be made up of a random collection of CUs and TUs.
12529 However, for each CU + set of TUs that came from the same original DWO
12530 file, we can combine them back into a virtual DWO file to save space
12531 (fewer struct dwo_file objects to allocate). Remember that for really
12532 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12533
12534 std::string virtual_dwo_name =
12535 string_printf ("virtual-dwo/%d-%d-%d-%d",
12536 get_section_id (&sections.abbrev),
12537 get_section_id (&sections.line),
12538 get_section_id (&sections.loc),
12539 get_section_id (&sections.str_offsets));
12540 /* Can we use an existing virtual DWO file? */
12541 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12542 virtual_dwo_name.c_str (),
12543 comp_dir);
12544 /* Create one if necessary. */
12545 if (*dwo_file_slot == NULL)
12546 {
12547 if (dwarf_read_debug)
12548 {
12549 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12550 virtual_dwo_name.c_str ());
12551 }
12552 dwo_file = new struct dwo_file;
12553 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
12554 virtual_dwo_name);
12555 dwo_file->comp_dir = comp_dir;
12556 dwo_file->sections.abbrev = sections.abbrev;
12557 dwo_file->sections.line = sections.line;
12558 dwo_file->sections.loc = sections.loc;
12559 dwo_file->sections.macinfo = sections.macinfo;
12560 dwo_file->sections.macro = sections.macro;
12561 dwo_file->sections.str_offsets = sections.str_offsets;
12562 /* The "str" section is global to the entire DWP file. */
12563 dwo_file->sections.str = dwp_file->sections.str;
12564 /* The info or types section is assigned below to dwo_unit,
12565 there's no need to record it in dwo_file.
12566 Also, we can't simply record type sections in dwo_file because
12567 we record a pointer into the vector in dwo_unit. As we collect more
12568 types we'll grow the vector and eventually have to reallocate space
12569 for it, invalidating all copies of pointers into the previous
12570 contents. */
12571 *dwo_file_slot = dwo_file;
12572 }
12573 else
12574 {
12575 if (dwarf_read_debug)
12576 {
12577 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12578 virtual_dwo_name.c_str ());
12579 }
12580 dwo_file = (struct dwo_file *) *dwo_file_slot;
12581 }
12582
12583 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12584 dwo_unit->dwo_file = dwo_file;
12585 dwo_unit->signature = signature;
12586 dwo_unit->section =
12587 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
12588 *dwo_unit->section = sections.info_or_types;
12589 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12590
12591 return dwo_unit;
12592 }
12593
12594 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
12595 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
12596 piece within that section used by a TU/CU, return a virtual section
12597 of just that piece. */
12598
12599 static struct dwarf2_section_info
12600 create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
12601 struct dwarf2_section_info *section,
12602 bfd_size_type offset, bfd_size_type size)
12603 {
12604 struct dwarf2_section_info result;
12605 asection *sectp;
12606
12607 gdb_assert (section != NULL);
12608 gdb_assert (!section->is_virtual);
12609
12610 memset (&result, 0, sizeof (result));
12611 result.s.containing_section = section;
12612 result.is_virtual = true;
12613
12614 if (size == 0)
12615 return result;
12616
12617 sectp = get_section_bfd_section (section);
12618
12619 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12620 bounds of the real section. This is a pretty-rare event, so just
12621 flag an error (easier) instead of a warning and trying to cope. */
12622 if (sectp == NULL
12623 || offset + size > bfd_section_size (sectp))
12624 {
12625 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
12626 " in section %s [in module %s]"),
12627 sectp ? bfd_section_name (sectp) : "<unknown>",
12628 objfile_name (dwarf2_per_objfile->objfile));
12629 }
12630
12631 result.virtual_offset = offset;
12632 result.size = size;
12633 return result;
12634 }
12635
12636 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12637 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12638 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12639 This is for DWP version 2 files. */
12640
12641 static struct dwo_unit *
12642 create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12643 struct dwp_file *dwp_file,
12644 uint32_t unit_index,
12645 const char *comp_dir,
12646 ULONGEST signature, int is_debug_types)
12647 {
12648 struct objfile *objfile = dwarf2_per_objfile->objfile;
12649 const struct dwp_hash_table *dwp_htab =
12650 is_debug_types ? dwp_file->tus : dwp_file->cus;
12651 bfd *dbfd = dwp_file->dbfd.get ();
12652 const char *kind = is_debug_types ? "TU" : "CU";
12653 struct dwo_file *dwo_file;
12654 struct dwo_unit *dwo_unit;
12655 struct virtual_v2_dwo_sections sections;
12656 void **dwo_file_slot;
12657 int i;
12658
12659 gdb_assert (dwp_file->version == 2);
12660
12661 if (dwarf_read_debug)
12662 {
12663 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12664 kind,
12665 pulongest (unit_index), hex_string (signature),
12666 dwp_file->name);
12667 }
12668
12669 /* Fetch the section offsets of this DWO unit. */
12670
12671 memset (&sections, 0, sizeof (sections));
12672
12673 for (i = 0; i < dwp_htab->nr_columns; ++i)
12674 {
12675 uint32_t offset = read_4_bytes (dbfd,
12676 dwp_htab->section_pool.v2.offsets
12677 + (((unit_index - 1) * dwp_htab->nr_columns
12678 + i)
12679 * sizeof (uint32_t)));
12680 uint32_t size = read_4_bytes (dbfd,
12681 dwp_htab->section_pool.v2.sizes
12682 + (((unit_index - 1) * dwp_htab->nr_columns
12683 + i)
12684 * sizeof (uint32_t)));
12685
12686 switch (dwp_htab->section_pool.v2.section_ids[i])
12687 {
12688 case DW_SECT_INFO:
12689 case DW_SECT_TYPES:
12690 sections.info_or_types_offset = offset;
12691 sections.info_or_types_size = size;
12692 break;
12693 case DW_SECT_ABBREV:
12694 sections.abbrev_offset = offset;
12695 sections.abbrev_size = size;
12696 break;
12697 case DW_SECT_LINE:
12698 sections.line_offset = offset;
12699 sections.line_size = size;
12700 break;
12701 case DW_SECT_LOC:
12702 sections.loc_offset = offset;
12703 sections.loc_size = size;
12704 break;
12705 case DW_SECT_STR_OFFSETS:
12706 sections.str_offsets_offset = offset;
12707 sections.str_offsets_size = size;
12708 break;
12709 case DW_SECT_MACINFO:
12710 sections.macinfo_offset = offset;
12711 sections.macinfo_size = size;
12712 break;
12713 case DW_SECT_MACRO:
12714 sections.macro_offset = offset;
12715 sections.macro_size = size;
12716 break;
12717 }
12718 }
12719
12720 /* It's easier for the rest of the code if we fake a struct dwo_file and
12721 have dwo_unit "live" in that. At least for now.
12722
12723 The DWP file can be made up of a random collection of CUs and TUs.
12724 However, for each CU + set of TUs that came from the same original DWO
12725 file, we can combine them back into a virtual DWO file to save space
12726 (fewer struct dwo_file objects to allocate). Remember that for really
12727 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12728
12729 std::string virtual_dwo_name =
12730 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12731 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12732 (long) (sections.line_size ? sections.line_offset : 0),
12733 (long) (sections.loc_size ? sections.loc_offset : 0),
12734 (long) (sections.str_offsets_size
12735 ? sections.str_offsets_offset : 0));
12736 /* Can we use an existing virtual DWO file? */
12737 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12738 virtual_dwo_name.c_str (),
12739 comp_dir);
12740 /* Create one if necessary. */
12741 if (*dwo_file_slot == NULL)
12742 {
12743 if (dwarf_read_debug)
12744 {
12745 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12746 virtual_dwo_name.c_str ());
12747 }
12748 dwo_file = new struct dwo_file;
12749 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
12750 virtual_dwo_name);
12751 dwo_file->comp_dir = comp_dir;
12752 dwo_file->sections.abbrev =
12753 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
12754 sections.abbrev_offset, sections.abbrev_size);
12755 dwo_file->sections.line =
12756 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
12757 sections.line_offset, sections.line_size);
12758 dwo_file->sections.loc =
12759 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
12760 sections.loc_offset, sections.loc_size);
12761 dwo_file->sections.macinfo =
12762 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
12763 sections.macinfo_offset, sections.macinfo_size);
12764 dwo_file->sections.macro =
12765 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
12766 sections.macro_offset, sections.macro_size);
12767 dwo_file->sections.str_offsets =
12768 create_dwp_v2_section (dwarf2_per_objfile,
12769 &dwp_file->sections.str_offsets,
12770 sections.str_offsets_offset,
12771 sections.str_offsets_size);
12772 /* The "str" section is global to the entire DWP file. */
12773 dwo_file->sections.str = dwp_file->sections.str;
12774 /* The info or types section is assigned below to dwo_unit,
12775 there's no need to record it in dwo_file.
12776 Also, we can't simply record type sections in dwo_file because
12777 we record a pointer into the vector in dwo_unit. As we collect more
12778 types we'll grow the vector and eventually have to reallocate space
12779 for it, invalidating all copies of pointers into the previous
12780 contents. */
12781 *dwo_file_slot = dwo_file;
12782 }
12783 else
12784 {
12785 if (dwarf_read_debug)
12786 {
12787 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12788 virtual_dwo_name.c_str ());
12789 }
12790 dwo_file = (struct dwo_file *) *dwo_file_slot;
12791 }
12792
12793 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12794 dwo_unit->dwo_file = dwo_file;
12795 dwo_unit->signature = signature;
12796 dwo_unit->section =
12797 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
12798 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12799 is_debug_types
12800 ? &dwp_file->sections.types
12801 : &dwp_file->sections.info,
12802 sections.info_or_types_offset,
12803 sections.info_or_types_size);
12804 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12805
12806 return dwo_unit;
12807 }
12808
12809 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12810 Returns NULL if the signature isn't found. */
12811
12812 static struct dwo_unit *
12813 lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12814 struct dwp_file *dwp_file, const char *comp_dir,
12815 ULONGEST signature, int is_debug_types)
12816 {
12817 const struct dwp_hash_table *dwp_htab =
12818 is_debug_types ? dwp_file->tus : dwp_file->cus;
12819 bfd *dbfd = dwp_file->dbfd.get ();
12820 uint32_t mask = dwp_htab->nr_slots - 1;
12821 uint32_t hash = signature & mask;
12822 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12823 unsigned int i;
12824 void **slot;
12825 struct dwo_unit find_dwo_cu;
12826
12827 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12828 find_dwo_cu.signature = signature;
12829 slot = htab_find_slot (is_debug_types
12830 ? dwp_file->loaded_tus
12831 : dwp_file->loaded_cus,
12832 &find_dwo_cu, INSERT);
12833
12834 if (*slot != NULL)
12835 return (struct dwo_unit *) *slot;
12836
12837 /* Use a for loop so that we don't loop forever on bad debug info. */
12838 for (i = 0; i < dwp_htab->nr_slots; ++i)
12839 {
12840 ULONGEST signature_in_table;
12841
12842 signature_in_table =
12843 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
12844 if (signature_in_table == signature)
12845 {
12846 uint32_t unit_index =
12847 read_4_bytes (dbfd,
12848 dwp_htab->unit_table + hash * sizeof (uint32_t));
12849
12850 if (dwp_file->version == 1)
12851 {
12852 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12853 dwp_file, unit_index,
12854 comp_dir, signature,
12855 is_debug_types);
12856 }
12857 else
12858 {
12859 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12860 dwp_file, unit_index,
12861 comp_dir, signature,
12862 is_debug_types);
12863 }
12864 return (struct dwo_unit *) *slot;
12865 }
12866 if (signature_in_table == 0)
12867 return NULL;
12868 hash = (hash + hash2) & mask;
12869 }
12870
12871 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12872 " [in module %s]"),
12873 dwp_file->name);
12874 }
12875
12876 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12877 Open the file specified by FILE_NAME and hand it off to BFD for
12878 preliminary analysis. Return a newly initialized bfd *, which
12879 includes a canonicalized copy of FILE_NAME.
12880 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12881 SEARCH_CWD is true if the current directory is to be searched.
12882 It will be searched before debug-file-directory.
12883 If successful, the file is added to the bfd include table of the
12884 objfile's bfd (see gdb_bfd_record_inclusion).
12885 If unable to find/open the file, return NULL.
12886 NOTE: This function is derived from symfile_bfd_open. */
12887
12888 static gdb_bfd_ref_ptr
12889 try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12890 const char *file_name, int is_dwp, int search_cwd)
12891 {
12892 int desc;
12893 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12894 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12895 to debug_file_directory. */
12896 const char *search_path;
12897 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12898
12899 gdb::unique_xmalloc_ptr<char> search_path_holder;
12900 if (search_cwd)
12901 {
12902 if (*debug_file_directory != '\0')
12903 {
12904 search_path_holder.reset (concat (".", dirname_separator_string,
12905 debug_file_directory,
12906 (char *) NULL));
12907 search_path = search_path_holder.get ();
12908 }
12909 else
12910 search_path = ".";
12911 }
12912 else
12913 search_path = debug_file_directory;
12914
12915 openp_flags flags = OPF_RETURN_REALPATH;
12916 if (is_dwp)
12917 flags |= OPF_SEARCH_IN_PATH;
12918
12919 gdb::unique_xmalloc_ptr<char> absolute_name;
12920 desc = openp (search_path, flags, file_name,
12921 O_RDONLY | O_BINARY, &absolute_name);
12922 if (desc < 0)
12923 return NULL;
12924
12925 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12926 gnutarget, desc));
12927 if (sym_bfd == NULL)
12928 return NULL;
12929 bfd_set_cacheable (sym_bfd.get (), 1);
12930
12931 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12932 return NULL;
12933
12934 /* Success. Record the bfd as having been included by the objfile's bfd.
12935 This is important because things like demangled_names_hash lives in the
12936 objfile's per_bfd space and may have references to things like symbol
12937 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12938 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
12939
12940 return sym_bfd;
12941 }
12942
12943 /* Try to open DWO file FILE_NAME.
12944 COMP_DIR is the DW_AT_comp_dir attribute.
12945 The result is the bfd handle of the file.
12946 If there is a problem finding or opening the file, return NULL.
12947 Upon success, the canonicalized path of the file is stored in the bfd,
12948 same as symfile_bfd_open. */
12949
12950 static gdb_bfd_ref_ptr
12951 open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12952 const char *file_name, const char *comp_dir)
12953 {
12954 if (IS_ABSOLUTE_PATH (file_name))
12955 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12956 0 /*is_dwp*/, 0 /*search_cwd*/);
12957
12958 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12959
12960 if (comp_dir != NULL)
12961 {
12962 char *path_to_try = concat (comp_dir, SLASH_STRING,
12963 file_name, (char *) NULL);
12964
12965 /* NOTE: If comp_dir is a relative path, this will also try the
12966 search path, which seems useful. */
12967 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
12968 path_to_try,
12969 0 /*is_dwp*/,
12970 1 /*search_cwd*/));
12971 xfree (path_to_try);
12972 if (abfd != NULL)
12973 return abfd;
12974 }
12975
12976 /* That didn't work, try debug-file-directory, which, despite its name,
12977 is a list of paths. */
12978
12979 if (*debug_file_directory == '\0')
12980 return NULL;
12981
12982 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12983 0 /*is_dwp*/, 1 /*search_cwd*/);
12984 }
12985
12986 /* This function is mapped across the sections and remembers the offset and
12987 size of each of the DWO debugging sections we are interested in. */
12988
12989 static void
12990 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12991 {
12992 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
12993 const struct dwop_section_names *names = &dwop_section_names;
12994
12995 if (section_is_p (sectp->name, &names->abbrev_dwo))
12996 {
12997 dwo_sections->abbrev.s.section = sectp;
12998 dwo_sections->abbrev.size = bfd_section_size (sectp);
12999 }
13000 else if (section_is_p (sectp->name, &names->info_dwo))
13001 {
13002 dwo_sections->info.s.section = sectp;
13003 dwo_sections->info.size = bfd_section_size (sectp);
13004 }
13005 else if (section_is_p (sectp->name, &names->line_dwo))
13006 {
13007 dwo_sections->line.s.section = sectp;
13008 dwo_sections->line.size = bfd_section_size (sectp);
13009 }
13010 else if (section_is_p (sectp->name, &names->loc_dwo))
13011 {
13012 dwo_sections->loc.s.section = sectp;
13013 dwo_sections->loc.size = bfd_section_size (sectp);
13014 }
13015 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13016 {
13017 dwo_sections->macinfo.s.section = sectp;
13018 dwo_sections->macinfo.size = bfd_section_size (sectp);
13019 }
13020 else if (section_is_p (sectp->name, &names->macro_dwo))
13021 {
13022 dwo_sections->macro.s.section = sectp;
13023 dwo_sections->macro.size = bfd_section_size (sectp);
13024 }
13025 else if (section_is_p (sectp->name, &names->str_dwo))
13026 {
13027 dwo_sections->str.s.section = sectp;
13028 dwo_sections->str.size = bfd_section_size (sectp);
13029 }
13030 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13031 {
13032 dwo_sections->str_offsets.s.section = sectp;
13033 dwo_sections->str_offsets.size = bfd_section_size (sectp);
13034 }
13035 else if (section_is_p (sectp->name, &names->types_dwo))
13036 {
13037 struct dwarf2_section_info type_section;
13038
13039 memset (&type_section, 0, sizeof (type_section));
13040 type_section.s.section = sectp;
13041 type_section.size = bfd_section_size (sectp);
13042 dwo_sections->types.push_back (type_section);
13043 }
13044 }
13045
13046 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
13047 by PER_CU. This is for the non-DWP case.
13048 The result is NULL if DWO_NAME can't be found. */
13049
13050 static struct dwo_file *
13051 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
13052 const char *dwo_name, const char *comp_dir)
13053 {
13054 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
13055
13056 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
13057 if (dbfd == NULL)
13058 {
13059 if (dwarf_read_debug)
13060 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
13061 return NULL;
13062 }
13063
13064 dwo_file_up dwo_file (new struct dwo_file);
13065 dwo_file->dwo_name = dwo_name;
13066 dwo_file->comp_dir = comp_dir;
13067 dwo_file->dbfd = std::move (dbfd);
13068
13069 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
13070 &dwo_file->sections);
13071
13072 create_cus_hash_table (dwarf2_per_objfile, *dwo_file, dwo_file->sections.info,
13073 dwo_file->cus);
13074
13075 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
13076 dwo_file->sections.types, dwo_file->tus);
13077
13078 if (dwarf_read_debug)
13079 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
13080
13081 return dwo_file.release ();
13082 }
13083
13084 /* This function is mapped across the sections and remembers the offset and
13085 size of each of the DWP debugging sections common to version 1 and 2 that
13086 we are interested in. */
13087
13088 static void
13089 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
13090 void *dwp_file_ptr)
13091 {
13092 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
13093 const struct dwop_section_names *names = &dwop_section_names;
13094 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13095
13096 /* Record the ELF section number for later lookup: this is what the
13097 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13098 gdb_assert (elf_section_nr < dwp_file->num_sections);
13099 dwp_file->elf_sections[elf_section_nr] = sectp;
13100
13101 /* Look for specific sections that we need. */
13102 if (section_is_p (sectp->name, &names->str_dwo))
13103 {
13104 dwp_file->sections.str.s.section = sectp;
13105 dwp_file->sections.str.size = bfd_section_size (sectp);
13106 }
13107 else if (section_is_p (sectp->name, &names->cu_index))
13108 {
13109 dwp_file->sections.cu_index.s.section = sectp;
13110 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
13111 }
13112 else if (section_is_p (sectp->name, &names->tu_index))
13113 {
13114 dwp_file->sections.tu_index.s.section = sectp;
13115 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
13116 }
13117 }
13118
13119 /* This function is mapped across the sections and remembers the offset and
13120 size of each of the DWP version 2 debugging sections that we are interested
13121 in. This is split into a separate function because we don't know if we
13122 have version 1 or 2 until we parse the cu_index/tu_index sections. */
13123
13124 static void
13125 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
13126 {
13127 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
13128 const struct dwop_section_names *names = &dwop_section_names;
13129 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13130
13131 /* Record the ELF section number for later lookup: this is what the
13132 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13133 gdb_assert (elf_section_nr < dwp_file->num_sections);
13134 dwp_file->elf_sections[elf_section_nr] = sectp;
13135
13136 /* Look for specific sections that we need. */
13137 if (section_is_p (sectp->name, &names->abbrev_dwo))
13138 {
13139 dwp_file->sections.abbrev.s.section = sectp;
13140 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
13141 }
13142 else if (section_is_p (sectp->name, &names->info_dwo))
13143 {
13144 dwp_file->sections.info.s.section = sectp;
13145 dwp_file->sections.info.size = bfd_section_size (sectp);
13146 }
13147 else if (section_is_p (sectp->name, &names->line_dwo))
13148 {
13149 dwp_file->sections.line.s.section = sectp;
13150 dwp_file->sections.line.size = bfd_section_size (sectp);
13151 }
13152 else if (section_is_p (sectp->name, &names->loc_dwo))
13153 {
13154 dwp_file->sections.loc.s.section = sectp;
13155 dwp_file->sections.loc.size = bfd_section_size (sectp);
13156 }
13157 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13158 {
13159 dwp_file->sections.macinfo.s.section = sectp;
13160 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
13161 }
13162 else if (section_is_p (sectp->name, &names->macro_dwo))
13163 {
13164 dwp_file->sections.macro.s.section = sectp;
13165 dwp_file->sections.macro.size = bfd_section_size (sectp);
13166 }
13167 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13168 {
13169 dwp_file->sections.str_offsets.s.section = sectp;
13170 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
13171 }
13172 else if (section_is_p (sectp->name, &names->types_dwo))
13173 {
13174 dwp_file->sections.types.s.section = sectp;
13175 dwp_file->sections.types.size = bfd_section_size (sectp);
13176 }
13177 }
13178
13179 /* Hash function for dwp_file loaded CUs/TUs. */
13180
13181 static hashval_t
13182 hash_dwp_loaded_cutus (const void *item)
13183 {
13184 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
13185
13186 /* This drops the top 32 bits of the signature, but is ok for a hash. */
13187 return dwo_unit->signature;
13188 }
13189
13190 /* Equality function for dwp_file loaded CUs/TUs. */
13191
13192 static int
13193 eq_dwp_loaded_cutus (const void *a, const void *b)
13194 {
13195 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13196 const struct dwo_unit *dub = (const struct dwo_unit *) b;
13197
13198 return dua->signature == dub->signature;
13199 }
13200
13201 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
13202
13203 static htab_t
13204 allocate_dwp_loaded_cutus_table (struct objfile *objfile)
13205 {
13206 return htab_create_alloc_ex (3,
13207 hash_dwp_loaded_cutus,
13208 eq_dwp_loaded_cutus,
13209 NULL,
13210 &objfile->objfile_obstack,
13211 hashtab_obstack_allocate,
13212 dummy_obstack_deallocate);
13213 }
13214
13215 /* Try to open DWP file FILE_NAME.
13216 The result is the bfd handle of the file.
13217 If there is a problem finding or opening the file, return NULL.
13218 Upon success, the canonicalized path of the file is stored in the bfd,
13219 same as symfile_bfd_open. */
13220
13221 static gdb_bfd_ref_ptr
13222 open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
13223 const char *file_name)
13224 {
13225 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
13226 1 /*is_dwp*/,
13227 1 /*search_cwd*/));
13228 if (abfd != NULL)
13229 return abfd;
13230
13231 /* Work around upstream bug 15652.
13232 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13233 [Whether that's a "bug" is debatable, but it is getting in our way.]
13234 We have no real idea where the dwp file is, because gdb's realpath-ing
13235 of the executable's path may have discarded the needed info.
13236 [IWBN if the dwp file name was recorded in the executable, akin to
13237 .gnu_debuglink, but that doesn't exist yet.]
13238 Strip the directory from FILE_NAME and search again. */
13239 if (*debug_file_directory != '\0')
13240 {
13241 /* Don't implicitly search the current directory here.
13242 If the user wants to search "." to handle this case,
13243 it must be added to debug-file-directory. */
13244 return try_open_dwop_file (dwarf2_per_objfile,
13245 lbasename (file_name), 1 /*is_dwp*/,
13246 0 /*search_cwd*/);
13247 }
13248
13249 return NULL;
13250 }
13251
13252 /* Initialize the use of the DWP file for the current objfile.
13253 By convention the name of the DWP file is ${objfile}.dwp.
13254 The result is NULL if it can't be found. */
13255
13256 static std::unique_ptr<struct dwp_file>
13257 open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
13258 {
13259 struct objfile *objfile = dwarf2_per_objfile->objfile;
13260
13261 /* Try to find first .dwp for the binary file before any symbolic links
13262 resolving. */
13263
13264 /* If the objfile is a debug file, find the name of the real binary
13265 file and get the name of dwp file from there. */
13266 std::string dwp_name;
13267 if (objfile->separate_debug_objfile_backlink != NULL)
13268 {
13269 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13270 const char *backlink_basename = lbasename (backlink->original_name);
13271
13272 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
13273 }
13274 else
13275 dwp_name = objfile->original_name;
13276
13277 dwp_name += ".dwp";
13278
13279 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
13280 if (dbfd == NULL
13281 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13282 {
13283 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
13284 dwp_name = objfile_name (objfile);
13285 dwp_name += ".dwp";
13286 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
13287 }
13288
13289 if (dbfd == NULL)
13290 {
13291 if (dwarf_read_debug)
13292 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
13293 return std::unique_ptr<dwp_file> ();
13294 }
13295
13296 const char *name = bfd_get_filename (dbfd.get ());
13297 std::unique_ptr<struct dwp_file> dwp_file
13298 (new struct dwp_file (name, std::move (dbfd)));
13299
13300 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
13301 dwp_file->elf_sections =
13302 OBSTACK_CALLOC (&objfile->objfile_obstack,
13303 dwp_file->num_sections, asection *);
13304
13305 bfd_map_over_sections (dwp_file->dbfd.get (),
13306 dwarf2_locate_common_dwp_sections,
13307 dwp_file.get ());
13308
13309 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
13310 0);
13311
13312 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
13313 1);
13314
13315 /* The DWP file version is stored in the hash table. Oh well. */
13316 if (dwp_file->cus && dwp_file->tus
13317 && dwp_file->cus->version != dwp_file->tus->version)
13318 {
13319 /* Technically speaking, we should try to limp along, but this is
13320 pretty bizarre. We use pulongest here because that's the established
13321 portability solution (e.g, we cannot use %u for uint32_t). */
13322 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13323 " TU version %s [in DWP file %s]"),
13324 pulongest (dwp_file->cus->version),
13325 pulongest (dwp_file->tus->version), dwp_name.c_str ());
13326 }
13327
13328 if (dwp_file->cus)
13329 dwp_file->version = dwp_file->cus->version;
13330 else if (dwp_file->tus)
13331 dwp_file->version = dwp_file->tus->version;
13332 else
13333 dwp_file->version = 2;
13334
13335 if (dwp_file->version == 2)
13336 bfd_map_over_sections (dwp_file->dbfd.get (),
13337 dwarf2_locate_v2_dwp_sections,
13338 dwp_file.get ());
13339
13340 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
13341 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
13342
13343 if (dwarf_read_debug)
13344 {
13345 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
13346 fprintf_unfiltered (gdb_stdlog,
13347 " %s CUs, %s TUs\n",
13348 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13349 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
13350 }
13351
13352 return dwp_file;
13353 }
13354
13355 /* Wrapper around open_and_init_dwp_file, only open it once. */
13356
13357 static struct dwp_file *
13358 get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
13359 {
13360 if (! dwarf2_per_objfile->dwp_checked)
13361 {
13362 dwarf2_per_objfile->dwp_file
13363 = open_and_init_dwp_file (dwarf2_per_objfile);
13364 dwarf2_per_objfile->dwp_checked = 1;
13365 }
13366 return dwarf2_per_objfile->dwp_file.get ();
13367 }
13368
13369 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13370 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13371 or in the DWP file for the objfile, referenced by THIS_UNIT.
13372 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
13373 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13374
13375 This is called, for example, when wanting to read a variable with a
13376 complex location. Therefore we don't want to do file i/o for every call.
13377 Therefore we don't want to look for a DWO file on every call.
13378 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13379 then we check if we've already seen DWO_NAME, and only THEN do we check
13380 for a DWO file.
13381
13382 The result is a pointer to the dwo_unit object or NULL if we didn't find it
13383 (dwo_id mismatch or couldn't find the DWO/DWP file). */
13384
13385 static struct dwo_unit *
13386 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
13387 const char *dwo_name, const char *comp_dir,
13388 ULONGEST signature, int is_debug_types)
13389 {
13390 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
13391 struct objfile *objfile = dwarf2_per_objfile->objfile;
13392 const char *kind = is_debug_types ? "TU" : "CU";
13393 void **dwo_file_slot;
13394 struct dwo_file *dwo_file;
13395 struct dwp_file *dwp_file;
13396
13397 /* First see if there's a DWP file.
13398 If we have a DWP file but didn't find the DWO inside it, don't
13399 look for the original DWO file. It makes gdb behave differently
13400 depending on whether one is debugging in the build tree. */
13401
13402 dwp_file = get_dwp_file (dwarf2_per_objfile);
13403 if (dwp_file != NULL)
13404 {
13405 const struct dwp_hash_table *dwp_htab =
13406 is_debug_types ? dwp_file->tus : dwp_file->cus;
13407
13408 if (dwp_htab != NULL)
13409 {
13410 struct dwo_unit *dwo_cutu =
13411 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
13412 signature, is_debug_types);
13413
13414 if (dwo_cutu != NULL)
13415 {
13416 if (dwarf_read_debug)
13417 {
13418 fprintf_unfiltered (gdb_stdlog,
13419 "Virtual DWO %s %s found: @%s\n",
13420 kind, hex_string (signature),
13421 host_address_to_string (dwo_cutu));
13422 }
13423 return dwo_cutu;
13424 }
13425 }
13426 }
13427 else
13428 {
13429 /* No DWP file, look for the DWO file. */
13430
13431 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
13432 dwo_name, comp_dir);
13433 if (*dwo_file_slot == NULL)
13434 {
13435 /* Read in the file and build a table of the CUs/TUs it contains. */
13436 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
13437 }
13438 /* NOTE: This will be NULL if unable to open the file. */
13439 dwo_file = (struct dwo_file *) *dwo_file_slot;
13440
13441 if (dwo_file != NULL)
13442 {
13443 struct dwo_unit *dwo_cutu = NULL;
13444
13445 if (is_debug_types && dwo_file->tus)
13446 {
13447 struct dwo_unit find_dwo_cutu;
13448
13449 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13450 find_dwo_cutu.signature = signature;
13451 dwo_cutu
13452 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
13453 }
13454 else if (!is_debug_types && dwo_file->cus)
13455 {
13456 struct dwo_unit find_dwo_cutu;
13457
13458 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13459 find_dwo_cutu.signature = signature;
13460 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus,
13461 &find_dwo_cutu);
13462 }
13463
13464 if (dwo_cutu != NULL)
13465 {
13466 if (dwarf_read_debug)
13467 {
13468 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
13469 kind, dwo_name, hex_string (signature),
13470 host_address_to_string (dwo_cutu));
13471 }
13472 return dwo_cutu;
13473 }
13474 }
13475 }
13476
13477 /* We didn't find it. This could mean a dwo_id mismatch, or
13478 someone deleted the DWO/DWP file, or the search path isn't set up
13479 correctly to find the file. */
13480
13481 if (dwarf_read_debug)
13482 {
13483 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
13484 kind, dwo_name, hex_string (signature));
13485 }
13486
13487 /* This is a warning and not a complaint because it can be caused by
13488 pilot error (e.g., user accidentally deleting the DWO). */
13489 {
13490 /* Print the name of the DWP file if we looked there, helps the user
13491 better diagnose the problem. */
13492 std::string dwp_text;
13493
13494 if (dwp_file != NULL)
13495 dwp_text = string_printf (" [in DWP file %s]",
13496 lbasename (dwp_file->name));
13497
13498 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
13499 " [in module %s]"),
13500 kind, dwo_name, hex_string (signature),
13501 dwp_text.c_str (),
13502 this_unit->is_debug_types ? "TU" : "CU",
13503 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
13504 }
13505 return NULL;
13506 }
13507
13508 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13509 See lookup_dwo_cutu_unit for details. */
13510
13511 static struct dwo_unit *
13512 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
13513 const char *dwo_name, const char *comp_dir,
13514 ULONGEST signature)
13515 {
13516 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
13517 }
13518
13519 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13520 See lookup_dwo_cutu_unit for details. */
13521
13522 static struct dwo_unit *
13523 lookup_dwo_type_unit (struct signatured_type *this_tu,
13524 const char *dwo_name, const char *comp_dir)
13525 {
13526 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
13527 }
13528
13529 /* Traversal function for queue_and_load_all_dwo_tus. */
13530
13531 static int
13532 queue_and_load_dwo_tu (void **slot, void *info)
13533 {
13534 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13535 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
13536 ULONGEST signature = dwo_unit->signature;
13537 struct signatured_type *sig_type =
13538 lookup_dwo_signatured_type (per_cu->cu, signature);
13539
13540 if (sig_type != NULL)
13541 {
13542 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13543
13544 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13545 a real dependency of PER_CU on SIG_TYPE. That is detected later
13546 while processing PER_CU. */
13547 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
13548 load_full_type_unit (sig_cu);
13549 per_cu->imported_symtabs_push (sig_cu);
13550 }
13551
13552 return 1;
13553 }
13554
13555 /* Queue all TUs contained in the DWO of PER_CU to be read in.
13556 The DWO may have the only definition of the type, though it may not be
13557 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13558 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13559
13560 static void
13561 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
13562 {
13563 struct dwo_unit *dwo_unit;
13564 struct dwo_file *dwo_file;
13565
13566 gdb_assert (!per_cu->is_debug_types);
13567 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
13568 gdb_assert (per_cu->cu != NULL);
13569
13570 dwo_unit = per_cu->cu->dwo_unit;
13571 gdb_assert (dwo_unit != NULL);
13572
13573 dwo_file = dwo_unit->dwo_file;
13574 if (dwo_file->tus != NULL)
13575 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
13576 }
13577
13578 /* Read in various DIEs. */
13579
13580 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
13581 Inherit only the children of the DW_AT_abstract_origin DIE not being
13582 already referenced by DW_AT_abstract_origin from the children of the
13583 current DIE. */
13584
13585 static void
13586 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13587 {
13588 struct die_info *child_die;
13589 sect_offset *offsetp;
13590 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13591 struct die_info *origin_die;
13592 /* Iterator of the ORIGIN_DIE children. */
13593 struct die_info *origin_child_die;
13594 struct attribute *attr;
13595 struct dwarf2_cu *origin_cu;
13596 struct pending **origin_previous_list_in_scope;
13597
13598 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13599 if (!attr)
13600 return;
13601
13602 /* Note that following die references may follow to a die in a
13603 different cu. */
13604
13605 origin_cu = cu;
13606 origin_die = follow_die_ref (die, attr, &origin_cu);
13607
13608 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13609 symbols in. */
13610 origin_previous_list_in_scope = origin_cu->list_in_scope;
13611 origin_cu->list_in_scope = cu->list_in_scope;
13612
13613 if (die->tag != origin_die->tag
13614 && !(die->tag == DW_TAG_inlined_subroutine
13615 && origin_die->tag == DW_TAG_subprogram))
13616 complaint (_("DIE %s and its abstract origin %s have different tags"),
13617 sect_offset_str (die->sect_off),
13618 sect_offset_str (origin_die->sect_off));
13619
13620 std::vector<sect_offset> offsets;
13621
13622 for (child_die = die->child;
13623 child_die && child_die->tag;
13624 child_die = sibling_die (child_die))
13625 {
13626 struct die_info *child_origin_die;
13627 struct dwarf2_cu *child_origin_cu;
13628
13629 /* We are trying to process concrete instance entries:
13630 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
13631 it's not relevant to our analysis here. i.e. detecting DIEs that are
13632 present in the abstract instance but not referenced in the concrete
13633 one. */
13634 if (child_die->tag == DW_TAG_call_site
13635 || child_die->tag == DW_TAG_GNU_call_site)
13636 continue;
13637
13638 /* For each CHILD_DIE, find the corresponding child of
13639 ORIGIN_DIE. If there is more than one layer of
13640 DW_AT_abstract_origin, follow them all; there shouldn't be,
13641 but GCC versions at least through 4.4 generate this (GCC PR
13642 40573). */
13643 child_origin_die = child_die;
13644 child_origin_cu = cu;
13645 while (1)
13646 {
13647 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13648 child_origin_cu);
13649 if (attr == NULL)
13650 break;
13651 child_origin_die = follow_die_ref (child_origin_die, attr,
13652 &child_origin_cu);
13653 }
13654
13655 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13656 counterpart may exist. */
13657 if (child_origin_die != child_die)
13658 {
13659 if (child_die->tag != child_origin_die->tag
13660 && !(child_die->tag == DW_TAG_inlined_subroutine
13661 && child_origin_die->tag == DW_TAG_subprogram))
13662 complaint (_("Child DIE %s and its abstract origin %s have "
13663 "different tags"),
13664 sect_offset_str (child_die->sect_off),
13665 sect_offset_str (child_origin_die->sect_off));
13666 if (child_origin_die->parent != origin_die)
13667 complaint (_("Child DIE %s and its abstract origin %s have "
13668 "different parents"),
13669 sect_offset_str (child_die->sect_off),
13670 sect_offset_str (child_origin_die->sect_off));
13671 else
13672 offsets.push_back (child_origin_die->sect_off);
13673 }
13674 }
13675 std::sort (offsets.begin (), offsets.end ());
13676 sect_offset *offsets_end = offsets.data () + offsets.size ();
13677 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
13678 if (offsetp[-1] == *offsetp)
13679 complaint (_("Multiple children of DIE %s refer "
13680 "to DIE %s as their abstract origin"),
13681 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
13682
13683 offsetp = offsets.data ();
13684 origin_child_die = origin_die->child;
13685 while (origin_child_die && origin_child_die->tag)
13686 {
13687 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
13688 while (offsetp < offsets_end
13689 && *offsetp < origin_child_die->sect_off)
13690 offsetp++;
13691 if (offsetp >= offsets_end
13692 || *offsetp > origin_child_die->sect_off)
13693 {
13694 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13695 Check whether we're already processing ORIGIN_CHILD_DIE.
13696 This can happen with mutually referenced abstract_origins.
13697 PR 16581. */
13698 if (!origin_child_die->in_process)
13699 process_die (origin_child_die, origin_cu);
13700 }
13701 origin_child_die = sibling_die (origin_child_die);
13702 }
13703 origin_cu->list_in_scope = origin_previous_list_in_scope;
13704
13705 if (cu != origin_cu)
13706 compute_delayed_physnames (origin_cu);
13707 }
13708
13709 static void
13710 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
13711 {
13712 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13713 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13714 struct context_stack *newobj;
13715 CORE_ADDR lowpc;
13716 CORE_ADDR highpc;
13717 struct die_info *child_die;
13718 struct attribute *attr, *call_line, *call_file;
13719 const char *name;
13720 CORE_ADDR baseaddr;
13721 struct block *block;
13722 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13723 std::vector<struct symbol *> template_args;
13724 struct template_symbol *templ_func = NULL;
13725
13726 if (inlined_func)
13727 {
13728 /* If we do not have call site information, we can't show the
13729 caller of this inlined function. That's too confusing, so
13730 only use the scope for local variables. */
13731 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13732 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13733 if (call_line == NULL || call_file == NULL)
13734 {
13735 read_lexical_block_scope (die, cu);
13736 return;
13737 }
13738 }
13739
13740 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13741
13742 name = dwarf2_name (die, cu);
13743
13744 /* Ignore functions with missing or empty names. These are actually
13745 illegal according to the DWARF standard. */
13746 if (name == NULL)
13747 {
13748 complaint (_("missing name for subprogram DIE at %s"),
13749 sect_offset_str (die->sect_off));
13750 return;
13751 }
13752
13753 /* Ignore functions with missing or invalid low and high pc attributes. */
13754 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
13755 <= PC_BOUNDS_INVALID)
13756 {
13757 attr = dwarf2_attr (die, DW_AT_external, cu);
13758 if (!attr || !DW_UNSND (attr))
13759 complaint (_("cannot get low and high bounds "
13760 "for subprogram DIE at %s"),
13761 sect_offset_str (die->sect_off));
13762 return;
13763 }
13764
13765 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13766 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13767
13768 /* If we have any template arguments, then we must allocate a
13769 different sort of symbol. */
13770 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
13771 {
13772 if (child_die->tag == DW_TAG_template_type_param
13773 || child_die->tag == DW_TAG_template_value_param)
13774 {
13775 templ_func = allocate_template_symbol (objfile);
13776 templ_func->subclass = SYMBOL_TEMPLATE;
13777 break;
13778 }
13779 }
13780
13781 newobj = cu->get_builder ()->push_context (0, lowpc);
13782 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13783 (struct symbol *) templ_func);
13784
13785 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
13786 set_objfile_main_name (objfile, newobj->name->linkage_name (),
13787 cu->language);
13788
13789 /* If there is a location expression for DW_AT_frame_base, record
13790 it. */
13791 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
13792 if (attr != nullptr)
13793 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
13794
13795 /* If there is a location for the static link, record it. */
13796 newobj->static_link = NULL;
13797 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13798 if (attr != nullptr)
13799 {
13800 newobj->static_link
13801 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
13802 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
13803 dwarf2_per_cu_addr_type (cu->per_cu));
13804 }
13805
13806 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
13807
13808 if (die->child != NULL)
13809 {
13810 child_die = die->child;
13811 while (child_die && child_die->tag)
13812 {
13813 if (child_die->tag == DW_TAG_template_type_param
13814 || child_die->tag == DW_TAG_template_value_param)
13815 {
13816 struct symbol *arg = new_symbol (child_die, NULL, cu);
13817
13818 if (arg != NULL)
13819 template_args.push_back (arg);
13820 }
13821 else
13822 process_die (child_die, cu);
13823 child_die = sibling_die (child_die);
13824 }
13825 }
13826
13827 inherit_abstract_dies (die, cu);
13828
13829 /* If we have a DW_AT_specification, we might need to import using
13830 directives from the context of the specification DIE. See the
13831 comment in determine_prefix. */
13832 if (cu->language == language_cplus
13833 && dwarf2_attr (die, DW_AT_specification, cu))
13834 {
13835 struct dwarf2_cu *spec_cu = cu;
13836 struct die_info *spec_die = die_specification (die, &spec_cu);
13837
13838 while (spec_die)
13839 {
13840 child_die = spec_die->child;
13841 while (child_die && child_die->tag)
13842 {
13843 if (child_die->tag == DW_TAG_imported_module)
13844 process_die (child_die, spec_cu);
13845 child_die = sibling_die (child_die);
13846 }
13847
13848 /* In some cases, GCC generates specification DIEs that
13849 themselves contain DW_AT_specification attributes. */
13850 spec_die = die_specification (spec_die, &spec_cu);
13851 }
13852 }
13853
13854 struct context_stack cstk = cu->get_builder ()->pop_context ();
13855 /* Make a block for the local symbols within. */
13856 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
13857 cstk.static_link, lowpc, highpc);
13858
13859 /* For C++, set the block's scope. */
13860 if ((cu->language == language_cplus
13861 || cu->language == language_fortran
13862 || cu->language == language_d
13863 || cu->language == language_rust)
13864 && cu->processing_has_namespace_info)
13865 block_set_scope (block, determine_prefix (die, cu),
13866 &objfile->objfile_obstack);
13867
13868 /* If we have address ranges, record them. */
13869 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13870
13871 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
13872
13873 /* Attach template arguments to function. */
13874 if (!template_args.empty ())
13875 {
13876 gdb_assert (templ_func != NULL);
13877
13878 templ_func->n_template_arguments = template_args.size ();
13879 templ_func->template_arguments
13880 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13881 templ_func->n_template_arguments);
13882 memcpy (templ_func->template_arguments,
13883 template_args.data (),
13884 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13885
13886 /* Make sure that the symtab is set on the new symbols. Even
13887 though they don't appear in this symtab directly, other parts
13888 of gdb assume that symbols do, and this is reasonably
13889 true. */
13890 for (symbol *sym : template_args)
13891 symbol_set_symtab (sym, symbol_symtab (templ_func));
13892 }
13893
13894 /* In C++, we can have functions nested inside functions (e.g., when
13895 a function declares a class that has methods). This means that
13896 when we finish processing a function scope, we may need to go
13897 back to building a containing block's symbol lists. */
13898 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13899 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13900
13901 /* If we've finished processing a top-level function, subsequent
13902 symbols go in the file symbol list. */
13903 if (cu->get_builder ()->outermost_context_p ())
13904 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
13905 }
13906
13907 /* Process all the DIES contained within a lexical block scope. Start
13908 a new scope, process the dies, and then close the scope. */
13909
13910 static void
13911 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13912 {
13913 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13914 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13915 CORE_ADDR lowpc, highpc;
13916 struct die_info *child_die;
13917 CORE_ADDR baseaddr;
13918
13919 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13920
13921 /* Ignore blocks with missing or invalid low and high pc attributes. */
13922 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13923 as multiple lexical blocks? Handling children in a sane way would
13924 be nasty. Might be easier to properly extend generic blocks to
13925 describe ranges. */
13926 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13927 {
13928 case PC_BOUNDS_NOT_PRESENT:
13929 /* DW_TAG_lexical_block has no attributes, process its children as if
13930 there was no wrapping by that DW_TAG_lexical_block.
13931 GCC does no longer produces such DWARF since GCC r224161. */
13932 for (child_die = die->child;
13933 child_die != NULL && child_die->tag;
13934 child_die = sibling_die (child_die))
13935 process_die (child_die, cu);
13936 return;
13937 case PC_BOUNDS_INVALID:
13938 return;
13939 }
13940 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13941 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13942
13943 cu->get_builder ()->push_context (0, lowpc);
13944 if (die->child != NULL)
13945 {
13946 child_die = die->child;
13947 while (child_die && child_die->tag)
13948 {
13949 process_die (child_die, cu);
13950 child_die = sibling_die (child_die);
13951 }
13952 }
13953 inherit_abstract_dies (die, cu);
13954 struct context_stack cstk = cu->get_builder ()->pop_context ();
13955
13956 if (*cu->get_builder ()->get_local_symbols () != NULL
13957 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13958 {
13959 struct block *block
13960 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13961 cstk.start_addr, highpc);
13962
13963 /* Note that recording ranges after traversing children, as we
13964 do here, means that recording a parent's ranges entails
13965 walking across all its children's ranges as they appear in
13966 the address map, which is quadratic behavior.
13967
13968 It would be nicer to record the parent's ranges before
13969 traversing its children, simply overriding whatever you find
13970 there. But since we don't even decide whether to create a
13971 block until after we've traversed its children, that's hard
13972 to do. */
13973 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13974 }
13975 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13976 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13977 }
13978
13979 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13980
13981 static void
13982 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13983 {
13984 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13985 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13986 CORE_ADDR pc, baseaddr;
13987 struct attribute *attr;
13988 struct call_site *call_site, call_site_local;
13989 void **slot;
13990 int nparams;
13991 struct die_info *child_die;
13992
13993 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13994
13995 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13996 if (attr == NULL)
13997 {
13998 /* This was a pre-DWARF-5 GNU extension alias
13999 for DW_AT_call_return_pc. */
14000 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14001 }
14002 if (!attr)
14003 {
14004 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
14005 "DIE %s [in module %s]"),
14006 sect_offset_str (die->sect_off), objfile_name (objfile));
14007 return;
14008 }
14009 pc = attr_value_as_address (attr) + baseaddr;
14010 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
14011
14012 if (cu->call_site_htab == NULL)
14013 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
14014 NULL, &objfile->objfile_obstack,
14015 hashtab_obstack_allocate, NULL);
14016 call_site_local.pc = pc;
14017 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
14018 if (*slot != NULL)
14019 {
14020 complaint (_("Duplicate PC %s for DW_TAG_call_site "
14021 "DIE %s [in module %s]"),
14022 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
14023 objfile_name (objfile));
14024 return;
14025 }
14026
14027 /* Count parameters at the caller. */
14028
14029 nparams = 0;
14030 for (child_die = die->child; child_die && child_die->tag;
14031 child_die = sibling_die (child_die))
14032 {
14033 if (child_die->tag != DW_TAG_call_site_parameter
14034 && child_die->tag != DW_TAG_GNU_call_site_parameter)
14035 {
14036 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
14037 "DW_TAG_call_site child DIE %s [in module %s]"),
14038 child_die->tag, sect_offset_str (child_die->sect_off),
14039 objfile_name (objfile));
14040 continue;
14041 }
14042
14043 nparams++;
14044 }
14045
14046 call_site
14047 = ((struct call_site *)
14048 obstack_alloc (&objfile->objfile_obstack,
14049 sizeof (*call_site)
14050 + (sizeof (*call_site->parameter) * (nparams - 1))));
14051 *slot = call_site;
14052 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
14053 call_site->pc = pc;
14054
14055 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
14056 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
14057 {
14058 struct die_info *func_die;
14059
14060 /* Skip also over DW_TAG_inlined_subroutine. */
14061 for (func_die = die->parent;
14062 func_die && func_die->tag != DW_TAG_subprogram
14063 && func_die->tag != DW_TAG_subroutine_type;
14064 func_die = func_die->parent);
14065
14066 /* DW_AT_call_all_calls is a superset
14067 of DW_AT_call_all_tail_calls. */
14068 if (func_die
14069 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
14070 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
14071 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
14072 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
14073 {
14074 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
14075 not complete. But keep CALL_SITE for look ups via call_site_htab,
14076 both the initial caller containing the real return address PC and
14077 the final callee containing the current PC of a chain of tail
14078 calls do not need to have the tail call list complete. But any
14079 function candidate for a virtual tail call frame searched via
14080 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
14081 determined unambiguously. */
14082 }
14083 else
14084 {
14085 struct type *func_type = NULL;
14086
14087 if (func_die)
14088 func_type = get_die_type (func_die, cu);
14089 if (func_type != NULL)
14090 {
14091 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
14092
14093 /* Enlist this call site to the function. */
14094 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
14095 TYPE_TAIL_CALL_LIST (func_type) = call_site;
14096 }
14097 else
14098 complaint (_("Cannot find function owning DW_TAG_call_site "
14099 "DIE %s [in module %s]"),
14100 sect_offset_str (die->sect_off), objfile_name (objfile));
14101 }
14102 }
14103
14104 attr = dwarf2_attr (die, DW_AT_call_target, cu);
14105 if (attr == NULL)
14106 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
14107 if (attr == NULL)
14108 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
14109 if (attr == NULL)
14110 {
14111 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
14112 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14113 }
14114 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
14115 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
14116 /* Keep NULL DWARF_BLOCK. */;
14117 else if (attr_form_is_block (attr))
14118 {
14119 struct dwarf2_locexpr_baton *dlbaton;
14120
14121 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
14122 dlbaton->data = DW_BLOCK (attr)->data;
14123 dlbaton->size = DW_BLOCK (attr)->size;
14124 dlbaton->per_cu = cu->per_cu;
14125
14126 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
14127 }
14128 else if (attr_form_is_ref (attr))
14129 {
14130 struct dwarf2_cu *target_cu = cu;
14131 struct die_info *target_die;
14132
14133 target_die = follow_die_ref (die, attr, &target_cu);
14134 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
14135 if (die_is_declaration (target_die, target_cu))
14136 {
14137 const char *target_physname;
14138
14139 /* Prefer the mangled name; otherwise compute the demangled one. */
14140 target_physname = dw2_linkage_name (target_die, target_cu);
14141 if (target_physname == NULL)
14142 target_physname = dwarf2_physname (NULL, target_die, target_cu);
14143 if (target_physname == NULL)
14144 complaint (_("DW_AT_call_target target DIE has invalid "
14145 "physname, for referencing DIE %s [in module %s]"),
14146 sect_offset_str (die->sect_off), objfile_name (objfile));
14147 else
14148 SET_FIELD_PHYSNAME (call_site->target, target_physname);
14149 }
14150 else
14151 {
14152 CORE_ADDR lowpc;
14153
14154 /* DW_AT_entry_pc should be preferred. */
14155 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
14156 <= PC_BOUNDS_INVALID)
14157 complaint (_("DW_AT_call_target target DIE has invalid "
14158 "low pc, for referencing DIE %s [in module %s]"),
14159 sect_offset_str (die->sect_off), objfile_name (objfile));
14160 else
14161 {
14162 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14163 SET_FIELD_PHYSADDR (call_site->target, lowpc);
14164 }
14165 }
14166 }
14167 else
14168 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
14169 "block nor reference, for DIE %s [in module %s]"),
14170 sect_offset_str (die->sect_off), objfile_name (objfile));
14171
14172 call_site->per_cu = cu->per_cu;
14173
14174 for (child_die = die->child;
14175 child_die && child_die->tag;
14176 child_die = sibling_die (child_die))
14177 {
14178 struct call_site_parameter *parameter;
14179 struct attribute *loc, *origin;
14180
14181 if (child_die->tag != DW_TAG_call_site_parameter
14182 && child_die->tag != DW_TAG_GNU_call_site_parameter)
14183 {
14184 /* Already printed the complaint above. */
14185 continue;
14186 }
14187
14188 gdb_assert (call_site->parameter_count < nparams);
14189 parameter = &call_site->parameter[call_site->parameter_count];
14190
14191 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14192 specifies DW_TAG_formal_parameter. Value of the data assumed for the
14193 register is contained in DW_AT_call_value. */
14194
14195 loc = dwarf2_attr (child_die, DW_AT_location, cu);
14196 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14197 if (origin == NULL)
14198 {
14199 /* This was a pre-DWARF-5 GNU extension alias
14200 for DW_AT_call_parameter. */
14201 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14202 }
14203 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
14204 {
14205 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
14206
14207 sect_offset sect_off
14208 = (sect_offset) dwarf2_get_ref_die_offset (origin);
14209 if (!offset_in_cu_p (&cu->header, sect_off))
14210 {
14211 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14212 binding can be done only inside one CU. Such referenced DIE
14213 therefore cannot be even moved to DW_TAG_partial_unit. */
14214 complaint (_("DW_AT_call_parameter offset is not in CU for "
14215 "DW_TAG_call_site child DIE %s [in module %s]"),
14216 sect_offset_str (child_die->sect_off),
14217 objfile_name (objfile));
14218 continue;
14219 }
14220 parameter->u.param_cu_off
14221 = (cu_offset) (sect_off - cu->header.sect_off);
14222 }
14223 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
14224 {
14225 complaint (_("No DW_FORM_block* DW_AT_location for "
14226 "DW_TAG_call_site child DIE %s [in module %s]"),
14227 sect_offset_str (child_die->sect_off), objfile_name (objfile));
14228 continue;
14229 }
14230 else
14231 {
14232 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
14233 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
14234 if (parameter->u.dwarf_reg != -1)
14235 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
14236 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
14237 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
14238 &parameter->u.fb_offset))
14239 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14240 else
14241 {
14242 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
14243 "for DW_FORM_block* DW_AT_location is supported for "
14244 "DW_TAG_call_site child DIE %s "
14245 "[in module %s]"),
14246 sect_offset_str (child_die->sect_off),
14247 objfile_name (objfile));
14248 continue;
14249 }
14250 }
14251
14252 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14253 if (attr == NULL)
14254 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
14255 if (!attr_form_is_block (attr))
14256 {
14257 complaint (_("No DW_FORM_block* DW_AT_call_value for "
14258 "DW_TAG_call_site child DIE %s [in module %s]"),
14259 sect_offset_str (child_die->sect_off),
14260 objfile_name (objfile));
14261 continue;
14262 }
14263 parameter->value = DW_BLOCK (attr)->data;
14264 parameter->value_size = DW_BLOCK (attr)->size;
14265
14266 /* Parameters are not pre-cleared by memset above. */
14267 parameter->data_value = NULL;
14268 parameter->data_value_size = 0;
14269 call_site->parameter_count++;
14270
14271 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14272 if (attr == NULL)
14273 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
14274 if (attr != nullptr)
14275 {
14276 if (!attr_form_is_block (attr))
14277 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
14278 "DW_TAG_call_site child DIE %s [in module %s]"),
14279 sect_offset_str (child_die->sect_off),
14280 objfile_name (objfile));
14281 else
14282 {
14283 parameter->data_value = DW_BLOCK (attr)->data;
14284 parameter->data_value_size = DW_BLOCK (attr)->size;
14285 }
14286 }
14287 }
14288 }
14289
14290 /* Helper function for read_variable. If DIE represents a virtual
14291 table, then return the type of the concrete object that is
14292 associated with the virtual table. Otherwise, return NULL. */
14293
14294 static struct type *
14295 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14296 {
14297 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14298 if (attr == NULL)
14299 return NULL;
14300
14301 /* Find the type DIE. */
14302 struct die_info *type_die = NULL;
14303 struct dwarf2_cu *type_cu = cu;
14304
14305 if (attr_form_is_ref (attr))
14306 type_die = follow_die_ref (die, attr, &type_cu);
14307 if (type_die == NULL)
14308 return NULL;
14309
14310 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14311 return NULL;
14312 return die_containing_type (type_die, type_cu);
14313 }
14314
14315 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14316
14317 static void
14318 read_variable (struct die_info *die, struct dwarf2_cu *cu)
14319 {
14320 struct rust_vtable_symbol *storage = NULL;
14321
14322 if (cu->language == language_rust)
14323 {
14324 struct type *containing_type = rust_containing_type (die, cu);
14325
14326 if (containing_type != NULL)
14327 {
14328 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14329
14330 storage = new (&objfile->objfile_obstack) rust_vtable_symbol ();
14331 initialize_objfile_symbol (storage);
14332 storage->concrete_type = containing_type;
14333 storage->subclass = SYMBOL_RUST_VTABLE;
14334 }
14335 }
14336
14337 struct symbol *res = new_symbol (die, NULL, cu, storage);
14338 struct attribute *abstract_origin
14339 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14340 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
14341 if (res == NULL && loc && abstract_origin)
14342 {
14343 /* We have a variable without a name, but with a location and an abstract
14344 origin. This may be a concrete instance of an abstract variable
14345 referenced from an DW_OP_GNU_variable_value, so save it to find it back
14346 later. */
14347 struct dwarf2_cu *origin_cu = cu;
14348 struct die_info *origin_die
14349 = follow_die_ref (die, abstract_origin, &origin_cu);
14350 dwarf2_per_objfile *dpo = cu->per_cu->dwarf2_per_objfile;
14351 dpo->abstract_to_concrete[origin_die->sect_off].push_back (die->sect_off);
14352 }
14353 }
14354
14355 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14356 reading .debug_rnglists.
14357 Callback's type should be:
14358 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14359 Return true if the attributes are present and valid, otherwise,
14360 return false. */
14361
14362 template <typename Callback>
14363 static bool
14364 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
14365 Callback &&callback)
14366 {
14367 struct dwarf2_per_objfile *dwarf2_per_objfile
14368 = cu->per_cu->dwarf2_per_objfile;
14369 struct objfile *objfile = dwarf2_per_objfile->objfile;
14370 bfd *obfd = objfile->obfd;
14371 /* Base address selection entry. */
14372 CORE_ADDR base;
14373 int found_base;
14374 const gdb_byte *buffer;
14375 CORE_ADDR baseaddr;
14376 bool overflow = false;
14377
14378 found_base = cu->base_known;
14379 base = cu->base_address;
14380
14381 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
14382 if (offset >= dwarf2_per_objfile->rnglists.size)
14383 {
14384 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
14385 offset);
14386 return false;
14387 }
14388 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
14389
14390 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14391
14392 while (1)
14393 {
14394 /* Initialize it due to a false compiler warning. */
14395 CORE_ADDR range_beginning = 0, range_end = 0;
14396 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
14397 + dwarf2_per_objfile->rnglists.size);
14398 unsigned int bytes_read;
14399
14400 if (buffer == buf_end)
14401 {
14402 overflow = true;
14403 break;
14404 }
14405 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14406 switch (rlet)
14407 {
14408 case DW_RLE_end_of_list:
14409 break;
14410 case DW_RLE_base_address:
14411 if (buffer + cu->header.addr_size > buf_end)
14412 {
14413 overflow = true;
14414 break;
14415 }
14416 base = read_address (obfd, buffer, cu, &bytes_read);
14417 found_base = 1;
14418 buffer += bytes_read;
14419 break;
14420 case DW_RLE_start_length:
14421 if (buffer + cu->header.addr_size > buf_end)
14422 {
14423 overflow = true;
14424 break;
14425 }
14426 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14427 buffer += bytes_read;
14428 range_end = (range_beginning
14429 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14430 buffer += bytes_read;
14431 if (buffer > buf_end)
14432 {
14433 overflow = true;
14434 break;
14435 }
14436 break;
14437 case DW_RLE_offset_pair:
14438 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14439 buffer += bytes_read;
14440 if (buffer > buf_end)
14441 {
14442 overflow = true;
14443 break;
14444 }
14445 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14446 buffer += bytes_read;
14447 if (buffer > buf_end)
14448 {
14449 overflow = true;
14450 break;
14451 }
14452 break;
14453 case DW_RLE_start_end:
14454 if (buffer + 2 * cu->header.addr_size > buf_end)
14455 {
14456 overflow = true;
14457 break;
14458 }
14459 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14460 buffer += bytes_read;
14461 range_end = read_address (obfd, buffer, cu, &bytes_read);
14462 buffer += bytes_read;
14463 break;
14464 default:
14465 complaint (_("Invalid .debug_rnglists data (no base address)"));
14466 return false;
14467 }
14468 if (rlet == DW_RLE_end_of_list || overflow)
14469 break;
14470 if (rlet == DW_RLE_base_address)
14471 continue;
14472
14473 if (!found_base)
14474 {
14475 /* We have no valid base address for the ranges
14476 data. */
14477 complaint (_("Invalid .debug_rnglists data (no base address)"));
14478 return false;
14479 }
14480
14481 if (range_beginning > range_end)
14482 {
14483 /* Inverted range entries are invalid. */
14484 complaint (_("Invalid .debug_rnglists data (inverted range)"));
14485 return false;
14486 }
14487
14488 /* Empty range entries have no effect. */
14489 if (range_beginning == range_end)
14490 continue;
14491
14492 range_beginning += base;
14493 range_end += base;
14494
14495 /* A not-uncommon case of bad debug info.
14496 Don't pollute the addrmap with bad data. */
14497 if (range_beginning + baseaddr == 0
14498 && !dwarf2_per_objfile->has_section_at_zero)
14499 {
14500 complaint (_(".debug_rnglists entry has start address of zero"
14501 " [in module %s]"), objfile_name (objfile));
14502 continue;
14503 }
14504
14505 callback (range_beginning, range_end);
14506 }
14507
14508 if (overflow)
14509 {
14510 complaint (_("Offset %d is not terminated "
14511 "for DW_AT_ranges attribute"),
14512 offset);
14513 return false;
14514 }
14515
14516 return true;
14517 }
14518
14519 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14520 Callback's type should be:
14521 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14522 Return 1 if the attributes are present and valid, otherwise, return 0. */
14523
14524 template <typename Callback>
14525 static int
14526 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
14527 Callback &&callback)
14528 {
14529 struct dwarf2_per_objfile *dwarf2_per_objfile
14530 = cu->per_cu->dwarf2_per_objfile;
14531 struct objfile *objfile = dwarf2_per_objfile->objfile;
14532 struct comp_unit_head *cu_header = &cu->header;
14533 bfd *obfd = objfile->obfd;
14534 unsigned int addr_size = cu_header->addr_size;
14535 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14536 /* Base address selection entry. */
14537 CORE_ADDR base;
14538 int found_base;
14539 unsigned int dummy;
14540 const gdb_byte *buffer;
14541 CORE_ADDR baseaddr;
14542
14543 if (cu_header->version >= 5)
14544 return dwarf2_rnglists_process (offset, cu, callback);
14545
14546 found_base = cu->base_known;
14547 base = cu->base_address;
14548
14549 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
14550 if (offset >= dwarf2_per_objfile->ranges.size)
14551 {
14552 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
14553 offset);
14554 return 0;
14555 }
14556 buffer = dwarf2_per_objfile->ranges.buffer + offset;
14557
14558 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14559
14560 while (1)
14561 {
14562 CORE_ADDR range_beginning, range_end;
14563
14564 range_beginning = read_address (obfd, buffer, cu, &dummy);
14565 buffer += addr_size;
14566 range_end = read_address (obfd, buffer, cu, &dummy);
14567 buffer += addr_size;
14568 offset += 2 * addr_size;
14569
14570 /* An end of list marker is a pair of zero addresses. */
14571 if (range_beginning == 0 && range_end == 0)
14572 /* Found the end of list entry. */
14573 break;
14574
14575 /* Each base address selection entry is a pair of 2 values.
14576 The first is the largest possible address, the second is
14577 the base address. Check for a base address here. */
14578 if ((range_beginning & mask) == mask)
14579 {
14580 /* If we found the largest possible address, then we already
14581 have the base address in range_end. */
14582 base = range_end;
14583 found_base = 1;
14584 continue;
14585 }
14586
14587 if (!found_base)
14588 {
14589 /* We have no valid base address for the ranges
14590 data. */
14591 complaint (_("Invalid .debug_ranges data (no base address)"));
14592 return 0;
14593 }
14594
14595 if (range_beginning > range_end)
14596 {
14597 /* Inverted range entries are invalid. */
14598 complaint (_("Invalid .debug_ranges data (inverted range)"));
14599 return 0;
14600 }
14601
14602 /* Empty range entries have no effect. */
14603 if (range_beginning == range_end)
14604 continue;
14605
14606 range_beginning += base;
14607 range_end += base;
14608
14609 /* A not-uncommon case of bad debug info.
14610 Don't pollute the addrmap with bad data. */
14611 if (range_beginning + baseaddr == 0
14612 && !dwarf2_per_objfile->has_section_at_zero)
14613 {
14614 complaint (_(".debug_ranges entry has start address of zero"
14615 " [in module %s]"), objfile_name (objfile));
14616 continue;
14617 }
14618
14619 callback (range_beginning, range_end);
14620 }
14621
14622 return 1;
14623 }
14624
14625 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14626 Return 1 if the attributes are present and valid, otherwise, return 0.
14627 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14628
14629 static int
14630 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14631 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14632 struct partial_symtab *ranges_pst)
14633 {
14634 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14635 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14636 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
14637 SECT_OFF_TEXT (objfile));
14638 int low_set = 0;
14639 CORE_ADDR low = 0;
14640 CORE_ADDR high = 0;
14641 int retval;
14642
14643 retval = dwarf2_ranges_process (offset, cu,
14644 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14645 {
14646 if (ranges_pst != NULL)
14647 {
14648 CORE_ADDR lowpc;
14649 CORE_ADDR highpc;
14650
14651 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14652 range_beginning + baseaddr)
14653 - baseaddr);
14654 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14655 range_end + baseaddr)
14656 - baseaddr);
14657 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
14658 lowpc, highpc - 1, ranges_pst);
14659 }
14660
14661 /* FIXME: This is recording everything as a low-high
14662 segment of consecutive addresses. We should have a
14663 data structure for discontiguous block ranges
14664 instead. */
14665 if (! low_set)
14666 {
14667 low = range_beginning;
14668 high = range_end;
14669 low_set = 1;
14670 }
14671 else
14672 {
14673 if (range_beginning < low)
14674 low = range_beginning;
14675 if (range_end > high)
14676 high = range_end;
14677 }
14678 });
14679 if (!retval)
14680 return 0;
14681
14682 if (! low_set)
14683 /* If the first entry is an end-of-list marker, the range
14684 describes an empty scope, i.e. no instructions. */
14685 return 0;
14686
14687 if (low_return)
14688 *low_return = low;
14689 if (high_return)
14690 *high_return = high;
14691 return 1;
14692 }
14693
14694 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
14695 definition for the return value. *LOWPC and *HIGHPC are set iff
14696 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
14697
14698 static enum pc_bounds_kind
14699 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
14700 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14701 struct partial_symtab *pst)
14702 {
14703 struct dwarf2_per_objfile *dwarf2_per_objfile
14704 = cu->per_cu->dwarf2_per_objfile;
14705 struct attribute *attr;
14706 struct attribute *attr_high;
14707 CORE_ADDR low = 0;
14708 CORE_ADDR high = 0;
14709 enum pc_bounds_kind ret;
14710
14711 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14712 if (attr_high)
14713 {
14714 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14715 if (attr != nullptr)
14716 {
14717 low = attr_value_as_address (attr);
14718 high = attr_value_as_address (attr_high);
14719 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14720 high += low;
14721 }
14722 else
14723 /* Found high w/o low attribute. */
14724 return PC_BOUNDS_INVALID;
14725
14726 /* Found consecutive range of addresses. */
14727 ret = PC_BOUNDS_HIGH_LOW;
14728 }
14729 else
14730 {
14731 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14732 if (attr != NULL)
14733 {
14734 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14735 We take advantage of the fact that DW_AT_ranges does not appear
14736 in DW_TAG_compile_unit of DWO files. */
14737 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14738 unsigned int ranges_offset = (DW_UNSND (attr)
14739 + (need_ranges_base
14740 ? cu->ranges_base
14741 : 0));
14742
14743 /* Value of the DW_AT_ranges attribute is the offset in the
14744 .debug_ranges section. */
14745 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
14746 return PC_BOUNDS_INVALID;
14747 /* Found discontinuous range of addresses. */
14748 ret = PC_BOUNDS_RANGES;
14749 }
14750 else
14751 return PC_BOUNDS_NOT_PRESENT;
14752 }
14753
14754 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
14755 if (high <= low)
14756 return PC_BOUNDS_INVALID;
14757
14758 /* When using the GNU linker, .gnu.linkonce. sections are used to
14759 eliminate duplicate copies of functions and vtables and such.
14760 The linker will arbitrarily choose one and discard the others.
14761 The AT_*_pc values for such functions refer to local labels in
14762 these sections. If the section from that file was discarded, the
14763 labels are not in the output, so the relocs get a value of 0.
14764 If this is a discarded function, mark the pc bounds as invalid,
14765 so that GDB will ignore it. */
14766 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
14767 return PC_BOUNDS_INVALID;
14768
14769 *lowpc = low;
14770 if (highpc)
14771 *highpc = high;
14772 return ret;
14773 }
14774
14775 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
14776 its low and high PC addresses. Do nothing if these addresses could not
14777 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14778 and HIGHPC to the high address if greater than HIGHPC. */
14779
14780 static void
14781 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14782 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14783 struct dwarf2_cu *cu)
14784 {
14785 CORE_ADDR low, high;
14786 struct die_info *child = die->child;
14787
14788 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
14789 {
14790 *lowpc = std::min (*lowpc, low);
14791 *highpc = std::max (*highpc, high);
14792 }
14793
14794 /* If the language does not allow nested subprograms (either inside
14795 subprograms or lexical blocks), we're done. */
14796 if (cu->language != language_ada)
14797 return;
14798
14799 /* Check all the children of the given DIE. If it contains nested
14800 subprograms, then check their pc bounds. Likewise, we need to
14801 check lexical blocks as well, as they may also contain subprogram
14802 definitions. */
14803 while (child && child->tag)
14804 {
14805 if (child->tag == DW_TAG_subprogram
14806 || child->tag == DW_TAG_lexical_block)
14807 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14808 child = sibling_die (child);
14809 }
14810 }
14811
14812 /* Get the low and high pc's represented by the scope DIE, and store
14813 them in *LOWPC and *HIGHPC. If the correct values can't be
14814 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14815
14816 static void
14817 get_scope_pc_bounds (struct die_info *die,
14818 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14819 struct dwarf2_cu *cu)
14820 {
14821 CORE_ADDR best_low = (CORE_ADDR) -1;
14822 CORE_ADDR best_high = (CORE_ADDR) 0;
14823 CORE_ADDR current_low, current_high;
14824
14825 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
14826 >= PC_BOUNDS_RANGES)
14827 {
14828 best_low = current_low;
14829 best_high = current_high;
14830 }
14831 else
14832 {
14833 struct die_info *child = die->child;
14834
14835 while (child && child->tag)
14836 {
14837 switch (child->tag) {
14838 case DW_TAG_subprogram:
14839 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14840 break;
14841 case DW_TAG_namespace:
14842 case DW_TAG_module:
14843 /* FIXME: carlton/2004-01-16: Should we do this for
14844 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14845 that current GCC's always emit the DIEs corresponding
14846 to definitions of methods of classes as children of a
14847 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14848 the DIEs giving the declarations, which could be
14849 anywhere). But I don't see any reason why the
14850 standards says that they have to be there. */
14851 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14852
14853 if (current_low != ((CORE_ADDR) -1))
14854 {
14855 best_low = std::min (best_low, current_low);
14856 best_high = std::max (best_high, current_high);
14857 }
14858 break;
14859 default:
14860 /* Ignore. */
14861 break;
14862 }
14863
14864 child = sibling_die (child);
14865 }
14866 }
14867
14868 *lowpc = best_low;
14869 *highpc = best_high;
14870 }
14871
14872 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14873 in DIE. */
14874
14875 static void
14876 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14877 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14878 {
14879 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14880 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14881 struct attribute *attr;
14882 struct attribute *attr_high;
14883
14884 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14885 if (attr_high)
14886 {
14887 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14888 if (attr != nullptr)
14889 {
14890 CORE_ADDR low = attr_value_as_address (attr);
14891 CORE_ADDR high = attr_value_as_address (attr_high);
14892
14893 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14894 high += low;
14895
14896 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14897 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14898 cu->get_builder ()->record_block_range (block, low, high - 1);
14899 }
14900 }
14901
14902 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14903 if (attr != nullptr)
14904 {
14905 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14906 We take advantage of the fact that DW_AT_ranges does not appear
14907 in DW_TAG_compile_unit of DWO files. */
14908 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14909
14910 /* The value of the DW_AT_ranges attribute is the offset of the
14911 address range list in the .debug_ranges section. */
14912 unsigned long offset = (DW_UNSND (attr)
14913 + (need_ranges_base ? cu->ranges_base : 0));
14914
14915 std::vector<blockrange> blockvec;
14916 dwarf2_ranges_process (offset, cu,
14917 [&] (CORE_ADDR start, CORE_ADDR end)
14918 {
14919 start += baseaddr;
14920 end += baseaddr;
14921 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14922 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14923 cu->get_builder ()->record_block_range (block, start, end - 1);
14924 blockvec.emplace_back (start, end);
14925 });
14926
14927 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
14928 }
14929 }
14930
14931 /* Check whether the producer field indicates either of GCC < 4.6, or the
14932 Intel C/C++ compiler, and cache the result in CU. */
14933
14934 static void
14935 check_producer (struct dwarf2_cu *cu)
14936 {
14937 int major, minor;
14938
14939 if (cu->producer == NULL)
14940 {
14941 /* For unknown compilers expect their behavior is DWARF version
14942 compliant.
14943
14944 GCC started to support .debug_types sections by -gdwarf-4 since
14945 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14946 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14947 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14948 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14949 }
14950 else if (producer_is_gcc (cu->producer, &major, &minor))
14951 {
14952 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14953 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14954 }
14955 else if (producer_is_icc (cu->producer, &major, &minor))
14956 {
14957 cu->producer_is_icc = true;
14958 cu->producer_is_icc_lt_14 = major < 14;
14959 }
14960 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14961 cu->producer_is_codewarrior = true;
14962 else
14963 {
14964 /* For other non-GCC compilers, expect their behavior is DWARF version
14965 compliant. */
14966 }
14967
14968 cu->checked_producer = true;
14969 }
14970
14971 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14972 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14973 during 4.6.0 experimental. */
14974
14975 static bool
14976 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14977 {
14978 if (!cu->checked_producer)
14979 check_producer (cu);
14980
14981 return cu->producer_is_gxx_lt_4_6;
14982 }
14983
14984
14985 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14986 with incorrect is_stmt attributes. */
14987
14988 static bool
14989 producer_is_codewarrior (struct dwarf2_cu *cu)
14990 {
14991 if (!cu->checked_producer)
14992 check_producer (cu);
14993
14994 return cu->producer_is_codewarrior;
14995 }
14996
14997 /* Return the default accessibility type if it is not overridden by
14998 DW_AT_accessibility. */
14999
15000 static enum dwarf_access_attribute
15001 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
15002 {
15003 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
15004 {
15005 /* The default DWARF 2 accessibility for members is public, the default
15006 accessibility for inheritance is private. */
15007
15008 if (die->tag != DW_TAG_inheritance)
15009 return DW_ACCESS_public;
15010 else
15011 return DW_ACCESS_private;
15012 }
15013 else
15014 {
15015 /* DWARF 3+ defines the default accessibility a different way. The same
15016 rules apply now for DW_TAG_inheritance as for the members and it only
15017 depends on the container kind. */
15018
15019 if (die->parent->tag == DW_TAG_class_type)
15020 return DW_ACCESS_private;
15021 else
15022 return DW_ACCESS_public;
15023 }
15024 }
15025
15026 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
15027 offset. If the attribute was not found return 0, otherwise return
15028 1. If it was found but could not properly be handled, set *OFFSET
15029 to 0. */
15030
15031 static int
15032 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
15033 LONGEST *offset)
15034 {
15035 struct attribute *attr;
15036
15037 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
15038 if (attr != NULL)
15039 {
15040 *offset = 0;
15041
15042 /* Note that we do not check for a section offset first here.
15043 This is because DW_AT_data_member_location is new in DWARF 4,
15044 so if we see it, we can assume that a constant form is really
15045 a constant and not a section offset. */
15046 if (attr_form_is_constant (attr))
15047 *offset = dwarf2_get_attr_constant_value (attr, 0);
15048 else if (attr_form_is_section_offset (attr))
15049 dwarf2_complex_location_expr_complaint ();
15050 else if (attr_form_is_block (attr))
15051 *offset = decode_locdesc (DW_BLOCK (attr), cu);
15052 else
15053 dwarf2_complex_location_expr_complaint ();
15054
15055 return 1;
15056 }
15057
15058 return 0;
15059 }
15060
15061 /* Add an aggregate field to the field list. */
15062
15063 static void
15064 dwarf2_add_field (struct field_info *fip, struct die_info *die,
15065 struct dwarf2_cu *cu)
15066 {
15067 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15068 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15069 struct nextfield *new_field;
15070 struct attribute *attr;
15071 struct field *fp;
15072 const char *fieldname = "";
15073
15074 if (die->tag == DW_TAG_inheritance)
15075 {
15076 fip->baseclasses.emplace_back ();
15077 new_field = &fip->baseclasses.back ();
15078 }
15079 else
15080 {
15081 fip->fields.emplace_back ();
15082 new_field = &fip->fields.back ();
15083 }
15084
15085 fip->nfields++;
15086
15087 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15088 if (attr != nullptr)
15089 new_field->accessibility = DW_UNSND (attr);
15090 else
15091 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
15092 if (new_field->accessibility != DW_ACCESS_public)
15093 fip->non_public_fields = 1;
15094
15095 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15096 if (attr != nullptr)
15097 new_field->virtuality = DW_UNSND (attr);
15098 else
15099 new_field->virtuality = DW_VIRTUALITY_none;
15100
15101 fp = &new_field->field;
15102
15103 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
15104 {
15105 LONGEST offset;
15106
15107 /* Data member other than a C++ static data member. */
15108
15109 /* Get type of field. */
15110 fp->type = die_type (die, cu);
15111
15112 SET_FIELD_BITPOS (*fp, 0);
15113
15114 /* Get bit size of field (zero if none). */
15115 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
15116 if (attr != nullptr)
15117 {
15118 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
15119 }
15120 else
15121 {
15122 FIELD_BITSIZE (*fp) = 0;
15123 }
15124
15125 /* Get bit offset of field. */
15126 if (handle_data_member_location (die, cu, &offset))
15127 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
15128 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
15129 if (attr != nullptr)
15130 {
15131 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
15132 {
15133 /* For big endian bits, the DW_AT_bit_offset gives the
15134 additional bit offset from the MSB of the containing
15135 anonymous object to the MSB of the field. We don't
15136 have to do anything special since we don't need to
15137 know the size of the anonymous object. */
15138 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
15139 }
15140 else
15141 {
15142 /* For little endian bits, compute the bit offset to the
15143 MSB of the anonymous object, subtract off the number of
15144 bits from the MSB of the field to the MSB of the
15145 object, and then subtract off the number of bits of
15146 the field itself. The result is the bit offset of
15147 the LSB of the field. */
15148 int anonymous_size;
15149 int bit_offset = DW_UNSND (attr);
15150
15151 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15152 if (attr != nullptr)
15153 {
15154 /* The size of the anonymous object containing
15155 the bit field is explicit, so use the
15156 indicated size (in bytes). */
15157 anonymous_size = DW_UNSND (attr);
15158 }
15159 else
15160 {
15161 /* The size of the anonymous object containing
15162 the bit field must be inferred from the type
15163 attribute of the data member containing the
15164 bit field. */
15165 anonymous_size = TYPE_LENGTH (fp->type);
15166 }
15167 SET_FIELD_BITPOS (*fp,
15168 (FIELD_BITPOS (*fp)
15169 + anonymous_size * bits_per_byte
15170 - bit_offset - FIELD_BITSIZE (*fp)));
15171 }
15172 }
15173 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15174 if (attr != NULL)
15175 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15176 + dwarf2_get_attr_constant_value (attr, 0)));
15177
15178 /* Get name of field. */
15179 fieldname = dwarf2_name (die, cu);
15180 if (fieldname == NULL)
15181 fieldname = "";
15182
15183 /* The name is already allocated along with this objfile, so we don't
15184 need to duplicate it for the type. */
15185 fp->name = fieldname;
15186
15187 /* Change accessibility for artificial fields (e.g. virtual table
15188 pointer or virtual base class pointer) to private. */
15189 if (dwarf2_attr (die, DW_AT_artificial, cu))
15190 {
15191 FIELD_ARTIFICIAL (*fp) = 1;
15192 new_field->accessibility = DW_ACCESS_private;
15193 fip->non_public_fields = 1;
15194 }
15195 }
15196 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
15197 {
15198 /* C++ static member. */
15199
15200 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15201 is a declaration, but all versions of G++ as of this writing
15202 (so through at least 3.2.1) incorrectly generate
15203 DW_TAG_variable tags. */
15204
15205 const char *physname;
15206
15207 /* Get name of field. */
15208 fieldname = dwarf2_name (die, cu);
15209 if (fieldname == NULL)
15210 return;
15211
15212 attr = dwarf2_attr (die, DW_AT_const_value, cu);
15213 if (attr
15214 /* Only create a symbol if this is an external value.
15215 new_symbol checks this and puts the value in the global symbol
15216 table, which we want. If it is not external, new_symbol
15217 will try to put the value in cu->list_in_scope which is wrong. */
15218 && dwarf2_flag_true_p (die, DW_AT_external, cu))
15219 {
15220 /* A static const member, not much different than an enum as far as
15221 we're concerned, except that we can support more types. */
15222 new_symbol (die, NULL, cu);
15223 }
15224
15225 /* Get physical name. */
15226 physname = dwarf2_physname (fieldname, die, cu);
15227
15228 /* The name is already allocated along with this objfile, so we don't
15229 need to duplicate it for the type. */
15230 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
15231 FIELD_TYPE (*fp) = die_type (die, cu);
15232 FIELD_NAME (*fp) = fieldname;
15233 }
15234 else if (die->tag == DW_TAG_inheritance)
15235 {
15236 LONGEST offset;
15237
15238 /* C++ base class field. */
15239 if (handle_data_member_location (die, cu, &offset))
15240 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
15241 FIELD_BITSIZE (*fp) = 0;
15242 FIELD_TYPE (*fp) = die_type (die, cu);
15243 FIELD_NAME (*fp) = TYPE_NAME (fp->type);
15244 }
15245 else if (die->tag == DW_TAG_variant_part)
15246 {
15247 /* process_structure_scope will treat this DIE as a union. */
15248 process_structure_scope (die, cu);
15249
15250 /* The variant part is relative to the start of the enclosing
15251 structure. */
15252 SET_FIELD_BITPOS (*fp, 0);
15253 fp->type = get_die_type (die, cu);
15254 fp->artificial = 1;
15255 fp->name = "<<variant>>";
15256
15257 /* Normally a DW_TAG_variant_part won't have a size, but our
15258 representation requires one, so set it to the maximum of the
15259 child sizes, being sure to account for the offset at which
15260 each child is seen. */
15261 if (TYPE_LENGTH (fp->type) == 0)
15262 {
15263 unsigned max = 0;
15264 for (int i = 0; i < TYPE_NFIELDS (fp->type); ++i)
15265 {
15266 unsigned len = ((TYPE_FIELD_BITPOS (fp->type, i) + 7) / 8
15267 + TYPE_LENGTH (TYPE_FIELD_TYPE (fp->type, i)));
15268 if (len > max)
15269 max = len;
15270 }
15271 TYPE_LENGTH (fp->type) = max;
15272 }
15273 }
15274 else
15275 gdb_assert_not_reached ("missing case in dwarf2_add_field");
15276 }
15277
15278 /* Can the type given by DIE define another type? */
15279
15280 static bool
15281 type_can_define_types (const struct die_info *die)
15282 {
15283 switch (die->tag)
15284 {
15285 case DW_TAG_typedef:
15286 case DW_TAG_class_type:
15287 case DW_TAG_structure_type:
15288 case DW_TAG_union_type:
15289 case DW_TAG_enumeration_type:
15290 return true;
15291
15292 default:
15293 return false;
15294 }
15295 }
15296
15297 /* Add a type definition defined in the scope of the FIP's class. */
15298
15299 static void
15300 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15301 struct dwarf2_cu *cu)
15302 {
15303 struct decl_field fp;
15304 memset (&fp, 0, sizeof (fp));
15305
15306 gdb_assert (type_can_define_types (die));
15307
15308 /* Get name of field. NULL is okay here, meaning an anonymous type. */
15309 fp.name = dwarf2_name (die, cu);
15310 fp.type = read_type_die (die, cu);
15311
15312 /* Save accessibility. */
15313 enum dwarf_access_attribute accessibility;
15314 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15315 if (attr != NULL)
15316 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15317 else
15318 accessibility = dwarf2_default_access_attribute (die, cu);
15319 switch (accessibility)
15320 {
15321 case DW_ACCESS_public:
15322 /* The assumed value if neither private nor protected. */
15323 break;
15324 case DW_ACCESS_private:
15325 fp.is_private = 1;
15326 break;
15327 case DW_ACCESS_protected:
15328 fp.is_protected = 1;
15329 break;
15330 default:
15331 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
15332 }
15333
15334 if (die->tag == DW_TAG_typedef)
15335 fip->typedef_field_list.push_back (fp);
15336 else
15337 fip->nested_types_list.push_back (fp);
15338 }
15339
15340 /* Create the vector of fields, and attach it to the type. */
15341
15342 static void
15343 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
15344 struct dwarf2_cu *cu)
15345 {
15346 int nfields = fip->nfields;
15347
15348 /* Record the field count, allocate space for the array of fields,
15349 and create blank accessibility bitfields if necessary. */
15350 TYPE_NFIELDS (type) = nfields;
15351 TYPE_FIELDS (type) = (struct field *)
15352 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
15353
15354 if (fip->non_public_fields && cu->language != language_ada)
15355 {
15356 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15357
15358 TYPE_FIELD_PRIVATE_BITS (type) =
15359 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15360 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15361
15362 TYPE_FIELD_PROTECTED_BITS (type) =
15363 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15364 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15365
15366 TYPE_FIELD_IGNORE_BITS (type) =
15367 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15368 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
15369 }
15370
15371 /* If the type has baseclasses, allocate and clear a bit vector for
15372 TYPE_FIELD_VIRTUAL_BITS. */
15373 if (!fip->baseclasses.empty () && cu->language != language_ada)
15374 {
15375 int num_bytes = B_BYTES (fip->baseclasses.size ());
15376 unsigned char *pointer;
15377
15378 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15379 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
15380 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
15381 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15382 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
15383 }
15384
15385 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
15386 {
15387 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
15388
15389 for (int index = 0; index < nfields; ++index)
15390 {
15391 struct nextfield &field = fip->fields[index];
15392
15393 if (field.variant.is_discriminant)
15394 di->discriminant_index = index;
15395 else if (field.variant.default_branch)
15396 di->default_index = index;
15397 else
15398 di->discriminants[index] = field.variant.discriminant_value;
15399 }
15400 }
15401
15402 /* Copy the saved-up fields into the field vector. */
15403 for (int i = 0; i < nfields; ++i)
15404 {
15405 struct nextfield &field
15406 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15407 : fip->fields[i - fip->baseclasses.size ()]);
15408
15409 TYPE_FIELD (type, i) = field.field;
15410 switch (field.accessibility)
15411 {
15412 case DW_ACCESS_private:
15413 if (cu->language != language_ada)
15414 SET_TYPE_FIELD_PRIVATE (type, i);
15415 break;
15416
15417 case DW_ACCESS_protected:
15418 if (cu->language != language_ada)
15419 SET_TYPE_FIELD_PROTECTED (type, i);
15420 break;
15421
15422 case DW_ACCESS_public:
15423 break;
15424
15425 default:
15426 /* Unknown accessibility. Complain and treat it as public. */
15427 {
15428 complaint (_("unsupported accessibility %d"),
15429 field.accessibility);
15430 }
15431 break;
15432 }
15433 if (i < fip->baseclasses.size ())
15434 {
15435 switch (field.virtuality)
15436 {
15437 case DW_VIRTUALITY_virtual:
15438 case DW_VIRTUALITY_pure_virtual:
15439 if (cu->language == language_ada)
15440 error (_("unexpected virtuality in component of Ada type"));
15441 SET_TYPE_FIELD_VIRTUAL (type, i);
15442 break;
15443 }
15444 }
15445 }
15446 }
15447
15448 /* Return true if this member function is a constructor, false
15449 otherwise. */
15450
15451 static int
15452 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15453 {
15454 const char *fieldname;
15455 const char *type_name;
15456 int len;
15457
15458 if (die->parent == NULL)
15459 return 0;
15460
15461 if (die->parent->tag != DW_TAG_structure_type
15462 && die->parent->tag != DW_TAG_union_type
15463 && die->parent->tag != DW_TAG_class_type)
15464 return 0;
15465
15466 fieldname = dwarf2_name (die, cu);
15467 type_name = dwarf2_name (die->parent, cu);
15468 if (fieldname == NULL || type_name == NULL)
15469 return 0;
15470
15471 len = strlen (fieldname);
15472 return (strncmp (fieldname, type_name, len) == 0
15473 && (type_name[len] == '\0' || type_name[len] == '<'));
15474 }
15475
15476 /* Check if the given VALUE is a recognized enum
15477 dwarf_defaulted_attribute constant according to DWARF5 spec,
15478 Table 7.24. */
15479
15480 static bool
15481 is_valid_DW_AT_defaulted (ULONGEST value)
15482 {
15483 switch (value)
15484 {
15485 case DW_DEFAULTED_no:
15486 case DW_DEFAULTED_in_class:
15487 case DW_DEFAULTED_out_of_class:
15488 return true;
15489 }
15490
15491 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
15492 return false;
15493 }
15494
15495 /* Add a member function to the proper fieldlist. */
15496
15497 static void
15498 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
15499 struct type *type, struct dwarf2_cu *cu)
15500 {
15501 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15502 struct attribute *attr;
15503 int i;
15504 struct fnfieldlist *flp = nullptr;
15505 struct fn_field *fnp;
15506 const char *fieldname;
15507 struct type *this_type;
15508 enum dwarf_access_attribute accessibility;
15509
15510 if (cu->language == language_ada)
15511 error (_("unexpected member function in Ada type"));
15512
15513 /* Get name of member function. */
15514 fieldname = dwarf2_name (die, cu);
15515 if (fieldname == NULL)
15516 return;
15517
15518 /* Look up member function name in fieldlist. */
15519 for (i = 0; i < fip->fnfieldlists.size (); i++)
15520 {
15521 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
15522 {
15523 flp = &fip->fnfieldlists[i];
15524 break;
15525 }
15526 }
15527
15528 /* Create a new fnfieldlist if necessary. */
15529 if (flp == nullptr)
15530 {
15531 fip->fnfieldlists.emplace_back ();
15532 flp = &fip->fnfieldlists.back ();
15533 flp->name = fieldname;
15534 i = fip->fnfieldlists.size () - 1;
15535 }
15536
15537 /* Create a new member function field and add it to the vector of
15538 fnfieldlists. */
15539 flp->fnfields.emplace_back ();
15540 fnp = &flp->fnfields.back ();
15541
15542 /* Delay processing of the physname until later. */
15543 if (cu->language == language_cplus)
15544 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15545 die, cu);
15546 else
15547 {
15548 const char *physname = dwarf2_physname (fieldname, die, cu);
15549 fnp->physname = physname ? physname : "";
15550 }
15551
15552 fnp->type = alloc_type (objfile);
15553 this_type = read_type_die (die, cu);
15554 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
15555 {
15556 int nparams = TYPE_NFIELDS (this_type);
15557
15558 /* TYPE is the domain of this method, and THIS_TYPE is the type
15559 of the method itself (TYPE_CODE_METHOD). */
15560 smash_to_method_type (fnp->type, type,
15561 TYPE_TARGET_TYPE (this_type),
15562 TYPE_FIELDS (this_type),
15563 TYPE_NFIELDS (this_type),
15564 TYPE_VARARGS (this_type));
15565
15566 /* Handle static member functions.
15567 Dwarf2 has no clean way to discern C++ static and non-static
15568 member functions. G++ helps GDB by marking the first
15569 parameter for non-static member functions (which is the this
15570 pointer) as artificial. We obtain this information from
15571 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15572 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15573 fnp->voffset = VOFFSET_STATIC;
15574 }
15575 else
15576 complaint (_("member function type missing for '%s'"),
15577 dwarf2_full_name (fieldname, die, cu));
15578
15579 /* Get fcontext from DW_AT_containing_type if present. */
15580 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15581 fnp->fcontext = die_containing_type (die, cu);
15582
15583 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15584 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15585
15586 /* Get accessibility. */
15587 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15588 if (attr != nullptr)
15589 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15590 else
15591 accessibility = dwarf2_default_access_attribute (die, cu);
15592 switch (accessibility)
15593 {
15594 case DW_ACCESS_private:
15595 fnp->is_private = 1;
15596 break;
15597 case DW_ACCESS_protected:
15598 fnp->is_protected = 1;
15599 break;
15600 }
15601
15602 /* Check for artificial methods. */
15603 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15604 if (attr && DW_UNSND (attr) != 0)
15605 fnp->is_artificial = 1;
15606
15607 /* Check for defaulted methods. */
15608 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
15609 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
15610 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
15611
15612 /* Check for deleted methods. */
15613 attr = dwarf2_attr (die, DW_AT_deleted, cu);
15614 if (attr != nullptr && DW_UNSND (attr) != 0)
15615 fnp->is_deleted = 1;
15616
15617 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15618
15619 /* Get index in virtual function table if it is a virtual member
15620 function. For older versions of GCC, this is an offset in the
15621 appropriate virtual table, as specified by DW_AT_containing_type.
15622 For everyone else, it is an expression to be evaluated relative
15623 to the object address. */
15624
15625 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15626 if (attr != nullptr)
15627 {
15628 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
15629 {
15630 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15631 {
15632 /* Old-style GCC. */
15633 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15634 }
15635 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15636 || (DW_BLOCK (attr)->size > 1
15637 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15638 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15639 {
15640 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15641 if ((fnp->voffset % cu->header.addr_size) != 0)
15642 dwarf2_complex_location_expr_complaint ();
15643 else
15644 fnp->voffset /= cu->header.addr_size;
15645 fnp->voffset += 2;
15646 }
15647 else
15648 dwarf2_complex_location_expr_complaint ();
15649
15650 if (!fnp->fcontext)
15651 {
15652 /* If there is no `this' field and no DW_AT_containing_type,
15653 we cannot actually find a base class context for the
15654 vtable! */
15655 if (TYPE_NFIELDS (this_type) == 0
15656 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15657 {
15658 complaint (_("cannot determine context for virtual member "
15659 "function \"%s\" (offset %s)"),
15660 fieldname, sect_offset_str (die->sect_off));
15661 }
15662 else
15663 {
15664 fnp->fcontext
15665 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15666 }
15667 }
15668 }
15669 else if (attr_form_is_section_offset (attr))
15670 {
15671 dwarf2_complex_location_expr_complaint ();
15672 }
15673 else
15674 {
15675 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15676 fieldname);
15677 }
15678 }
15679 else
15680 {
15681 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15682 if (attr && DW_UNSND (attr))
15683 {
15684 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15685 complaint (_("Member function \"%s\" (offset %s) is virtual "
15686 "but the vtable offset is not specified"),
15687 fieldname, sect_offset_str (die->sect_off));
15688 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15689 TYPE_CPLUS_DYNAMIC (type) = 1;
15690 }
15691 }
15692 }
15693
15694 /* Create the vector of member function fields, and attach it to the type. */
15695
15696 static void
15697 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15698 struct dwarf2_cu *cu)
15699 {
15700 if (cu->language == language_ada)
15701 error (_("unexpected member functions in Ada type"));
15702
15703 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15704 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15705 TYPE_ALLOC (type,
15706 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
15707
15708 for (int i = 0; i < fip->fnfieldlists.size (); i++)
15709 {
15710 struct fnfieldlist &nf = fip->fnfieldlists[i];
15711 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15712
15713 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15714 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
15715 fn_flp->fn_fields = (struct fn_field *)
15716 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15717
15718 for (int k = 0; k < nf.fnfields.size (); ++k)
15719 fn_flp->fn_fields[k] = nf.fnfields[k];
15720 }
15721
15722 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
15723 }
15724
15725 /* Returns non-zero if NAME is the name of a vtable member in CU's
15726 language, zero otherwise. */
15727 static int
15728 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15729 {
15730 static const char vptr[] = "_vptr";
15731
15732 /* Look for the C++ form of the vtable. */
15733 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15734 return 1;
15735
15736 return 0;
15737 }
15738
15739 /* GCC outputs unnamed structures that are really pointers to member
15740 functions, with the ABI-specified layout. If TYPE describes
15741 such a structure, smash it into a member function type.
15742
15743 GCC shouldn't do this; it should just output pointer to member DIEs.
15744 This is GCC PR debug/28767. */
15745
15746 static void
15747 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15748 {
15749 struct type *pfn_type, *self_type, *new_type;
15750
15751 /* Check for a structure with no name and two children. */
15752 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
15753 return;
15754
15755 /* Check for __pfn and __delta members. */
15756 if (TYPE_FIELD_NAME (type, 0) == NULL
15757 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15758 || TYPE_FIELD_NAME (type, 1) == NULL
15759 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15760 return;
15761
15762 /* Find the type of the method. */
15763 pfn_type = TYPE_FIELD_TYPE (type, 0);
15764 if (pfn_type == NULL
15765 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
15766 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
15767 return;
15768
15769 /* Look for the "this" argument. */
15770 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15771 if (TYPE_NFIELDS (pfn_type) == 0
15772 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
15773 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
15774 return;
15775
15776 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
15777 new_type = alloc_type (objfile);
15778 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15779 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
15780 TYPE_VARARGS (pfn_type));
15781 smash_to_methodptr_type (type, new_type);
15782 }
15783
15784 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
15785 appropriate error checking and issuing complaints if there is a
15786 problem. */
15787
15788 static ULONGEST
15789 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15790 {
15791 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15792
15793 if (attr == nullptr)
15794 return 0;
15795
15796 if (!attr_form_is_constant (attr))
15797 {
15798 complaint (_("DW_AT_alignment must have constant form"
15799 " - DIE at %s [in module %s]"),
15800 sect_offset_str (die->sect_off),
15801 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15802 return 0;
15803 }
15804
15805 ULONGEST align;
15806 if (attr->form == DW_FORM_sdata)
15807 {
15808 LONGEST val = DW_SND (attr);
15809 if (val < 0)
15810 {
15811 complaint (_("DW_AT_alignment value must not be negative"
15812 " - DIE at %s [in module %s]"),
15813 sect_offset_str (die->sect_off),
15814 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15815 return 0;
15816 }
15817 align = val;
15818 }
15819 else
15820 align = DW_UNSND (attr);
15821
15822 if (align == 0)
15823 {
15824 complaint (_("DW_AT_alignment value must not be zero"
15825 " - DIE at %s [in module %s]"),
15826 sect_offset_str (die->sect_off),
15827 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15828 return 0;
15829 }
15830 if ((align & (align - 1)) != 0)
15831 {
15832 complaint (_("DW_AT_alignment value must be a power of 2"
15833 " - DIE at %s [in module %s]"),
15834 sect_offset_str (die->sect_off),
15835 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15836 return 0;
15837 }
15838
15839 return align;
15840 }
15841
15842 /* If the DIE has a DW_AT_alignment attribute, use its value to set
15843 the alignment for TYPE. */
15844
15845 static void
15846 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15847 struct type *type)
15848 {
15849 if (!set_type_align (type, get_alignment (cu, die)))
15850 complaint (_("DW_AT_alignment value too large"
15851 " - DIE at %s [in module %s]"),
15852 sect_offset_str (die->sect_off),
15853 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15854 }
15855
15856 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15857 constant for a type, according to DWARF5 spec, Table 5.5. */
15858
15859 static bool
15860 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15861 {
15862 switch (value)
15863 {
15864 case DW_CC_normal:
15865 case DW_CC_pass_by_reference:
15866 case DW_CC_pass_by_value:
15867 return true;
15868
15869 default:
15870 complaint (_("unrecognized DW_AT_calling_convention value "
15871 "(%s) for a type"), pulongest (value));
15872 return false;
15873 }
15874 }
15875
15876 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15877 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15878 also according to GNU-specific values (see include/dwarf2.h). */
15879
15880 static bool
15881 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15882 {
15883 switch (value)
15884 {
15885 case DW_CC_normal:
15886 case DW_CC_program:
15887 case DW_CC_nocall:
15888 return true;
15889
15890 case DW_CC_GNU_renesas_sh:
15891 case DW_CC_GNU_borland_fastcall_i386:
15892 case DW_CC_GDB_IBM_OpenCL:
15893 return true;
15894
15895 default:
15896 complaint (_("unrecognized DW_AT_calling_convention value "
15897 "(%s) for a subroutine"), pulongest (value));
15898 return false;
15899 }
15900 }
15901
15902 /* Called when we find the DIE that starts a structure or union scope
15903 (definition) to create a type for the structure or union. Fill in
15904 the type's name and general properties; the members will not be
15905 processed until process_structure_scope. A symbol table entry for
15906 the type will also not be done until process_structure_scope (assuming
15907 the type has a name).
15908
15909 NOTE: we need to call these functions regardless of whether or not the
15910 DIE has a DW_AT_name attribute, since it might be an anonymous
15911 structure or union. This gets the type entered into our set of
15912 user defined types. */
15913
15914 static struct type *
15915 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15916 {
15917 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15918 struct type *type;
15919 struct attribute *attr;
15920 const char *name;
15921
15922 /* If the definition of this type lives in .debug_types, read that type.
15923 Don't follow DW_AT_specification though, that will take us back up
15924 the chain and we want to go down. */
15925 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
15926 if (attr != nullptr)
15927 {
15928 type = get_DW_AT_signature_type (die, attr, cu);
15929
15930 /* The type's CU may not be the same as CU.
15931 Ensure TYPE is recorded with CU in die_type_hash. */
15932 return set_die_type (die, type, cu);
15933 }
15934
15935 type = alloc_type (objfile);
15936 INIT_CPLUS_SPECIFIC (type);
15937
15938 name = dwarf2_name (die, cu);
15939 if (name != NULL)
15940 {
15941 if (cu->language == language_cplus
15942 || cu->language == language_d
15943 || cu->language == language_rust)
15944 {
15945 const char *full_name = dwarf2_full_name (name, die, cu);
15946
15947 /* dwarf2_full_name might have already finished building the DIE's
15948 type. If so, there is no need to continue. */
15949 if (get_die_type (die, cu) != NULL)
15950 return get_die_type (die, cu);
15951
15952 TYPE_NAME (type) = full_name;
15953 }
15954 else
15955 {
15956 /* The name is already allocated along with this objfile, so
15957 we don't need to duplicate it for the type. */
15958 TYPE_NAME (type) = name;
15959 }
15960 }
15961
15962 if (die->tag == DW_TAG_structure_type)
15963 {
15964 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15965 }
15966 else if (die->tag == DW_TAG_union_type)
15967 {
15968 TYPE_CODE (type) = TYPE_CODE_UNION;
15969 }
15970 else if (die->tag == DW_TAG_variant_part)
15971 {
15972 TYPE_CODE (type) = TYPE_CODE_UNION;
15973 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
15974 }
15975 else
15976 {
15977 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15978 }
15979
15980 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15981 TYPE_DECLARED_CLASS (type) = 1;
15982
15983 /* Store the calling convention in the type if it's available in
15984 the die. Otherwise the calling convention remains set to
15985 the default value DW_CC_normal. */
15986 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15987 if (attr != nullptr
15988 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15989 {
15990 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15991 TYPE_CPLUS_CALLING_CONVENTION (type)
15992 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15993 }
15994
15995 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15996 if (attr != nullptr)
15997 {
15998 if (attr_form_is_constant (attr))
15999 TYPE_LENGTH (type) = DW_UNSND (attr);
16000 else
16001 {
16002 /* For the moment, dynamic type sizes are not supported
16003 by GDB's struct type. The actual size is determined
16004 on-demand when resolving the type of a given object,
16005 so set the type's length to zero for now. Otherwise,
16006 we record an expression as the length, and that expression
16007 could lead to a very large value, which could eventually
16008 lead to us trying to allocate that much memory when creating
16009 a value of that type. */
16010 TYPE_LENGTH (type) = 0;
16011 }
16012 }
16013 else
16014 {
16015 TYPE_LENGTH (type) = 0;
16016 }
16017
16018 maybe_set_alignment (cu, die, type);
16019
16020 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
16021 {
16022 /* ICC<14 does not output the required DW_AT_declaration on
16023 incomplete types, but gives them a size of zero. */
16024 TYPE_STUB (type) = 1;
16025 }
16026 else
16027 TYPE_STUB_SUPPORTED (type) = 1;
16028
16029 if (die_is_declaration (die, cu))
16030 TYPE_STUB (type) = 1;
16031 else if (attr == NULL && die->child == NULL
16032 && producer_is_realview (cu->producer))
16033 /* RealView does not output the required DW_AT_declaration
16034 on incomplete types. */
16035 TYPE_STUB (type) = 1;
16036
16037 /* We need to add the type field to the die immediately so we don't
16038 infinitely recurse when dealing with pointers to the structure
16039 type within the structure itself. */
16040 set_die_type (die, type, cu);
16041
16042 /* set_die_type should be already done. */
16043 set_descriptive_type (type, die, cu);
16044
16045 return type;
16046 }
16047
16048 /* A helper for process_structure_scope that handles a single member
16049 DIE. */
16050
16051 static void
16052 handle_struct_member_die (struct die_info *child_die, struct type *type,
16053 struct field_info *fi,
16054 std::vector<struct symbol *> *template_args,
16055 struct dwarf2_cu *cu)
16056 {
16057 if (child_die->tag == DW_TAG_member
16058 || child_die->tag == DW_TAG_variable
16059 || child_die->tag == DW_TAG_variant_part)
16060 {
16061 /* NOTE: carlton/2002-11-05: A C++ static data member
16062 should be a DW_TAG_member that is a declaration, but
16063 all versions of G++ as of this writing (so through at
16064 least 3.2.1) incorrectly generate DW_TAG_variable
16065 tags for them instead. */
16066 dwarf2_add_field (fi, child_die, cu);
16067 }
16068 else if (child_die->tag == DW_TAG_subprogram)
16069 {
16070 /* Rust doesn't have member functions in the C++ sense.
16071 However, it does emit ordinary functions as children
16072 of a struct DIE. */
16073 if (cu->language == language_rust)
16074 read_func_scope (child_die, cu);
16075 else
16076 {
16077 /* C++ member function. */
16078 dwarf2_add_member_fn (fi, child_die, type, cu);
16079 }
16080 }
16081 else if (child_die->tag == DW_TAG_inheritance)
16082 {
16083 /* C++ base class field. */
16084 dwarf2_add_field (fi, child_die, cu);
16085 }
16086 else if (type_can_define_types (child_die))
16087 dwarf2_add_type_defn (fi, child_die, cu);
16088 else if (child_die->tag == DW_TAG_template_type_param
16089 || child_die->tag == DW_TAG_template_value_param)
16090 {
16091 struct symbol *arg = new_symbol (child_die, NULL, cu);
16092
16093 if (arg != NULL)
16094 template_args->push_back (arg);
16095 }
16096 else if (child_die->tag == DW_TAG_variant)
16097 {
16098 /* In a variant we want to get the discriminant and also add a
16099 field for our sole member child. */
16100 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
16101
16102 for (die_info *variant_child = child_die->child;
16103 variant_child != NULL;
16104 variant_child = sibling_die (variant_child))
16105 {
16106 if (variant_child->tag == DW_TAG_member)
16107 {
16108 handle_struct_member_die (variant_child, type, fi,
16109 template_args, cu);
16110 /* Only handle the one. */
16111 break;
16112 }
16113 }
16114
16115 /* We don't handle this but we might as well report it if we see
16116 it. */
16117 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
16118 complaint (_("DW_AT_discr_list is not supported yet"
16119 " - DIE at %s [in module %s]"),
16120 sect_offset_str (child_die->sect_off),
16121 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16122
16123 /* The first field was just added, so we can stash the
16124 discriminant there. */
16125 gdb_assert (!fi->fields.empty ());
16126 if (discr == NULL)
16127 fi->fields.back ().variant.default_branch = true;
16128 else
16129 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
16130 }
16131 }
16132
16133 /* Finish creating a structure or union type, including filling in
16134 its members and creating a symbol for it. */
16135
16136 static void
16137 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
16138 {
16139 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16140 struct die_info *child_die;
16141 struct type *type;
16142
16143 type = get_die_type (die, cu);
16144 if (type == NULL)
16145 type = read_structure_type (die, cu);
16146
16147 /* When reading a DW_TAG_variant_part, we need to notice when we
16148 read the discriminant member, so we can record it later in the
16149 discriminant_info. */
16150 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
16151 sect_offset discr_offset {};
16152 bool has_template_parameters = false;
16153
16154 if (is_variant_part)
16155 {
16156 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
16157 if (discr == NULL)
16158 {
16159 /* Maybe it's a univariant form, an extension we support.
16160 In this case arrange not to check the offset. */
16161 is_variant_part = false;
16162 }
16163 else if (attr_form_is_ref (discr))
16164 {
16165 struct dwarf2_cu *target_cu = cu;
16166 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
16167
16168 discr_offset = target_die->sect_off;
16169 }
16170 else
16171 {
16172 complaint (_("DW_AT_discr does not have DIE reference form"
16173 " - DIE at %s [in module %s]"),
16174 sect_offset_str (die->sect_off),
16175 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16176 is_variant_part = false;
16177 }
16178 }
16179
16180 if (die->child != NULL && ! die_is_declaration (die, cu))
16181 {
16182 struct field_info fi;
16183 std::vector<struct symbol *> template_args;
16184
16185 child_die = die->child;
16186
16187 while (child_die && child_die->tag)
16188 {
16189 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
16190
16191 if (is_variant_part && discr_offset == child_die->sect_off)
16192 fi.fields.back ().variant.is_discriminant = true;
16193
16194 child_die = sibling_die (child_die);
16195 }
16196
16197 /* Attach template arguments to type. */
16198 if (!template_args.empty ())
16199 {
16200 has_template_parameters = true;
16201 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16202 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
16203 TYPE_TEMPLATE_ARGUMENTS (type)
16204 = XOBNEWVEC (&objfile->objfile_obstack,
16205 struct symbol *,
16206 TYPE_N_TEMPLATE_ARGUMENTS (type));
16207 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
16208 template_args.data (),
16209 (TYPE_N_TEMPLATE_ARGUMENTS (type)
16210 * sizeof (struct symbol *)));
16211 }
16212
16213 /* Attach fields and member functions to the type. */
16214 if (fi.nfields)
16215 dwarf2_attach_fields_to_type (&fi, type, cu);
16216 if (!fi.fnfieldlists.empty ())
16217 {
16218 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
16219
16220 /* Get the type which refers to the base class (possibly this
16221 class itself) which contains the vtable pointer for the current
16222 class from the DW_AT_containing_type attribute. This use of
16223 DW_AT_containing_type is a GNU extension. */
16224
16225 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
16226 {
16227 struct type *t = die_containing_type (die, cu);
16228
16229 set_type_vptr_basetype (type, t);
16230 if (type == t)
16231 {
16232 int i;
16233
16234 /* Our own class provides vtbl ptr. */
16235 for (i = TYPE_NFIELDS (t) - 1;
16236 i >= TYPE_N_BASECLASSES (t);
16237 --i)
16238 {
16239 const char *fieldname = TYPE_FIELD_NAME (t, i);
16240
16241 if (is_vtable_name (fieldname, cu))
16242 {
16243 set_type_vptr_fieldno (type, i);
16244 break;
16245 }
16246 }
16247
16248 /* Complain if virtual function table field not found. */
16249 if (i < TYPE_N_BASECLASSES (t))
16250 complaint (_("virtual function table pointer "
16251 "not found when defining class '%s'"),
16252 TYPE_NAME (type) ? TYPE_NAME (type) : "");
16253 }
16254 else
16255 {
16256 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
16257 }
16258 }
16259 else if (cu->producer
16260 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
16261 {
16262 /* The IBM XLC compiler does not provide direct indication
16263 of the containing type, but the vtable pointer is
16264 always named __vfp. */
16265
16266 int i;
16267
16268 for (i = TYPE_NFIELDS (type) - 1;
16269 i >= TYPE_N_BASECLASSES (type);
16270 --i)
16271 {
16272 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16273 {
16274 set_type_vptr_fieldno (type, i);
16275 set_type_vptr_basetype (type, type);
16276 break;
16277 }
16278 }
16279 }
16280 }
16281
16282 /* Copy fi.typedef_field_list linked list elements content into the
16283 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
16284 if (!fi.typedef_field_list.empty ())
16285 {
16286 int count = fi.typedef_field_list.size ();
16287
16288 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16289 TYPE_TYPEDEF_FIELD_ARRAY (type)
16290 = ((struct decl_field *)
16291 TYPE_ALLOC (type,
16292 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
16293 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
16294
16295 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
16296 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
16297 }
16298
16299 /* Copy fi.nested_types_list linked list elements content into the
16300 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
16301 if (!fi.nested_types_list.empty () && cu->language != language_ada)
16302 {
16303 int count = fi.nested_types_list.size ();
16304
16305 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16306 TYPE_NESTED_TYPES_ARRAY (type)
16307 = ((struct decl_field *)
16308 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
16309 TYPE_NESTED_TYPES_COUNT (type) = count;
16310
16311 for (int i = 0; i < fi.nested_types_list.size (); ++i)
16312 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
16313 }
16314 }
16315
16316 quirk_gcc_member_function_pointer (type, objfile);
16317 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
16318 cu->rust_unions.push_back (type);
16319
16320 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16321 snapshots) has been known to create a die giving a declaration
16322 for a class that has, as a child, a die giving a definition for a
16323 nested class. So we have to process our children even if the
16324 current die is a declaration. Normally, of course, a declaration
16325 won't have any children at all. */
16326
16327 child_die = die->child;
16328
16329 while (child_die != NULL && child_die->tag)
16330 {
16331 if (child_die->tag == DW_TAG_member
16332 || child_die->tag == DW_TAG_variable
16333 || child_die->tag == DW_TAG_inheritance
16334 || child_die->tag == DW_TAG_template_value_param
16335 || child_die->tag == DW_TAG_template_type_param)
16336 {
16337 /* Do nothing. */
16338 }
16339 else
16340 process_die (child_die, cu);
16341
16342 child_die = sibling_die (child_die);
16343 }
16344
16345 /* Do not consider external references. According to the DWARF standard,
16346 these DIEs are identified by the fact that they have no byte_size
16347 attribute, and a declaration attribute. */
16348 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16349 || !die_is_declaration (die, cu))
16350 {
16351 struct symbol *sym = new_symbol (die, type, cu);
16352
16353 if (has_template_parameters)
16354 {
16355 struct symtab *symtab;
16356 if (sym != nullptr)
16357 symtab = symbol_symtab (sym);
16358 else if (cu->line_header != nullptr)
16359 {
16360 /* Any related symtab will do. */
16361 symtab
16362 = cu->line_header->file_names ()[0].symtab;
16363 }
16364 else
16365 {
16366 symtab = nullptr;
16367 complaint (_("could not find suitable "
16368 "symtab for template parameter"
16369 " - DIE at %s [in module %s]"),
16370 sect_offset_str (die->sect_off),
16371 objfile_name (objfile));
16372 }
16373
16374 if (symtab != nullptr)
16375 {
16376 /* Make sure that the symtab is set on the new symbols.
16377 Even though they don't appear in this symtab directly,
16378 other parts of gdb assume that symbols do, and this is
16379 reasonably true. */
16380 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
16381 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
16382 }
16383 }
16384 }
16385 }
16386
16387 /* Assuming DIE is an enumeration type, and TYPE is its associated type,
16388 update TYPE using some information only available in DIE's children. */
16389
16390 static void
16391 update_enumeration_type_from_children (struct die_info *die,
16392 struct type *type,
16393 struct dwarf2_cu *cu)
16394 {
16395 struct die_info *child_die;
16396 int unsigned_enum = 1;
16397 int flag_enum = 1;
16398 ULONGEST mask = 0;
16399
16400 auto_obstack obstack;
16401
16402 for (child_die = die->child;
16403 child_die != NULL && child_die->tag;
16404 child_die = sibling_die (child_die))
16405 {
16406 struct attribute *attr;
16407 LONGEST value;
16408 const gdb_byte *bytes;
16409 struct dwarf2_locexpr_baton *baton;
16410 const char *name;
16411
16412 if (child_die->tag != DW_TAG_enumerator)
16413 continue;
16414
16415 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16416 if (attr == NULL)
16417 continue;
16418
16419 name = dwarf2_name (child_die, cu);
16420 if (name == NULL)
16421 name = "<anonymous enumerator>";
16422
16423 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16424 &value, &bytes, &baton);
16425 if (value < 0)
16426 {
16427 unsigned_enum = 0;
16428 flag_enum = 0;
16429 }
16430 else if ((mask & value) != 0)
16431 flag_enum = 0;
16432 else
16433 mask |= value;
16434
16435 /* If we already know that the enum type is neither unsigned, nor
16436 a flag type, no need to look at the rest of the enumerates. */
16437 if (!unsigned_enum && !flag_enum)
16438 break;
16439 }
16440
16441 if (unsigned_enum)
16442 TYPE_UNSIGNED (type) = 1;
16443 if (flag_enum)
16444 TYPE_FLAG_ENUM (type) = 1;
16445 }
16446
16447 /* Given a DW_AT_enumeration_type die, set its type. We do not
16448 complete the type's fields yet, or create any symbols. */
16449
16450 static struct type *
16451 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
16452 {
16453 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16454 struct type *type;
16455 struct attribute *attr;
16456 const char *name;
16457
16458 /* If the definition of this type lives in .debug_types, read that type.
16459 Don't follow DW_AT_specification though, that will take us back up
16460 the chain and we want to go down. */
16461 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
16462 if (attr != nullptr)
16463 {
16464 type = get_DW_AT_signature_type (die, attr, cu);
16465
16466 /* The type's CU may not be the same as CU.
16467 Ensure TYPE is recorded with CU in die_type_hash. */
16468 return set_die_type (die, type, cu);
16469 }
16470
16471 type = alloc_type (objfile);
16472
16473 TYPE_CODE (type) = TYPE_CODE_ENUM;
16474 name = dwarf2_full_name (NULL, die, cu);
16475 if (name != NULL)
16476 TYPE_NAME (type) = name;
16477
16478 attr = dwarf2_attr (die, DW_AT_type, cu);
16479 if (attr != NULL)
16480 {
16481 struct type *underlying_type = die_type (die, cu);
16482
16483 TYPE_TARGET_TYPE (type) = underlying_type;
16484 }
16485
16486 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16487 if (attr != nullptr)
16488 {
16489 TYPE_LENGTH (type) = DW_UNSND (attr);
16490 }
16491 else
16492 {
16493 TYPE_LENGTH (type) = 0;
16494 }
16495
16496 maybe_set_alignment (cu, die, type);
16497
16498 /* The enumeration DIE can be incomplete. In Ada, any type can be
16499 declared as private in the package spec, and then defined only
16500 inside the package body. Such types are known as Taft Amendment
16501 Types. When another package uses such a type, an incomplete DIE
16502 may be generated by the compiler. */
16503 if (die_is_declaration (die, cu))
16504 TYPE_STUB (type) = 1;
16505
16506 /* Finish the creation of this type by using the enum's children.
16507 We must call this even when the underlying type has been provided
16508 so that we can determine if we're looking at a "flag" enum. */
16509 update_enumeration_type_from_children (die, type, cu);
16510
16511 /* If this type has an underlying type that is not a stub, then we
16512 may use its attributes. We always use the "unsigned" attribute
16513 in this situation, because ordinarily we guess whether the type
16514 is unsigned -- but the guess can be wrong and the underlying type
16515 can tell us the reality. However, we defer to a local size
16516 attribute if one exists, because this lets the compiler override
16517 the underlying type if needed. */
16518 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16519 {
16520 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
16521 if (TYPE_LENGTH (type) == 0)
16522 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
16523 if (TYPE_RAW_ALIGN (type) == 0
16524 && TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)) != 0)
16525 set_type_align (type, TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)));
16526 }
16527
16528 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16529
16530 return set_die_type (die, type, cu);
16531 }
16532
16533 /* Given a pointer to a die which begins an enumeration, process all
16534 the dies that define the members of the enumeration, and create the
16535 symbol for the enumeration type.
16536
16537 NOTE: We reverse the order of the element list. */
16538
16539 static void
16540 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16541 {
16542 struct type *this_type;
16543
16544 this_type = get_die_type (die, cu);
16545 if (this_type == NULL)
16546 this_type = read_enumeration_type (die, cu);
16547
16548 if (die->child != NULL)
16549 {
16550 struct die_info *child_die;
16551 struct symbol *sym;
16552 struct field *fields = NULL;
16553 int num_fields = 0;
16554 const char *name;
16555
16556 child_die = die->child;
16557 while (child_die && child_die->tag)
16558 {
16559 if (child_die->tag != DW_TAG_enumerator)
16560 {
16561 process_die (child_die, cu);
16562 }
16563 else
16564 {
16565 name = dwarf2_name (child_die, cu);
16566 if (name)
16567 {
16568 sym = new_symbol (child_die, this_type, cu);
16569
16570 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
16571 {
16572 fields = (struct field *)
16573 xrealloc (fields,
16574 (num_fields + DW_FIELD_ALLOC_CHUNK)
16575 * sizeof (struct field));
16576 }
16577
16578 FIELD_NAME (fields[num_fields]) = sym->linkage_name ();
16579 FIELD_TYPE (fields[num_fields]) = NULL;
16580 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
16581 FIELD_BITSIZE (fields[num_fields]) = 0;
16582
16583 num_fields++;
16584 }
16585 }
16586
16587 child_die = sibling_die (child_die);
16588 }
16589
16590 if (num_fields)
16591 {
16592 TYPE_NFIELDS (this_type) = num_fields;
16593 TYPE_FIELDS (this_type) = (struct field *)
16594 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
16595 memcpy (TYPE_FIELDS (this_type), fields,
16596 sizeof (struct field) * num_fields);
16597 xfree (fields);
16598 }
16599 }
16600
16601 /* If we are reading an enum from a .debug_types unit, and the enum
16602 is a declaration, and the enum is not the signatured type in the
16603 unit, then we do not want to add a symbol for it. Adding a
16604 symbol would in some cases obscure the true definition of the
16605 enum, giving users an incomplete type when the definition is
16606 actually available. Note that we do not want to do this for all
16607 enums which are just declarations, because C++0x allows forward
16608 enum declarations. */
16609 if (cu->per_cu->is_debug_types
16610 && die_is_declaration (die, cu))
16611 {
16612 struct signatured_type *sig_type;
16613
16614 sig_type = (struct signatured_type *) cu->per_cu;
16615 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16616 if (sig_type->type_offset_in_section != die->sect_off)
16617 return;
16618 }
16619
16620 new_symbol (die, this_type, cu);
16621 }
16622
16623 /* Extract all information from a DW_TAG_array_type DIE and put it in
16624 the DIE's type field. For now, this only handles one dimensional
16625 arrays. */
16626
16627 static struct type *
16628 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
16629 {
16630 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16631 struct die_info *child_die;
16632 struct type *type;
16633 struct type *element_type, *range_type, *index_type;
16634 struct attribute *attr;
16635 const char *name;
16636 struct dynamic_prop *byte_stride_prop = NULL;
16637 unsigned int bit_stride = 0;
16638
16639 element_type = die_type (die, cu);
16640
16641 /* The die_type call above may have already set the type for this DIE. */
16642 type = get_die_type (die, cu);
16643 if (type)
16644 return type;
16645
16646 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16647 if (attr != NULL)
16648 {
16649 int stride_ok;
16650 struct type *prop_type
16651 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
16652
16653 byte_stride_prop
16654 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16655 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16656 prop_type);
16657 if (!stride_ok)
16658 {
16659 complaint (_("unable to read array DW_AT_byte_stride "
16660 " - DIE at %s [in module %s]"),
16661 sect_offset_str (die->sect_off),
16662 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16663 /* Ignore this attribute. We will likely not be able to print
16664 arrays of this type correctly, but there is little we can do
16665 to help if we cannot read the attribute's value. */
16666 byte_stride_prop = NULL;
16667 }
16668 }
16669
16670 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16671 if (attr != NULL)
16672 bit_stride = DW_UNSND (attr);
16673
16674 /* Irix 6.2 native cc creates array types without children for
16675 arrays with unspecified length. */
16676 if (die->child == NULL)
16677 {
16678 index_type = objfile_type (objfile)->builtin_int;
16679 range_type = create_static_range_type (NULL, index_type, 0, -1);
16680 type = create_array_type_with_stride (NULL, element_type, range_type,
16681 byte_stride_prop, bit_stride);
16682 return set_die_type (die, type, cu);
16683 }
16684
16685 std::vector<struct type *> range_types;
16686 child_die = die->child;
16687 while (child_die && child_die->tag)
16688 {
16689 if (child_die->tag == DW_TAG_subrange_type)
16690 {
16691 struct type *child_type = read_type_die (child_die, cu);
16692
16693 if (child_type != NULL)
16694 {
16695 /* The range type was succesfully read. Save it for the
16696 array type creation. */
16697 range_types.push_back (child_type);
16698 }
16699 }
16700 child_die = sibling_die (child_die);
16701 }
16702
16703 /* Dwarf2 dimensions are output from left to right, create the
16704 necessary array types in backwards order. */
16705
16706 type = element_type;
16707
16708 if (read_array_order (die, cu) == DW_ORD_col_major)
16709 {
16710 int i = 0;
16711
16712 while (i < range_types.size ())
16713 type = create_array_type_with_stride (NULL, type, range_types[i++],
16714 byte_stride_prop, bit_stride);
16715 }
16716 else
16717 {
16718 size_t ndim = range_types.size ();
16719 while (ndim-- > 0)
16720 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16721 byte_stride_prop, bit_stride);
16722 }
16723
16724 /* Understand Dwarf2 support for vector types (like they occur on
16725 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16726 array type. This is not part of the Dwarf2/3 standard yet, but a
16727 custom vendor extension. The main difference between a regular
16728 array and the vector variant is that vectors are passed by value
16729 to functions. */
16730 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
16731 if (attr != nullptr)
16732 make_vector_type (type);
16733
16734 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16735 implementation may choose to implement triple vectors using this
16736 attribute. */
16737 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16738 if (attr != nullptr)
16739 {
16740 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16741 TYPE_LENGTH (type) = DW_UNSND (attr);
16742 else
16743 complaint (_("DW_AT_byte_size for array type smaller "
16744 "than the total size of elements"));
16745 }
16746
16747 name = dwarf2_name (die, cu);
16748 if (name)
16749 TYPE_NAME (type) = name;
16750
16751 maybe_set_alignment (cu, die, type);
16752
16753 /* Install the type in the die. */
16754 set_die_type (die, type, cu);
16755
16756 /* set_die_type should be already done. */
16757 set_descriptive_type (type, die, cu);
16758
16759 return type;
16760 }
16761
16762 static enum dwarf_array_dim_ordering
16763 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
16764 {
16765 struct attribute *attr;
16766
16767 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16768
16769 if (attr != nullptr)
16770 return (enum dwarf_array_dim_ordering) DW_SND (attr);
16771
16772 /* GNU F77 is a special case, as at 08/2004 array type info is the
16773 opposite order to the dwarf2 specification, but data is still
16774 laid out as per normal fortran.
16775
16776 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16777 version checking. */
16778
16779 if (cu->language == language_fortran
16780 && cu->producer && strstr (cu->producer, "GNU F77"))
16781 {
16782 return DW_ORD_row_major;
16783 }
16784
16785 switch (cu->language_defn->la_array_ordering)
16786 {
16787 case array_column_major:
16788 return DW_ORD_col_major;
16789 case array_row_major:
16790 default:
16791 return DW_ORD_row_major;
16792 };
16793 }
16794
16795 /* Extract all information from a DW_TAG_set_type DIE and put it in
16796 the DIE's type field. */
16797
16798 static struct type *
16799 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16800 {
16801 struct type *domain_type, *set_type;
16802 struct attribute *attr;
16803
16804 domain_type = die_type (die, cu);
16805
16806 /* The die_type call above may have already set the type for this DIE. */
16807 set_type = get_die_type (die, cu);
16808 if (set_type)
16809 return set_type;
16810
16811 set_type = create_set_type (NULL, domain_type);
16812
16813 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16814 if (attr != nullptr)
16815 TYPE_LENGTH (set_type) = DW_UNSND (attr);
16816
16817 maybe_set_alignment (cu, die, set_type);
16818
16819 return set_die_type (die, set_type, cu);
16820 }
16821
16822 /* A helper for read_common_block that creates a locexpr baton.
16823 SYM is the symbol which we are marking as computed.
16824 COMMON_DIE is the DIE for the common block.
16825 COMMON_LOC is the location expression attribute for the common
16826 block itself.
16827 MEMBER_LOC is the location expression attribute for the particular
16828 member of the common block that we are processing.
16829 CU is the CU from which the above come. */
16830
16831 static void
16832 mark_common_block_symbol_computed (struct symbol *sym,
16833 struct die_info *common_die,
16834 struct attribute *common_loc,
16835 struct attribute *member_loc,
16836 struct dwarf2_cu *cu)
16837 {
16838 struct dwarf2_per_objfile *dwarf2_per_objfile
16839 = cu->per_cu->dwarf2_per_objfile;
16840 struct objfile *objfile = dwarf2_per_objfile->objfile;
16841 struct dwarf2_locexpr_baton *baton;
16842 gdb_byte *ptr;
16843 unsigned int cu_off;
16844 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
16845 LONGEST offset = 0;
16846
16847 gdb_assert (common_loc && member_loc);
16848 gdb_assert (attr_form_is_block (common_loc));
16849 gdb_assert (attr_form_is_block (member_loc)
16850 || attr_form_is_constant (member_loc));
16851
16852 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
16853 baton->per_cu = cu->per_cu;
16854 gdb_assert (baton->per_cu);
16855
16856 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16857
16858 if (attr_form_is_constant (member_loc))
16859 {
16860 offset = dwarf2_get_attr_constant_value (member_loc, 0);
16861 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16862 }
16863 else
16864 baton->size += DW_BLOCK (member_loc)->size;
16865
16866 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
16867 baton->data = ptr;
16868
16869 *ptr++ = DW_OP_call4;
16870 cu_off = common_die->sect_off - cu->per_cu->sect_off;
16871 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16872 ptr += 4;
16873
16874 if (attr_form_is_constant (member_loc))
16875 {
16876 *ptr++ = DW_OP_addr;
16877 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16878 ptr += cu->header.addr_size;
16879 }
16880 else
16881 {
16882 /* We have to copy the data here, because DW_OP_call4 will only
16883 use a DW_AT_location attribute. */
16884 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16885 ptr += DW_BLOCK (member_loc)->size;
16886 }
16887
16888 *ptr++ = DW_OP_plus;
16889 gdb_assert (ptr - baton->data == baton->size);
16890
16891 SYMBOL_LOCATION_BATON (sym) = baton;
16892 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
16893 }
16894
16895 /* Create appropriate locally-scoped variables for all the
16896 DW_TAG_common_block entries. Also create a struct common_block
16897 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16898 is used to separate the common blocks name namespace from regular
16899 variable names. */
16900
16901 static void
16902 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
16903 {
16904 struct attribute *attr;
16905
16906 attr = dwarf2_attr (die, DW_AT_location, cu);
16907 if (attr != nullptr)
16908 {
16909 /* Support the .debug_loc offsets. */
16910 if (attr_form_is_block (attr))
16911 {
16912 /* Ok. */
16913 }
16914 else if (attr_form_is_section_offset (attr))
16915 {
16916 dwarf2_complex_location_expr_complaint ();
16917 attr = NULL;
16918 }
16919 else
16920 {
16921 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16922 "common block member");
16923 attr = NULL;
16924 }
16925 }
16926
16927 if (die->child != NULL)
16928 {
16929 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16930 struct die_info *child_die;
16931 size_t n_entries = 0, size;
16932 struct common_block *common_block;
16933 struct symbol *sym;
16934
16935 for (child_die = die->child;
16936 child_die && child_die->tag;
16937 child_die = sibling_die (child_die))
16938 ++n_entries;
16939
16940 size = (sizeof (struct common_block)
16941 + (n_entries - 1) * sizeof (struct symbol *));
16942 common_block
16943 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16944 size);
16945 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16946 common_block->n_entries = 0;
16947
16948 for (child_die = die->child;
16949 child_die && child_die->tag;
16950 child_die = sibling_die (child_die))
16951 {
16952 /* Create the symbol in the DW_TAG_common_block block in the current
16953 symbol scope. */
16954 sym = new_symbol (child_die, NULL, cu);
16955 if (sym != NULL)
16956 {
16957 struct attribute *member_loc;
16958
16959 common_block->contents[common_block->n_entries++] = sym;
16960
16961 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16962 cu);
16963 if (member_loc)
16964 {
16965 /* GDB has handled this for a long time, but it is
16966 not specified by DWARF. It seems to have been
16967 emitted by gfortran at least as recently as:
16968 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16969 complaint (_("Variable in common block has "
16970 "DW_AT_data_member_location "
16971 "- DIE at %s [in module %s]"),
16972 sect_offset_str (child_die->sect_off),
16973 objfile_name (objfile));
16974
16975 if (attr_form_is_section_offset (member_loc))
16976 dwarf2_complex_location_expr_complaint ();
16977 else if (attr_form_is_constant (member_loc)
16978 || attr_form_is_block (member_loc))
16979 {
16980 if (attr != nullptr)
16981 mark_common_block_symbol_computed (sym, die, attr,
16982 member_loc, cu);
16983 }
16984 else
16985 dwarf2_complex_location_expr_complaint ();
16986 }
16987 }
16988 }
16989
16990 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16991 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
16992 }
16993 }
16994
16995 /* Create a type for a C++ namespace. */
16996
16997 static struct type *
16998 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16999 {
17000 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17001 const char *previous_prefix, *name;
17002 int is_anonymous;
17003 struct type *type;
17004
17005 /* For extensions, reuse the type of the original namespace. */
17006 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
17007 {
17008 struct die_info *ext_die;
17009 struct dwarf2_cu *ext_cu = cu;
17010
17011 ext_die = dwarf2_extension (die, &ext_cu);
17012 type = read_type_die (ext_die, ext_cu);
17013
17014 /* EXT_CU may not be the same as CU.
17015 Ensure TYPE is recorded with CU in die_type_hash. */
17016 return set_die_type (die, type, cu);
17017 }
17018
17019 name = namespace_name (die, &is_anonymous, cu);
17020
17021 /* Now build the name of the current namespace. */
17022
17023 previous_prefix = determine_prefix (die, cu);
17024 if (previous_prefix[0] != '\0')
17025 name = typename_concat (&objfile->objfile_obstack,
17026 previous_prefix, name, 0, cu);
17027
17028 /* Create the type. */
17029 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
17030
17031 return set_die_type (die, type, cu);
17032 }
17033
17034 /* Read a namespace scope. */
17035
17036 static void
17037 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
17038 {
17039 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17040 int is_anonymous;
17041
17042 /* Add a symbol associated to this if we haven't seen the namespace
17043 before. Also, add a using directive if it's an anonymous
17044 namespace. */
17045
17046 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
17047 {
17048 struct type *type;
17049
17050 type = read_type_die (die, cu);
17051 new_symbol (die, type, cu);
17052
17053 namespace_name (die, &is_anonymous, cu);
17054 if (is_anonymous)
17055 {
17056 const char *previous_prefix = determine_prefix (die, cu);
17057
17058 std::vector<const char *> excludes;
17059 add_using_directive (using_directives (cu),
17060 previous_prefix, TYPE_NAME (type), NULL,
17061 NULL, excludes, 0, &objfile->objfile_obstack);
17062 }
17063 }
17064
17065 if (die->child != NULL)
17066 {
17067 struct die_info *child_die = die->child;
17068
17069 while (child_die && child_die->tag)
17070 {
17071 process_die (child_die, cu);
17072 child_die = sibling_die (child_die);
17073 }
17074 }
17075 }
17076
17077 /* Read a Fortran module as type. This DIE can be only a declaration used for
17078 imported module. Still we need that type as local Fortran "use ... only"
17079 declaration imports depend on the created type in determine_prefix. */
17080
17081 static struct type *
17082 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
17083 {
17084 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17085 const char *module_name;
17086 struct type *type;
17087
17088 module_name = dwarf2_name (die, cu);
17089 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
17090
17091 return set_die_type (die, type, cu);
17092 }
17093
17094 /* Read a Fortran module. */
17095
17096 static void
17097 read_module (struct die_info *die, struct dwarf2_cu *cu)
17098 {
17099 struct die_info *child_die = die->child;
17100 struct type *type;
17101
17102 type = read_type_die (die, cu);
17103 new_symbol (die, type, cu);
17104
17105 while (child_die && child_die->tag)
17106 {
17107 process_die (child_die, cu);
17108 child_die = sibling_die (child_die);
17109 }
17110 }
17111
17112 /* Return the name of the namespace represented by DIE. Set
17113 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
17114 namespace. */
17115
17116 static const char *
17117 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
17118 {
17119 struct die_info *current_die;
17120 const char *name = NULL;
17121
17122 /* Loop through the extensions until we find a name. */
17123
17124 for (current_die = die;
17125 current_die != NULL;
17126 current_die = dwarf2_extension (die, &cu))
17127 {
17128 /* We don't use dwarf2_name here so that we can detect the absence
17129 of a name -> anonymous namespace. */
17130 name = dwarf2_string_attr (die, DW_AT_name, cu);
17131
17132 if (name != NULL)
17133 break;
17134 }
17135
17136 /* Is it an anonymous namespace? */
17137
17138 *is_anonymous = (name == NULL);
17139 if (*is_anonymous)
17140 name = CP_ANONYMOUS_NAMESPACE_STR;
17141
17142 return name;
17143 }
17144
17145 /* Extract all information from a DW_TAG_pointer_type DIE and add to
17146 the user defined type vector. */
17147
17148 static struct type *
17149 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
17150 {
17151 struct gdbarch *gdbarch
17152 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
17153 struct comp_unit_head *cu_header = &cu->header;
17154 struct type *type;
17155 struct attribute *attr_byte_size;
17156 struct attribute *attr_address_class;
17157 int byte_size, addr_class;
17158 struct type *target_type;
17159
17160 target_type = die_type (die, cu);
17161
17162 /* The die_type call above may have already set the type for this DIE. */
17163 type = get_die_type (die, cu);
17164 if (type)
17165 return type;
17166
17167 type = lookup_pointer_type (target_type);
17168
17169 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
17170 if (attr_byte_size)
17171 byte_size = DW_UNSND (attr_byte_size);
17172 else
17173 byte_size = cu_header->addr_size;
17174
17175 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
17176 if (attr_address_class)
17177 addr_class = DW_UNSND (attr_address_class);
17178 else
17179 addr_class = DW_ADDR_none;
17180
17181 ULONGEST alignment = get_alignment (cu, die);
17182
17183 /* If the pointer size, alignment, or address class is different
17184 than the default, create a type variant marked as such and set
17185 the length accordingly. */
17186 if (TYPE_LENGTH (type) != byte_size
17187 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
17188 && alignment != TYPE_RAW_ALIGN (type))
17189 || addr_class != DW_ADDR_none)
17190 {
17191 if (gdbarch_address_class_type_flags_p (gdbarch))
17192 {
17193 int type_flags;
17194
17195 type_flags = gdbarch_address_class_type_flags
17196 (gdbarch, byte_size, addr_class);
17197 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17198 == 0);
17199 type = make_type_with_address_space (type, type_flags);
17200 }
17201 else if (TYPE_LENGTH (type) != byte_size)
17202 {
17203 complaint (_("invalid pointer size %d"), byte_size);
17204 }
17205 else if (TYPE_RAW_ALIGN (type) != alignment)
17206 {
17207 complaint (_("Invalid DW_AT_alignment"
17208 " - DIE at %s [in module %s]"),
17209 sect_offset_str (die->sect_off),
17210 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17211 }
17212 else
17213 {
17214 /* Should we also complain about unhandled address classes? */
17215 }
17216 }
17217
17218 TYPE_LENGTH (type) = byte_size;
17219 set_type_align (type, alignment);
17220 return set_die_type (die, type, cu);
17221 }
17222
17223 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17224 the user defined type vector. */
17225
17226 static struct type *
17227 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
17228 {
17229 struct type *type;
17230 struct type *to_type;
17231 struct type *domain;
17232
17233 to_type = die_type (die, cu);
17234 domain = die_containing_type (die, cu);
17235
17236 /* The calls above may have already set the type for this DIE. */
17237 type = get_die_type (die, cu);
17238 if (type)
17239 return type;
17240
17241 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
17242 type = lookup_methodptr_type (to_type);
17243 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
17244 {
17245 struct type *new_type
17246 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
17247
17248 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
17249 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
17250 TYPE_VARARGS (to_type));
17251 type = lookup_methodptr_type (new_type);
17252 }
17253 else
17254 type = lookup_memberptr_type (to_type, domain);
17255
17256 return set_die_type (die, type, cu);
17257 }
17258
17259 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
17260 the user defined type vector. */
17261
17262 static struct type *
17263 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
17264 enum type_code refcode)
17265 {
17266 struct comp_unit_head *cu_header = &cu->header;
17267 struct type *type, *target_type;
17268 struct attribute *attr;
17269
17270 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17271
17272 target_type = die_type (die, cu);
17273
17274 /* The die_type call above may have already set the type for this DIE. */
17275 type = get_die_type (die, cu);
17276 if (type)
17277 return type;
17278
17279 type = lookup_reference_type (target_type, refcode);
17280 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17281 if (attr != nullptr)
17282 {
17283 TYPE_LENGTH (type) = DW_UNSND (attr);
17284 }
17285 else
17286 {
17287 TYPE_LENGTH (type) = cu_header->addr_size;
17288 }
17289 maybe_set_alignment (cu, die, type);
17290 return set_die_type (die, type, cu);
17291 }
17292
17293 /* Add the given cv-qualifiers to the element type of the array. GCC
17294 outputs DWARF type qualifiers that apply to an array, not the
17295 element type. But GDB relies on the array element type to carry
17296 the cv-qualifiers. This mimics section 6.7.3 of the C99
17297 specification. */
17298
17299 static struct type *
17300 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17301 struct type *base_type, int cnst, int voltl)
17302 {
17303 struct type *el_type, *inner_array;
17304
17305 base_type = copy_type (base_type);
17306 inner_array = base_type;
17307
17308 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
17309 {
17310 TYPE_TARGET_TYPE (inner_array) =
17311 copy_type (TYPE_TARGET_TYPE (inner_array));
17312 inner_array = TYPE_TARGET_TYPE (inner_array);
17313 }
17314
17315 el_type = TYPE_TARGET_TYPE (inner_array);
17316 cnst |= TYPE_CONST (el_type);
17317 voltl |= TYPE_VOLATILE (el_type);
17318 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17319
17320 return set_die_type (die, base_type, cu);
17321 }
17322
17323 static struct type *
17324 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
17325 {
17326 struct type *base_type, *cv_type;
17327
17328 base_type = die_type (die, cu);
17329
17330 /* The die_type call above may have already set the type for this DIE. */
17331 cv_type = get_die_type (die, cu);
17332 if (cv_type)
17333 return cv_type;
17334
17335 /* In case the const qualifier is applied to an array type, the element type
17336 is so qualified, not the array type (section 6.7.3 of C99). */
17337 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17338 return add_array_cv_type (die, cu, base_type, 1, 0);
17339
17340 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17341 return set_die_type (die, cv_type, cu);
17342 }
17343
17344 static struct type *
17345 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
17346 {
17347 struct type *base_type, *cv_type;
17348
17349 base_type = die_type (die, cu);
17350
17351 /* The die_type call above may have already set the type for this DIE. */
17352 cv_type = get_die_type (die, cu);
17353 if (cv_type)
17354 return cv_type;
17355
17356 /* In case the volatile qualifier is applied to an array type, the
17357 element type is so qualified, not the array type (section 6.7.3
17358 of C99). */
17359 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17360 return add_array_cv_type (die, cu, base_type, 0, 1);
17361
17362 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17363 return set_die_type (die, cv_type, cu);
17364 }
17365
17366 /* Handle DW_TAG_restrict_type. */
17367
17368 static struct type *
17369 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17370 {
17371 struct type *base_type, *cv_type;
17372
17373 base_type = die_type (die, cu);
17374
17375 /* The die_type call above may have already set the type for this DIE. */
17376 cv_type = get_die_type (die, cu);
17377 if (cv_type)
17378 return cv_type;
17379
17380 cv_type = make_restrict_type (base_type);
17381 return set_die_type (die, cv_type, cu);
17382 }
17383
17384 /* Handle DW_TAG_atomic_type. */
17385
17386 static struct type *
17387 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17388 {
17389 struct type *base_type, *cv_type;
17390
17391 base_type = die_type (die, cu);
17392
17393 /* The die_type call above may have already set the type for this DIE. */
17394 cv_type = get_die_type (die, cu);
17395 if (cv_type)
17396 return cv_type;
17397
17398 cv_type = make_atomic_type (base_type);
17399 return set_die_type (die, cv_type, cu);
17400 }
17401
17402 /* Extract all information from a DW_TAG_string_type DIE and add to
17403 the user defined type vector. It isn't really a user defined type,
17404 but it behaves like one, with other DIE's using an AT_user_def_type
17405 attribute to reference it. */
17406
17407 static struct type *
17408 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
17409 {
17410 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17411 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17412 struct type *type, *range_type, *index_type, *char_type;
17413 struct attribute *attr;
17414 struct dynamic_prop prop;
17415 bool length_is_constant = true;
17416 LONGEST length;
17417
17418 /* There are a couple of places where bit sizes might be made use of
17419 when parsing a DW_TAG_string_type, however, no producer that we know
17420 of make use of these. Handling bit sizes that are a multiple of the
17421 byte size is easy enough, but what about other bit sizes? Lets deal
17422 with that problem when we have to. Warn about these attributes being
17423 unsupported, then parse the type and ignore them like we always
17424 have. */
17425 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
17426 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
17427 {
17428 static bool warning_printed = false;
17429 if (!warning_printed)
17430 {
17431 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
17432 "currently supported on DW_TAG_string_type."));
17433 warning_printed = true;
17434 }
17435 }
17436
17437 attr = dwarf2_attr (die, DW_AT_string_length, cu);
17438 if (attr != nullptr && !attr_form_is_constant (attr))
17439 {
17440 /* The string length describes the location at which the length of
17441 the string can be found. The size of the length field can be
17442 specified with one of the attributes below. */
17443 struct type *prop_type;
17444 struct attribute *len
17445 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
17446 if (len == nullptr)
17447 len = dwarf2_attr (die, DW_AT_byte_size, cu);
17448 if (len != nullptr && attr_form_is_constant (len))
17449 {
17450 /* Pass 0 as the default as we know this attribute is constant
17451 and the default value will not be returned. */
17452 LONGEST sz = dwarf2_get_attr_constant_value (len, 0);
17453 prop_type = dwarf2_per_cu_int_type (cu->per_cu, sz, true);
17454 }
17455 else
17456 {
17457 /* If the size is not specified then we assume it is the size of
17458 an address on this target. */
17459 prop_type = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, true);
17460 }
17461
17462 /* Convert the attribute into a dynamic property. */
17463 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17464 length = 1;
17465 else
17466 length_is_constant = false;
17467 }
17468 else if (attr != nullptr)
17469 {
17470 /* This DW_AT_string_length just contains the length with no
17471 indirection. There's no need to create a dynamic property in this
17472 case. Pass 0 for the default value as we know it will not be
17473 returned in this case. */
17474 length = dwarf2_get_attr_constant_value (attr, 0);
17475 }
17476 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
17477 {
17478 /* We don't currently support non-constant byte sizes for strings. */
17479 length = dwarf2_get_attr_constant_value (attr, 1);
17480 }
17481 else
17482 {
17483 /* Use 1 as a fallback length if we have nothing else. */
17484 length = 1;
17485 }
17486
17487 index_type = objfile_type (objfile)->builtin_int;
17488 if (length_is_constant)
17489 range_type = create_static_range_type (NULL, index_type, 1, length);
17490 else
17491 {
17492 struct dynamic_prop low_bound;
17493
17494 low_bound.kind = PROP_CONST;
17495 low_bound.data.const_val = 1;
17496 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17497 }
17498 char_type = language_string_char_type (cu->language_defn, gdbarch);
17499 type = create_string_type (NULL, char_type, range_type);
17500
17501 return set_die_type (die, type, cu);
17502 }
17503
17504 /* Assuming that DIE corresponds to a function, returns nonzero
17505 if the function is prototyped. */
17506
17507 static int
17508 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17509 {
17510 struct attribute *attr;
17511
17512 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17513 if (attr && (DW_UNSND (attr) != 0))
17514 return 1;
17515
17516 /* The DWARF standard implies that the DW_AT_prototyped attribute
17517 is only meaningful for C, but the concept also extends to other
17518 languages that allow unprototyped functions (Eg: Objective C).
17519 For all other languages, assume that functions are always
17520 prototyped. */
17521 if (cu->language != language_c
17522 && cu->language != language_objc
17523 && cu->language != language_opencl)
17524 return 1;
17525
17526 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17527 prototyped and unprototyped functions; default to prototyped,
17528 since that is more common in modern code (and RealView warns
17529 about unprototyped functions). */
17530 if (producer_is_realview (cu->producer))
17531 return 1;
17532
17533 return 0;
17534 }
17535
17536 /* Handle DIES due to C code like:
17537
17538 struct foo
17539 {
17540 int (*funcp)(int a, long l);
17541 int b;
17542 };
17543
17544 ('funcp' generates a DW_TAG_subroutine_type DIE). */
17545
17546 static struct type *
17547 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
17548 {
17549 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17550 struct type *type; /* Type that this function returns. */
17551 struct type *ftype; /* Function that returns above type. */
17552 struct attribute *attr;
17553
17554 type = die_type (die, cu);
17555
17556 /* The die_type call above may have already set the type for this DIE. */
17557 ftype = get_die_type (die, cu);
17558 if (ftype)
17559 return ftype;
17560
17561 ftype = lookup_function_type (type);
17562
17563 if (prototyped_function_p (die, cu))
17564 TYPE_PROTOTYPED (ftype) = 1;
17565
17566 /* Store the calling convention in the type if it's available in
17567 the subroutine die. Otherwise set the calling convention to
17568 the default value DW_CC_normal. */
17569 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
17570 if (attr != nullptr
17571 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
17572 TYPE_CALLING_CONVENTION (ftype)
17573 = (enum dwarf_calling_convention) (DW_UNSND (attr));
17574 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17575 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17576 else
17577 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
17578
17579 /* Record whether the function returns normally to its caller or not
17580 if the DWARF producer set that information. */
17581 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17582 if (attr && (DW_UNSND (attr) != 0))
17583 TYPE_NO_RETURN (ftype) = 1;
17584
17585 /* We need to add the subroutine type to the die immediately so
17586 we don't infinitely recurse when dealing with parameters
17587 declared as the same subroutine type. */
17588 set_die_type (die, ftype, cu);
17589
17590 if (die->child != NULL)
17591 {
17592 struct type *void_type = objfile_type (objfile)->builtin_void;
17593 struct die_info *child_die;
17594 int nparams, iparams;
17595
17596 /* Count the number of parameters.
17597 FIXME: GDB currently ignores vararg functions, but knows about
17598 vararg member functions. */
17599 nparams = 0;
17600 child_die = die->child;
17601 while (child_die && child_die->tag)
17602 {
17603 if (child_die->tag == DW_TAG_formal_parameter)
17604 nparams++;
17605 else if (child_die->tag == DW_TAG_unspecified_parameters)
17606 TYPE_VARARGS (ftype) = 1;
17607 child_die = sibling_die (child_die);
17608 }
17609
17610 /* Allocate storage for parameters and fill them in. */
17611 TYPE_NFIELDS (ftype) = nparams;
17612 TYPE_FIELDS (ftype) = (struct field *)
17613 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
17614
17615 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17616 even if we error out during the parameters reading below. */
17617 for (iparams = 0; iparams < nparams; iparams++)
17618 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17619
17620 iparams = 0;
17621 child_die = die->child;
17622 while (child_die && child_die->tag)
17623 {
17624 if (child_die->tag == DW_TAG_formal_parameter)
17625 {
17626 struct type *arg_type;
17627
17628 /* DWARF version 2 has no clean way to discern C++
17629 static and non-static member functions. G++ helps
17630 GDB by marking the first parameter for non-static
17631 member functions (which is the this pointer) as
17632 artificial. We pass this information to
17633 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17634
17635 DWARF version 3 added DW_AT_object_pointer, which GCC
17636 4.5 does not yet generate. */
17637 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
17638 if (attr != nullptr)
17639 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17640 else
17641 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
17642 arg_type = die_type (child_die, cu);
17643
17644 /* RealView does not mark THIS as const, which the testsuite
17645 expects. GCC marks THIS as const in method definitions,
17646 but not in the class specifications (GCC PR 43053). */
17647 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17648 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17649 {
17650 int is_this = 0;
17651 struct dwarf2_cu *arg_cu = cu;
17652 const char *name = dwarf2_name (child_die, cu);
17653
17654 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17655 if (attr != nullptr)
17656 {
17657 /* If the compiler emits this, use it. */
17658 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17659 is_this = 1;
17660 }
17661 else if (name && strcmp (name, "this") == 0)
17662 /* Function definitions will have the argument names. */
17663 is_this = 1;
17664 else if (name == NULL && iparams == 0)
17665 /* Declarations may not have the names, so like
17666 elsewhere in GDB, assume an artificial first
17667 argument is "this". */
17668 is_this = 1;
17669
17670 if (is_this)
17671 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17672 arg_type, 0);
17673 }
17674
17675 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
17676 iparams++;
17677 }
17678 child_die = sibling_die (child_die);
17679 }
17680 }
17681
17682 return ftype;
17683 }
17684
17685 static struct type *
17686 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
17687 {
17688 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17689 const char *name = NULL;
17690 struct type *this_type, *target_type;
17691
17692 name = dwarf2_full_name (NULL, die, cu);
17693 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17694 TYPE_TARGET_STUB (this_type) = 1;
17695 set_die_type (die, this_type, cu);
17696 target_type = die_type (die, cu);
17697 if (target_type != this_type)
17698 TYPE_TARGET_TYPE (this_type) = target_type;
17699 else
17700 {
17701 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17702 spec and cause infinite loops in GDB. */
17703 complaint (_("Self-referential DW_TAG_typedef "
17704 "- DIE at %s [in module %s]"),
17705 sect_offset_str (die->sect_off), objfile_name (objfile));
17706 TYPE_TARGET_TYPE (this_type) = NULL;
17707 }
17708 return this_type;
17709 }
17710
17711 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17712 (which may be different from NAME) to the architecture back-end to allow
17713 it to guess the correct format if necessary. */
17714
17715 static struct type *
17716 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17717 const char *name_hint, enum bfd_endian byte_order)
17718 {
17719 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17720 const struct floatformat **format;
17721 struct type *type;
17722
17723 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17724 if (format)
17725 type = init_float_type (objfile, bits, name, format, byte_order);
17726 else
17727 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17728
17729 return type;
17730 }
17731
17732 /* Allocate an integer type of size BITS and name NAME. */
17733
17734 static struct type *
17735 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17736 int bits, int unsigned_p, const char *name)
17737 {
17738 struct type *type;
17739
17740 /* Versions of Intel's C Compiler generate an integer type called "void"
17741 instead of using DW_TAG_unspecified_type. This has been seen on
17742 at least versions 14, 17, and 18. */
17743 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17744 && strcmp (name, "void") == 0)
17745 type = objfile_type (objfile)->builtin_void;
17746 else
17747 type = init_integer_type (objfile, bits, unsigned_p, name);
17748
17749 return type;
17750 }
17751
17752 /* Initialise and return a floating point type of size BITS suitable for
17753 use as a component of a complex number. The NAME_HINT is passed through
17754 when initialising the floating point type and is the name of the complex
17755 type.
17756
17757 As DWARF doesn't currently provide an explicit name for the components
17758 of a complex number, but it can be helpful to have these components
17759 named, we try to select a suitable name based on the size of the
17760 component. */
17761 static struct type *
17762 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17763 struct objfile *objfile,
17764 int bits, const char *name_hint,
17765 enum bfd_endian byte_order)
17766 {
17767 gdbarch *gdbarch = get_objfile_arch (objfile);
17768 struct type *tt = nullptr;
17769
17770 /* Try to find a suitable floating point builtin type of size BITS.
17771 We're going to use the name of this type as the name for the complex
17772 target type that we are about to create. */
17773 switch (cu->language)
17774 {
17775 case language_fortran:
17776 switch (bits)
17777 {
17778 case 32:
17779 tt = builtin_f_type (gdbarch)->builtin_real;
17780 break;
17781 case 64:
17782 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17783 break;
17784 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17785 case 128:
17786 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17787 break;
17788 }
17789 break;
17790 default:
17791 switch (bits)
17792 {
17793 case 32:
17794 tt = builtin_type (gdbarch)->builtin_float;
17795 break;
17796 case 64:
17797 tt = builtin_type (gdbarch)->builtin_double;
17798 break;
17799 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17800 case 128:
17801 tt = builtin_type (gdbarch)->builtin_long_double;
17802 break;
17803 }
17804 break;
17805 }
17806
17807 /* If the type we found doesn't match the size we were looking for, then
17808 pretend we didn't find a type at all, the complex target type we
17809 create will then be nameless. */
17810 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
17811 tt = nullptr;
17812
17813 const char *name = (tt == nullptr) ? nullptr : TYPE_NAME (tt);
17814 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
17815 }
17816
17817 /* Find a representation of a given base type and install
17818 it in the TYPE field of the die. */
17819
17820 static struct type *
17821 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
17822 {
17823 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17824 struct type *type;
17825 struct attribute *attr;
17826 int encoding = 0, bits = 0;
17827 const char *name;
17828 gdbarch *arch;
17829
17830 attr = dwarf2_attr (die, DW_AT_encoding, cu);
17831 if (attr != nullptr)
17832 encoding = DW_UNSND (attr);
17833 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17834 if (attr != nullptr)
17835 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
17836 name = dwarf2_name (die, cu);
17837 if (!name)
17838 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
17839
17840 arch = get_objfile_arch (objfile);
17841 enum bfd_endian byte_order = gdbarch_byte_order (arch);
17842
17843 attr = dwarf2_attr (die, DW_AT_endianity, cu);
17844 if (attr)
17845 {
17846 int endianity = DW_UNSND (attr);
17847
17848 switch (endianity)
17849 {
17850 case DW_END_big:
17851 byte_order = BFD_ENDIAN_BIG;
17852 break;
17853 case DW_END_little:
17854 byte_order = BFD_ENDIAN_LITTLE;
17855 break;
17856 default:
17857 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
17858 break;
17859 }
17860 }
17861
17862 switch (encoding)
17863 {
17864 case DW_ATE_address:
17865 /* Turn DW_ATE_address into a void * pointer. */
17866 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
17867 type = init_pointer_type (objfile, bits, name, type);
17868 break;
17869 case DW_ATE_boolean:
17870 type = init_boolean_type (objfile, bits, 1, name);
17871 break;
17872 case DW_ATE_complex_float:
17873 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
17874 byte_order);
17875 type = init_complex_type (objfile, name, type);
17876 break;
17877 case DW_ATE_decimal_float:
17878 type = init_decfloat_type (objfile, bits, name);
17879 break;
17880 case DW_ATE_float:
17881 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
17882 break;
17883 case DW_ATE_signed:
17884 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17885 break;
17886 case DW_ATE_unsigned:
17887 if (cu->language == language_fortran
17888 && name
17889 && startswith (name, "character("))
17890 type = init_character_type (objfile, bits, 1, name);
17891 else
17892 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17893 break;
17894 case DW_ATE_signed_char:
17895 if (cu->language == language_ada || cu->language == language_m2
17896 || cu->language == language_pascal
17897 || cu->language == language_fortran)
17898 type = init_character_type (objfile, bits, 0, name);
17899 else
17900 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17901 break;
17902 case DW_ATE_unsigned_char:
17903 if (cu->language == language_ada || cu->language == language_m2
17904 || cu->language == language_pascal
17905 || cu->language == language_fortran
17906 || cu->language == language_rust)
17907 type = init_character_type (objfile, bits, 1, name);
17908 else
17909 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17910 break;
17911 case DW_ATE_UTF:
17912 {
17913 if (bits == 16)
17914 type = builtin_type (arch)->builtin_char16;
17915 else if (bits == 32)
17916 type = builtin_type (arch)->builtin_char32;
17917 else
17918 {
17919 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
17920 bits);
17921 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17922 }
17923 return set_die_type (die, type, cu);
17924 }
17925 break;
17926
17927 default:
17928 complaint (_("unsupported DW_AT_encoding: '%s'"),
17929 dwarf_type_encoding_name (encoding));
17930 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17931 break;
17932 }
17933
17934 if (name && strcmp (name, "char") == 0)
17935 TYPE_NOSIGN (type) = 1;
17936
17937 maybe_set_alignment (cu, die, type);
17938
17939 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
17940
17941 return set_die_type (die, type, cu);
17942 }
17943
17944 /* Parse dwarf attribute if it's a block, reference or constant and put the
17945 resulting value of the attribute into struct bound_prop.
17946 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17947
17948 static int
17949 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17950 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17951 struct type *default_type)
17952 {
17953 struct dwarf2_property_baton *baton;
17954 struct obstack *obstack
17955 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
17956
17957 gdb_assert (default_type != NULL);
17958
17959 if (attr == NULL || prop == NULL)
17960 return 0;
17961
17962 if (attr_form_is_block (attr))
17963 {
17964 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17965 baton->property_type = default_type;
17966 baton->locexpr.per_cu = cu->per_cu;
17967 baton->locexpr.size = DW_BLOCK (attr)->size;
17968 baton->locexpr.data = DW_BLOCK (attr)->data;
17969 switch (attr->name)
17970 {
17971 case DW_AT_string_length:
17972 baton->locexpr.is_reference = true;
17973 break;
17974 default:
17975 baton->locexpr.is_reference = false;
17976 break;
17977 }
17978 prop->data.baton = baton;
17979 prop->kind = PROP_LOCEXPR;
17980 gdb_assert (prop->data.baton != NULL);
17981 }
17982 else if (attr_form_is_ref (attr))
17983 {
17984 struct dwarf2_cu *target_cu = cu;
17985 struct die_info *target_die;
17986 struct attribute *target_attr;
17987
17988 target_die = follow_die_ref (die, attr, &target_cu);
17989 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17990 if (target_attr == NULL)
17991 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17992 target_cu);
17993 if (target_attr == NULL)
17994 return 0;
17995
17996 switch (target_attr->name)
17997 {
17998 case DW_AT_location:
17999 if (attr_form_is_section_offset (target_attr))
18000 {
18001 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18002 baton->property_type = die_type (target_die, target_cu);
18003 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
18004 prop->data.baton = baton;
18005 prop->kind = PROP_LOCLIST;
18006 gdb_assert (prop->data.baton != NULL);
18007 }
18008 else if (attr_form_is_block (target_attr))
18009 {
18010 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18011 baton->property_type = die_type (target_die, target_cu);
18012 baton->locexpr.per_cu = cu->per_cu;
18013 baton->locexpr.size = DW_BLOCK (target_attr)->size;
18014 baton->locexpr.data = DW_BLOCK (target_attr)->data;
18015 baton->locexpr.is_reference = true;
18016 prop->data.baton = baton;
18017 prop->kind = PROP_LOCEXPR;
18018 gdb_assert (prop->data.baton != NULL);
18019 }
18020 else
18021 {
18022 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18023 "dynamic property");
18024 return 0;
18025 }
18026 break;
18027 case DW_AT_data_member_location:
18028 {
18029 LONGEST offset;
18030
18031 if (!handle_data_member_location (target_die, target_cu,
18032 &offset))
18033 return 0;
18034
18035 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18036 baton->property_type = read_type_die (target_die->parent,
18037 target_cu);
18038 baton->offset_info.offset = offset;
18039 baton->offset_info.type = die_type (target_die, target_cu);
18040 prop->data.baton = baton;
18041 prop->kind = PROP_ADDR_OFFSET;
18042 break;
18043 }
18044 }
18045 }
18046 else if (attr_form_is_constant (attr))
18047 {
18048 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
18049 prop->kind = PROP_CONST;
18050 }
18051 else
18052 {
18053 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
18054 dwarf2_name (die, cu));
18055 return 0;
18056 }
18057
18058 return 1;
18059 }
18060
18061 /* Find an integer type SIZE_IN_BYTES bytes in size and return it.
18062 UNSIGNED_P controls if the integer is unsigned or not. */
18063
18064 static struct type *
18065 dwarf2_per_cu_int_type (struct dwarf2_per_cu_data *per_cu,
18066 int size_in_bytes, bool unsigned_p)
18067 {
18068 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
18069 struct type *int_type;
18070
18071 /* Helper macro to examine the various builtin types. */
18072 #define TRY_TYPE(F) \
18073 int_type = (unsigned_p \
18074 ? objfile_type (objfile)->builtin_unsigned_ ## F \
18075 : objfile_type (objfile)->builtin_ ## F); \
18076 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
18077 return int_type
18078
18079 TRY_TYPE (char);
18080 TRY_TYPE (short);
18081 TRY_TYPE (int);
18082 TRY_TYPE (long);
18083 TRY_TYPE (long_long);
18084
18085 #undef TRY_TYPE
18086
18087 gdb_assert_not_reached ("unable to find suitable integer type");
18088 }
18089
18090 /* Find an integer type the same size as the address size given in the
18091 compilation unit header for PER_CU. UNSIGNED_P controls if the integer
18092 is unsigned or not. */
18093
18094 static struct type *
18095 dwarf2_per_cu_addr_sized_int_type (struct dwarf2_per_cu_data *per_cu,
18096 bool unsigned_p)
18097 {
18098 int addr_size = dwarf2_per_cu_addr_size (per_cu);
18099 return dwarf2_per_cu_int_type (per_cu, addr_size, unsigned_p);
18100 }
18101
18102 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
18103 present (which is valid) then compute the default type based on the
18104 compilation units address size. */
18105
18106 static struct type *
18107 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
18108 {
18109 struct type *index_type = die_type (die, cu);
18110
18111 /* Dwarf-2 specifications explicitly allows to create subrange types
18112 without specifying a base type.
18113 In that case, the base type must be set to the type of
18114 the lower bound, upper bound or count, in that order, if any of these
18115 three attributes references an object that has a type.
18116 If no base type is found, the Dwarf-2 specifications say that
18117 a signed integer type of size equal to the size of an address should
18118 be used.
18119 For the following C code: `extern char gdb_int [];'
18120 GCC produces an empty range DIE.
18121 FIXME: muller/2010-05-28: Possible references to object for low bound,
18122 high bound or count are not yet handled by this code. */
18123 if (TYPE_CODE (index_type) == TYPE_CODE_VOID)
18124 index_type = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
18125
18126 return index_type;
18127 }
18128
18129 /* Read the given DW_AT_subrange DIE. */
18130
18131 static struct type *
18132 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
18133 {
18134 struct type *base_type, *orig_base_type;
18135 struct type *range_type;
18136 struct attribute *attr;
18137 struct dynamic_prop low, high;
18138 int low_default_is_valid;
18139 int high_bound_is_count = 0;
18140 const char *name;
18141 ULONGEST negative_mask;
18142
18143 orig_base_type = read_subrange_index_type (die, cu);
18144
18145 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
18146 whereas the real type might be. So, we use ORIG_BASE_TYPE when
18147 creating the range type, but we use the result of check_typedef
18148 when examining properties of the type. */
18149 base_type = check_typedef (orig_base_type);
18150
18151 /* The die_type call above may have already set the type for this DIE. */
18152 range_type = get_die_type (die, cu);
18153 if (range_type)
18154 return range_type;
18155
18156 low.kind = PROP_CONST;
18157 high.kind = PROP_CONST;
18158 high.data.const_val = 0;
18159
18160 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
18161 omitting DW_AT_lower_bound. */
18162 switch (cu->language)
18163 {
18164 case language_c:
18165 case language_cplus:
18166 low.data.const_val = 0;
18167 low_default_is_valid = 1;
18168 break;
18169 case language_fortran:
18170 low.data.const_val = 1;
18171 low_default_is_valid = 1;
18172 break;
18173 case language_d:
18174 case language_objc:
18175 case language_rust:
18176 low.data.const_val = 0;
18177 low_default_is_valid = (cu->header.version >= 4);
18178 break;
18179 case language_ada:
18180 case language_m2:
18181 case language_pascal:
18182 low.data.const_val = 1;
18183 low_default_is_valid = (cu->header.version >= 4);
18184 break;
18185 default:
18186 low.data.const_val = 0;
18187 low_default_is_valid = 0;
18188 break;
18189 }
18190
18191 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
18192 if (attr != nullptr)
18193 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
18194 else if (!low_default_is_valid)
18195 complaint (_("Missing DW_AT_lower_bound "
18196 "- DIE at %s [in module %s]"),
18197 sect_offset_str (die->sect_off),
18198 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
18199
18200 struct attribute *attr_ub, *attr_count;
18201 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
18202 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
18203 {
18204 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
18205 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
18206 {
18207 /* If bounds are constant do the final calculation here. */
18208 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
18209 high.data.const_val = low.data.const_val + high.data.const_val - 1;
18210 else
18211 high_bound_is_count = 1;
18212 }
18213 else
18214 {
18215 if (attr_ub != NULL)
18216 complaint (_("Unresolved DW_AT_upper_bound "
18217 "- DIE at %s [in module %s]"),
18218 sect_offset_str (die->sect_off),
18219 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
18220 if (attr_count != NULL)
18221 complaint (_("Unresolved DW_AT_count "
18222 "- DIE at %s [in module %s]"),
18223 sect_offset_str (die->sect_off),
18224 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
18225 }
18226 }
18227
18228 LONGEST bias = 0;
18229 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
18230 if (bias_attr != nullptr && attr_form_is_constant (bias_attr))
18231 bias = dwarf2_get_attr_constant_value (bias_attr, 0);
18232
18233 /* Normally, the DWARF producers are expected to use a signed
18234 constant form (Eg. DW_FORM_sdata) to express negative bounds.
18235 But this is unfortunately not always the case, as witnessed
18236 with GCC, for instance, where the ambiguous DW_FORM_dataN form
18237 is used instead. To work around that ambiguity, we treat
18238 the bounds as signed, and thus sign-extend their values, when
18239 the base type is signed. */
18240 negative_mask =
18241 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
18242 if (low.kind == PROP_CONST
18243 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
18244 low.data.const_val |= negative_mask;
18245 if (high.kind == PROP_CONST
18246 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
18247 high.data.const_val |= negative_mask;
18248
18249 /* Check for bit and byte strides. */
18250 struct dynamic_prop byte_stride_prop;
18251 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
18252 if (attr_byte_stride != nullptr)
18253 {
18254 struct type *prop_type
18255 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
18256 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
18257 prop_type);
18258 }
18259
18260 struct dynamic_prop bit_stride_prop;
18261 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
18262 if (attr_bit_stride != nullptr)
18263 {
18264 /* It only makes sense to have either a bit or byte stride. */
18265 if (attr_byte_stride != nullptr)
18266 {
18267 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
18268 "- DIE at %s [in module %s]"),
18269 sect_offset_str (die->sect_off),
18270 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
18271 attr_bit_stride = nullptr;
18272 }
18273 else
18274 {
18275 struct type *prop_type
18276 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
18277 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
18278 prop_type);
18279 }
18280 }
18281
18282 if (attr_byte_stride != nullptr
18283 || attr_bit_stride != nullptr)
18284 {
18285 bool byte_stride_p = (attr_byte_stride != nullptr);
18286 struct dynamic_prop *stride
18287 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
18288
18289 range_type
18290 = create_range_type_with_stride (NULL, orig_base_type, &low,
18291 &high, bias, stride, byte_stride_p);
18292 }
18293 else
18294 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
18295
18296 if (high_bound_is_count)
18297 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
18298
18299 /* Ada expects an empty array on no boundary attributes. */
18300 if (attr == NULL && cu->language != language_ada)
18301 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
18302
18303 name = dwarf2_name (die, cu);
18304 if (name)
18305 TYPE_NAME (range_type) = name;
18306
18307 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
18308 if (attr != nullptr)
18309 TYPE_LENGTH (range_type) = DW_UNSND (attr);
18310
18311 maybe_set_alignment (cu, die, range_type);
18312
18313 set_die_type (die, range_type, cu);
18314
18315 /* set_die_type should be already done. */
18316 set_descriptive_type (range_type, die, cu);
18317
18318 return range_type;
18319 }
18320
18321 static struct type *
18322 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
18323 {
18324 struct type *type;
18325
18326 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
18327 NULL);
18328 TYPE_NAME (type) = dwarf2_name (die, cu);
18329
18330 /* In Ada, an unspecified type is typically used when the description
18331 of the type is deferred to a different unit. When encountering
18332 such a type, we treat it as a stub, and try to resolve it later on,
18333 when needed. */
18334 if (cu->language == language_ada)
18335 TYPE_STUB (type) = 1;
18336
18337 return set_die_type (die, type, cu);
18338 }
18339
18340 /* Read a single die and all its descendents. Set the die's sibling
18341 field to NULL; set other fields in the die correctly, and set all
18342 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
18343 location of the info_ptr after reading all of those dies. PARENT
18344 is the parent of the die in question. */
18345
18346 static struct die_info *
18347 read_die_and_children (const struct die_reader_specs *reader,
18348 const gdb_byte *info_ptr,
18349 const gdb_byte **new_info_ptr,
18350 struct die_info *parent)
18351 {
18352 struct die_info *die;
18353 const gdb_byte *cur_ptr;
18354 int has_children;
18355
18356 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
18357 if (die == NULL)
18358 {
18359 *new_info_ptr = cur_ptr;
18360 return NULL;
18361 }
18362 store_in_ref_table (die, reader->cu);
18363
18364 if (has_children)
18365 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
18366 else
18367 {
18368 die->child = NULL;
18369 *new_info_ptr = cur_ptr;
18370 }
18371
18372 die->sibling = NULL;
18373 die->parent = parent;
18374 return die;
18375 }
18376
18377 /* Read a die, all of its descendents, and all of its siblings; set
18378 all of the fields of all of the dies correctly. Arguments are as
18379 in read_die_and_children. */
18380
18381 static struct die_info *
18382 read_die_and_siblings_1 (const struct die_reader_specs *reader,
18383 const gdb_byte *info_ptr,
18384 const gdb_byte **new_info_ptr,
18385 struct die_info *parent)
18386 {
18387 struct die_info *first_die, *last_sibling;
18388 const gdb_byte *cur_ptr;
18389
18390 cur_ptr = info_ptr;
18391 first_die = last_sibling = NULL;
18392
18393 while (1)
18394 {
18395 struct die_info *die
18396 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
18397
18398 if (die == NULL)
18399 {
18400 *new_info_ptr = cur_ptr;
18401 return first_die;
18402 }
18403
18404 if (!first_die)
18405 first_die = die;
18406 else
18407 last_sibling->sibling = die;
18408
18409 last_sibling = die;
18410 }
18411 }
18412
18413 /* Read a die, all of its descendents, and all of its siblings; set
18414 all of the fields of all of the dies correctly. Arguments are as
18415 in read_die_and_children.
18416 This the main entry point for reading a DIE and all its children. */
18417
18418 static struct die_info *
18419 read_die_and_siblings (const struct die_reader_specs *reader,
18420 const gdb_byte *info_ptr,
18421 const gdb_byte **new_info_ptr,
18422 struct die_info *parent)
18423 {
18424 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
18425 new_info_ptr, parent);
18426
18427 if (dwarf_die_debug)
18428 {
18429 fprintf_unfiltered (gdb_stdlog,
18430 "Read die from %s@0x%x of %s:\n",
18431 get_section_name (reader->die_section),
18432 (unsigned) (info_ptr - reader->die_section->buffer),
18433 bfd_get_filename (reader->abfd));
18434 dump_die (die, dwarf_die_debug);
18435 }
18436
18437 return die;
18438 }
18439
18440 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
18441 attributes.
18442 The caller is responsible for filling in the extra attributes
18443 and updating (*DIEP)->num_attrs.
18444 Set DIEP to point to a newly allocated die with its information,
18445 except for its child, sibling, and parent fields.
18446 Set HAS_CHILDREN to tell whether the die has children or not. */
18447
18448 static const gdb_byte *
18449 read_full_die_1 (const struct die_reader_specs *reader,
18450 struct die_info **diep, const gdb_byte *info_ptr,
18451 int *has_children, int num_extra_attrs)
18452 {
18453 unsigned int abbrev_number, bytes_read, i;
18454 struct abbrev_info *abbrev;
18455 struct die_info *die;
18456 struct dwarf2_cu *cu = reader->cu;
18457 bfd *abfd = reader->abfd;
18458
18459 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
18460 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18461 info_ptr += bytes_read;
18462 if (!abbrev_number)
18463 {
18464 *diep = NULL;
18465 *has_children = 0;
18466 return info_ptr;
18467 }
18468
18469 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
18470 if (!abbrev)
18471 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18472 abbrev_number,
18473 bfd_get_filename (abfd));
18474
18475 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
18476 die->sect_off = sect_off;
18477 die->tag = abbrev->tag;
18478 die->abbrev = abbrev_number;
18479
18480 /* Make the result usable.
18481 The caller needs to update num_attrs after adding the extra
18482 attributes. */
18483 die->num_attrs = abbrev->num_attrs;
18484
18485 for (i = 0; i < abbrev->num_attrs; ++i)
18486 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18487 info_ptr);
18488
18489 *diep = die;
18490 *has_children = abbrev->has_children;
18491 return info_ptr;
18492 }
18493
18494 /* Read a die and all its attributes.
18495 Set DIEP to point to a newly allocated die with its information,
18496 except for its child, sibling, and parent fields.
18497 Set HAS_CHILDREN to tell whether the die has children or not. */
18498
18499 static const gdb_byte *
18500 read_full_die (const struct die_reader_specs *reader,
18501 struct die_info **diep, const gdb_byte *info_ptr,
18502 int *has_children)
18503 {
18504 const gdb_byte *result;
18505
18506 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
18507
18508 if (dwarf_die_debug)
18509 {
18510 fprintf_unfiltered (gdb_stdlog,
18511 "Read die from %s@0x%x of %s:\n",
18512 get_section_name (reader->die_section),
18513 (unsigned) (info_ptr - reader->die_section->buffer),
18514 bfd_get_filename (reader->abfd));
18515 dump_die (*diep, dwarf_die_debug);
18516 }
18517
18518 return result;
18519 }
18520 \f
18521 /* Abbreviation tables.
18522
18523 In DWARF version 2, the description of the debugging information is
18524 stored in a separate .debug_abbrev section. Before we read any
18525 dies from a section we read in all abbreviations and install them
18526 in a hash table. */
18527
18528 /* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
18529
18530 struct abbrev_info *
18531 abbrev_table::alloc_abbrev ()
18532 {
18533 struct abbrev_info *abbrev;
18534
18535 abbrev = XOBNEW (&abbrev_obstack, struct abbrev_info);
18536 memset (abbrev, 0, sizeof (struct abbrev_info));
18537
18538 return abbrev;
18539 }
18540
18541 /* Add an abbreviation to the table. */
18542
18543 void
18544 abbrev_table::add_abbrev (unsigned int abbrev_number,
18545 struct abbrev_info *abbrev)
18546 {
18547 unsigned int hash_number;
18548
18549 hash_number = abbrev_number % ABBREV_HASH_SIZE;
18550 abbrev->next = m_abbrevs[hash_number];
18551 m_abbrevs[hash_number] = abbrev;
18552 }
18553
18554 /* Look up an abbrev in the table.
18555 Returns NULL if the abbrev is not found. */
18556
18557 struct abbrev_info *
18558 abbrev_table::lookup_abbrev (unsigned int abbrev_number)
18559 {
18560 unsigned int hash_number;
18561 struct abbrev_info *abbrev;
18562
18563 hash_number = abbrev_number % ABBREV_HASH_SIZE;
18564 abbrev = m_abbrevs[hash_number];
18565
18566 while (abbrev)
18567 {
18568 if (abbrev->number == abbrev_number)
18569 return abbrev;
18570 abbrev = abbrev->next;
18571 }
18572 return NULL;
18573 }
18574
18575 /* Read in an abbrev table. */
18576
18577 static abbrev_table_up
18578 abbrev_table_read_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
18579 struct dwarf2_section_info *section,
18580 sect_offset sect_off)
18581 {
18582 struct objfile *objfile = dwarf2_per_objfile->objfile;
18583 bfd *abfd = get_section_bfd_owner (section);
18584 const gdb_byte *abbrev_ptr;
18585 struct abbrev_info *cur_abbrev;
18586 unsigned int abbrev_number, bytes_read, abbrev_name;
18587 unsigned int abbrev_form;
18588 struct attr_abbrev *cur_attrs;
18589 unsigned int allocated_attrs;
18590
18591 abbrev_table_up abbrev_table (new struct abbrev_table (sect_off));
18592
18593 dwarf2_read_section (objfile, section);
18594 abbrev_ptr = section->buffer + to_underlying (sect_off);
18595 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18596 abbrev_ptr += bytes_read;
18597
18598 allocated_attrs = ATTR_ALLOC_CHUNK;
18599 cur_attrs = XNEWVEC (struct attr_abbrev, allocated_attrs);
18600
18601 /* Loop until we reach an abbrev number of 0. */
18602 while (abbrev_number)
18603 {
18604 cur_abbrev = abbrev_table->alloc_abbrev ();
18605
18606 /* read in abbrev header */
18607 cur_abbrev->number = abbrev_number;
18608 cur_abbrev->tag
18609 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18610 abbrev_ptr += bytes_read;
18611 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
18612 abbrev_ptr += 1;
18613
18614 /* now read in declarations */
18615 for (;;)
18616 {
18617 LONGEST implicit_const;
18618
18619 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18620 abbrev_ptr += bytes_read;
18621 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18622 abbrev_ptr += bytes_read;
18623 if (abbrev_form == DW_FORM_implicit_const)
18624 {
18625 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
18626 &bytes_read);
18627 abbrev_ptr += bytes_read;
18628 }
18629 else
18630 {
18631 /* Initialize it due to a false compiler warning. */
18632 implicit_const = -1;
18633 }
18634
18635 if (abbrev_name == 0)
18636 break;
18637
18638 if (cur_abbrev->num_attrs == allocated_attrs)
18639 {
18640 allocated_attrs += ATTR_ALLOC_CHUNK;
18641 cur_attrs
18642 = XRESIZEVEC (struct attr_abbrev, cur_attrs, allocated_attrs);
18643 }
18644
18645 cur_attrs[cur_abbrev->num_attrs].name
18646 = (enum dwarf_attribute) abbrev_name;
18647 cur_attrs[cur_abbrev->num_attrs].form
18648 = (enum dwarf_form) abbrev_form;
18649 cur_attrs[cur_abbrev->num_attrs].implicit_const = implicit_const;
18650 ++cur_abbrev->num_attrs;
18651 }
18652
18653 cur_abbrev->attrs =
18654 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
18655 cur_abbrev->num_attrs);
18656 memcpy (cur_abbrev->attrs, cur_attrs,
18657 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
18658
18659 abbrev_table->add_abbrev (abbrev_number, cur_abbrev);
18660
18661 /* Get next abbreviation.
18662 Under Irix6 the abbreviations for a compilation unit are not
18663 always properly terminated with an abbrev number of 0.
18664 Exit loop if we encounter an abbreviation which we have
18665 already read (which means we are about to read the abbreviations
18666 for the next compile unit) or if the end of the abbreviation
18667 table is reached. */
18668 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
18669 break;
18670 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18671 abbrev_ptr += bytes_read;
18672 if (abbrev_table->lookup_abbrev (abbrev_number) != NULL)
18673 break;
18674 }
18675
18676 xfree (cur_attrs);
18677 return abbrev_table;
18678 }
18679
18680 /* Returns nonzero if TAG represents a type that we might generate a partial
18681 symbol for. */
18682
18683 static int
18684 is_type_tag_for_partial (int tag)
18685 {
18686 switch (tag)
18687 {
18688 #if 0
18689 /* Some types that would be reasonable to generate partial symbols for,
18690 that we don't at present. */
18691 case DW_TAG_array_type:
18692 case DW_TAG_file_type:
18693 case DW_TAG_ptr_to_member_type:
18694 case DW_TAG_set_type:
18695 case DW_TAG_string_type:
18696 case DW_TAG_subroutine_type:
18697 #endif
18698 case DW_TAG_base_type:
18699 case DW_TAG_class_type:
18700 case DW_TAG_interface_type:
18701 case DW_TAG_enumeration_type:
18702 case DW_TAG_structure_type:
18703 case DW_TAG_subrange_type:
18704 case DW_TAG_typedef:
18705 case DW_TAG_union_type:
18706 return 1;
18707 default:
18708 return 0;
18709 }
18710 }
18711
18712 /* Load all DIEs that are interesting for partial symbols into memory. */
18713
18714 static struct partial_die_info *
18715 load_partial_dies (const struct die_reader_specs *reader,
18716 const gdb_byte *info_ptr, int building_psymtab)
18717 {
18718 struct dwarf2_cu *cu = reader->cu;
18719 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18720 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
18721 unsigned int bytes_read;
18722 unsigned int load_all = 0;
18723 int nesting_level = 1;
18724
18725 parent_die = NULL;
18726 last_die = NULL;
18727
18728 gdb_assert (cu->per_cu != NULL);
18729 if (cu->per_cu->load_all_dies)
18730 load_all = 1;
18731
18732 cu->partial_dies
18733 = htab_create_alloc_ex (cu->header.length / 12,
18734 partial_die_hash,
18735 partial_die_eq,
18736 NULL,
18737 &cu->comp_unit_obstack,
18738 hashtab_obstack_allocate,
18739 dummy_obstack_deallocate);
18740
18741 while (1)
18742 {
18743 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
18744
18745 /* A NULL abbrev means the end of a series of children. */
18746 if (abbrev == NULL)
18747 {
18748 if (--nesting_level == 0)
18749 return first_die;
18750
18751 info_ptr += bytes_read;
18752 last_die = parent_die;
18753 parent_die = parent_die->die_parent;
18754 continue;
18755 }
18756
18757 /* Check for template arguments. We never save these; if
18758 they're seen, we just mark the parent, and go on our way. */
18759 if (parent_die != NULL
18760 && cu->language == language_cplus
18761 && (abbrev->tag == DW_TAG_template_type_param
18762 || abbrev->tag == DW_TAG_template_value_param))
18763 {
18764 parent_die->has_template_arguments = 1;
18765
18766 if (!load_all)
18767 {
18768 /* We don't need a partial DIE for the template argument. */
18769 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18770 continue;
18771 }
18772 }
18773
18774 /* We only recurse into c++ subprograms looking for template arguments.
18775 Skip their other children. */
18776 if (!load_all
18777 && cu->language == language_cplus
18778 && parent_die != NULL
18779 && parent_die->tag == DW_TAG_subprogram)
18780 {
18781 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18782 continue;
18783 }
18784
18785 /* Check whether this DIE is interesting enough to save. Normally
18786 we would not be interested in members here, but there may be
18787 later variables referencing them via DW_AT_specification (for
18788 static members). */
18789 if (!load_all
18790 && !is_type_tag_for_partial (abbrev->tag)
18791 && abbrev->tag != DW_TAG_constant
18792 && abbrev->tag != DW_TAG_enumerator
18793 && abbrev->tag != DW_TAG_subprogram
18794 && abbrev->tag != DW_TAG_inlined_subroutine
18795 && abbrev->tag != DW_TAG_lexical_block
18796 && abbrev->tag != DW_TAG_variable
18797 && abbrev->tag != DW_TAG_namespace
18798 && abbrev->tag != DW_TAG_module
18799 && abbrev->tag != DW_TAG_member
18800 && abbrev->tag != DW_TAG_imported_unit
18801 && abbrev->tag != DW_TAG_imported_declaration)
18802 {
18803 /* Otherwise we skip to the next sibling, if any. */
18804 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18805 continue;
18806 }
18807
18808 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18809 abbrev);
18810
18811 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
18812
18813 /* This two-pass algorithm for processing partial symbols has a
18814 high cost in cache pressure. Thus, handle some simple cases
18815 here which cover the majority of C partial symbols. DIEs
18816 which neither have specification tags in them, nor could have
18817 specification tags elsewhere pointing at them, can simply be
18818 processed and discarded.
18819
18820 This segment is also optional; scan_partial_symbols and
18821 add_partial_symbol will handle these DIEs if we chain
18822 them in normally. When compilers which do not emit large
18823 quantities of duplicate debug information are more common,
18824 this code can probably be removed. */
18825
18826 /* Any complete simple types at the top level (pretty much all
18827 of them, for a language without namespaces), can be processed
18828 directly. */
18829 if (parent_die == NULL
18830 && pdi.has_specification == 0
18831 && pdi.is_declaration == 0
18832 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18833 || pdi.tag == DW_TAG_base_type
18834 || pdi.tag == DW_TAG_subrange_type))
18835 {
18836 if (building_psymtab && pdi.name != NULL)
18837 add_psymbol_to_list (pdi.name, false,
18838 VAR_DOMAIN, LOC_TYPEDEF, -1,
18839 psymbol_placement::STATIC,
18840 0, cu->language, objfile);
18841 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18842 continue;
18843 }
18844
18845 /* The exception for DW_TAG_typedef with has_children above is
18846 a workaround of GCC PR debug/47510. In the case of this complaint
18847 type_name_or_error will error on such types later.
18848
18849 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18850 it could not find the child DIEs referenced later, this is checked
18851 above. In correct DWARF DW_TAG_typedef should have no children. */
18852
18853 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
18854 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
18855 "- DIE at %s [in module %s]"),
18856 sect_offset_str (pdi.sect_off), objfile_name (objfile));
18857
18858 /* If we're at the second level, and we're an enumerator, and
18859 our parent has no specification (meaning possibly lives in a
18860 namespace elsewhere), then we can add the partial symbol now
18861 instead of queueing it. */
18862 if (pdi.tag == DW_TAG_enumerator
18863 && parent_die != NULL
18864 && parent_die->die_parent == NULL
18865 && parent_die->tag == DW_TAG_enumeration_type
18866 && parent_die->has_specification == 0)
18867 {
18868 if (pdi.name == NULL)
18869 complaint (_("malformed enumerator DIE ignored"));
18870 else if (building_psymtab)
18871 add_psymbol_to_list (pdi.name, false,
18872 VAR_DOMAIN, LOC_CONST, -1,
18873 cu->language == language_cplus
18874 ? psymbol_placement::GLOBAL
18875 : psymbol_placement::STATIC,
18876 0, cu->language, objfile);
18877
18878 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18879 continue;
18880 }
18881
18882 struct partial_die_info *part_die
18883 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
18884
18885 /* We'll save this DIE so link it in. */
18886 part_die->die_parent = parent_die;
18887 part_die->die_sibling = NULL;
18888 part_die->die_child = NULL;
18889
18890 if (last_die && last_die == parent_die)
18891 last_die->die_child = part_die;
18892 else if (last_die)
18893 last_die->die_sibling = part_die;
18894
18895 last_die = part_die;
18896
18897 if (first_die == NULL)
18898 first_die = part_die;
18899
18900 /* Maybe add the DIE to the hash table. Not all DIEs that we
18901 find interesting need to be in the hash table, because we
18902 also have the parent/sibling/child chains; only those that we
18903 might refer to by offset later during partial symbol reading.
18904
18905 For now this means things that might have be the target of a
18906 DW_AT_specification, DW_AT_abstract_origin, or
18907 DW_AT_extension. DW_AT_extension will refer only to
18908 namespaces; DW_AT_abstract_origin refers to functions (and
18909 many things under the function DIE, but we do not recurse
18910 into function DIEs during partial symbol reading) and
18911 possibly variables as well; DW_AT_specification refers to
18912 declarations. Declarations ought to have the DW_AT_declaration
18913 flag. It happens that GCC forgets to put it in sometimes, but
18914 only for functions, not for types.
18915
18916 Adding more things than necessary to the hash table is harmless
18917 except for the performance cost. Adding too few will result in
18918 wasted time in find_partial_die, when we reread the compilation
18919 unit with load_all_dies set. */
18920
18921 if (load_all
18922 || abbrev->tag == DW_TAG_constant
18923 || abbrev->tag == DW_TAG_subprogram
18924 || abbrev->tag == DW_TAG_variable
18925 || abbrev->tag == DW_TAG_namespace
18926 || part_die->is_declaration)
18927 {
18928 void **slot;
18929
18930 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
18931 to_underlying (part_die->sect_off),
18932 INSERT);
18933 *slot = part_die;
18934 }
18935
18936 /* For some DIEs we want to follow their children (if any). For C
18937 we have no reason to follow the children of structures; for other
18938 languages we have to, so that we can get at method physnames
18939 to infer fully qualified class names, for DW_AT_specification,
18940 and for C++ template arguments. For C++, we also look one level
18941 inside functions to find template arguments (if the name of the
18942 function does not already contain the template arguments).
18943
18944 For Ada and Fortran, we need to scan the children of subprograms
18945 and lexical blocks as well because these languages allow the
18946 definition of nested entities that could be interesting for the
18947 debugger, such as nested subprograms for instance. */
18948 if (last_die->has_children
18949 && (load_all
18950 || last_die->tag == DW_TAG_namespace
18951 || last_die->tag == DW_TAG_module
18952 || last_die->tag == DW_TAG_enumeration_type
18953 || (cu->language == language_cplus
18954 && last_die->tag == DW_TAG_subprogram
18955 && (last_die->name == NULL
18956 || strchr (last_die->name, '<') == NULL))
18957 || (cu->language != language_c
18958 && (last_die->tag == DW_TAG_class_type
18959 || last_die->tag == DW_TAG_interface_type
18960 || last_die->tag == DW_TAG_structure_type
18961 || last_die->tag == DW_TAG_union_type))
18962 || ((cu->language == language_ada
18963 || cu->language == language_fortran)
18964 && (last_die->tag == DW_TAG_subprogram
18965 || last_die->tag == DW_TAG_lexical_block))))
18966 {
18967 nesting_level++;
18968 parent_die = last_die;
18969 continue;
18970 }
18971
18972 /* Otherwise we skip to the next sibling, if any. */
18973 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
18974
18975 /* Back to the top, do it again. */
18976 }
18977 }
18978
18979 partial_die_info::partial_die_info (sect_offset sect_off_,
18980 struct abbrev_info *abbrev)
18981 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18982 {
18983 }
18984
18985 /* Read a minimal amount of information into the minimal die structure.
18986 INFO_PTR should point just after the initial uleb128 of a DIE. */
18987
18988 const gdb_byte *
18989 partial_die_info::read (const struct die_reader_specs *reader,
18990 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
18991 {
18992 struct dwarf2_cu *cu = reader->cu;
18993 struct dwarf2_per_objfile *dwarf2_per_objfile
18994 = cu->per_cu->dwarf2_per_objfile;
18995 unsigned int i;
18996 int has_low_pc_attr = 0;
18997 int has_high_pc_attr = 0;
18998 int high_pc_relative = 0;
18999
19000 for (i = 0; i < abbrev.num_attrs; ++i)
19001 {
19002 struct attribute attr;
19003
19004 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
19005
19006 /* Store the data if it is of an attribute we want to keep in a
19007 partial symbol table. */
19008 switch (attr.name)
19009 {
19010 case DW_AT_name:
19011 switch (tag)
19012 {
19013 case DW_TAG_compile_unit:
19014 case DW_TAG_partial_unit:
19015 case DW_TAG_type_unit:
19016 /* Compilation units have a DW_AT_name that is a filename, not
19017 a source language identifier. */
19018 case DW_TAG_enumeration_type:
19019 case DW_TAG_enumerator:
19020 /* These tags always have simple identifiers already; no need
19021 to canonicalize them. */
19022 name = DW_STRING (&attr);
19023 break;
19024 default:
19025 {
19026 struct objfile *objfile = dwarf2_per_objfile->objfile;
19027
19028 name
19029 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
19030 &objfile->per_bfd->storage_obstack);
19031 }
19032 break;
19033 }
19034 break;
19035 case DW_AT_linkage_name:
19036 case DW_AT_MIPS_linkage_name:
19037 /* Note that both forms of linkage name might appear. We
19038 assume they will be the same, and we only store the last
19039 one we see. */
19040 linkage_name = DW_STRING (&attr);
19041 break;
19042 case DW_AT_low_pc:
19043 has_low_pc_attr = 1;
19044 lowpc = attr_value_as_address (&attr);
19045 break;
19046 case DW_AT_high_pc:
19047 has_high_pc_attr = 1;
19048 highpc = attr_value_as_address (&attr);
19049 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
19050 high_pc_relative = 1;
19051 break;
19052 case DW_AT_location:
19053 /* Support the .debug_loc offsets. */
19054 if (attr_form_is_block (&attr))
19055 {
19056 d.locdesc = DW_BLOCK (&attr);
19057 }
19058 else if (attr_form_is_section_offset (&attr))
19059 {
19060 dwarf2_complex_location_expr_complaint ();
19061 }
19062 else
19063 {
19064 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
19065 "partial symbol information");
19066 }
19067 break;
19068 case DW_AT_external:
19069 is_external = DW_UNSND (&attr);
19070 break;
19071 case DW_AT_declaration:
19072 is_declaration = DW_UNSND (&attr);
19073 break;
19074 case DW_AT_type:
19075 has_type = 1;
19076 break;
19077 case DW_AT_abstract_origin:
19078 case DW_AT_specification:
19079 case DW_AT_extension:
19080 has_specification = 1;
19081 spec_offset = dwarf2_get_ref_die_offset (&attr);
19082 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
19083 || cu->per_cu->is_dwz);
19084 break;
19085 case DW_AT_sibling:
19086 /* Ignore absolute siblings, they might point outside of
19087 the current compile unit. */
19088 if (attr.form == DW_FORM_ref_addr)
19089 complaint (_("ignoring absolute DW_AT_sibling"));
19090 else
19091 {
19092 const gdb_byte *buffer = reader->buffer;
19093 sect_offset off = dwarf2_get_ref_die_offset (&attr);
19094 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
19095
19096 if (sibling_ptr < info_ptr)
19097 complaint (_("DW_AT_sibling points backwards"));
19098 else if (sibling_ptr > reader->buffer_end)
19099 dwarf2_section_buffer_overflow_complaint (reader->die_section);
19100 else
19101 sibling = sibling_ptr;
19102 }
19103 break;
19104 case DW_AT_byte_size:
19105 has_byte_size = 1;
19106 break;
19107 case DW_AT_const_value:
19108 has_const_value = 1;
19109 break;
19110 case DW_AT_calling_convention:
19111 /* DWARF doesn't provide a way to identify a program's source-level
19112 entry point. DW_AT_calling_convention attributes are only meant
19113 to describe functions' calling conventions.
19114
19115 However, because it's a necessary piece of information in
19116 Fortran, and before DWARF 4 DW_CC_program was the only
19117 piece of debugging information whose definition refers to
19118 a 'main program' at all, several compilers marked Fortran
19119 main programs with DW_CC_program --- even when those
19120 functions use the standard calling conventions.
19121
19122 Although DWARF now specifies a way to provide this
19123 information, we support this practice for backward
19124 compatibility. */
19125 if (DW_UNSND (&attr) == DW_CC_program
19126 && cu->language == language_fortran)
19127 main_subprogram = 1;
19128 break;
19129 case DW_AT_inline:
19130 if (DW_UNSND (&attr) == DW_INL_inlined
19131 || DW_UNSND (&attr) == DW_INL_declared_inlined)
19132 may_be_inlined = 1;
19133 break;
19134
19135 case DW_AT_import:
19136 if (tag == DW_TAG_imported_unit)
19137 {
19138 d.sect_off = dwarf2_get_ref_die_offset (&attr);
19139 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
19140 || cu->per_cu->is_dwz);
19141 }
19142 break;
19143
19144 case DW_AT_main_subprogram:
19145 main_subprogram = DW_UNSND (&attr);
19146 break;
19147
19148 case DW_AT_ranges:
19149 {
19150 /* It would be nice to reuse dwarf2_get_pc_bounds here,
19151 but that requires a full DIE, so instead we just
19152 reimplement it. */
19153 int need_ranges_base = tag != DW_TAG_compile_unit;
19154 unsigned int ranges_offset = (DW_UNSND (&attr)
19155 + (need_ranges_base
19156 ? cu->ranges_base
19157 : 0));
19158
19159 /* Value of the DW_AT_ranges attribute is the offset in the
19160 .debug_ranges section. */
19161 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
19162 nullptr))
19163 has_pc_info = 1;
19164 }
19165 break;
19166
19167 default:
19168 break;
19169 }
19170 }
19171
19172 /* For Ada, if both the name and the linkage name appear, we prefer
19173 the latter. This lets "catch exception" work better, regardless
19174 of the order in which the name and linkage name were emitted.
19175 Really, though, this is just a workaround for the fact that gdb
19176 doesn't store both the name and the linkage name. */
19177 if (cu->language == language_ada && linkage_name != nullptr)
19178 name = linkage_name;
19179
19180 if (high_pc_relative)
19181 highpc += lowpc;
19182
19183 if (has_low_pc_attr && has_high_pc_attr)
19184 {
19185 /* When using the GNU linker, .gnu.linkonce. sections are used to
19186 eliminate duplicate copies of functions and vtables and such.
19187 The linker will arbitrarily choose one and discard the others.
19188 The AT_*_pc values for such functions refer to local labels in
19189 these sections. If the section from that file was discarded, the
19190 labels are not in the output, so the relocs get a value of 0.
19191 If this is a discarded function, mark the pc bounds as invalid,
19192 so that GDB will ignore it. */
19193 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
19194 {
19195 struct objfile *objfile = dwarf2_per_objfile->objfile;
19196 struct gdbarch *gdbarch = get_objfile_arch (objfile);
19197
19198 complaint (_("DW_AT_low_pc %s is zero "
19199 "for DIE at %s [in module %s]"),
19200 paddress (gdbarch, lowpc),
19201 sect_offset_str (sect_off),
19202 objfile_name (objfile));
19203 }
19204 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
19205 else if (lowpc >= highpc)
19206 {
19207 struct objfile *objfile = dwarf2_per_objfile->objfile;
19208 struct gdbarch *gdbarch = get_objfile_arch (objfile);
19209
19210 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
19211 "for DIE at %s [in module %s]"),
19212 paddress (gdbarch, lowpc),
19213 paddress (gdbarch, highpc),
19214 sect_offset_str (sect_off),
19215 objfile_name (objfile));
19216 }
19217 else
19218 has_pc_info = 1;
19219 }
19220
19221 return info_ptr;
19222 }
19223
19224 /* Find a cached partial DIE at OFFSET in CU. */
19225
19226 struct partial_die_info *
19227 dwarf2_cu::find_partial_die (sect_offset sect_off)
19228 {
19229 struct partial_die_info *lookup_die = NULL;
19230 struct partial_die_info part_die (sect_off);
19231
19232 lookup_die = ((struct partial_die_info *)
19233 htab_find_with_hash (partial_dies, &part_die,
19234 to_underlying (sect_off)));
19235
19236 return lookup_die;
19237 }
19238
19239 /* Find a partial DIE at OFFSET, which may or may not be in CU,
19240 except in the case of .debug_types DIEs which do not reference
19241 outside their CU (they do however referencing other types via
19242 DW_FORM_ref_sig8). */
19243
19244 static const struct cu_partial_die_info
19245 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
19246 {
19247 struct dwarf2_per_objfile *dwarf2_per_objfile
19248 = cu->per_cu->dwarf2_per_objfile;
19249 struct objfile *objfile = dwarf2_per_objfile->objfile;
19250 struct dwarf2_per_cu_data *per_cu = NULL;
19251 struct partial_die_info *pd = NULL;
19252
19253 if (offset_in_dwz == cu->per_cu->is_dwz
19254 && offset_in_cu_p (&cu->header, sect_off))
19255 {
19256 pd = cu->find_partial_die (sect_off);
19257 if (pd != NULL)
19258 return { cu, pd };
19259 /* We missed recording what we needed.
19260 Load all dies and try again. */
19261 per_cu = cu->per_cu;
19262 }
19263 else
19264 {
19265 /* TUs don't reference other CUs/TUs (except via type signatures). */
19266 if (cu->per_cu->is_debug_types)
19267 {
19268 error (_("Dwarf Error: Type Unit at offset %s contains"
19269 " external reference to offset %s [in module %s].\n"),
19270 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
19271 bfd_get_filename (objfile->obfd));
19272 }
19273 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
19274 dwarf2_per_objfile);
19275
19276 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
19277 load_partial_comp_unit (per_cu);
19278
19279 per_cu->cu->last_used = 0;
19280 pd = per_cu->cu->find_partial_die (sect_off);
19281 }
19282
19283 /* If we didn't find it, and not all dies have been loaded,
19284 load them all and try again. */
19285
19286 if (pd == NULL && per_cu->load_all_dies == 0)
19287 {
19288 per_cu->load_all_dies = 1;
19289
19290 /* This is nasty. When we reread the DIEs, somewhere up the call chain
19291 THIS_CU->cu may already be in use. So we can't just free it and
19292 replace its DIEs with the ones we read in. Instead, we leave those
19293 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
19294 and clobber THIS_CU->cu->partial_dies with the hash table for the new
19295 set. */
19296 load_partial_comp_unit (per_cu);
19297
19298 pd = per_cu->cu->find_partial_die (sect_off);
19299 }
19300
19301 if (pd == NULL)
19302 internal_error (__FILE__, __LINE__,
19303 _("could not find partial DIE %s "
19304 "in cache [from module %s]\n"),
19305 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
19306 return { per_cu->cu, pd };
19307 }
19308
19309 /* See if we can figure out if the class lives in a namespace. We do
19310 this by looking for a member function; its demangled name will
19311 contain namespace info, if there is any. */
19312
19313 static void
19314 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
19315 struct dwarf2_cu *cu)
19316 {
19317 /* NOTE: carlton/2003-10-07: Getting the info this way changes
19318 what template types look like, because the demangler
19319 frequently doesn't give the same name as the debug info. We
19320 could fix this by only using the demangled name to get the
19321 prefix (but see comment in read_structure_type). */
19322
19323 struct partial_die_info *real_pdi;
19324 struct partial_die_info *child_pdi;
19325
19326 /* If this DIE (this DIE's specification, if any) has a parent, then
19327 we should not do this. We'll prepend the parent's fully qualified
19328 name when we create the partial symbol. */
19329
19330 real_pdi = struct_pdi;
19331 while (real_pdi->has_specification)
19332 {
19333 auto res = find_partial_die (real_pdi->spec_offset,
19334 real_pdi->spec_is_dwz, cu);
19335 real_pdi = res.pdi;
19336 cu = res.cu;
19337 }
19338
19339 if (real_pdi->die_parent != NULL)
19340 return;
19341
19342 for (child_pdi = struct_pdi->die_child;
19343 child_pdi != NULL;
19344 child_pdi = child_pdi->die_sibling)
19345 {
19346 if (child_pdi->tag == DW_TAG_subprogram
19347 && child_pdi->linkage_name != NULL)
19348 {
19349 char *actual_class_name
19350 = language_class_name_from_physname (cu->language_defn,
19351 child_pdi->linkage_name);
19352 if (actual_class_name != NULL)
19353 {
19354 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19355 struct_pdi->name
19356 = obstack_strdup (&objfile->per_bfd->storage_obstack,
19357 actual_class_name);
19358 xfree (actual_class_name);
19359 }
19360 break;
19361 }
19362 }
19363 }
19364
19365 void
19366 partial_die_info::fixup (struct dwarf2_cu *cu)
19367 {
19368 /* Once we've fixed up a die, there's no point in doing so again.
19369 This also avoids a memory leak if we were to call
19370 guess_partial_die_structure_name multiple times. */
19371 if (fixup_called)
19372 return;
19373
19374 /* If we found a reference attribute and the DIE has no name, try
19375 to find a name in the referred to DIE. */
19376
19377 if (name == NULL && has_specification)
19378 {
19379 struct partial_die_info *spec_die;
19380
19381 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
19382 spec_die = res.pdi;
19383 cu = res.cu;
19384
19385 spec_die->fixup (cu);
19386
19387 if (spec_die->name)
19388 {
19389 name = spec_die->name;
19390
19391 /* Copy DW_AT_external attribute if it is set. */
19392 if (spec_die->is_external)
19393 is_external = spec_die->is_external;
19394 }
19395 }
19396
19397 /* Set default names for some unnamed DIEs. */
19398
19399 if (name == NULL && tag == DW_TAG_namespace)
19400 name = CP_ANONYMOUS_NAMESPACE_STR;
19401
19402 /* If there is no parent die to provide a namespace, and there are
19403 children, see if we can determine the namespace from their linkage
19404 name. */
19405 if (cu->language == language_cplus
19406 && !cu->per_cu->dwarf2_per_objfile->types.empty ()
19407 && die_parent == NULL
19408 && has_children
19409 && (tag == DW_TAG_class_type
19410 || tag == DW_TAG_structure_type
19411 || tag == DW_TAG_union_type))
19412 guess_partial_die_structure_name (this, cu);
19413
19414 /* GCC might emit a nameless struct or union that has a linkage
19415 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
19416 if (name == NULL
19417 && (tag == DW_TAG_class_type
19418 || tag == DW_TAG_interface_type
19419 || tag == DW_TAG_structure_type
19420 || tag == DW_TAG_union_type)
19421 && linkage_name != NULL)
19422 {
19423 char *demangled;
19424
19425 demangled = gdb_demangle (linkage_name, DMGL_TYPES);
19426 if (demangled)
19427 {
19428 const char *base;
19429
19430 /* Strip any leading namespaces/classes, keep only the base name.
19431 DW_AT_name for named DIEs does not contain the prefixes. */
19432 base = strrchr (demangled, ':');
19433 if (base && base > demangled && base[-1] == ':')
19434 base++;
19435 else
19436 base = demangled;
19437
19438 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19439 name = obstack_strdup (&objfile->per_bfd->storage_obstack, base);
19440 xfree (demangled);
19441 }
19442 }
19443
19444 fixup_called = 1;
19445 }
19446
19447 /* Read an attribute value described by an attribute form. */
19448
19449 static const gdb_byte *
19450 read_attribute_value (const struct die_reader_specs *reader,
19451 struct attribute *attr, unsigned form,
19452 LONGEST implicit_const, const gdb_byte *info_ptr)
19453 {
19454 struct dwarf2_cu *cu = reader->cu;
19455 struct dwarf2_per_objfile *dwarf2_per_objfile
19456 = cu->per_cu->dwarf2_per_objfile;
19457 struct objfile *objfile = dwarf2_per_objfile->objfile;
19458 struct gdbarch *gdbarch = get_objfile_arch (objfile);
19459 bfd *abfd = reader->abfd;
19460 struct comp_unit_head *cu_header = &cu->header;
19461 unsigned int bytes_read;
19462 struct dwarf_block *blk;
19463
19464 attr->form = (enum dwarf_form) form;
19465 switch (form)
19466 {
19467 case DW_FORM_ref_addr:
19468 if (cu->header.version == 2)
19469 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
19470 else
19471 DW_UNSND (attr) = read_offset (abfd, info_ptr,
19472 &cu->header, &bytes_read);
19473 info_ptr += bytes_read;
19474 break;
19475 case DW_FORM_GNU_ref_alt:
19476 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19477 info_ptr += bytes_read;
19478 break;
19479 case DW_FORM_addr:
19480 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
19481 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
19482 info_ptr += bytes_read;
19483 break;
19484 case DW_FORM_block2:
19485 blk = dwarf_alloc_block (cu);
19486 blk->size = read_2_bytes (abfd, info_ptr);
19487 info_ptr += 2;
19488 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19489 info_ptr += blk->size;
19490 DW_BLOCK (attr) = blk;
19491 break;
19492 case DW_FORM_block4:
19493 blk = dwarf_alloc_block (cu);
19494 blk->size = read_4_bytes (abfd, info_ptr);
19495 info_ptr += 4;
19496 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19497 info_ptr += blk->size;
19498 DW_BLOCK (attr) = blk;
19499 break;
19500 case DW_FORM_data2:
19501 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
19502 info_ptr += 2;
19503 break;
19504 case DW_FORM_data4:
19505 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
19506 info_ptr += 4;
19507 break;
19508 case DW_FORM_data8:
19509 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
19510 info_ptr += 8;
19511 break;
19512 case DW_FORM_data16:
19513 blk = dwarf_alloc_block (cu);
19514 blk->size = 16;
19515 blk->data = read_n_bytes (abfd, info_ptr, 16);
19516 info_ptr += 16;
19517 DW_BLOCK (attr) = blk;
19518 break;
19519 case DW_FORM_sec_offset:
19520 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19521 info_ptr += bytes_read;
19522 break;
19523 case DW_FORM_string:
19524 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
19525 DW_STRING_IS_CANONICAL (attr) = 0;
19526 info_ptr += bytes_read;
19527 break;
19528 case DW_FORM_strp:
19529 if (!cu->per_cu->is_dwz)
19530 {
19531 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
19532 abfd, info_ptr, cu_header,
19533 &bytes_read);
19534 DW_STRING_IS_CANONICAL (attr) = 0;
19535 info_ptr += bytes_read;
19536 break;
19537 }
19538 /* FALLTHROUGH */
19539 case DW_FORM_line_strp:
19540 if (!cu->per_cu->is_dwz)
19541 {
19542 DW_STRING (attr) = read_indirect_line_string (dwarf2_per_objfile,
19543 abfd, info_ptr,
19544 cu_header, &bytes_read);
19545 DW_STRING_IS_CANONICAL (attr) = 0;
19546 info_ptr += bytes_read;
19547 break;
19548 }
19549 /* FALLTHROUGH */
19550 case DW_FORM_GNU_strp_alt:
19551 {
19552 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
19553 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
19554 &bytes_read);
19555
19556 DW_STRING (attr) = read_indirect_string_from_dwz (objfile,
19557 dwz, str_offset);
19558 DW_STRING_IS_CANONICAL (attr) = 0;
19559 info_ptr += bytes_read;
19560 }
19561 break;
19562 case DW_FORM_exprloc:
19563 case DW_FORM_block:
19564 blk = dwarf_alloc_block (cu);
19565 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19566 info_ptr += bytes_read;
19567 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19568 info_ptr += blk->size;
19569 DW_BLOCK (attr) = blk;
19570 break;
19571 case DW_FORM_block1:
19572 blk = dwarf_alloc_block (cu);
19573 blk->size = read_1_byte (abfd, info_ptr);
19574 info_ptr += 1;
19575 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19576 info_ptr += blk->size;
19577 DW_BLOCK (attr) = blk;
19578 break;
19579 case DW_FORM_data1:
19580 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19581 info_ptr += 1;
19582 break;
19583 case DW_FORM_flag:
19584 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19585 info_ptr += 1;
19586 break;
19587 case DW_FORM_flag_present:
19588 DW_UNSND (attr) = 1;
19589 break;
19590 case DW_FORM_sdata:
19591 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19592 info_ptr += bytes_read;
19593 break;
19594 case DW_FORM_udata:
19595 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19596 info_ptr += bytes_read;
19597 break;
19598 case DW_FORM_ref1:
19599 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19600 + read_1_byte (abfd, info_ptr));
19601 info_ptr += 1;
19602 break;
19603 case DW_FORM_ref2:
19604 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19605 + read_2_bytes (abfd, info_ptr));
19606 info_ptr += 2;
19607 break;
19608 case DW_FORM_ref4:
19609 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19610 + read_4_bytes (abfd, info_ptr));
19611 info_ptr += 4;
19612 break;
19613 case DW_FORM_ref8:
19614 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19615 + read_8_bytes (abfd, info_ptr));
19616 info_ptr += 8;
19617 break;
19618 case DW_FORM_ref_sig8:
19619 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
19620 info_ptr += 8;
19621 break;
19622 case DW_FORM_ref_udata:
19623 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19624 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
19625 info_ptr += bytes_read;
19626 break;
19627 case DW_FORM_indirect:
19628 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19629 info_ptr += bytes_read;
19630 if (form == DW_FORM_implicit_const)
19631 {
19632 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19633 info_ptr += bytes_read;
19634 }
19635 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19636 info_ptr);
19637 break;
19638 case DW_FORM_implicit_const:
19639 DW_SND (attr) = implicit_const;
19640 break;
19641 case DW_FORM_addrx:
19642 case DW_FORM_GNU_addr_index:
19643 if (reader->dwo_file == NULL)
19644 {
19645 /* For now flag a hard error.
19646 Later we can turn this into a complaint. */
19647 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19648 dwarf_form_name (form),
19649 bfd_get_filename (abfd));
19650 }
19651 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
19652 info_ptr += bytes_read;
19653 break;
19654 case DW_FORM_strx:
19655 case DW_FORM_strx1:
19656 case DW_FORM_strx2:
19657 case DW_FORM_strx3:
19658 case DW_FORM_strx4:
19659 case DW_FORM_GNU_str_index:
19660 if (reader->dwo_file == NULL)
19661 {
19662 /* For now flag a hard error.
19663 Later we can turn this into a complaint if warranted. */
19664 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19665 dwarf_form_name (form),
19666 bfd_get_filename (abfd));
19667 }
19668 {
19669 ULONGEST str_index;
19670 if (form == DW_FORM_strx1)
19671 {
19672 str_index = read_1_byte (abfd, info_ptr);
19673 info_ptr += 1;
19674 }
19675 else if (form == DW_FORM_strx2)
19676 {
19677 str_index = read_2_bytes (abfd, info_ptr);
19678 info_ptr += 2;
19679 }
19680 else if (form == DW_FORM_strx3)
19681 {
19682 str_index = read_3_bytes (abfd, info_ptr);
19683 info_ptr += 3;
19684 }
19685 else if (form == DW_FORM_strx4)
19686 {
19687 str_index = read_4_bytes (abfd, info_ptr);
19688 info_ptr += 4;
19689 }
19690 else
19691 {
19692 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19693 info_ptr += bytes_read;
19694 }
19695 DW_STRING (attr) = read_str_index (reader, str_index);
19696 DW_STRING_IS_CANONICAL (attr) = 0;
19697 }
19698 break;
19699 default:
19700 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
19701 dwarf_form_name (form),
19702 bfd_get_filename (abfd));
19703 }
19704
19705 /* Super hack. */
19706 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
19707 attr->form = DW_FORM_GNU_ref_alt;
19708
19709 /* We have seen instances where the compiler tried to emit a byte
19710 size attribute of -1 which ended up being encoded as an unsigned
19711 0xffffffff. Although 0xffffffff is technically a valid size value,
19712 an object of this size seems pretty unlikely so we can relatively
19713 safely treat these cases as if the size attribute was invalid and
19714 treat them as zero by default. */
19715 if (attr->name == DW_AT_byte_size
19716 && form == DW_FORM_data4
19717 && DW_UNSND (attr) >= 0xffffffff)
19718 {
19719 complaint
19720 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19721 hex_string (DW_UNSND (attr)));
19722 DW_UNSND (attr) = 0;
19723 }
19724
19725 return info_ptr;
19726 }
19727
19728 /* Read an attribute described by an abbreviated attribute. */
19729
19730 static const gdb_byte *
19731 read_attribute (const struct die_reader_specs *reader,
19732 struct attribute *attr, struct attr_abbrev *abbrev,
19733 const gdb_byte *info_ptr)
19734 {
19735 attr->name = abbrev->name;
19736 return read_attribute_value (reader, attr, abbrev->form,
19737 abbrev->implicit_const, info_ptr);
19738 }
19739
19740 /* Read dwarf information from a buffer. */
19741
19742 static unsigned int
19743 read_1_byte (bfd *abfd, const gdb_byte *buf)
19744 {
19745 return bfd_get_8 (abfd, buf);
19746 }
19747
19748 static int
19749 read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
19750 {
19751 return bfd_get_signed_8 (abfd, buf);
19752 }
19753
19754 static unsigned int
19755 read_2_bytes (bfd *abfd, const gdb_byte *buf)
19756 {
19757 return bfd_get_16 (abfd, buf);
19758 }
19759
19760 static int
19761 read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
19762 {
19763 return bfd_get_signed_16 (abfd, buf);
19764 }
19765
19766 static unsigned int
19767 read_3_bytes (bfd *abfd, const gdb_byte *buf)
19768 {
19769 unsigned int result = 0;
19770 for (int i = 0; i < 3; ++i)
19771 {
19772 unsigned char byte = bfd_get_8 (abfd, buf);
19773 buf++;
19774 result |= ((unsigned int) byte << (i * 8));
19775 }
19776 return result;
19777 }
19778
19779 static unsigned int
19780 read_4_bytes (bfd *abfd, const gdb_byte *buf)
19781 {
19782 return bfd_get_32 (abfd, buf);
19783 }
19784
19785 static int
19786 read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
19787 {
19788 return bfd_get_signed_32 (abfd, buf);
19789 }
19790
19791 static ULONGEST
19792 read_8_bytes (bfd *abfd, const gdb_byte *buf)
19793 {
19794 return bfd_get_64 (abfd, buf);
19795 }
19796
19797 static CORE_ADDR
19798 read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
19799 unsigned int *bytes_read)
19800 {
19801 struct comp_unit_head *cu_header = &cu->header;
19802 CORE_ADDR retval = 0;
19803
19804 if (cu_header->signed_addr_p)
19805 {
19806 switch (cu_header->addr_size)
19807 {
19808 case 2:
19809 retval = bfd_get_signed_16 (abfd, buf);
19810 break;
19811 case 4:
19812 retval = bfd_get_signed_32 (abfd, buf);
19813 break;
19814 case 8:
19815 retval = bfd_get_signed_64 (abfd, buf);
19816 break;
19817 default:
19818 internal_error (__FILE__, __LINE__,
19819 _("read_address: bad switch, signed [in module %s]"),
19820 bfd_get_filename (abfd));
19821 }
19822 }
19823 else
19824 {
19825 switch (cu_header->addr_size)
19826 {
19827 case 2:
19828 retval = bfd_get_16 (abfd, buf);
19829 break;
19830 case 4:
19831 retval = bfd_get_32 (abfd, buf);
19832 break;
19833 case 8:
19834 retval = bfd_get_64 (abfd, buf);
19835 break;
19836 default:
19837 internal_error (__FILE__, __LINE__,
19838 _("read_address: bad switch, "
19839 "unsigned [in module %s]"),
19840 bfd_get_filename (abfd));
19841 }
19842 }
19843
19844 *bytes_read = cu_header->addr_size;
19845 return retval;
19846 }
19847
19848 /* Read the initial length from a section. The (draft) DWARF 3
19849 specification allows the initial length to take up either 4 bytes
19850 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
19851 bytes describe the length and all offsets will be 8 bytes in length
19852 instead of 4.
19853
19854 An older, non-standard 64-bit format is also handled by this
19855 function. The older format in question stores the initial length
19856 as an 8-byte quantity without an escape value. Lengths greater
19857 than 2^32 aren't very common which means that the initial 4 bytes
19858 is almost always zero. Since a length value of zero doesn't make
19859 sense for the 32-bit format, this initial zero can be considered to
19860 be an escape value which indicates the presence of the older 64-bit
19861 format. As written, the code can't detect (old format) lengths
19862 greater than 4GB. If it becomes necessary to handle lengths
19863 somewhat larger than 4GB, we could allow other small values (such
19864 as the non-sensical values of 1, 2, and 3) to also be used as
19865 escape values indicating the presence of the old format.
19866
19867 The value returned via bytes_read should be used to increment the
19868 relevant pointer after calling read_initial_length().
19869
19870 [ Note: read_initial_length() and read_offset() are based on the
19871 document entitled "DWARF Debugging Information Format", revision
19872 3, draft 8, dated November 19, 2001. This document was obtained
19873 from:
19874
19875 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
19876
19877 This document is only a draft and is subject to change. (So beware.)
19878
19879 Details regarding the older, non-standard 64-bit format were
19880 determined empirically by examining 64-bit ELF files produced by
19881 the SGI toolchain on an IRIX 6.5 machine.
19882
19883 - Kevin, July 16, 2002
19884 ] */
19885
19886 static LONGEST
19887 read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
19888 {
19889 LONGEST length = bfd_get_32 (abfd, buf);
19890
19891 if (length == 0xffffffff)
19892 {
19893 length = bfd_get_64 (abfd, buf + 4);
19894 *bytes_read = 12;
19895 }
19896 else if (length == 0)
19897 {
19898 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
19899 length = bfd_get_64 (abfd, buf);
19900 *bytes_read = 8;
19901 }
19902 else
19903 {
19904 *bytes_read = 4;
19905 }
19906
19907 return length;
19908 }
19909
19910 /* Cover function for read_initial_length.
19911 Returns the length of the object at BUF, and stores the size of the
19912 initial length in *BYTES_READ and stores the size that offsets will be in
19913 *OFFSET_SIZE.
19914 If the initial length size is not equivalent to that specified in
19915 CU_HEADER then issue a complaint.
19916 This is useful when reading non-comp-unit headers. */
19917
19918 static LONGEST
19919 read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
19920 const struct comp_unit_head *cu_header,
19921 unsigned int *bytes_read,
19922 unsigned int *offset_size)
19923 {
19924 LONGEST length = read_initial_length (abfd, buf, bytes_read);
19925
19926 gdb_assert (cu_header->initial_length_size == 4
19927 || cu_header->initial_length_size == 8
19928 || cu_header->initial_length_size == 12);
19929
19930 if (cu_header->initial_length_size != *bytes_read)
19931 complaint (_("intermixed 32-bit and 64-bit DWARF sections"));
19932
19933 *offset_size = (*bytes_read == 4) ? 4 : 8;
19934 return length;
19935 }
19936
19937 /* Read an offset from the data stream. The size of the offset is
19938 given by cu_header->offset_size. */
19939
19940 static LONGEST
19941 read_offset (bfd *abfd, const gdb_byte *buf,
19942 const struct comp_unit_head *cu_header,
19943 unsigned int *bytes_read)
19944 {
19945 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
19946
19947 *bytes_read = cu_header->offset_size;
19948 return offset;
19949 }
19950
19951 /* Read an offset from the data stream. */
19952
19953 static LONGEST
19954 read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
19955 {
19956 LONGEST retval = 0;
19957
19958 switch (offset_size)
19959 {
19960 case 4:
19961 retval = bfd_get_32 (abfd, buf);
19962 break;
19963 case 8:
19964 retval = bfd_get_64 (abfd, buf);
19965 break;
19966 default:
19967 internal_error (__FILE__, __LINE__,
19968 _("read_offset_1: bad switch [in module %s]"),
19969 bfd_get_filename (abfd));
19970 }
19971
19972 return retval;
19973 }
19974
19975 static const gdb_byte *
19976 read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
19977 {
19978 /* If the size of a host char is 8 bits, we can return a pointer
19979 to the buffer, otherwise we have to copy the data to a buffer
19980 allocated on the temporary obstack. */
19981 gdb_assert (HOST_CHAR_BIT == 8);
19982 return buf;
19983 }
19984
19985 static const char *
19986 read_direct_string (bfd *abfd, const gdb_byte *buf,
19987 unsigned int *bytes_read_ptr)
19988 {
19989 /* If the size of a host char is 8 bits, we can return a pointer
19990 to the string, otherwise we have to copy the string to a buffer
19991 allocated on the temporary obstack. */
19992 gdb_assert (HOST_CHAR_BIT == 8);
19993 if (*buf == '\0')
19994 {
19995 *bytes_read_ptr = 1;
19996 return NULL;
19997 }
19998 *bytes_read_ptr = strlen ((const char *) buf) + 1;
19999 return (const char *) buf;
20000 }
20001
20002 /* Return pointer to string at section SECT offset STR_OFFSET with error
20003 reporting strings FORM_NAME and SECT_NAME. */
20004
20005 static const char *
20006 read_indirect_string_at_offset_from (struct objfile *objfile,
20007 bfd *abfd, LONGEST str_offset,
20008 struct dwarf2_section_info *sect,
20009 const char *form_name,
20010 const char *sect_name)
20011 {
20012 dwarf2_read_section (objfile, sect);
20013 if (sect->buffer == NULL)
20014 error (_("%s used without %s section [in module %s]"),
20015 form_name, sect_name, bfd_get_filename (abfd));
20016 if (str_offset >= sect->size)
20017 error (_("%s pointing outside of %s section [in module %s]"),
20018 form_name, sect_name, bfd_get_filename (abfd));
20019 gdb_assert (HOST_CHAR_BIT == 8);
20020 if (sect->buffer[str_offset] == '\0')
20021 return NULL;
20022 return (const char *) (sect->buffer + str_offset);
20023 }
20024
20025 /* Return pointer to string at .debug_str offset STR_OFFSET. */
20026
20027 static const char *
20028 read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
20029 bfd *abfd, LONGEST str_offset)
20030 {
20031 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
20032 abfd, str_offset,
20033 &dwarf2_per_objfile->str,
20034 "DW_FORM_strp", ".debug_str");
20035 }
20036
20037 /* Return pointer to string at .debug_line_str offset STR_OFFSET. */
20038
20039 static const char *
20040 read_indirect_line_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
20041 bfd *abfd, LONGEST str_offset)
20042 {
20043 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
20044 abfd, str_offset,
20045 &dwarf2_per_objfile->line_str,
20046 "DW_FORM_line_strp",
20047 ".debug_line_str");
20048 }
20049
20050 /* Read a string at offset STR_OFFSET in the .debug_str section from
20051 the .dwz file DWZ. Throw an error if the offset is too large. If
20052 the string consists of a single NUL byte, return NULL; otherwise
20053 return a pointer to the string. */
20054
20055 static const char *
20056 read_indirect_string_from_dwz (struct objfile *objfile, struct dwz_file *dwz,
20057 LONGEST str_offset)
20058 {
20059 dwarf2_read_section (objfile, &dwz->str);
20060
20061 if (dwz->str.buffer == NULL)
20062 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
20063 "section [in module %s]"),
20064 bfd_get_filename (dwz->dwz_bfd.get ()));
20065 if (str_offset >= dwz->str.size)
20066 error (_("DW_FORM_GNU_strp_alt pointing outside of "
20067 ".debug_str section [in module %s]"),
20068 bfd_get_filename (dwz->dwz_bfd.get ()));
20069 gdb_assert (HOST_CHAR_BIT == 8);
20070 if (dwz->str.buffer[str_offset] == '\0')
20071 return NULL;
20072 return (const char *) (dwz->str.buffer + str_offset);
20073 }
20074
20075 /* Return pointer to string at .debug_str offset as read from BUF.
20076 BUF is assumed to be in a compilation unit described by CU_HEADER.
20077 Return *BYTES_READ_PTR count of bytes read from BUF. */
20078
20079 static const char *
20080 read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
20081 const gdb_byte *buf,
20082 const struct comp_unit_head *cu_header,
20083 unsigned int *bytes_read_ptr)
20084 {
20085 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
20086
20087 return read_indirect_string_at_offset (dwarf2_per_objfile, abfd, str_offset);
20088 }
20089
20090 /* Return pointer to string at .debug_line_str offset as read from BUF.
20091 BUF is assumed to be in a compilation unit described by CU_HEADER.
20092 Return *BYTES_READ_PTR count of bytes read from BUF. */
20093
20094 static const char *
20095 read_indirect_line_string (struct dwarf2_per_objfile *dwarf2_per_objfile,
20096 bfd *abfd, const gdb_byte *buf,
20097 const struct comp_unit_head *cu_header,
20098 unsigned int *bytes_read_ptr)
20099 {
20100 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
20101
20102 return read_indirect_line_string_at_offset (dwarf2_per_objfile, abfd,
20103 str_offset);
20104 }
20105
20106 ULONGEST
20107 read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
20108 unsigned int *bytes_read_ptr)
20109 {
20110 ULONGEST result;
20111 unsigned int num_read;
20112 int shift;
20113 unsigned char byte;
20114
20115 result = 0;
20116 shift = 0;
20117 num_read = 0;
20118 while (1)
20119 {
20120 byte = bfd_get_8 (abfd, buf);
20121 buf++;
20122 num_read++;
20123 result |= ((ULONGEST) (byte & 127) << shift);
20124 if ((byte & 128) == 0)
20125 {
20126 break;
20127 }
20128 shift += 7;
20129 }
20130 *bytes_read_ptr = num_read;
20131 return result;
20132 }
20133
20134 static LONGEST
20135 read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
20136 unsigned int *bytes_read_ptr)
20137 {
20138 ULONGEST result;
20139 int shift, num_read;
20140 unsigned char byte;
20141
20142 result = 0;
20143 shift = 0;
20144 num_read = 0;
20145 while (1)
20146 {
20147 byte = bfd_get_8 (abfd, buf);
20148 buf++;
20149 num_read++;
20150 result |= ((ULONGEST) (byte & 127) << shift);
20151 shift += 7;
20152 if ((byte & 128) == 0)
20153 {
20154 break;
20155 }
20156 }
20157 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
20158 result |= -(((ULONGEST) 1) << shift);
20159 *bytes_read_ptr = num_read;
20160 return result;
20161 }
20162
20163 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
20164 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
20165 ADDR_SIZE is the size of addresses from the CU header. */
20166
20167 static CORE_ADDR
20168 read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
20169 unsigned int addr_index, ULONGEST addr_base, int addr_size)
20170 {
20171 struct objfile *objfile = dwarf2_per_objfile->objfile;
20172 bfd *abfd = objfile->obfd;
20173 const gdb_byte *info_ptr;
20174
20175 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
20176 if (dwarf2_per_objfile->addr.buffer == NULL)
20177 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
20178 objfile_name (objfile));
20179 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
20180 error (_("DW_FORM_addr_index pointing outside of "
20181 ".debug_addr section [in module %s]"),
20182 objfile_name (objfile));
20183 info_ptr = (dwarf2_per_objfile->addr.buffer
20184 + addr_base + addr_index * addr_size);
20185 if (addr_size == 4)
20186 return bfd_get_32 (abfd, info_ptr);
20187 else
20188 return bfd_get_64 (abfd, info_ptr);
20189 }
20190
20191 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
20192
20193 static CORE_ADDR
20194 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
20195 {
20196 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
20197 cu->addr_base, cu->header.addr_size);
20198 }
20199
20200 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
20201
20202 static CORE_ADDR
20203 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
20204 unsigned int *bytes_read)
20205 {
20206 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
20207 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
20208
20209 return read_addr_index (cu, addr_index);
20210 }
20211
20212 /* Data structure to pass results from dwarf2_read_addr_index_reader
20213 back to dwarf2_read_addr_index. */
20214
20215 struct dwarf2_read_addr_index_data
20216 {
20217 ULONGEST addr_base;
20218 int addr_size;
20219 };
20220
20221 /* die_reader_func for dwarf2_read_addr_index. */
20222
20223 static void
20224 dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
20225 const gdb_byte *info_ptr,
20226 struct die_info *comp_unit_die,
20227 int has_children,
20228 void *data)
20229 {
20230 struct dwarf2_cu *cu = reader->cu;
20231 struct dwarf2_read_addr_index_data *aidata =
20232 (struct dwarf2_read_addr_index_data *) data;
20233
20234 aidata->addr_base = cu->addr_base;
20235 aidata->addr_size = cu->header.addr_size;
20236 }
20237
20238 /* Given an index in .debug_addr, fetch the value.
20239 NOTE: This can be called during dwarf expression evaluation,
20240 long after the debug information has been read, and thus per_cu->cu
20241 may no longer exist. */
20242
20243 CORE_ADDR
20244 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
20245 unsigned int addr_index)
20246 {
20247 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
20248 struct dwarf2_cu *cu = per_cu->cu;
20249 ULONGEST addr_base;
20250 int addr_size;
20251
20252 /* We need addr_base and addr_size.
20253 If we don't have PER_CU->cu, we have to get it.
20254 Nasty, but the alternative is storing the needed info in PER_CU,
20255 which at this point doesn't seem justified: it's not clear how frequently
20256 it would get used and it would increase the size of every PER_CU.
20257 Entry points like dwarf2_per_cu_addr_size do a similar thing
20258 so we're not in uncharted territory here.
20259 Alas we need to be a bit more complicated as addr_base is contained
20260 in the DIE.
20261
20262 We don't need to read the entire CU(/TU).
20263 We just need the header and top level die.
20264
20265 IWBN to use the aging mechanism to let us lazily later discard the CU.
20266 For now we skip this optimization. */
20267
20268 if (cu != NULL)
20269 {
20270 addr_base = cu->addr_base;
20271 addr_size = cu->header.addr_size;
20272 }
20273 else
20274 {
20275 struct dwarf2_read_addr_index_data aidata;
20276
20277 /* Note: We can't use init_cutu_and_read_dies_simple here,
20278 we need addr_base. */
20279 init_cutu_and_read_dies (per_cu, NULL, 0, 0, false,
20280 dwarf2_read_addr_index_reader, &aidata);
20281 addr_base = aidata.addr_base;
20282 addr_size = aidata.addr_size;
20283 }
20284
20285 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
20286 addr_size);
20287 }
20288
20289 /* Given a DW_FORM_GNU_str_index or DW_FORM_strx, fetch the string.
20290 This is only used by the Fission support. */
20291
20292 static const char *
20293 read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
20294 {
20295 struct dwarf2_cu *cu = reader->cu;
20296 struct dwarf2_per_objfile *dwarf2_per_objfile
20297 = cu->per_cu->dwarf2_per_objfile;
20298 struct objfile *objfile = dwarf2_per_objfile->objfile;
20299 const char *objf_name = objfile_name (objfile);
20300 bfd *abfd = objfile->obfd;
20301 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
20302 struct dwarf2_section_info *str_offsets_section =
20303 &reader->dwo_file->sections.str_offsets;
20304 const gdb_byte *info_ptr;
20305 ULONGEST str_offset;
20306 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
20307
20308 dwarf2_read_section (objfile, str_section);
20309 dwarf2_read_section (objfile, str_offsets_section);
20310 if (str_section->buffer == NULL)
20311 error (_("%s used without .debug_str.dwo section"
20312 " in CU at offset %s [in module %s]"),
20313 form_name, sect_offset_str (cu->header.sect_off), objf_name);
20314 if (str_offsets_section->buffer == NULL)
20315 error (_("%s used without .debug_str_offsets.dwo section"
20316 " in CU at offset %s [in module %s]"),
20317 form_name, sect_offset_str (cu->header.sect_off), objf_name);
20318 if (str_index * cu->header.offset_size >= str_offsets_section->size)
20319 error (_("%s pointing outside of .debug_str_offsets.dwo"
20320 " section in CU at offset %s [in module %s]"),
20321 form_name, sect_offset_str (cu->header.sect_off), objf_name);
20322 info_ptr = (str_offsets_section->buffer
20323 + str_index * cu->header.offset_size);
20324 if (cu->header.offset_size == 4)
20325 str_offset = bfd_get_32 (abfd, info_ptr);
20326 else
20327 str_offset = bfd_get_64 (abfd, info_ptr);
20328 if (str_offset >= str_section->size)
20329 error (_("Offset from %s pointing outside of"
20330 " .debug_str.dwo section in CU at offset %s [in module %s]"),
20331 form_name, sect_offset_str (cu->header.sect_off), objf_name);
20332 return (const char *) (str_section->buffer + str_offset);
20333 }
20334
20335 /* Return the length of an LEB128 number in BUF. */
20336
20337 static int
20338 leb128_size (const gdb_byte *buf)
20339 {
20340 const gdb_byte *begin = buf;
20341 gdb_byte byte;
20342
20343 while (1)
20344 {
20345 byte = *buf++;
20346 if ((byte & 128) == 0)
20347 return buf - begin;
20348 }
20349 }
20350
20351 static void
20352 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
20353 {
20354 switch (lang)
20355 {
20356 case DW_LANG_C89:
20357 case DW_LANG_C99:
20358 case DW_LANG_C11:
20359 case DW_LANG_C:
20360 case DW_LANG_UPC:
20361 cu->language = language_c;
20362 break;
20363 case DW_LANG_Java:
20364 case DW_LANG_C_plus_plus:
20365 case DW_LANG_C_plus_plus_11:
20366 case DW_LANG_C_plus_plus_14:
20367 cu->language = language_cplus;
20368 break;
20369 case DW_LANG_D:
20370 cu->language = language_d;
20371 break;
20372 case DW_LANG_Fortran77:
20373 case DW_LANG_Fortran90:
20374 case DW_LANG_Fortran95:
20375 case DW_LANG_Fortran03:
20376 case DW_LANG_Fortran08:
20377 cu->language = language_fortran;
20378 break;
20379 case DW_LANG_Go:
20380 cu->language = language_go;
20381 break;
20382 case DW_LANG_Mips_Assembler:
20383 cu->language = language_asm;
20384 break;
20385 case DW_LANG_Ada83:
20386 case DW_LANG_Ada95:
20387 cu->language = language_ada;
20388 break;
20389 case DW_LANG_Modula2:
20390 cu->language = language_m2;
20391 break;
20392 case DW_LANG_Pascal83:
20393 cu->language = language_pascal;
20394 break;
20395 case DW_LANG_ObjC:
20396 cu->language = language_objc;
20397 break;
20398 case DW_LANG_Rust:
20399 case DW_LANG_Rust_old:
20400 cu->language = language_rust;
20401 break;
20402 case DW_LANG_Cobol74:
20403 case DW_LANG_Cobol85:
20404 default:
20405 cu->language = language_minimal;
20406 break;
20407 }
20408 cu->language_defn = language_def (cu->language);
20409 }
20410
20411 /* Return the named attribute or NULL if not there. */
20412
20413 static struct attribute *
20414 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20415 {
20416 for (;;)
20417 {
20418 unsigned int i;
20419 struct attribute *spec = NULL;
20420
20421 for (i = 0; i < die->num_attrs; ++i)
20422 {
20423 if (die->attrs[i].name == name)
20424 return &die->attrs[i];
20425 if (die->attrs[i].name == DW_AT_specification
20426 || die->attrs[i].name == DW_AT_abstract_origin)
20427 spec = &die->attrs[i];
20428 }
20429
20430 if (!spec)
20431 break;
20432
20433 die = follow_die_ref (die, spec, &cu);
20434 }
20435
20436 return NULL;
20437 }
20438
20439 /* Return the named attribute or NULL if not there,
20440 but do not follow DW_AT_specification, etc.
20441 This is for use in contexts where we're reading .debug_types dies.
20442 Following DW_AT_specification, DW_AT_abstract_origin will take us
20443 back up the chain, and we want to go down. */
20444
20445 static struct attribute *
20446 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
20447 {
20448 unsigned int i;
20449
20450 for (i = 0; i < die->num_attrs; ++i)
20451 if (die->attrs[i].name == name)
20452 return &die->attrs[i];
20453
20454 return NULL;
20455 }
20456
20457 /* Return the string associated with a string-typed attribute, or NULL if it
20458 is either not found or is of an incorrect type. */
20459
20460 static const char *
20461 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20462 {
20463 struct attribute *attr;
20464 const char *str = NULL;
20465
20466 attr = dwarf2_attr (die, name, cu);
20467
20468 if (attr != NULL)
20469 {
20470 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
20471 || attr->form == DW_FORM_string
20472 || attr->form == DW_FORM_strx
20473 || attr->form == DW_FORM_strx1
20474 || attr->form == DW_FORM_strx2
20475 || attr->form == DW_FORM_strx3
20476 || attr->form == DW_FORM_strx4
20477 || attr->form == DW_FORM_GNU_str_index
20478 || attr->form == DW_FORM_GNU_strp_alt)
20479 str = DW_STRING (attr);
20480 else
20481 complaint (_("string type expected for attribute %s for "
20482 "DIE at %s in module %s"),
20483 dwarf_attr_name (name), sect_offset_str (die->sect_off),
20484 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
20485 }
20486
20487 return str;
20488 }
20489
20490 /* Return the dwo name or NULL if not present. If present, it is in either
20491 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
20492 static const char *
20493 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
20494 {
20495 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
20496 if (dwo_name == nullptr)
20497 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
20498 return dwo_name;
20499 }
20500
20501 /* Return non-zero iff the attribute NAME is defined for the given DIE,
20502 and holds a non-zero value. This function should only be used for
20503 DW_FORM_flag or DW_FORM_flag_present attributes. */
20504
20505 static int
20506 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
20507 {
20508 struct attribute *attr = dwarf2_attr (die, name, cu);
20509
20510 return (attr && DW_UNSND (attr));
20511 }
20512
20513 static int
20514 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
20515 {
20516 /* A DIE is a declaration if it has a DW_AT_declaration attribute
20517 which value is non-zero. However, we have to be careful with
20518 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
20519 (via dwarf2_flag_true_p) follows this attribute. So we may
20520 end up accidently finding a declaration attribute that belongs
20521 to a different DIE referenced by the specification attribute,
20522 even though the given DIE does not have a declaration attribute. */
20523 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
20524 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
20525 }
20526
20527 /* Return the die giving the specification for DIE, if there is
20528 one. *SPEC_CU is the CU containing DIE on input, and the CU
20529 containing the return value on output. If there is no
20530 specification, but there is an abstract origin, that is
20531 returned. */
20532
20533 static struct die_info *
20534 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
20535 {
20536 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
20537 *spec_cu);
20538
20539 if (spec_attr == NULL)
20540 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
20541
20542 if (spec_attr == NULL)
20543 return NULL;
20544 else
20545 return follow_die_ref (die, spec_attr, spec_cu);
20546 }
20547
20548 /* Stub for free_line_header to match void * callback types. */
20549
20550 static void
20551 free_line_header_voidp (void *arg)
20552 {
20553 struct line_header *lh = (struct line_header *) arg;
20554
20555 delete lh;
20556 }
20557
20558 void
20559 line_header::add_include_dir (const char *include_dir)
20560 {
20561 if (dwarf_line_debug >= 2)
20562 {
20563 size_t new_size;
20564 if (version >= 5)
20565 new_size = m_include_dirs.size ();
20566 else
20567 new_size = m_include_dirs.size () + 1;
20568 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
20569 new_size, include_dir);
20570 }
20571 m_include_dirs.push_back (include_dir);
20572 }
20573
20574 void
20575 line_header::add_file_name (const char *name,
20576 dir_index d_index,
20577 unsigned int mod_time,
20578 unsigned int length)
20579 {
20580 if (dwarf_line_debug >= 2)
20581 {
20582 size_t new_size;
20583 if (version >= 5)
20584 new_size = file_names_size ();
20585 else
20586 new_size = file_names_size () + 1;
20587 fprintf_unfiltered (gdb_stdlog, "Adding file %zu: %s\n",
20588 new_size, name);
20589 }
20590 m_file_names.emplace_back (name, d_index, mod_time, length);
20591 }
20592
20593 /* A convenience function to find the proper .debug_line section for a CU. */
20594
20595 static struct dwarf2_section_info *
20596 get_debug_line_section (struct dwarf2_cu *cu)
20597 {
20598 struct dwarf2_section_info *section;
20599 struct dwarf2_per_objfile *dwarf2_per_objfile
20600 = cu->per_cu->dwarf2_per_objfile;
20601
20602 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
20603 DWO file. */
20604 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20605 section = &cu->dwo_unit->dwo_file->sections.line;
20606 else if (cu->per_cu->is_dwz)
20607 {
20608 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
20609
20610 section = &dwz->line;
20611 }
20612 else
20613 section = &dwarf2_per_objfile->line;
20614
20615 return section;
20616 }
20617
20618 /* Read directory or file name entry format, starting with byte of
20619 format count entries, ULEB128 pairs of entry formats, ULEB128 of
20620 entries count and the entries themselves in the described entry
20621 format. */
20622
20623 static void
20624 read_formatted_entries (struct dwarf2_per_objfile *dwarf2_per_objfile,
20625 bfd *abfd, const gdb_byte **bufp,
20626 struct line_header *lh,
20627 const struct comp_unit_head *cu_header,
20628 void (*callback) (struct line_header *lh,
20629 const char *name,
20630 dir_index d_index,
20631 unsigned int mod_time,
20632 unsigned int length))
20633 {
20634 gdb_byte format_count, formati;
20635 ULONGEST data_count, datai;
20636 const gdb_byte *buf = *bufp;
20637 const gdb_byte *format_header_data;
20638 unsigned int bytes_read;
20639
20640 format_count = read_1_byte (abfd, buf);
20641 buf += 1;
20642 format_header_data = buf;
20643 for (formati = 0; formati < format_count; formati++)
20644 {
20645 read_unsigned_leb128 (abfd, buf, &bytes_read);
20646 buf += bytes_read;
20647 read_unsigned_leb128 (abfd, buf, &bytes_read);
20648 buf += bytes_read;
20649 }
20650
20651 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
20652 buf += bytes_read;
20653 for (datai = 0; datai < data_count; datai++)
20654 {
20655 const gdb_byte *format = format_header_data;
20656 struct file_entry fe;
20657
20658 for (formati = 0; formati < format_count; formati++)
20659 {
20660 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
20661 format += bytes_read;
20662
20663 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
20664 format += bytes_read;
20665
20666 gdb::optional<const char *> string;
20667 gdb::optional<unsigned int> uint;
20668
20669 switch (form)
20670 {
20671 case DW_FORM_string:
20672 string.emplace (read_direct_string (abfd, buf, &bytes_read));
20673 buf += bytes_read;
20674 break;
20675
20676 case DW_FORM_line_strp:
20677 string.emplace (read_indirect_line_string (dwarf2_per_objfile,
20678 abfd, buf,
20679 cu_header,
20680 &bytes_read));
20681 buf += bytes_read;
20682 break;
20683
20684 case DW_FORM_data1:
20685 uint.emplace (read_1_byte (abfd, buf));
20686 buf += 1;
20687 break;
20688
20689 case DW_FORM_data2:
20690 uint.emplace (read_2_bytes (abfd, buf));
20691 buf += 2;
20692 break;
20693
20694 case DW_FORM_data4:
20695 uint.emplace (read_4_bytes (abfd, buf));
20696 buf += 4;
20697 break;
20698
20699 case DW_FORM_data8:
20700 uint.emplace (read_8_bytes (abfd, buf));
20701 buf += 8;
20702 break;
20703
20704 case DW_FORM_data16:
20705 /* This is used for MD5, but file_entry does not record MD5s. */
20706 buf += 16;
20707 break;
20708
20709 case DW_FORM_udata:
20710 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
20711 buf += bytes_read;
20712 break;
20713
20714 case DW_FORM_block:
20715 /* It is valid only for DW_LNCT_timestamp which is ignored by
20716 current GDB. */
20717 break;
20718 }
20719
20720 switch (content_type)
20721 {
20722 case DW_LNCT_path:
20723 if (string.has_value ())
20724 fe.name = *string;
20725 break;
20726 case DW_LNCT_directory_index:
20727 if (uint.has_value ())
20728 fe.d_index = (dir_index) *uint;
20729 break;
20730 case DW_LNCT_timestamp:
20731 if (uint.has_value ())
20732 fe.mod_time = *uint;
20733 break;
20734 case DW_LNCT_size:
20735 if (uint.has_value ())
20736 fe.length = *uint;
20737 break;
20738 case DW_LNCT_MD5:
20739 break;
20740 default:
20741 complaint (_("Unknown format content type %s"),
20742 pulongest (content_type));
20743 }
20744 }
20745
20746 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
20747 }
20748
20749 *bufp = buf;
20750 }
20751
20752 /* Read the statement program header starting at OFFSET in
20753 .debug_line, or .debug_line.dwo. Return a pointer
20754 to a struct line_header, allocated using xmalloc.
20755 Returns NULL if there is a problem reading the header, e.g., if it
20756 has a version we don't understand.
20757
20758 NOTE: the strings in the include directory and file name tables of
20759 the returned object point into the dwarf line section buffer,
20760 and must not be freed. */
20761
20762 static line_header_up
20763 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
20764 {
20765 const gdb_byte *line_ptr;
20766 unsigned int bytes_read, offset_size;
20767 int i;
20768 const char *cur_dir, *cur_file;
20769 struct dwarf2_section_info *section;
20770 bfd *abfd;
20771 struct dwarf2_per_objfile *dwarf2_per_objfile
20772 = cu->per_cu->dwarf2_per_objfile;
20773
20774 section = get_debug_line_section (cu);
20775 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
20776 if (section->buffer == NULL)
20777 {
20778 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20779 complaint (_("missing .debug_line.dwo section"));
20780 else
20781 complaint (_("missing .debug_line section"));
20782 return 0;
20783 }
20784
20785 /* We can't do this until we know the section is non-empty.
20786 Only then do we know we have such a section. */
20787 abfd = get_section_bfd_owner (section);
20788
20789 /* Make sure that at least there's room for the total_length field.
20790 That could be 12 bytes long, but we're just going to fudge that. */
20791 if (to_underlying (sect_off) + 4 >= section->size)
20792 {
20793 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20794 return 0;
20795 }
20796
20797 line_header_up lh (new line_header ());
20798
20799 lh->sect_off = sect_off;
20800 lh->offset_in_dwz = cu->per_cu->is_dwz;
20801
20802 line_ptr = section->buffer + to_underlying (sect_off);
20803
20804 /* Read in the header. */
20805 lh->total_length =
20806 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
20807 &bytes_read, &offset_size);
20808 line_ptr += bytes_read;
20809
20810 const gdb_byte *start_here = line_ptr;
20811
20812 if (line_ptr + lh->total_length > (section->buffer + section->size))
20813 {
20814 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20815 return 0;
20816 }
20817 lh->statement_program_end = start_here + lh->total_length;
20818 lh->version = read_2_bytes (abfd, line_ptr);
20819 line_ptr += 2;
20820 if (lh->version > 5)
20821 {
20822 /* This is a version we don't understand. The format could have
20823 changed in ways we don't handle properly so just punt. */
20824 complaint (_("unsupported version in .debug_line section"));
20825 return NULL;
20826 }
20827 if (lh->version >= 5)
20828 {
20829 gdb_byte segment_selector_size;
20830
20831 /* Skip address size. */
20832 read_1_byte (abfd, line_ptr);
20833 line_ptr += 1;
20834
20835 segment_selector_size = read_1_byte (abfd, line_ptr);
20836 line_ptr += 1;
20837 if (segment_selector_size != 0)
20838 {
20839 complaint (_("unsupported segment selector size %u "
20840 "in .debug_line section"),
20841 segment_selector_size);
20842 return NULL;
20843 }
20844 }
20845 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
20846 line_ptr += offset_size;
20847 lh->statement_program_start = line_ptr + lh->header_length;
20848 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
20849 line_ptr += 1;
20850 if (lh->version >= 4)
20851 {
20852 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
20853 line_ptr += 1;
20854 }
20855 else
20856 lh->maximum_ops_per_instruction = 1;
20857
20858 if (lh->maximum_ops_per_instruction == 0)
20859 {
20860 lh->maximum_ops_per_instruction = 1;
20861 complaint (_("invalid maximum_ops_per_instruction "
20862 "in `.debug_line' section"));
20863 }
20864
20865 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
20866 line_ptr += 1;
20867 lh->line_base = read_1_signed_byte (abfd, line_ptr);
20868 line_ptr += 1;
20869 lh->line_range = read_1_byte (abfd, line_ptr);
20870 line_ptr += 1;
20871 lh->opcode_base = read_1_byte (abfd, line_ptr);
20872 line_ptr += 1;
20873 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
20874
20875 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
20876 for (i = 1; i < lh->opcode_base; ++i)
20877 {
20878 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
20879 line_ptr += 1;
20880 }
20881
20882 if (lh->version >= 5)
20883 {
20884 /* Read directory table. */
20885 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20886 &cu->header,
20887 [] (struct line_header *header, const char *name,
20888 dir_index d_index, unsigned int mod_time,
20889 unsigned int length)
20890 {
20891 header->add_include_dir (name);
20892 });
20893
20894 /* Read file name table. */
20895 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20896 &cu->header,
20897 [] (struct line_header *header, const char *name,
20898 dir_index d_index, unsigned int mod_time,
20899 unsigned int length)
20900 {
20901 header->add_file_name (name, d_index, mod_time, length);
20902 });
20903 }
20904 else
20905 {
20906 /* Read directory table. */
20907 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20908 {
20909 line_ptr += bytes_read;
20910 lh->add_include_dir (cur_dir);
20911 }
20912 line_ptr += bytes_read;
20913
20914 /* Read file name table. */
20915 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20916 {
20917 unsigned int mod_time, length;
20918 dir_index d_index;
20919
20920 line_ptr += bytes_read;
20921 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20922 line_ptr += bytes_read;
20923 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20924 line_ptr += bytes_read;
20925 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20926 line_ptr += bytes_read;
20927
20928 lh->add_file_name (cur_file, d_index, mod_time, length);
20929 }
20930 line_ptr += bytes_read;
20931 }
20932
20933 if (line_ptr > (section->buffer + section->size))
20934 complaint (_("line number info header doesn't "
20935 "fit in `.debug_line' section"));
20936
20937 return lh;
20938 }
20939
20940 /* Subroutine of dwarf_decode_lines to simplify it.
20941 Return the file name of the psymtab for the given file_entry.
20942 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20943 If space for the result is malloc'd, *NAME_HOLDER will be set.
20944 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
20945
20946 static const char *
20947 psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
20948 const struct partial_symtab *pst,
20949 const char *comp_dir,
20950 gdb::unique_xmalloc_ptr<char> *name_holder)
20951 {
20952 const char *include_name = fe.name;
20953 const char *include_name_to_compare = include_name;
20954 const char *pst_filename;
20955 int file_is_pst;
20956
20957 const char *dir_name = fe.include_dir (lh);
20958
20959 gdb::unique_xmalloc_ptr<char> hold_compare;
20960 if (!IS_ABSOLUTE_PATH (include_name)
20961 && (dir_name != NULL || comp_dir != NULL))
20962 {
20963 /* Avoid creating a duplicate psymtab for PST.
20964 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20965 Before we do the comparison, however, we need to account
20966 for DIR_NAME and COMP_DIR.
20967 First prepend dir_name (if non-NULL). If we still don't
20968 have an absolute path prepend comp_dir (if non-NULL).
20969 However, the directory we record in the include-file's
20970 psymtab does not contain COMP_DIR (to match the
20971 corresponding symtab(s)).
20972
20973 Example:
20974
20975 bash$ cd /tmp
20976 bash$ gcc -g ./hello.c
20977 include_name = "hello.c"
20978 dir_name = "."
20979 DW_AT_comp_dir = comp_dir = "/tmp"
20980 DW_AT_name = "./hello.c"
20981
20982 */
20983
20984 if (dir_name != NULL)
20985 {
20986 name_holder->reset (concat (dir_name, SLASH_STRING,
20987 include_name, (char *) NULL));
20988 include_name = name_holder->get ();
20989 include_name_to_compare = include_name;
20990 }
20991 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20992 {
20993 hold_compare.reset (concat (comp_dir, SLASH_STRING,
20994 include_name, (char *) NULL));
20995 include_name_to_compare = hold_compare.get ();
20996 }
20997 }
20998
20999 pst_filename = pst->filename;
21000 gdb::unique_xmalloc_ptr<char> copied_name;
21001 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
21002 {
21003 copied_name.reset (concat (pst->dirname, SLASH_STRING,
21004 pst_filename, (char *) NULL));
21005 pst_filename = copied_name.get ();
21006 }
21007
21008 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
21009
21010 if (file_is_pst)
21011 return NULL;
21012 return include_name;
21013 }
21014
21015 /* State machine to track the state of the line number program. */
21016
21017 class lnp_state_machine
21018 {
21019 public:
21020 /* Initialize a machine state for the start of a line number
21021 program. */
21022 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
21023 bool record_lines_p);
21024
21025 file_entry *current_file ()
21026 {
21027 /* lh->file_names is 0-based, but the file name numbers in the
21028 statement program are 1-based. */
21029 return m_line_header->file_name_at (m_file);
21030 }
21031
21032 /* Record the line in the state machine. END_SEQUENCE is true if
21033 we're processing the end of a sequence. */
21034 void record_line (bool end_sequence);
21035
21036 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
21037 nop-out rest of the lines in this sequence. */
21038 void check_line_address (struct dwarf2_cu *cu,
21039 const gdb_byte *line_ptr,
21040 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
21041
21042 void handle_set_discriminator (unsigned int discriminator)
21043 {
21044 m_discriminator = discriminator;
21045 m_line_has_non_zero_discriminator |= discriminator != 0;
21046 }
21047
21048 /* Handle DW_LNE_set_address. */
21049 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
21050 {
21051 m_op_index = 0;
21052 address += baseaddr;
21053 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
21054 }
21055
21056 /* Handle DW_LNS_advance_pc. */
21057 void handle_advance_pc (CORE_ADDR adjust);
21058
21059 /* Handle a special opcode. */
21060 void handle_special_opcode (unsigned char op_code);
21061
21062 /* Handle DW_LNS_advance_line. */
21063 void handle_advance_line (int line_delta)
21064 {
21065 advance_line (line_delta);
21066 }
21067
21068 /* Handle DW_LNS_set_file. */
21069 void handle_set_file (file_name_index file);
21070
21071 /* Handle DW_LNS_negate_stmt. */
21072 void handle_negate_stmt ()
21073 {
21074 m_is_stmt = !m_is_stmt;
21075 }
21076
21077 /* Handle DW_LNS_const_add_pc. */
21078 void handle_const_add_pc ();
21079
21080 /* Handle DW_LNS_fixed_advance_pc. */
21081 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
21082 {
21083 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21084 m_op_index = 0;
21085 }
21086
21087 /* Handle DW_LNS_copy. */
21088 void handle_copy ()
21089 {
21090 record_line (false);
21091 m_discriminator = 0;
21092 }
21093
21094 /* Handle DW_LNE_end_sequence. */
21095 void handle_end_sequence ()
21096 {
21097 m_currently_recording_lines = true;
21098 }
21099
21100 private:
21101 /* Advance the line by LINE_DELTA. */
21102 void advance_line (int line_delta)
21103 {
21104 m_line += line_delta;
21105
21106 if (line_delta != 0)
21107 m_line_has_non_zero_discriminator = m_discriminator != 0;
21108 }
21109
21110 struct dwarf2_cu *m_cu;
21111
21112 gdbarch *m_gdbarch;
21113
21114 /* True if we're recording lines.
21115 Otherwise we're building partial symtabs and are just interested in
21116 finding include files mentioned by the line number program. */
21117 bool m_record_lines_p;
21118
21119 /* The line number header. */
21120 line_header *m_line_header;
21121
21122 /* These are part of the standard DWARF line number state machine,
21123 and initialized according to the DWARF spec. */
21124
21125 unsigned char m_op_index = 0;
21126 /* The line table index of the current file. */
21127 file_name_index m_file = 1;
21128 unsigned int m_line = 1;
21129
21130 /* These are initialized in the constructor. */
21131
21132 CORE_ADDR m_address;
21133 bool m_is_stmt;
21134 unsigned int m_discriminator;
21135
21136 /* Additional bits of state we need to track. */
21137
21138 /* The last file that we called dwarf2_start_subfile for.
21139 This is only used for TLLs. */
21140 unsigned int m_last_file = 0;
21141 /* The last file a line number was recorded for. */
21142 struct subfile *m_last_subfile = NULL;
21143
21144 /* When true, record the lines we decode. */
21145 bool m_currently_recording_lines = false;
21146
21147 /* The last line number that was recorded, used to coalesce
21148 consecutive entries for the same line. This can happen, for
21149 example, when discriminators are present. PR 17276. */
21150 unsigned int m_last_line = 0;
21151 bool m_line_has_non_zero_discriminator = false;
21152 };
21153
21154 void
21155 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
21156 {
21157 CORE_ADDR addr_adj = (((m_op_index + adjust)
21158 / m_line_header->maximum_ops_per_instruction)
21159 * m_line_header->minimum_instruction_length);
21160 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21161 m_op_index = ((m_op_index + adjust)
21162 % m_line_header->maximum_ops_per_instruction);
21163 }
21164
21165 void
21166 lnp_state_machine::handle_special_opcode (unsigned char op_code)
21167 {
21168 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
21169 CORE_ADDR addr_adj = (((m_op_index
21170 + (adj_opcode / m_line_header->line_range))
21171 / m_line_header->maximum_ops_per_instruction)
21172 * m_line_header->minimum_instruction_length);
21173 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21174 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
21175 % m_line_header->maximum_ops_per_instruction);
21176
21177 int line_delta = (m_line_header->line_base
21178 + (adj_opcode % m_line_header->line_range));
21179 advance_line (line_delta);
21180 record_line (false);
21181 m_discriminator = 0;
21182 }
21183
21184 void
21185 lnp_state_machine::handle_set_file (file_name_index file)
21186 {
21187 m_file = file;
21188
21189 const file_entry *fe = current_file ();
21190 if (fe == NULL)
21191 dwarf2_debug_line_missing_file_complaint ();
21192 else if (m_record_lines_p)
21193 {
21194 const char *dir = fe->include_dir (m_line_header);
21195
21196 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
21197 m_line_has_non_zero_discriminator = m_discriminator != 0;
21198 dwarf2_start_subfile (m_cu, fe->name, dir);
21199 }
21200 }
21201
21202 void
21203 lnp_state_machine::handle_const_add_pc ()
21204 {
21205 CORE_ADDR adjust
21206 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
21207
21208 CORE_ADDR addr_adj
21209 = (((m_op_index + adjust)
21210 / m_line_header->maximum_ops_per_instruction)
21211 * m_line_header->minimum_instruction_length);
21212
21213 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21214 m_op_index = ((m_op_index + adjust)
21215 % m_line_header->maximum_ops_per_instruction);
21216 }
21217
21218 /* Return non-zero if we should add LINE to the line number table.
21219 LINE is the line to add, LAST_LINE is the last line that was added,
21220 LAST_SUBFILE is the subfile for LAST_LINE.
21221 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
21222 had a non-zero discriminator.
21223
21224 We have to be careful in the presence of discriminators.
21225 E.g., for this line:
21226
21227 for (i = 0; i < 100000; i++);
21228
21229 clang can emit four line number entries for that one line,
21230 each with a different discriminator.
21231 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
21232
21233 However, we want gdb to coalesce all four entries into one.
21234 Otherwise the user could stepi into the middle of the line and
21235 gdb would get confused about whether the pc really was in the
21236 middle of the line.
21237
21238 Things are further complicated by the fact that two consecutive
21239 line number entries for the same line is a heuristic used by gcc
21240 to denote the end of the prologue. So we can't just discard duplicate
21241 entries, we have to be selective about it. The heuristic we use is
21242 that we only collapse consecutive entries for the same line if at least
21243 one of those entries has a non-zero discriminator. PR 17276.
21244
21245 Note: Addresses in the line number state machine can never go backwards
21246 within one sequence, thus this coalescing is ok. */
21247
21248 static int
21249 dwarf_record_line_p (struct dwarf2_cu *cu,
21250 unsigned int line, unsigned int last_line,
21251 int line_has_non_zero_discriminator,
21252 struct subfile *last_subfile)
21253 {
21254 if (cu->get_builder ()->get_current_subfile () != last_subfile)
21255 return 1;
21256 if (line != last_line)
21257 return 1;
21258 /* Same line for the same file that we've seen already.
21259 As a last check, for pr 17276, only record the line if the line
21260 has never had a non-zero discriminator. */
21261 if (!line_has_non_zero_discriminator)
21262 return 1;
21263 return 0;
21264 }
21265
21266 /* Use the CU's builder to record line number LINE beginning at
21267 address ADDRESS in the line table of subfile SUBFILE. */
21268
21269 static void
21270 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
21271 unsigned int line, CORE_ADDR address,
21272 struct dwarf2_cu *cu)
21273 {
21274 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
21275
21276 if (dwarf_line_debug)
21277 {
21278 fprintf_unfiltered (gdb_stdlog,
21279 "Recording line %u, file %s, address %s\n",
21280 line, lbasename (subfile->name),
21281 paddress (gdbarch, address));
21282 }
21283
21284 if (cu != nullptr)
21285 cu->get_builder ()->record_line (subfile, line, addr);
21286 }
21287
21288 /* Subroutine of dwarf_decode_lines_1 to simplify it.
21289 Mark the end of a set of line number records.
21290 The arguments are the same as for dwarf_record_line_1.
21291 If SUBFILE is NULL the request is ignored. */
21292
21293 static void
21294 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
21295 CORE_ADDR address, struct dwarf2_cu *cu)
21296 {
21297 if (subfile == NULL)
21298 return;
21299
21300 if (dwarf_line_debug)
21301 {
21302 fprintf_unfiltered (gdb_stdlog,
21303 "Finishing current line, file %s, address %s\n",
21304 lbasename (subfile->name),
21305 paddress (gdbarch, address));
21306 }
21307
21308 dwarf_record_line_1 (gdbarch, subfile, 0, address, cu);
21309 }
21310
21311 void
21312 lnp_state_machine::record_line (bool end_sequence)
21313 {
21314 if (dwarf_line_debug)
21315 {
21316 fprintf_unfiltered (gdb_stdlog,
21317 "Processing actual line %u: file %u,"
21318 " address %s, is_stmt %u, discrim %u\n",
21319 m_line, m_file,
21320 paddress (m_gdbarch, m_address),
21321 m_is_stmt, m_discriminator);
21322 }
21323
21324 file_entry *fe = current_file ();
21325
21326 if (fe == NULL)
21327 dwarf2_debug_line_missing_file_complaint ();
21328 /* For now we ignore lines not starting on an instruction boundary.
21329 But not when processing end_sequence for compatibility with the
21330 previous version of the code. */
21331 else if (m_op_index == 0 || end_sequence)
21332 {
21333 fe->included_p = 1;
21334 if (m_record_lines_p && (producer_is_codewarrior (m_cu) || m_is_stmt))
21335 {
21336 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
21337 || end_sequence)
21338 {
21339 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
21340 m_currently_recording_lines ? m_cu : nullptr);
21341 }
21342
21343 if (!end_sequence)
21344 {
21345 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
21346 m_line_has_non_zero_discriminator,
21347 m_last_subfile))
21348 {
21349 buildsym_compunit *builder = m_cu->get_builder ();
21350 dwarf_record_line_1 (m_gdbarch,
21351 builder->get_current_subfile (),
21352 m_line, m_address,
21353 m_currently_recording_lines ? m_cu : nullptr);
21354 }
21355 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
21356 m_last_line = m_line;
21357 }
21358 }
21359 }
21360 }
21361
21362 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
21363 line_header *lh, bool record_lines_p)
21364 {
21365 m_cu = cu;
21366 m_gdbarch = arch;
21367 m_record_lines_p = record_lines_p;
21368 m_line_header = lh;
21369
21370 m_currently_recording_lines = true;
21371
21372 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
21373 was a line entry for it so that the backend has a chance to adjust it
21374 and also record it in case it needs it. This is currently used by MIPS
21375 code, cf. `mips_adjust_dwarf2_line'. */
21376 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
21377 m_is_stmt = lh->default_is_stmt;
21378 m_discriminator = 0;
21379 }
21380
21381 void
21382 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
21383 const gdb_byte *line_ptr,
21384 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
21385 {
21386 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
21387 the pc range of the CU. However, we restrict the test to only ADDRESS
21388 values of zero to preserve GDB's previous behaviour which is to handle
21389 the specific case of a function being GC'd by the linker. */
21390
21391 if (address == 0 && address < unrelocated_lowpc)
21392 {
21393 /* This line table is for a function which has been
21394 GCd by the linker. Ignore it. PR gdb/12528 */
21395
21396 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21397 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
21398
21399 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
21400 line_offset, objfile_name (objfile));
21401 m_currently_recording_lines = false;
21402 /* Note: m_currently_recording_lines is left as false until we see
21403 DW_LNE_end_sequence. */
21404 }
21405 }
21406
21407 /* Subroutine of dwarf_decode_lines to simplify it.
21408 Process the line number information in LH.
21409 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
21410 program in order to set included_p for every referenced header. */
21411
21412 static void
21413 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
21414 const int decode_for_pst_p, CORE_ADDR lowpc)
21415 {
21416 const gdb_byte *line_ptr, *extended_end;
21417 const gdb_byte *line_end;
21418 unsigned int bytes_read, extended_len;
21419 unsigned char op_code, extended_op;
21420 CORE_ADDR baseaddr;
21421 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21422 bfd *abfd = objfile->obfd;
21423 struct gdbarch *gdbarch = get_objfile_arch (objfile);
21424 /* True if we're recording line info (as opposed to building partial
21425 symtabs and just interested in finding include files mentioned by
21426 the line number program). */
21427 bool record_lines_p = !decode_for_pst_p;
21428
21429 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
21430
21431 line_ptr = lh->statement_program_start;
21432 line_end = lh->statement_program_end;
21433
21434 /* Read the statement sequences until there's nothing left. */
21435 while (line_ptr < line_end)
21436 {
21437 /* The DWARF line number program state machine. Reset the state
21438 machine at the start of each sequence. */
21439 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
21440 bool end_sequence = false;
21441
21442 if (record_lines_p)
21443 {
21444 /* Start a subfile for the current file of the state
21445 machine. */
21446 const file_entry *fe = state_machine.current_file ();
21447
21448 if (fe != NULL)
21449 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
21450 }
21451
21452 /* Decode the table. */
21453 while (line_ptr < line_end && !end_sequence)
21454 {
21455 op_code = read_1_byte (abfd, line_ptr);
21456 line_ptr += 1;
21457
21458 if (op_code >= lh->opcode_base)
21459 {
21460 /* Special opcode. */
21461 state_machine.handle_special_opcode (op_code);
21462 }
21463 else switch (op_code)
21464 {
21465 case DW_LNS_extended_op:
21466 extended_len = read_unsigned_leb128 (abfd, line_ptr,
21467 &bytes_read);
21468 line_ptr += bytes_read;
21469 extended_end = line_ptr + extended_len;
21470 extended_op = read_1_byte (abfd, line_ptr);
21471 line_ptr += 1;
21472 switch (extended_op)
21473 {
21474 case DW_LNE_end_sequence:
21475 state_machine.handle_end_sequence ();
21476 end_sequence = true;
21477 break;
21478 case DW_LNE_set_address:
21479 {
21480 CORE_ADDR address
21481 = read_address (abfd, line_ptr, cu, &bytes_read);
21482 line_ptr += bytes_read;
21483
21484 state_machine.check_line_address (cu, line_ptr,
21485 lowpc - baseaddr, address);
21486 state_machine.handle_set_address (baseaddr, address);
21487 }
21488 break;
21489 case DW_LNE_define_file:
21490 {
21491 const char *cur_file;
21492 unsigned int mod_time, length;
21493 dir_index dindex;
21494
21495 cur_file = read_direct_string (abfd, line_ptr,
21496 &bytes_read);
21497 line_ptr += bytes_read;
21498 dindex = (dir_index)
21499 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21500 line_ptr += bytes_read;
21501 mod_time =
21502 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21503 line_ptr += bytes_read;
21504 length =
21505 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21506 line_ptr += bytes_read;
21507 lh->add_file_name (cur_file, dindex, mod_time, length);
21508 }
21509 break;
21510 case DW_LNE_set_discriminator:
21511 {
21512 /* The discriminator is not interesting to the
21513 debugger; just ignore it. We still need to
21514 check its value though:
21515 if there are consecutive entries for the same
21516 (non-prologue) line we want to coalesce them.
21517 PR 17276. */
21518 unsigned int discr
21519 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21520 line_ptr += bytes_read;
21521
21522 state_machine.handle_set_discriminator (discr);
21523 }
21524 break;
21525 default:
21526 complaint (_("mangled .debug_line section"));
21527 return;
21528 }
21529 /* Make sure that we parsed the extended op correctly. If e.g.
21530 we expected a different address size than the producer used,
21531 we may have read the wrong number of bytes. */
21532 if (line_ptr != extended_end)
21533 {
21534 complaint (_("mangled .debug_line section"));
21535 return;
21536 }
21537 break;
21538 case DW_LNS_copy:
21539 state_machine.handle_copy ();
21540 break;
21541 case DW_LNS_advance_pc:
21542 {
21543 CORE_ADDR adjust
21544 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21545 line_ptr += bytes_read;
21546
21547 state_machine.handle_advance_pc (adjust);
21548 }
21549 break;
21550 case DW_LNS_advance_line:
21551 {
21552 int line_delta
21553 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
21554 line_ptr += bytes_read;
21555
21556 state_machine.handle_advance_line (line_delta);
21557 }
21558 break;
21559 case DW_LNS_set_file:
21560 {
21561 file_name_index file
21562 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21563 &bytes_read);
21564 line_ptr += bytes_read;
21565
21566 state_machine.handle_set_file (file);
21567 }
21568 break;
21569 case DW_LNS_set_column:
21570 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21571 line_ptr += bytes_read;
21572 break;
21573 case DW_LNS_negate_stmt:
21574 state_machine.handle_negate_stmt ();
21575 break;
21576 case DW_LNS_set_basic_block:
21577 break;
21578 /* Add to the address register of the state machine the
21579 address increment value corresponding to special opcode
21580 255. I.e., this value is scaled by the minimum
21581 instruction length since special opcode 255 would have
21582 scaled the increment. */
21583 case DW_LNS_const_add_pc:
21584 state_machine.handle_const_add_pc ();
21585 break;
21586 case DW_LNS_fixed_advance_pc:
21587 {
21588 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
21589 line_ptr += 2;
21590
21591 state_machine.handle_fixed_advance_pc (addr_adj);
21592 }
21593 break;
21594 default:
21595 {
21596 /* Unknown standard opcode, ignore it. */
21597 int i;
21598
21599 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
21600 {
21601 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21602 line_ptr += bytes_read;
21603 }
21604 }
21605 }
21606 }
21607
21608 if (!end_sequence)
21609 dwarf2_debug_line_missing_end_sequence_complaint ();
21610
21611 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21612 in which case we still finish recording the last line). */
21613 state_machine.record_line (true);
21614 }
21615 }
21616
21617 /* Decode the Line Number Program (LNP) for the given line_header
21618 structure and CU. The actual information extracted and the type
21619 of structures created from the LNP depends on the value of PST.
21620
21621 1. If PST is NULL, then this procedure uses the data from the program
21622 to create all necessary symbol tables, and their linetables.
21623
21624 2. If PST is not NULL, this procedure reads the program to determine
21625 the list of files included by the unit represented by PST, and
21626 builds all the associated partial symbol tables.
21627
21628 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21629 It is used for relative paths in the line table.
21630 NOTE: When processing partial symtabs (pst != NULL),
21631 comp_dir == pst->dirname.
21632
21633 NOTE: It is important that psymtabs have the same file name (via strcmp)
21634 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21635 symtab we don't use it in the name of the psymtabs we create.
21636 E.g. expand_line_sal requires this when finding psymtabs to expand.
21637 A good testcase for this is mb-inline.exp.
21638
21639 LOWPC is the lowest address in CU (or 0 if not known).
21640
21641 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21642 for its PC<->lines mapping information. Otherwise only the filename
21643 table is read in. */
21644
21645 static void
21646 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
21647 struct dwarf2_cu *cu, struct partial_symtab *pst,
21648 CORE_ADDR lowpc, int decode_mapping)
21649 {
21650 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21651 const int decode_for_pst_p = (pst != NULL);
21652
21653 if (decode_mapping)
21654 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
21655
21656 if (decode_for_pst_p)
21657 {
21658 /* Now that we're done scanning the Line Header Program, we can
21659 create the psymtab of each included file. */
21660 for (auto &file_entry : lh->file_names ())
21661 if (file_entry.included_p == 1)
21662 {
21663 gdb::unique_xmalloc_ptr<char> name_holder;
21664 const char *include_name =
21665 psymtab_include_file_name (lh, file_entry, pst,
21666 comp_dir, &name_holder);
21667 if (include_name != NULL)
21668 dwarf2_create_include_psymtab (include_name, pst, objfile);
21669 }
21670 }
21671 else
21672 {
21673 /* Make sure a symtab is created for every file, even files
21674 which contain only variables (i.e. no code with associated
21675 line numbers). */
21676 buildsym_compunit *builder = cu->get_builder ();
21677 struct compunit_symtab *cust = builder->get_compunit_symtab ();
21678
21679 for (auto &fe : lh->file_names ())
21680 {
21681 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
21682 if (builder->get_current_subfile ()->symtab == NULL)
21683 {
21684 builder->get_current_subfile ()->symtab
21685 = allocate_symtab (cust,
21686 builder->get_current_subfile ()->name);
21687 }
21688 fe.symtab = builder->get_current_subfile ()->symtab;
21689 }
21690 }
21691 }
21692
21693 /* Start a subfile for DWARF. FILENAME is the name of the file and
21694 DIRNAME the name of the source directory which contains FILENAME
21695 or NULL if not known.
21696 This routine tries to keep line numbers from identical absolute and
21697 relative file names in a common subfile.
21698
21699 Using the `list' example from the GDB testsuite, which resides in
21700 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21701 of /srcdir/list0.c yields the following debugging information for list0.c:
21702
21703 DW_AT_name: /srcdir/list0.c
21704 DW_AT_comp_dir: /compdir
21705 files.files[0].name: list0.h
21706 files.files[0].dir: /srcdir
21707 files.files[1].name: list0.c
21708 files.files[1].dir: /srcdir
21709
21710 The line number information for list0.c has to end up in a single
21711 subfile, so that `break /srcdir/list0.c:1' works as expected.
21712 start_subfile will ensure that this happens provided that we pass the
21713 concatenation of files.files[1].dir and files.files[1].name as the
21714 subfile's name. */
21715
21716 static void
21717 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
21718 const char *dirname)
21719 {
21720 char *copy = NULL;
21721
21722 /* In order not to lose the line information directory,
21723 we concatenate it to the filename when it makes sense.
21724 Note that the Dwarf3 standard says (speaking of filenames in line
21725 information): ``The directory index is ignored for file names
21726 that represent full path names''. Thus ignoring dirname in the
21727 `else' branch below isn't an issue. */
21728
21729 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
21730 {
21731 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
21732 filename = copy;
21733 }
21734
21735 cu->get_builder ()->start_subfile (filename);
21736
21737 if (copy != NULL)
21738 xfree (copy);
21739 }
21740
21741 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
21742 buildsym_compunit constructor. */
21743
21744 struct compunit_symtab *
21745 dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
21746 CORE_ADDR low_pc)
21747 {
21748 gdb_assert (m_builder == nullptr);
21749
21750 m_builder.reset (new struct buildsym_compunit
21751 (per_cu->dwarf2_per_objfile->objfile,
21752 name, comp_dir, language, low_pc));
21753
21754 list_in_scope = get_builder ()->get_file_symbols ();
21755
21756 get_builder ()->record_debugformat (xstrprintf ("DWARF %d", this->header.version));
21757 get_builder ()->record_producer (producer);
21758
21759 processing_has_namespace_info = false;
21760
21761 return get_builder ()->get_compunit_symtab ();
21762 }
21763
21764 static void
21765 var_decode_location (struct attribute *attr, struct symbol *sym,
21766 struct dwarf2_cu *cu)
21767 {
21768 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21769 struct comp_unit_head *cu_header = &cu->header;
21770
21771 /* NOTE drow/2003-01-30: There used to be a comment and some special
21772 code here to turn a symbol with DW_AT_external and a
21773 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21774 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21775 with some versions of binutils) where shared libraries could have
21776 relocations against symbols in their debug information - the
21777 minimal symbol would have the right address, but the debug info
21778 would not. It's no longer necessary, because we will explicitly
21779 apply relocations when we read in the debug information now. */
21780
21781 /* A DW_AT_location attribute with no contents indicates that a
21782 variable has been optimized away. */
21783 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
21784 {
21785 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21786 return;
21787 }
21788
21789 /* Handle one degenerate form of location expression specially, to
21790 preserve GDB's previous behavior when section offsets are
21791 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
21792 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
21793
21794 if (attr_form_is_block (attr)
21795 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
21796 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
21797 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
21798 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
21799 && (DW_BLOCK (attr)->size
21800 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
21801 {
21802 unsigned int dummy;
21803
21804 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
21805 SET_SYMBOL_VALUE_ADDRESS (sym,
21806 read_address (objfile->obfd,
21807 DW_BLOCK (attr)->data + 1,
21808 cu, &dummy));
21809 else
21810 SET_SYMBOL_VALUE_ADDRESS
21811 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
21812 &dummy));
21813 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
21814 fixup_symbol_section (sym, objfile);
21815 SET_SYMBOL_VALUE_ADDRESS (sym,
21816 SYMBOL_VALUE_ADDRESS (sym)
21817 + ANOFFSET (objfile->section_offsets,
21818 SYMBOL_SECTION (sym)));
21819 return;
21820 }
21821
21822 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21823 expression evaluator, and use LOC_COMPUTED only when necessary
21824 (i.e. when the value of a register or memory location is
21825 referenced, or a thread-local block, etc.). Then again, it might
21826 not be worthwhile. I'm assuming that it isn't unless performance
21827 or memory numbers show me otherwise. */
21828
21829 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
21830
21831 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
21832 cu->has_loclist = true;
21833 }
21834
21835 /* Given a pointer to a DWARF information entry, figure out if we need
21836 to make a symbol table entry for it, and if so, create a new entry
21837 and return a pointer to it.
21838 If TYPE is NULL, determine symbol type from the die, otherwise
21839 used the passed type.
21840 If SPACE is not NULL, use it to hold the new symbol. If it is
21841 NULL, allocate a new symbol on the objfile's obstack. */
21842
21843 static struct symbol *
21844 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21845 struct symbol *space)
21846 {
21847 struct dwarf2_per_objfile *dwarf2_per_objfile
21848 = cu->per_cu->dwarf2_per_objfile;
21849 struct objfile *objfile = dwarf2_per_objfile->objfile;
21850 struct gdbarch *gdbarch = get_objfile_arch (objfile);
21851 struct symbol *sym = NULL;
21852 const char *name;
21853 struct attribute *attr = NULL;
21854 struct attribute *attr2 = NULL;
21855 CORE_ADDR baseaddr;
21856 struct pending **list_to_add = NULL;
21857
21858 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
21859
21860 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
21861
21862 name = dwarf2_name (die, cu);
21863 if (name)
21864 {
21865 const char *linkagename;
21866 int suppress_add = 0;
21867
21868 if (space)
21869 sym = space;
21870 else
21871 sym = allocate_symbol (objfile);
21872 OBJSTAT (objfile, n_syms++);
21873
21874 /* Cache this symbol's name and the name's demangled form (if any). */
21875 sym->set_language (cu->language, &objfile->objfile_obstack);
21876 linkagename = dwarf2_physname (name, die, cu);
21877 sym->compute_and_set_names (linkagename, false, objfile->per_bfd);
21878
21879 /* Fortran does not have mangling standard and the mangling does differ
21880 between gfortran, iFort etc. */
21881 if (cu->language == language_fortran
21882 && symbol_get_demangled_name (sym) == NULL)
21883 symbol_set_demangled_name (sym,
21884 dwarf2_full_name (name, die, cu),
21885 NULL);
21886
21887 /* Default assumptions.
21888 Use the passed type or decode it from the die. */
21889 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21890 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21891 if (type != NULL)
21892 SYMBOL_TYPE (sym) = type;
21893 else
21894 SYMBOL_TYPE (sym) = die_type (die, cu);
21895 attr = dwarf2_attr (die,
21896 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21897 cu);
21898 if (attr != nullptr)
21899 {
21900 SYMBOL_LINE (sym) = DW_UNSND (attr);
21901 }
21902
21903 attr = dwarf2_attr (die,
21904 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21905 cu);
21906 if (attr != nullptr)
21907 {
21908 file_name_index file_index = (file_name_index) DW_UNSND (attr);
21909 struct file_entry *fe;
21910
21911 if (cu->line_header != NULL)
21912 fe = cu->line_header->file_name_at (file_index);
21913 else
21914 fe = NULL;
21915
21916 if (fe == NULL)
21917 complaint (_("file index out of range"));
21918 else
21919 symbol_set_symtab (sym, fe->symtab);
21920 }
21921
21922 switch (die->tag)
21923 {
21924 case DW_TAG_label:
21925 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
21926 if (attr != nullptr)
21927 {
21928 CORE_ADDR addr;
21929
21930 addr = attr_value_as_address (attr);
21931 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
21932 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
21933 }
21934 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21935 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
21936 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
21937 add_symbol_to_list (sym, cu->list_in_scope);
21938 break;
21939 case DW_TAG_subprogram:
21940 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21941 finish_block. */
21942 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21943 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21944 if ((attr2 && (DW_UNSND (attr2) != 0))
21945 || cu->language == language_ada
21946 || cu->language == language_fortran)
21947 {
21948 /* Subprograms marked external are stored as a global symbol.
21949 Ada and Fortran subprograms, whether marked external or
21950 not, are always stored as a global symbol, because we want
21951 to be able to access them globally. For instance, we want
21952 to be able to break on a nested subprogram without having
21953 to specify the context. */
21954 list_to_add = cu->get_builder ()->get_global_symbols ();
21955 }
21956 else
21957 {
21958 list_to_add = cu->list_in_scope;
21959 }
21960 break;
21961 case DW_TAG_inlined_subroutine:
21962 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21963 finish_block. */
21964 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21965 SYMBOL_INLINED (sym) = 1;
21966 list_to_add = cu->list_in_scope;
21967 break;
21968 case DW_TAG_template_value_param:
21969 suppress_add = 1;
21970 /* Fall through. */
21971 case DW_TAG_constant:
21972 case DW_TAG_variable:
21973 case DW_TAG_member:
21974 /* Compilation with minimal debug info may result in
21975 variables with missing type entries. Change the
21976 misleading `void' type to something sensible. */
21977 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
21978 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
21979
21980 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21981 /* In the case of DW_TAG_member, we should only be called for
21982 static const members. */
21983 if (die->tag == DW_TAG_member)
21984 {
21985 /* dwarf2_add_field uses die_is_declaration,
21986 so we do the same. */
21987 gdb_assert (die_is_declaration (die, cu));
21988 gdb_assert (attr);
21989 }
21990 if (attr != nullptr)
21991 {
21992 dwarf2_const_value (attr, sym, cu);
21993 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21994 if (!suppress_add)
21995 {
21996 if (attr2 && (DW_UNSND (attr2) != 0))
21997 list_to_add = cu->get_builder ()->get_global_symbols ();
21998 else
21999 list_to_add = cu->list_in_scope;
22000 }
22001 break;
22002 }
22003 attr = dwarf2_attr (die, DW_AT_location, cu);
22004 if (attr != nullptr)
22005 {
22006 var_decode_location (attr, sym, cu);
22007 attr2 = dwarf2_attr (die, DW_AT_external, cu);
22008
22009 /* Fortran explicitly imports any global symbols to the local
22010 scope by DW_TAG_common_block. */
22011 if (cu->language == language_fortran && die->parent
22012 && die->parent->tag == DW_TAG_common_block)
22013 attr2 = NULL;
22014
22015 if (SYMBOL_CLASS (sym) == LOC_STATIC
22016 && SYMBOL_VALUE_ADDRESS (sym) == 0
22017 && !dwarf2_per_objfile->has_section_at_zero)
22018 {
22019 /* When a static variable is eliminated by the linker,
22020 the corresponding debug information is not stripped
22021 out, but the variable address is set to null;
22022 do not add such variables into symbol table. */
22023 }
22024 else if (attr2 && (DW_UNSND (attr2) != 0))
22025 {
22026 if (SYMBOL_CLASS (sym) == LOC_STATIC
22027 && (objfile->flags & OBJF_MAINLINE) == 0
22028 && dwarf2_per_objfile->can_copy)
22029 {
22030 /* A global static variable might be subject to
22031 copy relocation. We first check for a local
22032 minsym, though, because maybe the symbol was
22033 marked hidden, in which case this would not
22034 apply. */
22035 bound_minimal_symbol found
22036 = (lookup_minimal_symbol_linkage
22037 (sym->linkage_name (), objfile));
22038 if (found.minsym != nullptr)
22039 sym->maybe_copied = 1;
22040 }
22041
22042 /* A variable with DW_AT_external is never static,
22043 but it may be block-scoped. */
22044 list_to_add
22045 = ((cu->list_in_scope
22046 == cu->get_builder ()->get_file_symbols ())
22047 ? cu->get_builder ()->get_global_symbols ()
22048 : cu->list_in_scope);
22049 }
22050 else
22051 list_to_add = cu->list_in_scope;
22052 }
22053 else
22054 {
22055 /* We do not know the address of this symbol.
22056 If it is an external symbol and we have type information
22057 for it, enter the symbol as a LOC_UNRESOLVED symbol.
22058 The address of the variable will then be determined from
22059 the minimal symbol table whenever the variable is
22060 referenced. */
22061 attr2 = dwarf2_attr (die, DW_AT_external, cu);
22062
22063 /* Fortran explicitly imports any global symbols to the local
22064 scope by DW_TAG_common_block. */
22065 if (cu->language == language_fortran && die->parent
22066 && die->parent->tag == DW_TAG_common_block)
22067 {
22068 /* SYMBOL_CLASS doesn't matter here because
22069 read_common_block is going to reset it. */
22070 if (!suppress_add)
22071 list_to_add = cu->list_in_scope;
22072 }
22073 else if (attr2 && (DW_UNSND (attr2) != 0)
22074 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
22075 {
22076 /* A variable with DW_AT_external is never static, but it
22077 may be block-scoped. */
22078 list_to_add
22079 = ((cu->list_in_scope
22080 == cu->get_builder ()->get_file_symbols ())
22081 ? cu->get_builder ()->get_global_symbols ()
22082 : cu->list_in_scope);
22083
22084 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
22085 }
22086 else if (!die_is_declaration (die, cu))
22087 {
22088 /* Use the default LOC_OPTIMIZED_OUT class. */
22089 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
22090 if (!suppress_add)
22091 list_to_add = cu->list_in_scope;
22092 }
22093 }
22094 break;
22095 case DW_TAG_formal_parameter:
22096 {
22097 /* If we are inside a function, mark this as an argument. If
22098 not, we might be looking at an argument to an inlined function
22099 when we do not have enough information to show inlined frames;
22100 pretend it's a local variable in that case so that the user can
22101 still see it. */
22102 struct context_stack *curr
22103 = cu->get_builder ()->get_current_context_stack ();
22104 if (curr != nullptr && curr->name != nullptr)
22105 SYMBOL_IS_ARGUMENT (sym) = 1;
22106 attr = dwarf2_attr (die, DW_AT_location, cu);
22107 if (attr != nullptr)
22108 {
22109 var_decode_location (attr, sym, cu);
22110 }
22111 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22112 if (attr != nullptr)
22113 {
22114 dwarf2_const_value (attr, sym, cu);
22115 }
22116
22117 list_to_add = cu->list_in_scope;
22118 }
22119 break;
22120 case DW_TAG_unspecified_parameters:
22121 /* From varargs functions; gdb doesn't seem to have any
22122 interest in this information, so just ignore it for now.
22123 (FIXME?) */
22124 break;
22125 case DW_TAG_template_type_param:
22126 suppress_add = 1;
22127 /* Fall through. */
22128 case DW_TAG_class_type:
22129 case DW_TAG_interface_type:
22130 case DW_TAG_structure_type:
22131 case DW_TAG_union_type:
22132 case DW_TAG_set_type:
22133 case DW_TAG_enumeration_type:
22134 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22135 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
22136
22137 {
22138 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
22139 really ever be static objects: otherwise, if you try
22140 to, say, break of a class's method and you're in a file
22141 which doesn't mention that class, it won't work unless
22142 the check for all static symbols in lookup_symbol_aux
22143 saves you. See the OtherFileClass tests in
22144 gdb.c++/namespace.exp. */
22145
22146 if (!suppress_add)
22147 {
22148 buildsym_compunit *builder = cu->get_builder ();
22149 list_to_add
22150 = (cu->list_in_scope == builder->get_file_symbols ()
22151 && cu->language == language_cplus
22152 ? builder->get_global_symbols ()
22153 : cu->list_in_scope);
22154
22155 /* The semantics of C++ state that "struct foo {
22156 ... }" also defines a typedef for "foo". */
22157 if (cu->language == language_cplus
22158 || cu->language == language_ada
22159 || cu->language == language_d
22160 || cu->language == language_rust)
22161 {
22162 /* The symbol's name is already allocated along
22163 with this objfile, so we don't need to
22164 duplicate it for the type. */
22165 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
22166 TYPE_NAME (SYMBOL_TYPE (sym)) = sym->search_name ();
22167 }
22168 }
22169 }
22170 break;
22171 case DW_TAG_typedef:
22172 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22173 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
22174 list_to_add = cu->list_in_scope;
22175 break;
22176 case DW_TAG_base_type:
22177 case DW_TAG_subrange_type:
22178 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22179 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
22180 list_to_add = cu->list_in_scope;
22181 break;
22182 case DW_TAG_enumerator:
22183 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22184 if (attr != nullptr)
22185 {
22186 dwarf2_const_value (attr, sym, cu);
22187 }
22188 {
22189 /* NOTE: carlton/2003-11-10: See comment above in the
22190 DW_TAG_class_type, etc. block. */
22191
22192 list_to_add
22193 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
22194 && cu->language == language_cplus
22195 ? cu->get_builder ()->get_global_symbols ()
22196 : cu->list_in_scope);
22197 }
22198 break;
22199 case DW_TAG_imported_declaration:
22200 case DW_TAG_namespace:
22201 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22202 list_to_add = cu->get_builder ()->get_global_symbols ();
22203 break;
22204 case DW_TAG_module:
22205 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22206 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
22207 list_to_add = cu->get_builder ()->get_global_symbols ();
22208 break;
22209 case DW_TAG_common_block:
22210 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
22211 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
22212 add_symbol_to_list (sym, cu->list_in_scope);
22213 break;
22214 default:
22215 /* Not a tag we recognize. Hopefully we aren't processing
22216 trash data, but since we must specifically ignore things
22217 we don't recognize, there is nothing else we should do at
22218 this point. */
22219 complaint (_("unsupported tag: '%s'"),
22220 dwarf_tag_name (die->tag));
22221 break;
22222 }
22223
22224 if (suppress_add)
22225 {
22226 sym->hash_next = objfile->template_symbols;
22227 objfile->template_symbols = sym;
22228 list_to_add = NULL;
22229 }
22230
22231 if (list_to_add != NULL)
22232 add_symbol_to_list (sym, list_to_add);
22233
22234 /* For the benefit of old versions of GCC, check for anonymous
22235 namespaces based on the demangled name. */
22236 if (!cu->processing_has_namespace_info
22237 && cu->language == language_cplus)
22238 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
22239 }
22240 return (sym);
22241 }
22242
22243 /* Given an attr with a DW_FORM_dataN value in host byte order,
22244 zero-extend it as appropriate for the symbol's type. The DWARF
22245 standard (v4) is not entirely clear about the meaning of using
22246 DW_FORM_dataN for a constant with a signed type, where the type is
22247 wider than the data. The conclusion of a discussion on the DWARF
22248 list was that this is unspecified. We choose to always zero-extend
22249 because that is the interpretation long in use by GCC. */
22250
22251 static gdb_byte *
22252 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
22253 struct dwarf2_cu *cu, LONGEST *value, int bits)
22254 {
22255 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22256 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
22257 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
22258 LONGEST l = DW_UNSND (attr);
22259
22260 if (bits < sizeof (*value) * 8)
22261 {
22262 l &= ((LONGEST) 1 << bits) - 1;
22263 *value = l;
22264 }
22265 else if (bits == sizeof (*value) * 8)
22266 *value = l;
22267 else
22268 {
22269 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
22270 store_unsigned_integer (bytes, bits / 8, byte_order, l);
22271 return bytes;
22272 }
22273
22274 return NULL;
22275 }
22276
22277 /* Read a constant value from an attribute. Either set *VALUE, or if
22278 the value does not fit in *VALUE, set *BYTES - either already
22279 allocated on the objfile obstack, or newly allocated on OBSTACK,
22280 or, set *BATON, if we translated the constant to a location
22281 expression. */
22282
22283 static void
22284 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
22285 const char *name, struct obstack *obstack,
22286 struct dwarf2_cu *cu,
22287 LONGEST *value, const gdb_byte **bytes,
22288 struct dwarf2_locexpr_baton **baton)
22289 {
22290 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22291 struct comp_unit_head *cu_header = &cu->header;
22292 struct dwarf_block *blk;
22293 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
22294 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22295
22296 *value = 0;
22297 *bytes = NULL;
22298 *baton = NULL;
22299
22300 switch (attr->form)
22301 {
22302 case DW_FORM_addr:
22303 case DW_FORM_addrx:
22304 case DW_FORM_GNU_addr_index:
22305 {
22306 gdb_byte *data;
22307
22308 if (TYPE_LENGTH (type) != cu_header->addr_size)
22309 dwarf2_const_value_length_mismatch_complaint (name,
22310 cu_header->addr_size,
22311 TYPE_LENGTH (type));
22312 /* Symbols of this form are reasonably rare, so we just
22313 piggyback on the existing location code rather than writing
22314 a new implementation of symbol_computed_ops. */
22315 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
22316 (*baton)->per_cu = cu->per_cu;
22317 gdb_assert ((*baton)->per_cu);
22318
22319 (*baton)->size = 2 + cu_header->addr_size;
22320 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
22321 (*baton)->data = data;
22322
22323 data[0] = DW_OP_addr;
22324 store_unsigned_integer (&data[1], cu_header->addr_size,
22325 byte_order, DW_ADDR (attr));
22326 data[cu_header->addr_size + 1] = DW_OP_stack_value;
22327 }
22328 break;
22329 case DW_FORM_string:
22330 case DW_FORM_strp:
22331 case DW_FORM_strx:
22332 case DW_FORM_GNU_str_index:
22333 case DW_FORM_GNU_strp_alt:
22334 /* DW_STRING is already allocated on the objfile obstack, point
22335 directly to it. */
22336 *bytes = (const gdb_byte *) DW_STRING (attr);
22337 break;
22338 case DW_FORM_block1:
22339 case DW_FORM_block2:
22340 case DW_FORM_block4:
22341 case DW_FORM_block:
22342 case DW_FORM_exprloc:
22343 case DW_FORM_data16:
22344 blk = DW_BLOCK (attr);
22345 if (TYPE_LENGTH (type) != blk->size)
22346 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
22347 TYPE_LENGTH (type));
22348 *bytes = blk->data;
22349 break;
22350
22351 /* The DW_AT_const_value attributes are supposed to carry the
22352 symbol's value "represented as it would be on the target
22353 architecture." By the time we get here, it's already been
22354 converted to host endianness, so we just need to sign- or
22355 zero-extend it as appropriate. */
22356 case DW_FORM_data1:
22357 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
22358 break;
22359 case DW_FORM_data2:
22360 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
22361 break;
22362 case DW_FORM_data4:
22363 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
22364 break;
22365 case DW_FORM_data8:
22366 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
22367 break;
22368
22369 case DW_FORM_sdata:
22370 case DW_FORM_implicit_const:
22371 *value = DW_SND (attr);
22372 break;
22373
22374 case DW_FORM_udata:
22375 *value = DW_UNSND (attr);
22376 break;
22377
22378 default:
22379 complaint (_("unsupported const value attribute form: '%s'"),
22380 dwarf_form_name (attr->form));
22381 *value = 0;
22382 break;
22383 }
22384 }
22385
22386
22387 /* Copy constant value from an attribute to a symbol. */
22388
22389 static void
22390 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
22391 struct dwarf2_cu *cu)
22392 {
22393 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22394 LONGEST value;
22395 const gdb_byte *bytes;
22396 struct dwarf2_locexpr_baton *baton;
22397
22398 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
22399 sym->print_name (),
22400 &objfile->objfile_obstack, cu,
22401 &value, &bytes, &baton);
22402
22403 if (baton != NULL)
22404 {
22405 SYMBOL_LOCATION_BATON (sym) = baton;
22406 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
22407 }
22408 else if (bytes != NULL)
22409 {
22410 SYMBOL_VALUE_BYTES (sym) = bytes;
22411 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
22412 }
22413 else
22414 {
22415 SYMBOL_VALUE (sym) = value;
22416 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
22417 }
22418 }
22419
22420 /* Return the type of the die in question using its DW_AT_type attribute. */
22421
22422 static struct type *
22423 die_type (struct die_info *die, struct dwarf2_cu *cu)
22424 {
22425 struct attribute *type_attr;
22426
22427 type_attr = dwarf2_attr (die, DW_AT_type, cu);
22428 if (!type_attr)
22429 {
22430 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22431 /* A missing DW_AT_type represents a void type. */
22432 return objfile_type (objfile)->builtin_void;
22433 }
22434
22435 return lookup_die_type (die, type_attr, cu);
22436 }
22437
22438 /* True iff CU's producer generates GNAT Ada auxiliary information
22439 that allows to find parallel types through that information instead
22440 of having to do expensive parallel lookups by type name. */
22441
22442 static int
22443 need_gnat_info (struct dwarf2_cu *cu)
22444 {
22445 /* Assume that the Ada compiler was GNAT, which always produces
22446 the auxiliary information. */
22447 return (cu->language == language_ada);
22448 }
22449
22450 /* Return the auxiliary type of the die in question using its
22451 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
22452 attribute is not present. */
22453
22454 static struct type *
22455 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
22456 {
22457 struct attribute *type_attr;
22458
22459 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
22460 if (!type_attr)
22461 return NULL;
22462
22463 return lookup_die_type (die, type_attr, cu);
22464 }
22465
22466 /* If DIE has a descriptive_type attribute, then set the TYPE's
22467 descriptive type accordingly. */
22468
22469 static void
22470 set_descriptive_type (struct type *type, struct die_info *die,
22471 struct dwarf2_cu *cu)
22472 {
22473 struct type *descriptive_type = die_descriptive_type (die, cu);
22474
22475 if (descriptive_type)
22476 {
22477 ALLOCATE_GNAT_AUX_TYPE (type);
22478 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
22479 }
22480 }
22481
22482 /* Return the containing type of the die in question using its
22483 DW_AT_containing_type attribute. */
22484
22485 static struct type *
22486 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
22487 {
22488 struct attribute *type_attr;
22489 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22490
22491 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
22492 if (!type_attr)
22493 error (_("Dwarf Error: Problem turning containing type into gdb type "
22494 "[in module %s]"), objfile_name (objfile));
22495
22496 return lookup_die_type (die, type_attr, cu);
22497 }
22498
22499 /* Return an error marker type to use for the ill formed type in DIE/CU. */
22500
22501 static struct type *
22502 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22503 {
22504 struct dwarf2_per_objfile *dwarf2_per_objfile
22505 = cu->per_cu->dwarf2_per_objfile;
22506 struct objfile *objfile = dwarf2_per_objfile->objfile;
22507 char *saved;
22508
22509 std::string message
22510 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
22511 objfile_name (objfile),
22512 sect_offset_str (cu->header.sect_off),
22513 sect_offset_str (die->sect_off));
22514 saved = obstack_strdup (&objfile->objfile_obstack, message);
22515
22516 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
22517 }
22518
22519 /* Look up the type of DIE in CU using its type attribute ATTR.
22520 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22521 DW_AT_containing_type.
22522 If there is no type substitute an error marker. */
22523
22524 static struct type *
22525 lookup_die_type (struct die_info *die, const struct attribute *attr,
22526 struct dwarf2_cu *cu)
22527 {
22528 struct dwarf2_per_objfile *dwarf2_per_objfile
22529 = cu->per_cu->dwarf2_per_objfile;
22530 struct objfile *objfile = dwarf2_per_objfile->objfile;
22531 struct type *this_type;
22532
22533 gdb_assert (attr->name == DW_AT_type
22534 || attr->name == DW_AT_GNAT_descriptive_type
22535 || attr->name == DW_AT_containing_type);
22536
22537 /* First see if we have it cached. */
22538
22539 if (attr->form == DW_FORM_GNU_ref_alt)
22540 {
22541 struct dwarf2_per_cu_data *per_cu;
22542 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
22543
22544 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
22545 dwarf2_per_objfile);
22546 this_type = get_die_type_at_offset (sect_off, per_cu);
22547 }
22548 else if (attr_form_is_ref (attr))
22549 {
22550 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
22551
22552 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
22553 }
22554 else if (attr->form == DW_FORM_ref_sig8)
22555 {
22556 ULONGEST signature = DW_SIGNATURE (attr);
22557
22558 return get_signatured_type (die, signature, cu);
22559 }
22560 else
22561 {
22562 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
22563 " at %s [in module %s]"),
22564 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
22565 objfile_name (objfile));
22566 return build_error_marker_type (cu, die);
22567 }
22568
22569 /* If not cached we need to read it in. */
22570
22571 if (this_type == NULL)
22572 {
22573 struct die_info *type_die = NULL;
22574 struct dwarf2_cu *type_cu = cu;
22575
22576 if (attr_form_is_ref (attr))
22577 type_die = follow_die_ref (die, attr, &type_cu);
22578 if (type_die == NULL)
22579 return build_error_marker_type (cu, die);
22580 /* If we find the type now, it's probably because the type came
22581 from an inter-CU reference and the type's CU got expanded before
22582 ours. */
22583 this_type = read_type_die (type_die, type_cu);
22584 }
22585
22586 /* If we still don't have a type use an error marker. */
22587
22588 if (this_type == NULL)
22589 return build_error_marker_type (cu, die);
22590
22591 return this_type;
22592 }
22593
22594 /* Return the type in DIE, CU.
22595 Returns NULL for invalid types.
22596
22597 This first does a lookup in die_type_hash,
22598 and only reads the die in if necessary.
22599
22600 NOTE: This can be called when reading in partial or full symbols. */
22601
22602 static struct type *
22603 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
22604 {
22605 struct type *this_type;
22606
22607 this_type = get_die_type (die, cu);
22608 if (this_type)
22609 return this_type;
22610
22611 return read_type_die_1 (die, cu);
22612 }
22613
22614 /* Read the type in DIE, CU.
22615 Returns NULL for invalid types. */
22616
22617 static struct type *
22618 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22619 {
22620 struct type *this_type = NULL;
22621
22622 switch (die->tag)
22623 {
22624 case DW_TAG_class_type:
22625 case DW_TAG_interface_type:
22626 case DW_TAG_structure_type:
22627 case DW_TAG_union_type:
22628 this_type = read_structure_type (die, cu);
22629 break;
22630 case DW_TAG_enumeration_type:
22631 this_type = read_enumeration_type (die, cu);
22632 break;
22633 case DW_TAG_subprogram:
22634 case DW_TAG_subroutine_type:
22635 case DW_TAG_inlined_subroutine:
22636 this_type = read_subroutine_type (die, cu);
22637 break;
22638 case DW_TAG_array_type:
22639 this_type = read_array_type (die, cu);
22640 break;
22641 case DW_TAG_set_type:
22642 this_type = read_set_type (die, cu);
22643 break;
22644 case DW_TAG_pointer_type:
22645 this_type = read_tag_pointer_type (die, cu);
22646 break;
22647 case DW_TAG_ptr_to_member_type:
22648 this_type = read_tag_ptr_to_member_type (die, cu);
22649 break;
22650 case DW_TAG_reference_type:
22651 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22652 break;
22653 case DW_TAG_rvalue_reference_type:
22654 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
22655 break;
22656 case DW_TAG_const_type:
22657 this_type = read_tag_const_type (die, cu);
22658 break;
22659 case DW_TAG_volatile_type:
22660 this_type = read_tag_volatile_type (die, cu);
22661 break;
22662 case DW_TAG_restrict_type:
22663 this_type = read_tag_restrict_type (die, cu);
22664 break;
22665 case DW_TAG_string_type:
22666 this_type = read_tag_string_type (die, cu);
22667 break;
22668 case DW_TAG_typedef:
22669 this_type = read_typedef (die, cu);
22670 break;
22671 case DW_TAG_subrange_type:
22672 this_type = read_subrange_type (die, cu);
22673 break;
22674 case DW_TAG_base_type:
22675 this_type = read_base_type (die, cu);
22676 break;
22677 case DW_TAG_unspecified_type:
22678 this_type = read_unspecified_type (die, cu);
22679 break;
22680 case DW_TAG_namespace:
22681 this_type = read_namespace_type (die, cu);
22682 break;
22683 case DW_TAG_module:
22684 this_type = read_module_type (die, cu);
22685 break;
22686 case DW_TAG_atomic_type:
22687 this_type = read_tag_atomic_type (die, cu);
22688 break;
22689 default:
22690 complaint (_("unexpected tag in read_type_die: '%s'"),
22691 dwarf_tag_name (die->tag));
22692 break;
22693 }
22694
22695 return this_type;
22696 }
22697
22698 /* See if we can figure out if the class lives in a namespace. We do
22699 this by looking for a member function; its demangled name will
22700 contain namespace info, if there is any.
22701 Return the computed name or NULL.
22702 Space for the result is allocated on the objfile's obstack.
22703 This is the full-die version of guess_partial_die_structure_name.
22704 In this case we know DIE has no useful parent. */
22705
22706 static char *
22707 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22708 {
22709 struct die_info *spec_die;
22710 struct dwarf2_cu *spec_cu;
22711 struct die_info *child;
22712 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22713
22714 spec_cu = cu;
22715 spec_die = die_specification (die, &spec_cu);
22716 if (spec_die != NULL)
22717 {
22718 die = spec_die;
22719 cu = spec_cu;
22720 }
22721
22722 for (child = die->child;
22723 child != NULL;
22724 child = child->sibling)
22725 {
22726 if (child->tag == DW_TAG_subprogram)
22727 {
22728 const char *linkage_name = dw2_linkage_name (child, cu);
22729
22730 if (linkage_name != NULL)
22731 {
22732 char *actual_name
22733 = language_class_name_from_physname (cu->language_defn,
22734 linkage_name);
22735 char *name = NULL;
22736
22737 if (actual_name != NULL)
22738 {
22739 const char *die_name = dwarf2_name (die, cu);
22740
22741 if (die_name != NULL
22742 && strcmp (die_name, actual_name) != 0)
22743 {
22744 /* Strip off the class name from the full name.
22745 We want the prefix. */
22746 int die_name_len = strlen (die_name);
22747 int actual_name_len = strlen (actual_name);
22748
22749 /* Test for '::' as a sanity check. */
22750 if (actual_name_len > die_name_len + 2
22751 && actual_name[actual_name_len
22752 - die_name_len - 1] == ':')
22753 name = obstack_strndup (
22754 &objfile->per_bfd->storage_obstack,
22755 actual_name, actual_name_len - die_name_len - 2);
22756 }
22757 }
22758 xfree (actual_name);
22759 return name;
22760 }
22761 }
22762 }
22763
22764 return NULL;
22765 }
22766
22767 /* GCC might emit a nameless typedef that has a linkage name. Determine the
22768 prefix part in such case. See
22769 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22770
22771 static const char *
22772 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22773 {
22774 struct attribute *attr;
22775 const char *base;
22776
22777 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22778 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22779 return NULL;
22780
22781 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
22782 return NULL;
22783
22784 attr = dw2_linkage_name_attr (die, cu);
22785 if (attr == NULL || DW_STRING (attr) == NULL)
22786 return NULL;
22787
22788 /* dwarf2_name had to be already called. */
22789 gdb_assert (DW_STRING_IS_CANONICAL (attr));
22790
22791 /* Strip the base name, keep any leading namespaces/classes. */
22792 base = strrchr (DW_STRING (attr), ':');
22793 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
22794 return "";
22795
22796 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22797 return obstack_strndup (&objfile->per_bfd->storage_obstack,
22798 DW_STRING (attr),
22799 &base[-1] - DW_STRING (attr));
22800 }
22801
22802 /* Return the name of the namespace/class that DIE is defined within,
22803 or "" if we can't tell. The caller should not xfree the result.
22804
22805 For example, if we're within the method foo() in the following
22806 code:
22807
22808 namespace N {
22809 class C {
22810 void foo () {
22811 }
22812 };
22813 }
22814
22815 then determine_prefix on foo's die will return "N::C". */
22816
22817 static const char *
22818 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
22819 {
22820 struct dwarf2_per_objfile *dwarf2_per_objfile
22821 = cu->per_cu->dwarf2_per_objfile;
22822 struct die_info *parent, *spec_die;
22823 struct dwarf2_cu *spec_cu;
22824 struct type *parent_type;
22825 const char *retval;
22826
22827 if (cu->language != language_cplus
22828 && cu->language != language_fortran && cu->language != language_d
22829 && cu->language != language_rust)
22830 return "";
22831
22832 retval = anonymous_struct_prefix (die, cu);
22833 if (retval)
22834 return retval;
22835
22836 /* We have to be careful in the presence of DW_AT_specification.
22837 For example, with GCC 3.4, given the code
22838
22839 namespace N {
22840 void foo() {
22841 // Definition of N::foo.
22842 }
22843 }
22844
22845 then we'll have a tree of DIEs like this:
22846
22847 1: DW_TAG_compile_unit
22848 2: DW_TAG_namespace // N
22849 3: DW_TAG_subprogram // declaration of N::foo
22850 4: DW_TAG_subprogram // definition of N::foo
22851 DW_AT_specification // refers to die #3
22852
22853 Thus, when processing die #4, we have to pretend that we're in
22854 the context of its DW_AT_specification, namely the contex of die
22855 #3. */
22856 spec_cu = cu;
22857 spec_die = die_specification (die, &spec_cu);
22858 if (spec_die == NULL)
22859 parent = die->parent;
22860 else
22861 {
22862 parent = spec_die->parent;
22863 cu = spec_cu;
22864 }
22865
22866 if (parent == NULL)
22867 return "";
22868 else if (parent->building_fullname)
22869 {
22870 const char *name;
22871 const char *parent_name;
22872
22873 /* It has been seen on RealView 2.2 built binaries,
22874 DW_TAG_template_type_param types actually _defined_ as
22875 children of the parent class:
22876
22877 enum E {};
22878 template class <class Enum> Class{};
22879 Class<enum E> class_e;
22880
22881 1: DW_TAG_class_type (Class)
22882 2: DW_TAG_enumeration_type (E)
22883 3: DW_TAG_enumerator (enum1:0)
22884 3: DW_TAG_enumerator (enum2:1)
22885 ...
22886 2: DW_TAG_template_type_param
22887 DW_AT_type DW_FORM_ref_udata (E)
22888
22889 Besides being broken debug info, it can put GDB into an
22890 infinite loop. Consider:
22891
22892 When we're building the full name for Class<E>, we'll start
22893 at Class, and go look over its template type parameters,
22894 finding E. We'll then try to build the full name of E, and
22895 reach here. We're now trying to build the full name of E,
22896 and look over the parent DIE for containing scope. In the
22897 broken case, if we followed the parent DIE of E, we'd again
22898 find Class, and once again go look at its template type
22899 arguments, etc., etc. Simply don't consider such parent die
22900 as source-level parent of this die (it can't be, the language
22901 doesn't allow it), and break the loop here. */
22902 name = dwarf2_name (die, cu);
22903 parent_name = dwarf2_name (parent, cu);
22904 complaint (_("template param type '%s' defined within parent '%s'"),
22905 name ? name : "<unknown>",
22906 parent_name ? parent_name : "<unknown>");
22907 return "";
22908 }
22909 else
22910 switch (parent->tag)
22911 {
22912 case DW_TAG_namespace:
22913 parent_type = read_type_die (parent, cu);
22914 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22915 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22916 Work around this problem here. */
22917 if (cu->language == language_cplus
22918 && strcmp (TYPE_NAME (parent_type), "::") == 0)
22919 return "";
22920 /* We give a name to even anonymous namespaces. */
22921 return TYPE_NAME (parent_type);
22922 case DW_TAG_class_type:
22923 case DW_TAG_interface_type:
22924 case DW_TAG_structure_type:
22925 case DW_TAG_union_type:
22926 case DW_TAG_module:
22927 parent_type = read_type_die (parent, cu);
22928 if (TYPE_NAME (parent_type) != NULL)
22929 return TYPE_NAME (parent_type);
22930 else
22931 /* An anonymous structure is only allowed non-static data
22932 members; no typedefs, no member functions, et cetera.
22933 So it does not need a prefix. */
22934 return "";
22935 case DW_TAG_compile_unit:
22936 case DW_TAG_partial_unit:
22937 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22938 if (cu->language == language_cplus
22939 && !dwarf2_per_objfile->types.empty ()
22940 && die->child != NULL
22941 && (die->tag == DW_TAG_class_type
22942 || die->tag == DW_TAG_structure_type
22943 || die->tag == DW_TAG_union_type))
22944 {
22945 char *name = guess_full_die_structure_name (die, cu);
22946 if (name != NULL)
22947 return name;
22948 }
22949 return "";
22950 case DW_TAG_subprogram:
22951 /* Nested subroutines in Fortran get a prefix with the name
22952 of the parent's subroutine. */
22953 if (cu->language == language_fortran)
22954 {
22955 if ((die->tag == DW_TAG_subprogram)
22956 && (dwarf2_name (parent, cu) != NULL))
22957 return dwarf2_name (parent, cu);
22958 }
22959 return determine_prefix (parent, cu);
22960 case DW_TAG_enumeration_type:
22961 parent_type = read_type_die (parent, cu);
22962 if (TYPE_DECLARED_CLASS (parent_type))
22963 {
22964 if (TYPE_NAME (parent_type) != NULL)
22965 return TYPE_NAME (parent_type);
22966 return "";
22967 }
22968 /* Fall through. */
22969 default:
22970 return determine_prefix (parent, cu);
22971 }
22972 }
22973
22974 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22975 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22976 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22977 an obconcat, otherwise allocate storage for the result. The CU argument is
22978 used to determine the language and hence, the appropriate separator. */
22979
22980 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
22981
22982 static char *
22983 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22984 int physname, struct dwarf2_cu *cu)
22985 {
22986 const char *lead = "";
22987 const char *sep;
22988
22989 if (suffix == NULL || suffix[0] == '\0'
22990 || prefix == NULL || prefix[0] == '\0')
22991 sep = "";
22992 else if (cu->language == language_d)
22993 {
22994 /* For D, the 'main' function could be defined in any module, but it
22995 should never be prefixed. */
22996 if (strcmp (suffix, "D main") == 0)
22997 {
22998 prefix = "";
22999 sep = "";
23000 }
23001 else
23002 sep = ".";
23003 }
23004 else if (cu->language == language_fortran && physname)
23005 {
23006 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
23007 DW_AT_MIPS_linkage_name is preferred and used instead. */
23008
23009 lead = "__";
23010 sep = "_MOD_";
23011 }
23012 else
23013 sep = "::";
23014
23015 if (prefix == NULL)
23016 prefix = "";
23017 if (suffix == NULL)
23018 suffix = "";
23019
23020 if (obs == NULL)
23021 {
23022 char *retval
23023 = ((char *)
23024 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
23025
23026 strcpy (retval, lead);
23027 strcat (retval, prefix);
23028 strcat (retval, sep);
23029 strcat (retval, suffix);
23030 return retval;
23031 }
23032 else
23033 {
23034 /* We have an obstack. */
23035 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
23036 }
23037 }
23038
23039 /* Return sibling of die, NULL if no sibling. */
23040
23041 static struct die_info *
23042 sibling_die (struct die_info *die)
23043 {
23044 return die->sibling;
23045 }
23046
23047 /* Get name of a die, return NULL if not found. */
23048
23049 static const char *
23050 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
23051 struct obstack *obstack)
23052 {
23053 if (name && cu->language == language_cplus)
23054 {
23055 std::string canon_name = cp_canonicalize_string (name);
23056
23057 if (!canon_name.empty ())
23058 {
23059 if (canon_name != name)
23060 name = obstack_strdup (obstack, canon_name);
23061 }
23062 }
23063
23064 return name;
23065 }
23066
23067 /* Get name of a die, return NULL if not found.
23068 Anonymous namespaces are converted to their magic string. */
23069
23070 static const char *
23071 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
23072 {
23073 struct attribute *attr;
23074 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
23075
23076 attr = dwarf2_attr (die, DW_AT_name, cu);
23077 if ((!attr || !DW_STRING (attr))
23078 && die->tag != DW_TAG_namespace
23079 && die->tag != DW_TAG_class_type
23080 && die->tag != DW_TAG_interface_type
23081 && die->tag != DW_TAG_structure_type
23082 && die->tag != DW_TAG_union_type)
23083 return NULL;
23084
23085 switch (die->tag)
23086 {
23087 case DW_TAG_compile_unit:
23088 case DW_TAG_partial_unit:
23089 /* Compilation units have a DW_AT_name that is a filename, not
23090 a source language identifier. */
23091 case DW_TAG_enumeration_type:
23092 case DW_TAG_enumerator:
23093 /* These tags always have simple identifiers already; no need
23094 to canonicalize them. */
23095 return DW_STRING (attr);
23096
23097 case DW_TAG_namespace:
23098 if (attr != NULL && DW_STRING (attr) != NULL)
23099 return DW_STRING (attr);
23100 return CP_ANONYMOUS_NAMESPACE_STR;
23101
23102 case DW_TAG_class_type:
23103 case DW_TAG_interface_type:
23104 case DW_TAG_structure_type:
23105 case DW_TAG_union_type:
23106 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
23107 structures or unions. These were of the form "._%d" in GCC 4.1,
23108 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
23109 and GCC 4.4. We work around this problem by ignoring these. */
23110 if (attr && DW_STRING (attr)
23111 && (startswith (DW_STRING (attr), "._")
23112 || startswith (DW_STRING (attr), "<anonymous")))
23113 return NULL;
23114
23115 /* GCC might emit a nameless typedef that has a linkage name. See
23116 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
23117 if (!attr || DW_STRING (attr) == NULL)
23118 {
23119 char *demangled = NULL;
23120
23121 attr = dw2_linkage_name_attr (die, cu);
23122 if (attr == NULL || DW_STRING (attr) == NULL)
23123 return NULL;
23124
23125 /* Avoid demangling DW_STRING (attr) the second time on a second
23126 call for the same DIE. */
23127 if (!DW_STRING_IS_CANONICAL (attr))
23128 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
23129
23130 if (demangled)
23131 {
23132 const char *base;
23133
23134 /* FIXME: we already did this for the partial symbol... */
23135 DW_STRING (attr)
23136 = obstack_strdup (&objfile->per_bfd->storage_obstack,
23137 demangled);
23138 DW_STRING_IS_CANONICAL (attr) = 1;
23139 xfree (demangled);
23140
23141 /* Strip any leading namespaces/classes, keep only the base name.
23142 DW_AT_name for named DIEs does not contain the prefixes. */
23143 base = strrchr (DW_STRING (attr), ':');
23144 if (base && base > DW_STRING (attr) && base[-1] == ':')
23145 return &base[1];
23146 else
23147 return DW_STRING (attr);
23148 }
23149 }
23150 break;
23151
23152 default:
23153 break;
23154 }
23155
23156 if (!DW_STRING_IS_CANONICAL (attr))
23157 {
23158 DW_STRING (attr)
23159 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
23160 &objfile->per_bfd->storage_obstack);
23161 DW_STRING_IS_CANONICAL (attr) = 1;
23162 }
23163 return DW_STRING (attr);
23164 }
23165
23166 /* Return the die that this die in an extension of, or NULL if there
23167 is none. *EXT_CU is the CU containing DIE on input, and the CU
23168 containing the return value on output. */
23169
23170 static struct die_info *
23171 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
23172 {
23173 struct attribute *attr;
23174
23175 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
23176 if (attr == NULL)
23177 return NULL;
23178
23179 return follow_die_ref (die, attr, ext_cu);
23180 }
23181
23182 /* A convenience function that returns an "unknown" DWARF name,
23183 including the value of V. STR is the name of the entity being
23184 printed, e.g., "TAG". */
23185
23186 static const char *
23187 dwarf_unknown (const char *str, unsigned v)
23188 {
23189 char *cell = get_print_cell ();
23190 xsnprintf (cell, PRINT_CELL_SIZE, "DW_%s_<unknown: %u>", str, v);
23191 return cell;
23192 }
23193
23194 /* Convert a DIE tag into its string name. */
23195
23196 static const char *
23197 dwarf_tag_name (unsigned tag)
23198 {
23199 const char *name = get_DW_TAG_name (tag);
23200
23201 if (name == NULL)
23202 return dwarf_unknown ("TAG", tag);
23203
23204 return name;
23205 }
23206
23207 /* Convert a DWARF attribute code into its string name. */
23208
23209 static const char *
23210 dwarf_attr_name (unsigned attr)
23211 {
23212 const char *name;
23213
23214 #ifdef MIPS /* collides with DW_AT_HP_block_index */
23215 if (attr == DW_AT_MIPS_fde)
23216 return "DW_AT_MIPS_fde";
23217 #else
23218 if (attr == DW_AT_HP_block_index)
23219 return "DW_AT_HP_block_index";
23220 #endif
23221
23222 name = get_DW_AT_name (attr);
23223
23224 if (name == NULL)
23225 return dwarf_unknown ("AT", attr);
23226
23227 return name;
23228 }
23229
23230 /* Convert a unit type to corresponding DW_UT name. */
23231
23232 static const char *
23233 dwarf_unit_type_name (int unit_type) {
23234 switch (unit_type)
23235 {
23236 case 0x01:
23237 return "DW_UT_compile (0x01)";
23238 case 0x02:
23239 return "DW_UT_type (0x02)";
23240 case 0x03:
23241 return "DW_UT_partial (0x03)";
23242 case 0x04:
23243 return "DW_UT_skeleton (0x04)";
23244 case 0x05:
23245 return "DW_UT_split_compile (0x05)";
23246 case 0x06:
23247 return "DW_UT_split_type (0x06)";
23248 case 0x80:
23249 return "DW_UT_lo_user (0x80)";
23250 case 0xff:
23251 return "DW_UT_hi_user (0xff)";
23252 default:
23253 return nullptr;
23254 }
23255 }
23256
23257 /* Convert a DWARF value form code into its string name. */
23258
23259 static const char *
23260 dwarf_form_name (unsigned form)
23261 {
23262 const char *name = get_DW_FORM_name (form);
23263
23264 if (name == NULL)
23265 return dwarf_unknown ("FORM", form);
23266
23267 return name;
23268 }
23269
23270 static const char *
23271 dwarf_bool_name (unsigned mybool)
23272 {
23273 if (mybool)
23274 return "TRUE";
23275 else
23276 return "FALSE";
23277 }
23278
23279 /* Convert a DWARF type code into its string name. */
23280
23281 static const char *
23282 dwarf_type_encoding_name (unsigned enc)
23283 {
23284 const char *name = get_DW_ATE_name (enc);
23285
23286 if (name == NULL)
23287 return dwarf_unknown ("ATE", enc);
23288
23289 return name;
23290 }
23291
23292 static void
23293 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
23294 {
23295 unsigned int i;
23296
23297 print_spaces (indent, f);
23298 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
23299 dwarf_tag_name (die->tag), die->abbrev,
23300 sect_offset_str (die->sect_off));
23301
23302 if (die->parent != NULL)
23303 {
23304 print_spaces (indent, f);
23305 fprintf_unfiltered (f, " parent at offset: %s\n",
23306 sect_offset_str (die->parent->sect_off));
23307 }
23308
23309 print_spaces (indent, f);
23310 fprintf_unfiltered (f, " has children: %s\n",
23311 dwarf_bool_name (die->child != NULL));
23312
23313 print_spaces (indent, f);
23314 fprintf_unfiltered (f, " attributes:\n");
23315
23316 for (i = 0; i < die->num_attrs; ++i)
23317 {
23318 print_spaces (indent, f);
23319 fprintf_unfiltered (f, " %s (%s) ",
23320 dwarf_attr_name (die->attrs[i].name),
23321 dwarf_form_name (die->attrs[i].form));
23322
23323 switch (die->attrs[i].form)
23324 {
23325 case DW_FORM_addr:
23326 case DW_FORM_addrx:
23327 case DW_FORM_GNU_addr_index:
23328 fprintf_unfiltered (f, "address: ");
23329 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
23330 break;
23331 case DW_FORM_block2:
23332 case DW_FORM_block4:
23333 case DW_FORM_block:
23334 case DW_FORM_block1:
23335 fprintf_unfiltered (f, "block: size %s",
23336 pulongest (DW_BLOCK (&die->attrs[i])->size));
23337 break;
23338 case DW_FORM_exprloc:
23339 fprintf_unfiltered (f, "expression: size %s",
23340 pulongest (DW_BLOCK (&die->attrs[i])->size));
23341 break;
23342 case DW_FORM_data16:
23343 fprintf_unfiltered (f, "constant of 16 bytes");
23344 break;
23345 case DW_FORM_ref_addr:
23346 fprintf_unfiltered (f, "ref address: ");
23347 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
23348 break;
23349 case DW_FORM_GNU_ref_alt:
23350 fprintf_unfiltered (f, "alt ref address: ");
23351 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
23352 break;
23353 case DW_FORM_ref1:
23354 case DW_FORM_ref2:
23355 case DW_FORM_ref4:
23356 case DW_FORM_ref8:
23357 case DW_FORM_ref_udata:
23358 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
23359 (long) (DW_UNSND (&die->attrs[i])));
23360 break;
23361 case DW_FORM_data1:
23362 case DW_FORM_data2:
23363 case DW_FORM_data4:
23364 case DW_FORM_data8:
23365 case DW_FORM_udata:
23366 case DW_FORM_sdata:
23367 fprintf_unfiltered (f, "constant: %s",
23368 pulongest (DW_UNSND (&die->attrs[i])));
23369 break;
23370 case DW_FORM_sec_offset:
23371 fprintf_unfiltered (f, "section offset: %s",
23372 pulongest (DW_UNSND (&die->attrs[i])));
23373 break;
23374 case DW_FORM_ref_sig8:
23375 fprintf_unfiltered (f, "signature: %s",
23376 hex_string (DW_SIGNATURE (&die->attrs[i])));
23377 break;
23378 case DW_FORM_string:
23379 case DW_FORM_strp:
23380 case DW_FORM_line_strp:
23381 case DW_FORM_strx:
23382 case DW_FORM_GNU_str_index:
23383 case DW_FORM_GNU_strp_alt:
23384 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
23385 DW_STRING (&die->attrs[i])
23386 ? DW_STRING (&die->attrs[i]) : "",
23387 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
23388 break;
23389 case DW_FORM_flag:
23390 if (DW_UNSND (&die->attrs[i]))
23391 fprintf_unfiltered (f, "flag: TRUE");
23392 else
23393 fprintf_unfiltered (f, "flag: FALSE");
23394 break;
23395 case DW_FORM_flag_present:
23396 fprintf_unfiltered (f, "flag: TRUE");
23397 break;
23398 case DW_FORM_indirect:
23399 /* The reader will have reduced the indirect form to
23400 the "base form" so this form should not occur. */
23401 fprintf_unfiltered (f,
23402 "unexpected attribute form: DW_FORM_indirect");
23403 break;
23404 case DW_FORM_implicit_const:
23405 fprintf_unfiltered (f, "constant: %s",
23406 plongest (DW_SND (&die->attrs[i])));
23407 break;
23408 default:
23409 fprintf_unfiltered (f, "unsupported attribute form: %d.",
23410 die->attrs[i].form);
23411 break;
23412 }
23413 fprintf_unfiltered (f, "\n");
23414 }
23415 }
23416
23417 static void
23418 dump_die_for_error (struct die_info *die)
23419 {
23420 dump_die_shallow (gdb_stderr, 0, die);
23421 }
23422
23423 static void
23424 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
23425 {
23426 int indent = level * 4;
23427
23428 gdb_assert (die != NULL);
23429
23430 if (level >= max_level)
23431 return;
23432
23433 dump_die_shallow (f, indent, die);
23434
23435 if (die->child != NULL)
23436 {
23437 print_spaces (indent, f);
23438 fprintf_unfiltered (f, " Children:");
23439 if (level + 1 < max_level)
23440 {
23441 fprintf_unfiltered (f, "\n");
23442 dump_die_1 (f, level + 1, max_level, die->child);
23443 }
23444 else
23445 {
23446 fprintf_unfiltered (f,
23447 " [not printed, max nesting level reached]\n");
23448 }
23449 }
23450
23451 if (die->sibling != NULL && level > 0)
23452 {
23453 dump_die_1 (f, level, max_level, die->sibling);
23454 }
23455 }
23456
23457 /* This is called from the pdie macro in gdbinit.in.
23458 It's not static so gcc will keep a copy callable from gdb. */
23459
23460 void
23461 dump_die (struct die_info *die, int max_level)
23462 {
23463 dump_die_1 (gdb_stdlog, 0, max_level, die);
23464 }
23465
23466 static void
23467 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
23468 {
23469 void **slot;
23470
23471 slot = htab_find_slot_with_hash (cu->die_hash, die,
23472 to_underlying (die->sect_off),
23473 INSERT);
23474
23475 *slot = die;
23476 }
23477
23478 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
23479 required kind. */
23480
23481 static sect_offset
23482 dwarf2_get_ref_die_offset (const struct attribute *attr)
23483 {
23484 if (attr_form_is_ref (attr))
23485 return (sect_offset) DW_UNSND (attr);
23486
23487 complaint (_("unsupported die ref attribute form: '%s'"),
23488 dwarf_form_name (attr->form));
23489 return {};
23490 }
23491
23492 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
23493 * the value held by the attribute is not constant. */
23494
23495 static LONGEST
23496 dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
23497 {
23498 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
23499 return DW_SND (attr);
23500 else if (attr->form == DW_FORM_udata
23501 || attr->form == DW_FORM_data1
23502 || attr->form == DW_FORM_data2
23503 || attr->form == DW_FORM_data4
23504 || attr->form == DW_FORM_data8)
23505 return DW_UNSND (attr);
23506 else
23507 {
23508 /* For DW_FORM_data16 see attr_form_is_constant. */
23509 complaint (_("Attribute value is not a constant (%s)"),
23510 dwarf_form_name (attr->form));
23511 return default_value;
23512 }
23513 }
23514
23515 /* Follow reference or signature attribute ATTR of SRC_DIE.
23516 On entry *REF_CU is the CU of SRC_DIE.
23517 On exit *REF_CU is the CU of the result. */
23518
23519 static struct die_info *
23520 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
23521 struct dwarf2_cu **ref_cu)
23522 {
23523 struct die_info *die;
23524
23525 if (attr_form_is_ref (attr))
23526 die = follow_die_ref (src_die, attr, ref_cu);
23527 else if (attr->form == DW_FORM_ref_sig8)
23528 die = follow_die_sig (src_die, attr, ref_cu);
23529 else
23530 {
23531 dump_die_for_error (src_die);
23532 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
23533 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23534 }
23535
23536 return die;
23537 }
23538
23539 /* Follow reference OFFSET.
23540 On entry *REF_CU is the CU of the source die referencing OFFSET.
23541 On exit *REF_CU is the CU of the result.
23542 Returns NULL if OFFSET is invalid. */
23543
23544 static struct die_info *
23545 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
23546 struct dwarf2_cu **ref_cu)
23547 {
23548 struct die_info temp_die;
23549 struct dwarf2_cu *target_cu, *cu = *ref_cu;
23550 struct dwarf2_per_objfile *dwarf2_per_objfile
23551 = cu->per_cu->dwarf2_per_objfile;
23552
23553 gdb_assert (cu->per_cu != NULL);
23554
23555 target_cu = cu;
23556
23557 if (cu->per_cu->is_debug_types)
23558 {
23559 /* .debug_types CUs cannot reference anything outside their CU.
23560 If they need to, they have to reference a signatured type via
23561 DW_FORM_ref_sig8. */
23562 if (!offset_in_cu_p (&cu->header, sect_off))
23563 return NULL;
23564 }
23565 else if (offset_in_dwz != cu->per_cu->is_dwz
23566 || !offset_in_cu_p (&cu->header, sect_off))
23567 {
23568 struct dwarf2_per_cu_data *per_cu;
23569
23570 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
23571 dwarf2_per_objfile);
23572
23573 /* If necessary, add it to the queue and load its DIEs. */
23574 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
23575 load_full_comp_unit (per_cu, false, cu->language);
23576
23577 target_cu = per_cu->cu;
23578 }
23579 else if (cu->dies == NULL)
23580 {
23581 /* We're loading full DIEs during partial symbol reading. */
23582 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
23583 load_full_comp_unit (cu->per_cu, false, language_minimal);
23584 }
23585
23586 *ref_cu = target_cu;
23587 temp_die.sect_off = sect_off;
23588
23589 if (target_cu != cu)
23590 target_cu->ancestor = cu;
23591
23592 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
23593 &temp_die,
23594 to_underlying (sect_off));
23595 }
23596
23597 /* Follow reference attribute ATTR of SRC_DIE.
23598 On entry *REF_CU is the CU of SRC_DIE.
23599 On exit *REF_CU is the CU of the result. */
23600
23601 static struct die_info *
23602 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
23603 struct dwarf2_cu **ref_cu)
23604 {
23605 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
23606 struct dwarf2_cu *cu = *ref_cu;
23607 struct die_info *die;
23608
23609 die = follow_die_offset (sect_off,
23610 (attr->form == DW_FORM_GNU_ref_alt
23611 || cu->per_cu->is_dwz),
23612 ref_cu);
23613 if (!die)
23614 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
23615 "at %s [in module %s]"),
23616 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
23617 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
23618
23619 return die;
23620 }
23621
23622 /* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
23623 Returned value is intended for DW_OP_call*. Returned
23624 dwarf2_locexpr_baton->data has lifetime of
23625 PER_CU->DWARF2_PER_OBJFILE->OBJFILE. */
23626
23627 struct dwarf2_locexpr_baton
23628 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
23629 struct dwarf2_per_cu_data *per_cu,
23630 CORE_ADDR (*get_frame_pc) (void *baton),
23631 void *baton, bool resolve_abstract_p)
23632 {
23633 struct dwarf2_cu *cu;
23634 struct die_info *die;
23635 struct attribute *attr;
23636 struct dwarf2_locexpr_baton retval;
23637 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
23638 struct objfile *objfile = dwarf2_per_objfile->objfile;
23639
23640 if (per_cu->cu == NULL)
23641 load_cu (per_cu, false);
23642 cu = per_cu->cu;
23643 if (cu == NULL)
23644 {
23645 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23646 Instead just throw an error, not much else we can do. */
23647 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23648 sect_offset_str (sect_off), objfile_name (objfile));
23649 }
23650
23651 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23652 if (!die)
23653 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23654 sect_offset_str (sect_off), objfile_name (objfile));
23655
23656 attr = dwarf2_attr (die, DW_AT_location, cu);
23657 if (!attr && resolve_abstract_p
23658 && (dwarf2_per_objfile->abstract_to_concrete.find (die->sect_off)
23659 != dwarf2_per_objfile->abstract_to_concrete.end ()))
23660 {
23661 CORE_ADDR pc = (*get_frame_pc) (baton);
23662 CORE_ADDR baseaddr
23663 = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
23664 struct gdbarch *gdbarch = get_objfile_arch (objfile);
23665
23666 for (const auto &cand_off
23667 : dwarf2_per_objfile->abstract_to_concrete[die->sect_off])
23668 {
23669 struct dwarf2_cu *cand_cu = cu;
23670 struct die_info *cand
23671 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
23672 if (!cand
23673 || !cand->parent
23674 || cand->parent->tag != DW_TAG_subprogram)
23675 continue;
23676
23677 CORE_ADDR pc_low, pc_high;
23678 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
23679 if (pc_low == ((CORE_ADDR) -1))
23680 continue;
23681 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
23682 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
23683 if (!(pc_low <= pc && pc < pc_high))
23684 continue;
23685
23686 die = cand;
23687 attr = dwarf2_attr (die, DW_AT_location, cu);
23688 break;
23689 }
23690 }
23691
23692 if (!attr)
23693 {
23694 /* DWARF: "If there is no such attribute, then there is no effect.".
23695 DATA is ignored if SIZE is 0. */
23696
23697 retval.data = NULL;
23698 retval.size = 0;
23699 }
23700 else if (attr_form_is_section_offset (attr))
23701 {
23702 struct dwarf2_loclist_baton loclist_baton;
23703 CORE_ADDR pc = (*get_frame_pc) (baton);
23704 size_t size;
23705
23706 fill_in_loclist_baton (cu, &loclist_baton, attr);
23707
23708 retval.data = dwarf2_find_location_expression (&loclist_baton,
23709 &size, pc);
23710 retval.size = size;
23711 }
23712 else
23713 {
23714 if (!attr_form_is_block (attr))
23715 error (_("Dwarf Error: DIE at %s referenced in module %s "
23716 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
23717 sect_offset_str (sect_off), objfile_name (objfile));
23718
23719 retval.data = DW_BLOCK (attr)->data;
23720 retval.size = DW_BLOCK (attr)->size;
23721 }
23722 retval.per_cu = cu->per_cu;
23723
23724 age_cached_comp_units (dwarf2_per_objfile);
23725
23726 return retval;
23727 }
23728
23729 /* Like dwarf2_fetch_die_loc_sect_off, but take a CU
23730 offset. */
23731
23732 struct dwarf2_locexpr_baton
23733 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
23734 struct dwarf2_per_cu_data *per_cu,
23735 CORE_ADDR (*get_frame_pc) (void *baton),
23736 void *baton)
23737 {
23738 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
23739
23740 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
23741 }
23742
23743 /* Write a constant of a given type as target-ordered bytes into
23744 OBSTACK. */
23745
23746 static const gdb_byte *
23747 write_constant_as_bytes (struct obstack *obstack,
23748 enum bfd_endian byte_order,
23749 struct type *type,
23750 ULONGEST value,
23751 LONGEST *len)
23752 {
23753 gdb_byte *result;
23754
23755 *len = TYPE_LENGTH (type);
23756 result = (gdb_byte *) obstack_alloc (obstack, *len);
23757 store_unsigned_integer (result, *len, byte_order, value);
23758
23759 return result;
23760 }
23761
23762 /* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
23763 pointer to the constant bytes and set LEN to the length of the
23764 data. If memory is needed, allocate it on OBSTACK. If the DIE
23765 does not have a DW_AT_const_value, return NULL. */
23766
23767 const gdb_byte *
23768 dwarf2_fetch_constant_bytes (sect_offset sect_off,
23769 struct dwarf2_per_cu_data *per_cu,
23770 struct obstack *obstack,
23771 LONGEST *len)
23772 {
23773 struct dwarf2_cu *cu;
23774 struct die_info *die;
23775 struct attribute *attr;
23776 const gdb_byte *result = NULL;
23777 struct type *type;
23778 LONGEST value;
23779 enum bfd_endian byte_order;
23780 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
23781
23782 if (per_cu->cu == NULL)
23783 load_cu (per_cu, false);
23784 cu = per_cu->cu;
23785 if (cu == NULL)
23786 {
23787 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23788 Instead just throw an error, not much else we can do. */
23789 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23790 sect_offset_str (sect_off), objfile_name (objfile));
23791 }
23792
23793 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23794 if (!die)
23795 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23796 sect_offset_str (sect_off), objfile_name (objfile));
23797
23798 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23799 if (attr == NULL)
23800 return NULL;
23801
23802 byte_order = (bfd_big_endian (objfile->obfd)
23803 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23804
23805 switch (attr->form)
23806 {
23807 case DW_FORM_addr:
23808 case DW_FORM_addrx:
23809 case DW_FORM_GNU_addr_index:
23810 {
23811 gdb_byte *tem;
23812
23813 *len = cu->header.addr_size;
23814 tem = (gdb_byte *) obstack_alloc (obstack, *len);
23815 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
23816 result = tem;
23817 }
23818 break;
23819 case DW_FORM_string:
23820 case DW_FORM_strp:
23821 case DW_FORM_strx:
23822 case DW_FORM_GNU_str_index:
23823 case DW_FORM_GNU_strp_alt:
23824 /* DW_STRING is already allocated on the objfile obstack, point
23825 directly to it. */
23826 result = (const gdb_byte *) DW_STRING (attr);
23827 *len = strlen (DW_STRING (attr));
23828 break;
23829 case DW_FORM_block1:
23830 case DW_FORM_block2:
23831 case DW_FORM_block4:
23832 case DW_FORM_block:
23833 case DW_FORM_exprloc:
23834 case DW_FORM_data16:
23835 result = DW_BLOCK (attr)->data;
23836 *len = DW_BLOCK (attr)->size;
23837 break;
23838
23839 /* The DW_AT_const_value attributes are supposed to carry the
23840 symbol's value "represented as it would be on the target
23841 architecture." By the time we get here, it's already been
23842 converted to host endianness, so we just need to sign- or
23843 zero-extend it as appropriate. */
23844 case DW_FORM_data1:
23845 type = die_type (die, cu);
23846 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23847 if (result == NULL)
23848 result = write_constant_as_bytes (obstack, byte_order,
23849 type, value, len);
23850 break;
23851 case DW_FORM_data2:
23852 type = die_type (die, cu);
23853 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23854 if (result == NULL)
23855 result = write_constant_as_bytes (obstack, byte_order,
23856 type, value, len);
23857 break;
23858 case DW_FORM_data4:
23859 type = die_type (die, cu);
23860 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23861 if (result == NULL)
23862 result = write_constant_as_bytes (obstack, byte_order,
23863 type, value, len);
23864 break;
23865 case DW_FORM_data8:
23866 type = die_type (die, cu);
23867 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23868 if (result == NULL)
23869 result = write_constant_as_bytes (obstack, byte_order,
23870 type, value, len);
23871 break;
23872
23873 case DW_FORM_sdata:
23874 case DW_FORM_implicit_const:
23875 type = die_type (die, cu);
23876 result = write_constant_as_bytes (obstack, byte_order,
23877 type, DW_SND (attr), len);
23878 break;
23879
23880 case DW_FORM_udata:
23881 type = die_type (die, cu);
23882 result = write_constant_as_bytes (obstack, byte_order,
23883 type, DW_UNSND (attr), len);
23884 break;
23885
23886 default:
23887 complaint (_("unsupported const value attribute form: '%s'"),
23888 dwarf_form_name (attr->form));
23889 break;
23890 }
23891
23892 return result;
23893 }
23894
23895 /* Return the type of the die at OFFSET in PER_CU. Return NULL if no
23896 valid type for this die is found. */
23897
23898 struct type *
23899 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
23900 struct dwarf2_per_cu_data *per_cu)
23901 {
23902 struct dwarf2_cu *cu;
23903 struct die_info *die;
23904
23905 if (per_cu->cu == NULL)
23906 load_cu (per_cu, false);
23907 cu = per_cu->cu;
23908 if (!cu)
23909 return NULL;
23910
23911 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23912 if (!die)
23913 return NULL;
23914
23915 return die_type (die, cu);
23916 }
23917
23918 /* Return the type of the DIE at DIE_OFFSET in the CU named by
23919 PER_CU. */
23920
23921 struct type *
23922 dwarf2_get_die_type (cu_offset die_offset,
23923 struct dwarf2_per_cu_data *per_cu)
23924 {
23925 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
23926 return get_die_type_at_offset (die_offset_sect, per_cu);
23927 }
23928
23929 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
23930 On entry *REF_CU is the CU of SRC_DIE.
23931 On exit *REF_CU is the CU of the result.
23932 Returns NULL if the referenced DIE isn't found. */
23933
23934 static struct die_info *
23935 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23936 struct dwarf2_cu **ref_cu)
23937 {
23938 struct die_info temp_die;
23939 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
23940 struct die_info *die;
23941
23942 /* While it might be nice to assert sig_type->type == NULL here,
23943 we can get here for DW_AT_imported_declaration where we need
23944 the DIE not the type. */
23945
23946 /* If necessary, add it to the queue and load its DIEs. */
23947
23948 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
23949 read_signatured_type (sig_type);
23950
23951 sig_cu = sig_type->per_cu.cu;
23952 gdb_assert (sig_cu != NULL);
23953 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23954 temp_die.sect_off = sig_type->type_offset_in_section;
23955 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
23956 to_underlying (temp_die.sect_off));
23957 if (die)
23958 {
23959 struct dwarf2_per_objfile *dwarf2_per_objfile
23960 = (*ref_cu)->per_cu->dwarf2_per_objfile;
23961
23962 /* For .gdb_index version 7 keep track of included TUs.
23963 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23964 if (dwarf2_per_objfile->index_table != NULL
23965 && dwarf2_per_objfile->index_table->version <= 7)
23966 {
23967 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
23968 }
23969
23970 *ref_cu = sig_cu;
23971 if (sig_cu != cu)
23972 sig_cu->ancestor = cu;
23973
23974 return die;
23975 }
23976
23977 return NULL;
23978 }
23979
23980 /* Follow signatured type referenced by ATTR in SRC_DIE.
23981 On entry *REF_CU is the CU of SRC_DIE.
23982 On exit *REF_CU is the CU of the result.
23983 The result is the DIE of the type.
23984 If the referenced type cannot be found an error is thrown. */
23985
23986 static struct die_info *
23987 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
23988 struct dwarf2_cu **ref_cu)
23989 {
23990 ULONGEST signature = DW_SIGNATURE (attr);
23991 struct signatured_type *sig_type;
23992 struct die_info *die;
23993
23994 gdb_assert (attr->form == DW_FORM_ref_sig8);
23995
23996 sig_type = lookup_signatured_type (*ref_cu, signature);
23997 /* sig_type will be NULL if the signatured type is missing from
23998 the debug info. */
23999 if (sig_type == NULL)
24000 {
24001 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
24002 " from DIE at %s [in module %s]"),
24003 hex_string (signature), sect_offset_str (src_die->sect_off),
24004 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
24005 }
24006
24007 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
24008 if (die == NULL)
24009 {
24010 dump_die_for_error (src_die);
24011 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
24012 " from DIE at %s [in module %s]"),
24013 hex_string (signature), sect_offset_str (src_die->sect_off),
24014 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
24015 }
24016
24017 return die;
24018 }
24019
24020 /* Get the type specified by SIGNATURE referenced in DIE/CU,
24021 reading in and processing the type unit if necessary. */
24022
24023 static struct type *
24024 get_signatured_type (struct die_info *die, ULONGEST signature,
24025 struct dwarf2_cu *cu)
24026 {
24027 struct dwarf2_per_objfile *dwarf2_per_objfile
24028 = cu->per_cu->dwarf2_per_objfile;
24029 struct signatured_type *sig_type;
24030 struct dwarf2_cu *type_cu;
24031 struct die_info *type_die;
24032 struct type *type;
24033
24034 sig_type = lookup_signatured_type (cu, signature);
24035 /* sig_type will be NULL if the signatured type is missing from
24036 the debug info. */
24037 if (sig_type == NULL)
24038 {
24039 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
24040 " from DIE at %s [in module %s]"),
24041 hex_string (signature), sect_offset_str (die->sect_off),
24042 objfile_name (dwarf2_per_objfile->objfile));
24043 return build_error_marker_type (cu, die);
24044 }
24045
24046 /* If we already know the type we're done. */
24047 if (sig_type->type != NULL)
24048 return sig_type->type;
24049
24050 type_cu = cu;
24051 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
24052 if (type_die != NULL)
24053 {
24054 /* N.B. We need to call get_die_type to ensure only one type for this DIE
24055 is created. This is important, for example, because for c++ classes
24056 we need TYPE_NAME set which is only done by new_symbol. Blech. */
24057 type = read_type_die (type_die, type_cu);
24058 if (type == NULL)
24059 {
24060 complaint (_("Dwarf Error: Cannot build signatured type %s"
24061 " referenced from DIE at %s [in module %s]"),
24062 hex_string (signature), sect_offset_str (die->sect_off),
24063 objfile_name (dwarf2_per_objfile->objfile));
24064 type = build_error_marker_type (cu, die);
24065 }
24066 }
24067 else
24068 {
24069 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
24070 " from DIE at %s [in module %s]"),
24071 hex_string (signature), sect_offset_str (die->sect_off),
24072 objfile_name (dwarf2_per_objfile->objfile));
24073 type = build_error_marker_type (cu, die);
24074 }
24075 sig_type->type = type;
24076
24077 return type;
24078 }
24079
24080 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
24081 reading in and processing the type unit if necessary. */
24082
24083 static struct type *
24084 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
24085 struct dwarf2_cu *cu) /* ARI: editCase function */
24086 {
24087 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
24088 if (attr_form_is_ref (attr))
24089 {
24090 struct dwarf2_cu *type_cu = cu;
24091 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
24092
24093 return read_type_die (type_die, type_cu);
24094 }
24095 else if (attr->form == DW_FORM_ref_sig8)
24096 {
24097 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
24098 }
24099 else
24100 {
24101 struct dwarf2_per_objfile *dwarf2_per_objfile
24102 = cu->per_cu->dwarf2_per_objfile;
24103
24104 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
24105 " at %s [in module %s]"),
24106 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
24107 objfile_name (dwarf2_per_objfile->objfile));
24108 return build_error_marker_type (cu, die);
24109 }
24110 }
24111
24112 /* Load the DIEs associated with type unit PER_CU into memory. */
24113
24114 static void
24115 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
24116 {
24117 struct signatured_type *sig_type;
24118
24119 /* Caller is responsible for ensuring type_unit_groups don't get here. */
24120 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
24121
24122 /* We have the per_cu, but we need the signatured_type.
24123 Fortunately this is an easy translation. */
24124 gdb_assert (per_cu->is_debug_types);
24125 sig_type = (struct signatured_type *) per_cu;
24126
24127 gdb_assert (per_cu->cu == NULL);
24128
24129 read_signatured_type (sig_type);
24130
24131 gdb_assert (per_cu->cu != NULL);
24132 }
24133
24134 /* die_reader_func for read_signatured_type.
24135 This is identical to load_full_comp_unit_reader,
24136 but is kept separate for now. */
24137
24138 static void
24139 read_signatured_type_reader (const struct die_reader_specs *reader,
24140 const gdb_byte *info_ptr,
24141 struct die_info *comp_unit_die,
24142 int has_children,
24143 void *data)
24144 {
24145 struct dwarf2_cu *cu = reader->cu;
24146
24147 gdb_assert (cu->die_hash == NULL);
24148 cu->die_hash =
24149 htab_create_alloc_ex (cu->header.length / 12,
24150 die_hash,
24151 die_eq,
24152 NULL,
24153 &cu->comp_unit_obstack,
24154 hashtab_obstack_allocate,
24155 dummy_obstack_deallocate);
24156
24157 if (has_children)
24158 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
24159 &info_ptr, comp_unit_die);
24160 cu->dies = comp_unit_die;
24161 /* comp_unit_die is not stored in die_hash, no need. */
24162
24163 /* We try not to read any attributes in this function, because not
24164 all CUs needed for references have been loaded yet, and symbol
24165 table processing isn't initialized. But we have to set the CU language,
24166 or we won't be able to build types correctly.
24167 Similarly, if we do not read the producer, we can not apply
24168 producer-specific interpretation. */
24169 prepare_one_comp_unit (cu, cu->dies, language_minimal);
24170 }
24171
24172 /* Read in a signatured type and build its CU and DIEs.
24173 If the type is a stub for the real type in a DWO file,
24174 read in the real type from the DWO file as well. */
24175
24176 static void
24177 read_signatured_type (struct signatured_type *sig_type)
24178 {
24179 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
24180
24181 gdb_assert (per_cu->is_debug_types);
24182 gdb_assert (per_cu->cu == NULL);
24183
24184 init_cutu_and_read_dies (per_cu, NULL, 0, 1, false,
24185 read_signatured_type_reader, NULL);
24186 sig_type->per_cu.tu_read = 1;
24187 }
24188
24189 /* Decode simple location descriptions.
24190 Given a pointer to a dwarf block that defines a location, compute
24191 the location and return the value.
24192
24193 NOTE drow/2003-11-18: This function is called in two situations
24194 now: for the address of static or global variables (partial symbols
24195 only) and for offsets into structures which are expected to be
24196 (more or less) constant. The partial symbol case should go away,
24197 and only the constant case should remain. That will let this
24198 function complain more accurately. A few special modes are allowed
24199 without complaint for global variables (for instance, global
24200 register values and thread-local values).
24201
24202 A location description containing no operations indicates that the
24203 object is optimized out. The return value is 0 for that case.
24204 FIXME drow/2003-11-16: No callers check for this case any more; soon all
24205 callers will only want a very basic result and this can become a
24206 complaint.
24207
24208 Note that stack[0] is unused except as a default error return. */
24209
24210 static CORE_ADDR
24211 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
24212 {
24213 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
24214 size_t i;
24215 size_t size = blk->size;
24216 const gdb_byte *data = blk->data;
24217 CORE_ADDR stack[64];
24218 int stacki;
24219 unsigned int bytes_read, unsnd;
24220 gdb_byte op;
24221
24222 i = 0;
24223 stacki = 0;
24224 stack[stacki] = 0;
24225 stack[++stacki] = 0;
24226
24227 while (i < size)
24228 {
24229 op = data[i++];
24230 switch (op)
24231 {
24232 case DW_OP_lit0:
24233 case DW_OP_lit1:
24234 case DW_OP_lit2:
24235 case DW_OP_lit3:
24236 case DW_OP_lit4:
24237 case DW_OP_lit5:
24238 case DW_OP_lit6:
24239 case DW_OP_lit7:
24240 case DW_OP_lit8:
24241 case DW_OP_lit9:
24242 case DW_OP_lit10:
24243 case DW_OP_lit11:
24244 case DW_OP_lit12:
24245 case DW_OP_lit13:
24246 case DW_OP_lit14:
24247 case DW_OP_lit15:
24248 case DW_OP_lit16:
24249 case DW_OP_lit17:
24250 case DW_OP_lit18:
24251 case DW_OP_lit19:
24252 case DW_OP_lit20:
24253 case DW_OP_lit21:
24254 case DW_OP_lit22:
24255 case DW_OP_lit23:
24256 case DW_OP_lit24:
24257 case DW_OP_lit25:
24258 case DW_OP_lit26:
24259 case DW_OP_lit27:
24260 case DW_OP_lit28:
24261 case DW_OP_lit29:
24262 case DW_OP_lit30:
24263 case DW_OP_lit31:
24264 stack[++stacki] = op - DW_OP_lit0;
24265 break;
24266
24267 case DW_OP_reg0:
24268 case DW_OP_reg1:
24269 case DW_OP_reg2:
24270 case DW_OP_reg3:
24271 case DW_OP_reg4:
24272 case DW_OP_reg5:
24273 case DW_OP_reg6:
24274 case DW_OP_reg7:
24275 case DW_OP_reg8:
24276 case DW_OP_reg9:
24277 case DW_OP_reg10:
24278 case DW_OP_reg11:
24279 case DW_OP_reg12:
24280 case DW_OP_reg13:
24281 case DW_OP_reg14:
24282 case DW_OP_reg15:
24283 case DW_OP_reg16:
24284 case DW_OP_reg17:
24285 case DW_OP_reg18:
24286 case DW_OP_reg19:
24287 case DW_OP_reg20:
24288 case DW_OP_reg21:
24289 case DW_OP_reg22:
24290 case DW_OP_reg23:
24291 case DW_OP_reg24:
24292 case DW_OP_reg25:
24293 case DW_OP_reg26:
24294 case DW_OP_reg27:
24295 case DW_OP_reg28:
24296 case DW_OP_reg29:
24297 case DW_OP_reg30:
24298 case DW_OP_reg31:
24299 stack[++stacki] = op - DW_OP_reg0;
24300 if (i < size)
24301 dwarf2_complex_location_expr_complaint ();
24302 break;
24303
24304 case DW_OP_regx:
24305 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
24306 i += bytes_read;
24307 stack[++stacki] = unsnd;
24308 if (i < size)
24309 dwarf2_complex_location_expr_complaint ();
24310 break;
24311
24312 case DW_OP_addr:
24313 stack[++stacki] = read_address (objfile->obfd, &data[i],
24314 cu, &bytes_read);
24315 i += bytes_read;
24316 break;
24317
24318 case DW_OP_const1u:
24319 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
24320 i += 1;
24321 break;
24322
24323 case DW_OP_const1s:
24324 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
24325 i += 1;
24326 break;
24327
24328 case DW_OP_const2u:
24329 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
24330 i += 2;
24331 break;
24332
24333 case DW_OP_const2s:
24334 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
24335 i += 2;
24336 break;
24337
24338 case DW_OP_const4u:
24339 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
24340 i += 4;
24341 break;
24342
24343 case DW_OP_const4s:
24344 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
24345 i += 4;
24346 break;
24347
24348 case DW_OP_const8u:
24349 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
24350 i += 8;
24351 break;
24352
24353 case DW_OP_constu:
24354 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
24355 &bytes_read);
24356 i += bytes_read;
24357 break;
24358
24359 case DW_OP_consts:
24360 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
24361 i += bytes_read;
24362 break;
24363
24364 case DW_OP_dup:
24365 stack[stacki + 1] = stack[stacki];
24366 stacki++;
24367 break;
24368
24369 case DW_OP_plus:
24370 stack[stacki - 1] += stack[stacki];
24371 stacki--;
24372 break;
24373
24374 case DW_OP_plus_uconst:
24375 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
24376 &bytes_read);
24377 i += bytes_read;
24378 break;
24379
24380 case DW_OP_minus:
24381 stack[stacki - 1] -= stack[stacki];
24382 stacki--;
24383 break;
24384
24385 case DW_OP_deref:
24386 /* If we're not the last op, then we definitely can't encode
24387 this using GDB's address_class enum. This is valid for partial
24388 global symbols, although the variable's address will be bogus
24389 in the psymtab. */
24390 if (i < size)
24391 dwarf2_complex_location_expr_complaint ();
24392 break;
24393
24394 case DW_OP_GNU_push_tls_address:
24395 case DW_OP_form_tls_address:
24396 /* The top of the stack has the offset from the beginning
24397 of the thread control block at which the variable is located. */
24398 /* Nothing should follow this operator, so the top of stack would
24399 be returned. */
24400 /* This is valid for partial global symbols, but the variable's
24401 address will be bogus in the psymtab. Make it always at least
24402 non-zero to not look as a variable garbage collected by linker
24403 which have DW_OP_addr 0. */
24404 if (i < size)
24405 dwarf2_complex_location_expr_complaint ();
24406 stack[stacki]++;
24407 break;
24408
24409 case DW_OP_GNU_uninit:
24410 break;
24411
24412 case DW_OP_addrx:
24413 case DW_OP_GNU_addr_index:
24414 case DW_OP_GNU_const_index:
24415 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
24416 &bytes_read);
24417 i += bytes_read;
24418 break;
24419
24420 default:
24421 {
24422 const char *name = get_DW_OP_name (op);
24423
24424 if (name)
24425 complaint (_("unsupported stack op: '%s'"),
24426 name);
24427 else
24428 complaint (_("unsupported stack op: '%02x'"),
24429 op);
24430 }
24431
24432 return (stack[stacki]);
24433 }
24434
24435 /* Enforce maximum stack depth of SIZE-1 to avoid writing
24436 outside of the allocated space. Also enforce minimum>0. */
24437 if (stacki >= ARRAY_SIZE (stack) - 1)
24438 {
24439 complaint (_("location description stack overflow"));
24440 return 0;
24441 }
24442
24443 if (stacki <= 0)
24444 {
24445 complaint (_("location description stack underflow"));
24446 return 0;
24447 }
24448 }
24449 return (stack[stacki]);
24450 }
24451
24452 /* memory allocation interface */
24453
24454 static struct dwarf_block *
24455 dwarf_alloc_block (struct dwarf2_cu *cu)
24456 {
24457 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
24458 }
24459
24460 static struct die_info *
24461 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
24462 {
24463 struct die_info *die;
24464 size_t size = sizeof (struct die_info);
24465
24466 if (num_attrs > 1)
24467 size += (num_attrs - 1) * sizeof (struct attribute);
24468
24469 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
24470 memset (die, 0, sizeof (struct die_info));
24471 return (die);
24472 }
24473
24474 \f
24475 /* Macro support. */
24476
24477 /* Return file name relative to the compilation directory of file number I in
24478 *LH's file name table. The result is allocated using xmalloc; the caller is
24479 responsible for freeing it. */
24480
24481 static char *
24482 file_file_name (int file, struct line_header *lh)
24483 {
24484 /* Is the file number a valid index into the line header's file name
24485 table? Remember that file numbers start with one, not zero. */
24486 if (lh->is_valid_file_index (file))
24487 {
24488 const file_entry *fe = lh->file_name_at (file);
24489
24490 if (!IS_ABSOLUTE_PATH (fe->name))
24491 {
24492 const char *dir = fe->include_dir (lh);
24493 if (dir != NULL)
24494 return concat (dir, SLASH_STRING, fe->name, (char *) NULL);
24495 }
24496 return xstrdup (fe->name);
24497 }
24498 else
24499 {
24500 /* The compiler produced a bogus file number. We can at least
24501 record the macro definitions made in the file, even if we
24502 won't be able to find the file by name. */
24503 char fake_name[80];
24504
24505 xsnprintf (fake_name, sizeof (fake_name),
24506 "<bad macro file number %d>", file);
24507
24508 complaint (_("bad file number in macro information (%d)"),
24509 file);
24510
24511 return xstrdup (fake_name);
24512 }
24513 }
24514
24515 /* Return the full name of file number I in *LH's file name table.
24516 Use COMP_DIR as the name of the current directory of the
24517 compilation. The result is allocated using xmalloc; the caller is
24518 responsible for freeing it. */
24519 static char *
24520 file_full_name (int file, struct line_header *lh, const char *comp_dir)
24521 {
24522 /* Is the file number a valid index into the line header's file name
24523 table? Remember that file numbers start with one, not zero. */
24524 if (lh->is_valid_file_index (file))
24525 {
24526 char *relative = file_file_name (file, lh);
24527
24528 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
24529 return relative;
24530 return reconcat (relative, comp_dir, SLASH_STRING,
24531 relative, (char *) NULL);
24532 }
24533 else
24534 return file_file_name (file, lh);
24535 }
24536
24537
24538 static struct macro_source_file *
24539 macro_start_file (struct dwarf2_cu *cu,
24540 int file, int line,
24541 struct macro_source_file *current_file,
24542 struct line_header *lh)
24543 {
24544 /* File name relative to the compilation directory of this source file. */
24545 char *file_name = file_file_name (file, lh);
24546
24547 if (! current_file)
24548 {
24549 /* Note: We don't create a macro table for this compilation unit
24550 at all until we actually get a filename. */
24551 struct macro_table *macro_table = cu->get_builder ()->get_macro_table ();
24552
24553 /* If we have no current file, then this must be the start_file
24554 directive for the compilation unit's main source file. */
24555 current_file = macro_set_main (macro_table, file_name);
24556 macro_define_special (macro_table);
24557 }
24558 else
24559 current_file = macro_include (current_file, line, file_name);
24560
24561 xfree (file_name);
24562
24563 return current_file;
24564 }
24565
24566 static const char *
24567 consume_improper_spaces (const char *p, const char *body)
24568 {
24569 if (*p == ' ')
24570 {
24571 complaint (_("macro definition contains spaces "
24572 "in formal argument list:\n`%s'"),
24573 body);
24574
24575 while (*p == ' ')
24576 p++;
24577 }
24578
24579 return p;
24580 }
24581
24582
24583 static void
24584 parse_macro_definition (struct macro_source_file *file, int line,
24585 const char *body)
24586 {
24587 const char *p;
24588
24589 /* The body string takes one of two forms. For object-like macro
24590 definitions, it should be:
24591
24592 <macro name> " " <definition>
24593
24594 For function-like macro definitions, it should be:
24595
24596 <macro name> "() " <definition>
24597 or
24598 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
24599
24600 Spaces may appear only where explicitly indicated, and in the
24601 <definition>.
24602
24603 The Dwarf 2 spec says that an object-like macro's name is always
24604 followed by a space, but versions of GCC around March 2002 omit
24605 the space when the macro's definition is the empty string.
24606
24607 The Dwarf 2 spec says that there should be no spaces between the
24608 formal arguments in a function-like macro's formal argument list,
24609 but versions of GCC around March 2002 include spaces after the
24610 commas. */
24611
24612
24613 /* Find the extent of the macro name. The macro name is terminated
24614 by either a space or null character (for an object-like macro) or
24615 an opening paren (for a function-like macro). */
24616 for (p = body; *p; p++)
24617 if (*p == ' ' || *p == '(')
24618 break;
24619
24620 if (*p == ' ' || *p == '\0')
24621 {
24622 /* It's an object-like macro. */
24623 int name_len = p - body;
24624 char *name = savestring (body, name_len);
24625 const char *replacement;
24626
24627 if (*p == ' ')
24628 replacement = body + name_len + 1;
24629 else
24630 {
24631 dwarf2_macro_malformed_definition_complaint (body);
24632 replacement = body + name_len;
24633 }
24634
24635 macro_define_object (file, line, name, replacement);
24636
24637 xfree (name);
24638 }
24639 else if (*p == '(')
24640 {
24641 /* It's a function-like macro. */
24642 char *name = savestring (body, p - body);
24643 int argc = 0;
24644 int argv_size = 1;
24645 char **argv = XNEWVEC (char *, argv_size);
24646
24647 p++;
24648
24649 p = consume_improper_spaces (p, body);
24650
24651 /* Parse the formal argument list. */
24652 while (*p && *p != ')')
24653 {
24654 /* Find the extent of the current argument name. */
24655 const char *arg_start = p;
24656
24657 while (*p && *p != ',' && *p != ')' && *p != ' ')
24658 p++;
24659
24660 if (! *p || p == arg_start)
24661 dwarf2_macro_malformed_definition_complaint (body);
24662 else
24663 {
24664 /* Make sure argv has room for the new argument. */
24665 if (argc >= argv_size)
24666 {
24667 argv_size *= 2;
24668 argv = XRESIZEVEC (char *, argv, argv_size);
24669 }
24670
24671 argv[argc++] = savestring (arg_start, p - arg_start);
24672 }
24673
24674 p = consume_improper_spaces (p, body);
24675
24676 /* Consume the comma, if present. */
24677 if (*p == ',')
24678 {
24679 p++;
24680
24681 p = consume_improper_spaces (p, body);
24682 }
24683 }
24684
24685 if (*p == ')')
24686 {
24687 p++;
24688
24689 if (*p == ' ')
24690 /* Perfectly formed definition, no complaints. */
24691 macro_define_function (file, line, name,
24692 argc, (const char **) argv,
24693 p + 1);
24694 else if (*p == '\0')
24695 {
24696 /* Complain, but do define it. */
24697 dwarf2_macro_malformed_definition_complaint (body);
24698 macro_define_function (file, line, name,
24699 argc, (const char **) argv,
24700 p);
24701 }
24702 else
24703 /* Just complain. */
24704 dwarf2_macro_malformed_definition_complaint (body);
24705 }
24706 else
24707 /* Just complain. */
24708 dwarf2_macro_malformed_definition_complaint (body);
24709
24710 xfree (name);
24711 {
24712 int i;
24713
24714 for (i = 0; i < argc; i++)
24715 xfree (argv[i]);
24716 }
24717 xfree (argv);
24718 }
24719 else
24720 dwarf2_macro_malformed_definition_complaint (body);
24721 }
24722
24723 /* Skip some bytes from BYTES according to the form given in FORM.
24724 Returns the new pointer. */
24725
24726 static const gdb_byte *
24727 skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
24728 enum dwarf_form form,
24729 unsigned int offset_size,
24730 struct dwarf2_section_info *section)
24731 {
24732 unsigned int bytes_read;
24733
24734 switch (form)
24735 {
24736 case DW_FORM_data1:
24737 case DW_FORM_flag:
24738 ++bytes;
24739 break;
24740
24741 case DW_FORM_data2:
24742 bytes += 2;
24743 break;
24744
24745 case DW_FORM_data4:
24746 bytes += 4;
24747 break;
24748
24749 case DW_FORM_data8:
24750 bytes += 8;
24751 break;
24752
24753 case DW_FORM_data16:
24754 bytes += 16;
24755 break;
24756
24757 case DW_FORM_string:
24758 read_direct_string (abfd, bytes, &bytes_read);
24759 bytes += bytes_read;
24760 break;
24761
24762 case DW_FORM_sec_offset:
24763 case DW_FORM_strp:
24764 case DW_FORM_GNU_strp_alt:
24765 bytes += offset_size;
24766 break;
24767
24768 case DW_FORM_block:
24769 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
24770 bytes += bytes_read;
24771 break;
24772
24773 case DW_FORM_block1:
24774 bytes += 1 + read_1_byte (abfd, bytes);
24775 break;
24776 case DW_FORM_block2:
24777 bytes += 2 + read_2_bytes (abfd, bytes);
24778 break;
24779 case DW_FORM_block4:
24780 bytes += 4 + read_4_bytes (abfd, bytes);
24781 break;
24782
24783 case DW_FORM_addrx:
24784 case DW_FORM_sdata:
24785 case DW_FORM_strx:
24786 case DW_FORM_udata:
24787 case DW_FORM_GNU_addr_index:
24788 case DW_FORM_GNU_str_index:
24789 bytes = gdb_skip_leb128 (bytes, buffer_end);
24790 if (bytes == NULL)
24791 {
24792 dwarf2_section_buffer_overflow_complaint (section);
24793 return NULL;
24794 }
24795 break;
24796
24797 case DW_FORM_implicit_const:
24798 break;
24799
24800 default:
24801 {
24802 complaint (_("invalid form 0x%x in `%s'"),
24803 form, get_section_name (section));
24804 return NULL;
24805 }
24806 }
24807
24808 return bytes;
24809 }
24810
24811 /* A helper for dwarf_decode_macros that handles skipping an unknown
24812 opcode. Returns an updated pointer to the macro data buffer; or,
24813 on error, issues a complaint and returns NULL. */
24814
24815 static const gdb_byte *
24816 skip_unknown_opcode (unsigned int opcode,
24817 const gdb_byte **opcode_definitions,
24818 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
24819 bfd *abfd,
24820 unsigned int offset_size,
24821 struct dwarf2_section_info *section)
24822 {
24823 unsigned int bytes_read, i;
24824 unsigned long arg;
24825 const gdb_byte *defn;
24826
24827 if (opcode_definitions[opcode] == NULL)
24828 {
24829 complaint (_("unrecognized DW_MACFINO opcode 0x%x"),
24830 opcode);
24831 return NULL;
24832 }
24833
24834 defn = opcode_definitions[opcode];
24835 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
24836 defn += bytes_read;
24837
24838 for (i = 0; i < arg; ++i)
24839 {
24840 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
24841 (enum dwarf_form) defn[i], offset_size,
24842 section);
24843 if (mac_ptr == NULL)
24844 {
24845 /* skip_form_bytes already issued the complaint. */
24846 return NULL;
24847 }
24848 }
24849
24850 return mac_ptr;
24851 }
24852
24853 /* A helper function which parses the header of a macro section.
24854 If the macro section is the extended (for now called "GNU") type,
24855 then this updates *OFFSET_SIZE. Returns a pointer to just after
24856 the header, or issues a complaint and returns NULL on error. */
24857
24858 static const gdb_byte *
24859 dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
24860 bfd *abfd,
24861 const gdb_byte *mac_ptr,
24862 unsigned int *offset_size,
24863 int section_is_gnu)
24864 {
24865 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
24866
24867 if (section_is_gnu)
24868 {
24869 unsigned int version, flags;
24870
24871 version = read_2_bytes (abfd, mac_ptr);
24872 if (version != 4 && version != 5)
24873 {
24874 complaint (_("unrecognized version `%d' in .debug_macro section"),
24875 version);
24876 return NULL;
24877 }
24878 mac_ptr += 2;
24879
24880 flags = read_1_byte (abfd, mac_ptr);
24881 ++mac_ptr;
24882 *offset_size = (flags & 1) ? 8 : 4;
24883
24884 if ((flags & 2) != 0)
24885 /* We don't need the line table offset. */
24886 mac_ptr += *offset_size;
24887
24888 /* Vendor opcode descriptions. */
24889 if ((flags & 4) != 0)
24890 {
24891 unsigned int i, count;
24892
24893 count = read_1_byte (abfd, mac_ptr);
24894 ++mac_ptr;
24895 for (i = 0; i < count; ++i)
24896 {
24897 unsigned int opcode, bytes_read;
24898 unsigned long arg;
24899
24900 opcode = read_1_byte (abfd, mac_ptr);
24901 ++mac_ptr;
24902 opcode_definitions[opcode] = mac_ptr;
24903 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24904 mac_ptr += bytes_read;
24905 mac_ptr += arg;
24906 }
24907 }
24908 }
24909
24910 return mac_ptr;
24911 }
24912
24913 /* A helper for dwarf_decode_macros that handles the GNU extensions,
24914 including DW_MACRO_import. */
24915
24916 static void
24917 dwarf_decode_macro_bytes (struct dwarf2_cu *cu,
24918 bfd *abfd,
24919 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
24920 struct macro_source_file *current_file,
24921 struct line_header *lh,
24922 struct dwarf2_section_info *section,
24923 int section_is_gnu, int section_is_dwz,
24924 unsigned int offset_size,
24925 htab_t include_hash)
24926 {
24927 struct dwarf2_per_objfile *dwarf2_per_objfile
24928 = cu->per_cu->dwarf2_per_objfile;
24929 struct objfile *objfile = dwarf2_per_objfile->objfile;
24930 enum dwarf_macro_record_type macinfo_type;
24931 int at_commandline;
24932 const gdb_byte *opcode_definitions[256];
24933
24934 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24935 &offset_size, section_is_gnu);
24936 if (mac_ptr == NULL)
24937 {
24938 /* We already issued a complaint. */
24939 return;
24940 }
24941
24942 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
24943 GDB is still reading the definitions from command line. First
24944 DW_MACINFO_start_file will need to be ignored as it was already executed
24945 to create CURRENT_FILE for the main source holding also the command line
24946 definitions. On first met DW_MACINFO_start_file this flag is reset to
24947 normally execute all the remaining DW_MACINFO_start_file macinfos. */
24948
24949 at_commandline = 1;
24950
24951 do
24952 {
24953 /* Do we at least have room for a macinfo type byte? */
24954 if (mac_ptr >= mac_end)
24955 {
24956 dwarf2_section_buffer_overflow_complaint (section);
24957 break;
24958 }
24959
24960 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
24961 mac_ptr++;
24962
24963 /* Note that we rely on the fact that the corresponding GNU and
24964 DWARF constants are the same. */
24965 DIAGNOSTIC_PUSH
24966 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
24967 switch (macinfo_type)
24968 {
24969 /* A zero macinfo type indicates the end of the macro
24970 information. */
24971 case 0:
24972 break;
24973
24974 case DW_MACRO_define:
24975 case DW_MACRO_undef:
24976 case DW_MACRO_define_strp:
24977 case DW_MACRO_undef_strp:
24978 case DW_MACRO_define_sup:
24979 case DW_MACRO_undef_sup:
24980 {
24981 unsigned int bytes_read;
24982 int line;
24983 const char *body;
24984 int is_define;
24985
24986 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24987 mac_ptr += bytes_read;
24988
24989 if (macinfo_type == DW_MACRO_define
24990 || macinfo_type == DW_MACRO_undef)
24991 {
24992 body = read_direct_string (abfd, mac_ptr, &bytes_read);
24993 mac_ptr += bytes_read;
24994 }
24995 else
24996 {
24997 LONGEST str_offset;
24998
24999 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
25000 mac_ptr += offset_size;
25001
25002 if (macinfo_type == DW_MACRO_define_sup
25003 || macinfo_type == DW_MACRO_undef_sup
25004 || section_is_dwz)
25005 {
25006 struct dwz_file *dwz
25007 = dwarf2_get_dwz_file (dwarf2_per_objfile);
25008
25009 body = read_indirect_string_from_dwz (objfile,
25010 dwz, str_offset);
25011 }
25012 else
25013 body = read_indirect_string_at_offset (dwarf2_per_objfile,
25014 abfd, str_offset);
25015 }
25016
25017 is_define = (macinfo_type == DW_MACRO_define
25018 || macinfo_type == DW_MACRO_define_strp
25019 || macinfo_type == DW_MACRO_define_sup);
25020 if (! current_file)
25021 {
25022 /* DWARF violation as no main source is present. */
25023 complaint (_("debug info with no main source gives macro %s "
25024 "on line %d: %s"),
25025 is_define ? _("definition") : _("undefinition"),
25026 line, body);
25027 break;
25028 }
25029 if ((line == 0 && !at_commandline)
25030 || (line != 0 && at_commandline))
25031 complaint (_("debug info gives %s macro %s with %s line %d: %s"),
25032 at_commandline ? _("command-line") : _("in-file"),
25033 is_define ? _("definition") : _("undefinition"),
25034 line == 0 ? _("zero") : _("non-zero"), line, body);
25035
25036 if (body == NULL)
25037 {
25038 /* Fedora's rpm-build's "debugedit" binary
25039 corrupted .debug_macro sections.
25040
25041 For more info, see
25042 https://bugzilla.redhat.com/show_bug.cgi?id=1708786 */
25043 complaint (_("debug info gives %s invalid macro %s "
25044 "without body (corrupted?) at line %d "
25045 "on file %s"),
25046 at_commandline ? _("command-line") : _("in-file"),
25047 is_define ? _("definition") : _("undefinition"),
25048 line, current_file->filename);
25049 }
25050 else if (is_define)
25051 parse_macro_definition (current_file, line, body);
25052 else
25053 {
25054 gdb_assert (macinfo_type == DW_MACRO_undef
25055 || macinfo_type == DW_MACRO_undef_strp
25056 || macinfo_type == DW_MACRO_undef_sup);
25057 macro_undef (current_file, line, body);
25058 }
25059 }
25060 break;
25061
25062 case DW_MACRO_start_file:
25063 {
25064 unsigned int bytes_read;
25065 int line, file;
25066
25067 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25068 mac_ptr += bytes_read;
25069 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25070 mac_ptr += bytes_read;
25071
25072 if ((line == 0 && !at_commandline)
25073 || (line != 0 && at_commandline))
25074 complaint (_("debug info gives source %d included "
25075 "from %s at %s line %d"),
25076 file, at_commandline ? _("command-line") : _("file"),
25077 line == 0 ? _("zero") : _("non-zero"), line);
25078
25079 if (at_commandline)
25080 {
25081 /* This DW_MACRO_start_file was executed in the
25082 pass one. */
25083 at_commandline = 0;
25084 }
25085 else
25086 current_file = macro_start_file (cu, file, line, current_file,
25087 lh);
25088 }
25089 break;
25090
25091 case DW_MACRO_end_file:
25092 if (! current_file)
25093 complaint (_("macro debug info has an unmatched "
25094 "`close_file' directive"));
25095 else
25096 {
25097 current_file = current_file->included_by;
25098 if (! current_file)
25099 {
25100 enum dwarf_macro_record_type next_type;
25101
25102 /* GCC circa March 2002 doesn't produce the zero
25103 type byte marking the end of the compilation
25104 unit. Complain if it's not there, but exit no
25105 matter what. */
25106
25107 /* Do we at least have room for a macinfo type byte? */
25108 if (mac_ptr >= mac_end)
25109 {
25110 dwarf2_section_buffer_overflow_complaint (section);
25111 return;
25112 }
25113
25114 /* We don't increment mac_ptr here, so this is just
25115 a look-ahead. */
25116 next_type
25117 = (enum dwarf_macro_record_type) read_1_byte (abfd,
25118 mac_ptr);
25119 if (next_type != 0)
25120 complaint (_("no terminating 0-type entry for "
25121 "macros in `.debug_macinfo' section"));
25122
25123 return;
25124 }
25125 }
25126 break;
25127
25128 case DW_MACRO_import:
25129 case DW_MACRO_import_sup:
25130 {
25131 LONGEST offset;
25132 void **slot;
25133 bfd *include_bfd = abfd;
25134 struct dwarf2_section_info *include_section = section;
25135 const gdb_byte *include_mac_end = mac_end;
25136 int is_dwz = section_is_dwz;
25137 const gdb_byte *new_mac_ptr;
25138
25139 offset = read_offset_1 (abfd, mac_ptr, offset_size);
25140 mac_ptr += offset_size;
25141
25142 if (macinfo_type == DW_MACRO_import_sup)
25143 {
25144 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
25145
25146 dwarf2_read_section (objfile, &dwz->macro);
25147
25148 include_section = &dwz->macro;
25149 include_bfd = get_section_bfd_owner (include_section);
25150 include_mac_end = dwz->macro.buffer + dwz->macro.size;
25151 is_dwz = 1;
25152 }
25153
25154 new_mac_ptr = include_section->buffer + offset;
25155 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
25156
25157 if (*slot != NULL)
25158 {
25159 /* This has actually happened; see
25160 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
25161 complaint (_("recursive DW_MACRO_import in "
25162 ".debug_macro section"));
25163 }
25164 else
25165 {
25166 *slot = (void *) new_mac_ptr;
25167
25168 dwarf_decode_macro_bytes (cu, include_bfd, new_mac_ptr,
25169 include_mac_end, current_file, lh,
25170 section, section_is_gnu, is_dwz,
25171 offset_size, include_hash);
25172
25173 htab_remove_elt (include_hash, (void *) new_mac_ptr);
25174 }
25175 }
25176 break;
25177
25178 case DW_MACINFO_vendor_ext:
25179 if (!section_is_gnu)
25180 {
25181 unsigned int bytes_read;
25182
25183 /* This reads the constant, but since we don't recognize
25184 any vendor extensions, we ignore it. */
25185 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25186 mac_ptr += bytes_read;
25187 read_direct_string (abfd, mac_ptr, &bytes_read);
25188 mac_ptr += bytes_read;
25189
25190 /* We don't recognize any vendor extensions. */
25191 break;
25192 }
25193 /* FALLTHROUGH */
25194
25195 default:
25196 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
25197 mac_ptr, mac_end, abfd, offset_size,
25198 section);
25199 if (mac_ptr == NULL)
25200 return;
25201 break;
25202 }
25203 DIAGNOSTIC_POP
25204 } while (macinfo_type != 0);
25205 }
25206
25207 static void
25208 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
25209 int section_is_gnu)
25210 {
25211 struct dwarf2_per_objfile *dwarf2_per_objfile
25212 = cu->per_cu->dwarf2_per_objfile;
25213 struct objfile *objfile = dwarf2_per_objfile->objfile;
25214 struct line_header *lh = cu->line_header;
25215 bfd *abfd;
25216 const gdb_byte *mac_ptr, *mac_end;
25217 struct macro_source_file *current_file = 0;
25218 enum dwarf_macro_record_type macinfo_type;
25219 unsigned int offset_size = cu->header.offset_size;
25220 const gdb_byte *opcode_definitions[256];
25221 void **slot;
25222 struct dwarf2_section_info *section;
25223 const char *section_name;
25224
25225 if (cu->dwo_unit != NULL)
25226 {
25227 if (section_is_gnu)
25228 {
25229 section = &cu->dwo_unit->dwo_file->sections.macro;
25230 section_name = ".debug_macro.dwo";
25231 }
25232 else
25233 {
25234 section = &cu->dwo_unit->dwo_file->sections.macinfo;
25235 section_name = ".debug_macinfo.dwo";
25236 }
25237 }
25238 else
25239 {
25240 if (section_is_gnu)
25241 {
25242 section = &dwarf2_per_objfile->macro;
25243 section_name = ".debug_macro";
25244 }
25245 else
25246 {
25247 section = &dwarf2_per_objfile->macinfo;
25248 section_name = ".debug_macinfo";
25249 }
25250 }
25251
25252 dwarf2_read_section (objfile, section);
25253 if (section->buffer == NULL)
25254 {
25255 complaint (_("missing %s section"), section_name);
25256 return;
25257 }
25258 abfd = get_section_bfd_owner (section);
25259
25260 /* First pass: Find the name of the base filename.
25261 This filename is needed in order to process all macros whose definition
25262 (or undefinition) comes from the command line. These macros are defined
25263 before the first DW_MACINFO_start_file entry, and yet still need to be
25264 associated to the base file.
25265
25266 To determine the base file name, we scan the macro definitions until we
25267 reach the first DW_MACINFO_start_file entry. We then initialize
25268 CURRENT_FILE accordingly so that any macro definition found before the
25269 first DW_MACINFO_start_file can still be associated to the base file. */
25270
25271 mac_ptr = section->buffer + offset;
25272 mac_end = section->buffer + section->size;
25273
25274 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
25275 &offset_size, section_is_gnu);
25276 if (mac_ptr == NULL)
25277 {
25278 /* We already issued a complaint. */
25279 return;
25280 }
25281
25282 do
25283 {
25284 /* Do we at least have room for a macinfo type byte? */
25285 if (mac_ptr >= mac_end)
25286 {
25287 /* Complaint is printed during the second pass as GDB will probably
25288 stop the first pass earlier upon finding
25289 DW_MACINFO_start_file. */
25290 break;
25291 }
25292
25293 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
25294 mac_ptr++;
25295
25296 /* Note that we rely on the fact that the corresponding GNU and
25297 DWARF constants are the same. */
25298 DIAGNOSTIC_PUSH
25299 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
25300 switch (macinfo_type)
25301 {
25302 /* A zero macinfo type indicates the end of the macro
25303 information. */
25304 case 0:
25305 break;
25306
25307 case DW_MACRO_define:
25308 case DW_MACRO_undef:
25309 /* Only skip the data by MAC_PTR. */
25310 {
25311 unsigned int bytes_read;
25312
25313 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25314 mac_ptr += bytes_read;
25315 read_direct_string (abfd, mac_ptr, &bytes_read);
25316 mac_ptr += bytes_read;
25317 }
25318 break;
25319
25320 case DW_MACRO_start_file:
25321 {
25322 unsigned int bytes_read;
25323 int line, file;
25324
25325 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25326 mac_ptr += bytes_read;
25327 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25328 mac_ptr += bytes_read;
25329
25330 current_file = macro_start_file (cu, file, line, current_file, lh);
25331 }
25332 break;
25333
25334 case DW_MACRO_end_file:
25335 /* No data to skip by MAC_PTR. */
25336 break;
25337
25338 case DW_MACRO_define_strp:
25339 case DW_MACRO_undef_strp:
25340 case DW_MACRO_define_sup:
25341 case DW_MACRO_undef_sup:
25342 {
25343 unsigned int bytes_read;
25344
25345 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25346 mac_ptr += bytes_read;
25347 mac_ptr += offset_size;
25348 }
25349 break;
25350
25351 case DW_MACRO_import:
25352 case DW_MACRO_import_sup:
25353 /* Note that, according to the spec, a transparent include
25354 chain cannot call DW_MACRO_start_file. So, we can just
25355 skip this opcode. */
25356 mac_ptr += offset_size;
25357 break;
25358
25359 case DW_MACINFO_vendor_ext:
25360 /* Only skip the data by MAC_PTR. */
25361 if (!section_is_gnu)
25362 {
25363 unsigned int bytes_read;
25364
25365 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25366 mac_ptr += bytes_read;
25367 read_direct_string (abfd, mac_ptr, &bytes_read);
25368 mac_ptr += bytes_read;
25369 }
25370 /* FALLTHROUGH */
25371
25372 default:
25373 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
25374 mac_ptr, mac_end, abfd, offset_size,
25375 section);
25376 if (mac_ptr == NULL)
25377 return;
25378 break;
25379 }
25380 DIAGNOSTIC_POP
25381 } while (macinfo_type != 0 && current_file == NULL);
25382
25383 /* Second pass: Process all entries.
25384
25385 Use the AT_COMMAND_LINE flag to determine whether we are still processing
25386 command-line macro definitions/undefinitions. This flag is unset when we
25387 reach the first DW_MACINFO_start_file entry. */
25388
25389 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
25390 htab_eq_pointer,
25391 NULL, xcalloc, xfree));
25392 mac_ptr = section->buffer + offset;
25393 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
25394 *slot = (void *) mac_ptr;
25395 dwarf_decode_macro_bytes (cu, abfd, mac_ptr, mac_end,
25396 current_file, lh, section,
25397 section_is_gnu, 0, offset_size,
25398 include_hash.get ());
25399 }
25400
25401 /* Check if the attribute's form is a DW_FORM_block*
25402 if so return true else false. */
25403
25404 static int
25405 attr_form_is_block (const struct attribute *attr)
25406 {
25407 return (attr == NULL ? 0 :
25408 attr->form == DW_FORM_block1
25409 || attr->form == DW_FORM_block2
25410 || attr->form == DW_FORM_block4
25411 || attr->form == DW_FORM_block
25412 || attr->form == DW_FORM_exprloc);
25413 }
25414
25415 /* Return non-zero if ATTR's value is a section offset --- classes
25416 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
25417 You may use DW_UNSND (attr) to retrieve such offsets.
25418
25419 Section 7.5.4, "Attribute Encodings", explains that no attribute
25420 may have a value that belongs to more than one of these classes; it
25421 would be ambiguous if we did, because we use the same forms for all
25422 of them. */
25423
25424 static int
25425 attr_form_is_section_offset (const struct attribute *attr)
25426 {
25427 return (attr->form == DW_FORM_data4
25428 || attr->form == DW_FORM_data8
25429 || attr->form == DW_FORM_sec_offset);
25430 }
25431
25432 /* Return non-zero if ATTR's value falls in the 'constant' class, or
25433 zero otherwise. When this function returns true, you can apply
25434 dwarf2_get_attr_constant_value to it.
25435
25436 However, note that for some attributes you must check
25437 attr_form_is_section_offset before using this test. DW_FORM_data4
25438 and DW_FORM_data8 are members of both the constant class, and of
25439 the classes that contain offsets into other debug sections
25440 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
25441 that, if an attribute's can be either a constant or one of the
25442 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
25443 taken as section offsets, not constants.
25444
25445 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
25446 cannot handle that. */
25447
25448 static int
25449 attr_form_is_constant (const struct attribute *attr)
25450 {
25451 switch (attr->form)
25452 {
25453 case DW_FORM_sdata:
25454 case DW_FORM_udata:
25455 case DW_FORM_data1:
25456 case DW_FORM_data2:
25457 case DW_FORM_data4:
25458 case DW_FORM_data8:
25459 case DW_FORM_implicit_const:
25460 return 1;
25461 default:
25462 return 0;
25463 }
25464 }
25465
25466
25467 /* DW_ADDR is always stored already as sect_offset; despite for the forms
25468 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
25469
25470 static int
25471 attr_form_is_ref (const struct attribute *attr)
25472 {
25473 switch (attr->form)
25474 {
25475 case DW_FORM_ref_addr:
25476 case DW_FORM_ref1:
25477 case DW_FORM_ref2:
25478 case DW_FORM_ref4:
25479 case DW_FORM_ref8:
25480 case DW_FORM_ref_udata:
25481 case DW_FORM_GNU_ref_alt:
25482 return 1;
25483 default:
25484 return 0;
25485 }
25486 }
25487
25488 /* Return the .debug_loc section to use for CU.
25489 For DWO files use .debug_loc.dwo. */
25490
25491 static struct dwarf2_section_info *
25492 cu_debug_loc_section (struct dwarf2_cu *cu)
25493 {
25494 struct dwarf2_per_objfile *dwarf2_per_objfile
25495 = cu->per_cu->dwarf2_per_objfile;
25496
25497 if (cu->dwo_unit)
25498 {
25499 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
25500
25501 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
25502 }
25503 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
25504 : &dwarf2_per_objfile->loc);
25505 }
25506
25507 /* A helper function that fills in a dwarf2_loclist_baton. */
25508
25509 static void
25510 fill_in_loclist_baton (struct dwarf2_cu *cu,
25511 struct dwarf2_loclist_baton *baton,
25512 const struct attribute *attr)
25513 {
25514 struct dwarf2_per_objfile *dwarf2_per_objfile
25515 = cu->per_cu->dwarf2_per_objfile;
25516 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
25517
25518 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
25519
25520 baton->per_cu = cu->per_cu;
25521 gdb_assert (baton->per_cu);
25522 /* We don't know how long the location list is, but make sure we
25523 don't run off the edge of the section. */
25524 baton->size = section->size - DW_UNSND (attr);
25525 baton->data = section->buffer + DW_UNSND (attr);
25526 baton->base_address = cu->base_address;
25527 baton->from_dwo = cu->dwo_unit != NULL;
25528 }
25529
25530 static void
25531 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
25532 struct dwarf2_cu *cu, int is_block)
25533 {
25534 struct dwarf2_per_objfile *dwarf2_per_objfile
25535 = cu->per_cu->dwarf2_per_objfile;
25536 struct objfile *objfile = dwarf2_per_objfile->objfile;
25537 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
25538
25539 if (attr_form_is_section_offset (attr)
25540 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
25541 the section. If so, fall through to the complaint in the
25542 other branch. */
25543 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
25544 {
25545 struct dwarf2_loclist_baton *baton;
25546
25547 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
25548
25549 fill_in_loclist_baton (cu, baton, attr);
25550
25551 if (cu->base_known == 0)
25552 complaint (_("Location list used without "
25553 "specifying the CU base address."));
25554
25555 SYMBOL_ACLASS_INDEX (sym) = (is_block
25556 ? dwarf2_loclist_block_index
25557 : dwarf2_loclist_index);
25558 SYMBOL_LOCATION_BATON (sym) = baton;
25559 }
25560 else
25561 {
25562 struct dwarf2_locexpr_baton *baton;
25563
25564 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
25565 baton->per_cu = cu->per_cu;
25566 gdb_assert (baton->per_cu);
25567
25568 if (attr_form_is_block (attr))
25569 {
25570 /* Note that we're just copying the block's data pointer
25571 here, not the actual data. We're still pointing into the
25572 info_buffer for SYM's objfile; right now we never release
25573 that buffer, but when we do clean up properly this may
25574 need to change. */
25575 baton->size = DW_BLOCK (attr)->size;
25576 baton->data = DW_BLOCK (attr)->data;
25577 }
25578 else
25579 {
25580 dwarf2_invalid_attrib_class_complaint ("location description",
25581 sym->natural_name ());
25582 baton->size = 0;
25583 }
25584
25585 SYMBOL_ACLASS_INDEX (sym) = (is_block
25586 ? dwarf2_locexpr_block_index
25587 : dwarf2_locexpr_index);
25588 SYMBOL_LOCATION_BATON (sym) = baton;
25589 }
25590 }
25591
25592 /* Return the OBJFILE associated with the compilation unit CU. If CU
25593 came from a separate debuginfo file, then the master objfile is
25594 returned. */
25595
25596 struct objfile *
25597 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
25598 {
25599 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
25600
25601 /* Return the master objfile, so that we can report and look up the
25602 correct file containing this variable. */
25603 if (objfile->separate_debug_objfile_backlink)
25604 objfile = objfile->separate_debug_objfile_backlink;
25605
25606 return objfile;
25607 }
25608
25609 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
25610 (CU_HEADERP is unused in such case) or prepare a temporary copy at
25611 CU_HEADERP first. */
25612
25613 static const struct comp_unit_head *
25614 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
25615 struct dwarf2_per_cu_data *per_cu)
25616 {
25617 const gdb_byte *info_ptr;
25618
25619 if (per_cu->cu)
25620 return &per_cu->cu->header;
25621
25622 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
25623
25624 memset (cu_headerp, 0, sizeof (*cu_headerp));
25625 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
25626 rcuh_kind::COMPILE);
25627
25628 return cu_headerp;
25629 }
25630
25631 /* Return the address size given in the compilation unit header for CU. */
25632
25633 int
25634 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
25635 {
25636 struct comp_unit_head cu_header_local;
25637 const struct comp_unit_head *cu_headerp;
25638
25639 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25640
25641 return cu_headerp->addr_size;
25642 }
25643
25644 /* Return the offset size given in the compilation unit header for CU. */
25645
25646 int
25647 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
25648 {
25649 struct comp_unit_head cu_header_local;
25650 const struct comp_unit_head *cu_headerp;
25651
25652 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25653
25654 return cu_headerp->offset_size;
25655 }
25656
25657 /* See its dwarf2loc.h declaration. */
25658
25659 int
25660 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
25661 {
25662 struct comp_unit_head cu_header_local;
25663 const struct comp_unit_head *cu_headerp;
25664
25665 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25666
25667 if (cu_headerp->version == 2)
25668 return cu_headerp->addr_size;
25669 else
25670 return cu_headerp->offset_size;
25671 }
25672
25673 /* Return the text offset of the CU. The returned offset comes from
25674 this CU's objfile. If this objfile came from a separate debuginfo
25675 file, then the offset may be different from the corresponding
25676 offset in the parent objfile. */
25677
25678 CORE_ADDR
25679 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
25680 {
25681 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
25682
25683 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
25684 }
25685
25686 /* Return a type that is a generic pointer type, the size of which matches
25687 the address size given in the compilation unit header for PER_CU. */
25688 static struct type *
25689 dwarf2_per_cu_addr_type (struct dwarf2_per_cu_data *per_cu)
25690 {
25691 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
25692 struct type *void_type = objfile_type (objfile)->builtin_void;
25693 struct type *addr_type = lookup_pointer_type (void_type);
25694 int addr_size = dwarf2_per_cu_addr_size (per_cu);
25695
25696 if (TYPE_LENGTH (addr_type) == addr_size)
25697 return addr_type;
25698
25699 addr_type
25700 = dwarf2_per_cu_addr_sized_int_type (per_cu, TYPE_UNSIGNED (addr_type));
25701 return addr_type;
25702 }
25703
25704 /* Return DWARF version number of PER_CU. */
25705
25706 short
25707 dwarf2_version (struct dwarf2_per_cu_data *per_cu)
25708 {
25709 return per_cu->dwarf_version;
25710 }
25711
25712 /* Locate the .debug_info compilation unit from CU's objfile which contains
25713 the DIE at OFFSET. Raises an error on failure. */
25714
25715 static struct dwarf2_per_cu_data *
25716 dwarf2_find_containing_comp_unit (sect_offset sect_off,
25717 unsigned int offset_in_dwz,
25718 struct dwarf2_per_objfile *dwarf2_per_objfile)
25719 {
25720 struct dwarf2_per_cu_data *this_cu;
25721 int low, high;
25722
25723 low = 0;
25724 high = dwarf2_per_objfile->all_comp_units.size () - 1;
25725 while (high > low)
25726 {
25727 struct dwarf2_per_cu_data *mid_cu;
25728 int mid = low + (high - low) / 2;
25729
25730 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
25731 if (mid_cu->is_dwz > offset_in_dwz
25732 || (mid_cu->is_dwz == offset_in_dwz
25733 && mid_cu->sect_off + mid_cu->length >= sect_off))
25734 high = mid;
25735 else
25736 low = mid + 1;
25737 }
25738 gdb_assert (low == high);
25739 this_cu = dwarf2_per_objfile->all_comp_units[low];
25740 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
25741 {
25742 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
25743 error (_("Dwarf Error: could not find partial DIE containing "
25744 "offset %s [in module %s]"),
25745 sect_offset_str (sect_off),
25746 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
25747
25748 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
25749 <= sect_off);
25750 return dwarf2_per_objfile->all_comp_units[low-1];
25751 }
25752 else
25753 {
25754 if (low == dwarf2_per_objfile->all_comp_units.size () - 1
25755 && sect_off >= this_cu->sect_off + this_cu->length)
25756 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
25757 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
25758 return this_cu;
25759 }
25760 }
25761
25762 /* Initialize dwarf2_cu CU, owned by PER_CU. */
25763
25764 dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
25765 : per_cu (per_cu_),
25766 mark (false),
25767 has_loclist (false),
25768 checked_producer (false),
25769 producer_is_gxx_lt_4_6 (false),
25770 producer_is_gcc_lt_4_3 (false),
25771 producer_is_icc (false),
25772 producer_is_icc_lt_14 (false),
25773 producer_is_codewarrior (false),
25774 processing_has_namespace_info (false)
25775 {
25776 per_cu->cu = this;
25777 }
25778
25779 /* Destroy a dwarf2_cu. */
25780
25781 dwarf2_cu::~dwarf2_cu ()
25782 {
25783 per_cu->cu = NULL;
25784 }
25785
25786 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
25787
25788 static void
25789 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
25790 enum language pretend_language)
25791 {
25792 struct attribute *attr;
25793
25794 /* Set the language we're debugging. */
25795 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
25796 if (attr != nullptr)
25797 set_cu_language (DW_UNSND (attr), cu);
25798 else
25799 {
25800 cu->language = pretend_language;
25801 cu->language_defn = language_def (cu->language);
25802 }
25803
25804 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
25805 }
25806
25807 /* Increase the age counter on each cached compilation unit, and free
25808 any that are too old. */
25809
25810 static void
25811 age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
25812 {
25813 struct dwarf2_per_cu_data *per_cu, **last_chain;
25814
25815 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
25816 per_cu = dwarf2_per_objfile->read_in_chain;
25817 while (per_cu != NULL)
25818 {
25819 per_cu->cu->last_used ++;
25820 if (per_cu->cu->last_used <= dwarf_max_cache_age)
25821 dwarf2_mark (per_cu->cu);
25822 per_cu = per_cu->cu->read_in_chain;
25823 }
25824
25825 per_cu = dwarf2_per_objfile->read_in_chain;
25826 last_chain = &dwarf2_per_objfile->read_in_chain;
25827 while (per_cu != NULL)
25828 {
25829 struct dwarf2_per_cu_data *next_cu;
25830
25831 next_cu = per_cu->cu->read_in_chain;
25832
25833 if (!per_cu->cu->mark)
25834 {
25835 delete per_cu->cu;
25836 *last_chain = next_cu;
25837 }
25838 else
25839 last_chain = &per_cu->cu->read_in_chain;
25840
25841 per_cu = next_cu;
25842 }
25843 }
25844
25845 /* Remove a single compilation unit from the cache. */
25846
25847 static void
25848 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
25849 {
25850 struct dwarf2_per_cu_data *per_cu, **last_chain;
25851 struct dwarf2_per_objfile *dwarf2_per_objfile
25852 = target_per_cu->dwarf2_per_objfile;
25853
25854 per_cu = dwarf2_per_objfile->read_in_chain;
25855 last_chain = &dwarf2_per_objfile->read_in_chain;
25856 while (per_cu != NULL)
25857 {
25858 struct dwarf2_per_cu_data *next_cu;
25859
25860 next_cu = per_cu->cu->read_in_chain;
25861
25862 if (per_cu == target_per_cu)
25863 {
25864 delete per_cu->cu;
25865 per_cu->cu = NULL;
25866 *last_chain = next_cu;
25867 break;
25868 }
25869 else
25870 last_chain = &per_cu->cu->read_in_chain;
25871
25872 per_cu = next_cu;
25873 }
25874 }
25875
25876 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25877 We store these in a hash table separate from the DIEs, and preserve them
25878 when the DIEs are flushed out of cache.
25879
25880 The CU "per_cu" pointer is needed because offset alone is not enough to
25881 uniquely identify the type. A file may have multiple .debug_types sections,
25882 or the type may come from a DWO file. Furthermore, while it's more logical
25883 to use per_cu->section+offset, with Fission the section with the data is in
25884 the DWO file but we don't know that section at the point we need it.
25885 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25886 because we can enter the lookup routine, get_die_type_at_offset, from
25887 outside this file, and thus won't necessarily have PER_CU->cu.
25888 Fortunately, PER_CU is stable for the life of the objfile. */
25889
25890 struct dwarf2_per_cu_offset_and_type
25891 {
25892 const struct dwarf2_per_cu_data *per_cu;
25893 sect_offset sect_off;
25894 struct type *type;
25895 };
25896
25897 /* Hash function for a dwarf2_per_cu_offset_and_type. */
25898
25899 static hashval_t
25900 per_cu_offset_and_type_hash (const void *item)
25901 {
25902 const struct dwarf2_per_cu_offset_and_type *ofs
25903 = (const struct dwarf2_per_cu_offset_and_type *) item;
25904
25905 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
25906 }
25907
25908 /* Equality function for a dwarf2_per_cu_offset_and_type. */
25909
25910 static int
25911 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
25912 {
25913 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25914 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25915 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25916 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
25917
25918 return (ofs_lhs->per_cu == ofs_rhs->per_cu
25919 && ofs_lhs->sect_off == ofs_rhs->sect_off);
25920 }
25921
25922 /* Set the type associated with DIE to TYPE. Save it in CU's hash
25923 table if necessary. For convenience, return TYPE.
25924
25925 The DIEs reading must have careful ordering to:
25926 * Not cause infinite loops trying to read in DIEs as a prerequisite for
25927 reading current DIE.
25928 * Not trying to dereference contents of still incompletely read in types
25929 while reading in other DIEs.
25930 * Enable referencing still incompletely read in types just by a pointer to
25931 the type without accessing its fields.
25932
25933 Therefore caller should follow these rules:
25934 * Try to fetch any prerequisite types we may need to build this DIE type
25935 before building the type and calling set_die_type.
25936 * After building type call set_die_type for current DIE as soon as
25937 possible before fetching more types to complete the current type.
25938 * Make the type as complete as possible before fetching more types. */
25939
25940 static struct type *
25941 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
25942 {
25943 struct dwarf2_per_objfile *dwarf2_per_objfile
25944 = cu->per_cu->dwarf2_per_objfile;
25945 struct dwarf2_per_cu_offset_and_type **slot, ofs;
25946 struct objfile *objfile = dwarf2_per_objfile->objfile;
25947 struct attribute *attr;
25948 struct dynamic_prop prop;
25949
25950 /* For Ada types, make sure that the gnat-specific data is always
25951 initialized (if not already set). There are a few types where
25952 we should not be doing so, because the type-specific area is
25953 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25954 where the type-specific area is used to store the floatformat).
25955 But this is not a problem, because the gnat-specific information
25956 is actually not needed for these types. */
25957 if (need_gnat_info (cu)
25958 && TYPE_CODE (type) != TYPE_CODE_FUNC
25959 && TYPE_CODE (type) != TYPE_CODE_FLT
25960 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
25961 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
25962 && TYPE_CODE (type) != TYPE_CODE_METHOD
25963 && !HAVE_GNAT_AUX_INFO (type))
25964 INIT_GNAT_SPECIFIC (type);
25965
25966 /* Read DW_AT_allocated and set in type. */
25967 attr = dwarf2_attr (die, DW_AT_allocated, cu);
25968 if (attr_form_is_block (attr))
25969 {
25970 struct type *prop_type
25971 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
25972 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
25973 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
25974 }
25975 else if (attr != NULL)
25976 {
25977 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
25978 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25979 sect_offset_str (die->sect_off));
25980 }
25981
25982 /* Read DW_AT_associated and set in type. */
25983 attr = dwarf2_attr (die, DW_AT_associated, cu);
25984 if (attr_form_is_block (attr))
25985 {
25986 struct type *prop_type
25987 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
25988 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
25989 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
25990 }
25991 else if (attr != NULL)
25992 {
25993 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
25994 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25995 sect_offset_str (die->sect_off));
25996 }
25997
25998 /* Read DW_AT_data_location and set in type. */
25999 attr = dwarf2_attr (die, DW_AT_data_location, cu);
26000 if (attr_to_dynamic_prop (attr, die, cu, &prop,
26001 dwarf2_per_cu_addr_type (cu->per_cu)))
26002 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
26003
26004 if (dwarf2_per_objfile->die_type_hash == NULL)
26005 {
26006 dwarf2_per_objfile->die_type_hash =
26007 htab_create_alloc_ex (127,
26008 per_cu_offset_and_type_hash,
26009 per_cu_offset_and_type_eq,
26010 NULL,
26011 &objfile->objfile_obstack,
26012 hashtab_obstack_allocate,
26013 dummy_obstack_deallocate);
26014 }
26015
26016 ofs.per_cu = cu->per_cu;
26017 ofs.sect_off = die->sect_off;
26018 ofs.type = type;
26019 slot = (struct dwarf2_per_cu_offset_and_type **)
26020 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
26021 if (*slot)
26022 complaint (_("A problem internal to GDB: DIE %s has type already set"),
26023 sect_offset_str (die->sect_off));
26024 *slot = XOBNEW (&objfile->objfile_obstack,
26025 struct dwarf2_per_cu_offset_and_type);
26026 **slot = ofs;
26027 return type;
26028 }
26029
26030 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
26031 or return NULL if the die does not have a saved type. */
26032
26033 static struct type *
26034 get_die_type_at_offset (sect_offset sect_off,
26035 struct dwarf2_per_cu_data *per_cu)
26036 {
26037 struct dwarf2_per_cu_offset_and_type *slot, ofs;
26038 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
26039
26040 if (dwarf2_per_objfile->die_type_hash == NULL)
26041 return NULL;
26042
26043 ofs.per_cu = per_cu;
26044 ofs.sect_off = sect_off;
26045 slot = ((struct dwarf2_per_cu_offset_and_type *)
26046 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
26047 if (slot)
26048 return slot->type;
26049 else
26050 return NULL;
26051 }
26052
26053 /* Look up the type for DIE in CU in die_type_hash,
26054 or return NULL if DIE does not have a saved type. */
26055
26056 static struct type *
26057 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
26058 {
26059 return get_die_type_at_offset (die->sect_off, cu->per_cu);
26060 }
26061
26062 /* Add a dependence relationship from CU to REF_PER_CU. */
26063
26064 static void
26065 dwarf2_add_dependence (struct dwarf2_cu *cu,
26066 struct dwarf2_per_cu_data *ref_per_cu)
26067 {
26068 void **slot;
26069
26070 if (cu->dependencies == NULL)
26071 cu->dependencies
26072 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
26073 NULL, &cu->comp_unit_obstack,
26074 hashtab_obstack_allocate,
26075 dummy_obstack_deallocate);
26076
26077 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
26078 if (*slot == NULL)
26079 *slot = ref_per_cu;
26080 }
26081
26082 /* Subroutine of dwarf2_mark to pass to htab_traverse.
26083 Set the mark field in every compilation unit in the
26084 cache that we must keep because we are keeping CU. */
26085
26086 static int
26087 dwarf2_mark_helper (void **slot, void *data)
26088 {
26089 struct dwarf2_per_cu_data *per_cu;
26090
26091 per_cu = (struct dwarf2_per_cu_data *) *slot;
26092
26093 /* cu->dependencies references may not yet have been ever read if QUIT aborts
26094 reading of the chain. As such dependencies remain valid it is not much
26095 useful to track and undo them during QUIT cleanups. */
26096 if (per_cu->cu == NULL)
26097 return 1;
26098
26099 if (per_cu->cu->mark)
26100 return 1;
26101 per_cu->cu->mark = true;
26102
26103 if (per_cu->cu->dependencies != NULL)
26104 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
26105
26106 return 1;
26107 }
26108
26109 /* Set the mark field in CU and in every other compilation unit in the
26110 cache that we must keep because we are keeping CU. */
26111
26112 static void
26113 dwarf2_mark (struct dwarf2_cu *cu)
26114 {
26115 if (cu->mark)
26116 return;
26117 cu->mark = true;
26118 if (cu->dependencies != NULL)
26119 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
26120 }
26121
26122 static void
26123 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
26124 {
26125 while (per_cu)
26126 {
26127 per_cu->cu->mark = false;
26128 per_cu = per_cu->cu->read_in_chain;
26129 }
26130 }
26131
26132 /* Trivial hash function for partial_die_info: the hash value of a DIE
26133 is its offset in .debug_info for this objfile. */
26134
26135 static hashval_t
26136 partial_die_hash (const void *item)
26137 {
26138 const struct partial_die_info *part_die
26139 = (const struct partial_die_info *) item;
26140
26141 return to_underlying (part_die->sect_off);
26142 }
26143
26144 /* Trivial comparison function for partial_die_info structures: two DIEs
26145 are equal if they have the same offset. */
26146
26147 static int
26148 partial_die_eq (const void *item_lhs, const void *item_rhs)
26149 {
26150 const struct partial_die_info *part_die_lhs
26151 = (const struct partial_die_info *) item_lhs;
26152 const struct partial_die_info *part_die_rhs
26153 = (const struct partial_die_info *) item_rhs;
26154
26155 return part_die_lhs->sect_off == part_die_rhs->sect_off;
26156 }
26157
26158 struct cmd_list_element *set_dwarf_cmdlist;
26159 struct cmd_list_element *show_dwarf_cmdlist;
26160
26161 static void
26162 set_dwarf_cmd (const char *args, int from_tty)
26163 {
26164 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
26165 gdb_stdout);
26166 }
26167
26168 static void
26169 show_dwarf_cmd (const char *args, int from_tty)
26170 {
26171 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
26172 }
26173
26174 bool dwarf_always_disassemble;
26175
26176 static void
26177 show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
26178 struct cmd_list_element *c, const char *value)
26179 {
26180 fprintf_filtered (file,
26181 _("Whether to always disassemble "
26182 "DWARF expressions is %s.\n"),
26183 value);
26184 }
26185
26186 static void
26187 show_check_physname (struct ui_file *file, int from_tty,
26188 struct cmd_list_element *c, const char *value)
26189 {
26190 fprintf_filtered (file,
26191 _("Whether to check \"physname\" is %s.\n"),
26192 value);
26193 }
26194
26195 void
26196 _initialize_dwarf2_read (void)
26197 {
26198 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
26199 Set DWARF specific variables.\n\
26200 Configure DWARF variables such as the cache size."),
26201 &set_dwarf_cmdlist, "maintenance set dwarf ",
26202 0/*allow-unknown*/, &maintenance_set_cmdlist);
26203
26204 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
26205 Show DWARF specific variables.\n\
26206 Show DWARF variables such as the cache size."),
26207 &show_dwarf_cmdlist, "maintenance show dwarf ",
26208 0/*allow-unknown*/, &maintenance_show_cmdlist);
26209
26210 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
26211 &dwarf_max_cache_age, _("\
26212 Set the upper bound on the age of cached DWARF compilation units."), _("\
26213 Show the upper bound on the age of cached DWARF compilation units."), _("\
26214 A higher limit means that cached compilation units will be stored\n\
26215 in memory longer, and more total memory will be used. Zero disables\n\
26216 caching, which can slow down startup."),
26217 NULL,
26218 show_dwarf_max_cache_age,
26219 &set_dwarf_cmdlist,
26220 &show_dwarf_cmdlist);
26221
26222 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
26223 &dwarf_always_disassemble, _("\
26224 Set whether `info address' always disassembles DWARF expressions."), _("\
26225 Show whether `info address' always disassembles DWARF expressions."), _("\
26226 When enabled, DWARF expressions are always printed in an assembly-like\n\
26227 syntax. When disabled, expressions will be printed in a more\n\
26228 conversational style, when possible."),
26229 NULL,
26230 show_dwarf_always_disassemble,
26231 &set_dwarf_cmdlist,
26232 &show_dwarf_cmdlist);
26233
26234 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
26235 Set debugging of the DWARF reader."), _("\
26236 Show debugging of the DWARF reader."), _("\
26237 When enabled (non-zero), debugging messages are printed during DWARF\n\
26238 reading and symtab expansion. A value of 1 (one) provides basic\n\
26239 information. A value greater than 1 provides more verbose information."),
26240 NULL,
26241 NULL,
26242 &setdebuglist, &showdebuglist);
26243
26244 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
26245 Set debugging of the DWARF DIE reader."), _("\
26246 Show debugging of the DWARF DIE reader."), _("\
26247 When enabled (non-zero), DIEs are dumped after they are read in.\n\
26248 The value is the maximum depth to print."),
26249 NULL,
26250 NULL,
26251 &setdebuglist, &showdebuglist);
26252
26253 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
26254 Set debugging of the dwarf line reader."), _("\
26255 Show debugging of the dwarf line reader."), _("\
26256 When enabled (non-zero), line number entries are dumped as they are read in.\n\
26257 A value of 1 (one) provides basic information.\n\
26258 A value greater than 1 provides more verbose information."),
26259 NULL,
26260 NULL,
26261 &setdebuglist, &showdebuglist);
26262
26263 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
26264 Set cross-checking of \"physname\" code against demangler."), _("\
26265 Show cross-checking of \"physname\" code against demangler."), _("\
26266 When enabled, GDB's internal \"physname\" code is checked against\n\
26267 the demangler."),
26268 NULL, show_check_physname,
26269 &setdebuglist, &showdebuglist);
26270
26271 add_setshow_boolean_cmd ("use-deprecated-index-sections",
26272 no_class, &use_deprecated_index_sections, _("\
26273 Set whether to use deprecated gdb_index sections."), _("\
26274 Show whether to use deprecated gdb_index sections."), _("\
26275 When enabled, deprecated .gdb_index sections are used anyway.\n\
26276 Normally they are ignored either because of a missing feature or\n\
26277 performance issue.\n\
26278 Warning: This option must be enabled before gdb reads the file."),
26279 NULL,
26280 NULL,
26281 &setlist, &showlist);
26282
26283 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
26284 &dwarf2_locexpr_funcs);
26285 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
26286 &dwarf2_loclist_funcs);
26287
26288 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
26289 &dwarf2_block_frame_base_locexpr_funcs);
26290 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
26291 &dwarf2_block_frame_base_loclist_funcs);
26292
26293 #if GDB_SELF_TEST
26294 selftests::register_test ("dw2_expand_symtabs_matching",
26295 selftests::dw2_expand_symtabs_matching::run_test);
26296 #endif
26297 }
This page took 0.553393 seconds and 5 git commands to generate.