Fix a memory leak and remove an unused member
[deliverable/binutils-gdb.git] / gdb / dwarf2 / read.c
1 /* DWARF 2 debugging format support for GDB.
2
3 Copyright (C) 1994-2020 Free Software Foundation, Inc.
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
10 support.
11
12 This file is part of GDB.
13
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26
27 /* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
31 #include "defs.h"
32 #include "dwarf2/read.h"
33 #include "dwarf2/abbrev.h"
34 #include "dwarf2/attribute.h"
35 #include "dwarf2/comp-unit.h"
36 #include "dwarf2/index-cache.h"
37 #include "dwarf2/index-common.h"
38 #include "dwarf2/leb.h"
39 #include "dwarf2/line-header.h"
40 #include "bfd.h"
41 #include "elf-bfd.h"
42 #include "symtab.h"
43 #include "gdbtypes.h"
44 #include "objfiles.h"
45 #include "dwarf2.h"
46 #include "buildsym.h"
47 #include "demangle.h"
48 #include "gdb-demangle.h"
49 #include "filenames.h" /* for DOSish file names */
50 #include "macrotab.h"
51 #include "language.h"
52 #include "complaints.h"
53 #include "dwarf2/expr.h"
54 #include "dwarf2/loc.h"
55 #include "cp-support.h"
56 #include "hashtab.h"
57 #include "command.h"
58 #include "gdbcmd.h"
59 #include "block.h"
60 #include "addrmap.h"
61 #include "typeprint.h"
62 #include "psympriv.h"
63 #include "c-lang.h"
64 #include "go-lang.h"
65 #include "valprint.h"
66 #include "gdbcore.h" /* for gnutarget */
67 #include "gdb/gdb-index.h"
68 #include "gdb_bfd.h"
69 #include "f-lang.h"
70 #include "source.h"
71 #include "build-id.h"
72 #include "namespace.h"
73 #include "gdbsupport/function-view.h"
74 #include "gdbsupport/gdb_optional.h"
75 #include "gdbsupport/underlying.h"
76 #include "gdbsupport/hash_enum.h"
77 #include "filename-seen-cache.h"
78 #include "producer.h"
79 #include <fcntl.h>
80 #include <algorithm>
81 #include <unordered_map>
82 #include "gdbsupport/selftest.h"
83 #include "rust-lang.h"
84 #include "gdbsupport/pathstuff.h"
85 #include "count-one-bits.h"
86
87 /* When == 1, print basic high level tracing messages.
88 When > 1, be more verbose.
89 This is in contrast to the low level DIE reading of dwarf_die_debug. */
90 static unsigned int dwarf_read_debug = 0;
91
92 /* When non-zero, dump DIEs after they are read in. */
93 static unsigned int dwarf_die_debug = 0;
94
95 /* When non-zero, dump line number entries as they are read in. */
96 unsigned int dwarf_line_debug = 0;
97
98 /* When true, cross-check physname against demangler. */
99 static bool check_physname = false;
100
101 /* When true, do not reject deprecated .gdb_index sections. */
102 static bool use_deprecated_index_sections = false;
103
104 static const struct objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
105
106 /* The "aclass" indices for various kinds of computed DWARF symbols. */
107
108 static int dwarf2_locexpr_index;
109 static int dwarf2_loclist_index;
110 static int dwarf2_locexpr_block_index;
111 static int dwarf2_loclist_block_index;
112
113 /* An index into a (C++) symbol name component in a symbol name as
114 recorded in the mapped_index's symbol table. For each C++ symbol
115 in the symbol table, we record one entry for the start of each
116 component in the symbol in a table of name components, and then
117 sort the table, in order to be able to binary search symbol names,
118 ignoring leading namespaces, both completion and regular look up.
119 For example, for symbol "A::B::C", we'll have an entry that points
120 to "A::B::C", another that points to "B::C", and another for "C".
121 Note that function symbols in GDB index have no parameter
122 information, just the function/method names. You can convert a
123 name_component to a "const char *" using the
124 'mapped_index::symbol_name_at(offset_type)' method. */
125
126 struct name_component
127 {
128 /* Offset in the symbol name where the component starts. Stored as
129 a (32-bit) offset instead of a pointer to save memory and improve
130 locality on 64-bit architectures. */
131 offset_type name_offset;
132
133 /* The symbol's index in the symbol and constant pool tables of a
134 mapped_index. */
135 offset_type idx;
136 };
137
138 /* Base class containing bits shared by both .gdb_index and
139 .debug_name indexes. */
140
141 struct mapped_index_base
142 {
143 mapped_index_base () = default;
144 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
145
146 /* The name_component table (a sorted vector). See name_component's
147 description above. */
148 std::vector<name_component> name_components;
149
150 /* How NAME_COMPONENTS is sorted. */
151 enum case_sensitivity name_components_casing;
152
153 /* Return the number of names in the symbol table. */
154 virtual size_t symbol_name_count () const = 0;
155
156 /* Get the name of the symbol at IDX in the symbol table. */
157 virtual const char *symbol_name_at (offset_type idx) const = 0;
158
159 /* Return whether the name at IDX in the symbol table should be
160 ignored. */
161 virtual bool symbol_name_slot_invalid (offset_type idx) const
162 {
163 return false;
164 }
165
166 /* Build the symbol name component sorted vector, if we haven't
167 yet. */
168 void build_name_components ();
169
170 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
171 possible matches for LN_NO_PARAMS in the name component
172 vector. */
173 std::pair<std::vector<name_component>::const_iterator,
174 std::vector<name_component>::const_iterator>
175 find_name_components_bounds (const lookup_name_info &ln_no_params,
176 enum language lang) const;
177
178 /* Prevent deleting/destroying via a base class pointer. */
179 protected:
180 ~mapped_index_base() = default;
181 };
182
183 /* A description of the mapped index. The file format is described in
184 a comment by the code that writes the index. */
185 struct mapped_index final : public mapped_index_base
186 {
187 /* A slot/bucket in the symbol table hash. */
188 struct symbol_table_slot
189 {
190 const offset_type name;
191 const offset_type vec;
192 };
193
194 /* Index data format version. */
195 int version = 0;
196
197 /* The address table data. */
198 gdb::array_view<const gdb_byte> address_table;
199
200 /* The symbol table, implemented as a hash table. */
201 gdb::array_view<symbol_table_slot> symbol_table;
202
203 /* A pointer to the constant pool. */
204 const char *constant_pool = nullptr;
205
206 bool symbol_name_slot_invalid (offset_type idx) const override
207 {
208 const auto &bucket = this->symbol_table[idx];
209 return bucket.name == 0 && bucket.vec == 0;
210 }
211
212 /* Convenience method to get at the name of the symbol at IDX in the
213 symbol table. */
214 const char *symbol_name_at (offset_type idx) const override
215 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
216
217 size_t symbol_name_count () const override
218 { return this->symbol_table.size (); }
219 };
220
221 /* A description of the mapped .debug_names.
222 Uninitialized map has CU_COUNT 0. */
223 struct mapped_debug_names final : public mapped_index_base
224 {
225 mapped_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile_)
226 : dwarf2_per_objfile (dwarf2_per_objfile_)
227 {}
228
229 struct dwarf2_per_objfile *dwarf2_per_objfile;
230 bfd_endian dwarf5_byte_order;
231 bool dwarf5_is_dwarf64;
232 bool augmentation_is_gdb;
233 uint8_t offset_size;
234 uint32_t cu_count = 0;
235 uint32_t tu_count, bucket_count, name_count;
236 const gdb_byte *cu_table_reordered, *tu_table_reordered;
237 const uint32_t *bucket_table_reordered, *hash_table_reordered;
238 const gdb_byte *name_table_string_offs_reordered;
239 const gdb_byte *name_table_entry_offs_reordered;
240 const gdb_byte *entry_pool;
241
242 struct index_val
243 {
244 ULONGEST dwarf_tag;
245 struct attr
246 {
247 /* Attribute name DW_IDX_*. */
248 ULONGEST dw_idx;
249
250 /* Attribute form DW_FORM_*. */
251 ULONGEST form;
252
253 /* Value if FORM is DW_FORM_implicit_const. */
254 LONGEST implicit_const;
255 };
256 std::vector<attr> attr_vec;
257 };
258
259 std::unordered_map<ULONGEST, index_val> abbrev_map;
260
261 const char *namei_to_name (uint32_t namei) const;
262
263 /* Implementation of the mapped_index_base virtual interface, for
264 the name_components cache. */
265
266 const char *symbol_name_at (offset_type idx) const override
267 { return namei_to_name (idx); }
268
269 size_t symbol_name_count () const override
270 { return this->name_count; }
271 };
272
273 /* See dwarf2read.h. */
274
275 dwarf2_per_objfile *
276 get_dwarf2_per_objfile (struct objfile *objfile)
277 {
278 return dwarf2_objfile_data_key.get (objfile);
279 }
280
281 /* Default names of the debugging sections. */
282
283 /* Note that if the debugging section has been compressed, it might
284 have a name like .zdebug_info. */
285
286 static const struct dwarf2_debug_sections dwarf2_elf_names =
287 {
288 { ".debug_info", ".zdebug_info" },
289 { ".debug_abbrev", ".zdebug_abbrev" },
290 { ".debug_line", ".zdebug_line" },
291 { ".debug_loc", ".zdebug_loc" },
292 { ".debug_loclists", ".zdebug_loclists" },
293 { ".debug_macinfo", ".zdebug_macinfo" },
294 { ".debug_macro", ".zdebug_macro" },
295 { ".debug_str", ".zdebug_str" },
296 { ".debug_str_offsets", ".zdebug_str_offsets" },
297 { ".debug_line_str", ".zdebug_line_str" },
298 { ".debug_ranges", ".zdebug_ranges" },
299 { ".debug_rnglists", ".zdebug_rnglists" },
300 { ".debug_types", ".zdebug_types" },
301 { ".debug_addr", ".zdebug_addr" },
302 { ".debug_frame", ".zdebug_frame" },
303 { ".eh_frame", NULL },
304 { ".gdb_index", ".zgdb_index" },
305 { ".debug_names", ".zdebug_names" },
306 { ".debug_aranges", ".zdebug_aranges" },
307 23
308 };
309
310 /* List of DWO/DWP sections. */
311
312 static const struct dwop_section_names
313 {
314 struct dwarf2_section_names abbrev_dwo;
315 struct dwarf2_section_names info_dwo;
316 struct dwarf2_section_names line_dwo;
317 struct dwarf2_section_names loc_dwo;
318 struct dwarf2_section_names loclists_dwo;
319 struct dwarf2_section_names macinfo_dwo;
320 struct dwarf2_section_names macro_dwo;
321 struct dwarf2_section_names str_dwo;
322 struct dwarf2_section_names str_offsets_dwo;
323 struct dwarf2_section_names types_dwo;
324 struct dwarf2_section_names cu_index;
325 struct dwarf2_section_names tu_index;
326 }
327 dwop_section_names =
328 {
329 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
330 { ".debug_info.dwo", ".zdebug_info.dwo" },
331 { ".debug_line.dwo", ".zdebug_line.dwo" },
332 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
333 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
334 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
335 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
336 { ".debug_str.dwo", ".zdebug_str.dwo" },
337 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
338 { ".debug_types.dwo", ".zdebug_types.dwo" },
339 { ".debug_cu_index", ".zdebug_cu_index" },
340 { ".debug_tu_index", ".zdebug_tu_index" },
341 };
342
343 /* local data types */
344
345 /* Type used for delaying computation of method physnames.
346 See comments for compute_delayed_physnames. */
347 struct delayed_method_info
348 {
349 /* The type to which the method is attached, i.e., its parent class. */
350 struct type *type;
351
352 /* The index of the method in the type's function fieldlists. */
353 int fnfield_index;
354
355 /* The index of the method in the fieldlist. */
356 int index;
357
358 /* The name of the DIE. */
359 const char *name;
360
361 /* The DIE associated with this method. */
362 struct die_info *die;
363 };
364
365 /* Internal state when decoding a particular compilation unit. */
366 struct dwarf2_cu
367 {
368 explicit dwarf2_cu (struct dwarf2_per_cu_data *per_cu);
369 ~dwarf2_cu ();
370
371 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
372
373 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
374 Create the set of symtabs used by this TU, or if this TU is sharing
375 symtabs with another TU and the symtabs have already been created
376 then restore those symtabs in the line header.
377 We don't need the pc/line-number mapping for type units. */
378 void setup_type_unit_groups (struct die_info *die);
379
380 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
381 buildsym_compunit constructor. */
382 struct compunit_symtab *start_symtab (const char *name,
383 const char *comp_dir,
384 CORE_ADDR low_pc);
385
386 /* Reset the builder. */
387 void reset_builder () { m_builder.reset (); }
388
389 /* The header of the compilation unit. */
390 struct comp_unit_head header {};
391
392 /* Base address of this compilation unit. */
393 CORE_ADDR base_address = 0;
394
395 /* Non-zero if base_address has been set. */
396 int base_known = 0;
397
398 /* The language we are debugging. */
399 enum language language = language_unknown;
400 const struct language_defn *language_defn = nullptr;
401
402 const char *producer = nullptr;
403
404 private:
405 /* The symtab builder for this CU. This is only non-NULL when full
406 symbols are being read. */
407 std::unique_ptr<buildsym_compunit> m_builder;
408
409 public:
410 /* The generic symbol table building routines have separate lists for
411 file scope symbols and all all other scopes (local scopes). So
412 we need to select the right one to pass to add_symbol_to_list().
413 We do it by keeping a pointer to the correct list in list_in_scope.
414
415 FIXME: The original dwarf code just treated the file scope as the
416 first local scope, and all other local scopes as nested local
417 scopes, and worked fine. Check to see if we really need to
418 distinguish these in buildsym.c. */
419 struct pending **list_in_scope = nullptr;
420
421 /* Hash table holding all the loaded partial DIEs
422 with partial_die->offset.SECT_OFF as hash. */
423 htab_t partial_dies = nullptr;
424
425 /* Storage for things with the same lifetime as this read-in compilation
426 unit, including partial DIEs. */
427 auto_obstack comp_unit_obstack;
428
429 /* When multiple dwarf2_cu structures are living in memory, this field
430 chains them all together, so that they can be released efficiently.
431 We will probably also want a generation counter so that most-recently-used
432 compilation units are cached... */
433 struct dwarf2_per_cu_data *read_in_chain = nullptr;
434
435 /* Backlink to our per_cu entry. */
436 struct dwarf2_per_cu_data *per_cu;
437
438 /* How many compilation units ago was this CU last referenced? */
439 int last_used = 0;
440
441 /* A hash table of DIE cu_offset for following references with
442 die_info->offset.sect_off as hash. */
443 htab_t die_hash = nullptr;
444
445 /* Full DIEs if read in. */
446 struct die_info *dies = nullptr;
447
448 /* A set of pointers to dwarf2_per_cu_data objects for compilation
449 units referenced by this one. Only set during full symbol processing;
450 partial symbol tables do not have dependencies. */
451 htab_t dependencies = nullptr;
452
453 /* Header data from the line table, during full symbol processing. */
454 struct line_header *line_header = nullptr;
455 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
456 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
457 this is the DW_TAG_compile_unit die for this CU. We'll hold on
458 to the line header as long as this DIE is being processed. See
459 process_die_scope. */
460 die_info *line_header_die_owner = nullptr;
461
462 /* A list of methods which need to have physnames computed
463 after all type information has been read. */
464 std::vector<delayed_method_info> method_list;
465
466 /* To be copied to symtab->call_site_htab. */
467 htab_t call_site_htab = nullptr;
468
469 /* Non-NULL if this CU came from a DWO file.
470 There is an invariant here that is important to remember:
471 Except for attributes copied from the top level DIE in the "main"
472 (or "stub") file in preparation for reading the DWO file
473 (e.g., DW_AT_addr_base), we KISS: there is only *one* CU.
474 Either there isn't a DWO file (in which case this is NULL and the point
475 is moot), or there is and either we're not going to read it (in which
476 case this is NULL) or there is and we are reading it (in which case this
477 is non-NULL). */
478 struct dwo_unit *dwo_unit = nullptr;
479
480 /* The DW_AT_addr_base (DW_AT_GNU_addr_base) attribute if present.
481 Note this value comes from the Fission stub CU/TU's DIE. */
482 gdb::optional<ULONGEST> addr_base;
483
484 /* The DW_AT_rnglists_base attribute if present.
485 Note this value comes from the Fission stub CU/TU's DIE.
486 Also note that the value is zero in the non-DWO case so this value can
487 be used without needing to know whether DWO files are in use or not.
488 N.B. This does not apply to DW_AT_ranges appearing in
489 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
490 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
491 DW_AT_rnglists_base *would* have to be applied, and we'd have to care
492 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
493 ULONGEST ranges_base = 0;
494
495 /* When reading debug info generated by older versions of rustc, we
496 have to rewrite some union types to be struct types with a
497 variant part. This rewriting must be done after the CU is fully
498 read in, because otherwise at the point of rewriting some struct
499 type might not have been fully processed. So, we keep a list of
500 all such types here and process them after expansion. */
501 std::vector<struct type *> rust_unions;
502
503 /* The DW_AT_str_offsets_base attribute if present. For DWARF 4 version DWO
504 files, the value is implicitly zero. For DWARF 5 version DWO files, the
505 value is often implicit and is the size of the header of
506 .debug_str_offsets section (8 or 4, depending on the address size). */
507 gdb::optional<ULONGEST> str_offsets_base;
508
509 /* Mark used when releasing cached dies. */
510 bool mark : 1;
511
512 /* This CU references .debug_loc. See the symtab->locations_valid field.
513 This test is imperfect as there may exist optimized debug code not using
514 any location list and still facing inlining issues if handled as
515 unoptimized code. For a future better test see GCC PR other/32998. */
516 bool has_loclist : 1;
517
518 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
519 if all the producer_is_* fields are valid. This information is cached
520 because profiling CU expansion showed excessive time spent in
521 producer_is_gxx_lt_4_6. */
522 bool checked_producer : 1;
523 bool producer_is_gxx_lt_4_6 : 1;
524 bool producer_is_gcc_lt_4_3 : 1;
525 bool producer_is_icc : 1;
526 bool producer_is_icc_lt_14 : 1;
527 bool producer_is_codewarrior : 1;
528
529 /* When true, the file that we're processing is known to have
530 debugging info for C++ namespaces. GCC 3.3.x did not produce
531 this information, but later versions do. */
532
533 bool processing_has_namespace_info : 1;
534
535 struct partial_die_info *find_partial_die (sect_offset sect_off);
536
537 /* If this CU was inherited by another CU (via specification,
538 abstract_origin, etc), this is the ancestor CU. */
539 dwarf2_cu *ancestor;
540
541 /* Get the buildsym_compunit for this CU. */
542 buildsym_compunit *get_builder ()
543 {
544 /* If this CU has a builder associated with it, use that. */
545 if (m_builder != nullptr)
546 return m_builder.get ();
547
548 /* Otherwise, search ancestors for a valid builder. */
549 if (ancestor != nullptr)
550 return ancestor->get_builder ();
551
552 return nullptr;
553 }
554 };
555
556 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
557 This includes type_unit_group and quick_file_names. */
558
559 struct stmt_list_hash
560 {
561 /* The DWO unit this table is from or NULL if there is none. */
562 struct dwo_unit *dwo_unit;
563
564 /* Offset in .debug_line or .debug_line.dwo. */
565 sect_offset line_sect_off;
566 };
567
568 /* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
569 an object of this type. */
570
571 struct type_unit_group
572 {
573 /* dwarf2read.c's main "handle" on a TU symtab.
574 To simplify things we create an artificial CU that "includes" all the
575 type units using this stmt_list so that the rest of the code still has
576 a "per_cu" handle on the symtab. */
577 struct dwarf2_per_cu_data per_cu;
578
579 /* The TUs that share this DW_AT_stmt_list entry.
580 This is added to while parsing type units to build partial symtabs,
581 and is deleted afterwards and not used again. */
582 std::vector<signatured_type *> *tus;
583
584 /* The compunit symtab.
585 Type units in a group needn't all be defined in the same source file,
586 so we create an essentially anonymous symtab as the compunit symtab. */
587 struct compunit_symtab *compunit_symtab;
588
589 /* The data used to construct the hash key. */
590 struct stmt_list_hash hash;
591
592 /* The symbol tables for this TU (obtained from the files listed in
593 DW_AT_stmt_list).
594 WARNING: The order of entries here must match the order of entries
595 in the line header. After the first TU using this type_unit_group, the
596 line header for the subsequent TUs is recreated from this. This is done
597 because we need to use the same symtabs for each TU using the same
598 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
599 there's no guarantee the line header doesn't have duplicate entries. */
600 struct symtab **symtabs;
601 };
602
603 /* These sections are what may appear in a (real or virtual) DWO file. */
604
605 struct dwo_sections
606 {
607 struct dwarf2_section_info abbrev;
608 struct dwarf2_section_info line;
609 struct dwarf2_section_info loc;
610 struct dwarf2_section_info loclists;
611 struct dwarf2_section_info macinfo;
612 struct dwarf2_section_info macro;
613 struct dwarf2_section_info str;
614 struct dwarf2_section_info str_offsets;
615 /* In the case of a virtual DWO file, these two are unused. */
616 struct dwarf2_section_info info;
617 std::vector<dwarf2_section_info> types;
618 };
619
620 /* CUs/TUs in DWP/DWO files. */
621
622 struct dwo_unit
623 {
624 /* Backlink to the containing struct dwo_file. */
625 struct dwo_file *dwo_file;
626
627 /* The "id" that distinguishes this CU/TU.
628 .debug_info calls this "dwo_id", .debug_types calls this "signature".
629 Since signatures came first, we stick with it for consistency. */
630 ULONGEST signature;
631
632 /* The section this CU/TU lives in, in the DWO file. */
633 struct dwarf2_section_info *section;
634
635 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
636 sect_offset sect_off;
637 unsigned int length;
638
639 /* For types, offset in the type's DIE of the type defined by this TU. */
640 cu_offset type_offset_in_tu;
641 };
642
643 /* include/dwarf2.h defines the DWP section codes.
644 It defines a max value but it doesn't define a min value, which we
645 use for error checking, so provide one. */
646
647 enum dwp_v2_section_ids
648 {
649 DW_SECT_MIN = 1
650 };
651
652 /* Data for one DWO file.
653
654 This includes virtual DWO files (a virtual DWO file is a DWO file as it
655 appears in a DWP file). DWP files don't really have DWO files per se -
656 comdat folding of types "loses" the DWO file they came from, and from
657 a high level view DWP files appear to contain a mass of random types.
658 However, to maintain consistency with the non-DWP case we pretend DWP
659 files contain virtual DWO files, and we assign each TU with one virtual
660 DWO file (generally based on the line and abbrev section offsets -
661 a heuristic that seems to work in practice). */
662
663 struct dwo_file
664 {
665 dwo_file () = default;
666 DISABLE_COPY_AND_ASSIGN (dwo_file);
667
668 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
669 For virtual DWO files the name is constructed from the section offsets
670 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
671 from related CU+TUs. */
672 const char *dwo_name = nullptr;
673
674 /* The DW_AT_comp_dir attribute. */
675 const char *comp_dir = nullptr;
676
677 /* The bfd, when the file is open. Otherwise this is NULL.
678 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
679 gdb_bfd_ref_ptr dbfd;
680
681 /* The sections that make up this DWO file.
682 Remember that for virtual DWO files in DWP V2, these are virtual
683 sections (for lack of a better name). */
684 struct dwo_sections sections {};
685
686 /* The CUs in the file.
687 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
688 an extension to handle LLVM's Link Time Optimization output (where
689 multiple source files may be compiled into a single object/dwo pair). */
690 htab_up cus;
691
692 /* Table of TUs in the file.
693 Each element is a struct dwo_unit. */
694 htab_up tus;
695 };
696
697 /* These sections are what may appear in a DWP file. */
698
699 struct dwp_sections
700 {
701 /* These are used by both DWP version 1 and 2. */
702 struct dwarf2_section_info str;
703 struct dwarf2_section_info cu_index;
704 struct dwarf2_section_info tu_index;
705
706 /* These are only used by DWP version 2 files.
707 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
708 sections are referenced by section number, and are not recorded here.
709 In DWP version 2 there is at most one copy of all these sections, each
710 section being (effectively) comprised of the concatenation of all of the
711 individual sections that exist in the version 1 format.
712 To keep the code simple we treat each of these concatenated pieces as a
713 section itself (a virtual section?). */
714 struct dwarf2_section_info abbrev;
715 struct dwarf2_section_info info;
716 struct dwarf2_section_info line;
717 struct dwarf2_section_info loc;
718 struct dwarf2_section_info macinfo;
719 struct dwarf2_section_info macro;
720 struct dwarf2_section_info str_offsets;
721 struct dwarf2_section_info types;
722 };
723
724 /* These sections are what may appear in a virtual DWO file in DWP version 1.
725 A virtual DWO file is a DWO file as it appears in a DWP file. */
726
727 struct virtual_v1_dwo_sections
728 {
729 struct dwarf2_section_info abbrev;
730 struct dwarf2_section_info line;
731 struct dwarf2_section_info loc;
732 struct dwarf2_section_info macinfo;
733 struct dwarf2_section_info macro;
734 struct dwarf2_section_info str_offsets;
735 /* Each DWP hash table entry records one CU or one TU.
736 That is recorded here, and copied to dwo_unit.section. */
737 struct dwarf2_section_info info_or_types;
738 };
739
740 /* Similar to virtual_v1_dwo_sections, but for DWP version 2.
741 In version 2, the sections of the DWO files are concatenated together
742 and stored in one section of that name. Thus each ELF section contains
743 several "virtual" sections. */
744
745 struct virtual_v2_dwo_sections
746 {
747 bfd_size_type abbrev_offset;
748 bfd_size_type abbrev_size;
749
750 bfd_size_type line_offset;
751 bfd_size_type line_size;
752
753 bfd_size_type loc_offset;
754 bfd_size_type loc_size;
755
756 bfd_size_type macinfo_offset;
757 bfd_size_type macinfo_size;
758
759 bfd_size_type macro_offset;
760 bfd_size_type macro_size;
761
762 bfd_size_type str_offsets_offset;
763 bfd_size_type str_offsets_size;
764
765 /* Each DWP hash table entry records one CU or one TU.
766 That is recorded here, and copied to dwo_unit.section. */
767 bfd_size_type info_or_types_offset;
768 bfd_size_type info_or_types_size;
769 };
770
771 /* Contents of DWP hash tables. */
772
773 struct dwp_hash_table
774 {
775 uint32_t version, nr_columns;
776 uint32_t nr_units, nr_slots;
777 const gdb_byte *hash_table, *unit_table;
778 union
779 {
780 struct
781 {
782 const gdb_byte *indices;
783 } v1;
784 struct
785 {
786 /* This is indexed by column number and gives the id of the section
787 in that column. */
788 #define MAX_NR_V2_DWO_SECTIONS \
789 (1 /* .debug_info or .debug_types */ \
790 + 1 /* .debug_abbrev */ \
791 + 1 /* .debug_line */ \
792 + 1 /* .debug_loc */ \
793 + 1 /* .debug_str_offsets */ \
794 + 1 /* .debug_macro or .debug_macinfo */)
795 int section_ids[MAX_NR_V2_DWO_SECTIONS];
796 const gdb_byte *offsets;
797 const gdb_byte *sizes;
798 } v2;
799 } section_pool;
800 };
801
802 /* Data for one DWP file. */
803
804 struct dwp_file
805 {
806 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
807 : name (name_),
808 dbfd (std::move (abfd))
809 {
810 }
811
812 /* Name of the file. */
813 const char *name;
814
815 /* File format version. */
816 int version = 0;
817
818 /* The bfd. */
819 gdb_bfd_ref_ptr dbfd;
820
821 /* Section info for this file. */
822 struct dwp_sections sections {};
823
824 /* Table of CUs in the file. */
825 const struct dwp_hash_table *cus = nullptr;
826
827 /* Table of TUs in the file. */
828 const struct dwp_hash_table *tus = nullptr;
829
830 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
831 htab_up loaded_cus;
832 htab_up loaded_tus;
833
834 /* Table to map ELF section numbers to their sections.
835 This is only needed for the DWP V1 file format. */
836 unsigned int num_sections = 0;
837 asection **elf_sections = nullptr;
838 };
839
840 /* Struct used to pass misc. parameters to read_die_and_children, et
841 al. which are used for both .debug_info and .debug_types dies.
842 All parameters here are unchanging for the life of the call. This
843 struct exists to abstract away the constant parameters of die reading. */
844
845 struct die_reader_specs
846 {
847 /* The bfd of die_section. */
848 bfd* abfd;
849
850 /* The CU of the DIE we are parsing. */
851 struct dwarf2_cu *cu;
852
853 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
854 struct dwo_file *dwo_file;
855
856 /* The section the die comes from.
857 This is either .debug_info or .debug_types, or the .dwo variants. */
858 struct dwarf2_section_info *die_section;
859
860 /* die_section->buffer. */
861 const gdb_byte *buffer;
862
863 /* The end of the buffer. */
864 const gdb_byte *buffer_end;
865
866 /* The abbreviation table to use when reading the DIEs. */
867 struct abbrev_table *abbrev_table;
868 };
869
870 /* A subclass of die_reader_specs that holds storage and has complex
871 constructor and destructor behavior. */
872
873 class cutu_reader : public die_reader_specs
874 {
875 public:
876
877 cutu_reader (struct dwarf2_per_cu_data *this_cu,
878 struct abbrev_table *abbrev_table,
879 int use_existing_cu,
880 bool skip_partial);
881
882 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
883 struct dwarf2_cu *parent_cu = nullptr,
884 struct dwo_file *dwo_file = nullptr);
885
886 DISABLE_COPY_AND_ASSIGN (cutu_reader);
887
888 const gdb_byte *info_ptr = nullptr;
889 struct die_info *comp_unit_die = nullptr;
890 bool dummy_p = false;
891
892 /* Release the new CU, putting it on the chain. This cannot be done
893 for dummy CUs. */
894 void keep ();
895
896 private:
897 void init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
898 int use_existing_cu);
899
900 struct dwarf2_per_cu_data *m_this_cu;
901 std::unique_ptr<dwarf2_cu> m_new_cu;
902
903 /* The ordinary abbreviation table. */
904 abbrev_table_up m_abbrev_table_holder;
905
906 /* The DWO abbreviation table. */
907 abbrev_table_up m_dwo_abbrev_table;
908 };
909
910 /* When we construct a partial symbol table entry we only
911 need this much information. */
912 struct partial_die_info : public allocate_on_obstack
913 {
914 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
915
916 /* Disable assign but still keep copy ctor, which is needed
917 load_partial_dies. */
918 partial_die_info& operator=(const partial_die_info& rhs) = delete;
919
920 /* Adjust the partial die before generating a symbol for it. This
921 function may set the is_external flag or change the DIE's
922 name. */
923 void fixup (struct dwarf2_cu *cu);
924
925 /* Read a minimal amount of information into the minimal die
926 structure. */
927 const gdb_byte *read (const struct die_reader_specs *reader,
928 const struct abbrev_info &abbrev,
929 const gdb_byte *info_ptr);
930
931 /* Offset of this DIE. */
932 const sect_offset sect_off;
933
934 /* DWARF-2 tag for this DIE. */
935 const ENUM_BITFIELD(dwarf_tag) tag : 16;
936
937 /* Assorted flags describing the data found in this DIE. */
938 const unsigned int has_children : 1;
939
940 unsigned int is_external : 1;
941 unsigned int is_declaration : 1;
942 unsigned int has_type : 1;
943 unsigned int has_specification : 1;
944 unsigned int has_pc_info : 1;
945 unsigned int may_be_inlined : 1;
946
947 /* This DIE has been marked DW_AT_main_subprogram. */
948 unsigned int main_subprogram : 1;
949
950 /* Flag set if the SCOPE field of this structure has been
951 computed. */
952 unsigned int scope_set : 1;
953
954 /* Flag set if the DIE has a byte_size attribute. */
955 unsigned int has_byte_size : 1;
956
957 /* Flag set if the DIE has a DW_AT_const_value attribute. */
958 unsigned int has_const_value : 1;
959
960 /* Flag set if any of the DIE's children are template arguments. */
961 unsigned int has_template_arguments : 1;
962
963 /* Flag set if fixup has been called on this die. */
964 unsigned int fixup_called : 1;
965
966 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
967 unsigned int is_dwz : 1;
968
969 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
970 unsigned int spec_is_dwz : 1;
971
972 /* The name of this DIE. Normally the value of DW_AT_name, but
973 sometimes a default name for unnamed DIEs. */
974 const char *name = nullptr;
975
976 /* The linkage name, if present. */
977 const char *linkage_name = nullptr;
978
979 /* The scope to prepend to our children. This is generally
980 allocated on the comp_unit_obstack, so will disappear
981 when this compilation unit leaves the cache. */
982 const char *scope = nullptr;
983
984 /* Some data associated with the partial DIE. The tag determines
985 which field is live. */
986 union
987 {
988 /* The location description associated with this DIE, if any. */
989 struct dwarf_block *locdesc;
990 /* The offset of an import, for DW_TAG_imported_unit. */
991 sect_offset sect_off;
992 } d {};
993
994 /* If HAS_PC_INFO, the PC range associated with this DIE. */
995 CORE_ADDR lowpc = 0;
996 CORE_ADDR highpc = 0;
997
998 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
999 DW_AT_sibling, if any. */
1000 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1001 could return DW_AT_sibling values to its caller load_partial_dies. */
1002 const gdb_byte *sibling = nullptr;
1003
1004 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1005 DW_AT_specification (or DW_AT_abstract_origin or
1006 DW_AT_extension). */
1007 sect_offset spec_offset {};
1008
1009 /* Pointers to this DIE's parent, first child, and next sibling,
1010 if any. */
1011 struct partial_die_info *die_parent = nullptr;
1012 struct partial_die_info *die_child = nullptr;
1013 struct partial_die_info *die_sibling = nullptr;
1014
1015 friend struct partial_die_info *
1016 dwarf2_cu::find_partial_die (sect_offset sect_off);
1017
1018 private:
1019 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1020 partial_die_info (sect_offset sect_off)
1021 : partial_die_info (sect_off, DW_TAG_padding, 0)
1022 {
1023 }
1024
1025 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1026 int has_children_)
1027 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1028 {
1029 is_external = 0;
1030 is_declaration = 0;
1031 has_type = 0;
1032 has_specification = 0;
1033 has_pc_info = 0;
1034 may_be_inlined = 0;
1035 main_subprogram = 0;
1036 scope_set = 0;
1037 has_byte_size = 0;
1038 has_const_value = 0;
1039 has_template_arguments = 0;
1040 fixup_called = 0;
1041 is_dwz = 0;
1042 spec_is_dwz = 0;
1043 }
1044 };
1045
1046 /* This data structure holds a complete die structure. */
1047 struct die_info
1048 {
1049 /* DWARF-2 tag for this DIE. */
1050 ENUM_BITFIELD(dwarf_tag) tag : 16;
1051
1052 /* Number of attributes */
1053 unsigned char num_attrs;
1054
1055 /* True if we're presently building the full type name for the
1056 type derived from this DIE. */
1057 unsigned char building_fullname : 1;
1058
1059 /* True if this die is in process. PR 16581. */
1060 unsigned char in_process : 1;
1061
1062 /* True if this DIE has children. */
1063 unsigned char has_children : 1;
1064
1065 /* Abbrev number */
1066 unsigned int abbrev;
1067
1068 /* Offset in .debug_info or .debug_types section. */
1069 sect_offset sect_off;
1070
1071 /* The dies in a compilation unit form an n-ary tree. PARENT
1072 points to this die's parent; CHILD points to the first child of
1073 this node; and all the children of a given node are chained
1074 together via their SIBLING fields. */
1075 struct die_info *child; /* Its first child, if any. */
1076 struct die_info *sibling; /* Its next sibling, if any. */
1077 struct die_info *parent; /* Its parent, if any. */
1078
1079 /* An array of attributes, with NUM_ATTRS elements. There may be
1080 zero, but it's not common and zero-sized arrays are not
1081 sufficiently portable C. */
1082 struct attribute attrs[1];
1083 };
1084
1085 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1086 but this would require a corresponding change in unpack_field_as_long
1087 and friends. */
1088 static int bits_per_byte = 8;
1089
1090 /* When reading a variant or variant part, we track a bit more
1091 information about the field, and store it in an object of this
1092 type. */
1093
1094 struct variant_field
1095 {
1096 /* If we see a DW_TAG_variant, then this will be the discriminant
1097 value. */
1098 ULONGEST discriminant_value;
1099 /* If we see a DW_TAG_variant, then this will be set if this is the
1100 default branch. */
1101 bool default_branch;
1102 /* While reading a DW_TAG_variant_part, this will be set if this
1103 field is the discriminant. */
1104 bool is_discriminant;
1105 };
1106
1107 struct nextfield
1108 {
1109 int accessibility = 0;
1110 int virtuality = 0;
1111 /* Extra information to describe a variant or variant part. */
1112 struct variant_field variant {};
1113 struct field field {};
1114 };
1115
1116 struct fnfieldlist
1117 {
1118 const char *name = nullptr;
1119 std::vector<struct fn_field> fnfields;
1120 };
1121
1122 /* The routines that read and process dies for a C struct or C++ class
1123 pass lists of data member fields and lists of member function fields
1124 in an instance of a field_info structure, as defined below. */
1125 struct field_info
1126 {
1127 /* List of data member and baseclasses fields. */
1128 std::vector<struct nextfield> fields;
1129 std::vector<struct nextfield> baseclasses;
1130
1131 /* Number of fields (including baseclasses). */
1132 int nfields = 0;
1133
1134 /* Set if the accessibility of one of the fields is not public. */
1135 int non_public_fields = 0;
1136
1137 /* Member function fieldlist array, contains name of possibly overloaded
1138 member function, number of overloaded member functions and a pointer
1139 to the head of the member function field chain. */
1140 std::vector<struct fnfieldlist> fnfieldlists;
1141
1142 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1143 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1144 std::vector<struct decl_field> typedef_field_list;
1145
1146 /* Nested types defined by this class and the number of elements in this
1147 list. */
1148 std::vector<struct decl_field> nested_types_list;
1149 };
1150
1151 /* Loaded secondary compilation units are kept in memory until they
1152 have not been referenced for the processing of this many
1153 compilation units. Set this to zero to disable caching. Cache
1154 sizes of up to at least twenty will improve startup time for
1155 typical inter-CU-reference binaries, at an obvious memory cost. */
1156 static int dwarf_max_cache_age = 5;
1157 static void
1158 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1159 struct cmd_list_element *c, const char *value)
1160 {
1161 fprintf_filtered (file, _("The upper bound on the age of cached "
1162 "DWARF compilation units is %s.\n"),
1163 value);
1164 }
1165 \f
1166 /* local function prototypes */
1167
1168 static void dwarf2_find_base_address (struct die_info *die,
1169 struct dwarf2_cu *cu);
1170
1171 static dwarf2_psymtab *create_partial_symtab
1172 (struct dwarf2_per_cu_data *per_cu, const char *name);
1173
1174 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1175 const gdb_byte *info_ptr,
1176 struct die_info *type_unit_die);
1177
1178 static void dwarf2_build_psymtabs_hard
1179 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1180
1181 static void scan_partial_symbols (struct partial_die_info *,
1182 CORE_ADDR *, CORE_ADDR *,
1183 int, struct dwarf2_cu *);
1184
1185 static void add_partial_symbol (struct partial_die_info *,
1186 struct dwarf2_cu *);
1187
1188 static void add_partial_namespace (struct partial_die_info *pdi,
1189 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1190 int set_addrmap, struct dwarf2_cu *cu);
1191
1192 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1193 CORE_ADDR *highpc, int set_addrmap,
1194 struct dwarf2_cu *cu);
1195
1196 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1197 struct dwarf2_cu *cu);
1198
1199 static void add_partial_subprogram (struct partial_die_info *pdi,
1200 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1201 int need_pc, struct dwarf2_cu *cu);
1202
1203 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1204
1205 static struct partial_die_info *load_partial_dies
1206 (const struct die_reader_specs *, const gdb_byte *, int);
1207
1208 /* A pair of partial_die_info and compilation unit. */
1209 struct cu_partial_die_info
1210 {
1211 /* The compilation unit of the partial_die_info. */
1212 struct dwarf2_cu *cu;
1213 /* A partial_die_info. */
1214 struct partial_die_info *pdi;
1215
1216 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1217 : cu (cu),
1218 pdi (pdi)
1219 { /* Nothing. */ }
1220
1221 private:
1222 cu_partial_die_info () = delete;
1223 };
1224
1225 static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1226 struct dwarf2_cu *);
1227
1228 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1229 struct attribute *, struct attr_abbrev *,
1230 const gdb_byte *, bool *need_reprocess);
1231
1232 static void read_attribute_reprocess (const struct die_reader_specs *reader,
1233 struct attribute *attr);
1234
1235 static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
1236
1237 static LONGEST read_checked_initial_length_and_offset
1238 (bfd *, const gdb_byte *, const struct comp_unit_head *,
1239 unsigned int *, unsigned int *);
1240
1241 static sect_offset read_abbrev_offset
1242 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1243 struct dwarf2_section_info *, sect_offset);
1244
1245 static const char *read_indirect_string
1246 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1247 const struct comp_unit_head *, unsigned int *);
1248
1249 static const char *read_indirect_line_string
1250 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1251 const struct comp_unit_head *, unsigned int *);
1252
1253 static const char *read_indirect_string_at_offset
1254 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
1255 LONGEST str_offset);
1256
1257 static const char *read_indirect_string_from_dwz
1258 (struct objfile *objfile, struct dwz_file *, LONGEST);
1259
1260 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1261 const gdb_byte *,
1262 unsigned int *);
1263
1264 static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1265 ULONGEST str_index);
1266
1267 static const char *read_stub_str_index (struct dwarf2_cu *cu,
1268 ULONGEST str_index);
1269
1270 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1271
1272 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1273 struct dwarf2_cu *);
1274
1275 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1276 unsigned int);
1277
1278 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1279 struct dwarf2_cu *cu);
1280
1281 static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1282
1283 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1284 struct dwarf2_cu *cu);
1285
1286 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1287
1288 static struct die_info *die_specification (struct die_info *die,
1289 struct dwarf2_cu **);
1290
1291 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1292 struct dwarf2_cu *cu);
1293
1294 static void dwarf_decode_lines (struct line_header *, const char *,
1295 struct dwarf2_cu *, dwarf2_psymtab *,
1296 CORE_ADDR, int decode_mapping);
1297
1298 static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1299 const char *);
1300
1301 static struct symbol *new_symbol (struct die_info *, struct type *,
1302 struct dwarf2_cu *, struct symbol * = NULL);
1303
1304 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1305 struct dwarf2_cu *);
1306
1307 static void dwarf2_const_value_attr (const struct attribute *attr,
1308 struct type *type,
1309 const char *name,
1310 struct obstack *obstack,
1311 struct dwarf2_cu *cu, LONGEST *value,
1312 const gdb_byte **bytes,
1313 struct dwarf2_locexpr_baton **baton);
1314
1315 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1316
1317 static int need_gnat_info (struct dwarf2_cu *);
1318
1319 static struct type *die_descriptive_type (struct die_info *,
1320 struct dwarf2_cu *);
1321
1322 static void set_descriptive_type (struct type *, struct die_info *,
1323 struct dwarf2_cu *);
1324
1325 static struct type *die_containing_type (struct die_info *,
1326 struct dwarf2_cu *);
1327
1328 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1329 struct dwarf2_cu *);
1330
1331 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1332
1333 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1334
1335 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1336
1337 static char *typename_concat (struct obstack *obs, const char *prefix,
1338 const char *suffix, int physname,
1339 struct dwarf2_cu *cu);
1340
1341 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1342
1343 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1344
1345 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1346
1347 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1348
1349 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1350
1351 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1352
1353 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1354 struct dwarf2_cu *, dwarf2_psymtab *);
1355
1356 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1357 values. Keep the items ordered with increasing constraints compliance. */
1358 enum pc_bounds_kind
1359 {
1360 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1361 PC_BOUNDS_NOT_PRESENT,
1362
1363 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1364 were present but they do not form a valid range of PC addresses. */
1365 PC_BOUNDS_INVALID,
1366
1367 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1368 PC_BOUNDS_RANGES,
1369
1370 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1371 PC_BOUNDS_HIGH_LOW,
1372 };
1373
1374 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1375 CORE_ADDR *, CORE_ADDR *,
1376 struct dwarf2_cu *,
1377 dwarf2_psymtab *);
1378
1379 static void get_scope_pc_bounds (struct die_info *,
1380 CORE_ADDR *, CORE_ADDR *,
1381 struct dwarf2_cu *);
1382
1383 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1384 CORE_ADDR, struct dwarf2_cu *);
1385
1386 static void dwarf2_add_field (struct field_info *, struct die_info *,
1387 struct dwarf2_cu *);
1388
1389 static void dwarf2_attach_fields_to_type (struct field_info *,
1390 struct type *, struct dwarf2_cu *);
1391
1392 static void dwarf2_add_member_fn (struct field_info *,
1393 struct die_info *, struct type *,
1394 struct dwarf2_cu *);
1395
1396 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1397 struct type *,
1398 struct dwarf2_cu *);
1399
1400 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1401
1402 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1403
1404 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1405
1406 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1407
1408 static struct using_direct **using_directives (struct dwarf2_cu *cu);
1409
1410 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1411
1412 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1413
1414 static struct type *read_module_type (struct die_info *die,
1415 struct dwarf2_cu *cu);
1416
1417 static const char *namespace_name (struct die_info *die,
1418 int *is_anonymous, struct dwarf2_cu *);
1419
1420 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1421
1422 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1423
1424 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1425 struct dwarf2_cu *);
1426
1427 static struct die_info *read_die_and_siblings_1
1428 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1429 struct die_info *);
1430
1431 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1432 const gdb_byte *info_ptr,
1433 const gdb_byte **new_info_ptr,
1434 struct die_info *parent);
1435
1436 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1437 struct die_info **, const gdb_byte *,
1438 int);
1439
1440 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1441 struct die_info **, const gdb_byte *);
1442
1443 static void process_die (struct die_info *, struct dwarf2_cu *);
1444
1445 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1446 struct obstack *);
1447
1448 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1449
1450 static const char *dwarf2_full_name (const char *name,
1451 struct die_info *die,
1452 struct dwarf2_cu *cu);
1453
1454 static const char *dwarf2_physname (const char *name, struct die_info *die,
1455 struct dwarf2_cu *cu);
1456
1457 static struct die_info *dwarf2_extension (struct die_info *die,
1458 struct dwarf2_cu **);
1459
1460 static const char *dwarf_tag_name (unsigned int);
1461
1462 static const char *dwarf_attr_name (unsigned int);
1463
1464 static const char *dwarf_form_name (unsigned int);
1465
1466 static const char *dwarf_bool_name (unsigned int);
1467
1468 static const char *dwarf_type_encoding_name (unsigned int);
1469
1470 static struct die_info *sibling_die (struct die_info *);
1471
1472 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1473
1474 static void dump_die_for_error (struct die_info *);
1475
1476 static void dump_die_1 (struct ui_file *, int level, int max_level,
1477 struct die_info *);
1478
1479 /*static*/ void dump_die (struct die_info *, int max_level);
1480
1481 static void store_in_ref_table (struct die_info *,
1482 struct dwarf2_cu *);
1483
1484 static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
1485
1486 static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
1487
1488 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1489 const struct attribute *,
1490 struct dwarf2_cu **);
1491
1492 static struct die_info *follow_die_ref (struct die_info *,
1493 const struct attribute *,
1494 struct dwarf2_cu **);
1495
1496 static struct die_info *follow_die_sig (struct die_info *,
1497 const struct attribute *,
1498 struct dwarf2_cu **);
1499
1500 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1501 struct dwarf2_cu *);
1502
1503 static struct type *get_DW_AT_signature_type (struct die_info *,
1504 const struct attribute *,
1505 struct dwarf2_cu *);
1506
1507 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1508
1509 static void read_signatured_type (struct signatured_type *);
1510
1511 static int attr_to_dynamic_prop (const struct attribute *attr,
1512 struct die_info *die, struct dwarf2_cu *cu,
1513 struct dynamic_prop *prop, struct type *type);
1514
1515 /* memory allocation interface */
1516
1517 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1518
1519 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1520
1521 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1522
1523 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1524 struct dwarf2_loclist_baton *baton,
1525 const struct attribute *attr);
1526
1527 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1528 struct symbol *sym,
1529 struct dwarf2_cu *cu,
1530 int is_block);
1531
1532 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1533 const gdb_byte *info_ptr,
1534 struct abbrev_info *abbrev);
1535
1536 static hashval_t partial_die_hash (const void *item);
1537
1538 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1539
1540 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1541 (sect_offset sect_off, unsigned int offset_in_dwz,
1542 struct dwarf2_per_objfile *dwarf2_per_objfile);
1543
1544 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1545 struct die_info *comp_unit_die,
1546 enum language pretend_language);
1547
1548 static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1549
1550 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1551
1552 static struct type *set_die_type (struct die_info *, struct type *,
1553 struct dwarf2_cu *);
1554
1555 static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1556
1557 static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1558
1559 static void load_full_comp_unit (struct dwarf2_per_cu_data *, bool,
1560 enum language);
1561
1562 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1563 enum language);
1564
1565 static void process_full_type_unit (struct dwarf2_per_cu_data *,
1566 enum language);
1567
1568 static void dwarf2_add_dependence (struct dwarf2_cu *,
1569 struct dwarf2_per_cu_data *);
1570
1571 static void dwarf2_mark (struct dwarf2_cu *);
1572
1573 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1574
1575 static struct type *get_die_type_at_offset (sect_offset,
1576 struct dwarf2_per_cu_data *);
1577
1578 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1579
1580 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1581 enum language pretend_language);
1582
1583 static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
1584
1585 /* Class, the destructor of which frees all allocated queue entries. This
1586 will only have work to do if an error was thrown while processing the
1587 dwarf. If no error was thrown then the queue entries should have all
1588 been processed, and freed, as we went along. */
1589
1590 class dwarf2_queue_guard
1591 {
1592 public:
1593 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1594 : m_per_objfile (per_objfile)
1595 {
1596 }
1597
1598 /* Free any entries remaining on the queue. There should only be
1599 entries left if we hit an error while processing the dwarf. */
1600 ~dwarf2_queue_guard ()
1601 {
1602 /* Ensure that no memory is allocated by the queue. */
1603 std::queue<dwarf2_queue_item> empty;
1604 std::swap (m_per_objfile->queue, empty);
1605 }
1606
1607 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
1608
1609 private:
1610 dwarf2_per_objfile *m_per_objfile;
1611 };
1612
1613 dwarf2_queue_item::~dwarf2_queue_item ()
1614 {
1615 /* Anything still marked queued is likely to be in an
1616 inconsistent state, so discard it. */
1617 if (per_cu->queued)
1618 {
1619 if (per_cu->cu != NULL)
1620 free_one_cached_comp_unit (per_cu);
1621 per_cu->queued = 0;
1622 }
1623 }
1624
1625 /* The return type of find_file_and_directory. Note, the enclosed
1626 string pointers are only valid while this object is valid. */
1627
1628 struct file_and_directory
1629 {
1630 /* The filename. This is never NULL. */
1631 const char *name;
1632
1633 /* The compilation directory. NULL if not known. If we needed to
1634 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1635 points directly to the DW_AT_comp_dir string attribute owned by
1636 the obstack that owns the DIE. */
1637 const char *comp_dir;
1638
1639 /* If we needed to build a new string for comp_dir, this is what
1640 owns the storage. */
1641 std::string comp_dir_storage;
1642 };
1643
1644 static file_and_directory find_file_and_directory (struct die_info *die,
1645 struct dwarf2_cu *cu);
1646
1647 static htab_up allocate_signatured_type_table ();
1648
1649 static htab_up allocate_dwo_unit_table ();
1650
1651 static struct dwo_unit *lookup_dwo_unit_in_dwp
1652 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1653 struct dwp_file *dwp_file, const char *comp_dir,
1654 ULONGEST signature, int is_debug_types);
1655
1656 static struct dwp_file *get_dwp_file
1657 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1658
1659 static struct dwo_unit *lookup_dwo_comp_unit
1660 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
1661
1662 static struct dwo_unit *lookup_dwo_type_unit
1663 (struct signatured_type *, const char *, const char *);
1664
1665 static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1666
1667 /* A unique pointer to a dwo_file. */
1668
1669 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
1670
1671 static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
1672
1673 static void check_producer (struct dwarf2_cu *cu);
1674
1675 static void free_line_header_voidp (void *arg);
1676 \f
1677 /* Various complaints about symbol reading that don't abort the process. */
1678
1679 static void
1680 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
1681 {
1682 complaint (_("statement list doesn't fit in .debug_line section"));
1683 }
1684
1685 static void
1686 dwarf2_debug_line_missing_file_complaint (void)
1687 {
1688 complaint (_(".debug_line section has line data without a file"));
1689 }
1690
1691 static void
1692 dwarf2_debug_line_missing_end_sequence_complaint (void)
1693 {
1694 complaint (_(".debug_line section has line "
1695 "program sequence without an end"));
1696 }
1697
1698 static void
1699 dwarf2_complex_location_expr_complaint (void)
1700 {
1701 complaint (_("location expression too complex"));
1702 }
1703
1704 static void
1705 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1706 int arg3)
1707 {
1708 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
1709 arg1, arg2, arg3);
1710 }
1711
1712 static void
1713 dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
1714 {
1715 complaint (_("debug info runs off end of %s section"
1716 " [in module %s]"),
1717 section->get_name (),
1718 section->get_file_name ());
1719 }
1720
1721 static void
1722 dwarf2_macro_malformed_definition_complaint (const char *arg1)
1723 {
1724 complaint (_("macro debug info contains a "
1725 "malformed macro definition:\n`%s'"),
1726 arg1);
1727 }
1728
1729 static void
1730 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1731 {
1732 complaint (_("invalid attribute class or form for '%s' in '%s'"),
1733 arg1, arg2);
1734 }
1735
1736 /* Hash function for line_header_hash. */
1737
1738 static hashval_t
1739 line_header_hash (const struct line_header *ofs)
1740 {
1741 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
1742 }
1743
1744 /* Hash function for htab_create_alloc_ex for line_header_hash. */
1745
1746 static hashval_t
1747 line_header_hash_voidp (const void *item)
1748 {
1749 const struct line_header *ofs = (const struct line_header *) item;
1750
1751 return line_header_hash (ofs);
1752 }
1753
1754 /* Equality function for line_header_hash. */
1755
1756 static int
1757 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1758 {
1759 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1760 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
1761
1762 return (ofs_lhs->sect_off == ofs_rhs->sect_off
1763 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1764 }
1765
1766 \f
1767
1768 /* See declaration. */
1769
1770 dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
1771 const dwarf2_debug_sections *names,
1772 bool can_copy_)
1773 : objfile (objfile_),
1774 can_copy (can_copy_)
1775 {
1776 if (names == NULL)
1777 names = &dwarf2_elf_names;
1778
1779 bfd *obfd = objfile->obfd;
1780
1781 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1782 locate_sections (obfd, sec, *names);
1783 }
1784
1785 dwarf2_per_objfile::~dwarf2_per_objfile ()
1786 {
1787 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
1788 free_cached_comp_units ();
1789
1790 for (dwarf2_per_cu_data *per_cu : all_comp_units)
1791 per_cu->imported_symtabs_free ();
1792
1793 for (signatured_type *sig_type : all_type_units)
1794 sig_type->per_cu.imported_symtabs_free ();
1795
1796 /* Everything else should be on the objfile obstack. */
1797 }
1798
1799 /* See declaration. */
1800
1801 void
1802 dwarf2_per_objfile::free_cached_comp_units ()
1803 {
1804 dwarf2_per_cu_data *per_cu = read_in_chain;
1805 dwarf2_per_cu_data **last_chain = &read_in_chain;
1806 while (per_cu != NULL)
1807 {
1808 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
1809
1810 delete per_cu->cu;
1811 *last_chain = next_cu;
1812 per_cu = next_cu;
1813 }
1814 }
1815
1816 /* A helper class that calls free_cached_comp_units on
1817 destruction. */
1818
1819 class free_cached_comp_units
1820 {
1821 public:
1822
1823 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1824 : m_per_objfile (per_objfile)
1825 {
1826 }
1827
1828 ~free_cached_comp_units ()
1829 {
1830 m_per_objfile->free_cached_comp_units ();
1831 }
1832
1833 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1834
1835 private:
1836
1837 dwarf2_per_objfile *m_per_objfile;
1838 };
1839
1840 /* Try to locate the sections we need for DWARF 2 debugging
1841 information and return true if we have enough to do something.
1842 NAMES points to the dwarf2 section names, or is NULL if the standard
1843 ELF names are used. CAN_COPY is true for formats where symbol
1844 interposition is possible and so symbol values must follow copy
1845 relocation rules. */
1846
1847 int
1848 dwarf2_has_info (struct objfile *objfile,
1849 const struct dwarf2_debug_sections *names,
1850 bool can_copy)
1851 {
1852 if (objfile->flags & OBJF_READNEVER)
1853 return 0;
1854
1855 struct dwarf2_per_objfile *dwarf2_per_objfile
1856 = get_dwarf2_per_objfile (objfile);
1857
1858 if (dwarf2_per_objfile == NULL)
1859 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile,
1860 names,
1861 can_copy);
1862
1863 return (!dwarf2_per_objfile->info.is_virtual
1864 && dwarf2_per_objfile->info.s.section != NULL
1865 && !dwarf2_per_objfile->abbrev.is_virtual
1866 && dwarf2_per_objfile->abbrev.s.section != NULL);
1867 }
1868
1869 /* When loading sections, we look either for uncompressed section or for
1870 compressed section names. */
1871
1872 static int
1873 section_is_p (const char *section_name,
1874 const struct dwarf2_section_names *names)
1875 {
1876 if (names->normal != NULL
1877 && strcmp (section_name, names->normal) == 0)
1878 return 1;
1879 if (names->compressed != NULL
1880 && strcmp (section_name, names->compressed) == 0)
1881 return 1;
1882 return 0;
1883 }
1884
1885 /* See declaration. */
1886
1887 void
1888 dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
1889 const dwarf2_debug_sections &names)
1890 {
1891 flagword aflag = bfd_section_flags (sectp);
1892
1893 if ((aflag & SEC_HAS_CONTENTS) == 0)
1894 {
1895 }
1896 else if (elf_section_data (sectp)->this_hdr.sh_size
1897 > bfd_get_file_size (abfd))
1898 {
1899 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1900 warning (_("Discarding section %s which has a section size (%s"
1901 ") larger than the file size [in module %s]"),
1902 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1903 bfd_get_filename (abfd));
1904 }
1905 else if (section_is_p (sectp->name, &names.info))
1906 {
1907 this->info.s.section = sectp;
1908 this->info.size = bfd_section_size (sectp);
1909 }
1910 else if (section_is_p (sectp->name, &names.abbrev))
1911 {
1912 this->abbrev.s.section = sectp;
1913 this->abbrev.size = bfd_section_size (sectp);
1914 }
1915 else if (section_is_p (sectp->name, &names.line))
1916 {
1917 this->line.s.section = sectp;
1918 this->line.size = bfd_section_size (sectp);
1919 }
1920 else if (section_is_p (sectp->name, &names.loc))
1921 {
1922 this->loc.s.section = sectp;
1923 this->loc.size = bfd_section_size (sectp);
1924 }
1925 else if (section_is_p (sectp->name, &names.loclists))
1926 {
1927 this->loclists.s.section = sectp;
1928 this->loclists.size = bfd_section_size (sectp);
1929 }
1930 else if (section_is_p (sectp->name, &names.macinfo))
1931 {
1932 this->macinfo.s.section = sectp;
1933 this->macinfo.size = bfd_section_size (sectp);
1934 }
1935 else if (section_is_p (sectp->name, &names.macro))
1936 {
1937 this->macro.s.section = sectp;
1938 this->macro.size = bfd_section_size (sectp);
1939 }
1940 else if (section_is_p (sectp->name, &names.str))
1941 {
1942 this->str.s.section = sectp;
1943 this->str.size = bfd_section_size (sectp);
1944 }
1945 else if (section_is_p (sectp->name, &names.str_offsets))
1946 {
1947 this->str_offsets.s.section = sectp;
1948 this->str_offsets.size = bfd_section_size (sectp);
1949 }
1950 else if (section_is_p (sectp->name, &names.line_str))
1951 {
1952 this->line_str.s.section = sectp;
1953 this->line_str.size = bfd_section_size (sectp);
1954 }
1955 else if (section_is_p (sectp->name, &names.addr))
1956 {
1957 this->addr.s.section = sectp;
1958 this->addr.size = bfd_section_size (sectp);
1959 }
1960 else if (section_is_p (sectp->name, &names.frame))
1961 {
1962 this->frame.s.section = sectp;
1963 this->frame.size = bfd_section_size (sectp);
1964 }
1965 else if (section_is_p (sectp->name, &names.eh_frame))
1966 {
1967 this->eh_frame.s.section = sectp;
1968 this->eh_frame.size = bfd_section_size (sectp);
1969 }
1970 else if (section_is_p (sectp->name, &names.ranges))
1971 {
1972 this->ranges.s.section = sectp;
1973 this->ranges.size = bfd_section_size (sectp);
1974 }
1975 else if (section_is_p (sectp->name, &names.rnglists))
1976 {
1977 this->rnglists.s.section = sectp;
1978 this->rnglists.size = bfd_section_size (sectp);
1979 }
1980 else if (section_is_p (sectp->name, &names.types))
1981 {
1982 struct dwarf2_section_info type_section;
1983
1984 memset (&type_section, 0, sizeof (type_section));
1985 type_section.s.section = sectp;
1986 type_section.size = bfd_section_size (sectp);
1987
1988 this->types.push_back (type_section);
1989 }
1990 else if (section_is_p (sectp->name, &names.gdb_index))
1991 {
1992 this->gdb_index.s.section = sectp;
1993 this->gdb_index.size = bfd_section_size (sectp);
1994 }
1995 else if (section_is_p (sectp->name, &names.debug_names))
1996 {
1997 this->debug_names.s.section = sectp;
1998 this->debug_names.size = bfd_section_size (sectp);
1999 }
2000 else if (section_is_p (sectp->name, &names.debug_aranges))
2001 {
2002 this->debug_aranges.s.section = sectp;
2003 this->debug_aranges.size = bfd_section_size (sectp);
2004 }
2005
2006 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2007 && bfd_section_vma (sectp) == 0)
2008 this->has_section_at_zero = true;
2009 }
2010
2011 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2012 SECTION_NAME. */
2013
2014 void
2015 dwarf2_get_section_info (struct objfile *objfile,
2016 enum dwarf2_section_enum sect,
2017 asection **sectp, const gdb_byte **bufp,
2018 bfd_size_type *sizep)
2019 {
2020 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
2021 struct dwarf2_section_info *info;
2022
2023 /* We may see an objfile without any DWARF, in which case we just
2024 return nothing. */
2025 if (data == NULL)
2026 {
2027 *sectp = NULL;
2028 *bufp = NULL;
2029 *sizep = 0;
2030 return;
2031 }
2032 switch (sect)
2033 {
2034 case DWARF2_DEBUG_FRAME:
2035 info = &data->frame;
2036 break;
2037 case DWARF2_EH_FRAME:
2038 info = &data->eh_frame;
2039 break;
2040 default:
2041 gdb_assert_not_reached ("unexpected section");
2042 }
2043
2044 info->read (objfile);
2045
2046 *sectp = info->get_bfd_section ();
2047 *bufp = info->buffer;
2048 *sizep = info->size;
2049 }
2050
2051 /* A helper function to find the sections for a .dwz file. */
2052
2053 static void
2054 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2055 {
2056 struct dwz_file *dwz_file = (struct dwz_file *) arg;
2057
2058 /* Note that we only support the standard ELF names, because .dwz
2059 is ELF-only (at the time of writing). */
2060 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2061 {
2062 dwz_file->abbrev.s.section = sectp;
2063 dwz_file->abbrev.size = bfd_section_size (sectp);
2064 }
2065 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2066 {
2067 dwz_file->info.s.section = sectp;
2068 dwz_file->info.size = bfd_section_size (sectp);
2069 }
2070 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2071 {
2072 dwz_file->str.s.section = sectp;
2073 dwz_file->str.size = bfd_section_size (sectp);
2074 }
2075 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2076 {
2077 dwz_file->line.s.section = sectp;
2078 dwz_file->line.size = bfd_section_size (sectp);
2079 }
2080 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2081 {
2082 dwz_file->macro.s.section = sectp;
2083 dwz_file->macro.size = bfd_section_size (sectp);
2084 }
2085 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2086 {
2087 dwz_file->gdb_index.s.section = sectp;
2088 dwz_file->gdb_index.size = bfd_section_size (sectp);
2089 }
2090 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2091 {
2092 dwz_file->debug_names.s.section = sectp;
2093 dwz_file->debug_names.size = bfd_section_size (sectp);
2094 }
2095 }
2096
2097 /* See dwarf2read.h. */
2098
2099 struct dwz_file *
2100 dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
2101 {
2102 const char *filename;
2103 bfd_size_type buildid_len_arg;
2104 size_t buildid_len;
2105 bfd_byte *buildid;
2106
2107 if (dwarf2_per_objfile->dwz_file != NULL)
2108 return dwarf2_per_objfile->dwz_file.get ();
2109
2110 bfd_set_error (bfd_error_no_error);
2111 gdb::unique_xmalloc_ptr<char> data
2112 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2113 &buildid_len_arg, &buildid));
2114 if (data == NULL)
2115 {
2116 if (bfd_get_error () == bfd_error_no_error)
2117 return NULL;
2118 error (_("could not read '.gnu_debugaltlink' section: %s"),
2119 bfd_errmsg (bfd_get_error ()));
2120 }
2121
2122 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
2123
2124 buildid_len = (size_t) buildid_len_arg;
2125
2126 filename = data.get ();
2127
2128 std::string abs_storage;
2129 if (!IS_ABSOLUTE_PATH (filename))
2130 {
2131 gdb::unique_xmalloc_ptr<char> abs
2132 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
2133
2134 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
2135 filename = abs_storage.c_str ();
2136 }
2137
2138 /* First try the file name given in the section. If that doesn't
2139 work, try to use the build-id instead. */
2140 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
2141 if (dwz_bfd != NULL)
2142 {
2143 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2144 dwz_bfd.reset (nullptr);
2145 }
2146
2147 if (dwz_bfd == NULL)
2148 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2149
2150 if (dwz_bfd == NULL)
2151 error (_("could not find '.gnu_debugaltlink' file for %s"),
2152 objfile_name (dwarf2_per_objfile->objfile));
2153
2154 std::unique_ptr<struct dwz_file> result
2155 (new struct dwz_file (std::move (dwz_bfd)));
2156
2157 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2158 result.get ());
2159
2160 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd,
2161 result->dwz_bfd.get ());
2162 dwarf2_per_objfile->dwz_file = std::move (result);
2163 return dwarf2_per_objfile->dwz_file.get ();
2164 }
2165 \f
2166 /* DWARF quick_symbols_functions support. */
2167
2168 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2169 unique line tables, so we maintain a separate table of all .debug_line
2170 derived entries to support the sharing.
2171 All the quick functions need is the list of file names. We discard the
2172 line_header when we're done and don't need to record it here. */
2173 struct quick_file_names
2174 {
2175 /* The data used to construct the hash key. */
2176 struct stmt_list_hash hash;
2177
2178 /* The number of entries in file_names, real_names. */
2179 unsigned int num_file_names;
2180
2181 /* The file names from the line table, after being run through
2182 file_full_name. */
2183 const char **file_names;
2184
2185 /* The file names from the line table after being run through
2186 gdb_realpath. These are computed lazily. */
2187 const char **real_names;
2188 };
2189
2190 /* When using the index (and thus not using psymtabs), each CU has an
2191 object of this type. This is used to hold information needed by
2192 the various "quick" methods. */
2193 struct dwarf2_per_cu_quick_data
2194 {
2195 /* The file table. This can be NULL if there was no file table
2196 or it's currently not read in.
2197 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2198 struct quick_file_names *file_names;
2199
2200 /* The corresponding symbol table. This is NULL if symbols for this
2201 CU have not yet been read. */
2202 struct compunit_symtab *compunit_symtab;
2203
2204 /* A temporary mark bit used when iterating over all CUs in
2205 expand_symtabs_matching. */
2206 unsigned int mark : 1;
2207
2208 /* True if we've tried to read the file table and found there isn't one.
2209 There will be no point in trying to read it again next time. */
2210 unsigned int no_file_data : 1;
2211 };
2212
2213 /* Utility hash function for a stmt_list_hash. */
2214
2215 static hashval_t
2216 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2217 {
2218 hashval_t v = 0;
2219
2220 if (stmt_list_hash->dwo_unit != NULL)
2221 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2222 v += to_underlying (stmt_list_hash->line_sect_off);
2223 return v;
2224 }
2225
2226 /* Utility equality function for a stmt_list_hash. */
2227
2228 static int
2229 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2230 const struct stmt_list_hash *rhs)
2231 {
2232 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2233 return 0;
2234 if (lhs->dwo_unit != NULL
2235 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2236 return 0;
2237
2238 return lhs->line_sect_off == rhs->line_sect_off;
2239 }
2240
2241 /* Hash function for a quick_file_names. */
2242
2243 static hashval_t
2244 hash_file_name_entry (const void *e)
2245 {
2246 const struct quick_file_names *file_data
2247 = (const struct quick_file_names *) e;
2248
2249 return hash_stmt_list_entry (&file_data->hash);
2250 }
2251
2252 /* Equality function for a quick_file_names. */
2253
2254 static int
2255 eq_file_name_entry (const void *a, const void *b)
2256 {
2257 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2258 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2259
2260 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2261 }
2262
2263 /* Delete function for a quick_file_names. */
2264
2265 static void
2266 delete_file_name_entry (void *e)
2267 {
2268 struct quick_file_names *file_data = (struct quick_file_names *) e;
2269 int i;
2270
2271 for (i = 0; i < file_data->num_file_names; ++i)
2272 {
2273 xfree ((void*) file_data->file_names[i]);
2274 if (file_data->real_names)
2275 xfree ((void*) file_data->real_names[i]);
2276 }
2277
2278 /* The space for the struct itself lives on objfile_obstack,
2279 so we don't free it here. */
2280 }
2281
2282 /* Create a quick_file_names hash table. */
2283
2284 static htab_up
2285 create_quick_file_names_table (unsigned int nr_initial_entries)
2286 {
2287 return htab_up (htab_create_alloc (nr_initial_entries,
2288 hash_file_name_entry, eq_file_name_entry,
2289 delete_file_name_entry, xcalloc, xfree));
2290 }
2291
2292 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2293 have to be created afterwards. You should call age_cached_comp_units after
2294 processing PER_CU->CU. dw2_setup must have been already called. */
2295
2296 static void
2297 load_cu (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2298 {
2299 if (per_cu->is_debug_types)
2300 load_full_type_unit (per_cu);
2301 else
2302 load_full_comp_unit (per_cu, skip_partial, language_minimal);
2303
2304 if (per_cu->cu == NULL)
2305 return; /* Dummy CU. */
2306
2307 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2308 }
2309
2310 /* Read in the symbols for PER_CU. */
2311
2312 static void
2313 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2314 {
2315 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2316
2317 /* Skip type_unit_groups, reading the type units they contain
2318 is handled elsewhere. */
2319 if (per_cu->type_unit_group_p ())
2320 return;
2321
2322 /* The destructor of dwarf2_queue_guard frees any entries left on
2323 the queue. After this point we're guaranteed to leave this function
2324 with the dwarf queue empty. */
2325 dwarf2_queue_guard q_guard (dwarf2_per_objfile);
2326
2327 if (dwarf2_per_objfile->using_index
2328 ? per_cu->v.quick->compunit_symtab == NULL
2329 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2330 {
2331 queue_comp_unit (per_cu, language_minimal);
2332 load_cu (per_cu, skip_partial);
2333
2334 /* If we just loaded a CU from a DWO, and we're working with an index
2335 that may badly handle TUs, load all the TUs in that DWO as well.
2336 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2337 if (!per_cu->is_debug_types
2338 && per_cu->cu != NULL
2339 && per_cu->cu->dwo_unit != NULL
2340 && dwarf2_per_objfile->index_table != NULL
2341 && dwarf2_per_objfile->index_table->version <= 7
2342 /* DWP files aren't supported yet. */
2343 && get_dwp_file (dwarf2_per_objfile) == NULL)
2344 queue_and_load_all_dwo_tus (per_cu);
2345 }
2346
2347 process_queue (dwarf2_per_objfile);
2348
2349 /* Age the cache, releasing compilation units that have not
2350 been used recently. */
2351 age_cached_comp_units (dwarf2_per_objfile);
2352 }
2353
2354 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2355 the objfile from which this CU came. Returns the resulting symbol
2356 table. */
2357
2358 static struct compunit_symtab *
2359 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2360 {
2361 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2362
2363 gdb_assert (dwarf2_per_objfile->using_index);
2364 if (!per_cu->v.quick->compunit_symtab)
2365 {
2366 free_cached_comp_units freer (dwarf2_per_objfile);
2367 scoped_restore decrementer = increment_reading_symtab ();
2368 dw2_do_instantiate_symtab (per_cu, skip_partial);
2369 process_cu_includes (dwarf2_per_objfile);
2370 }
2371
2372 return per_cu->v.quick->compunit_symtab;
2373 }
2374
2375 /* See declaration. */
2376
2377 dwarf2_per_cu_data *
2378 dwarf2_per_objfile::get_cutu (int index)
2379 {
2380 if (index >= this->all_comp_units.size ())
2381 {
2382 index -= this->all_comp_units.size ();
2383 gdb_assert (index < this->all_type_units.size ());
2384 return &this->all_type_units[index]->per_cu;
2385 }
2386
2387 return this->all_comp_units[index];
2388 }
2389
2390 /* See declaration. */
2391
2392 dwarf2_per_cu_data *
2393 dwarf2_per_objfile::get_cu (int index)
2394 {
2395 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
2396
2397 return this->all_comp_units[index];
2398 }
2399
2400 /* See declaration. */
2401
2402 signatured_type *
2403 dwarf2_per_objfile::get_tu (int index)
2404 {
2405 gdb_assert (index >= 0 && index < this->all_type_units.size ());
2406
2407 return this->all_type_units[index];
2408 }
2409
2410 /* Return a new dwarf2_per_cu_data allocated on OBJFILE's
2411 objfile_obstack, and constructed with the specified field
2412 values. */
2413
2414 static dwarf2_per_cu_data *
2415 create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2416 struct dwarf2_section_info *section,
2417 int is_dwz,
2418 sect_offset sect_off, ULONGEST length)
2419 {
2420 struct objfile *objfile = dwarf2_per_objfile->objfile;
2421 dwarf2_per_cu_data *the_cu
2422 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2423 struct dwarf2_per_cu_data);
2424 the_cu->sect_off = sect_off;
2425 the_cu->length = length;
2426 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
2427 the_cu->section = section;
2428 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2429 struct dwarf2_per_cu_quick_data);
2430 the_cu->is_dwz = is_dwz;
2431 return the_cu;
2432 }
2433
2434 /* A helper for create_cus_from_index that handles a given list of
2435 CUs. */
2436
2437 static void
2438 create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2439 const gdb_byte *cu_list, offset_type n_elements,
2440 struct dwarf2_section_info *section,
2441 int is_dwz)
2442 {
2443 for (offset_type i = 0; i < n_elements; i += 2)
2444 {
2445 gdb_static_assert (sizeof (ULONGEST) >= 8);
2446
2447 sect_offset sect_off
2448 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2449 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2450 cu_list += 2 * 8;
2451
2452 dwarf2_per_cu_data *per_cu
2453 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
2454 sect_off, length);
2455 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
2456 }
2457 }
2458
2459 /* Read the CU list from the mapped index, and use it to create all
2460 the CU objects for this objfile. */
2461
2462 static void
2463 create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2464 const gdb_byte *cu_list, offset_type cu_list_elements,
2465 const gdb_byte *dwz_list, offset_type dwz_elements)
2466 {
2467 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
2468 dwarf2_per_objfile->all_comp_units.reserve
2469 ((cu_list_elements + dwz_elements) / 2);
2470
2471 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
2472 &dwarf2_per_objfile->info, 0);
2473
2474 if (dwz_elements == 0)
2475 return;
2476
2477 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
2478 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
2479 &dwz->info, 1);
2480 }
2481
2482 /* Create the signatured type hash table from the index. */
2483
2484 static void
2485 create_signatured_type_table_from_index
2486 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2487 struct dwarf2_section_info *section,
2488 const gdb_byte *bytes,
2489 offset_type elements)
2490 {
2491 struct objfile *objfile = dwarf2_per_objfile->objfile;
2492
2493 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
2494 dwarf2_per_objfile->all_type_units.reserve (elements / 3);
2495
2496 htab_up sig_types_hash = allocate_signatured_type_table ();
2497
2498 for (offset_type i = 0; i < elements; i += 3)
2499 {
2500 struct signatured_type *sig_type;
2501 ULONGEST signature;
2502 void **slot;
2503 cu_offset type_offset_in_tu;
2504
2505 gdb_static_assert (sizeof (ULONGEST) >= 8);
2506 sect_offset sect_off
2507 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2508 type_offset_in_tu
2509 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2510 BFD_ENDIAN_LITTLE);
2511 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2512 bytes += 3 * 8;
2513
2514 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2515 struct signatured_type);
2516 sig_type->signature = signature;
2517 sig_type->type_offset_in_tu = type_offset_in_tu;
2518 sig_type->per_cu.is_debug_types = 1;
2519 sig_type->per_cu.section = section;
2520 sig_type->per_cu.sect_off = sect_off;
2521 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
2522 sig_type->per_cu.v.quick
2523 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2524 struct dwarf2_per_cu_quick_data);
2525
2526 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2527 *slot = sig_type;
2528
2529 dwarf2_per_objfile->all_type_units.push_back (sig_type);
2530 }
2531
2532 dwarf2_per_objfile->signatured_types = std::move (sig_types_hash);
2533 }
2534
2535 /* Create the signatured type hash table from .debug_names. */
2536
2537 static void
2538 create_signatured_type_table_from_debug_names
2539 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2540 const mapped_debug_names &map,
2541 struct dwarf2_section_info *section,
2542 struct dwarf2_section_info *abbrev_section)
2543 {
2544 struct objfile *objfile = dwarf2_per_objfile->objfile;
2545
2546 section->read (objfile);
2547 abbrev_section->read (objfile);
2548
2549 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
2550 dwarf2_per_objfile->all_type_units.reserve (map.tu_count);
2551
2552 htab_up sig_types_hash = allocate_signatured_type_table ();
2553
2554 for (uint32_t i = 0; i < map.tu_count; ++i)
2555 {
2556 struct signatured_type *sig_type;
2557 void **slot;
2558
2559 sect_offset sect_off
2560 = (sect_offset) (extract_unsigned_integer
2561 (map.tu_table_reordered + i * map.offset_size,
2562 map.offset_size,
2563 map.dwarf5_byte_order));
2564
2565 comp_unit_head cu_header;
2566 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
2567 abbrev_section,
2568 section->buffer + to_underlying (sect_off),
2569 rcuh_kind::TYPE);
2570
2571 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2572 struct signatured_type);
2573 sig_type->signature = cu_header.signature;
2574 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2575 sig_type->per_cu.is_debug_types = 1;
2576 sig_type->per_cu.section = section;
2577 sig_type->per_cu.sect_off = sect_off;
2578 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
2579 sig_type->per_cu.v.quick
2580 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2581 struct dwarf2_per_cu_quick_data);
2582
2583 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2584 *slot = sig_type;
2585
2586 dwarf2_per_objfile->all_type_units.push_back (sig_type);
2587 }
2588
2589 dwarf2_per_objfile->signatured_types = std::move (sig_types_hash);
2590 }
2591
2592 /* Read the address map data from the mapped index, and use it to
2593 populate the objfile's psymtabs_addrmap. */
2594
2595 static void
2596 create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2597 struct mapped_index *index)
2598 {
2599 struct objfile *objfile = dwarf2_per_objfile->objfile;
2600 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2601 const gdb_byte *iter, *end;
2602 struct addrmap *mutable_map;
2603 CORE_ADDR baseaddr;
2604
2605 auto_obstack temp_obstack;
2606
2607 mutable_map = addrmap_create_mutable (&temp_obstack);
2608
2609 iter = index->address_table.data ();
2610 end = iter + index->address_table.size ();
2611
2612 baseaddr = objfile->text_section_offset ();
2613
2614 while (iter < end)
2615 {
2616 ULONGEST hi, lo, cu_index;
2617 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2618 iter += 8;
2619 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2620 iter += 8;
2621 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2622 iter += 4;
2623
2624 if (lo > hi)
2625 {
2626 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
2627 hex_string (lo), hex_string (hi));
2628 continue;
2629 }
2630
2631 if (cu_index >= dwarf2_per_objfile->all_comp_units.size ())
2632 {
2633 complaint (_(".gdb_index address table has invalid CU number %u"),
2634 (unsigned) cu_index);
2635 continue;
2636 }
2637
2638 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2639 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
2640 addrmap_set_empty (mutable_map, lo, hi - 1,
2641 dwarf2_per_objfile->get_cu (cu_index));
2642 }
2643
2644 objfile->partial_symtabs->psymtabs_addrmap
2645 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2646 }
2647
2648 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
2649 populate the objfile's psymtabs_addrmap. */
2650
2651 static void
2652 create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
2653 struct dwarf2_section_info *section)
2654 {
2655 struct objfile *objfile = dwarf2_per_objfile->objfile;
2656 bfd *abfd = objfile->obfd;
2657 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2658 const CORE_ADDR baseaddr = objfile->text_section_offset ();
2659
2660 auto_obstack temp_obstack;
2661 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2662
2663 std::unordered_map<sect_offset,
2664 dwarf2_per_cu_data *,
2665 gdb::hash_enum<sect_offset>>
2666 debug_info_offset_to_per_cu;
2667 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
2668 {
2669 const auto insertpair
2670 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2671 if (!insertpair.second)
2672 {
2673 warning (_("Section .debug_aranges in %s has duplicate "
2674 "debug_info_offset %s, ignoring .debug_aranges."),
2675 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
2676 return;
2677 }
2678 }
2679
2680 section->read (objfile);
2681
2682 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2683
2684 const gdb_byte *addr = section->buffer;
2685
2686 while (addr < section->buffer + section->size)
2687 {
2688 const gdb_byte *const entry_addr = addr;
2689 unsigned int bytes_read;
2690
2691 const LONGEST entry_length = read_initial_length (abfd, addr,
2692 &bytes_read);
2693 addr += bytes_read;
2694
2695 const gdb_byte *const entry_end = addr + entry_length;
2696 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2697 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2698 if (addr + entry_length > section->buffer + section->size)
2699 {
2700 warning (_("Section .debug_aranges in %s entry at offset %s "
2701 "length %s exceeds section length %s, "
2702 "ignoring .debug_aranges."),
2703 objfile_name (objfile),
2704 plongest (entry_addr - section->buffer),
2705 plongest (bytes_read + entry_length),
2706 pulongest (section->size));
2707 return;
2708 }
2709
2710 /* The version number. */
2711 const uint16_t version = read_2_bytes (abfd, addr);
2712 addr += 2;
2713 if (version != 2)
2714 {
2715 warning (_("Section .debug_aranges in %s entry at offset %s "
2716 "has unsupported version %d, ignoring .debug_aranges."),
2717 objfile_name (objfile),
2718 plongest (entry_addr - section->buffer), version);
2719 return;
2720 }
2721
2722 const uint64_t debug_info_offset
2723 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2724 addr += offset_size;
2725 const auto per_cu_it
2726 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2727 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2728 {
2729 warning (_("Section .debug_aranges in %s entry at offset %s "
2730 "debug_info_offset %s does not exists, "
2731 "ignoring .debug_aranges."),
2732 objfile_name (objfile),
2733 plongest (entry_addr - section->buffer),
2734 pulongest (debug_info_offset));
2735 return;
2736 }
2737 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2738
2739 const uint8_t address_size = *addr++;
2740 if (address_size < 1 || address_size > 8)
2741 {
2742 warning (_("Section .debug_aranges in %s entry at offset %s "
2743 "address_size %u is invalid, ignoring .debug_aranges."),
2744 objfile_name (objfile),
2745 plongest (entry_addr - section->buffer), address_size);
2746 return;
2747 }
2748
2749 const uint8_t segment_selector_size = *addr++;
2750 if (segment_selector_size != 0)
2751 {
2752 warning (_("Section .debug_aranges in %s entry at offset %s "
2753 "segment_selector_size %u is not supported, "
2754 "ignoring .debug_aranges."),
2755 objfile_name (objfile),
2756 plongest (entry_addr - section->buffer),
2757 segment_selector_size);
2758 return;
2759 }
2760
2761 /* Must pad to an alignment boundary that is twice the address
2762 size. It is undocumented by the DWARF standard but GCC does
2763 use it. */
2764 for (size_t padding = ((-(addr - section->buffer))
2765 & (2 * address_size - 1));
2766 padding > 0; padding--)
2767 if (*addr++ != 0)
2768 {
2769 warning (_("Section .debug_aranges in %s entry at offset %s "
2770 "padding is not zero, ignoring .debug_aranges."),
2771 objfile_name (objfile),
2772 plongest (entry_addr - section->buffer));
2773 return;
2774 }
2775
2776 for (;;)
2777 {
2778 if (addr + 2 * address_size > entry_end)
2779 {
2780 warning (_("Section .debug_aranges in %s entry at offset %s "
2781 "address list is not properly terminated, "
2782 "ignoring .debug_aranges."),
2783 objfile_name (objfile),
2784 plongest (entry_addr - section->buffer));
2785 return;
2786 }
2787 ULONGEST start = extract_unsigned_integer (addr, address_size,
2788 dwarf5_byte_order);
2789 addr += address_size;
2790 ULONGEST length = extract_unsigned_integer (addr, address_size,
2791 dwarf5_byte_order);
2792 addr += address_size;
2793 if (start == 0 && length == 0)
2794 break;
2795 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
2796 {
2797 /* Symbol was eliminated due to a COMDAT group. */
2798 continue;
2799 }
2800 ULONGEST end = start + length;
2801 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2802 - baseaddr);
2803 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2804 - baseaddr);
2805 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2806 }
2807 }
2808
2809 objfile->partial_symtabs->psymtabs_addrmap
2810 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2811 }
2812
2813 /* Find a slot in the mapped index INDEX for the object named NAME.
2814 If NAME is found, set *VEC_OUT to point to the CU vector in the
2815 constant pool and return true. If NAME cannot be found, return
2816 false. */
2817
2818 static bool
2819 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2820 offset_type **vec_out)
2821 {
2822 offset_type hash;
2823 offset_type slot, step;
2824 int (*cmp) (const char *, const char *);
2825
2826 gdb::unique_xmalloc_ptr<char> without_params;
2827 if (current_language->la_language == language_cplus
2828 || current_language->la_language == language_fortran
2829 || current_language->la_language == language_d)
2830 {
2831 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2832 not contain any. */
2833
2834 if (strchr (name, '(') != NULL)
2835 {
2836 without_params = cp_remove_params (name);
2837
2838 if (without_params != NULL)
2839 name = without_params.get ();
2840 }
2841 }
2842
2843 /* Index version 4 did not support case insensitive searches. But the
2844 indices for case insensitive languages are built in lowercase, therefore
2845 simulate our NAME being searched is also lowercased. */
2846 hash = mapped_index_string_hash ((index->version == 4
2847 && case_sensitivity == case_sensitive_off
2848 ? 5 : index->version),
2849 name);
2850
2851 slot = hash & (index->symbol_table.size () - 1);
2852 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
2853 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2854
2855 for (;;)
2856 {
2857 const char *str;
2858
2859 const auto &bucket = index->symbol_table[slot];
2860 if (bucket.name == 0 && bucket.vec == 0)
2861 return false;
2862
2863 str = index->constant_pool + MAYBE_SWAP (bucket.name);
2864 if (!cmp (name, str))
2865 {
2866 *vec_out = (offset_type *) (index->constant_pool
2867 + MAYBE_SWAP (bucket.vec));
2868 return true;
2869 }
2870
2871 slot = (slot + step) & (index->symbol_table.size () - 1);
2872 }
2873 }
2874
2875 /* A helper function that reads the .gdb_index from BUFFER and fills
2876 in MAP. FILENAME is the name of the file containing the data;
2877 it is used for error reporting. DEPRECATED_OK is true if it is
2878 ok to use deprecated sections.
2879
2880 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2881 out parameters that are filled in with information about the CU and
2882 TU lists in the section.
2883
2884 Returns true if all went well, false otherwise. */
2885
2886 static bool
2887 read_gdb_index_from_buffer (struct objfile *objfile,
2888 const char *filename,
2889 bool deprecated_ok,
2890 gdb::array_view<const gdb_byte> buffer,
2891 struct mapped_index *map,
2892 const gdb_byte **cu_list,
2893 offset_type *cu_list_elements,
2894 const gdb_byte **types_list,
2895 offset_type *types_list_elements)
2896 {
2897 const gdb_byte *addr = &buffer[0];
2898
2899 /* Version check. */
2900 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
2901 /* Versions earlier than 3 emitted every copy of a psymbol. This
2902 causes the index to behave very poorly for certain requests. Version 3
2903 contained incomplete addrmap. So, it seems better to just ignore such
2904 indices. */
2905 if (version < 4)
2906 {
2907 static int warning_printed = 0;
2908 if (!warning_printed)
2909 {
2910 warning (_("Skipping obsolete .gdb_index section in %s."),
2911 filename);
2912 warning_printed = 1;
2913 }
2914 return 0;
2915 }
2916 /* Index version 4 uses a different hash function than index version
2917 5 and later.
2918
2919 Versions earlier than 6 did not emit psymbols for inlined
2920 functions. Using these files will cause GDB not to be able to
2921 set breakpoints on inlined functions by name, so we ignore these
2922 indices unless the user has done
2923 "set use-deprecated-index-sections on". */
2924 if (version < 6 && !deprecated_ok)
2925 {
2926 static int warning_printed = 0;
2927 if (!warning_printed)
2928 {
2929 warning (_("\
2930 Skipping deprecated .gdb_index section in %s.\n\
2931 Do \"set use-deprecated-index-sections on\" before the file is read\n\
2932 to use the section anyway."),
2933 filename);
2934 warning_printed = 1;
2935 }
2936 return 0;
2937 }
2938 /* Version 7 indices generated by gold refer to the CU for a symbol instead
2939 of the TU (for symbols coming from TUs),
2940 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
2941 Plus gold-generated indices can have duplicate entries for global symbols,
2942 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
2943 These are just performance bugs, and we can't distinguish gdb-generated
2944 indices from gold-generated ones, so issue no warning here. */
2945
2946 /* Indexes with higher version than the one supported by GDB may be no
2947 longer backward compatible. */
2948 if (version > 8)
2949 return 0;
2950
2951 map->version = version;
2952
2953 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
2954
2955 int i = 0;
2956 *cu_list = addr + MAYBE_SWAP (metadata[i]);
2957 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2958 / 8);
2959 ++i;
2960
2961 *types_list = addr + MAYBE_SWAP (metadata[i]);
2962 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2963 - MAYBE_SWAP (metadata[i]))
2964 / 8);
2965 ++i;
2966
2967 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
2968 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
2969 map->address_table
2970 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
2971 ++i;
2972
2973 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
2974 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
2975 map->symbol_table
2976 = gdb::array_view<mapped_index::symbol_table_slot>
2977 ((mapped_index::symbol_table_slot *) symbol_table,
2978 (mapped_index::symbol_table_slot *) symbol_table_end);
2979
2980 ++i;
2981 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
2982
2983 return 1;
2984 }
2985
2986 /* Callback types for dwarf2_read_gdb_index. */
2987
2988 typedef gdb::function_view
2989 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_objfile *)>
2990 get_gdb_index_contents_ftype;
2991 typedef gdb::function_view
2992 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
2993 get_gdb_index_contents_dwz_ftype;
2994
2995 /* Read .gdb_index. If everything went ok, initialize the "quick"
2996 elements of all the CUs and return 1. Otherwise, return 0. */
2997
2998 static int
2999 dwarf2_read_gdb_index
3000 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3001 get_gdb_index_contents_ftype get_gdb_index_contents,
3002 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
3003 {
3004 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3005 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3006 struct dwz_file *dwz;
3007 struct objfile *objfile = dwarf2_per_objfile->objfile;
3008
3009 gdb::array_view<const gdb_byte> main_index_contents
3010 = get_gdb_index_contents (objfile, dwarf2_per_objfile);
3011
3012 if (main_index_contents.empty ())
3013 return 0;
3014
3015 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3016 if (!read_gdb_index_from_buffer (objfile, objfile_name (objfile),
3017 use_deprecated_index_sections,
3018 main_index_contents, map.get (), &cu_list,
3019 &cu_list_elements, &types_list,
3020 &types_list_elements))
3021 return 0;
3022
3023 /* Don't use the index if it's empty. */
3024 if (map->symbol_table.empty ())
3025 return 0;
3026
3027 /* If there is a .dwz file, read it so we can get its CU list as
3028 well. */
3029 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3030 if (dwz != NULL)
3031 {
3032 struct mapped_index dwz_map;
3033 const gdb_byte *dwz_types_ignore;
3034 offset_type dwz_types_elements_ignore;
3035
3036 gdb::array_view<const gdb_byte> dwz_index_content
3037 = get_gdb_index_contents_dwz (objfile, dwz);
3038
3039 if (dwz_index_content.empty ())
3040 return 0;
3041
3042 if (!read_gdb_index_from_buffer (objfile,
3043 bfd_get_filename (dwz->dwz_bfd.get ()),
3044 1, dwz_index_content, &dwz_map,
3045 &dwz_list, &dwz_list_elements,
3046 &dwz_types_ignore,
3047 &dwz_types_elements_ignore))
3048 {
3049 warning (_("could not read '.gdb_index' section from %s; skipping"),
3050 bfd_get_filename (dwz->dwz_bfd.get ()));
3051 return 0;
3052 }
3053 }
3054
3055 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3056 dwz_list, dwz_list_elements);
3057
3058 if (types_list_elements)
3059 {
3060 /* We can only handle a single .debug_types when we have an
3061 index. */
3062 if (dwarf2_per_objfile->types.size () != 1)
3063 return 0;
3064
3065 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
3066
3067 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3068 types_list, types_list_elements);
3069 }
3070
3071 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
3072
3073 dwarf2_per_objfile->index_table = std::move (map);
3074 dwarf2_per_objfile->using_index = 1;
3075 dwarf2_per_objfile->quick_file_names_table =
3076 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
3077
3078 return 1;
3079 }
3080
3081 /* die_reader_func for dw2_get_file_names. */
3082
3083 static void
3084 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3085 const gdb_byte *info_ptr,
3086 struct die_info *comp_unit_die)
3087 {
3088 struct dwarf2_cu *cu = reader->cu;
3089 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3090 struct dwarf2_per_objfile *dwarf2_per_objfile
3091 = cu->per_cu->dwarf2_per_objfile;
3092 struct objfile *objfile = dwarf2_per_objfile->objfile;
3093 struct dwarf2_per_cu_data *lh_cu;
3094 struct attribute *attr;
3095 void **slot;
3096 struct quick_file_names *qfn;
3097
3098 gdb_assert (! this_cu->is_debug_types);
3099
3100 /* Our callers never want to match partial units -- instead they
3101 will match the enclosing full CU. */
3102 if (comp_unit_die->tag == DW_TAG_partial_unit)
3103 {
3104 this_cu->v.quick->no_file_data = 1;
3105 return;
3106 }
3107
3108 lh_cu = this_cu;
3109 slot = NULL;
3110
3111 line_header_up lh;
3112 sect_offset line_offset {};
3113
3114 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3115 if (attr != nullptr)
3116 {
3117 struct quick_file_names find_entry;
3118
3119 line_offset = (sect_offset) DW_UNSND (attr);
3120
3121 /* We may have already read in this line header (TU line header sharing).
3122 If we have we're done. */
3123 find_entry.hash.dwo_unit = cu->dwo_unit;
3124 find_entry.hash.line_sect_off = line_offset;
3125 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table.get (),
3126 &find_entry, INSERT);
3127 if (*slot != NULL)
3128 {
3129 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3130 return;
3131 }
3132
3133 lh = dwarf_decode_line_header (line_offset, cu);
3134 }
3135 if (lh == NULL)
3136 {
3137 lh_cu->v.quick->no_file_data = 1;
3138 return;
3139 }
3140
3141 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
3142 qfn->hash.dwo_unit = cu->dwo_unit;
3143 qfn->hash.line_sect_off = line_offset;
3144 gdb_assert (slot != NULL);
3145 *slot = qfn;
3146
3147 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3148
3149 int offset = 0;
3150 if (strcmp (fnd.name, "<unknown>") != 0)
3151 ++offset;
3152
3153 qfn->num_file_names = offset + lh->file_names_size ();
3154 qfn->file_names =
3155 XOBNEWVEC (&objfile->objfile_obstack, const char *, qfn->num_file_names);
3156 if (offset != 0)
3157 qfn->file_names[0] = xstrdup (fnd.name);
3158 for (int i = 0; i < lh->file_names_size (); ++i)
3159 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3160 fnd.comp_dir).release ();
3161 qfn->real_names = NULL;
3162
3163 lh_cu->v.quick->file_names = qfn;
3164 }
3165
3166 /* A helper for the "quick" functions which attempts to read the line
3167 table for THIS_CU. */
3168
3169 static struct quick_file_names *
3170 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
3171 {
3172 /* This should never be called for TUs. */
3173 gdb_assert (! this_cu->is_debug_types);
3174 /* Nor type unit groups. */
3175 gdb_assert (! this_cu->type_unit_group_p ());
3176
3177 if (this_cu->v.quick->file_names != NULL)
3178 return this_cu->v.quick->file_names;
3179 /* If we know there is no line data, no point in looking again. */
3180 if (this_cu->v.quick->no_file_data)
3181 return NULL;
3182
3183 cutu_reader reader (this_cu);
3184 if (!reader.dummy_p)
3185 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die);
3186
3187 if (this_cu->v.quick->no_file_data)
3188 return NULL;
3189 return this_cu->v.quick->file_names;
3190 }
3191
3192 /* A helper for the "quick" functions which computes and caches the
3193 real path for a given file name from the line table. */
3194
3195 static const char *
3196 dw2_get_real_path (struct objfile *objfile,
3197 struct quick_file_names *qfn, int index)
3198 {
3199 if (qfn->real_names == NULL)
3200 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
3201 qfn->num_file_names, const char *);
3202
3203 if (qfn->real_names[index] == NULL)
3204 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3205
3206 return qfn->real_names[index];
3207 }
3208
3209 static struct symtab *
3210 dw2_find_last_source_symtab (struct objfile *objfile)
3211 {
3212 struct dwarf2_per_objfile *dwarf2_per_objfile
3213 = get_dwarf2_per_objfile (objfile);
3214 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->all_comp_units.back ();
3215 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, false);
3216
3217 if (cust == NULL)
3218 return NULL;
3219
3220 return compunit_primary_filetab (cust);
3221 }
3222
3223 /* Traversal function for dw2_forget_cached_source_info. */
3224
3225 static int
3226 dw2_free_cached_file_names (void **slot, void *info)
3227 {
3228 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3229
3230 if (file_data->real_names)
3231 {
3232 int i;
3233
3234 for (i = 0; i < file_data->num_file_names; ++i)
3235 {
3236 xfree ((void*) file_data->real_names[i]);
3237 file_data->real_names[i] = NULL;
3238 }
3239 }
3240
3241 return 1;
3242 }
3243
3244 static void
3245 dw2_forget_cached_source_info (struct objfile *objfile)
3246 {
3247 struct dwarf2_per_objfile *dwarf2_per_objfile
3248 = get_dwarf2_per_objfile (objfile);
3249
3250 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table.get (),
3251 dw2_free_cached_file_names, NULL);
3252 }
3253
3254 /* Helper function for dw2_map_symtabs_matching_filename that expands
3255 the symtabs and calls the iterator. */
3256
3257 static int
3258 dw2_map_expand_apply (struct objfile *objfile,
3259 struct dwarf2_per_cu_data *per_cu,
3260 const char *name, const char *real_path,
3261 gdb::function_view<bool (symtab *)> callback)
3262 {
3263 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3264
3265 /* Don't visit already-expanded CUs. */
3266 if (per_cu->v.quick->compunit_symtab)
3267 return 0;
3268
3269 /* This may expand more than one symtab, and we want to iterate over
3270 all of them. */
3271 dw2_instantiate_symtab (per_cu, false);
3272
3273 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3274 last_made, callback);
3275 }
3276
3277 /* Implementation of the map_symtabs_matching_filename method. */
3278
3279 static bool
3280 dw2_map_symtabs_matching_filename
3281 (struct objfile *objfile, const char *name, const char *real_path,
3282 gdb::function_view<bool (symtab *)> callback)
3283 {
3284 const char *name_basename = lbasename (name);
3285 struct dwarf2_per_objfile *dwarf2_per_objfile
3286 = get_dwarf2_per_objfile (objfile);
3287
3288 /* The rule is CUs specify all the files, including those used by
3289 any TU, so there's no need to scan TUs here. */
3290
3291 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3292 {
3293 /* We only need to look at symtabs not already expanded. */
3294 if (per_cu->v.quick->compunit_symtab)
3295 continue;
3296
3297 quick_file_names *file_data = dw2_get_file_names (per_cu);
3298 if (file_data == NULL)
3299 continue;
3300
3301 for (int j = 0; j < file_data->num_file_names; ++j)
3302 {
3303 const char *this_name = file_data->file_names[j];
3304 const char *this_real_name;
3305
3306 if (compare_filenames_for_search (this_name, name))
3307 {
3308 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3309 callback))
3310 return true;
3311 continue;
3312 }
3313
3314 /* Before we invoke realpath, which can get expensive when many
3315 files are involved, do a quick comparison of the basenames. */
3316 if (! basenames_may_differ
3317 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3318 continue;
3319
3320 this_real_name = dw2_get_real_path (objfile, file_data, j);
3321 if (compare_filenames_for_search (this_real_name, name))
3322 {
3323 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3324 callback))
3325 return true;
3326 continue;
3327 }
3328
3329 if (real_path != NULL)
3330 {
3331 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3332 gdb_assert (IS_ABSOLUTE_PATH (name));
3333 if (this_real_name != NULL
3334 && FILENAME_CMP (real_path, this_real_name) == 0)
3335 {
3336 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3337 callback))
3338 return true;
3339 continue;
3340 }
3341 }
3342 }
3343 }
3344
3345 return false;
3346 }
3347
3348 /* Struct used to manage iterating over all CUs looking for a symbol. */
3349
3350 struct dw2_symtab_iterator
3351 {
3352 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3353 struct dwarf2_per_objfile *dwarf2_per_objfile;
3354 /* If set, only look for symbols that match that block. Valid values are
3355 GLOBAL_BLOCK and STATIC_BLOCK. */
3356 gdb::optional<block_enum> block_index;
3357 /* The kind of symbol we're looking for. */
3358 domain_enum domain;
3359 /* The list of CUs from the index entry of the symbol,
3360 or NULL if not found. */
3361 offset_type *vec;
3362 /* The next element in VEC to look at. */
3363 int next;
3364 /* The number of elements in VEC, or zero if there is no match. */
3365 int length;
3366 /* Have we seen a global version of the symbol?
3367 If so we can ignore all further global instances.
3368 This is to work around gold/15646, inefficient gold-generated
3369 indices. */
3370 int global_seen;
3371 };
3372
3373 /* Initialize the index symtab iterator ITER. */
3374
3375 static void
3376 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3377 struct dwarf2_per_objfile *dwarf2_per_objfile,
3378 gdb::optional<block_enum> block_index,
3379 domain_enum domain,
3380 const char *name)
3381 {
3382 iter->dwarf2_per_objfile = dwarf2_per_objfile;
3383 iter->block_index = block_index;
3384 iter->domain = domain;
3385 iter->next = 0;
3386 iter->global_seen = 0;
3387
3388 mapped_index *index = dwarf2_per_objfile->index_table.get ();
3389
3390 /* index is NULL if OBJF_READNOW. */
3391 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
3392 iter->length = MAYBE_SWAP (*iter->vec);
3393 else
3394 {
3395 iter->vec = NULL;
3396 iter->length = 0;
3397 }
3398 }
3399
3400 /* Return the next matching CU or NULL if there are no more. */
3401
3402 static struct dwarf2_per_cu_data *
3403 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3404 {
3405 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3406
3407 for ( ; iter->next < iter->length; ++iter->next)
3408 {
3409 offset_type cu_index_and_attrs =
3410 MAYBE_SWAP (iter->vec[iter->next + 1]);
3411 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3412 gdb_index_symbol_kind symbol_kind =
3413 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3414 /* Only check the symbol attributes if they're present.
3415 Indices prior to version 7 don't record them,
3416 and indices >= 7 may elide them for certain symbols
3417 (gold does this). */
3418 int attrs_valid =
3419 (dwarf2_per_objfile->index_table->version >= 7
3420 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3421
3422 /* Don't crash on bad data. */
3423 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
3424 + dwarf2_per_objfile->all_type_units.size ()))
3425 {
3426 complaint (_(".gdb_index entry has bad CU index"
3427 " [in module %s]"),
3428 objfile_name (dwarf2_per_objfile->objfile));
3429 continue;
3430 }
3431
3432 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
3433
3434 /* Skip if already read in. */
3435 if (per_cu->v.quick->compunit_symtab)
3436 continue;
3437
3438 /* Check static vs global. */
3439 if (attrs_valid)
3440 {
3441 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3442
3443 if (iter->block_index.has_value ())
3444 {
3445 bool want_static = *iter->block_index == STATIC_BLOCK;
3446
3447 if (is_static != want_static)
3448 continue;
3449 }
3450
3451 /* Work around gold/15646. */
3452 if (!is_static && iter->global_seen)
3453 continue;
3454 if (!is_static)
3455 iter->global_seen = 1;
3456 }
3457
3458 /* Only check the symbol's kind if it has one. */
3459 if (attrs_valid)
3460 {
3461 switch (iter->domain)
3462 {
3463 case VAR_DOMAIN:
3464 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3465 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3466 /* Some types are also in VAR_DOMAIN. */
3467 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3468 continue;
3469 break;
3470 case STRUCT_DOMAIN:
3471 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3472 continue;
3473 break;
3474 case LABEL_DOMAIN:
3475 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3476 continue;
3477 break;
3478 case MODULE_DOMAIN:
3479 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3480 continue;
3481 break;
3482 default:
3483 break;
3484 }
3485 }
3486
3487 ++iter->next;
3488 return per_cu;
3489 }
3490
3491 return NULL;
3492 }
3493
3494 static struct compunit_symtab *
3495 dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
3496 const char *name, domain_enum domain)
3497 {
3498 struct compunit_symtab *stab_best = NULL;
3499 struct dwarf2_per_objfile *dwarf2_per_objfile
3500 = get_dwarf2_per_objfile (objfile);
3501
3502 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3503
3504 struct dw2_symtab_iterator iter;
3505 struct dwarf2_per_cu_data *per_cu;
3506
3507 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
3508
3509 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3510 {
3511 struct symbol *sym, *with_opaque = NULL;
3512 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
3513 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
3514 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
3515
3516 sym = block_find_symbol (block, name, domain,
3517 block_find_non_opaque_type_preferred,
3518 &with_opaque);
3519
3520 /* Some caution must be observed with overloaded functions
3521 and methods, since the index will not contain any overload
3522 information (but NAME might contain it). */
3523
3524 if (sym != NULL
3525 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3526 return stab;
3527 if (with_opaque != NULL
3528 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3529 stab_best = stab;
3530
3531 /* Keep looking through other CUs. */
3532 }
3533
3534 return stab_best;
3535 }
3536
3537 static void
3538 dw2_print_stats (struct objfile *objfile)
3539 {
3540 struct dwarf2_per_objfile *dwarf2_per_objfile
3541 = get_dwarf2_per_objfile (objfile);
3542 int total = (dwarf2_per_objfile->all_comp_units.size ()
3543 + dwarf2_per_objfile->all_type_units.size ());
3544 int count = 0;
3545
3546 for (int i = 0; i < total; ++i)
3547 {
3548 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
3549
3550 if (!per_cu->v.quick->compunit_symtab)
3551 ++count;
3552 }
3553 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3554 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3555 }
3556
3557 /* This dumps minimal information about the index.
3558 It is called via "mt print objfiles".
3559 One use is to verify .gdb_index has been loaded by the
3560 gdb.dwarf2/gdb-index.exp testcase. */
3561
3562 static void
3563 dw2_dump (struct objfile *objfile)
3564 {
3565 struct dwarf2_per_objfile *dwarf2_per_objfile
3566 = get_dwarf2_per_objfile (objfile);
3567
3568 gdb_assert (dwarf2_per_objfile->using_index);
3569 printf_filtered (".gdb_index:");
3570 if (dwarf2_per_objfile->index_table != NULL)
3571 {
3572 printf_filtered (" version %d\n",
3573 dwarf2_per_objfile->index_table->version);
3574 }
3575 else
3576 printf_filtered (" faked for \"readnow\"\n");
3577 printf_filtered ("\n");
3578 }
3579
3580 static void
3581 dw2_expand_symtabs_for_function (struct objfile *objfile,
3582 const char *func_name)
3583 {
3584 struct dwarf2_per_objfile *dwarf2_per_objfile
3585 = get_dwarf2_per_objfile (objfile);
3586
3587 struct dw2_symtab_iterator iter;
3588 struct dwarf2_per_cu_data *per_cu;
3589
3590 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
3591
3592 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3593 dw2_instantiate_symtab (per_cu, false);
3594
3595 }
3596
3597 static void
3598 dw2_expand_all_symtabs (struct objfile *objfile)
3599 {
3600 struct dwarf2_per_objfile *dwarf2_per_objfile
3601 = get_dwarf2_per_objfile (objfile);
3602 int total_units = (dwarf2_per_objfile->all_comp_units.size ()
3603 + dwarf2_per_objfile->all_type_units.size ());
3604
3605 for (int i = 0; i < total_units; ++i)
3606 {
3607 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
3608
3609 /* We don't want to directly expand a partial CU, because if we
3610 read it with the wrong language, then assertion failures can
3611 be triggered later on. See PR symtab/23010. So, tell
3612 dw2_instantiate_symtab to skip partial CUs -- any important
3613 partial CU will be read via DW_TAG_imported_unit anyway. */
3614 dw2_instantiate_symtab (per_cu, true);
3615 }
3616 }
3617
3618 static void
3619 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3620 const char *fullname)
3621 {
3622 struct dwarf2_per_objfile *dwarf2_per_objfile
3623 = get_dwarf2_per_objfile (objfile);
3624
3625 /* We don't need to consider type units here.
3626 This is only called for examining code, e.g. expand_line_sal.
3627 There can be an order of magnitude (or more) more type units
3628 than comp units, and we avoid them if we can. */
3629
3630 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3631 {
3632 /* We only need to look at symtabs not already expanded. */
3633 if (per_cu->v.quick->compunit_symtab)
3634 continue;
3635
3636 quick_file_names *file_data = dw2_get_file_names (per_cu);
3637 if (file_data == NULL)
3638 continue;
3639
3640 for (int j = 0; j < file_data->num_file_names; ++j)
3641 {
3642 const char *this_fullname = file_data->file_names[j];
3643
3644 if (filename_cmp (this_fullname, fullname) == 0)
3645 {
3646 dw2_instantiate_symtab (per_cu, false);
3647 break;
3648 }
3649 }
3650 }
3651 }
3652
3653 static void
3654 dw2_map_matching_symbols
3655 (struct objfile *objfile,
3656 const lookup_name_info &name, domain_enum domain,
3657 int global,
3658 gdb::function_view<symbol_found_callback_ftype> callback,
3659 symbol_compare_ftype *ordered_compare)
3660 {
3661 /* Currently unimplemented; used for Ada. The function can be called if the
3662 current language is Ada for a non-Ada objfile using GNU index. As Ada
3663 does not look for non-Ada symbols this function should just return. */
3664 }
3665
3666 /* Starting from a search name, return the string that finds the upper
3667 bound of all strings that start with SEARCH_NAME in a sorted name
3668 list. Returns the empty string to indicate that the upper bound is
3669 the end of the list. */
3670
3671 static std::string
3672 make_sort_after_prefix_name (const char *search_name)
3673 {
3674 /* When looking to complete "func", we find the upper bound of all
3675 symbols that start with "func" by looking for where we'd insert
3676 the closest string that would follow "func" in lexicographical
3677 order. Usually, that's "func"-with-last-character-incremented,
3678 i.e. "fund". Mind non-ASCII characters, though. Usually those
3679 will be UTF-8 multi-byte sequences, but we can't be certain.
3680 Especially mind the 0xff character, which is a valid character in
3681 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3682 rule out compilers allowing it in identifiers. Note that
3683 conveniently, strcmp/strcasecmp are specified to compare
3684 characters interpreted as unsigned char. So what we do is treat
3685 the whole string as a base 256 number composed of a sequence of
3686 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3687 to 0, and carries 1 to the following more-significant position.
3688 If the very first character in SEARCH_NAME ends up incremented
3689 and carries/overflows, then the upper bound is the end of the
3690 list. The string after the empty string is also the empty
3691 string.
3692
3693 Some examples of this operation:
3694
3695 SEARCH_NAME => "+1" RESULT
3696
3697 "abc" => "abd"
3698 "ab\xff" => "ac"
3699 "\xff" "a" "\xff" => "\xff" "b"
3700 "\xff" => ""
3701 "\xff\xff" => ""
3702 "" => ""
3703
3704 Then, with these symbols for example:
3705
3706 func
3707 func1
3708 fund
3709
3710 completing "func" looks for symbols between "func" and
3711 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3712 which finds "func" and "func1", but not "fund".
3713
3714 And with:
3715
3716 funcÿ (Latin1 'ÿ' [0xff])
3717 funcÿ1
3718 fund
3719
3720 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3721 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3722
3723 And with:
3724
3725 ÿÿ (Latin1 'ÿ' [0xff])
3726 ÿÿ1
3727
3728 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3729 the end of the list.
3730 */
3731 std::string after = search_name;
3732 while (!after.empty () && (unsigned char) after.back () == 0xff)
3733 after.pop_back ();
3734 if (!after.empty ())
3735 after.back () = (unsigned char) after.back () + 1;
3736 return after;
3737 }
3738
3739 /* See declaration. */
3740
3741 std::pair<std::vector<name_component>::const_iterator,
3742 std::vector<name_component>::const_iterator>
3743 mapped_index_base::find_name_components_bounds
3744 (const lookup_name_info &lookup_name_without_params, language lang) const
3745 {
3746 auto *name_cmp
3747 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3748
3749 const char *lang_name
3750 = lookup_name_without_params.language_lookup_name (lang).c_str ();
3751
3752 /* Comparison function object for lower_bound that matches against a
3753 given symbol name. */
3754 auto lookup_compare_lower = [&] (const name_component &elem,
3755 const char *name)
3756 {
3757 const char *elem_qualified = this->symbol_name_at (elem.idx);
3758 const char *elem_name = elem_qualified + elem.name_offset;
3759 return name_cmp (elem_name, name) < 0;
3760 };
3761
3762 /* Comparison function object for upper_bound that matches against a
3763 given symbol name. */
3764 auto lookup_compare_upper = [&] (const char *name,
3765 const name_component &elem)
3766 {
3767 const char *elem_qualified = this->symbol_name_at (elem.idx);
3768 const char *elem_name = elem_qualified + elem.name_offset;
3769 return name_cmp (name, elem_name) < 0;
3770 };
3771
3772 auto begin = this->name_components.begin ();
3773 auto end = this->name_components.end ();
3774
3775 /* Find the lower bound. */
3776 auto lower = [&] ()
3777 {
3778 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3779 return begin;
3780 else
3781 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3782 } ();
3783
3784 /* Find the upper bound. */
3785 auto upper = [&] ()
3786 {
3787 if (lookup_name_without_params.completion_mode ())
3788 {
3789 /* In completion mode, we want UPPER to point past all
3790 symbols names that have the same prefix. I.e., with
3791 these symbols, and completing "func":
3792
3793 function << lower bound
3794 function1
3795 other_function << upper bound
3796
3797 We find the upper bound by looking for the insertion
3798 point of "func"-with-last-character-incremented,
3799 i.e. "fund". */
3800 std::string after = make_sort_after_prefix_name (lang_name);
3801 if (after.empty ())
3802 return end;
3803 return std::lower_bound (lower, end, after.c_str (),
3804 lookup_compare_lower);
3805 }
3806 else
3807 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3808 } ();
3809
3810 return {lower, upper};
3811 }
3812
3813 /* See declaration. */
3814
3815 void
3816 mapped_index_base::build_name_components ()
3817 {
3818 if (!this->name_components.empty ())
3819 return;
3820
3821 this->name_components_casing = case_sensitivity;
3822 auto *name_cmp
3823 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3824
3825 /* The code below only knows how to break apart components of C++
3826 symbol names (and other languages that use '::' as
3827 namespace/module separator) and Ada symbol names. */
3828 auto count = this->symbol_name_count ();
3829 for (offset_type idx = 0; idx < count; idx++)
3830 {
3831 if (this->symbol_name_slot_invalid (idx))
3832 continue;
3833
3834 const char *name = this->symbol_name_at (idx);
3835
3836 /* Add each name component to the name component table. */
3837 unsigned int previous_len = 0;
3838
3839 if (strstr (name, "::") != nullptr)
3840 {
3841 for (unsigned int current_len = cp_find_first_component (name);
3842 name[current_len] != '\0';
3843 current_len += cp_find_first_component (name + current_len))
3844 {
3845 gdb_assert (name[current_len] == ':');
3846 this->name_components.push_back ({previous_len, idx});
3847 /* Skip the '::'. */
3848 current_len += 2;
3849 previous_len = current_len;
3850 }
3851 }
3852 else
3853 {
3854 /* Handle the Ada encoded (aka mangled) form here. */
3855 for (const char *iter = strstr (name, "__");
3856 iter != nullptr;
3857 iter = strstr (iter, "__"))
3858 {
3859 this->name_components.push_back ({previous_len, idx});
3860 iter += 2;
3861 previous_len = iter - name;
3862 }
3863 }
3864
3865 this->name_components.push_back ({previous_len, idx});
3866 }
3867
3868 /* Sort name_components elements by name. */
3869 auto name_comp_compare = [&] (const name_component &left,
3870 const name_component &right)
3871 {
3872 const char *left_qualified = this->symbol_name_at (left.idx);
3873 const char *right_qualified = this->symbol_name_at (right.idx);
3874
3875 const char *left_name = left_qualified + left.name_offset;
3876 const char *right_name = right_qualified + right.name_offset;
3877
3878 return name_cmp (left_name, right_name) < 0;
3879 };
3880
3881 std::sort (this->name_components.begin (),
3882 this->name_components.end (),
3883 name_comp_compare);
3884 }
3885
3886 /* Helper for dw2_expand_symtabs_matching that works with a
3887 mapped_index_base instead of the containing objfile. This is split
3888 to a separate function in order to be able to unit test the
3889 name_components matching using a mock mapped_index_base. For each
3890 symbol name that matches, calls MATCH_CALLBACK, passing it the
3891 symbol's index in the mapped_index_base symbol table. */
3892
3893 static void
3894 dw2_expand_symtabs_matching_symbol
3895 (mapped_index_base &index,
3896 const lookup_name_info &lookup_name_in,
3897 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3898 enum search_domain kind,
3899 gdb::function_view<bool (offset_type)> match_callback)
3900 {
3901 lookup_name_info lookup_name_without_params
3902 = lookup_name_in.make_ignore_params ();
3903
3904 /* Build the symbol name component sorted vector, if we haven't
3905 yet. */
3906 index.build_name_components ();
3907
3908 /* The same symbol may appear more than once in the range though.
3909 E.g., if we're looking for symbols that complete "w", and we have
3910 a symbol named "w1::w2", we'll find the two name components for
3911 that same symbol in the range. To be sure we only call the
3912 callback once per symbol, we first collect the symbol name
3913 indexes that matched in a temporary vector and ignore
3914 duplicates. */
3915 std::vector<offset_type> matches;
3916
3917 struct name_and_matcher
3918 {
3919 symbol_name_matcher_ftype *matcher;
3920 const std::string &name;
3921
3922 bool operator== (const name_and_matcher &other) const
3923 {
3924 return matcher == other.matcher && name == other.name;
3925 }
3926 };
3927
3928 /* A vector holding all the different symbol name matchers, for all
3929 languages. */
3930 std::vector<name_and_matcher> matchers;
3931
3932 for (int i = 0; i < nr_languages; i++)
3933 {
3934 enum language lang_e = (enum language) i;
3935
3936 const language_defn *lang = language_def (lang_e);
3937 symbol_name_matcher_ftype *name_matcher
3938 = get_symbol_name_matcher (lang, lookup_name_without_params);
3939
3940 name_and_matcher key {
3941 name_matcher,
3942 lookup_name_without_params.language_lookup_name (lang_e)
3943 };
3944
3945 /* Don't insert the same comparison routine more than once.
3946 Note that we do this linear walk. This is not a problem in
3947 practice because the number of supported languages is
3948 low. */
3949 if (std::find (matchers.begin (), matchers.end (), key)
3950 != matchers.end ())
3951 continue;
3952 matchers.push_back (std::move (key));
3953
3954 auto bounds
3955 = index.find_name_components_bounds (lookup_name_without_params,
3956 lang_e);
3957
3958 /* Now for each symbol name in range, check to see if we have a name
3959 match, and if so, call the MATCH_CALLBACK callback. */
3960
3961 for (; bounds.first != bounds.second; ++bounds.first)
3962 {
3963 const char *qualified = index.symbol_name_at (bounds.first->idx);
3964
3965 if (!name_matcher (qualified, lookup_name_without_params, NULL)
3966 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
3967 continue;
3968
3969 matches.push_back (bounds.first->idx);
3970 }
3971 }
3972
3973 std::sort (matches.begin (), matches.end ());
3974
3975 /* Finally call the callback, once per match. */
3976 ULONGEST prev = -1;
3977 for (offset_type idx : matches)
3978 {
3979 if (prev != idx)
3980 {
3981 if (!match_callback (idx))
3982 break;
3983 prev = idx;
3984 }
3985 }
3986
3987 /* Above we use a type wider than idx's for 'prev', since 0 and
3988 (offset_type)-1 are both possible values. */
3989 static_assert (sizeof (prev) > sizeof (offset_type), "");
3990 }
3991
3992 #if GDB_SELF_TEST
3993
3994 namespace selftests { namespace dw2_expand_symtabs_matching {
3995
3996 /* A mock .gdb_index/.debug_names-like name index table, enough to
3997 exercise dw2_expand_symtabs_matching_symbol, which works with the
3998 mapped_index_base interface. Builds an index from the symbol list
3999 passed as parameter to the constructor. */
4000 class mock_mapped_index : public mapped_index_base
4001 {
4002 public:
4003 mock_mapped_index (gdb::array_view<const char *> symbols)
4004 : m_symbol_table (symbols)
4005 {}
4006
4007 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
4008
4009 /* Return the number of names in the symbol table. */
4010 size_t symbol_name_count () const override
4011 {
4012 return m_symbol_table.size ();
4013 }
4014
4015 /* Get the name of the symbol at IDX in the symbol table. */
4016 const char *symbol_name_at (offset_type idx) const override
4017 {
4018 return m_symbol_table[idx];
4019 }
4020
4021 private:
4022 gdb::array_view<const char *> m_symbol_table;
4023 };
4024
4025 /* Convenience function that converts a NULL pointer to a "<null>"
4026 string, to pass to print routines. */
4027
4028 static const char *
4029 string_or_null (const char *str)
4030 {
4031 return str != NULL ? str : "<null>";
4032 }
4033
4034 /* Check if a lookup_name_info built from
4035 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4036 index. EXPECTED_LIST is the list of expected matches, in expected
4037 matching order. If no match expected, then an empty list is
4038 specified. Returns true on success. On failure prints a warning
4039 indicating the file:line that failed, and returns false. */
4040
4041 static bool
4042 check_match (const char *file, int line,
4043 mock_mapped_index &mock_index,
4044 const char *name, symbol_name_match_type match_type,
4045 bool completion_mode,
4046 std::initializer_list<const char *> expected_list)
4047 {
4048 lookup_name_info lookup_name (name, match_type, completion_mode);
4049
4050 bool matched = true;
4051
4052 auto mismatch = [&] (const char *expected_str,
4053 const char *got)
4054 {
4055 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4056 "expected=\"%s\", got=\"%s\"\n"),
4057 file, line,
4058 (match_type == symbol_name_match_type::FULL
4059 ? "FULL" : "WILD"),
4060 name, string_or_null (expected_str), string_or_null (got));
4061 matched = false;
4062 };
4063
4064 auto expected_it = expected_list.begin ();
4065 auto expected_end = expected_list.end ();
4066
4067 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4068 NULL, ALL_DOMAIN,
4069 [&] (offset_type idx)
4070 {
4071 const char *matched_name = mock_index.symbol_name_at (idx);
4072 const char *expected_str
4073 = expected_it == expected_end ? NULL : *expected_it++;
4074
4075 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4076 mismatch (expected_str, matched_name);
4077 return true;
4078 });
4079
4080 const char *expected_str
4081 = expected_it == expected_end ? NULL : *expected_it++;
4082 if (expected_str != NULL)
4083 mismatch (expected_str, NULL);
4084
4085 return matched;
4086 }
4087
4088 /* The symbols added to the mock mapped_index for testing (in
4089 canonical form). */
4090 static const char *test_symbols[] = {
4091 "function",
4092 "std::bar",
4093 "std::zfunction",
4094 "std::zfunction2",
4095 "w1::w2",
4096 "ns::foo<char*>",
4097 "ns::foo<int>",
4098 "ns::foo<long>",
4099 "ns2::tmpl<int>::foo2",
4100 "(anonymous namespace)::A::B::C",
4101
4102 /* These are used to check that the increment-last-char in the
4103 matching algorithm for completion doesn't match "t1_fund" when
4104 completing "t1_func". */
4105 "t1_func",
4106 "t1_func1",
4107 "t1_fund",
4108 "t1_fund1",
4109
4110 /* A UTF-8 name with multi-byte sequences to make sure that
4111 cp-name-parser understands this as a single identifier ("função"
4112 is "function" in PT). */
4113 u8"u8função",
4114
4115 /* \377 (0xff) is Latin1 'ÿ'. */
4116 "yfunc\377",
4117
4118 /* \377 (0xff) is Latin1 'ÿ'. */
4119 "\377",
4120 "\377\377123",
4121
4122 /* A name with all sorts of complications. Starts with "z" to make
4123 it easier for the completion tests below. */
4124 #define Z_SYM_NAME \
4125 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4126 "::tuple<(anonymous namespace)::ui*, " \
4127 "std::default_delete<(anonymous namespace)::ui>, void>"
4128
4129 Z_SYM_NAME
4130 };
4131
4132 /* Returns true if the mapped_index_base::find_name_component_bounds
4133 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4134 in completion mode. */
4135
4136 static bool
4137 check_find_bounds_finds (mapped_index_base &index,
4138 const char *search_name,
4139 gdb::array_view<const char *> expected_syms)
4140 {
4141 lookup_name_info lookup_name (search_name,
4142 symbol_name_match_type::FULL, true);
4143
4144 auto bounds = index.find_name_components_bounds (lookup_name,
4145 language_cplus);
4146
4147 size_t distance = std::distance (bounds.first, bounds.second);
4148 if (distance != expected_syms.size ())
4149 return false;
4150
4151 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4152 {
4153 auto nc_elem = bounds.first + exp_elem;
4154 const char *qualified = index.symbol_name_at (nc_elem->idx);
4155 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4156 return false;
4157 }
4158
4159 return true;
4160 }
4161
4162 /* Test the lower-level mapped_index::find_name_component_bounds
4163 method. */
4164
4165 static void
4166 test_mapped_index_find_name_component_bounds ()
4167 {
4168 mock_mapped_index mock_index (test_symbols);
4169
4170 mock_index.build_name_components ();
4171
4172 /* Test the lower-level mapped_index::find_name_component_bounds
4173 method in completion mode. */
4174 {
4175 static const char *expected_syms[] = {
4176 "t1_func",
4177 "t1_func1",
4178 };
4179
4180 SELF_CHECK (check_find_bounds_finds (mock_index,
4181 "t1_func", expected_syms));
4182 }
4183
4184 /* Check that the increment-last-char in the name matching algorithm
4185 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4186 {
4187 static const char *expected_syms1[] = {
4188 "\377",
4189 "\377\377123",
4190 };
4191 SELF_CHECK (check_find_bounds_finds (mock_index,
4192 "\377", expected_syms1));
4193
4194 static const char *expected_syms2[] = {
4195 "\377\377123",
4196 };
4197 SELF_CHECK (check_find_bounds_finds (mock_index,
4198 "\377\377", expected_syms2));
4199 }
4200 }
4201
4202 /* Test dw2_expand_symtabs_matching_symbol. */
4203
4204 static void
4205 test_dw2_expand_symtabs_matching_symbol ()
4206 {
4207 mock_mapped_index mock_index (test_symbols);
4208
4209 /* We let all tests run until the end even if some fails, for debug
4210 convenience. */
4211 bool any_mismatch = false;
4212
4213 /* Create the expected symbols list (an initializer_list). Needed
4214 because lists have commas, and we need to pass them to CHECK,
4215 which is a macro. */
4216 #define EXPECT(...) { __VA_ARGS__ }
4217
4218 /* Wrapper for check_match that passes down the current
4219 __FILE__/__LINE__. */
4220 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4221 any_mismatch |= !check_match (__FILE__, __LINE__, \
4222 mock_index, \
4223 NAME, MATCH_TYPE, COMPLETION_MODE, \
4224 EXPECTED_LIST)
4225
4226 /* Identity checks. */
4227 for (const char *sym : test_symbols)
4228 {
4229 /* Should be able to match all existing symbols. */
4230 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4231 EXPECT (sym));
4232
4233 /* Should be able to match all existing symbols with
4234 parameters. */
4235 std::string with_params = std::string (sym) + "(int)";
4236 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4237 EXPECT (sym));
4238
4239 /* Should be able to match all existing symbols with
4240 parameters and qualifiers. */
4241 with_params = std::string (sym) + " ( int ) const";
4242 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4243 EXPECT (sym));
4244
4245 /* This should really find sym, but cp-name-parser.y doesn't
4246 know about lvalue/rvalue qualifiers yet. */
4247 with_params = std::string (sym) + " ( int ) &&";
4248 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4249 {});
4250 }
4251
4252 /* Check that the name matching algorithm for completion doesn't get
4253 confused with Latin1 'ÿ' / 0xff. */
4254 {
4255 static const char str[] = "\377";
4256 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4257 EXPECT ("\377", "\377\377123"));
4258 }
4259
4260 /* Check that the increment-last-char in the matching algorithm for
4261 completion doesn't match "t1_fund" when completing "t1_func". */
4262 {
4263 static const char str[] = "t1_func";
4264 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4265 EXPECT ("t1_func", "t1_func1"));
4266 }
4267
4268 /* Check that completion mode works at each prefix of the expected
4269 symbol name. */
4270 {
4271 static const char str[] = "function(int)";
4272 size_t len = strlen (str);
4273 std::string lookup;
4274
4275 for (size_t i = 1; i < len; i++)
4276 {
4277 lookup.assign (str, i);
4278 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4279 EXPECT ("function"));
4280 }
4281 }
4282
4283 /* While "w" is a prefix of both components, the match function
4284 should still only be called once. */
4285 {
4286 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4287 EXPECT ("w1::w2"));
4288 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4289 EXPECT ("w1::w2"));
4290 }
4291
4292 /* Same, with a "complicated" symbol. */
4293 {
4294 static const char str[] = Z_SYM_NAME;
4295 size_t len = strlen (str);
4296 std::string lookup;
4297
4298 for (size_t i = 1; i < len; i++)
4299 {
4300 lookup.assign (str, i);
4301 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4302 EXPECT (Z_SYM_NAME));
4303 }
4304 }
4305
4306 /* In FULL mode, an incomplete symbol doesn't match. */
4307 {
4308 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4309 {});
4310 }
4311
4312 /* A complete symbol with parameters matches any overload, since the
4313 index has no overload info. */
4314 {
4315 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4316 EXPECT ("std::zfunction", "std::zfunction2"));
4317 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4318 EXPECT ("std::zfunction", "std::zfunction2"));
4319 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4320 EXPECT ("std::zfunction", "std::zfunction2"));
4321 }
4322
4323 /* Check that whitespace is ignored appropriately. A symbol with a
4324 template argument list. */
4325 {
4326 static const char expected[] = "ns::foo<int>";
4327 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4328 EXPECT (expected));
4329 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4330 EXPECT (expected));
4331 }
4332
4333 /* Check that whitespace is ignored appropriately. A symbol with a
4334 template argument list that includes a pointer. */
4335 {
4336 static const char expected[] = "ns::foo<char*>";
4337 /* Try both completion and non-completion modes. */
4338 static const bool completion_mode[2] = {false, true};
4339 for (size_t i = 0; i < 2; i++)
4340 {
4341 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4342 completion_mode[i], EXPECT (expected));
4343 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4344 completion_mode[i], EXPECT (expected));
4345
4346 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4347 completion_mode[i], EXPECT (expected));
4348 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4349 completion_mode[i], EXPECT (expected));
4350 }
4351 }
4352
4353 {
4354 /* Check method qualifiers are ignored. */
4355 static const char expected[] = "ns::foo<char*>";
4356 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4357 symbol_name_match_type::FULL, true, EXPECT (expected));
4358 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4359 symbol_name_match_type::FULL, true, EXPECT (expected));
4360 CHECK_MATCH ("foo < char * > ( int ) const",
4361 symbol_name_match_type::WILD, true, EXPECT (expected));
4362 CHECK_MATCH ("foo < char * > ( int ) &&",
4363 symbol_name_match_type::WILD, true, EXPECT (expected));
4364 }
4365
4366 /* Test lookup names that don't match anything. */
4367 {
4368 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4369 {});
4370
4371 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4372 {});
4373 }
4374
4375 /* Some wild matching tests, exercising "(anonymous namespace)",
4376 which should not be confused with a parameter list. */
4377 {
4378 static const char *syms[] = {
4379 "A::B::C",
4380 "B::C",
4381 "C",
4382 "A :: B :: C ( int )",
4383 "B :: C ( int )",
4384 "C ( int )",
4385 };
4386
4387 for (const char *s : syms)
4388 {
4389 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4390 EXPECT ("(anonymous namespace)::A::B::C"));
4391 }
4392 }
4393
4394 {
4395 static const char expected[] = "ns2::tmpl<int>::foo2";
4396 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4397 EXPECT (expected));
4398 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4399 EXPECT (expected));
4400 }
4401
4402 SELF_CHECK (!any_mismatch);
4403
4404 #undef EXPECT
4405 #undef CHECK_MATCH
4406 }
4407
4408 static void
4409 run_test ()
4410 {
4411 test_mapped_index_find_name_component_bounds ();
4412 test_dw2_expand_symtabs_matching_symbol ();
4413 }
4414
4415 }} // namespace selftests::dw2_expand_symtabs_matching
4416
4417 #endif /* GDB_SELF_TEST */
4418
4419 /* If FILE_MATCHER is NULL or if PER_CU has
4420 dwarf2_per_cu_quick_data::MARK set (see
4421 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4422 EXPANSION_NOTIFY on it. */
4423
4424 static void
4425 dw2_expand_symtabs_matching_one
4426 (struct dwarf2_per_cu_data *per_cu,
4427 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4428 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4429 {
4430 if (file_matcher == NULL || per_cu->v.quick->mark)
4431 {
4432 bool symtab_was_null
4433 = (per_cu->v.quick->compunit_symtab == NULL);
4434
4435 dw2_instantiate_symtab (per_cu, false);
4436
4437 if (expansion_notify != NULL
4438 && symtab_was_null
4439 && per_cu->v.quick->compunit_symtab != NULL)
4440 expansion_notify (per_cu->v.quick->compunit_symtab);
4441 }
4442 }
4443
4444 /* Helper for dw2_expand_matching symtabs. Called on each symbol
4445 matched, to expand corresponding CUs that were marked. IDX is the
4446 index of the symbol name that matched. */
4447
4448 static void
4449 dw2_expand_marked_cus
4450 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
4451 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4452 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4453 search_domain kind)
4454 {
4455 offset_type *vec, vec_len, vec_idx;
4456 bool global_seen = false;
4457 mapped_index &index = *dwarf2_per_objfile->index_table;
4458
4459 vec = (offset_type *) (index.constant_pool
4460 + MAYBE_SWAP (index.symbol_table[idx].vec));
4461 vec_len = MAYBE_SWAP (vec[0]);
4462 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4463 {
4464 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4465 /* This value is only valid for index versions >= 7. */
4466 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4467 gdb_index_symbol_kind symbol_kind =
4468 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4469 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4470 /* Only check the symbol attributes if they're present.
4471 Indices prior to version 7 don't record them,
4472 and indices >= 7 may elide them for certain symbols
4473 (gold does this). */
4474 int attrs_valid =
4475 (index.version >= 7
4476 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4477
4478 /* Work around gold/15646. */
4479 if (attrs_valid)
4480 {
4481 if (!is_static && global_seen)
4482 continue;
4483 if (!is_static)
4484 global_seen = true;
4485 }
4486
4487 /* Only check the symbol's kind if it has one. */
4488 if (attrs_valid)
4489 {
4490 switch (kind)
4491 {
4492 case VARIABLES_DOMAIN:
4493 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4494 continue;
4495 break;
4496 case FUNCTIONS_DOMAIN:
4497 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4498 continue;
4499 break;
4500 case TYPES_DOMAIN:
4501 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4502 continue;
4503 break;
4504 case MODULES_DOMAIN:
4505 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4506 continue;
4507 break;
4508 default:
4509 break;
4510 }
4511 }
4512
4513 /* Don't crash on bad data. */
4514 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
4515 + dwarf2_per_objfile->all_type_units.size ()))
4516 {
4517 complaint (_(".gdb_index entry has bad CU index"
4518 " [in module %s]"),
4519 objfile_name (dwarf2_per_objfile->objfile));
4520 continue;
4521 }
4522
4523 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
4524 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
4525 expansion_notify);
4526 }
4527 }
4528
4529 /* If FILE_MATCHER is non-NULL, set all the
4530 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4531 that match FILE_MATCHER. */
4532
4533 static void
4534 dw_expand_symtabs_matching_file_matcher
4535 (struct dwarf2_per_objfile *dwarf2_per_objfile,
4536 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
4537 {
4538 if (file_matcher == NULL)
4539 return;
4540
4541 objfile *const objfile = dwarf2_per_objfile->objfile;
4542
4543 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4544 htab_eq_pointer,
4545 NULL, xcalloc, xfree));
4546 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4547 htab_eq_pointer,
4548 NULL, xcalloc, xfree));
4549
4550 /* The rule is CUs specify all the files, including those used by
4551 any TU, so there's no need to scan TUs here. */
4552
4553 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4554 {
4555 QUIT;
4556
4557 per_cu->v.quick->mark = 0;
4558
4559 /* We only need to look at symtabs not already expanded. */
4560 if (per_cu->v.quick->compunit_symtab)
4561 continue;
4562
4563 quick_file_names *file_data = dw2_get_file_names (per_cu);
4564 if (file_data == NULL)
4565 continue;
4566
4567 if (htab_find (visited_not_found.get (), file_data) != NULL)
4568 continue;
4569 else if (htab_find (visited_found.get (), file_data) != NULL)
4570 {
4571 per_cu->v.quick->mark = 1;
4572 continue;
4573 }
4574
4575 for (int j = 0; j < file_data->num_file_names; ++j)
4576 {
4577 const char *this_real_name;
4578
4579 if (file_matcher (file_data->file_names[j], false))
4580 {
4581 per_cu->v.quick->mark = 1;
4582 break;
4583 }
4584
4585 /* Before we invoke realpath, which can get expensive when many
4586 files are involved, do a quick comparison of the basenames. */
4587 if (!basenames_may_differ
4588 && !file_matcher (lbasename (file_data->file_names[j]),
4589 true))
4590 continue;
4591
4592 this_real_name = dw2_get_real_path (objfile, file_data, j);
4593 if (file_matcher (this_real_name, false))
4594 {
4595 per_cu->v.quick->mark = 1;
4596 break;
4597 }
4598 }
4599
4600 void **slot = htab_find_slot (per_cu->v.quick->mark
4601 ? visited_found.get ()
4602 : visited_not_found.get (),
4603 file_data, INSERT);
4604 *slot = file_data;
4605 }
4606 }
4607
4608 static void
4609 dw2_expand_symtabs_matching
4610 (struct objfile *objfile,
4611 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4612 const lookup_name_info &lookup_name,
4613 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4614 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4615 enum search_domain kind)
4616 {
4617 struct dwarf2_per_objfile *dwarf2_per_objfile
4618 = get_dwarf2_per_objfile (objfile);
4619
4620 /* index_table is NULL if OBJF_READNOW. */
4621 if (!dwarf2_per_objfile->index_table)
4622 return;
4623
4624 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
4625
4626 mapped_index &index = *dwarf2_per_objfile->index_table;
4627
4628 dw2_expand_symtabs_matching_symbol (index, lookup_name,
4629 symbol_matcher,
4630 kind, [&] (offset_type idx)
4631 {
4632 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
4633 expansion_notify, kind);
4634 return true;
4635 });
4636 }
4637
4638 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4639 symtab. */
4640
4641 static struct compunit_symtab *
4642 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4643 CORE_ADDR pc)
4644 {
4645 int i;
4646
4647 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4648 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4649 return cust;
4650
4651 if (cust->includes == NULL)
4652 return NULL;
4653
4654 for (i = 0; cust->includes[i]; ++i)
4655 {
4656 struct compunit_symtab *s = cust->includes[i];
4657
4658 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4659 if (s != NULL)
4660 return s;
4661 }
4662
4663 return NULL;
4664 }
4665
4666 static struct compunit_symtab *
4667 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4668 struct bound_minimal_symbol msymbol,
4669 CORE_ADDR pc,
4670 struct obj_section *section,
4671 int warn_if_readin)
4672 {
4673 struct dwarf2_per_cu_data *data;
4674 struct compunit_symtab *result;
4675
4676 if (!objfile->partial_symtabs->psymtabs_addrmap)
4677 return NULL;
4678
4679 CORE_ADDR baseaddr = objfile->text_section_offset ();
4680 data = (struct dwarf2_per_cu_data *) addrmap_find
4681 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
4682 if (!data)
4683 return NULL;
4684
4685 if (warn_if_readin && data->v.quick->compunit_symtab)
4686 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4687 paddress (get_objfile_arch (objfile), pc));
4688
4689 result
4690 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data,
4691 false),
4692 pc);
4693 gdb_assert (result != NULL);
4694 return result;
4695 }
4696
4697 static void
4698 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4699 void *data, int need_fullname)
4700 {
4701 struct dwarf2_per_objfile *dwarf2_per_objfile
4702 = get_dwarf2_per_objfile (objfile);
4703
4704 if (!dwarf2_per_objfile->filenames_cache)
4705 {
4706 dwarf2_per_objfile->filenames_cache.emplace ();
4707
4708 htab_up visited (htab_create_alloc (10,
4709 htab_hash_pointer, htab_eq_pointer,
4710 NULL, xcalloc, xfree));
4711
4712 /* The rule is CUs specify all the files, including those used
4713 by any TU, so there's no need to scan TUs here. We can
4714 ignore file names coming from already-expanded CUs. */
4715
4716 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4717 {
4718 if (per_cu->v.quick->compunit_symtab)
4719 {
4720 void **slot = htab_find_slot (visited.get (),
4721 per_cu->v.quick->file_names,
4722 INSERT);
4723
4724 *slot = per_cu->v.quick->file_names;
4725 }
4726 }
4727
4728 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4729 {
4730 /* We only need to look at symtabs not already expanded. */
4731 if (per_cu->v.quick->compunit_symtab)
4732 continue;
4733
4734 quick_file_names *file_data = dw2_get_file_names (per_cu);
4735 if (file_data == NULL)
4736 continue;
4737
4738 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
4739 if (*slot)
4740 {
4741 /* Already visited. */
4742 continue;
4743 }
4744 *slot = file_data;
4745
4746 for (int j = 0; j < file_data->num_file_names; ++j)
4747 {
4748 const char *filename = file_data->file_names[j];
4749 dwarf2_per_objfile->filenames_cache->seen (filename);
4750 }
4751 }
4752 }
4753
4754 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
4755 {
4756 gdb::unique_xmalloc_ptr<char> this_real_name;
4757
4758 if (need_fullname)
4759 this_real_name = gdb_realpath (filename);
4760 (*fun) (filename, this_real_name.get (), data);
4761 });
4762 }
4763
4764 static int
4765 dw2_has_symbols (struct objfile *objfile)
4766 {
4767 return 1;
4768 }
4769
4770 const struct quick_symbol_functions dwarf2_gdb_index_functions =
4771 {
4772 dw2_has_symbols,
4773 dw2_find_last_source_symtab,
4774 dw2_forget_cached_source_info,
4775 dw2_map_symtabs_matching_filename,
4776 dw2_lookup_symbol,
4777 dw2_print_stats,
4778 dw2_dump,
4779 dw2_expand_symtabs_for_function,
4780 dw2_expand_all_symtabs,
4781 dw2_expand_symtabs_with_fullname,
4782 dw2_map_matching_symbols,
4783 dw2_expand_symtabs_matching,
4784 dw2_find_pc_sect_compunit_symtab,
4785 NULL,
4786 dw2_map_symbol_filenames
4787 };
4788
4789 /* DWARF-5 debug_names reader. */
4790
4791 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4792 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4793
4794 /* A helper function that reads the .debug_names section in SECTION
4795 and fills in MAP. FILENAME is the name of the file containing the
4796 section; it is used for error reporting.
4797
4798 Returns true if all went well, false otherwise. */
4799
4800 static bool
4801 read_debug_names_from_section (struct objfile *objfile,
4802 const char *filename,
4803 struct dwarf2_section_info *section,
4804 mapped_debug_names &map)
4805 {
4806 if (section->empty ())
4807 return false;
4808
4809 /* Older elfutils strip versions could keep the section in the main
4810 executable while splitting it for the separate debug info file. */
4811 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4812 return false;
4813
4814 section->read (objfile);
4815
4816 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
4817
4818 const gdb_byte *addr = section->buffer;
4819
4820 bfd *const abfd = section->get_bfd_owner ();
4821
4822 unsigned int bytes_read;
4823 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4824 addr += bytes_read;
4825
4826 map.dwarf5_is_dwarf64 = bytes_read != 4;
4827 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4828 if (bytes_read + length != section->size)
4829 {
4830 /* There may be multiple per-CU indices. */
4831 warning (_("Section .debug_names in %s length %s does not match "
4832 "section length %s, ignoring .debug_names."),
4833 filename, plongest (bytes_read + length),
4834 pulongest (section->size));
4835 return false;
4836 }
4837
4838 /* The version number. */
4839 uint16_t version = read_2_bytes (abfd, addr);
4840 addr += 2;
4841 if (version != 5)
4842 {
4843 warning (_("Section .debug_names in %s has unsupported version %d, "
4844 "ignoring .debug_names."),
4845 filename, version);
4846 return false;
4847 }
4848
4849 /* Padding. */
4850 uint16_t padding = read_2_bytes (abfd, addr);
4851 addr += 2;
4852 if (padding != 0)
4853 {
4854 warning (_("Section .debug_names in %s has unsupported padding %d, "
4855 "ignoring .debug_names."),
4856 filename, padding);
4857 return false;
4858 }
4859
4860 /* comp_unit_count - The number of CUs in the CU list. */
4861 map.cu_count = read_4_bytes (abfd, addr);
4862 addr += 4;
4863
4864 /* local_type_unit_count - The number of TUs in the local TU
4865 list. */
4866 map.tu_count = read_4_bytes (abfd, addr);
4867 addr += 4;
4868
4869 /* foreign_type_unit_count - The number of TUs in the foreign TU
4870 list. */
4871 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
4872 addr += 4;
4873 if (foreign_tu_count != 0)
4874 {
4875 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
4876 "ignoring .debug_names."),
4877 filename, static_cast<unsigned long> (foreign_tu_count));
4878 return false;
4879 }
4880
4881 /* bucket_count - The number of hash buckets in the hash lookup
4882 table. */
4883 map.bucket_count = read_4_bytes (abfd, addr);
4884 addr += 4;
4885
4886 /* name_count - The number of unique names in the index. */
4887 map.name_count = read_4_bytes (abfd, addr);
4888 addr += 4;
4889
4890 /* abbrev_table_size - The size in bytes of the abbreviations
4891 table. */
4892 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
4893 addr += 4;
4894
4895 /* augmentation_string_size - The size in bytes of the augmentation
4896 string. This value is rounded up to a multiple of 4. */
4897 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
4898 addr += 4;
4899 map.augmentation_is_gdb = ((augmentation_string_size
4900 == sizeof (dwarf5_augmentation))
4901 && memcmp (addr, dwarf5_augmentation,
4902 sizeof (dwarf5_augmentation)) == 0);
4903 augmentation_string_size += (-augmentation_string_size) & 3;
4904 addr += augmentation_string_size;
4905
4906 /* List of CUs */
4907 map.cu_table_reordered = addr;
4908 addr += map.cu_count * map.offset_size;
4909
4910 /* List of Local TUs */
4911 map.tu_table_reordered = addr;
4912 addr += map.tu_count * map.offset_size;
4913
4914 /* Hash Lookup Table */
4915 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4916 addr += map.bucket_count * 4;
4917 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4918 addr += map.name_count * 4;
4919
4920 /* Name Table */
4921 map.name_table_string_offs_reordered = addr;
4922 addr += map.name_count * map.offset_size;
4923 map.name_table_entry_offs_reordered = addr;
4924 addr += map.name_count * map.offset_size;
4925
4926 const gdb_byte *abbrev_table_start = addr;
4927 for (;;)
4928 {
4929 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
4930 addr += bytes_read;
4931 if (index_num == 0)
4932 break;
4933
4934 const auto insertpair
4935 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
4936 if (!insertpair.second)
4937 {
4938 warning (_("Section .debug_names in %s has duplicate index %s, "
4939 "ignoring .debug_names."),
4940 filename, pulongest (index_num));
4941 return false;
4942 }
4943 mapped_debug_names::index_val &indexval = insertpair.first->second;
4944 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
4945 addr += bytes_read;
4946
4947 for (;;)
4948 {
4949 mapped_debug_names::index_val::attr attr;
4950 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
4951 addr += bytes_read;
4952 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
4953 addr += bytes_read;
4954 if (attr.form == DW_FORM_implicit_const)
4955 {
4956 attr.implicit_const = read_signed_leb128 (abfd, addr,
4957 &bytes_read);
4958 addr += bytes_read;
4959 }
4960 if (attr.dw_idx == 0 && attr.form == 0)
4961 break;
4962 indexval.attr_vec.push_back (std::move (attr));
4963 }
4964 }
4965 if (addr != abbrev_table_start + abbrev_table_size)
4966 {
4967 warning (_("Section .debug_names in %s has abbreviation_table "
4968 "of size %s vs. written as %u, ignoring .debug_names."),
4969 filename, plongest (addr - abbrev_table_start),
4970 abbrev_table_size);
4971 return false;
4972 }
4973 map.entry_pool = addr;
4974
4975 return true;
4976 }
4977
4978 /* A helper for create_cus_from_debug_names that handles the MAP's CU
4979 list. */
4980
4981 static void
4982 create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
4983 const mapped_debug_names &map,
4984 dwarf2_section_info &section,
4985 bool is_dwz)
4986 {
4987 sect_offset sect_off_prev;
4988 for (uint32_t i = 0; i <= map.cu_count; ++i)
4989 {
4990 sect_offset sect_off_next;
4991 if (i < map.cu_count)
4992 {
4993 sect_off_next
4994 = (sect_offset) (extract_unsigned_integer
4995 (map.cu_table_reordered + i * map.offset_size,
4996 map.offset_size,
4997 map.dwarf5_byte_order));
4998 }
4999 else
5000 sect_off_next = (sect_offset) section.size;
5001 if (i >= 1)
5002 {
5003 const ULONGEST length = sect_off_next - sect_off_prev;
5004 dwarf2_per_cu_data *per_cu
5005 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
5006 sect_off_prev, length);
5007 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
5008 }
5009 sect_off_prev = sect_off_next;
5010 }
5011 }
5012
5013 /* Read the CU list from the mapped index, and use it to create all
5014 the CU objects for this dwarf2_per_objfile. */
5015
5016 static void
5017 create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
5018 const mapped_debug_names &map,
5019 const mapped_debug_names &dwz_map)
5020 {
5021 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
5022 dwarf2_per_objfile->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
5023
5024 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5025 dwarf2_per_objfile->info,
5026 false /* is_dwz */);
5027
5028 if (dwz_map.cu_count == 0)
5029 return;
5030
5031 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5032 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
5033 true /* is_dwz */);
5034 }
5035
5036 /* Read .debug_names. If everything went ok, initialize the "quick"
5037 elements of all the CUs and return true. Otherwise, return false. */
5038
5039 static bool
5040 dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
5041 {
5042 std::unique_ptr<mapped_debug_names> map
5043 (new mapped_debug_names (dwarf2_per_objfile));
5044 mapped_debug_names dwz_map (dwarf2_per_objfile);
5045 struct objfile *objfile = dwarf2_per_objfile->objfile;
5046
5047 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5048 &dwarf2_per_objfile->debug_names,
5049 *map))
5050 return false;
5051
5052 /* Don't use the index if it's empty. */
5053 if (map->name_count == 0)
5054 return false;
5055
5056 /* If there is a .dwz file, read it so we can get its CU list as
5057 well. */
5058 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5059 if (dwz != NULL)
5060 {
5061 if (!read_debug_names_from_section (objfile,
5062 bfd_get_filename (dwz->dwz_bfd.get ()),
5063 &dwz->debug_names, dwz_map))
5064 {
5065 warning (_("could not read '.debug_names' section from %s; skipping"),
5066 bfd_get_filename (dwz->dwz_bfd.get ()));
5067 return false;
5068 }
5069 }
5070
5071 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
5072
5073 if (map->tu_count != 0)
5074 {
5075 /* We can only handle a single .debug_types when we have an
5076 index. */
5077 if (dwarf2_per_objfile->types.size () != 1)
5078 return false;
5079
5080 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
5081
5082 create_signatured_type_table_from_debug_names
5083 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->abbrev);
5084 }
5085
5086 create_addrmap_from_aranges (dwarf2_per_objfile,
5087 &dwarf2_per_objfile->debug_aranges);
5088
5089 dwarf2_per_objfile->debug_names_table = std::move (map);
5090 dwarf2_per_objfile->using_index = 1;
5091 dwarf2_per_objfile->quick_file_names_table =
5092 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
5093
5094 return true;
5095 }
5096
5097 /* Type used to manage iterating over all CUs looking for a symbol for
5098 .debug_names. */
5099
5100 class dw2_debug_names_iterator
5101 {
5102 public:
5103 dw2_debug_names_iterator (const mapped_debug_names &map,
5104 gdb::optional<block_enum> block_index,
5105 domain_enum domain,
5106 const char *name)
5107 : m_map (map), m_block_index (block_index), m_domain (domain),
5108 m_addr (find_vec_in_debug_names (map, name))
5109 {}
5110
5111 dw2_debug_names_iterator (const mapped_debug_names &map,
5112 search_domain search, uint32_t namei)
5113 : m_map (map),
5114 m_search (search),
5115 m_addr (find_vec_in_debug_names (map, namei))
5116 {}
5117
5118 dw2_debug_names_iterator (const mapped_debug_names &map,
5119 block_enum block_index, domain_enum domain,
5120 uint32_t namei)
5121 : m_map (map), m_block_index (block_index), m_domain (domain),
5122 m_addr (find_vec_in_debug_names (map, namei))
5123 {}
5124
5125 /* Return the next matching CU or NULL if there are no more. */
5126 dwarf2_per_cu_data *next ();
5127
5128 private:
5129 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5130 const char *name);
5131 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5132 uint32_t namei);
5133
5134 /* The internalized form of .debug_names. */
5135 const mapped_debug_names &m_map;
5136
5137 /* If set, only look for symbols that match that block. Valid values are
5138 GLOBAL_BLOCK and STATIC_BLOCK. */
5139 const gdb::optional<block_enum> m_block_index;
5140
5141 /* The kind of symbol we're looking for. */
5142 const domain_enum m_domain = UNDEF_DOMAIN;
5143 const search_domain m_search = ALL_DOMAIN;
5144
5145 /* The list of CUs from the index entry of the symbol, or NULL if
5146 not found. */
5147 const gdb_byte *m_addr;
5148 };
5149
5150 const char *
5151 mapped_debug_names::namei_to_name (uint32_t namei) const
5152 {
5153 const ULONGEST namei_string_offs
5154 = extract_unsigned_integer ((name_table_string_offs_reordered
5155 + namei * offset_size),
5156 offset_size,
5157 dwarf5_byte_order);
5158 return read_indirect_string_at_offset
5159 (dwarf2_per_objfile, dwarf2_per_objfile->objfile->obfd, namei_string_offs);
5160 }
5161
5162 /* Find a slot in .debug_names for the object named NAME. If NAME is
5163 found, return pointer to its pool data. If NAME cannot be found,
5164 return NULL. */
5165
5166 const gdb_byte *
5167 dw2_debug_names_iterator::find_vec_in_debug_names
5168 (const mapped_debug_names &map, const char *name)
5169 {
5170 int (*cmp) (const char *, const char *);
5171
5172 gdb::unique_xmalloc_ptr<char> without_params;
5173 if (current_language->la_language == language_cplus
5174 || current_language->la_language == language_fortran
5175 || current_language->la_language == language_d)
5176 {
5177 /* NAME is already canonical. Drop any qualifiers as
5178 .debug_names does not contain any. */
5179
5180 if (strchr (name, '(') != NULL)
5181 {
5182 without_params = cp_remove_params (name);
5183 if (without_params != NULL)
5184 name = without_params.get ();
5185 }
5186 }
5187
5188 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5189
5190 const uint32_t full_hash = dwarf5_djb_hash (name);
5191 uint32_t namei
5192 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5193 (map.bucket_table_reordered
5194 + (full_hash % map.bucket_count)), 4,
5195 map.dwarf5_byte_order);
5196 if (namei == 0)
5197 return NULL;
5198 --namei;
5199 if (namei >= map.name_count)
5200 {
5201 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5202 "[in module %s]"),
5203 namei, map.name_count,
5204 objfile_name (map.dwarf2_per_objfile->objfile));
5205 return NULL;
5206 }
5207
5208 for (;;)
5209 {
5210 const uint32_t namei_full_hash
5211 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5212 (map.hash_table_reordered + namei), 4,
5213 map.dwarf5_byte_order);
5214 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5215 return NULL;
5216
5217 if (full_hash == namei_full_hash)
5218 {
5219 const char *const namei_string = map.namei_to_name (namei);
5220
5221 #if 0 /* An expensive sanity check. */
5222 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5223 {
5224 complaint (_("Wrong .debug_names hash for string at index %u "
5225 "[in module %s]"),
5226 namei, objfile_name (dwarf2_per_objfile->objfile));
5227 return NULL;
5228 }
5229 #endif
5230
5231 if (cmp (namei_string, name) == 0)
5232 {
5233 const ULONGEST namei_entry_offs
5234 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5235 + namei * map.offset_size),
5236 map.offset_size, map.dwarf5_byte_order);
5237 return map.entry_pool + namei_entry_offs;
5238 }
5239 }
5240
5241 ++namei;
5242 if (namei >= map.name_count)
5243 return NULL;
5244 }
5245 }
5246
5247 const gdb_byte *
5248 dw2_debug_names_iterator::find_vec_in_debug_names
5249 (const mapped_debug_names &map, uint32_t namei)
5250 {
5251 if (namei >= map.name_count)
5252 {
5253 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5254 "[in module %s]"),
5255 namei, map.name_count,
5256 objfile_name (map.dwarf2_per_objfile->objfile));
5257 return NULL;
5258 }
5259
5260 const ULONGEST namei_entry_offs
5261 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5262 + namei * map.offset_size),
5263 map.offset_size, map.dwarf5_byte_order);
5264 return map.entry_pool + namei_entry_offs;
5265 }
5266
5267 /* See dw2_debug_names_iterator. */
5268
5269 dwarf2_per_cu_data *
5270 dw2_debug_names_iterator::next ()
5271 {
5272 if (m_addr == NULL)
5273 return NULL;
5274
5275 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5276 struct objfile *objfile = dwarf2_per_objfile->objfile;
5277 bfd *const abfd = objfile->obfd;
5278
5279 again:
5280
5281 unsigned int bytes_read;
5282 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5283 m_addr += bytes_read;
5284 if (abbrev == 0)
5285 return NULL;
5286
5287 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5288 if (indexval_it == m_map.abbrev_map.cend ())
5289 {
5290 complaint (_("Wrong .debug_names undefined abbrev code %s "
5291 "[in module %s]"),
5292 pulongest (abbrev), objfile_name (objfile));
5293 return NULL;
5294 }
5295 const mapped_debug_names::index_val &indexval = indexval_it->second;
5296 enum class symbol_linkage {
5297 unknown,
5298 static_,
5299 extern_,
5300 } symbol_linkage_ = symbol_linkage::unknown;
5301 dwarf2_per_cu_data *per_cu = NULL;
5302 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5303 {
5304 ULONGEST ull;
5305 switch (attr.form)
5306 {
5307 case DW_FORM_implicit_const:
5308 ull = attr.implicit_const;
5309 break;
5310 case DW_FORM_flag_present:
5311 ull = 1;
5312 break;
5313 case DW_FORM_udata:
5314 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5315 m_addr += bytes_read;
5316 break;
5317 default:
5318 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5319 dwarf_form_name (attr.form),
5320 objfile_name (objfile));
5321 return NULL;
5322 }
5323 switch (attr.dw_idx)
5324 {
5325 case DW_IDX_compile_unit:
5326 /* Don't crash on bad data. */
5327 if (ull >= dwarf2_per_objfile->all_comp_units.size ())
5328 {
5329 complaint (_(".debug_names entry has bad CU index %s"
5330 " [in module %s]"),
5331 pulongest (ull),
5332 objfile_name (dwarf2_per_objfile->objfile));
5333 continue;
5334 }
5335 per_cu = dwarf2_per_objfile->get_cutu (ull);
5336 break;
5337 case DW_IDX_type_unit:
5338 /* Don't crash on bad data. */
5339 if (ull >= dwarf2_per_objfile->all_type_units.size ())
5340 {
5341 complaint (_(".debug_names entry has bad TU index %s"
5342 " [in module %s]"),
5343 pulongest (ull),
5344 objfile_name (dwarf2_per_objfile->objfile));
5345 continue;
5346 }
5347 per_cu = &dwarf2_per_objfile->get_tu (ull)->per_cu;
5348 break;
5349 case DW_IDX_GNU_internal:
5350 if (!m_map.augmentation_is_gdb)
5351 break;
5352 symbol_linkage_ = symbol_linkage::static_;
5353 break;
5354 case DW_IDX_GNU_external:
5355 if (!m_map.augmentation_is_gdb)
5356 break;
5357 symbol_linkage_ = symbol_linkage::extern_;
5358 break;
5359 }
5360 }
5361
5362 /* Skip if already read in. */
5363 if (per_cu->v.quick->compunit_symtab)
5364 goto again;
5365
5366 /* Check static vs global. */
5367 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
5368 {
5369 const bool want_static = *m_block_index == STATIC_BLOCK;
5370 const bool symbol_is_static =
5371 symbol_linkage_ == symbol_linkage::static_;
5372 if (want_static != symbol_is_static)
5373 goto again;
5374 }
5375
5376 /* Match dw2_symtab_iter_next, symbol_kind
5377 and debug_names::psymbol_tag. */
5378 switch (m_domain)
5379 {
5380 case VAR_DOMAIN:
5381 switch (indexval.dwarf_tag)
5382 {
5383 case DW_TAG_variable:
5384 case DW_TAG_subprogram:
5385 /* Some types are also in VAR_DOMAIN. */
5386 case DW_TAG_typedef:
5387 case DW_TAG_structure_type:
5388 break;
5389 default:
5390 goto again;
5391 }
5392 break;
5393 case STRUCT_DOMAIN:
5394 switch (indexval.dwarf_tag)
5395 {
5396 case DW_TAG_typedef:
5397 case DW_TAG_structure_type:
5398 break;
5399 default:
5400 goto again;
5401 }
5402 break;
5403 case LABEL_DOMAIN:
5404 switch (indexval.dwarf_tag)
5405 {
5406 case 0:
5407 case DW_TAG_variable:
5408 break;
5409 default:
5410 goto again;
5411 }
5412 break;
5413 case MODULE_DOMAIN:
5414 switch (indexval.dwarf_tag)
5415 {
5416 case DW_TAG_module:
5417 break;
5418 default:
5419 goto again;
5420 }
5421 break;
5422 default:
5423 break;
5424 }
5425
5426 /* Match dw2_expand_symtabs_matching, symbol_kind and
5427 debug_names::psymbol_tag. */
5428 switch (m_search)
5429 {
5430 case VARIABLES_DOMAIN:
5431 switch (indexval.dwarf_tag)
5432 {
5433 case DW_TAG_variable:
5434 break;
5435 default:
5436 goto again;
5437 }
5438 break;
5439 case FUNCTIONS_DOMAIN:
5440 switch (indexval.dwarf_tag)
5441 {
5442 case DW_TAG_subprogram:
5443 break;
5444 default:
5445 goto again;
5446 }
5447 break;
5448 case TYPES_DOMAIN:
5449 switch (indexval.dwarf_tag)
5450 {
5451 case DW_TAG_typedef:
5452 case DW_TAG_structure_type:
5453 break;
5454 default:
5455 goto again;
5456 }
5457 break;
5458 case MODULES_DOMAIN:
5459 switch (indexval.dwarf_tag)
5460 {
5461 case DW_TAG_module:
5462 break;
5463 default:
5464 goto again;
5465 }
5466 default:
5467 break;
5468 }
5469
5470 return per_cu;
5471 }
5472
5473 static struct compunit_symtab *
5474 dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
5475 const char *name, domain_enum domain)
5476 {
5477 struct dwarf2_per_objfile *dwarf2_per_objfile
5478 = get_dwarf2_per_objfile (objfile);
5479
5480 const auto &mapp = dwarf2_per_objfile->debug_names_table;
5481 if (!mapp)
5482 {
5483 /* index is NULL if OBJF_READNOW. */
5484 return NULL;
5485 }
5486 const auto &map = *mapp;
5487
5488 dw2_debug_names_iterator iter (map, block_index, domain, name);
5489
5490 struct compunit_symtab *stab_best = NULL;
5491 struct dwarf2_per_cu_data *per_cu;
5492 while ((per_cu = iter.next ()) != NULL)
5493 {
5494 struct symbol *sym, *with_opaque = NULL;
5495 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
5496 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
5497 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
5498
5499 sym = block_find_symbol (block, name, domain,
5500 block_find_non_opaque_type_preferred,
5501 &with_opaque);
5502
5503 /* Some caution must be observed with overloaded functions and
5504 methods, since the index will not contain any overload
5505 information (but NAME might contain it). */
5506
5507 if (sym != NULL
5508 && strcmp_iw (sym->search_name (), name) == 0)
5509 return stab;
5510 if (with_opaque != NULL
5511 && strcmp_iw (with_opaque->search_name (), name) == 0)
5512 stab_best = stab;
5513
5514 /* Keep looking through other CUs. */
5515 }
5516
5517 return stab_best;
5518 }
5519
5520 /* This dumps minimal information about .debug_names. It is called
5521 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5522 uses this to verify that .debug_names has been loaded. */
5523
5524 static void
5525 dw2_debug_names_dump (struct objfile *objfile)
5526 {
5527 struct dwarf2_per_objfile *dwarf2_per_objfile
5528 = get_dwarf2_per_objfile (objfile);
5529
5530 gdb_assert (dwarf2_per_objfile->using_index);
5531 printf_filtered (".debug_names:");
5532 if (dwarf2_per_objfile->debug_names_table)
5533 printf_filtered (" exists\n");
5534 else
5535 printf_filtered (" faked for \"readnow\"\n");
5536 printf_filtered ("\n");
5537 }
5538
5539 static void
5540 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5541 const char *func_name)
5542 {
5543 struct dwarf2_per_objfile *dwarf2_per_objfile
5544 = get_dwarf2_per_objfile (objfile);
5545
5546 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
5547 if (dwarf2_per_objfile->debug_names_table)
5548 {
5549 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5550
5551 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
5552
5553 struct dwarf2_per_cu_data *per_cu;
5554 while ((per_cu = iter.next ()) != NULL)
5555 dw2_instantiate_symtab (per_cu, false);
5556 }
5557 }
5558
5559 static void
5560 dw2_debug_names_map_matching_symbols
5561 (struct objfile *objfile,
5562 const lookup_name_info &name, domain_enum domain,
5563 int global,
5564 gdb::function_view<symbol_found_callback_ftype> callback,
5565 symbol_compare_ftype *ordered_compare)
5566 {
5567 struct dwarf2_per_objfile *dwarf2_per_objfile
5568 = get_dwarf2_per_objfile (objfile);
5569
5570 /* debug_names_table is NULL if OBJF_READNOW. */
5571 if (!dwarf2_per_objfile->debug_names_table)
5572 return;
5573
5574 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5575 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5576
5577 const char *match_name = name.ada ().lookup_name ().c_str ();
5578 auto matcher = [&] (const char *symname)
5579 {
5580 if (ordered_compare == nullptr)
5581 return true;
5582 return ordered_compare (symname, match_name) == 0;
5583 };
5584
5585 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5586 [&] (offset_type namei)
5587 {
5588 /* The name was matched, now expand corresponding CUs that were
5589 marked. */
5590 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
5591
5592 struct dwarf2_per_cu_data *per_cu;
5593 while ((per_cu = iter.next ()) != NULL)
5594 dw2_expand_symtabs_matching_one (per_cu, nullptr, nullptr);
5595 return true;
5596 });
5597
5598 /* It's a shame we couldn't do this inside the
5599 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5600 that have already been expanded. Instead, this loop matches what
5601 the psymtab code does. */
5602 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5603 {
5604 struct compunit_symtab *cust = per_cu->v.quick->compunit_symtab;
5605 if (cust != nullptr)
5606 {
5607 const struct block *block
5608 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
5609 if (!iterate_over_symbols_terminated (block, name,
5610 domain, callback))
5611 break;
5612 }
5613 }
5614 }
5615
5616 static void
5617 dw2_debug_names_expand_symtabs_matching
5618 (struct objfile *objfile,
5619 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5620 const lookup_name_info &lookup_name,
5621 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5622 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5623 enum search_domain kind)
5624 {
5625 struct dwarf2_per_objfile *dwarf2_per_objfile
5626 = get_dwarf2_per_objfile (objfile);
5627
5628 /* debug_names_table is NULL if OBJF_READNOW. */
5629 if (!dwarf2_per_objfile->debug_names_table)
5630 return;
5631
5632 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
5633
5634 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5635
5636 dw2_expand_symtabs_matching_symbol (map, lookup_name,
5637 symbol_matcher,
5638 kind, [&] (offset_type namei)
5639 {
5640 /* The name was matched, now expand corresponding CUs that were
5641 marked. */
5642 dw2_debug_names_iterator iter (map, kind, namei);
5643
5644 struct dwarf2_per_cu_data *per_cu;
5645 while ((per_cu = iter.next ()) != NULL)
5646 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5647 expansion_notify);
5648 return true;
5649 });
5650 }
5651
5652 const struct quick_symbol_functions dwarf2_debug_names_functions =
5653 {
5654 dw2_has_symbols,
5655 dw2_find_last_source_symtab,
5656 dw2_forget_cached_source_info,
5657 dw2_map_symtabs_matching_filename,
5658 dw2_debug_names_lookup_symbol,
5659 dw2_print_stats,
5660 dw2_debug_names_dump,
5661 dw2_debug_names_expand_symtabs_for_function,
5662 dw2_expand_all_symtabs,
5663 dw2_expand_symtabs_with_fullname,
5664 dw2_debug_names_map_matching_symbols,
5665 dw2_debug_names_expand_symtabs_matching,
5666 dw2_find_pc_sect_compunit_symtab,
5667 NULL,
5668 dw2_map_symbol_filenames
5669 };
5670
5671 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5672 to either a dwarf2_per_objfile or dwz_file object. */
5673
5674 template <typename T>
5675 static gdb::array_view<const gdb_byte>
5676 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5677 {
5678 dwarf2_section_info *section = &section_owner->gdb_index;
5679
5680 if (section->empty ())
5681 return {};
5682
5683 /* Older elfutils strip versions could keep the section in the main
5684 executable while splitting it for the separate debug info file. */
5685 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5686 return {};
5687
5688 section->read (obj);
5689
5690 /* dwarf2_section_info::size is a bfd_size_type, while
5691 gdb::array_view works with size_t. On 32-bit hosts, with
5692 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5693 is 32-bit. So we need an explicit narrowing conversion here.
5694 This is fine, because it's impossible to allocate or mmap an
5695 array/buffer larger than what size_t can represent. */
5696 return gdb::make_array_view (section->buffer, section->size);
5697 }
5698
5699 /* Lookup the index cache for the contents of the index associated to
5700 DWARF2_OBJ. */
5701
5702 static gdb::array_view<const gdb_byte>
5703 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_objfile *dwarf2_obj)
5704 {
5705 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5706 if (build_id == nullptr)
5707 return {};
5708
5709 return global_index_cache.lookup_gdb_index (build_id,
5710 &dwarf2_obj->index_cache_res);
5711 }
5712
5713 /* Same as the above, but for DWZ. */
5714
5715 static gdb::array_view<const gdb_byte>
5716 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5717 {
5718 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5719 if (build_id == nullptr)
5720 return {};
5721
5722 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5723 }
5724
5725 /* See symfile.h. */
5726
5727 bool
5728 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
5729 {
5730 struct dwarf2_per_objfile *dwarf2_per_objfile
5731 = get_dwarf2_per_objfile (objfile);
5732
5733 /* If we're about to read full symbols, don't bother with the
5734 indices. In this case we also don't care if some other debug
5735 format is making psymtabs, because they are all about to be
5736 expanded anyway. */
5737 if ((objfile->flags & OBJF_READNOW))
5738 {
5739 dwarf2_per_objfile->using_index = 1;
5740 create_all_comp_units (dwarf2_per_objfile);
5741 create_all_type_units (dwarf2_per_objfile);
5742 dwarf2_per_objfile->quick_file_names_table
5743 = create_quick_file_names_table
5744 (dwarf2_per_objfile->all_comp_units.size ());
5745
5746 for (int i = 0; i < (dwarf2_per_objfile->all_comp_units.size ()
5747 + dwarf2_per_objfile->all_type_units.size ()); ++i)
5748 {
5749 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
5750
5751 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5752 struct dwarf2_per_cu_quick_data);
5753 }
5754
5755 /* Return 1 so that gdb sees the "quick" functions. However,
5756 these functions will be no-ops because we will have expanded
5757 all symtabs. */
5758 *index_kind = dw_index_kind::GDB_INDEX;
5759 return true;
5760 }
5761
5762 if (dwarf2_read_debug_names (dwarf2_per_objfile))
5763 {
5764 *index_kind = dw_index_kind::DEBUG_NAMES;
5765 return true;
5766 }
5767
5768 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5769 get_gdb_index_contents_from_section<struct dwarf2_per_objfile>,
5770 get_gdb_index_contents_from_section<dwz_file>))
5771 {
5772 *index_kind = dw_index_kind::GDB_INDEX;
5773 return true;
5774 }
5775
5776 /* ... otherwise, try to find the index in the index cache. */
5777 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5778 get_gdb_index_contents_from_cache,
5779 get_gdb_index_contents_from_cache_dwz))
5780 {
5781 global_index_cache.hit ();
5782 *index_kind = dw_index_kind::GDB_INDEX;
5783 return true;
5784 }
5785
5786 global_index_cache.miss ();
5787 return false;
5788 }
5789
5790 \f
5791
5792 /* Build a partial symbol table. */
5793
5794 void
5795 dwarf2_build_psymtabs (struct objfile *objfile)
5796 {
5797 struct dwarf2_per_objfile *dwarf2_per_objfile
5798 = get_dwarf2_per_objfile (objfile);
5799
5800 init_psymbol_list (objfile, 1024);
5801
5802 try
5803 {
5804 /* This isn't really ideal: all the data we allocate on the
5805 objfile's obstack is still uselessly kept around. However,
5806 freeing it seems unsafe. */
5807 psymtab_discarder psymtabs (objfile);
5808 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
5809 psymtabs.keep ();
5810
5811 /* (maybe) store an index in the cache. */
5812 global_index_cache.store (dwarf2_per_objfile);
5813 }
5814 catch (const gdb_exception_error &except)
5815 {
5816 exception_print (gdb_stderr, except);
5817 }
5818 }
5819
5820 /* Find the base address of the compilation unit for range lists and
5821 location lists. It will normally be specified by DW_AT_low_pc.
5822 In DWARF-3 draft 4, the base address could be overridden by
5823 DW_AT_entry_pc. It's been removed, but GCC still uses this for
5824 compilation units with discontinuous ranges. */
5825
5826 static void
5827 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
5828 {
5829 struct attribute *attr;
5830
5831 cu->base_known = 0;
5832 cu->base_address = 0;
5833
5834 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
5835 if (attr != nullptr)
5836 {
5837 cu->base_address = attr->value_as_address ();
5838 cu->base_known = 1;
5839 }
5840 else
5841 {
5842 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5843 if (attr != nullptr)
5844 {
5845 cu->base_address = attr->value_as_address ();
5846 cu->base_known = 1;
5847 }
5848 }
5849 }
5850
5851 /* Helper function that returns the proper abbrev section for
5852 THIS_CU. */
5853
5854 static struct dwarf2_section_info *
5855 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
5856 {
5857 struct dwarf2_section_info *abbrev;
5858 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
5859
5860 if (this_cu->is_dwz)
5861 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
5862 else
5863 abbrev = &dwarf2_per_objfile->abbrev;
5864
5865 return abbrev;
5866 }
5867
5868 /* Fetch the abbreviation table offset from a comp or type unit header. */
5869
5870 static sect_offset
5871 read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
5872 struct dwarf2_section_info *section,
5873 sect_offset sect_off)
5874 {
5875 bfd *abfd = section->get_bfd_owner ();
5876 const gdb_byte *info_ptr;
5877 unsigned int initial_length_size, offset_size;
5878 uint16_t version;
5879
5880 section->read (dwarf2_per_objfile->objfile);
5881 info_ptr = section->buffer + to_underlying (sect_off);
5882 read_initial_length (abfd, info_ptr, &initial_length_size);
5883 offset_size = initial_length_size == 4 ? 4 : 8;
5884 info_ptr += initial_length_size;
5885
5886 version = read_2_bytes (abfd, info_ptr);
5887 info_ptr += 2;
5888 if (version >= 5)
5889 {
5890 /* Skip unit type and address size. */
5891 info_ptr += 2;
5892 }
5893
5894 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
5895 }
5896
5897 /* Allocate a new partial symtab for file named NAME and mark this new
5898 partial symtab as being an include of PST. */
5899
5900 static void
5901 dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
5902 struct objfile *objfile)
5903 {
5904 dwarf2_psymtab *subpst = new dwarf2_psymtab (name, objfile);
5905
5906 if (!IS_ABSOLUTE_PATH (subpst->filename))
5907 {
5908 /* It shares objfile->objfile_obstack. */
5909 subpst->dirname = pst->dirname;
5910 }
5911
5912 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
5913 subpst->dependencies[0] = pst;
5914 subpst->number_of_dependencies = 1;
5915
5916 /* No private part is necessary for include psymtabs. This property
5917 can be used to differentiate between such include psymtabs and
5918 the regular ones. */
5919 subpst->per_cu_data = nullptr;
5920 }
5921
5922 /* Read the Line Number Program data and extract the list of files
5923 included by the source file represented by PST. Build an include
5924 partial symtab for each of these included files. */
5925
5926 static void
5927 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
5928 struct die_info *die,
5929 dwarf2_psymtab *pst)
5930 {
5931 line_header_up lh;
5932 struct attribute *attr;
5933
5934 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5935 if (attr != nullptr)
5936 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
5937 if (lh == NULL)
5938 return; /* No linetable, so no includes. */
5939
5940 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
5941 that we pass in the raw text_low here; that is ok because we're
5942 only decoding the line table to make include partial symtabs, and
5943 so the addresses aren't really used. */
5944 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
5945 pst->raw_text_low (), 1);
5946 }
5947
5948 static hashval_t
5949 hash_signatured_type (const void *item)
5950 {
5951 const struct signatured_type *sig_type
5952 = (const struct signatured_type *) item;
5953
5954 /* This drops the top 32 bits of the signature, but is ok for a hash. */
5955 return sig_type->signature;
5956 }
5957
5958 static int
5959 eq_signatured_type (const void *item_lhs, const void *item_rhs)
5960 {
5961 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
5962 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
5963
5964 return lhs->signature == rhs->signature;
5965 }
5966
5967 /* Allocate a hash table for signatured types. */
5968
5969 static htab_up
5970 allocate_signatured_type_table ()
5971 {
5972 return htab_up (htab_create_alloc (41,
5973 hash_signatured_type,
5974 eq_signatured_type,
5975 NULL, xcalloc, xfree));
5976 }
5977
5978 /* A helper function to add a signatured type CU to a table. */
5979
5980 static int
5981 add_signatured_type_cu_to_table (void **slot, void *datum)
5982 {
5983 struct signatured_type *sigt = (struct signatured_type *) *slot;
5984 std::vector<signatured_type *> *all_type_units
5985 = (std::vector<signatured_type *> *) datum;
5986
5987 all_type_units->push_back (sigt);
5988
5989 return 1;
5990 }
5991
5992 /* A helper for create_debug_types_hash_table. Read types from SECTION
5993 and fill them into TYPES_HTAB. It will process only type units,
5994 therefore DW_UT_type. */
5995
5996 static void
5997 create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
5998 struct dwo_file *dwo_file,
5999 dwarf2_section_info *section, htab_up &types_htab,
6000 rcuh_kind section_kind)
6001 {
6002 struct objfile *objfile = dwarf2_per_objfile->objfile;
6003 struct dwarf2_section_info *abbrev_section;
6004 bfd *abfd;
6005 const gdb_byte *info_ptr, *end_ptr;
6006
6007 abbrev_section = (dwo_file != NULL
6008 ? &dwo_file->sections.abbrev
6009 : &dwarf2_per_objfile->abbrev);
6010
6011 if (dwarf_read_debug)
6012 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6013 section->get_name (),
6014 abbrev_section->get_file_name ());
6015
6016 section->read (objfile);
6017 info_ptr = section->buffer;
6018
6019 if (info_ptr == NULL)
6020 return;
6021
6022 /* We can't set abfd until now because the section may be empty or
6023 not present, in which case the bfd is unknown. */
6024 abfd = section->get_bfd_owner ();
6025
6026 /* We don't use cutu_reader here because we don't need to read
6027 any dies: the signature is in the header. */
6028
6029 end_ptr = info_ptr + section->size;
6030 while (info_ptr < end_ptr)
6031 {
6032 struct signatured_type *sig_type;
6033 struct dwo_unit *dwo_tu;
6034 void **slot;
6035 const gdb_byte *ptr = info_ptr;
6036 struct comp_unit_head header;
6037 unsigned int length;
6038
6039 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6040
6041 /* Initialize it due to a false compiler warning. */
6042 header.signature = -1;
6043 header.type_cu_offset_in_tu = (cu_offset) -1;
6044
6045 /* We need to read the type's signature in order to build the hash
6046 table, but we don't need anything else just yet. */
6047
6048 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
6049 abbrev_section, ptr, section_kind);
6050
6051 length = header.get_length ();
6052
6053 /* Skip dummy type units. */
6054 if (ptr >= info_ptr + length
6055 || peek_abbrev_code (abfd, ptr) == 0
6056 || header.unit_type != DW_UT_type)
6057 {
6058 info_ptr += length;
6059 continue;
6060 }
6061
6062 if (types_htab == NULL)
6063 {
6064 if (dwo_file)
6065 types_htab = allocate_dwo_unit_table ();
6066 else
6067 types_htab = allocate_signatured_type_table ();
6068 }
6069
6070 if (dwo_file)
6071 {
6072 sig_type = NULL;
6073 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6074 struct dwo_unit);
6075 dwo_tu->dwo_file = dwo_file;
6076 dwo_tu->signature = header.signature;
6077 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6078 dwo_tu->section = section;
6079 dwo_tu->sect_off = sect_off;
6080 dwo_tu->length = length;
6081 }
6082 else
6083 {
6084 /* N.B.: type_offset is not usable if this type uses a DWO file.
6085 The real type_offset is in the DWO file. */
6086 dwo_tu = NULL;
6087 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6088 struct signatured_type);
6089 sig_type->signature = header.signature;
6090 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6091 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6092 sig_type->per_cu.is_debug_types = 1;
6093 sig_type->per_cu.section = section;
6094 sig_type->per_cu.sect_off = sect_off;
6095 sig_type->per_cu.length = length;
6096 }
6097
6098 slot = htab_find_slot (types_htab.get (),
6099 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6100 INSERT);
6101 gdb_assert (slot != NULL);
6102 if (*slot != NULL)
6103 {
6104 sect_offset dup_sect_off;
6105
6106 if (dwo_file)
6107 {
6108 const struct dwo_unit *dup_tu
6109 = (const struct dwo_unit *) *slot;
6110
6111 dup_sect_off = dup_tu->sect_off;
6112 }
6113 else
6114 {
6115 const struct signatured_type *dup_tu
6116 = (const struct signatured_type *) *slot;
6117
6118 dup_sect_off = dup_tu->per_cu.sect_off;
6119 }
6120
6121 complaint (_("debug type entry at offset %s is duplicate to"
6122 " the entry at offset %s, signature %s"),
6123 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
6124 hex_string (header.signature));
6125 }
6126 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
6127
6128 if (dwarf_read_debug > 1)
6129 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6130 sect_offset_str (sect_off),
6131 hex_string (header.signature));
6132
6133 info_ptr += length;
6134 }
6135 }
6136
6137 /* Create the hash table of all entries in the .debug_types
6138 (or .debug_types.dwo) section(s).
6139 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6140 otherwise it is NULL.
6141
6142 The result is a pointer to the hash table or NULL if there are no types.
6143
6144 Note: This function processes DWO files only, not DWP files. */
6145
6146 static void
6147 create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6148 struct dwo_file *dwo_file,
6149 gdb::array_view<dwarf2_section_info> type_sections,
6150 htab_up &types_htab)
6151 {
6152 for (dwarf2_section_info &section : type_sections)
6153 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
6154 types_htab, rcuh_kind::TYPE);
6155 }
6156
6157 /* Create the hash table of all entries in the .debug_types section,
6158 and initialize all_type_units.
6159 The result is zero if there is an error (e.g. missing .debug_types section),
6160 otherwise non-zero. */
6161
6162 static int
6163 create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6164 {
6165 htab_up types_htab;
6166
6167 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6168 &dwarf2_per_objfile->info, types_htab,
6169 rcuh_kind::COMPILE);
6170 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6171 dwarf2_per_objfile->types, types_htab);
6172 if (types_htab == NULL)
6173 {
6174 dwarf2_per_objfile->signatured_types = NULL;
6175 return 0;
6176 }
6177
6178 dwarf2_per_objfile->signatured_types = std::move (types_htab);
6179
6180 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
6181 dwarf2_per_objfile->all_type_units.reserve
6182 (htab_elements (dwarf2_per_objfile->signatured_types.get ()));
6183
6184 htab_traverse_noresize (dwarf2_per_objfile->signatured_types.get (),
6185 add_signatured_type_cu_to_table,
6186 &dwarf2_per_objfile->all_type_units);
6187
6188 return 1;
6189 }
6190
6191 /* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6192 If SLOT is non-NULL, it is the entry to use in the hash table.
6193 Otherwise we find one. */
6194
6195 static struct signatured_type *
6196 add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6197 void **slot)
6198 {
6199 struct objfile *objfile = dwarf2_per_objfile->objfile;
6200
6201 if (dwarf2_per_objfile->all_type_units.size ()
6202 == dwarf2_per_objfile->all_type_units.capacity ())
6203 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
6204
6205 signatured_type *sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6206 struct signatured_type);
6207
6208 dwarf2_per_objfile->all_type_units.push_back (sig_type);
6209 sig_type->signature = sig;
6210 sig_type->per_cu.is_debug_types = 1;
6211 if (dwarf2_per_objfile->using_index)
6212 {
6213 sig_type->per_cu.v.quick =
6214 OBSTACK_ZALLOC (&objfile->objfile_obstack,
6215 struct dwarf2_per_cu_quick_data);
6216 }
6217
6218 if (slot == NULL)
6219 {
6220 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6221 sig_type, INSERT);
6222 }
6223 gdb_assert (*slot == NULL);
6224 *slot = sig_type;
6225 /* The rest of sig_type must be filled in by the caller. */
6226 return sig_type;
6227 }
6228
6229 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6230 Fill in SIG_ENTRY with DWO_ENTRY. */
6231
6232 static void
6233 fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
6234 struct signatured_type *sig_entry,
6235 struct dwo_unit *dwo_entry)
6236 {
6237 /* Make sure we're not clobbering something we don't expect to. */
6238 gdb_assert (! sig_entry->per_cu.queued);
6239 gdb_assert (sig_entry->per_cu.cu == NULL);
6240 if (dwarf2_per_objfile->using_index)
6241 {
6242 gdb_assert (sig_entry->per_cu.v.quick != NULL);
6243 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6244 }
6245 else
6246 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
6247 gdb_assert (sig_entry->signature == dwo_entry->signature);
6248 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
6249 gdb_assert (sig_entry->type_unit_group == NULL);
6250 gdb_assert (sig_entry->dwo_unit == NULL);
6251
6252 sig_entry->per_cu.section = dwo_entry->section;
6253 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
6254 sig_entry->per_cu.length = dwo_entry->length;
6255 sig_entry->per_cu.reading_dwo_directly = 1;
6256 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6257 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6258 sig_entry->dwo_unit = dwo_entry;
6259 }
6260
6261 /* Subroutine of lookup_signatured_type.
6262 If we haven't read the TU yet, create the signatured_type data structure
6263 for a TU to be read in directly from a DWO file, bypassing the stub.
6264 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6265 using .gdb_index, then when reading a CU we want to stay in the DWO file
6266 containing that CU. Otherwise we could end up reading several other DWO
6267 files (due to comdat folding) to process the transitive closure of all the
6268 mentioned TUs, and that can be slow. The current DWO file will have every
6269 type signature that it needs.
6270 We only do this for .gdb_index because in the psymtab case we already have
6271 to read all the DWOs to build the type unit groups. */
6272
6273 static struct signatured_type *
6274 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6275 {
6276 struct dwarf2_per_objfile *dwarf2_per_objfile
6277 = cu->per_cu->dwarf2_per_objfile;
6278 struct dwo_file *dwo_file;
6279 struct dwo_unit find_dwo_entry, *dwo_entry;
6280 struct signatured_type find_sig_entry, *sig_entry;
6281 void **slot;
6282
6283 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6284
6285 /* If TU skeletons have been removed then we may not have read in any
6286 TUs yet. */
6287 if (dwarf2_per_objfile->signatured_types == NULL)
6288 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
6289
6290 /* We only ever need to read in one copy of a signatured type.
6291 Use the global signatured_types array to do our own comdat-folding
6292 of types. If this is the first time we're reading this TU, and
6293 the TU has an entry in .gdb_index, replace the recorded data from
6294 .gdb_index with this TU. */
6295
6296 find_sig_entry.signature = sig;
6297 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6298 &find_sig_entry, INSERT);
6299 sig_entry = (struct signatured_type *) *slot;
6300
6301 /* We can get here with the TU already read, *or* in the process of being
6302 read. Don't reassign the global entry to point to this DWO if that's
6303 the case. Also note that if the TU is already being read, it may not
6304 have come from a DWO, the program may be a mix of Fission-compiled
6305 code and non-Fission-compiled code. */
6306
6307 /* Have we already tried to read this TU?
6308 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6309 needn't exist in the global table yet). */
6310 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
6311 return sig_entry;
6312
6313 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6314 dwo_unit of the TU itself. */
6315 dwo_file = cu->dwo_unit->dwo_file;
6316
6317 /* Ok, this is the first time we're reading this TU. */
6318 if (dwo_file->tus == NULL)
6319 return NULL;
6320 find_dwo_entry.signature = sig;
6321 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6322 &find_dwo_entry);
6323 if (dwo_entry == NULL)
6324 return NULL;
6325
6326 /* If the global table doesn't have an entry for this TU, add one. */
6327 if (sig_entry == NULL)
6328 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6329
6330 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6331 sig_entry->per_cu.tu_read = 1;
6332 return sig_entry;
6333 }
6334
6335 /* Subroutine of lookup_signatured_type.
6336 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6337 then try the DWP file. If the TU stub (skeleton) has been removed then
6338 it won't be in .gdb_index. */
6339
6340 static struct signatured_type *
6341 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6342 {
6343 struct dwarf2_per_objfile *dwarf2_per_objfile
6344 = cu->per_cu->dwarf2_per_objfile;
6345 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
6346 struct dwo_unit *dwo_entry;
6347 struct signatured_type find_sig_entry, *sig_entry;
6348 void **slot;
6349
6350 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6351 gdb_assert (dwp_file != NULL);
6352
6353 /* If TU skeletons have been removed then we may not have read in any
6354 TUs yet. */
6355 if (dwarf2_per_objfile->signatured_types == NULL)
6356 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
6357
6358 find_sig_entry.signature = sig;
6359 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6360 &find_sig_entry, INSERT);
6361 sig_entry = (struct signatured_type *) *slot;
6362
6363 /* Have we already tried to read this TU?
6364 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6365 needn't exist in the global table yet). */
6366 if (sig_entry != NULL)
6367 return sig_entry;
6368
6369 if (dwp_file->tus == NULL)
6370 return NULL;
6371 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
6372 sig, 1 /* is_debug_types */);
6373 if (dwo_entry == NULL)
6374 return NULL;
6375
6376 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6377 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6378
6379 return sig_entry;
6380 }
6381
6382 /* Lookup a signature based type for DW_FORM_ref_sig8.
6383 Returns NULL if signature SIG is not present in the table.
6384 It is up to the caller to complain about this. */
6385
6386 static struct signatured_type *
6387 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6388 {
6389 struct dwarf2_per_objfile *dwarf2_per_objfile
6390 = cu->per_cu->dwarf2_per_objfile;
6391
6392 if (cu->dwo_unit
6393 && dwarf2_per_objfile->using_index)
6394 {
6395 /* We're in a DWO/DWP file, and we're using .gdb_index.
6396 These cases require special processing. */
6397 if (get_dwp_file (dwarf2_per_objfile) == NULL)
6398 return lookup_dwo_signatured_type (cu, sig);
6399 else
6400 return lookup_dwp_signatured_type (cu, sig);
6401 }
6402 else
6403 {
6404 struct signatured_type find_entry, *entry;
6405
6406 if (dwarf2_per_objfile->signatured_types == NULL)
6407 return NULL;
6408 find_entry.signature = sig;
6409 entry = ((struct signatured_type *)
6410 htab_find (dwarf2_per_objfile->signatured_types.get (),
6411 &find_entry));
6412 return entry;
6413 }
6414 }
6415
6416 /* Return the address base of the compile unit, which, if exists, is stored
6417 either at the attribute DW_AT_GNU_addr_base, or DW_AT_addr_base. */
6418 static gdb::optional<ULONGEST>
6419 lookup_addr_base (struct die_info *comp_unit_die)
6420 {
6421 struct attribute *attr;
6422 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_addr_base);
6423 if (attr == nullptr)
6424 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_GNU_addr_base);
6425 if (attr == nullptr)
6426 return gdb::optional<ULONGEST> ();
6427 return DW_UNSND (attr);
6428 }
6429
6430 /* Return range lists base of the compile unit, which, if exists, is stored
6431 either at the attribute DW_AT_rnglists_base or DW_AT_GNU_ranges_base. */
6432 static ULONGEST
6433 lookup_ranges_base (struct die_info *comp_unit_die)
6434 {
6435 struct attribute *attr;
6436 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_rnglists_base);
6437 if (attr == nullptr)
6438 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_GNU_ranges_base);
6439 if (attr == nullptr)
6440 return 0;
6441 return DW_UNSND (attr);
6442 }
6443
6444 /* Low level DIE reading support. */
6445
6446 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6447
6448 static void
6449 init_cu_die_reader (struct die_reader_specs *reader,
6450 struct dwarf2_cu *cu,
6451 struct dwarf2_section_info *section,
6452 struct dwo_file *dwo_file,
6453 struct abbrev_table *abbrev_table)
6454 {
6455 gdb_assert (section->readin && section->buffer != NULL);
6456 reader->abfd = section->get_bfd_owner ();
6457 reader->cu = cu;
6458 reader->dwo_file = dwo_file;
6459 reader->die_section = section;
6460 reader->buffer = section->buffer;
6461 reader->buffer_end = section->buffer + section->size;
6462 reader->abbrev_table = abbrev_table;
6463 }
6464
6465 /* Subroutine of cutu_reader to simplify it.
6466 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
6467 There's just a lot of work to do, and cutu_reader is big enough
6468 already.
6469
6470 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6471 from it to the DIE in the DWO. If NULL we are skipping the stub.
6472 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6473 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
6474 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6475 STUB_COMP_DIR may be non-NULL.
6476 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
6477 are filled in with the info of the DIE from the DWO file.
6478 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6479 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6480 kept around for at least as long as *RESULT_READER.
6481
6482 The result is non-zero if a valid (non-dummy) DIE was found. */
6483
6484 static int
6485 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
6486 struct dwo_unit *dwo_unit,
6487 struct die_info *stub_comp_unit_die,
6488 const char *stub_comp_dir,
6489 struct die_reader_specs *result_reader,
6490 const gdb_byte **result_info_ptr,
6491 struct die_info **result_comp_unit_die,
6492 abbrev_table_up *result_dwo_abbrev_table)
6493 {
6494 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6495 struct objfile *objfile = dwarf2_per_objfile->objfile;
6496 struct dwarf2_cu *cu = this_cu->cu;
6497 bfd *abfd;
6498 const gdb_byte *begin_info_ptr, *info_ptr;
6499 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6500 int i,num_extra_attrs;
6501 struct dwarf2_section_info *dwo_abbrev_section;
6502 struct die_info *comp_unit_die;
6503
6504 /* At most one of these may be provided. */
6505 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
6506
6507 /* These attributes aren't processed until later:
6508 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
6509 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6510 referenced later. However, these attributes are found in the stub
6511 which we won't have later. In order to not impose this complication
6512 on the rest of the code, we read them here and copy them to the
6513 DWO CU/TU die. */
6514
6515 stmt_list = NULL;
6516 low_pc = NULL;
6517 high_pc = NULL;
6518 ranges = NULL;
6519 comp_dir = NULL;
6520
6521 if (stub_comp_unit_die != NULL)
6522 {
6523 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6524 DWO file. */
6525 if (! this_cu->is_debug_types)
6526 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6527 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6528 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6529 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6530 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6531
6532 cu->addr_base = lookup_addr_base (stub_comp_unit_die);
6533
6534 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
6535 here (if needed). We need the value before we can process
6536 DW_AT_ranges. */
6537 cu->ranges_base = lookup_ranges_base (stub_comp_unit_die);
6538 }
6539 else if (stub_comp_dir != NULL)
6540 {
6541 /* Reconstruct the comp_dir attribute to simplify the code below. */
6542 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
6543 comp_dir->name = DW_AT_comp_dir;
6544 comp_dir->form = DW_FORM_string;
6545 DW_STRING_IS_CANONICAL (comp_dir) = 0;
6546 DW_STRING (comp_dir) = stub_comp_dir;
6547 }
6548
6549 /* Set up for reading the DWO CU/TU. */
6550 cu->dwo_unit = dwo_unit;
6551 dwarf2_section_info *section = dwo_unit->section;
6552 section->read (objfile);
6553 abfd = section->get_bfd_owner ();
6554 begin_info_ptr = info_ptr = (section->buffer
6555 + to_underlying (dwo_unit->sect_off));
6556 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
6557
6558 if (this_cu->is_debug_types)
6559 {
6560 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
6561
6562 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6563 &cu->header, section,
6564 dwo_abbrev_section,
6565 info_ptr, rcuh_kind::TYPE);
6566 /* This is not an assert because it can be caused by bad debug info. */
6567 if (sig_type->signature != cu->header.signature)
6568 {
6569 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
6570 " TU at offset %s [in module %s]"),
6571 hex_string (sig_type->signature),
6572 hex_string (cu->header.signature),
6573 sect_offset_str (dwo_unit->sect_off),
6574 bfd_get_filename (abfd));
6575 }
6576 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6577 /* For DWOs coming from DWP files, we don't know the CU length
6578 nor the type's offset in the TU until now. */
6579 dwo_unit->length = cu->header.get_length ();
6580 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
6581
6582 /* Establish the type offset that can be used to lookup the type.
6583 For DWO files, we don't know it until now. */
6584 sig_type->type_offset_in_section
6585 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
6586 }
6587 else
6588 {
6589 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6590 &cu->header, section,
6591 dwo_abbrev_section,
6592 info_ptr, rcuh_kind::COMPILE);
6593 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6594 /* For DWOs coming from DWP files, we don't know the CU length
6595 until now. */
6596 dwo_unit->length = cu->header.get_length ();
6597 }
6598
6599 *result_dwo_abbrev_table
6600 = abbrev_table::read (objfile, dwo_abbrev_section,
6601 cu->header.abbrev_sect_off);
6602 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6603 result_dwo_abbrev_table->get ());
6604
6605 /* Read in the die, but leave space to copy over the attributes
6606 from the stub. This has the benefit of simplifying the rest of
6607 the code - all the work to maintain the illusion of a single
6608 DW_TAG_{compile,type}_unit DIE is done here. */
6609 num_extra_attrs = ((stmt_list != NULL)
6610 + (low_pc != NULL)
6611 + (high_pc != NULL)
6612 + (ranges != NULL)
6613 + (comp_dir != NULL));
6614 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
6615 num_extra_attrs);
6616
6617 /* Copy over the attributes from the stub to the DIE we just read in. */
6618 comp_unit_die = *result_comp_unit_die;
6619 i = comp_unit_die->num_attrs;
6620 if (stmt_list != NULL)
6621 comp_unit_die->attrs[i++] = *stmt_list;
6622 if (low_pc != NULL)
6623 comp_unit_die->attrs[i++] = *low_pc;
6624 if (high_pc != NULL)
6625 comp_unit_die->attrs[i++] = *high_pc;
6626 if (ranges != NULL)
6627 comp_unit_die->attrs[i++] = *ranges;
6628 if (comp_dir != NULL)
6629 comp_unit_die->attrs[i++] = *comp_dir;
6630 comp_unit_die->num_attrs += num_extra_attrs;
6631
6632 if (dwarf_die_debug)
6633 {
6634 fprintf_unfiltered (gdb_stdlog,
6635 "Read die from %s@0x%x of %s:\n",
6636 section->get_name (),
6637 (unsigned) (begin_info_ptr - section->buffer),
6638 bfd_get_filename (abfd));
6639 dump_die (comp_unit_die, dwarf_die_debug);
6640 }
6641
6642 /* Skip dummy compilation units. */
6643 if (info_ptr >= begin_info_ptr + dwo_unit->length
6644 || peek_abbrev_code (abfd, info_ptr) == 0)
6645 return 0;
6646
6647 *result_info_ptr = info_ptr;
6648 return 1;
6649 }
6650
6651 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
6652 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6653 signature is part of the header. */
6654 static gdb::optional<ULONGEST>
6655 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6656 {
6657 if (cu->header.version >= 5)
6658 return cu->header.signature;
6659 struct attribute *attr;
6660 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6661 if (attr == nullptr)
6662 return gdb::optional<ULONGEST> ();
6663 return DW_UNSND (attr);
6664 }
6665
6666 /* Subroutine of cutu_reader to simplify it.
6667 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6668 Returns NULL if the specified DWO unit cannot be found. */
6669
6670 static struct dwo_unit *
6671 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
6672 struct die_info *comp_unit_die,
6673 const char *dwo_name)
6674 {
6675 struct dwarf2_cu *cu = this_cu->cu;
6676 struct dwo_unit *dwo_unit;
6677 const char *comp_dir;
6678
6679 gdb_assert (cu != NULL);
6680
6681 /* Yeah, we look dwo_name up again, but it simplifies the code. */
6682 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6683 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
6684
6685 if (this_cu->is_debug_types)
6686 {
6687 struct signatured_type *sig_type;
6688
6689 /* Since this_cu is the first member of struct signatured_type,
6690 we can go from a pointer to one to a pointer to the other. */
6691 sig_type = (struct signatured_type *) this_cu;
6692 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
6693 }
6694 else
6695 {
6696 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
6697 if (!signature.has_value ())
6698 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6699 " [in module %s]"),
6700 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
6701 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
6702 *signature);
6703 }
6704
6705 return dwo_unit;
6706 }
6707
6708 /* Subroutine of cutu_reader to simplify it.
6709 See it for a description of the parameters.
6710 Read a TU directly from a DWO file, bypassing the stub. */
6711
6712 void
6713 cutu_reader::init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
6714 int use_existing_cu)
6715 {
6716 struct signatured_type *sig_type;
6717 struct die_reader_specs reader;
6718
6719 /* Verify we can do the following downcast, and that we have the
6720 data we need. */
6721 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6722 sig_type = (struct signatured_type *) this_cu;
6723 gdb_assert (sig_type->dwo_unit != NULL);
6724
6725 if (use_existing_cu && this_cu->cu != NULL)
6726 {
6727 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6728 /* There's no need to do the rereading_dwo_cu handling that
6729 cutu_reader does since we don't read the stub. */
6730 }
6731 else
6732 {
6733 /* If !use_existing_cu, this_cu->cu must be NULL. */
6734 gdb_assert (this_cu->cu == NULL);
6735 m_new_cu.reset (new dwarf2_cu (this_cu));
6736 }
6737
6738 /* A future optimization, if needed, would be to use an existing
6739 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6740 could share abbrev tables. */
6741
6742 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
6743 NULL /* stub_comp_unit_die */,
6744 sig_type->dwo_unit->dwo_file->comp_dir,
6745 &reader, &info_ptr,
6746 &comp_unit_die,
6747 &m_dwo_abbrev_table) == 0)
6748 {
6749 /* Dummy die. */
6750 dummy_p = true;
6751 }
6752 }
6753
6754 /* Initialize a CU (or TU) and read its DIEs.
6755 If the CU defers to a DWO file, read the DWO file as well.
6756
6757 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6758 Otherwise the table specified in the comp unit header is read in and used.
6759 This is an optimization for when we already have the abbrev table.
6760
6761 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
6762 Otherwise, a new CU is allocated with xmalloc. */
6763
6764 cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
6765 struct abbrev_table *abbrev_table,
6766 int use_existing_cu,
6767 bool skip_partial)
6768 : die_reader_specs {},
6769 m_this_cu (this_cu)
6770 {
6771 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6772 struct objfile *objfile = dwarf2_per_objfile->objfile;
6773 struct dwarf2_section_info *section = this_cu->section;
6774 bfd *abfd = section->get_bfd_owner ();
6775 struct dwarf2_cu *cu;
6776 const gdb_byte *begin_info_ptr;
6777 struct signatured_type *sig_type = NULL;
6778 struct dwarf2_section_info *abbrev_section;
6779 /* Non-zero if CU currently points to a DWO file and we need to
6780 reread it. When this happens we need to reread the skeleton die
6781 before we can reread the DWO file (this only applies to CUs, not TUs). */
6782 int rereading_dwo_cu = 0;
6783
6784 if (dwarf_die_debug)
6785 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
6786 this_cu->is_debug_types ? "type" : "comp",
6787 sect_offset_str (this_cu->sect_off));
6788
6789 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6790 file (instead of going through the stub), short-circuit all of this. */
6791 if (this_cu->reading_dwo_directly)
6792 {
6793 /* Narrow down the scope of possibilities to have to understand. */
6794 gdb_assert (this_cu->is_debug_types);
6795 gdb_assert (abbrev_table == NULL);
6796 init_tu_and_read_dwo_dies (this_cu, use_existing_cu);
6797 return;
6798 }
6799
6800 /* This is cheap if the section is already read in. */
6801 section->read (objfile);
6802
6803 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
6804
6805 abbrev_section = get_abbrev_section_for_cu (this_cu);
6806
6807 if (use_existing_cu && this_cu->cu != NULL)
6808 {
6809 cu = this_cu->cu;
6810 /* If this CU is from a DWO file we need to start over, we need to
6811 refetch the attributes from the skeleton CU.
6812 This could be optimized by retrieving those attributes from when we
6813 were here the first time: the previous comp_unit_die was stored in
6814 comp_unit_obstack. But there's no data yet that we need this
6815 optimization. */
6816 if (cu->dwo_unit != NULL)
6817 rereading_dwo_cu = 1;
6818 }
6819 else
6820 {
6821 /* If !use_existing_cu, this_cu->cu must be NULL. */
6822 gdb_assert (this_cu->cu == NULL);
6823 m_new_cu.reset (new dwarf2_cu (this_cu));
6824 cu = m_new_cu.get ();
6825 }
6826
6827 /* Get the header. */
6828 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
6829 {
6830 /* We already have the header, there's no need to read it in again. */
6831 info_ptr += to_underlying (cu->header.first_die_cu_offset);
6832 }
6833 else
6834 {
6835 if (this_cu->is_debug_types)
6836 {
6837 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6838 &cu->header, section,
6839 abbrev_section, info_ptr,
6840 rcuh_kind::TYPE);
6841
6842 /* Since per_cu is the first member of struct signatured_type,
6843 we can go from a pointer to one to a pointer to the other. */
6844 sig_type = (struct signatured_type *) this_cu;
6845 gdb_assert (sig_type->signature == cu->header.signature);
6846 gdb_assert (sig_type->type_offset_in_tu
6847 == cu->header.type_cu_offset_in_tu);
6848 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6849
6850 /* LENGTH has not been set yet for type units if we're
6851 using .gdb_index. */
6852 this_cu->length = cu->header.get_length ();
6853
6854 /* Establish the type offset that can be used to lookup the type. */
6855 sig_type->type_offset_in_section =
6856 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
6857
6858 this_cu->dwarf_version = cu->header.version;
6859 }
6860 else
6861 {
6862 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6863 &cu->header, section,
6864 abbrev_section,
6865 info_ptr,
6866 rcuh_kind::COMPILE);
6867
6868 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6869 gdb_assert (this_cu->length == cu->header.get_length ());
6870 this_cu->dwarf_version = cu->header.version;
6871 }
6872 }
6873
6874 /* Skip dummy compilation units. */
6875 if (info_ptr >= begin_info_ptr + this_cu->length
6876 || peek_abbrev_code (abfd, info_ptr) == 0)
6877 {
6878 dummy_p = true;
6879 return;
6880 }
6881
6882 /* If we don't have them yet, read the abbrevs for this compilation unit.
6883 And if we need to read them now, make sure they're freed when we're
6884 done. */
6885 if (abbrev_table != NULL)
6886 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
6887 else
6888 {
6889 m_abbrev_table_holder
6890 = abbrev_table::read (objfile, abbrev_section,
6891 cu->header.abbrev_sect_off);
6892 abbrev_table = m_abbrev_table_holder.get ();
6893 }
6894
6895 /* Read the top level CU/TU die. */
6896 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
6897 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
6898
6899 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
6900 {
6901 dummy_p = true;
6902 return;
6903 }
6904
6905 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
6906 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
6907 table from the DWO file and pass the ownership over to us. It will be
6908 referenced from READER, so we must make sure to free it after we're done
6909 with READER.
6910
6911 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
6912 DWO CU, that this test will fail (the attribute will not be present). */
6913 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6914 if (dwo_name != nullptr)
6915 {
6916 struct dwo_unit *dwo_unit;
6917 struct die_info *dwo_comp_unit_die;
6918
6919 if (comp_unit_die->has_children)
6920 {
6921 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
6922 " has children (offset %s) [in module %s]"),
6923 sect_offset_str (this_cu->sect_off),
6924 bfd_get_filename (abfd));
6925 }
6926 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die, dwo_name);
6927 if (dwo_unit != NULL)
6928 {
6929 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
6930 comp_unit_die, NULL,
6931 this, &info_ptr,
6932 &dwo_comp_unit_die,
6933 &m_dwo_abbrev_table) == 0)
6934 {
6935 /* Dummy die. */
6936 dummy_p = true;
6937 return;
6938 }
6939 comp_unit_die = dwo_comp_unit_die;
6940 }
6941 else
6942 {
6943 /* Yikes, we couldn't find the rest of the DIE, we only have
6944 the stub. A complaint has already been logged. There's
6945 not much more we can do except pass on the stub DIE to
6946 die_reader_func. We don't want to throw an error on bad
6947 debug info. */
6948 }
6949 }
6950 }
6951
6952 void
6953 cutu_reader::keep ()
6954 {
6955 /* Done, clean up. */
6956 gdb_assert (!dummy_p);
6957 if (m_new_cu != NULL)
6958 {
6959 struct dwarf2_per_objfile *dwarf2_per_objfile
6960 = m_this_cu->dwarf2_per_objfile;
6961 /* Link this CU into read_in_chain. */
6962 m_this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
6963 dwarf2_per_objfile->read_in_chain = m_this_cu;
6964 /* The chain owns it now. */
6965 m_new_cu.release ();
6966 }
6967 }
6968
6969 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
6970 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
6971 assumed to have already done the lookup to find the DWO file).
6972
6973 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
6974 THIS_CU->is_debug_types, but nothing else.
6975
6976 We fill in THIS_CU->length.
6977
6978 THIS_CU->cu is always freed when done.
6979 This is done in order to not leave THIS_CU->cu in a state where we have
6980 to care whether it refers to the "main" CU or the DWO CU.
6981
6982 When parent_cu is passed, it is used to provide a default value for
6983 str_offsets_base and addr_base from the parent. */
6984
6985 cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
6986 struct dwarf2_cu *parent_cu,
6987 struct dwo_file *dwo_file)
6988 : die_reader_specs {},
6989 m_this_cu (this_cu)
6990 {
6991 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6992 struct objfile *objfile = dwarf2_per_objfile->objfile;
6993 struct dwarf2_section_info *section = this_cu->section;
6994 bfd *abfd = section->get_bfd_owner ();
6995 struct dwarf2_section_info *abbrev_section;
6996 const gdb_byte *begin_info_ptr, *info_ptr;
6997
6998 if (dwarf_die_debug)
6999 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7000 this_cu->is_debug_types ? "type" : "comp",
7001 sect_offset_str (this_cu->sect_off));
7002
7003 gdb_assert (this_cu->cu == NULL);
7004
7005 abbrev_section = (dwo_file != NULL
7006 ? &dwo_file->sections.abbrev
7007 : get_abbrev_section_for_cu (this_cu));
7008
7009 /* This is cheap if the section is already read in. */
7010 section->read (objfile);
7011
7012 m_new_cu.reset (new dwarf2_cu (this_cu));
7013
7014 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7015 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7016 &m_new_cu->header, section,
7017 abbrev_section, info_ptr,
7018 (this_cu->is_debug_types
7019 ? rcuh_kind::TYPE
7020 : rcuh_kind::COMPILE));
7021
7022 if (parent_cu != nullptr)
7023 {
7024 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7025 m_new_cu->addr_base = parent_cu->addr_base;
7026 }
7027 this_cu->length = m_new_cu->header.get_length ();
7028
7029 /* Skip dummy compilation units. */
7030 if (info_ptr >= begin_info_ptr + this_cu->length
7031 || peek_abbrev_code (abfd, info_ptr) == 0)
7032 {
7033 dummy_p = true;
7034 return;
7035 }
7036
7037 m_abbrev_table_holder
7038 = abbrev_table::read (objfile, abbrev_section,
7039 m_new_cu->header.abbrev_sect_off);
7040
7041 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7042 m_abbrev_table_holder.get ());
7043 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7044 }
7045
7046 \f
7047 /* Type Unit Groups.
7048
7049 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7050 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7051 so that all types coming from the same compilation (.o file) are grouped
7052 together. A future step could be to put the types in the same symtab as
7053 the CU the types ultimately came from. */
7054
7055 static hashval_t
7056 hash_type_unit_group (const void *item)
7057 {
7058 const struct type_unit_group *tu_group
7059 = (const struct type_unit_group *) item;
7060
7061 return hash_stmt_list_entry (&tu_group->hash);
7062 }
7063
7064 static int
7065 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
7066 {
7067 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7068 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
7069
7070 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
7071 }
7072
7073 /* Allocate a hash table for type unit groups. */
7074
7075 static htab_up
7076 allocate_type_unit_groups_table ()
7077 {
7078 return htab_up (htab_create_alloc (3,
7079 hash_type_unit_group,
7080 eq_type_unit_group,
7081 NULL, xcalloc, xfree));
7082 }
7083
7084 /* Type units that don't have DW_AT_stmt_list are grouped into their own
7085 partial symtabs. We combine several TUs per psymtab to not let the size
7086 of any one psymtab grow too big. */
7087 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7088 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
7089
7090 /* Helper routine for get_type_unit_group.
7091 Create the type_unit_group object used to hold one or more TUs. */
7092
7093 static struct type_unit_group *
7094 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
7095 {
7096 struct dwarf2_per_objfile *dwarf2_per_objfile
7097 = cu->per_cu->dwarf2_per_objfile;
7098 struct objfile *objfile = dwarf2_per_objfile->objfile;
7099 struct dwarf2_per_cu_data *per_cu;
7100 struct type_unit_group *tu_group;
7101
7102 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7103 struct type_unit_group);
7104 per_cu = &tu_group->per_cu;
7105 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
7106
7107 if (dwarf2_per_objfile->using_index)
7108 {
7109 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7110 struct dwarf2_per_cu_quick_data);
7111 }
7112 else
7113 {
7114 unsigned int line_offset = to_underlying (line_offset_struct);
7115 dwarf2_psymtab *pst;
7116 std::string name;
7117
7118 /* Give the symtab a useful name for debug purposes. */
7119 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7120 name = string_printf ("<type_units_%d>",
7121 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7122 else
7123 name = string_printf ("<type_units_at_0x%x>", line_offset);
7124
7125 pst = create_partial_symtab (per_cu, name.c_str ());
7126 pst->anonymous = true;
7127 }
7128
7129 tu_group->hash.dwo_unit = cu->dwo_unit;
7130 tu_group->hash.line_sect_off = line_offset_struct;
7131
7132 return tu_group;
7133 }
7134
7135 /* Look up the type_unit_group for type unit CU, and create it if necessary.
7136 STMT_LIST is a DW_AT_stmt_list attribute. */
7137
7138 static struct type_unit_group *
7139 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7140 {
7141 struct dwarf2_per_objfile *dwarf2_per_objfile
7142 = cu->per_cu->dwarf2_per_objfile;
7143 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7144 struct type_unit_group *tu_group;
7145 void **slot;
7146 unsigned int line_offset;
7147 struct type_unit_group type_unit_group_for_lookup;
7148
7149 if (dwarf2_per_objfile->type_unit_groups == NULL)
7150 dwarf2_per_objfile->type_unit_groups = allocate_type_unit_groups_table ();
7151
7152 /* Do we need to create a new group, or can we use an existing one? */
7153
7154 if (stmt_list)
7155 {
7156 line_offset = DW_UNSND (stmt_list);
7157 ++tu_stats->nr_symtab_sharers;
7158 }
7159 else
7160 {
7161 /* Ugh, no stmt_list. Rare, but we have to handle it.
7162 We can do various things here like create one group per TU or
7163 spread them over multiple groups to split up the expansion work.
7164 To avoid worst case scenarios (too many groups or too large groups)
7165 we, umm, group them in bunches. */
7166 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7167 | (tu_stats->nr_stmt_less_type_units
7168 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7169 ++tu_stats->nr_stmt_less_type_units;
7170 }
7171
7172 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7173 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7174 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups.get (),
7175 &type_unit_group_for_lookup, INSERT);
7176 if (*slot != NULL)
7177 {
7178 tu_group = (struct type_unit_group *) *slot;
7179 gdb_assert (tu_group != NULL);
7180 }
7181 else
7182 {
7183 sect_offset line_offset_struct = (sect_offset) line_offset;
7184 tu_group = create_type_unit_group (cu, line_offset_struct);
7185 *slot = tu_group;
7186 ++tu_stats->nr_symtabs;
7187 }
7188
7189 return tu_group;
7190 }
7191 \f
7192 /* Partial symbol tables. */
7193
7194 /* Create a psymtab named NAME and assign it to PER_CU.
7195
7196 The caller must fill in the following details:
7197 dirname, textlow, texthigh. */
7198
7199 static dwarf2_psymtab *
7200 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
7201 {
7202 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
7203 dwarf2_psymtab *pst;
7204
7205 pst = new dwarf2_psymtab (name, objfile, 0);
7206
7207 pst->psymtabs_addrmap_supported = true;
7208
7209 /* This is the glue that links PST into GDB's symbol API. */
7210 pst->per_cu_data = per_cu;
7211 per_cu->v.psymtab = pst;
7212
7213 return pst;
7214 }
7215
7216 /* DIE reader function for process_psymtab_comp_unit. */
7217
7218 static void
7219 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
7220 const gdb_byte *info_ptr,
7221 struct die_info *comp_unit_die,
7222 enum language pretend_language)
7223 {
7224 struct dwarf2_cu *cu = reader->cu;
7225 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
7226 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7227 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7228 CORE_ADDR baseaddr;
7229 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7230 dwarf2_psymtab *pst;
7231 enum pc_bounds_kind cu_bounds_kind;
7232 const char *filename;
7233
7234 gdb_assert (! per_cu->is_debug_types);
7235
7236 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
7237
7238 /* Allocate a new partial symbol table structure. */
7239 gdb::unique_xmalloc_ptr<char> debug_filename;
7240 static const char artificial[] = "<artificial>";
7241 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7242 if (filename == NULL)
7243 filename = "";
7244 else if (strcmp (filename, artificial) == 0)
7245 {
7246 debug_filename.reset (concat (artificial, "@",
7247 sect_offset_str (per_cu->sect_off),
7248 (char *) NULL));
7249 filename = debug_filename.get ();
7250 }
7251
7252 pst = create_partial_symtab (per_cu, filename);
7253
7254 /* This must be done before calling dwarf2_build_include_psymtabs. */
7255 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7256
7257 baseaddr = objfile->text_section_offset ();
7258
7259 dwarf2_find_base_address (comp_unit_die, cu);
7260
7261 /* Possibly set the default values of LOWPC and HIGHPC from
7262 `DW_AT_ranges'. */
7263 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7264 &best_highpc, cu, pst);
7265 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
7266 {
7267 CORE_ADDR low
7268 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7269 - baseaddr);
7270 CORE_ADDR high
7271 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7272 - baseaddr - 1);
7273 /* Store the contiguous range if it is not empty; it can be
7274 empty for CUs with no code. */
7275 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7276 low, high, pst);
7277 }
7278
7279 /* Check if comp unit has_children.
7280 If so, read the rest of the partial symbols from this comp unit.
7281 If not, there's no more debug_info for this comp unit. */
7282 if (comp_unit_die->has_children)
7283 {
7284 struct partial_die_info *first_die;
7285 CORE_ADDR lowpc, highpc;
7286
7287 lowpc = ((CORE_ADDR) -1);
7288 highpc = ((CORE_ADDR) 0);
7289
7290 first_die = load_partial_dies (reader, info_ptr, 1);
7291
7292 scan_partial_symbols (first_die, &lowpc, &highpc,
7293 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
7294
7295 /* If we didn't find a lowpc, set it to highpc to avoid
7296 complaints from `maint check'. */
7297 if (lowpc == ((CORE_ADDR) -1))
7298 lowpc = highpc;
7299
7300 /* If the compilation unit didn't have an explicit address range,
7301 then use the information extracted from its child dies. */
7302 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
7303 {
7304 best_lowpc = lowpc;
7305 best_highpc = highpc;
7306 }
7307 }
7308 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
7309 best_lowpc + baseaddr)
7310 - baseaddr);
7311 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
7312 best_highpc + baseaddr)
7313 - baseaddr);
7314
7315 end_psymtab_common (objfile, pst);
7316
7317 if (!cu->per_cu->imported_symtabs_empty ())
7318 {
7319 int i;
7320 int len = cu->per_cu->imported_symtabs_size ();
7321
7322 /* Fill in 'dependencies' here; we fill in 'users' in a
7323 post-pass. */
7324 pst->number_of_dependencies = len;
7325 pst->dependencies
7326 = objfile->partial_symtabs->allocate_dependencies (len);
7327 for (i = 0; i < len; ++i)
7328 {
7329 pst->dependencies[i]
7330 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7331 }
7332
7333 cu->per_cu->imported_symtabs_free ();
7334 }
7335
7336 /* Get the list of files included in the current compilation unit,
7337 and build a psymtab for each of them. */
7338 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7339
7340 if (dwarf_read_debug)
7341 fprintf_unfiltered (gdb_stdlog,
7342 "Psymtab for %s unit @%s: %s - %s"
7343 ", %d global, %d static syms\n",
7344 per_cu->is_debug_types ? "type" : "comp",
7345 sect_offset_str (per_cu->sect_off),
7346 paddress (gdbarch, pst->text_low (objfile)),
7347 paddress (gdbarch, pst->text_high (objfile)),
7348 pst->n_global_syms, pst->n_static_syms);
7349 }
7350
7351 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7352 Process compilation unit THIS_CU for a psymtab. */
7353
7354 static void
7355 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
7356 bool want_partial_unit,
7357 enum language pretend_language)
7358 {
7359 /* If this compilation unit was already read in, free the
7360 cached copy in order to read it in again. This is
7361 necessary because we skipped some symbols when we first
7362 read in the compilation unit (see load_partial_dies).
7363 This problem could be avoided, but the benefit is unclear. */
7364 if (this_cu->cu != NULL)
7365 free_one_cached_comp_unit (this_cu);
7366
7367 cutu_reader reader (this_cu, NULL, 0, false);
7368
7369 if (reader.dummy_p)
7370 {
7371 /* Nothing. */
7372 }
7373 else if (this_cu->is_debug_types)
7374 build_type_psymtabs_reader (&reader, reader.info_ptr,
7375 reader.comp_unit_die);
7376 else if (want_partial_unit
7377 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
7378 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7379 reader.comp_unit_die,
7380 pretend_language);
7381
7382 /* Age out any secondary CUs. */
7383 age_cached_comp_units (this_cu->dwarf2_per_objfile);
7384 }
7385
7386 /* Reader function for build_type_psymtabs. */
7387
7388 static void
7389 build_type_psymtabs_reader (const struct die_reader_specs *reader,
7390 const gdb_byte *info_ptr,
7391 struct die_info *type_unit_die)
7392 {
7393 struct dwarf2_per_objfile *dwarf2_per_objfile
7394 = reader->cu->per_cu->dwarf2_per_objfile;
7395 struct objfile *objfile = dwarf2_per_objfile->objfile;
7396 struct dwarf2_cu *cu = reader->cu;
7397 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7398 struct signatured_type *sig_type;
7399 struct type_unit_group *tu_group;
7400 struct attribute *attr;
7401 struct partial_die_info *first_die;
7402 CORE_ADDR lowpc, highpc;
7403 dwarf2_psymtab *pst;
7404
7405 gdb_assert (per_cu->is_debug_types);
7406 sig_type = (struct signatured_type *) per_cu;
7407
7408 if (! type_unit_die->has_children)
7409 return;
7410
7411 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
7412 tu_group = get_type_unit_group (cu, attr);
7413
7414 if (tu_group->tus == nullptr)
7415 tu_group->tus = new std::vector<signatured_type *>;
7416 tu_group->tus->push_back (sig_type);
7417
7418 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7419 pst = create_partial_symtab (per_cu, "");
7420 pst->anonymous = true;
7421
7422 first_die = load_partial_dies (reader, info_ptr, 1);
7423
7424 lowpc = (CORE_ADDR) -1;
7425 highpc = (CORE_ADDR) 0;
7426 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7427
7428 end_psymtab_common (objfile, pst);
7429 }
7430
7431 /* Struct used to sort TUs by their abbreviation table offset. */
7432
7433 struct tu_abbrev_offset
7434 {
7435 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7436 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7437 {}
7438
7439 signatured_type *sig_type;
7440 sect_offset abbrev_offset;
7441 };
7442
7443 /* Helper routine for build_type_psymtabs_1, passed to std::sort. */
7444
7445 static bool
7446 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7447 const struct tu_abbrev_offset &b)
7448 {
7449 return a.abbrev_offset < b.abbrev_offset;
7450 }
7451
7452 /* Efficiently read all the type units.
7453 This does the bulk of the work for build_type_psymtabs.
7454
7455 The efficiency is because we sort TUs by the abbrev table they use and
7456 only read each abbrev table once. In one program there are 200K TUs
7457 sharing 8K abbrev tables.
7458
7459 The main purpose of this function is to support building the
7460 dwarf2_per_objfile->type_unit_groups table.
7461 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7462 can collapse the search space by grouping them by stmt_list.
7463 The savings can be significant, in the same program from above the 200K TUs
7464 share 8K stmt_list tables.
7465
7466 FUNC is expected to call get_type_unit_group, which will create the
7467 struct type_unit_group if necessary and add it to
7468 dwarf2_per_objfile->type_unit_groups. */
7469
7470 static void
7471 build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
7472 {
7473 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7474 abbrev_table_up abbrev_table;
7475 sect_offset abbrev_offset;
7476
7477 /* It's up to the caller to not call us multiple times. */
7478 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
7479
7480 if (dwarf2_per_objfile->all_type_units.empty ())
7481 return;
7482
7483 /* TUs typically share abbrev tables, and there can be way more TUs than
7484 abbrev tables. Sort by abbrev table to reduce the number of times we
7485 read each abbrev table in.
7486 Alternatives are to punt or to maintain a cache of abbrev tables.
7487 This is simpler and efficient enough for now.
7488
7489 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7490 symtab to use). Typically TUs with the same abbrev offset have the same
7491 stmt_list value too so in practice this should work well.
7492
7493 The basic algorithm here is:
7494
7495 sort TUs by abbrev table
7496 for each TU with same abbrev table:
7497 read abbrev table if first user
7498 read TU top level DIE
7499 [IWBN if DWO skeletons had DW_AT_stmt_list]
7500 call FUNC */
7501
7502 if (dwarf_read_debug)
7503 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
7504
7505 /* Sort in a separate table to maintain the order of all_type_units
7506 for .gdb_index: TU indices directly index all_type_units. */
7507 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7508 sorted_by_abbrev.reserve (dwarf2_per_objfile->all_type_units.size ());
7509
7510 for (signatured_type *sig_type : dwarf2_per_objfile->all_type_units)
7511 sorted_by_abbrev.emplace_back
7512 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
7513 sig_type->per_cu.section,
7514 sig_type->per_cu.sect_off));
7515
7516 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7517 sort_tu_by_abbrev_offset);
7518
7519 abbrev_offset = (sect_offset) ~(unsigned) 0;
7520
7521 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
7522 {
7523 /* Switch to the next abbrev table if necessary. */
7524 if (abbrev_table == NULL
7525 || tu.abbrev_offset != abbrev_offset)
7526 {
7527 abbrev_offset = tu.abbrev_offset;
7528 abbrev_table =
7529 abbrev_table::read (dwarf2_per_objfile->objfile,
7530 &dwarf2_per_objfile->abbrev,
7531 abbrev_offset);
7532 ++tu_stats->nr_uniq_abbrev_tables;
7533 }
7534
7535 cutu_reader reader (&tu.sig_type->per_cu, abbrev_table.get (),
7536 0, false);
7537 if (!reader.dummy_p)
7538 build_type_psymtabs_reader (&reader, reader.info_ptr,
7539 reader.comp_unit_die);
7540 }
7541 }
7542
7543 /* Print collected type unit statistics. */
7544
7545 static void
7546 print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
7547 {
7548 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7549
7550 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
7551 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
7552 dwarf2_per_objfile->all_type_units.size ());
7553 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
7554 tu_stats->nr_uniq_abbrev_tables);
7555 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
7556 tu_stats->nr_symtabs);
7557 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
7558 tu_stats->nr_symtab_sharers);
7559 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
7560 tu_stats->nr_stmt_less_type_units);
7561 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
7562 tu_stats->nr_all_type_units_reallocs);
7563 }
7564
7565 /* Traversal function for build_type_psymtabs. */
7566
7567 static int
7568 build_type_psymtab_dependencies (void **slot, void *info)
7569 {
7570 struct dwarf2_per_objfile *dwarf2_per_objfile
7571 = (struct dwarf2_per_objfile *) info;
7572 struct objfile *objfile = dwarf2_per_objfile->objfile;
7573 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
7574 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
7575 dwarf2_psymtab *pst = per_cu->v.psymtab;
7576 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
7577 int i;
7578
7579 gdb_assert (len > 0);
7580 gdb_assert (per_cu->type_unit_group_p ());
7581
7582 pst->number_of_dependencies = len;
7583 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
7584 for (i = 0; i < len; ++i)
7585 {
7586 struct signatured_type *iter = tu_group->tus->at (i);
7587 gdb_assert (iter->per_cu.is_debug_types);
7588 pst->dependencies[i] = iter->per_cu.v.psymtab;
7589 iter->type_unit_group = tu_group;
7590 }
7591
7592 delete tu_group->tus;
7593 tu_group->tus = nullptr;
7594
7595 return 1;
7596 }
7597
7598 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7599 Build partial symbol tables for the .debug_types comp-units. */
7600
7601 static void
7602 build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
7603 {
7604 if (! create_all_type_units (dwarf2_per_objfile))
7605 return;
7606
7607 build_type_psymtabs_1 (dwarf2_per_objfile);
7608 }
7609
7610 /* Traversal function for process_skeletonless_type_unit.
7611 Read a TU in a DWO file and build partial symbols for it. */
7612
7613 static int
7614 process_skeletonless_type_unit (void **slot, void *info)
7615 {
7616 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
7617 struct dwarf2_per_objfile *dwarf2_per_objfile
7618 = (struct dwarf2_per_objfile *) info;
7619 struct signatured_type find_entry, *entry;
7620
7621 /* If this TU doesn't exist in the global table, add it and read it in. */
7622
7623 if (dwarf2_per_objfile->signatured_types == NULL)
7624 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
7625
7626 find_entry.signature = dwo_unit->signature;
7627 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
7628 &find_entry, INSERT);
7629 /* If we've already seen this type there's nothing to do. What's happening
7630 is we're doing our own version of comdat-folding here. */
7631 if (*slot != NULL)
7632 return 1;
7633
7634 /* This does the job that create_all_type_units would have done for
7635 this TU. */
7636 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
7637 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
7638 *slot = entry;
7639
7640 /* This does the job that build_type_psymtabs_1 would have done. */
7641 cutu_reader reader (&entry->per_cu, NULL, 0, false);
7642 if (!reader.dummy_p)
7643 build_type_psymtabs_reader (&reader, reader.info_ptr,
7644 reader.comp_unit_die);
7645
7646 return 1;
7647 }
7648
7649 /* Traversal function for process_skeletonless_type_units. */
7650
7651 static int
7652 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7653 {
7654 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7655
7656 if (dwo_file->tus != NULL)
7657 htab_traverse_noresize (dwo_file->tus.get (),
7658 process_skeletonless_type_unit, info);
7659
7660 return 1;
7661 }
7662
7663 /* Scan all TUs of DWO files, verifying we've processed them.
7664 This is needed in case a TU was emitted without its skeleton.
7665 Note: This can't be done until we know what all the DWO files are. */
7666
7667 static void
7668 process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
7669 {
7670 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
7671 if (get_dwp_file (dwarf2_per_objfile) == NULL
7672 && dwarf2_per_objfile->dwo_files != NULL)
7673 {
7674 htab_traverse_noresize (dwarf2_per_objfile->dwo_files.get (),
7675 process_dwo_file_for_skeletonless_type_units,
7676 dwarf2_per_objfile);
7677 }
7678 }
7679
7680 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
7681
7682 static void
7683 set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
7684 {
7685 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
7686 {
7687 dwarf2_psymtab *pst = per_cu->v.psymtab;
7688
7689 if (pst == NULL)
7690 continue;
7691
7692 for (int j = 0; j < pst->number_of_dependencies; ++j)
7693 {
7694 /* Set the 'user' field only if it is not already set. */
7695 if (pst->dependencies[j]->user == NULL)
7696 pst->dependencies[j]->user = pst;
7697 }
7698 }
7699 }
7700
7701 /* Build the partial symbol table by doing a quick pass through the
7702 .debug_info and .debug_abbrev sections. */
7703
7704 static void
7705 dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
7706 {
7707 struct objfile *objfile = dwarf2_per_objfile->objfile;
7708
7709 if (dwarf_read_debug)
7710 {
7711 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
7712 objfile_name (objfile));
7713 }
7714
7715 scoped_restore restore_reading_psyms
7716 = make_scoped_restore (&dwarf2_per_objfile->reading_partial_symbols,
7717 true);
7718
7719 dwarf2_per_objfile->info.read (objfile);
7720
7721 /* Any cached compilation units will be linked by the per-objfile
7722 read_in_chain. Make sure to free them when we're done. */
7723 free_cached_comp_units freer (dwarf2_per_objfile);
7724
7725 build_type_psymtabs (dwarf2_per_objfile);
7726
7727 create_all_comp_units (dwarf2_per_objfile);
7728
7729 /* Create a temporary address map on a temporary obstack. We later
7730 copy this to the final obstack. */
7731 auto_obstack temp_obstack;
7732
7733 scoped_restore save_psymtabs_addrmap
7734 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
7735 addrmap_create_mutable (&temp_obstack));
7736
7737 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
7738 process_psymtab_comp_unit (per_cu, false, language_minimal);
7739
7740 /* This has to wait until we read the CUs, we need the list of DWOs. */
7741 process_skeletonless_type_units (dwarf2_per_objfile);
7742
7743 /* Now that all TUs have been processed we can fill in the dependencies. */
7744 if (dwarf2_per_objfile->type_unit_groups != NULL)
7745 {
7746 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups.get (),
7747 build_type_psymtab_dependencies, dwarf2_per_objfile);
7748 }
7749
7750 if (dwarf_read_debug)
7751 print_tu_stats (dwarf2_per_objfile);
7752
7753 set_partial_user (dwarf2_per_objfile);
7754
7755 objfile->partial_symtabs->psymtabs_addrmap
7756 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
7757 objfile->partial_symtabs->obstack ());
7758 /* At this point we want to keep the address map. */
7759 save_psymtabs_addrmap.release ();
7760
7761 if (dwarf_read_debug)
7762 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
7763 objfile_name (objfile));
7764 }
7765
7766 /* Load the partial DIEs for a secondary CU into memory.
7767 This is also used when rereading a primary CU with load_all_dies. */
7768
7769 static void
7770 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
7771 {
7772 cutu_reader reader (this_cu, NULL, 1, false);
7773
7774 if (!reader.dummy_p)
7775 {
7776 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
7777 language_minimal);
7778
7779 /* Check if comp unit has_children.
7780 If so, read the rest of the partial symbols from this comp unit.
7781 If not, there's no more debug_info for this comp unit. */
7782 if (reader.comp_unit_die->has_children)
7783 load_partial_dies (&reader, reader.info_ptr, 0);
7784
7785 reader.keep ();
7786 }
7787 }
7788
7789 static void
7790 read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
7791 struct dwarf2_section_info *section,
7792 struct dwarf2_section_info *abbrev_section,
7793 unsigned int is_dwz)
7794 {
7795 const gdb_byte *info_ptr;
7796 struct objfile *objfile = dwarf2_per_objfile->objfile;
7797
7798 if (dwarf_read_debug)
7799 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
7800 section->get_name (),
7801 section->get_file_name ());
7802
7803 section->read (objfile);
7804
7805 info_ptr = section->buffer;
7806
7807 while (info_ptr < section->buffer + section->size)
7808 {
7809 struct dwarf2_per_cu_data *this_cu;
7810
7811 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
7812
7813 comp_unit_head cu_header;
7814 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
7815 abbrev_section, info_ptr,
7816 rcuh_kind::COMPILE);
7817
7818 /* Save the compilation unit for later lookup. */
7819 if (cu_header.unit_type != DW_UT_type)
7820 {
7821 this_cu = XOBNEW (&objfile->objfile_obstack,
7822 struct dwarf2_per_cu_data);
7823 memset (this_cu, 0, sizeof (*this_cu));
7824 }
7825 else
7826 {
7827 auto sig_type = XOBNEW (&objfile->objfile_obstack,
7828 struct signatured_type);
7829 memset (sig_type, 0, sizeof (*sig_type));
7830 sig_type->signature = cu_header.signature;
7831 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
7832 this_cu = &sig_type->per_cu;
7833 }
7834 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
7835 this_cu->sect_off = sect_off;
7836 this_cu->length = cu_header.length + cu_header.initial_length_size;
7837 this_cu->is_dwz = is_dwz;
7838 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
7839 this_cu->section = section;
7840
7841 dwarf2_per_objfile->all_comp_units.push_back (this_cu);
7842
7843 info_ptr = info_ptr + this_cu->length;
7844 }
7845 }
7846
7847 /* Create a list of all compilation units in OBJFILE.
7848 This is only done for -readnow and building partial symtabs. */
7849
7850 static void
7851 create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
7852 {
7853 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
7854 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
7855 &dwarf2_per_objfile->abbrev, 0);
7856
7857 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
7858 if (dwz != NULL)
7859 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
7860 1);
7861 }
7862
7863 /* Process all loaded DIEs for compilation unit CU, starting at
7864 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
7865 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
7866 DW_AT_ranges). See the comments of add_partial_subprogram on how
7867 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
7868
7869 static void
7870 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
7871 CORE_ADDR *highpc, int set_addrmap,
7872 struct dwarf2_cu *cu)
7873 {
7874 struct partial_die_info *pdi;
7875
7876 /* Now, march along the PDI's, descending into ones which have
7877 interesting children but skipping the children of the other ones,
7878 until we reach the end of the compilation unit. */
7879
7880 pdi = first_die;
7881
7882 while (pdi != NULL)
7883 {
7884 pdi->fixup (cu);
7885
7886 /* Anonymous namespaces or modules have no name but have interesting
7887 children, so we need to look at them. Ditto for anonymous
7888 enums. */
7889
7890 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
7891 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
7892 || pdi->tag == DW_TAG_imported_unit
7893 || pdi->tag == DW_TAG_inlined_subroutine)
7894 {
7895 switch (pdi->tag)
7896 {
7897 case DW_TAG_subprogram:
7898 case DW_TAG_inlined_subroutine:
7899 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
7900 break;
7901 case DW_TAG_constant:
7902 case DW_TAG_variable:
7903 case DW_TAG_typedef:
7904 case DW_TAG_union_type:
7905 if (!pdi->is_declaration)
7906 {
7907 add_partial_symbol (pdi, cu);
7908 }
7909 break;
7910 case DW_TAG_class_type:
7911 case DW_TAG_interface_type:
7912 case DW_TAG_structure_type:
7913 if (!pdi->is_declaration)
7914 {
7915 add_partial_symbol (pdi, cu);
7916 }
7917 if ((cu->language == language_rust
7918 || cu->language == language_cplus) && pdi->has_children)
7919 scan_partial_symbols (pdi->die_child, lowpc, highpc,
7920 set_addrmap, cu);
7921 break;
7922 case DW_TAG_enumeration_type:
7923 if (!pdi->is_declaration)
7924 add_partial_enumeration (pdi, cu);
7925 break;
7926 case DW_TAG_base_type:
7927 case DW_TAG_subrange_type:
7928 /* File scope base type definitions are added to the partial
7929 symbol table. */
7930 add_partial_symbol (pdi, cu);
7931 break;
7932 case DW_TAG_namespace:
7933 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
7934 break;
7935 case DW_TAG_module:
7936 if (!pdi->is_declaration)
7937 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
7938 break;
7939 case DW_TAG_imported_unit:
7940 {
7941 struct dwarf2_per_cu_data *per_cu;
7942
7943 /* For now we don't handle imported units in type units. */
7944 if (cu->per_cu->is_debug_types)
7945 {
7946 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7947 " supported in type units [in module %s]"),
7948 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
7949 }
7950
7951 per_cu = dwarf2_find_containing_comp_unit
7952 (pdi->d.sect_off, pdi->is_dwz,
7953 cu->per_cu->dwarf2_per_objfile);
7954
7955 /* Go read the partial unit, if needed. */
7956 if (per_cu->v.psymtab == NULL)
7957 process_psymtab_comp_unit (per_cu, true, cu->language);
7958
7959 cu->per_cu->imported_symtabs_push (per_cu);
7960 }
7961 break;
7962 case DW_TAG_imported_declaration:
7963 add_partial_symbol (pdi, cu);
7964 break;
7965 default:
7966 break;
7967 }
7968 }
7969
7970 /* If the die has a sibling, skip to the sibling. */
7971
7972 pdi = pdi->die_sibling;
7973 }
7974 }
7975
7976 /* Functions used to compute the fully scoped name of a partial DIE.
7977
7978 Normally, this is simple. For C++, the parent DIE's fully scoped
7979 name is concatenated with "::" and the partial DIE's name.
7980 Enumerators are an exception; they use the scope of their parent
7981 enumeration type, i.e. the name of the enumeration type is not
7982 prepended to the enumerator.
7983
7984 There are two complexities. One is DW_AT_specification; in this
7985 case "parent" means the parent of the target of the specification,
7986 instead of the direct parent of the DIE. The other is compilers
7987 which do not emit DW_TAG_namespace; in this case we try to guess
7988 the fully qualified name of structure types from their members'
7989 linkage names. This must be done using the DIE's children rather
7990 than the children of any DW_AT_specification target. We only need
7991 to do this for structures at the top level, i.e. if the target of
7992 any DW_AT_specification (if any; otherwise the DIE itself) does not
7993 have a parent. */
7994
7995 /* Compute the scope prefix associated with PDI's parent, in
7996 compilation unit CU. The result will be allocated on CU's
7997 comp_unit_obstack, or a copy of the already allocated PDI->NAME
7998 field. NULL is returned if no prefix is necessary. */
7999 static const char *
8000 partial_die_parent_scope (struct partial_die_info *pdi,
8001 struct dwarf2_cu *cu)
8002 {
8003 const char *grandparent_scope;
8004 struct partial_die_info *parent, *real_pdi;
8005
8006 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8007 then this means the parent of the specification DIE. */
8008
8009 real_pdi = pdi;
8010 while (real_pdi->has_specification)
8011 {
8012 auto res = find_partial_die (real_pdi->spec_offset,
8013 real_pdi->spec_is_dwz, cu);
8014 real_pdi = res.pdi;
8015 cu = res.cu;
8016 }
8017
8018 parent = real_pdi->die_parent;
8019 if (parent == NULL)
8020 return NULL;
8021
8022 if (parent->scope_set)
8023 return parent->scope;
8024
8025 parent->fixup (cu);
8026
8027 grandparent_scope = partial_die_parent_scope (parent, cu);
8028
8029 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8030 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8031 Work around this problem here. */
8032 if (cu->language == language_cplus
8033 && parent->tag == DW_TAG_namespace
8034 && strcmp (parent->name, "::") == 0
8035 && grandparent_scope == NULL)
8036 {
8037 parent->scope = NULL;
8038 parent->scope_set = 1;
8039 return NULL;
8040 }
8041
8042 /* Nested subroutines in Fortran get a prefix. */
8043 if (pdi->tag == DW_TAG_enumerator)
8044 /* Enumerators should not get the name of the enumeration as a prefix. */
8045 parent->scope = grandparent_scope;
8046 else if (parent->tag == DW_TAG_namespace
8047 || parent->tag == DW_TAG_module
8048 || parent->tag == DW_TAG_structure_type
8049 || parent->tag == DW_TAG_class_type
8050 || parent->tag == DW_TAG_interface_type
8051 || parent->tag == DW_TAG_union_type
8052 || parent->tag == DW_TAG_enumeration_type
8053 || (cu->language == language_fortran
8054 && parent->tag == DW_TAG_subprogram
8055 && pdi->tag == DW_TAG_subprogram))
8056 {
8057 if (grandparent_scope == NULL)
8058 parent->scope = parent->name;
8059 else
8060 parent->scope = typename_concat (&cu->comp_unit_obstack,
8061 grandparent_scope,
8062 parent->name, 0, cu);
8063 }
8064 else
8065 {
8066 /* FIXME drow/2004-04-01: What should we be doing with
8067 function-local names? For partial symbols, we should probably be
8068 ignoring them. */
8069 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8070 dwarf_tag_name (parent->tag),
8071 sect_offset_str (pdi->sect_off));
8072 parent->scope = grandparent_scope;
8073 }
8074
8075 parent->scope_set = 1;
8076 return parent->scope;
8077 }
8078
8079 /* Return the fully scoped name associated with PDI, from compilation unit
8080 CU. The result will be allocated with malloc. */
8081
8082 static gdb::unique_xmalloc_ptr<char>
8083 partial_die_full_name (struct partial_die_info *pdi,
8084 struct dwarf2_cu *cu)
8085 {
8086 const char *parent_scope;
8087
8088 /* If this is a template instantiation, we can not work out the
8089 template arguments from partial DIEs. So, unfortunately, we have
8090 to go through the full DIEs. At least any work we do building
8091 types here will be reused if full symbols are loaded later. */
8092 if (pdi->has_template_arguments)
8093 {
8094 pdi->fixup (cu);
8095
8096 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8097 {
8098 struct die_info *die;
8099 struct attribute attr;
8100 struct dwarf2_cu *ref_cu = cu;
8101
8102 /* DW_FORM_ref_addr is using section offset. */
8103 attr.name = (enum dwarf_attribute) 0;
8104 attr.form = DW_FORM_ref_addr;
8105 attr.u.unsnd = to_underlying (pdi->sect_off);
8106 die = follow_die_ref (NULL, &attr, &ref_cu);
8107
8108 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8109 }
8110 }
8111
8112 parent_scope = partial_die_parent_scope (pdi, cu);
8113 if (parent_scope == NULL)
8114 return NULL;
8115 else
8116 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8117 pdi->name, 0, cu));
8118 }
8119
8120 static void
8121 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8122 {
8123 struct dwarf2_per_objfile *dwarf2_per_objfile
8124 = cu->per_cu->dwarf2_per_objfile;
8125 struct objfile *objfile = dwarf2_per_objfile->objfile;
8126 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8127 CORE_ADDR addr = 0;
8128 const char *actual_name = NULL;
8129 CORE_ADDR baseaddr;
8130
8131 baseaddr = objfile->text_section_offset ();
8132
8133 gdb::unique_xmalloc_ptr<char> built_actual_name
8134 = partial_die_full_name (pdi, cu);
8135 if (built_actual_name != NULL)
8136 actual_name = built_actual_name.get ();
8137
8138 if (actual_name == NULL)
8139 actual_name = pdi->name;
8140
8141 switch (pdi->tag)
8142 {
8143 case DW_TAG_inlined_subroutine:
8144 case DW_TAG_subprogram:
8145 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8146 - baseaddr);
8147 if (pdi->is_external
8148 || cu->language == language_ada
8149 || (cu->language == language_fortran
8150 && pdi->die_parent != NULL
8151 && pdi->die_parent->tag == DW_TAG_subprogram))
8152 {
8153 /* Normally, only "external" DIEs are part of the global scope.
8154 But in Ada and Fortran, we want to be able to access nested
8155 procedures globally. So all Ada and Fortran subprograms are
8156 stored in the global scope. */
8157 add_psymbol_to_list (actual_name,
8158 built_actual_name != NULL,
8159 VAR_DOMAIN, LOC_BLOCK,
8160 SECT_OFF_TEXT (objfile),
8161 psymbol_placement::GLOBAL,
8162 addr,
8163 cu->language, objfile);
8164 }
8165 else
8166 {
8167 add_psymbol_to_list (actual_name,
8168 built_actual_name != NULL,
8169 VAR_DOMAIN, LOC_BLOCK,
8170 SECT_OFF_TEXT (objfile),
8171 psymbol_placement::STATIC,
8172 addr, cu->language, objfile);
8173 }
8174
8175 if (pdi->main_subprogram && actual_name != NULL)
8176 set_objfile_main_name (objfile, actual_name, cu->language);
8177 break;
8178 case DW_TAG_constant:
8179 add_psymbol_to_list (actual_name,
8180 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
8181 -1, (pdi->is_external
8182 ? psymbol_placement::GLOBAL
8183 : psymbol_placement::STATIC),
8184 0, cu->language, objfile);
8185 break;
8186 case DW_TAG_variable:
8187 if (pdi->d.locdesc)
8188 addr = decode_locdesc (pdi->d.locdesc, cu);
8189
8190 if (pdi->d.locdesc
8191 && addr == 0
8192 && !dwarf2_per_objfile->has_section_at_zero)
8193 {
8194 /* A global or static variable may also have been stripped
8195 out by the linker if unused, in which case its address
8196 will be nullified; do not add such variables into partial
8197 symbol table then. */
8198 }
8199 else if (pdi->is_external)
8200 {
8201 /* Global Variable.
8202 Don't enter into the minimal symbol tables as there is
8203 a minimal symbol table entry from the ELF symbols already.
8204 Enter into partial symbol table if it has a location
8205 descriptor or a type.
8206 If the location descriptor is missing, new_symbol will create
8207 a LOC_UNRESOLVED symbol, the address of the variable will then
8208 be determined from the minimal symbol table whenever the variable
8209 is referenced.
8210 The address for the partial symbol table entry is not
8211 used by GDB, but it comes in handy for debugging partial symbol
8212 table building. */
8213
8214 if (pdi->d.locdesc || pdi->has_type)
8215 add_psymbol_to_list (actual_name,
8216 built_actual_name != NULL,
8217 VAR_DOMAIN, LOC_STATIC,
8218 SECT_OFF_TEXT (objfile),
8219 psymbol_placement::GLOBAL,
8220 addr, cu->language, objfile);
8221 }
8222 else
8223 {
8224 int has_loc = pdi->d.locdesc != NULL;
8225
8226 /* Static Variable. Skip symbols whose value we cannot know (those
8227 without location descriptors or constant values). */
8228 if (!has_loc && !pdi->has_const_value)
8229 return;
8230
8231 add_psymbol_to_list (actual_name,
8232 built_actual_name != NULL,
8233 VAR_DOMAIN, LOC_STATIC,
8234 SECT_OFF_TEXT (objfile),
8235 psymbol_placement::STATIC,
8236 has_loc ? addr : 0,
8237 cu->language, objfile);
8238 }
8239 break;
8240 case DW_TAG_typedef:
8241 case DW_TAG_base_type:
8242 case DW_TAG_subrange_type:
8243 add_psymbol_to_list (actual_name,
8244 built_actual_name != NULL,
8245 VAR_DOMAIN, LOC_TYPEDEF, -1,
8246 psymbol_placement::STATIC,
8247 0, cu->language, objfile);
8248 break;
8249 case DW_TAG_imported_declaration:
8250 case DW_TAG_namespace:
8251 add_psymbol_to_list (actual_name,
8252 built_actual_name != NULL,
8253 VAR_DOMAIN, LOC_TYPEDEF, -1,
8254 psymbol_placement::GLOBAL,
8255 0, cu->language, objfile);
8256 break;
8257 case DW_TAG_module:
8258 /* With Fortran 77 there might be a "BLOCK DATA" module
8259 available without any name. If so, we skip the module as it
8260 doesn't bring any value. */
8261 if (actual_name != nullptr)
8262 add_psymbol_to_list (actual_name,
8263 built_actual_name != NULL,
8264 MODULE_DOMAIN, LOC_TYPEDEF, -1,
8265 psymbol_placement::GLOBAL,
8266 0, cu->language, objfile);
8267 break;
8268 case DW_TAG_class_type:
8269 case DW_TAG_interface_type:
8270 case DW_TAG_structure_type:
8271 case DW_TAG_union_type:
8272 case DW_TAG_enumeration_type:
8273 /* Skip external references. The DWARF standard says in the section
8274 about "Structure, Union, and Class Type Entries": "An incomplete
8275 structure, union or class type is represented by a structure,
8276 union or class entry that does not have a byte size attribute
8277 and that has a DW_AT_declaration attribute." */
8278 if (!pdi->has_byte_size && pdi->is_declaration)
8279 return;
8280
8281 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8282 static vs. global. */
8283 add_psymbol_to_list (actual_name,
8284 built_actual_name != NULL,
8285 STRUCT_DOMAIN, LOC_TYPEDEF, -1,
8286 cu->language == language_cplus
8287 ? psymbol_placement::GLOBAL
8288 : psymbol_placement::STATIC,
8289 0, cu->language, objfile);
8290
8291 break;
8292 case DW_TAG_enumerator:
8293 add_psymbol_to_list (actual_name,
8294 built_actual_name != NULL,
8295 VAR_DOMAIN, LOC_CONST, -1,
8296 cu->language == language_cplus
8297 ? psymbol_placement::GLOBAL
8298 : psymbol_placement::STATIC,
8299 0, cu->language, objfile);
8300 break;
8301 default:
8302 break;
8303 }
8304 }
8305
8306 /* Read a partial die corresponding to a namespace; also, add a symbol
8307 corresponding to that namespace to the symbol table. NAMESPACE is
8308 the name of the enclosing namespace. */
8309
8310 static void
8311 add_partial_namespace (struct partial_die_info *pdi,
8312 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8313 int set_addrmap, struct dwarf2_cu *cu)
8314 {
8315 /* Add a symbol for the namespace. */
8316
8317 add_partial_symbol (pdi, cu);
8318
8319 /* Now scan partial symbols in that namespace. */
8320
8321 if (pdi->has_children)
8322 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8323 }
8324
8325 /* Read a partial die corresponding to a Fortran module. */
8326
8327 static void
8328 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
8329 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
8330 {
8331 /* Add a symbol for the namespace. */
8332
8333 add_partial_symbol (pdi, cu);
8334
8335 /* Now scan partial symbols in that module. */
8336
8337 if (pdi->has_children)
8338 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8339 }
8340
8341 /* Read a partial die corresponding to a subprogram or an inlined
8342 subprogram and create a partial symbol for that subprogram.
8343 When the CU language allows it, this routine also defines a partial
8344 symbol for each nested subprogram that this subprogram contains.
8345 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8346 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
8347
8348 PDI may also be a lexical block, in which case we simply search
8349 recursively for subprograms defined inside that lexical block.
8350 Again, this is only performed when the CU language allows this
8351 type of definitions. */
8352
8353 static void
8354 add_partial_subprogram (struct partial_die_info *pdi,
8355 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8356 int set_addrmap, struct dwarf2_cu *cu)
8357 {
8358 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
8359 {
8360 if (pdi->has_pc_info)
8361 {
8362 if (pdi->lowpc < *lowpc)
8363 *lowpc = pdi->lowpc;
8364 if (pdi->highpc > *highpc)
8365 *highpc = pdi->highpc;
8366 if (set_addrmap)
8367 {
8368 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
8369 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8370 CORE_ADDR baseaddr;
8371 CORE_ADDR this_highpc;
8372 CORE_ADDR this_lowpc;
8373
8374 baseaddr = objfile->text_section_offset ();
8375 this_lowpc
8376 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8377 pdi->lowpc + baseaddr)
8378 - baseaddr);
8379 this_highpc
8380 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8381 pdi->highpc + baseaddr)
8382 - baseaddr);
8383 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8384 this_lowpc, this_highpc - 1,
8385 cu->per_cu->v.psymtab);
8386 }
8387 }
8388
8389 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8390 {
8391 if (!pdi->is_declaration)
8392 /* Ignore subprogram DIEs that do not have a name, they are
8393 illegal. Do not emit a complaint at this point, we will
8394 do so when we convert this psymtab into a symtab. */
8395 if (pdi->name)
8396 add_partial_symbol (pdi, cu);
8397 }
8398 }
8399
8400 if (! pdi->has_children)
8401 return;
8402
8403 if (cu->language == language_ada || cu->language == language_fortran)
8404 {
8405 pdi = pdi->die_child;
8406 while (pdi != NULL)
8407 {
8408 pdi->fixup (cu);
8409 if (pdi->tag == DW_TAG_subprogram
8410 || pdi->tag == DW_TAG_inlined_subroutine
8411 || pdi->tag == DW_TAG_lexical_block)
8412 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8413 pdi = pdi->die_sibling;
8414 }
8415 }
8416 }
8417
8418 /* Read a partial die corresponding to an enumeration type. */
8419
8420 static void
8421 add_partial_enumeration (struct partial_die_info *enum_pdi,
8422 struct dwarf2_cu *cu)
8423 {
8424 struct partial_die_info *pdi;
8425
8426 if (enum_pdi->name != NULL)
8427 add_partial_symbol (enum_pdi, cu);
8428
8429 pdi = enum_pdi->die_child;
8430 while (pdi)
8431 {
8432 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
8433 complaint (_("malformed enumerator DIE ignored"));
8434 else
8435 add_partial_symbol (pdi, cu);
8436 pdi = pdi->die_sibling;
8437 }
8438 }
8439
8440 /* Return the initial uleb128 in the die at INFO_PTR. */
8441
8442 static unsigned int
8443 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
8444 {
8445 unsigned int bytes_read;
8446
8447 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8448 }
8449
8450 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8451 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8452
8453 Return the corresponding abbrev, or NULL if the number is zero (indicating
8454 an empty DIE). In either case *BYTES_READ will be set to the length of
8455 the initial number. */
8456
8457 static struct abbrev_info *
8458 peek_die_abbrev (const die_reader_specs &reader,
8459 const gdb_byte *info_ptr, unsigned int *bytes_read)
8460 {
8461 dwarf2_cu *cu = reader.cu;
8462 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
8463 unsigned int abbrev_number
8464 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
8465
8466 if (abbrev_number == 0)
8467 return NULL;
8468
8469 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
8470 if (!abbrev)
8471 {
8472 error (_("Dwarf Error: Could not find abbrev number %d in %s"
8473 " at offset %s [in module %s]"),
8474 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
8475 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
8476 }
8477
8478 return abbrev;
8479 }
8480
8481 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8482 Returns a pointer to the end of a series of DIEs, terminated by an empty
8483 DIE. Any children of the skipped DIEs will also be skipped. */
8484
8485 static const gdb_byte *
8486 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
8487 {
8488 while (1)
8489 {
8490 unsigned int bytes_read;
8491 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8492
8493 if (abbrev == NULL)
8494 return info_ptr + bytes_read;
8495 else
8496 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
8497 }
8498 }
8499
8500 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8501 INFO_PTR should point just after the initial uleb128 of a DIE, and the
8502 abbrev corresponding to that skipped uleb128 should be passed in
8503 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8504 children. */
8505
8506 static const gdb_byte *
8507 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
8508 struct abbrev_info *abbrev)
8509 {
8510 unsigned int bytes_read;
8511 struct attribute attr;
8512 bfd *abfd = reader->abfd;
8513 struct dwarf2_cu *cu = reader->cu;
8514 const gdb_byte *buffer = reader->buffer;
8515 const gdb_byte *buffer_end = reader->buffer_end;
8516 unsigned int form, i;
8517
8518 for (i = 0; i < abbrev->num_attrs; i++)
8519 {
8520 /* The only abbrev we care about is DW_AT_sibling. */
8521 if (abbrev->attrs[i].name == DW_AT_sibling)
8522 {
8523 bool ignored;
8524 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr,
8525 &ignored);
8526 if (attr.form == DW_FORM_ref_addr)
8527 complaint (_("ignoring absolute DW_AT_sibling"));
8528 else
8529 {
8530 sect_offset off = dwarf2_get_ref_die_offset (&attr);
8531 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
8532
8533 if (sibling_ptr < info_ptr)
8534 complaint (_("DW_AT_sibling points backwards"));
8535 else if (sibling_ptr > reader->buffer_end)
8536 dwarf2_section_buffer_overflow_complaint (reader->die_section);
8537 else
8538 return sibling_ptr;
8539 }
8540 }
8541
8542 /* If it isn't DW_AT_sibling, skip this attribute. */
8543 form = abbrev->attrs[i].form;
8544 skip_attribute:
8545 switch (form)
8546 {
8547 case DW_FORM_ref_addr:
8548 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8549 and later it is offset sized. */
8550 if (cu->header.version == 2)
8551 info_ptr += cu->header.addr_size;
8552 else
8553 info_ptr += cu->header.offset_size;
8554 break;
8555 case DW_FORM_GNU_ref_alt:
8556 info_ptr += cu->header.offset_size;
8557 break;
8558 case DW_FORM_addr:
8559 info_ptr += cu->header.addr_size;
8560 break;
8561 case DW_FORM_data1:
8562 case DW_FORM_ref1:
8563 case DW_FORM_flag:
8564 case DW_FORM_strx1:
8565 info_ptr += 1;
8566 break;
8567 case DW_FORM_flag_present:
8568 case DW_FORM_implicit_const:
8569 break;
8570 case DW_FORM_data2:
8571 case DW_FORM_ref2:
8572 case DW_FORM_strx2:
8573 info_ptr += 2;
8574 break;
8575 case DW_FORM_strx3:
8576 info_ptr += 3;
8577 break;
8578 case DW_FORM_data4:
8579 case DW_FORM_ref4:
8580 case DW_FORM_strx4:
8581 info_ptr += 4;
8582 break;
8583 case DW_FORM_data8:
8584 case DW_FORM_ref8:
8585 case DW_FORM_ref_sig8:
8586 info_ptr += 8;
8587 break;
8588 case DW_FORM_data16:
8589 info_ptr += 16;
8590 break;
8591 case DW_FORM_string:
8592 read_direct_string (abfd, info_ptr, &bytes_read);
8593 info_ptr += bytes_read;
8594 break;
8595 case DW_FORM_sec_offset:
8596 case DW_FORM_strp:
8597 case DW_FORM_GNU_strp_alt:
8598 info_ptr += cu->header.offset_size;
8599 break;
8600 case DW_FORM_exprloc:
8601 case DW_FORM_block:
8602 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8603 info_ptr += bytes_read;
8604 break;
8605 case DW_FORM_block1:
8606 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8607 break;
8608 case DW_FORM_block2:
8609 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8610 break;
8611 case DW_FORM_block4:
8612 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8613 break;
8614 case DW_FORM_addrx:
8615 case DW_FORM_strx:
8616 case DW_FORM_sdata:
8617 case DW_FORM_udata:
8618 case DW_FORM_ref_udata:
8619 case DW_FORM_GNU_addr_index:
8620 case DW_FORM_GNU_str_index:
8621 case DW_FORM_rnglistx:
8622 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
8623 break;
8624 case DW_FORM_indirect:
8625 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8626 info_ptr += bytes_read;
8627 /* We need to continue parsing from here, so just go back to
8628 the top. */
8629 goto skip_attribute;
8630
8631 default:
8632 error (_("Dwarf Error: Cannot handle %s "
8633 "in DWARF reader [in module %s]"),
8634 dwarf_form_name (form),
8635 bfd_get_filename (abfd));
8636 }
8637 }
8638
8639 if (abbrev->has_children)
8640 return skip_children (reader, info_ptr);
8641 else
8642 return info_ptr;
8643 }
8644
8645 /* Locate ORIG_PDI's sibling.
8646 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
8647
8648 static const gdb_byte *
8649 locate_pdi_sibling (const struct die_reader_specs *reader,
8650 struct partial_die_info *orig_pdi,
8651 const gdb_byte *info_ptr)
8652 {
8653 /* Do we know the sibling already? */
8654
8655 if (orig_pdi->sibling)
8656 return orig_pdi->sibling;
8657
8658 /* Are there any children to deal with? */
8659
8660 if (!orig_pdi->has_children)
8661 return info_ptr;
8662
8663 /* Skip the children the long way. */
8664
8665 return skip_children (reader, info_ptr);
8666 }
8667
8668 /* Expand this partial symbol table into a full symbol table. SELF is
8669 not NULL. */
8670
8671 void
8672 dwarf2_psymtab::read_symtab (struct objfile *objfile)
8673 {
8674 struct dwarf2_per_objfile *dwarf2_per_objfile
8675 = get_dwarf2_per_objfile (objfile);
8676
8677 gdb_assert (!readin);
8678 /* If this psymtab is constructed from a debug-only objfile, the
8679 has_section_at_zero flag will not necessarily be correct. We
8680 can get the correct value for this flag by looking at the data
8681 associated with the (presumably stripped) associated objfile. */
8682 if (objfile->separate_debug_objfile_backlink)
8683 {
8684 struct dwarf2_per_objfile *dpo_backlink
8685 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
8686
8687 dwarf2_per_objfile->has_section_at_zero
8688 = dpo_backlink->has_section_at_zero;
8689 }
8690
8691 expand_psymtab (objfile);
8692
8693 process_cu_includes (dwarf2_per_objfile);
8694 }
8695 \f
8696 /* Reading in full CUs. */
8697
8698 /* Add PER_CU to the queue. */
8699
8700 static void
8701 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
8702 enum language pretend_language)
8703 {
8704 per_cu->queued = 1;
8705 per_cu->dwarf2_per_objfile->queue.emplace (per_cu, pretend_language);
8706 }
8707
8708 /* If PER_CU is not yet queued, add it to the queue.
8709 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8710 dependency.
8711 The result is non-zero if PER_CU was queued, otherwise the result is zero
8712 meaning either PER_CU is already queued or it is already loaded.
8713
8714 N.B. There is an invariant here that if a CU is queued then it is loaded.
8715 The caller is required to load PER_CU if we return non-zero. */
8716
8717 static int
8718 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
8719 struct dwarf2_per_cu_data *per_cu,
8720 enum language pretend_language)
8721 {
8722 /* We may arrive here during partial symbol reading, if we need full
8723 DIEs to process an unusual case (e.g. template arguments). Do
8724 not queue PER_CU, just tell our caller to load its DIEs. */
8725 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
8726 {
8727 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
8728 return 1;
8729 return 0;
8730 }
8731
8732 /* Mark the dependence relation so that we don't flush PER_CU
8733 too early. */
8734 if (dependent_cu != NULL)
8735 dwarf2_add_dependence (dependent_cu, per_cu);
8736
8737 /* If it's already on the queue, we have nothing to do. */
8738 if (per_cu->queued)
8739 return 0;
8740
8741 /* If the compilation unit is already loaded, just mark it as
8742 used. */
8743 if (per_cu->cu != NULL)
8744 {
8745 per_cu->cu->last_used = 0;
8746 return 0;
8747 }
8748
8749 /* Add it to the queue. */
8750 queue_comp_unit (per_cu, pretend_language);
8751
8752 return 1;
8753 }
8754
8755 /* Process the queue. */
8756
8757 static void
8758 process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
8759 {
8760 if (dwarf_read_debug)
8761 {
8762 fprintf_unfiltered (gdb_stdlog,
8763 "Expanding one or more symtabs of objfile %s ...\n",
8764 objfile_name (dwarf2_per_objfile->objfile));
8765 }
8766
8767 /* The queue starts out with one item, but following a DIE reference
8768 may load a new CU, adding it to the end of the queue. */
8769 while (!dwarf2_per_objfile->queue.empty ())
8770 {
8771 dwarf2_queue_item &item = dwarf2_per_objfile->queue.front ();
8772
8773 if ((dwarf2_per_objfile->using_index
8774 ? !item.per_cu->v.quick->compunit_symtab
8775 : (item.per_cu->v.psymtab && !item.per_cu->v.psymtab->readin))
8776 /* Skip dummy CUs. */
8777 && item.per_cu->cu != NULL)
8778 {
8779 struct dwarf2_per_cu_data *per_cu = item.per_cu;
8780 unsigned int debug_print_threshold;
8781 char buf[100];
8782
8783 if (per_cu->is_debug_types)
8784 {
8785 struct signatured_type *sig_type =
8786 (struct signatured_type *) per_cu;
8787
8788 sprintf (buf, "TU %s at offset %s",
8789 hex_string (sig_type->signature),
8790 sect_offset_str (per_cu->sect_off));
8791 /* There can be 100s of TUs.
8792 Only print them in verbose mode. */
8793 debug_print_threshold = 2;
8794 }
8795 else
8796 {
8797 sprintf (buf, "CU at offset %s",
8798 sect_offset_str (per_cu->sect_off));
8799 debug_print_threshold = 1;
8800 }
8801
8802 if (dwarf_read_debug >= debug_print_threshold)
8803 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
8804
8805 if (per_cu->is_debug_types)
8806 process_full_type_unit (per_cu, item.pretend_language);
8807 else
8808 process_full_comp_unit (per_cu, item.pretend_language);
8809
8810 if (dwarf_read_debug >= debug_print_threshold)
8811 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
8812 }
8813
8814 item.per_cu->queued = 0;
8815 dwarf2_per_objfile->queue.pop ();
8816 }
8817
8818 if (dwarf_read_debug)
8819 {
8820 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
8821 objfile_name (dwarf2_per_objfile->objfile));
8822 }
8823 }
8824
8825 /* Read in full symbols for PST, and anything it depends on. */
8826
8827 void
8828 dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
8829 {
8830 struct dwarf2_per_cu_data *per_cu;
8831
8832 if (readin)
8833 return;
8834
8835 read_dependencies (objfile);
8836
8837 per_cu = per_cu_data;
8838
8839 if (per_cu == NULL)
8840 {
8841 /* It's an include file, no symbols to read for it.
8842 Everything is in the parent symtab. */
8843 readin = true;
8844 return;
8845 }
8846
8847 dw2_do_instantiate_symtab (per_cu, false);
8848 }
8849
8850 /* Trivial hash function for die_info: the hash value of a DIE
8851 is its offset in .debug_info for this objfile. */
8852
8853 static hashval_t
8854 die_hash (const void *item)
8855 {
8856 const struct die_info *die = (const struct die_info *) item;
8857
8858 return to_underlying (die->sect_off);
8859 }
8860
8861 /* Trivial comparison function for die_info structures: two DIEs
8862 are equal if they have the same offset. */
8863
8864 static int
8865 die_eq (const void *item_lhs, const void *item_rhs)
8866 {
8867 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
8868 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
8869
8870 return die_lhs->sect_off == die_rhs->sect_off;
8871 }
8872
8873 /* Load the DIEs associated with PER_CU into memory. */
8874
8875 static void
8876 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
8877 bool skip_partial,
8878 enum language pretend_language)
8879 {
8880 gdb_assert (! this_cu->is_debug_types);
8881
8882 cutu_reader reader (this_cu, NULL, 1, skip_partial);
8883 if (reader.dummy_p)
8884 return;
8885
8886 struct dwarf2_cu *cu = reader.cu;
8887 const gdb_byte *info_ptr = reader.info_ptr;
8888
8889 gdb_assert (cu->die_hash == NULL);
8890 cu->die_hash =
8891 htab_create_alloc_ex (cu->header.length / 12,
8892 die_hash,
8893 die_eq,
8894 NULL,
8895 &cu->comp_unit_obstack,
8896 hashtab_obstack_allocate,
8897 dummy_obstack_deallocate);
8898
8899 if (reader.comp_unit_die->has_children)
8900 reader.comp_unit_die->child
8901 = read_die_and_siblings (&reader, reader.info_ptr,
8902 &info_ptr, reader.comp_unit_die);
8903 cu->dies = reader.comp_unit_die;
8904 /* comp_unit_die is not stored in die_hash, no need. */
8905
8906 /* We try not to read any attributes in this function, because not
8907 all CUs needed for references have been loaded yet, and symbol
8908 table processing isn't initialized. But we have to set the CU language,
8909 or we won't be able to build types correctly.
8910 Similarly, if we do not read the producer, we can not apply
8911 producer-specific interpretation. */
8912 prepare_one_comp_unit (cu, cu->dies, pretend_language);
8913
8914 reader.keep ();
8915 }
8916
8917 /* Add a DIE to the delayed physname list. */
8918
8919 static void
8920 add_to_method_list (struct type *type, int fnfield_index, int index,
8921 const char *name, struct die_info *die,
8922 struct dwarf2_cu *cu)
8923 {
8924 struct delayed_method_info mi;
8925 mi.type = type;
8926 mi.fnfield_index = fnfield_index;
8927 mi.index = index;
8928 mi.name = name;
8929 mi.die = die;
8930 cu->method_list.push_back (mi);
8931 }
8932
8933 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
8934 "const" / "volatile". If so, decrements LEN by the length of the
8935 modifier and return true. Otherwise return false. */
8936
8937 template<size_t N>
8938 static bool
8939 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
8940 {
8941 size_t mod_len = sizeof (mod) - 1;
8942 if (len > mod_len && startswith (physname + (len - mod_len), mod))
8943 {
8944 len -= mod_len;
8945 return true;
8946 }
8947 return false;
8948 }
8949
8950 /* Compute the physnames of any methods on the CU's method list.
8951
8952 The computation of method physnames is delayed in order to avoid the
8953 (bad) condition that one of the method's formal parameters is of an as yet
8954 incomplete type. */
8955
8956 static void
8957 compute_delayed_physnames (struct dwarf2_cu *cu)
8958 {
8959 /* Only C++ delays computing physnames. */
8960 if (cu->method_list.empty ())
8961 return;
8962 gdb_assert (cu->language == language_cplus);
8963
8964 for (const delayed_method_info &mi : cu->method_list)
8965 {
8966 const char *physname;
8967 struct fn_fieldlist *fn_flp
8968 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
8969 physname = dwarf2_physname (mi.name, mi.die, cu);
8970 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
8971 = physname ? physname : "";
8972
8973 /* Since there's no tag to indicate whether a method is a
8974 const/volatile overload, extract that information out of the
8975 demangled name. */
8976 if (physname != NULL)
8977 {
8978 size_t len = strlen (physname);
8979
8980 while (1)
8981 {
8982 if (physname[len] == ')') /* shortcut */
8983 break;
8984 else if (check_modifier (physname, len, " const"))
8985 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
8986 else if (check_modifier (physname, len, " volatile"))
8987 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
8988 else
8989 break;
8990 }
8991 }
8992 }
8993
8994 /* The list is no longer needed. */
8995 cu->method_list.clear ();
8996 }
8997
8998 /* Go objects should be embedded in a DW_TAG_module DIE,
8999 and it's not clear if/how imported objects will appear.
9000 To keep Go support simple until that's worked out,
9001 go back through what we've read and create something usable.
9002 We could do this while processing each DIE, and feels kinda cleaner,
9003 but that way is more invasive.
9004 This is to, for example, allow the user to type "p var" or "b main"
9005 without having to specify the package name, and allow lookups
9006 of module.object to work in contexts that use the expression
9007 parser. */
9008
9009 static void
9010 fixup_go_packaging (struct dwarf2_cu *cu)
9011 {
9012 gdb::unique_xmalloc_ptr<char> package_name;
9013 struct pending *list;
9014 int i;
9015
9016 for (list = *cu->get_builder ()->get_global_symbols ();
9017 list != NULL;
9018 list = list->next)
9019 {
9020 for (i = 0; i < list->nsyms; ++i)
9021 {
9022 struct symbol *sym = list->symbol[i];
9023
9024 if (sym->language () == language_go
9025 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9026 {
9027 gdb::unique_xmalloc_ptr<char> this_package_name
9028 (go_symbol_package_name (sym));
9029
9030 if (this_package_name == NULL)
9031 continue;
9032 if (package_name == NULL)
9033 package_name = std::move (this_package_name);
9034 else
9035 {
9036 struct objfile *objfile
9037 = cu->per_cu->dwarf2_per_objfile->objfile;
9038 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
9039 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
9040 (symbol_symtab (sym) != NULL
9041 ? symtab_to_filename_for_display
9042 (symbol_symtab (sym))
9043 : objfile_name (objfile)),
9044 this_package_name.get (), package_name.get ());
9045 }
9046 }
9047 }
9048 }
9049
9050 if (package_name != NULL)
9051 {
9052 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9053 const char *saved_package_name
9054 = obstack_strdup (&objfile->per_bfd->storage_obstack, package_name.get ());
9055 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9056 saved_package_name);
9057 struct symbol *sym;
9058
9059 sym = allocate_symbol (objfile);
9060 sym->set_language (language_go, &objfile->objfile_obstack);
9061 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
9062 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9063 e.g., "main" finds the "main" module and not C's main(). */
9064 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9065 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9066 SYMBOL_TYPE (sym) = type;
9067
9068 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
9069 }
9070 }
9071
9072 /* Allocate a fully-qualified name consisting of the two parts on the
9073 obstack. */
9074
9075 static const char *
9076 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9077 {
9078 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9079 }
9080
9081 /* A helper that allocates a struct discriminant_info to attach to a
9082 union type. */
9083
9084 static struct discriminant_info *
9085 alloc_discriminant_info (struct type *type, int discriminant_index,
9086 int default_index)
9087 {
9088 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9089 gdb_assert (discriminant_index == -1
9090 || (discriminant_index >= 0
9091 && discriminant_index < TYPE_NFIELDS (type)));
9092 gdb_assert (default_index == -1
9093 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
9094
9095 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
9096
9097 struct discriminant_info *disc
9098 = ((struct discriminant_info *)
9099 TYPE_ZALLOC (type,
9100 offsetof (struct discriminant_info, discriminants)
9101 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
9102 disc->default_index = default_index;
9103 disc->discriminant_index = discriminant_index;
9104
9105 struct dynamic_prop prop;
9106 prop.kind = PROP_UNDEFINED;
9107 prop.data.baton = disc;
9108
9109 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
9110
9111 return disc;
9112 }
9113
9114 /* Some versions of rustc emitted enums in an unusual way.
9115
9116 Ordinary enums were emitted as unions. The first element of each
9117 structure in the union was named "RUST$ENUM$DISR". This element
9118 held the discriminant.
9119
9120 These versions of Rust also implemented the "non-zero"
9121 optimization. When the enum had two values, and one is empty and
9122 the other holds a pointer that cannot be zero, the pointer is used
9123 as the discriminant, with a zero value meaning the empty variant.
9124 Here, the union's first member is of the form
9125 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9126 where the fieldnos are the indices of the fields that should be
9127 traversed in order to find the field (which may be several fields deep)
9128 and the variantname is the name of the variant of the case when the
9129 field is zero.
9130
9131 This function recognizes whether TYPE is of one of these forms,
9132 and, if so, smashes it to be a variant type. */
9133
9134 static void
9135 quirk_rust_enum (struct type *type, struct objfile *objfile)
9136 {
9137 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9138
9139 /* We don't need to deal with empty enums. */
9140 if (TYPE_NFIELDS (type) == 0)
9141 return;
9142
9143 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9144 if (TYPE_NFIELDS (type) == 1
9145 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9146 {
9147 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9148
9149 /* Decode the field name to find the offset of the
9150 discriminant. */
9151 ULONGEST bit_offset = 0;
9152 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9153 while (name[0] >= '0' && name[0] <= '9')
9154 {
9155 char *tail;
9156 unsigned long index = strtoul (name, &tail, 10);
9157 name = tail;
9158 if (*name != '$'
9159 || index >= TYPE_NFIELDS (field_type)
9160 || (TYPE_FIELD_LOC_KIND (field_type, index)
9161 != FIELD_LOC_KIND_BITPOS))
9162 {
9163 complaint (_("Could not parse Rust enum encoding string \"%s\""
9164 "[in module %s]"),
9165 TYPE_FIELD_NAME (type, 0),
9166 objfile_name (objfile));
9167 return;
9168 }
9169 ++name;
9170
9171 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9172 field_type = TYPE_FIELD_TYPE (field_type, index);
9173 }
9174
9175 /* Make a union to hold the variants. */
9176 struct type *union_type = alloc_type (objfile);
9177 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9178 TYPE_NFIELDS (union_type) = 3;
9179 TYPE_FIELDS (union_type)
9180 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
9181 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9182 set_type_align (union_type, TYPE_RAW_ALIGN (type));
9183
9184 /* Put the discriminant must at index 0. */
9185 TYPE_FIELD_TYPE (union_type, 0) = field_type;
9186 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9187 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9188 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
9189
9190 /* The order of fields doesn't really matter, so put the real
9191 field at index 1 and the data-less field at index 2. */
9192 struct discriminant_info *disc
9193 = alloc_discriminant_info (union_type, 0, 1);
9194 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
9195 TYPE_FIELD_NAME (union_type, 1)
9196 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
9197 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
9198 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9199 TYPE_FIELD_NAME (union_type, 1));
9200
9201 const char *dataless_name
9202 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9203 name);
9204 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9205 dataless_name);
9206 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
9207 /* NAME points into the original discriminant name, which
9208 already has the correct lifetime. */
9209 TYPE_FIELD_NAME (union_type, 2) = name;
9210 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
9211 disc->discriminants[2] = 0;
9212
9213 /* Smash this type to be a structure type. We have to do this
9214 because the type has already been recorded. */
9215 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9216 TYPE_NFIELDS (type) = 1;
9217 TYPE_FIELDS (type)
9218 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
9219
9220 /* Install the variant part. */
9221 TYPE_FIELD_TYPE (type, 0) = union_type;
9222 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9223 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9224 }
9225 /* A union with a single anonymous field is probably an old-style
9226 univariant enum. */
9227 else if (TYPE_NFIELDS (type) == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
9228 {
9229 /* Smash this type to be a structure type. We have to do this
9230 because the type has already been recorded. */
9231 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9232
9233 /* Make a union to hold the variants. */
9234 struct type *union_type = alloc_type (objfile);
9235 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9236 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
9237 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9238 set_type_align (union_type, TYPE_RAW_ALIGN (type));
9239 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
9240
9241 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
9242 const char *variant_name
9243 = rust_last_path_segment (TYPE_NAME (field_type));
9244 TYPE_FIELD_NAME (union_type, 0) = variant_name;
9245 TYPE_NAME (field_type)
9246 = rust_fully_qualify (&objfile->objfile_obstack,
9247 TYPE_NAME (type), variant_name);
9248
9249 /* Install the union in the outer struct type. */
9250 TYPE_NFIELDS (type) = 1;
9251 TYPE_FIELDS (type)
9252 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
9253 TYPE_FIELD_TYPE (type, 0) = union_type;
9254 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9255 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9256
9257 alloc_discriminant_info (union_type, -1, 0);
9258 }
9259 else
9260 {
9261 struct type *disr_type = nullptr;
9262 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
9263 {
9264 disr_type = TYPE_FIELD_TYPE (type, i);
9265
9266 if (TYPE_CODE (disr_type) != TYPE_CODE_STRUCT)
9267 {
9268 /* All fields of a true enum will be structs. */
9269 return;
9270 }
9271 else if (TYPE_NFIELDS (disr_type) == 0)
9272 {
9273 /* Could be data-less variant, so keep going. */
9274 disr_type = nullptr;
9275 }
9276 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9277 "RUST$ENUM$DISR") != 0)
9278 {
9279 /* Not a Rust enum. */
9280 return;
9281 }
9282 else
9283 {
9284 /* Found one. */
9285 break;
9286 }
9287 }
9288
9289 /* If we got here without a discriminant, then it's probably
9290 just a union. */
9291 if (disr_type == nullptr)
9292 return;
9293
9294 /* Smash this type to be a structure type. We have to do this
9295 because the type has already been recorded. */
9296 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9297
9298 /* Make a union to hold the variants. */
9299 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
9300 struct type *union_type = alloc_type (objfile);
9301 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9302 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
9303 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9304 set_type_align (union_type, TYPE_RAW_ALIGN (type));
9305 TYPE_FIELDS (union_type)
9306 = (struct field *) TYPE_ZALLOC (union_type,
9307 (TYPE_NFIELDS (union_type)
9308 * sizeof (struct field)));
9309
9310 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
9311 TYPE_NFIELDS (type) * sizeof (struct field));
9312
9313 /* Install the discriminant at index 0 in the union. */
9314 TYPE_FIELD (union_type, 0) = *disr_field;
9315 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9316 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9317
9318 /* Install the union in the outer struct type. */
9319 TYPE_FIELD_TYPE (type, 0) = union_type;
9320 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9321 TYPE_NFIELDS (type) = 1;
9322
9323 /* Set the size and offset of the union type. */
9324 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9325
9326 /* We need a way to find the correct discriminant given a
9327 variant name. For convenience we build a map here. */
9328 struct type *enum_type = FIELD_TYPE (*disr_field);
9329 std::unordered_map<std::string, ULONGEST> discriminant_map;
9330 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
9331 {
9332 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9333 {
9334 const char *name
9335 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9336 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9337 }
9338 }
9339
9340 int n_fields = TYPE_NFIELDS (union_type);
9341 struct discriminant_info *disc
9342 = alloc_discriminant_info (union_type, 0, -1);
9343 /* Skip the discriminant here. */
9344 for (int i = 1; i < n_fields; ++i)
9345 {
9346 /* Find the final word in the name of this variant's type.
9347 That name can be used to look up the correct
9348 discriminant. */
9349 const char *variant_name
9350 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
9351 i)));
9352
9353 auto iter = discriminant_map.find (variant_name);
9354 if (iter != discriminant_map.end ())
9355 disc->discriminants[i] = iter->second;
9356
9357 /* Remove the discriminant field, if it exists. */
9358 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
9359 if (TYPE_NFIELDS (sub_type) > 0)
9360 {
9361 --TYPE_NFIELDS (sub_type);
9362 ++TYPE_FIELDS (sub_type);
9363 }
9364 TYPE_FIELD_NAME (union_type, i) = variant_name;
9365 TYPE_NAME (sub_type)
9366 = rust_fully_qualify (&objfile->objfile_obstack,
9367 TYPE_NAME (type), variant_name);
9368 }
9369 }
9370 }
9371
9372 /* Rewrite some Rust unions to be structures with variants parts. */
9373
9374 static void
9375 rust_union_quirks (struct dwarf2_cu *cu)
9376 {
9377 gdb_assert (cu->language == language_rust);
9378 for (type *type_ : cu->rust_unions)
9379 quirk_rust_enum (type_, cu->per_cu->dwarf2_per_objfile->objfile);
9380 /* We don't need this any more. */
9381 cu->rust_unions.clear ();
9382 }
9383
9384 /* Return the symtab for PER_CU. This works properly regardless of
9385 whether we're using the index or psymtabs. */
9386
9387 static struct compunit_symtab *
9388 get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
9389 {
9390 return (per_cu->dwarf2_per_objfile->using_index
9391 ? per_cu->v.quick->compunit_symtab
9392 : per_cu->v.psymtab->compunit_symtab);
9393 }
9394
9395 /* A helper function for computing the list of all symbol tables
9396 included by PER_CU. */
9397
9398 static void
9399 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
9400 htab_t all_children, htab_t all_type_symtabs,
9401 struct dwarf2_per_cu_data *per_cu,
9402 struct compunit_symtab *immediate_parent)
9403 {
9404 void **slot;
9405 struct compunit_symtab *cust;
9406
9407 slot = htab_find_slot (all_children, per_cu, INSERT);
9408 if (*slot != NULL)
9409 {
9410 /* This inclusion and its children have been processed. */
9411 return;
9412 }
9413
9414 *slot = per_cu;
9415 /* Only add a CU if it has a symbol table. */
9416 cust = get_compunit_symtab (per_cu);
9417 if (cust != NULL)
9418 {
9419 /* If this is a type unit only add its symbol table if we haven't
9420 seen it yet (type unit per_cu's can share symtabs). */
9421 if (per_cu->is_debug_types)
9422 {
9423 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
9424 if (*slot == NULL)
9425 {
9426 *slot = cust;
9427 result->push_back (cust);
9428 if (cust->user == NULL)
9429 cust->user = immediate_parent;
9430 }
9431 }
9432 else
9433 {
9434 result->push_back (cust);
9435 if (cust->user == NULL)
9436 cust->user = immediate_parent;
9437 }
9438 }
9439
9440 if (!per_cu->imported_symtabs_empty ())
9441 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9442 {
9443 recursively_compute_inclusions (result, all_children,
9444 all_type_symtabs, ptr, cust);
9445 }
9446 }
9447
9448 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
9449 PER_CU. */
9450
9451 static void
9452 compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
9453 {
9454 gdb_assert (! per_cu->is_debug_types);
9455
9456 if (!per_cu->imported_symtabs_empty ())
9457 {
9458 int len;
9459 std::vector<compunit_symtab *> result_symtabs;
9460 htab_t all_children, all_type_symtabs;
9461 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
9462
9463 /* If we don't have a symtab, we can just skip this case. */
9464 if (cust == NULL)
9465 return;
9466
9467 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9468 NULL, xcalloc, xfree);
9469 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9470 NULL, xcalloc, xfree);
9471
9472 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9473 {
9474 recursively_compute_inclusions (&result_symtabs, all_children,
9475 all_type_symtabs, ptr, cust);
9476 }
9477
9478 /* Now we have a transitive closure of all the included symtabs. */
9479 len = result_symtabs.size ();
9480 cust->includes
9481 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
9482 struct compunit_symtab *, len + 1);
9483 memcpy (cust->includes, result_symtabs.data (),
9484 len * sizeof (compunit_symtab *));
9485 cust->includes[len] = NULL;
9486
9487 htab_delete (all_children);
9488 htab_delete (all_type_symtabs);
9489 }
9490 }
9491
9492 /* Compute the 'includes' field for the symtabs of all the CUs we just
9493 read. */
9494
9495 static void
9496 process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
9497 {
9498 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->just_read_cus)
9499 {
9500 if (! iter->is_debug_types)
9501 compute_compunit_symtab_includes (iter);
9502 }
9503
9504 dwarf2_per_objfile->just_read_cus.clear ();
9505 }
9506
9507 /* Generate full symbol information for PER_CU, whose DIEs have
9508 already been loaded into memory. */
9509
9510 static void
9511 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
9512 enum language pretend_language)
9513 {
9514 struct dwarf2_cu *cu = per_cu->cu;
9515 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9516 struct objfile *objfile = dwarf2_per_objfile->objfile;
9517 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9518 CORE_ADDR lowpc, highpc;
9519 struct compunit_symtab *cust;
9520 CORE_ADDR baseaddr;
9521 struct block *static_block;
9522 CORE_ADDR addr;
9523
9524 baseaddr = objfile->text_section_offset ();
9525
9526 /* Clear the list here in case something was left over. */
9527 cu->method_list.clear ();
9528
9529 cu->language = pretend_language;
9530 cu->language_defn = language_def (cu->language);
9531
9532 /* Do line number decoding in read_file_scope () */
9533 process_die (cu->dies, cu);
9534
9535 /* For now fudge the Go package. */
9536 if (cu->language == language_go)
9537 fixup_go_packaging (cu);
9538
9539 /* Now that we have processed all the DIEs in the CU, all the types
9540 should be complete, and it should now be safe to compute all of the
9541 physnames. */
9542 compute_delayed_physnames (cu);
9543
9544 if (cu->language == language_rust)
9545 rust_union_quirks (cu);
9546
9547 /* Some compilers don't define a DW_AT_high_pc attribute for the
9548 compilation unit. If the DW_AT_high_pc is missing, synthesize
9549 it, by scanning the DIE's below the compilation unit. */
9550 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
9551
9552 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
9553 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
9554
9555 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9556 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9557 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9558 addrmap to help ensure it has an accurate map of pc values belonging to
9559 this comp unit. */
9560 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9561
9562 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
9563 SECT_OFF_TEXT (objfile),
9564 0);
9565
9566 if (cust != NULL)
9567 {
9568 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
9569
9570 /* Set symtab language to language from DW_AT_language. If the
9571 compilation is from a C file generated by language preprocessors, do
9572 not set the language if it was already deduced by start_subfile. */
9573 if (!(cu->language == language_c
9574 && COMPUNIT_FILETABS (cust)->language != language_unknown))
9575 COMPUNIT_FILETABS (cust)->language = cu->language;
9576
9577 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9578 produce DW_AT_location with location lists but it can be possibly
9579 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9580 there were bugs in prologue debug info, fixed later in GCC-4.5
9581 by "unwind info for epilogues" patch (which is not directly related).
9582
9583 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9584 needed, it would be wrong due to missing DW_AT_producer there.
9585
9586 Still one can confuse GDB by using non-standard GCC compilation
9587 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
9588 */
9589 if (cu->has_loclist && gcc_4_minor >= 5)
9590 cust->locations_valid = 1;
9591
9592 if (gcc_4_minor >= 5)
9593 cust->epilogue_unwind_valid = 1;
9594
9595 cust->call_site_htab = cu->call_site_htab;
9596 }
9597
9598 if (dwarf2_per_objfile->using_index)
9599 per_cu->v.quick->compunit_symtab = cust;
9600 else
9601 {
9602 dwarf2_psymtab *pst = per_cu->v.psymtab;
9603 pst->compunit_symtab = cust;
9604 pst->readin = true;
9605 }
9606
9607 /* Push it for inclusion processing later. */
9608 dwarf2_per_objfile->just_read_cus.push_back (per_cu);
9609
9610 /* Not needed any more. */
9611 cu->reset_builder ();
9612 }
9613
9614 /* Generate full symbol information for type unit PER_CU, whose DIEs have
9615 already been loaded into memory. */
9616
9617 static void
9618 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
9619 enum language pretend_language)
9620 {
9621 struct dwarf2_cu *cu = per_cu->cu;
9622 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9623 struct objfile *objfile = dwarf2_per_objfile->objfile;
9624 struct compunit_symtab *cust;
9625 struct signatured_type *sig_type;
9626
9627 gdb_assert (per_cu->is_debug_types);
9628 sig_type = (struct signatured_type *) per_cu;
9629
9630 /* Clear the list here in case something was left over. */
9631 cu->method_list.clear ();
9632
9633 cu->language = pretend_language;
9634 cu->language_defn = language_def (cu->language);
9635
9636 /* The symbol tables are set up in read_type_unit_scope. */
9637 process_die (cu->dies, cu);
9638
9639 /* For now fudge the Go package. */
9640 if (cu->language == language_go)
9641 fixup_go_packaging (cu);
9642
9643 /* Now that we have processed all the DIEs in the CU, all the types
9644 should be complete, and it should now be safe to compute all of the
9645 physnames. */
9646 compute_delayed_physnames (cu);
9647
9648 if (cu->language == language_rust)
9649 rust_union_quirks (cu);
9650
9651 /* TUs share symbol tables.
9652 If this is the first TU to use this symtab, complete the construction
9653 of it with end_expandable_symtab. Otherwise, complete the addition of
9654 this TU's symbols to the existing symtab. */
9655 if (sig_type->type_unit_group->compunit_symtab == NULL)
9656 {
9657 buildsym_compunit *builder = cu->get_builder ();
9658 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
9659 sig_type->type_unit_group->compunit_symtab = cust;
9660
9661 if (cust != NULL)
9662 {
9663 /* Set symtab language to language from DW_AT_language. If the
9664 compilation is from a C file generated by language preprocessors,
9665 do not set the language if it was already deduced by
9666 start_subfile. */
9667 if (!(cu->language == language_c
9668 && COMPUNIT_FILETABS (cust)->language != language_c))
9669 COMPUNIT_FILETABS (cust)->language = cu->language;
9670 }
9671 }
9672 else
9673 {
9674 cu->get_builder ()->augment_type_symtab ();
9675 cust = sig_type->type_unit_group->compunit_symtab;
9676 }
9677
9678 if (dwarf2_per_objfile->using_index)
9679 per_cu->v.quick->compunit_symtab = cust;
9680 else
9681 {
9682 dwarf2_psymtab *pst = per_cu->v.psymtab;
9683 pst->compunit_symtab = cust;
9684 pst->readin = true;
9685 }
9686
9687 /* Not needed any more. */
9688 cu->reset_builder ();
9689 }
9690
9691 /* Process an imported unit DIE. */
9692
9693 static void
9694 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9695 {
9696 struct attribute *attr;
9697
9698 /* For now we don't handle imported units in type units. */
9699 if (cu->per_cu->is_debug_types)
9700 {
9701 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9702 " supported in type units [in module %s]"),
9703 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
9704 }
9705
9706 attr = dwarf2_attr (die, DW_AT_import, cu);
9707 if (attr != NULL)
9708 {
9709 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
9710 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
9711 dwarf2_per_cu_data *per_cu
9712 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
9713 cu->per_cu->dwarf2_per_objfile);
9714
9715 /* If necessary, add it to the queue and load its DIEs. */
9716 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
9717 load_full_comp_unit (per_cu, false, cu->language);
9718
9719 cu->per_cu->imported_symtabs_push (per_cu);
9720 }
9721 }
9722
9723 /* RAII object that represents a process_die scope: i.e.,
9724 starts/finishes processing a DIE. */
9725 class process_die_scope
9726 {
9727 public:
9728 process_die_scope (die_info *die, dwarf2_cu *cu)
9729 : m_die (die), m_cu (cu)
9730 {
9731 /* We should only be processing DIEs not already in process. */
9732 gdb_assert (!m_die->in_process);
9733 m_die->in_process = true;
9734 }
9735
9736 ~process_die_scope ()
9737 {
9738 m_die->in_process = false;
9739
9740 /* If we're done processing the DIE for the CU that owns the line
9741 header, we don't need the line header anymore. */
9742 if (m_cu->line_header_die_owner == m_die)
9743 {
9744 delete m_cu->line_header;
9745 m_cu->line_header = NULL;
9746 m_cu->line_header_die_owner = NULL;
9747 }
9748 }
9749
9750 private:
9751 die_info *m_die;
9752 dwarf2_cu *m_cu;
9753 };
9754
9755 /* Process a die and its children. */
9756
9757 static void
9758 process_die (struct die_info *die, struct dwarf2_cu *cu)
9759 {
9760 process_die_scope scope (die, cu);
9761
9762 switch (die->tag)
9763 {
9764 case DW_TAG_padding:
9765 break;
9766 case DW_TAG_compile_unit:
9767 case DW_TAG_partial_unit:
9768 read_file_scope (die, cu);
9769 break;
9770 case DW_TAG_type_unit:
9771 read_type_unit_scope (die, cu);
9772 break;
9773 case DW_TAG_subprogram:
9774 /* Nested subprograms in Fortran get a prefix. */
9775 if (cu->language == language_fortran
9776 && die->parent != NULL
9777 && die->parent->tag == DW_TAG_subprogram)
9778 cu->processing_has_namespace_info = true;
9779 /* Fall through. */
9780 case DW_TAG_inlined_subroutine:
9781 read_func_scope (die, cu);
9782 break;
9783 case DW_TAG_lexical_block:
9784 case DW_TAG_try_block:
9785 case DW_TAG_catch_block:
9786 read_lexical_block_scope (die, cu);
9787 break;
9788 case DW_TAG_call_site:
9789 case DW_TAG_GNU_call_site:
9790 read_call_site_scope (die, cu);
9791 break;
9792 case DW_TAG_class_type:
9793 case DW_TAG_interface_type:
9794 case DW_TAG_structure_type:
9795 case DW_TAG_union_type:
9796 process_structure_scope (die, cu);
9797 break;
9798 case DW_TAG_enumeration_type:
9799 process_enumeration_scope (die, cu);
9800 break;
9801
9802 /* These dies have a type, but processing them does not create
9803 a symbol or recurse to process the children. Therefore we can
9804 read them on-demand through read_type_die. */
9805 case DW_TAG_subroutine_type:
9806 case DW_TAG_set_type:
9807 case DW_TAG_array_type:
9808 case DW_TAG_pointer_type:
9809 case DW_TAG_ptr_to_member_type:
9810 case DW_TAG_reference_type:
9811 case DW_TAG_rvalue_reference_type:
9812 case DW_TAG_string_type:
9813 break;
9814
9815 case DW_TAG_base_type:
9816 case DW_TAG_subrange_type:
9817 case DW_TAG_typedef:
9818 /* Add a typedef symbol for the type definition, if it has a
9819 DW_AT_name. */
9820 new_symbol (die, read_type_die (die, cu), cu);
9821 break;
9822 case DW_TAG_common_block:
9823 read_common_block (die, cu);
9824 break;
9825 case DW_TAG_common_inclusion:
9826 break;
9827 case DW_TAG_namespace:
9828 cu->processing_has_namespace_info = true;
9829 read_namespace (die, cu);
9830 break;
9831 case DW_TAG_module:
9832 cu->processing_has_namespace_info = true;
9833 read_module (die, cu);
9834 break;
9835 case DW_TAG_imported_declaration:
9836 cu->processing_has_namespace_info = true;
9837 if (read_namespace_alias (die, cu))
9838 break;
9839 /* The declaration is not a global namespace alias. */
9840 /* Fall through. */
9841 case DW_TAG_imported_module:
9842 cu->processing_has_namespace_info = true;
9843 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
9844 || cu->language != language_fortran))
9845 complaint (_("Tag '%s' has unexpected children"),
9846 dwarf_tag_name (die->tag));
9847 read_import_statement (die, cu);
9848 break;
9849
9850 case DW_TAG_imported_unit:
9851 process_imported_unit_die (die, cu);
9852 break;
9853
9854 case DW_TAG_variable:
9855 read_variable (die, cu);
9856 break;
9857
9858 default:
9859 new_symbol (die, NULL, cu);
9860 break;
9861 }
9862 }
9863 \f
9864 /* DWARF name computation. */
9865
9866 /* A helper function for dwarf2_compute_name which determines whether DIE
9867 needs to have the name of the scope prepended to the name listed in the
9868 die. */
9869
9870 static int
9871 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
9872 {
9873 struct attribute *attr;
9874
9875 switch (die->tag)
9876 {
9877 case DW_TAG_namespace:
9878 case DW_TAG_typedef:
9879 case DW_TAG_class_type:
9880 case DW_TAG_interface_type:
9881 case DW_TAG_structure_type:
9882 case DW_TAG_union_type:
9883 case DW_TAG_enumeration_type:
9884 case DW_TAG_enumerator:
9885 case DW_TAG_subprogram:
9886 case DW_TAG_inlined_subroutine:
9887 case DW_TAG_member:
9888 case DW_TAG_imported_declaration:
9889 return 1;
9890
9891 case DW_TAG_variable:
9892 case DW_TAG_constant:
9893 /* We only need to prefix "globally" visible variables. These include
9894 any variable marked with DW_AT_external or any variable that
9895 lives in a namespace. [Variables in anonymous namespaces
9896 require prefixing, but they are not DW_AT_external.] */
9897
9898 if (dwarf2_attr (die, DW_AT_specification, cu))
9899 {
9900 struct dwarf2_cu *spec_cu = cu;
9901
9902 return die_needs_namespace (die_specification (die, &spec_cu),
9903 spec_cu);
9904 }
9905
9906 attr = dwarf2_attr (die, DW_AT_external, cu);
9907 if (attr == NULL && die->parent->tag != DW_TAG_namespace
9908 && die->parent->tag != DW_TAG_module)
9909 return 0;
9910 /* A variable in a lexical block of some kind does not need a
9911 namespace, even though in C++ such variables may be external
9912 and have a mangled name. */
9913 if (die->parent->tag == DW_TAG_lexical_block
9914 || die->parent->tag == DW_TAG_try_block
9915 || die->parent->tag == DW_TAG_catch_block
9916 || die->parent->tag == DW_TAG_subprogram)
9917 return 0;
9918 return 1;
9919
9920 default:
9921 return 0;
9922 }
9923 }
9924
9925 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
9926 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9927 defined for the given DIE. */
9928
9929 static struct attribute *
9930 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
9931 {
9932 struct attribute *attr;
9933
9934 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
9935 if (attr == NULL)
9936 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
9937
9938 return attr;
9939 }
9940
9941 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
9942 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9943 defined for the given DIE. */
9944
9945 static const char *
9946 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
9947 {
9948 const char *linkage_name;
9949
9950 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
9951 if (linkage_name == NULL)
9952 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
9953
9954 return linkage_name;
9955 }
9956
9957 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
9958 compute the physname for the object, which include a method's:
9959 - formal parameters (C++),
9960 - receiver type (Go),
9961
9962 The term "physname" is a bit confusing.
9963 For C++, for example, it is the demangled name.
9964 For Go, for example, it's the mangled name.
9965
9966 For Ada, return the DIE's linkage name rather than the fully qualified
9967 name. PHYSNAME is ignored..
9968
9969 The result is allocated on the objfile_obstack and canonicalized. */
9970
9971 static const char *
9972 dwarf2_compute_name (const char *name,
9973 struct die_info *die, struct dwarf2_cu *cu,
9974 int physname)
9975 {
9976 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9977
9978 if (name == NULL)
9979 name = dwarf2_name (die, cu);
9980
9981 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
9982 but otherwise compute it by typename_concat inside GDB.
9983 FIXME: Actually this is not really true, or at least not always true.
9984 It's all very confusing. compute_and_set_names doesn't try to demangle
9985 Fortran names because there is no mangling standard. So new_symbol
9986 will set the demangled name to the result of dwarf2_full_name, and it is
9987 the demangled name that GDB uses if it exists. */
9988 if (cu->language == language_ada
9989 || (cu->language == language_fortran && physname))
9990 {
9991 /* For Ada unit, we prefer the linkage name over the name, as
9992 the former contains the exported name, which the user expects
9993 to be able to reference. Ideally, we want the user to be able
9994 to reference this entity using either natural or linkage name,
9995 but we haven't started looking at this enhancement yet. */
9996 const char *linkage_name = dw2_linkage_name (die, cu);
9997
9998 if (linkage_name != NULL)
9999 return linkage_name;
10000 }
10001
10002 /* These are the only languages we know how to qualify names in. */
10003 if (name != NULL
10004 && (cu->language == language_cplus
10005 || cu->language == language_fortran || cu->language == language_d
10006 || cu->language == language_rust))
10007 {
10008 if (die_needs_namespace (die, cu))
10009 {
10010 const char *prefix;
10011 const char *canonical_name = NULL;
10012
10013 string_file buf;
10014
10015 prefix = determine_prefix (die, cu);
10016 if (*prefix != '\0')
10017 {
10018 gdb::unique_xmalloc_ptr<char> prefixed_name
10019 (typename_concat (NULL, prefix, name, physname, cu));
10020
10021 buf.puts (prefixed_name.get ());
10022 }
10023 else
10024 buf.puts (name);
10025
10026 /* Template parameters may be specified in the DIE's DW_AT_name, or
10027 as children with DW_TAG_template_type_param or
10028 DW_TAG_value_type_param. If the latter, add them to the name
10029 here. If the name already has template parameters, then
10030 skip this step; some versions of GCC emit both, and
10031 it is more efficient to use the pre-computed name.
10032
10033 Something to keep in mind about this process: it is very
10034 unlikely, or in some cases downright impossible, to produce
10035 something that will match the mangled name of a function.
10036 If the definition of the function has the same debug info,
10037 we should be able to match up with it anyway. But fallbacks
10038 using the minimal symbol, for instance to find a method
10039 implemented in a stripped copy of libstdc++, will not work.
10040 If we do not have debug info for the definition, we will have to
10041 match them up some other way.
10042
10043 When we do name matching there is a related problem with function
10044 templates; two instantiated function templates are allowed to
10045 differ only by their return types, which we do not add here. */
10046
10047 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10048 {
10049 struct attribute *attr;
10050 struct die_info *child;
10051 int first = 1;
10052
10053 die->building_fullname = 1;
10054
10055 for (child = die->child; child != NULL; child = child->sibling)
10056 {
10057 struct type *type;
10058 LONGEST value;
10059 const gdb_byte *bytes;
10060 struct dwarf2_locexpr_baton *baton;
10061 struct value *v;
10062
10063 if (child->tag != DW_TAG_template_type_param
10064 && child->tag != DW_TAG_template_value_param)
10065 continue;
10066
10067 if (first)
10068 {
10069 buf.puts ("<");
10070 first = 0;
10071 }
10072 else
10073 buf.puts (", ");
10074
10075 attr = dwarf2_attr (child, DW_AT_type, cu);
10076 if (attr == NULL)
10077 {
10078 complaint (_("template parameter missing DW_AT_type"));
10079 buf.puts ("UNKNOWN_TYPE");
10080 continue;
10081 }
10082 type = die_type (child, cu);
10083
10084 if (child->tag == DW_TAG_template_type_param)
10085 {
10086 c_print_type (type, "", &buf, -1, 0, cu->language,
10087 &type_print_raw_options);
10088 continue;
10089 }
10090
10091 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10092 if (attr == NULL)
10093 {
10094 complaint (_("template parameter missing "
10095 "DW_AT_const_value"));
10096 buf.puts ("UNKNOWN_VALUE");
10097 continue;
10098 }
10099
10100 dwarf2_const_value_attr (attr, type, name,
10101 &cu->comp_unit_obstack, cu,
10102 &value, &bytes, &baton);
10103
10104 if (TYPE_NOSIGN (type))
10105 /* GDB prints characters as NUMBER 'CHAR'. If that's
10106 changed, this can use value_print instead. */
10107 c_printchar (value, type, &buf);
10108 else
10109 {
10110 struct value_print_options opts;
10111
10112 if (baton != NULL)
10113 v = dwarf2_evaluate_loc_desc (type, NULL,
10114 baton->data,
10115 baton->size,
10116 baton->per_cu);
10117 else if (bytes != NULL)
10118 {
10119 v = allocate_value (type);
10120 memcpy (value_contents_writeable (v), bytes,
10121 TYPE_LENGTH (type));
10122 }
10123 else
10124 v = value_from_longest (type, value);
10125
10126 /* Specify decimal so that we do not depend on
10127 the radix. */
10128 get_formatted_print_options (&opts, 'd');
10129 opts.raw = 1;
10130 value_print (v, &buf, &opts);
10131 release_value (v);
10132 }
10133 }
10134
10135 die->building_fullname = 0;
10136
10137 if (!first)
10138 {
10139 /* Close the argument list, with a space if necessary
10140 (nested templates). */
10141 if (!buf.empty () && buf.string ().back () == '>')
10142 buf.puts (" >");
10143 else
10144 buf.puts (">");
10145 }
10146 }
10147
10148 /* For C++ methods, append formal parameter type
10149 information, if PHYSNAME. */
10150
10151 if (physname && die->tag == DW_TAG_subprogram
10152 && cu->language == language_cplus)
10153 {
10154 struct type *type = read_type_die (die, cu);
10155
10156 c_type_print_args (type, &buf, 1, cu->language,
10157 &type_print_raw_options);
10158
10159 if (cu->language == language_cplus)
10160 {
10161 /* Assume that an artificial first parameter is
10162 "this", but do not crash if it is not. RealView
10163 marks unnamed (and thus unused) parameters as
10164 artificial; there is no way to differentiate
10165 the two cases. */
10166 if (TYPE_NFIELDS (type) > 0
10167 && TYPE_FIELD_ARTIFICIAL (type, 0)
10168 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
10169 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10170 0))))
10171 buf.puts (" const");
10172 }
10173 }
10174
10175 const std::string &intermediate_name = buf.string ();
10176
10177 if (cu->language == language_cplus)
10178 canonical_name
10179 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
10180 &objfile->per_bfd->storage_obstack);
10181
10182 /* If we only computed INTERMEDIATE_NAME, or if
10183 INTERMEDIATE_NAME is already canonical, then we need to
10184 copy it to the appropriate obstack. */
10185 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
10186 name = obstack_strdup (&objfile->per_bfd->storage_obstack,
10187 intermediate_name);
10188 else
10189 name = canonical_name;
10190 }
10191 }
10192
10193 return name;
10194 }
10195
10196 /* Return the fully qualified name of DIE, based on its DW_AT_name.
10197 If scope qualifiers are appropriate they will be added. The result
10198 will be allocated on the storage_obstack, or NULL if the DIE does
10199 not have a name. NAME may either be from a previous call to
10200 dwarf2_name or NULL.
10201
10202 The output string will be canonicalized (if C++). */
10203
10204 static const char *
10205 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10206 {
10207 return dwarf2_compute_name (name, die, cu, 0);
10208 }
10209
10210 /* Construct a physname for the given DIE in CU. NAME may either be
10211 from a previous call to dwarf2_name or NULL. The result will be
10212 allocated on the objfile_objstack or NULL if the DIE does not have a
10213 name.
10214
10215 The output string will be canonicalized (if C++). */
10216
10217 static const char *
10218 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10219 {
10220 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10221 const char *retval, *mangled = NULL, *canon = NULL;
10222 int need_copy = 1;
10223
10224 /* In this case dwarf2_compute_name is just a shortcut not building anything
10225 on its own. */
10226 if (!die_needs_namespace (die, cu))
10227 return dwarf2_compute_name (name, die, cu, 1);
10228
10229 mangled = dw2_linkage_name (die, cu);
10230
10231 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10232 See https://github.com/rust-lang/rust/issues/32925. */
10233 if (cu->language == language_rust && mangled != NULL
10234 && strchr (mangled, '{') != NULL)
10235 mangled = NULL;
10236
10237 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10238 has computed. */
10239 gdb::unique_xmalloc_ptr<char> demangled;
10240 if (mangled != NULL)
10241 {
10242
10243 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
10244 {
10245 /* Do nothing (do not demangle the symbol name). */
10246 }
10247 else if (cu->language == language_go)
10248 {
10249 /* This is a lie, but we already lie to the caller new_symbol.
10250 new_symbol assumes we return the mangled name.
10251 This just undoes that lie until things are cleaned up. */
10252 }
10253 else
10254 {
10255 /* Use DMGL_RET_DROP for C++ template functions to suppress
10256 their return type. It is easier for GDB users to search
10257 for such functions as `name(params)' than `long name(params)'.
10258 In such case the minimal symbol names do not match the full
10259 symbol names but for template functions there is never a need
10260 to look up their definition from their declaration so
10261 the only disadvantage remains the minimal symbol variant
10262 `long name(params)' does not have the proper inferior type. */
10263 demangled.reset (gdb_demangle (mangled,
10264 (DMGL_PARAMS | DMGL_ANSI
10265 | DMGL_RET_DROP)));
10266 }
10267 if (demangled)
10268 canon = demangled.get ();
10269 else
10270 {
10271 canon = mangled;
10272 need_copy = 0;
10273 }
10274 }
10275
10276 if (canon == NULL || check_physname)
10277 {
10278 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10279
10280 if (canon != NULL && strcmp (physname, canon) != 0)
10281 {
10282 /* It may not mean a bug in GDB. The compiler could also
10283 compute DW_AT_linkage_name incorrectly. But in such case
10284 GDB would need to be bug-to-bug compatible. */
10285
10286 complaint (_("Computed physname <%s> does not match demangled <%s> "
10287 "(from linkage <%s>) - DIE at %s [in module %s]"),
10288 physname, canon, mangled, sect_offset_str (die->sect_off),
10289 objfile_name (objfile));
10290
10291 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10292 is available here - over computed PHYSNAME. It is safer
10293 against both buggy GDB and buggy compilers. */
10294
10295 retval = canon;
10296 }
10297 else
10298 {
10299 retval = physname;
10300 need_copy = 0;
10301 }
10302 }
10303 else
10304 retval = canon;
10305
10306 if (need_copy)
10307 retval = obstack_strdup (&objfile->per_bfd->storage_obstack, retval);
10308
10309 return retval;
10310 }
10311
10312 /* Inspect DIE in CU for a namespace alias. If one exists, record
10313 a new symbol for it.
10314
10315 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10316
10317 static int
10318 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10319 {
10320 struct attribute *attr;
10321
10322 /* If the die does not have a name, this is not a namespace
10323 alias. */
10324 attr = dwarf2_attr (die, DW_AT_name, cu);
10325 if (attr != NULL)
10326 {
10327 int num;
10328 struct die_info *d = die;
10329 struct dwarf2_cu *imported_cu = cu;
10330
10331 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10332 keep inspecting DIEs until we hit the underlying import. */
10333 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
10334 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10335 {
10336 attr = dwarf2_attr (d, DW_AT_import, cu);
10337 if (attr == NULL)
10338 break;
10339
10340 d = follow_die_ref (d, attr, &imported_cu);
10341 if (d->tag != DW_TAG_imported_declaration)
10342 break;
10343 }
10344
10345 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10346 {
10347 complaint (_("DIE at %s has too many recursively imported "
10348 "declarations"), sect_offset_str (d->sect_off));
10349 return 0;
10350 }
10351
10352 if (attr != NULL)
10353 {
10354 struct type *type;
10355 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10356
10357 type = get_die_type_at_offset (sect_off, cu->per_cu);
10358 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
10359 {
10360 /* This declaration is a global namespace alias. Add
10361 a symbol for it whose type is the aliased namespace. */
10362 new_symbol (die, type, cu);
10363 return 1;
10364 }
10365 }
10366 }
10367
10368 return 0;
10369 }
10370
10371 /* Return the using directives repository (global or local?) to use in the
10372 current context for CU.
10373
10374 For Ada, imported declarations can materialize renamings, which *may* be
10375 global. However it is impossible (for now?) in DWARF to distinguish
10376 "external" imported declarations and "static" ones. As all imported
10377 declarations seem to be static in all other languages, make them all CU-wide
10378 global only in Ada. */
10379
10380 static struct using_direct **
10381 using_directives (struct dwarf2_cu *cu)
10382 {
10383 if (cu->language == language_ada
10384 && cu->get_builder ()->outermost_context_p ())
10385 return cu->get_builder ()->get_global_using_directives ();
10386 else
10387 return cu->get_builder ()->get_local_using_directives ();
10388 }
10389
10390 /* Read the import statement specified by the given die and record it. */
10391
10392 static void
10393 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10394 {
10395 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10396 struct attribute *import_attr;
10397 struct die_info *imported_die, *child_die;
10398 struct dwarf2_cu *imported_cu;
10399 const char *imported_name;
10400 const char *imported_name_prefix;
10401 const char *canonical_name;
10402 const char *import_alias;
10403 const char *imported_declaration = NULL;
10404 const char *import_prefix;
10405 std::vector<const char *> excludes;
10406
10407 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10408 if (import_attr == NULL)
10409 {
10410 complaint (_("Tag '%s' has no DW_AT_import"),
10411 dwarf_tag_name (die->tag));
10412 return;
10413 }
10414
10415 imported_cu = cu;
10416 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10417 imported_name = dwarf2_name (imported_die, imported_cu);
10418 if (imported_name == NULL)
10419 {
10420 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10421
10422 The import in the following code:
10423 namespace A
10424 {
10425 typedef int B;
10426 }
10427
10428 int main ()
10429 {
10430 using A::B;
10431 B b;
10432 return b;
10433 }
10434
10435 ...
10436 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10437 <52> DW_AT_decl_file : 1
10438 <53> DW_AT_decl_line : 6
10439 <54> DW_AT_import : <0x75>
10440 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10441 <59> DW_AT_name : B
10442 <5b> DW_AT_decl_file : 1
10443 <5c> DW_AT_decl_line : 2
10444 <5d> DW_AT_type : <0x6e>
10445 ...
10446 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10447 <76> DW_AT_byte_size : 4
10448 <77> DW_AT_encoding : 5 (signed)
10449
10450 imports the wrong die ( 0x75 instead of 0x58 ).
10451 This case will be ignored until the gcc bug is fixed. */
10452 return;
10453 }
10454
10455 /* Figure out the local name after import. */
10456 import_alias = dwarf2_name (die, cu);
10457
10458 /* Figure out where the statement is being imported to. */
10459 import_prefix = determine_prefix (die, cu);
10460
10461 /* Figure out what the scope of the imported die is and prepend it
10462 to the name of the imported die. */
10463 imported_name_prefix = determine_prefix (imported_die, imported_cu);
10464
10465 if (imported_die->tag != DW_TAG_namespace
10466 && imported_die->tag != DW_TAG_module)
10467 {
10468 imported_declaration = imported_name;
10469 canonical_name = imported_name_prefix;
10470 }
10471 else if (strlen (imported_name_prefix) > 0)
10472 canonical_name = obconcat (&objfile->objfile_obstack,
10473 imported_name_prefix,
10474 (cu->language == language_d ? "." : "::"),
10475 imported_name, (char *) NULL);
10476 else
10477 canonical_name = imported_name;
10478
10479 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10480 for (child_die = die->child; child_die && child_die->tag;
10481 child_die = sibling_die (child_die))
10482 {
10483 /* DWARF-4: A Fortran use statement with a “rename list” may be
10484 represented by an imported module entry with an import attribute
10485 referring to the module and owned entries corresponding to those
10486 entities that are renamed as part of being imported. */
10487
10488 if (child_die->tag != DW_TAG_imported_declaration)
10489 {
10490 complaint (_("child DW_TAG_imported_declaration expected "
10491 "- DIE at %s [in module %s]"),
10492 sect_offset_str (child_die->sect_off),
10493 objfile_name (objfile));
10494 continue;
10495 }
10496
10497 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10498 if (import_attr == NULL)
10499 {
10500 complaint (_("Tag '%s' has no DW_AT_import"),
10501 dwarf_tag_name (child_die->tag));
10502 continue;
10503 }
10504
10505 imported_cu = cu;
10506 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10507 &imported_cu);
10508 imported_name = dwarf2_name (imported_die, imported_cu);
10509 if (imported_name == NULL)
10510 {
10511 complaint (_("child DW_TAG_imported_declaration has unknown "
10512 "imported name - DIE at %s [in module %s]"),
10513 sect_offset_str (child_die->sect_off),
10514 objfile_name (objfile));
10515 continue;
10516 }
10517
10518 excludes.push_back (imported_name);
10519
10520 process_die (child_die, cu);
10521 }
10522
10523 add_using_directive (using_directives (cu),
10524 import_prefix,
10525 canonical_name,
10526 import_alias,
10527 imported_declaration,
10528 excludes,
10529 0,
10530 &objfile->objfile_obstack);
10531 }
10532
10533 /* ICC<14 does not output the required DW_AT_declaration on incomplete
10534 types, but gives them a size of zero. Starting with version 14,
10535 ICC is compatible with GCC. */
10536
10537 static bool
10538 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10539 {
10540 if (!cu->checked_producer)
10541 check_producer (cu);
10542
10543 return cu->producer_is_icc_lt_14;
10544 }
10545
10546 /* ICC generates a DW_AT_type for C void functions. This was observed on
10547 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10548 which says that void functions should not have a DW_AT_type. */
10549
10550 static bool
10551 producer_is_icc (struct dwarf2_cu *cu)
10552 {
10553 if (!cu->checked_producer)
10554 check_producer (cu);
10555
10556 return cu->producer_is_icc;
10557 }
10558
10559 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10560 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10561 this, it was first present in GCC release 4.3.0. */
10562
10563 static bool
10564 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10565 {
10566 if (!cu->checked_producer)
10567 check_producer (cu);
10568
10569 return cu->producer_is_gcc_lt_4_3;
10570 }
10571
10572 static file_and_directory
10573 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
10574 {
10575 file_and_directory res;
10576
10577 /* Find the filename. Do not use dwarf2_name here, since the filename
10578 is not a source language identifier. */
10579 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10580 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
10581
10582 if (res.comp_dir == NULL
10583 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10584 && IS_ABSOLUTE_PATH (res.name))
10585 {
10586 res.comp_dir_storage = ldirname (res.name);
10587 if (!res.comp_dir_storage.empty ())
10588 res.comp_dir = res.comp_dir_storage.c_str ();
10589 }
10590 if (res.comp_dir != NULL)
10591 {
10592 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10593 directory, get rid of it. */
10594 const char *cp = strchr (res.comp_dir, ':');
10595
10596 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10597 res.comp_dir = cp + 1;
10598 }
10599
10600 if (res.name == NULL)
10601 res.name = "<unknown>";
10602
10603 return res;
10604 }
10605
10606 /* Handle DW_AT_stmt_list for a compilation unit.
10607 DIE is the DW_TAG_compile_unit die for CU.
10608 COMP_DIR is the compilation directory. LOWPC is passed to
10609 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
10610
10611 static void
10612 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
10613 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
10614 {
10615 struct dwarf2_per_objfile *dwarf2_per_objfile
10616 = cu->per_cu->dwarf2_per_objfile;
10617 struct attribute *attr;
10618 struct line_header line_header_local;
10619 hashval_t line_header_local_hash;
10620 void **slot;
10621 int decode_mapping;
10622
10623 gdb_assert (! cu->per_cu->is_debug_types);
10624
10625 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
10626 if (attr == NULL)
10627 return;
10628
10629 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
10630
10631 /* The line header hash table is only created if needed (it exists to
10632 prevent redundant reading of the line table for partial_units).
10633 If we're given a partial_unit, we'll need it. If we're given a
10634 compile_unit, then use the line header hash table if it's already
10635 created, but don't create one just yet. */
10636
10637 if (dwarf2_per_objfile->line_header_hash == NULL
10638 && die->tag == DW_TAG_partial_unit)
10639 {
10640 dwarf2_per_objfile->line_header_hash
10641 .reset (htab_create_alloc (127, line_header_hash_voidp,
10642 line_header_eq_voidp,
10643 free_line_header_voidp,
10644 xcalloc, xfree));
10645 }
10646
10647 line_header_local.sect_off = line_offset;
10648 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10649 line_header_local_hash = line_header_hash (&line_header_local);
10650 if (dwarf2_per_objfile->line_header_hash != NULL)
10651 {
10652 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash.get (),
10653 &line_header_local,
10654 line_header_local_hash, NO_INSERT);
10655
10656 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10657 is not present in *SLOT (since if there is something in *SLOT then
10658 it will be for a partial_unit). */
10659 if (die->tag == DW_TAG_partial_unit && slot != NULL)
10660 {
10661 gdb_assert (*slot != NULL);
10662 cu->line_header = (struct line_header *) *slot;
10663 return;
10664 }
10665 }
10666
10667 /* dwarf_decode_line_header does not yet provide sufficient information.
10668 We always have to call also dwarf_decode_lines for it. */
10669 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10670 if (lh == NULL)
10671 return;
10672
10673 cu->line_header = lh.release ();
10674 cu->line_header_die_owner = die;
10675
10676 if (dwarf2_per_objfile->line_header_hash == NULL)
10677 slot = NULL;
10678 else
10679 {
10680 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash.get (),
10681 &line_header_local,
10682 line_header_local_hash, INSERT);
10683 gdb_assert (slot != NULL);
10684 }
10685 if (slot != NULL && *slot == NULL)
10686 {
10687 /* This newly decoded line number information unit will be owned
10688 by line_header_hash hash table. */
10689 *slot = cu->line_header;
10690 cu->line_header_die_owner = NULL;
10691 }
10692 else
10693 {
10694 /* We cannot free any current entry in (*slot) as that struct line_header
10695 may be already used by multiple CUs. Create only temporary decoded
10696 line_header for this CU - it may happen at most once for each line
10697 number information unit. And if we're not using line_header_hash
10698 then this is what we want as well. */
10699 gdb_assert (die->tag != DW_TAG_partial_unit);
10700 }
10701 decode_mapping = (die->tag != DW_TAG_partial_unit);
10702 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
10703 decode_mapping);
10704
10705 }
10706
10707 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
10708
10709 static void
10710 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
10711 {
10712 struct dwarf2_per_objfile *dwarf2_per_objfile
10713 = cu->per_cu->dwarf2_per_objfile;
10714 struct objfile *objfile = dwarf2_per_objfile->objfile;
10715 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10716 CORE_ADDR lowpc = ((CORE_ADDR) -1);
10717 CORE_ADDR highpc = ((CORE_ADDR) 0);
10718 struct attribute *attr;
10719 struct die_info *child_die;
10720 CORE_ADDR baseaddr;
10721
10722 prepare_one_comp_unit (cu, die, cu->language);
10723 baseaddr = objfile->text_section_offset ();
10724
10725 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
10726
10727 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10728 from finish_block. */
10729 if (lowpc == ((CORE_ADDR) -1))
10730 lowpc = highpc;
10731 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
10732
10733 file_and_directory fnd = find_file_and_directory (die, cu);
10734
10735 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
10736 standardised yet. As a workaround for the language detection we fall
10737 back to the DW_AT_producer string. */
10738 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
10739 cu->language = language_opencl;
10740
10741 /* Similar hack for Go. */
10742 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
10743 set_cu_language (DW_LANG_Go, cu);
10744
10745 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
10746
10747 /* Decode line number information if present. We do this before
10748 processing child DIEs, so that the line header table is available
10749 for DW_AT_decl_file. */
10750 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
10751
10752 /* Process all dies in compilation unit. */
10753 if (die->child != NULL)
10754 {
10755 child_die = die->child;
10756 while (child_die && child_die->tag)
10757 {
10758 process_die (child_die, cu);
10759 child_die = sibling_die (child_die);
10760 }
10761 }
10762
10763 /* Decode macro information, if present. Dwarf 2 macro information
10764 refers to information in the line number info statement program
10765 header, so we can only read it if we've read the header
10766 successfully. */
10767 attr = dwarf2_attr (die, DW_AT_macros, cu);
10768 if (attr == NULL)
10769 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
10770 if (attr && cu->line_header)
10771 {
10772 if (dwarf2_attr (die, DW_AT_macro_info, cu))
10773 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
10774
10775 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
10776 }
10777 else
10778 {
10779 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
10780 if (attr && cu->line_header)
10781 {
10782 unsigned int macro_offset = DW_UNSND (attr);
10783
10784 dwarf_decode_macros (cu, macro_offset, 0);
10785 }
10786 }
10787 }
10788
10789 void
10790 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
10791 {
10792 struct type_unit_group *tu_group;
10793 int first_time;
10794 struct attribute *attr;
10795 unsigned int i;
10796 struct signatured_type *sig_type;
10797
10798 gdb_assert (per_cu->is_debug_types);
10799 sig_type = (struct signatured_type *) per_cu;
10800
10801 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
10802
10803 /* If we're using .gdb_index (includes -readnow) then
10804 per_cu->type_unit_group may not have been set up yet. */
10805 if (sig_type->type_unit_group == NULL)
10806 sig_type->type_unit_group = get_type_unit_group (this, attr);
10807 tu_group = sig_type->type_unit_group;
10808
10809 /* If we've already processed this stmt_list there's no real need to
10810 do it again, we could fake it and just recreate the part we need
10811 (file name,index -> symtab mapping). If data shows this optimization
10812 is useful we can do it then. */
10813 first_time = tu_group->compunit_symtab == NULL;
10814
10815 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
10816 debug info. */
10817 line_header_up lh;
10818 if (attr != NULL)
10819 {
10820 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
10821 lh = dwarf_decode_line_header (line_offset, this);
10822 }
10823 if (lh == NULL)
10824 {
10825 if (first_time)
10826 start_symtab ("", NULL, 0);
10827 else
10828 {
10829 gdb_assert (tu_group->symtabs == NULL);
10830 gdb_assert (m_builder == nullptr);
10831 struct compunit_symtab *cust = tu_group->compunit_symtab;
10832 m_builder.reset (new struct buildsym_compunit
10833 (COMPUNIT_OBJFILE (cust), "",
10834 COMPUNIT_DIRNAME (cust),
10835 compunit_language (cust),
10836 0, cust));
10837 }
10838 return;
10839 }
10840
10841 line_header = lh.release ();
10842 line_header_die_owner = die;
10843
10844 if (first_time)
10845 {
10846 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
10847
10848 /* Note: We don't assign tu_group->compunit_symtab yet because we're
10849 still initializing it, and our caller (a few levels up)
10850 process_full_type_unit still needs to know if this is the first
10851 time. */
10852
10853 tu_group->symtabs
10854 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
10855 struct symtab *, line_header->file_names_size ());
10856
10857 auto &file_names = line_header->file_names ();
10858 for (i = 0; i < file_names.size (); ++i)
10859 {
10860 file_entry &fe = file_names[i];
10861 dwarf2_start_subfile (this, fe.name,
10862 fe.include_dir (line_header));
10863 buildsym_compunit *b = get_builder ();
10864 if (b->get_current_subfile ()->symtab == NULL)
10865 {
10866 /* NOTE: start_subfile will recognize when it's been
10867 passed a file it has already seen. So we can't
10868 assume there's a simple mapping from
10869 cu->line_header->file_names to subfiles, plus
10870 cu->line_header->file_names may contain dups. */
10871 b->get_current_subfile ()->symtab
10872 = allocate_symtab (cust, b->get_current_subfile ()->name);
10873 }
10874
10875 fe.symtab = b->get_current_subfile ()->symtab;
10876 tu_group->symtabs[i] = fe.symtab;
10877 }
10878 }
10879 else
10880 {
10881 gdb_assert (m_builder == nullptr);
10882 struct compunit_symtab *cust = tu_group->compunit_symtab;
10883 m_builder.reset (new struct buildsym_compunit
10884 (COMPUNIT_OBJFILE (cust), "",
10885 COMPUNIT_DIRNAME (cust),
10886 compunit_language (cust),
10887 0, cust));
10888
10889 auto &file_names = line_header->file_names ();
10890 for (i = 0; i < file_names.size (); ++i)
10891 {
10892 file_entry &fe = file_names[i];
10893 fe.symtab = tu_group->symtabs[i];
10894 }
10895 }
10896
10897 /* The main symtab is allocated last. Type units don't have DW_AT_name
10898 so they don't have a "real" (so to speak) symtab anyway.
10899 There is later code that will assign the main symtab to all symbols
10900 that don't have one. We need to handle the case of a symbol with a
10901 missing symtab (DW_AT_decl_file) anyway. */
10902 }
10903
10904 /* Process DW_TAG_type_unit.
10905 For TUs we want to skip the first top level sibling if it's not the
10906 actual type being defined by this TU. In this case the first top
10907 level sibling is there to provide context only. */
10908
10909 static void
10910 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
10911 {
10912 struct die_info *child_die;
10913
10914 prepare_one_comp_unit (cu, die, language_minimal);
10915
10916 /* Initialize (or reinitialize) the machinery for building symtabs.
10917 We do this before processing child DIEs, so that the line header table
10918 is available for DW_AT_decl_file. */
10919 cu->setup_type_unit_groups (die);
10920
10921 if (die->child != NULL)
10922 {
10923 child_die = die->child;
10924 while (child_die && child_die->tag)
10925 {
10926 process_die (child_die, cu);
10927 child_die = sibling_die (child_die);
10928 }
10929 }
10930 }
10931 \f
10932 /* DWO/DWP files.
10933
10934 http://gcc.gnu.org/wiki/DebugFission
10935 http://gcc.gnu.org/wiki/DebugFissionDWP
10936
10937 To simplify handling of both DWO files ("object" files with the DWARF info)
10938 and DWP files (a file with the DWOs packaged up into one file), we treat
10939 DWP files as having a collection of virtual DWO files. */
10940
10941 static hashval_t
10942 hash_dwo_file (const void *item)
10943 {
10944 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
10945 hashval_t hash;
10946
10947 hash = htab_hash_string (dwo_file->dwo_name);
10948 if (dwo_file->comp_dir != NULL)
10949 hash += htab_hash_string (dwo_file->comp_dir);
10950 return hash;
10951 }
10952
10953 static int
10954 eq_dwo_file (const void *item_lhs, const void *item_rhs)
10955 {
10956 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
10957 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
10958
10959 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
10960 return 0;
10961 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
10962 return lhs->comp_dir == rhs->comp_dir;
10963 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
10964 }
10965
10966 /* Allocate a hash table for DWO files. */
10967
10968 static htab_up
10969 allocate_dwo_file_hash_table ()
10970 {
10971 auto delete_dwo_file = [] (void *item)
10972 {
10973 struct dwo_file *dwo_file = (struct dwo_file *) item;
10974
10975 delete dwo_file;
10976 };
10977
10978 return htab_up (htab_create_alloc (41,
10979 hash_dwo_file,
10980 eq_dwo_file,
10981 delete_dwo_file,
10982 xcalloc, xfree));
10983 }
10984
10985 /* Lookup DWO file DWO_NAME. */
10986
10987 static void **
10988 lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
10989 const char *dwo_name,
10990 const char *comp_dir)
10991 {
10992 struct dwo_file find_entry;
10993 void **slot;
10994
10995 if (dwarf2_per_objfile->dwo_files == NULL)
10996 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
10997
10998 find_entry.dwo_name = dwo_name;
10999 find_entry.comp_dir = comp_dir;
11000 slot = htab_find_slot (dwarf2_per_objfile->dwo_files.get (), &find_entry,
11001 INSERT);
11002
11003 return slot;
11004 }
11005
11006 static hashval_t
11007 hash_dwo_unit (const void *item)
11008 {
11009 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11010
11011 /* This drops the top 32 bits of the id, but is ok for a hash. */
11012 return dwo_unit->signature;
11013 }
11014
11015 static int
11016 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11017 {
11018 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11019 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11020
11021 /* The signature is assumed to be unique within the DWO file.
11022 So while object file CU dwo_id's always have the value zero,
11023 that's OK, assuming each object file DWO file has only one CU,
11024 and that's the rule for now. */
11025 return lhs->signature == rhs->signature;
11026 }
11027
11028 /* Allocate a hash table for DWO CUs,TUs.
11029 There is one of these tables for each of CUs,TUs for each DWO file. */
11030
11031 static htab_up
11032 allocate_dwo_unit_table ()
11033 {
11034 /* Start out with a pretty small number.
11035 Generally DWO files contain only one CU and maybe some TUs. */
11036 return htab_up (htab_create_alloc (3,
11037 hash_dwo_unit,
11038 eq_dwo_unit,
11039 NULL, xcalloc, xfree));
11040 }
11041
11042 /* die_reader_func for create_dwo_cu. */
11043
11044 static void
11045 create_dwo_cu_reader (const struct die_reader_specs *reader,
11046 const gdb_byte *info_ptr,
11047 struct die_info *comp_unit_die,
11048 struct dwo_file *dwo_file,
11049 struct dwo_unit *dwo_unit)
11050 {
11051 struct dwarf2_cu *cu = reader->cu;
11052 sect_offset sect_off = cu->per_cu->sect_off;
11053 struct dwarf2_section_info *section = cu->per_cu->section;
11054
11055 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11056 if (!signature.has_value ())
11057 {
11058 complaint (_("Dwarf Error: debug entry at offset %s is missing"
11059 " its dwo_id [in module %s]"),
11060 sect_offset_str (sect_off), dwo_file->dwo_name);
11061 return;
11062 }
11063
11064 dwo_unit->dwo_file = dwo_file;
11065 dwo_unit->signature = *signature;
11066 dwo_unit->section = section;
11067 dwo_unit->sect_off = sect_off;
11068 dwo_unit->length = cu->per_cu->length;
11069
11070 if (dwarf_read_debug)
11071 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11072 sect_offset_str (sect_off),
11073 hex_string (dwo_unit->signature));
11074 }
11075
11076 /* Create the dwo_units for the CUs in a DWO_FILE.
11077 Note: This function processes DWO files only, not DWP files. */
11078
11079 static void
11080 create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11081 dwarf2_cu *cu, struct dwo_file &dwo_file,
11082 dwarf2_section_info &section, htab_up &cus_htab)
11083 {
11084 struct objfile *objfile = dwarf2_per_objfile->objfile;
11085 const gdb_byte *info_ptr, *end_ptr;
11086
11087 section.read (objfile);
11088 info_ptr = section.buffer;
11089
11090 if (info_ptr == NULL)
11091 return;
11092
11093 if (dwarf_read_debug)
11094 {
11095 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
11096 section.get_name (),
11097 section.get_file_name ());
11098 }
11099
11100 end_ptr = info_ptr + section.size;
11101 while (info_ptr < end_ptr)
11102 {
11103 struct dwarf2_per_cu_data per_cu;
11104 struct dwo_unit read_unit {};
11105 struct dwo_unit *dwo_unit;
11106 void **slot;
11107 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11108
11109 memset (&per_cu, 0, sizeof (per_cu));
11110 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
11111 per_cu.is_debug_types = 0;
11112 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11113 per_cu.section = &section;
11114
11115 cutu_reader reader (&per_cu, cu, &dwo_file);
11116 if (!reader.dummy_p)
11117 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
11118 &dwo_file, &read_unit);
11119 info_ptr += per_cu.length;
11120
11121 // If the unit could not be parsed, skip it.
11122 if (read_unit.dwo_file == NULL)
11123 continue;
11124
11125 if (cus_htab == NULL)
11126 cus_htab = allocate_dwo_unit_table ();
11127
11128 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11129 *dwo_unit = read_unit;
11130 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
11131 gdb_assert (slot != NULL);
11132 if (*slot != NULL)
11133 {
11134 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11135 sect_offset dup_sect_off = dup_cu->sect_off;
11136
11137 complaint (_("debug cu entry at offset %s is duplicate to"
11138 " the entry at offset %s, signature %s"),
11139 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
11140 hex_string (dwo_unit->signature));
11141 }
11142 *slot = (void *)dwo_unit;
11143 }
11144 }
11145
11146 /* DWP file .debug_{cu,tu}_index section format:
11147 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11148
11149 DWP Version 1:
11150
11151 Both index sections have the same format, and serve to map a 64-bit
11152 signature to a set of section numbers. Each section begins with a header,
11153 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11154 indexes, and a pool of 32-bit section numbers. The index sections will be
11155 aligned at 8-byte boundaries in the file.
11156
11157 The index section header consists of:
11158
11159 V, 32 bit version number
11160 -, 32 bits unused
11161 N, 32 bit number of compilation units or type units in the index
11162 M, 32 bit number of slots in the hash table
11163
11164 Numbers are recorded using the byte order of the application binary.
11165
11166 The hash table begins at offset 16 in the section, and consists of an array
11167 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11168 order of the application binary). Unused slots in the hash table are 0.
11169 (We rely on the extreme unlikeliness of a signature being exactly 0.)
11170
11171 The parallel table begins immediately after the hash table
11172 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11173 array of 32-bit indexes (using the byte order of the application binary),
11174 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11175 table contains a 32-bit index into the pool of section numbers. For unused
11176 hash table slots, the corresponding entry in the parallel table will be 0.
11177
11178 The pool of section numbers begins immediately following the hash table
11179 (at offset 16 + 12 * M from the beginning of the section). The pool of
11180 section numbers consists of an array of 32-bit words (using the byte order
11181 of the application binary). Each item in the array is indexed starting
11182 from 0. The hash table entry provides the index of the first section
11183 number in the set. Additional section numbers in the set follow, and the
11184 set is terminated by a 0 entry (section number 0 is not used in ELF).
11185
11186 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11187 section must be the first entry in the set, and the .debug_abbrev.dwo must
11188 be the second entry. Other members of the set may follow in any order.
11189
11190 ---
11191
11192 DWP Version 2:
11193
11194 DWP Version 2 combines all the .debug_info, etc. sections into one,
11195 and the entries in the index tables are now offsets into these sections.
11196 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11197 section.
11198
11199 Index Section Contents:
11200 Header
11201 Hash Table of Signatures dwp_hash_table.hash_table
11202 Parallel Table of Indices dwp_hash_table.unit_table
11203 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
11204 Table of Section Sizes dwp_hash_table.v2.sizes
11205
11206 The index section header consists of:
11207
11208 V, 32 bit version number
11209 L, 32 bit number of columns in the table of section offsets
11210 N, 32 bit number of compilation units or type units in the index
11211 M, 32 bit number of slots in the hash table
11212
11213 Numbers are recorded using the byte order of the application binary.
11214
11215 The hash table has the same format as version 1.
11216 The parallel table of indices has the same format as version 1,
11217 except that the entries are origin-1 indices into the table of sections
11218 offsets and the table of section sizes.
11219
11220 The table of offsets begins immediately following the parallel table
11221 (at offset 16 + 12 * M from the beginning of the section). The table is
11222 a two-dimensional array of 32-bit words (using the byte order of the
11223 application binary), with L columns and N+1 rows, in row-major order.
11224 Each row in the array is indexed starting from 0. The first row provides
11225 a key to the remaining rows: each column in this row provides an identifier
11226 for a debug section, and the offsets in the same column of subsequent rows
11227 refer to that section. The section identifiers are:
11228
11229 DW_SECT_INFO 1 .debug_info.dwo
11230 DW_SECT_TYPES 2 .debug_types.dwo
11231 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11232 DW_SECT_LINE 4 .debug_line.dwo
11233 DW_SECT_LOC 5 .debug_loc.dwo
11234 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11235 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11236 DW_SECT_MACRO 8 .debug_macro.dwo
11237
11238 The offsets provided by the CU and TU index sections are the base offsets
11239 for the contributions made by each CU or TU to the corresponding section
11240 in the package file. Each CU and TU header contains an abbrev_offset
11241 field, used to find the abbreviations table for that CU or TU within the
11242 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11243 be interpreted as relative to the base offset given in the index section.
11244 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11245 should be interpreted as relative to the base offset for .debug_line.dwo,
11246 and offsets into other debug sections obtained from DWARF attributes should
11247 also be interpreted as relative to the corresponding base offset.
11248
11249 The table of sizes begins immediately following the table of offsets.
11250 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11251 with L columns and N rows, in row-major order. Each row in the array is
11252 indexed starting from 1 (row 0 is shared by the two tables).
11253
11254 ---
11255
11256 Hash table lookup is handled the same in version 1 and 2:
11257
11258 We assume that N and M will not exceed 2^32 - 1.
11259 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11260
11261 Given a 64-bit compilation unit signature or a type signature S, an entry
11262 in the hash table is located as follows:
11263
11264 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11265 the low-order k bits all set to 1.
11266
11267 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
11268
11269 3) If the hash table entry at index H matches the signature, use that
11270 entry. If the hash table entry at index H is unused (all zeroes),
11271 terminate the search: the signature is not present in the table.
11272
11273 4) Let H = (H + H') modulo M. Repeat at Step 3.
11274
11275 Because M > N and H' and M are relatively prime, the search is guaranteed
11276 to stop at an unused slot or find the match. */
11277
11278 /* Create a hash table to map DWO IDs to their CU/TU entry in
11279 .debug_{info,types}.dwo in DWP_FILE.
11280 Returns NULL if there isn't one.
11281 Note: This function processes DWP files only, not DWO files. */
11282
11283 static struct dwp_hash_table *
11284 create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11285 struct dwp_file *dwp_file, int is_debug_types)
11286 {
11287 struct objfile *objfile = dwarf2_per_objfile->objfile;
11288 bfd *dbfd = dwp_file->dbfd.get ();
11289 const gdb_byte *index_ptr, *index_end;
11290 struct dwarf2_section_info *index;
11291 uint32_t version, nr_columns, nr_units, nr_slots;
11292 struct dwp_hash_table *htab;
11293
11294 if (is_debug_types)
11295 index = &dwp_file->sections.tu_index;
11296 else
11297 index = &dwp_file->sections.cu_index;
11298
11299 if (index->empty ())
11300 return NULL;
11301 index->read (objfile);
11302
11303 index_ptr = index->buffer;
11304 index_end = index_ptr + index->size;
11305
11306 version = read_4_bytes (dbfd, index_ptr);
11307 index_ptr += 4;
11308 if (version == 2)
11309 nr_columns = read_4_bytes (dbfd, index_ptr);
11310 else
11311 nr_columns = 0;
11312 index_ptr += 4;
11313 nr_units = read_4_bytes (dbfd, index_ptr);
11314 index_ptr += 4;
11315 nr_slots = read_4_bytes (dbfd, index_ptr);
11316 index_ptr += 4;
11317
11318 if (version != 1 && version != 2)
11319 {
11320 error (_("Dwarf Error: unsupported DWP file version (%s)"
11321 " [in module %s]"),
11322 pulongest (version), dwp_file->name);
11323 }
11324 if (nr_slots != (nr_slots & -nr_slots))
11325 {
11326 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
11327 " is not power of 2 [in module %s]"),
11328 pulongest (nr_slots), dwp_file->name);
11329 }
11330
11331 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
11332 htab->version = version;
11333 htab->nr_columns = nr_columns;
11334 htab->nr_units = nr_units;
11335 htab->nr_slots = nr_slots;
11336 htab->hash_table = index_ptr;
11337 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
11338
11339 /* Exit early if the table is empty. */
11340 if (nr_slots == 0 || nr_units == 0
11341 || (version == 2 && nr_columns == 0))
11342 {
11343 /* All must be zero. */
11344 if (nr_slots != 0 || nr_units != 0
11345 || (version == 2 && nr_columns != 0))
11346 {
11347 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
11348 " all zero [in modules %s]"),
11349 dwp_file->name);
11350 }
11351 return htab;
11352 }
11353
11354 if (version == 1)
11355 {
11356 htab->section_pool.v1.indices =
11357 htab->unit_table + sizeof (uint32_t) * nr_slots;
11358 /* It's harder to decide whether the section is too small in v1.
11359 V1 is deprecated anyway so we punt. */
11360 }
11361 else
11362 {
11363 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11364 int *ids = htab->section_pool.v2.section_ids;
11365 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
11366 /* Reverse map for error checking. */
11367 int ids_seen[DW_SECT_MAX + 1];
11368 int i;
11369
11370 if (nr_columns < 2)
11371 {
11372 error (_("Dwarf Error: bad DWP hash table, too few columns"
11373 " in section table [in module %s]"),
11374 dwp_file->name);
11375 }
11376 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11377 {
11378 error (_("Dwarf Error: bad DWP hash table, too many columns"
11379 " in section table [in module %s]"),
11380 dwp_file->name);
11381 }
11382 memset (ids, 255, sizeof_ids);
11383 memset (ids_seen, 255, sizeof (ids_seen));
11384 for (i = 0; i < nr_columns; ++i)
11385 {
11386 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11387
11388 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11389 {
11390 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11391 " in section table [in module %s]"),
11392 id, dwp_file->name);
11393 }
11394 if (ids_seen[id] != -1)
11395 {
11396 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11397 " id %d in section table [in module %s]"),
11398 id, dwp_file->name);
11399 }
11400 ids_seen[id] = i;
11401 ids[i] = id;
11402 }
11403 /* Must have exactly one info or types section. */
11404 if (((ids_seen[DW_SECT_INFO] != -1)
11405 + (ids_seen[DW_SECT_TYPES] != -1))
11406 != 1)
11407 {
11408 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11409 " DWO info/types section [in module %s]"),
11410 dwp_file->name);
11411 }
11412 /* Must have an abbrev section. */
11413 if (ids_seen[DW_SECT_ABBREV] == -1)
11414 {
11415 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11416 " section [in module %s]"),
11417 dwp_file->name);
11418 }
11419 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11420 htab->section_pool.v2.sizes =
11421 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11422 * nr_units * nr_columns);
11423 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11424 * nr_units * nr_columns))
11425 > index_end)
11426 {
11427 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11428 " [in module %s]"),
11429 dwp_file->name);
11430 }
11431 }
11432
11433 return htab;
11434 }
11435
11436 /* Update SECTIONS with the data from SECTP.
11437
11438 This function is like the other "locate" section routines that are
11439 passed to bfd_map_over_sections, but in this context the sections to
11440 read comes from the DWP V1 hash table, not the full ELF section table.
11441
11442 The result is non-zero for success, or zero if an error was found. */
11443
11444 static int
11445 locate_v1_virtual_dwo_sections (asection *sectp,
11446 struct virtual_v1_dwo_sections *sections)
11447 {
11448 const struct dwop_section_names *names = &dwop_section_names;
11449
11450 if (section_is_p (sectp->name, &names->abbrev_dwo))
11451 {
11452 /* There can be only one. */
11453 if (sections->abbrev.s.section != NULL)
11454 return 0;
11455 sections->abbrev.s.section = sectp;
11456 sections->abbrev.size = bfd_section_size (sectp);
11457 }
11458 else if (section_is_p (sectp->name, &names->info_dwo)
11459 || section_is_p (sectp->name, &names->types_dwo))
11460 {
11461 /* There can be only one. */
11462 if (sections->info_or_types.s.section != NULL)
11463 return 0;
11464 sections->info_or_types.s.section = sectp;
11465 sections->info_or_types.size = bfd_section_size (sectp);
11466 }
11467 else if (section_is_p (sectp->name, &names->line_dwo))
11468 {
11469 /* There can be only one. */
11470 if (sections->line.s.section != NULL)
11471 return 0;
11472 sections->line.s.section = sectp;
11473 sections->line.size = bfd_section_size (sectp);
11474 }
11475 else if (section_is_p (sectp->name, &names->loc_dwo))
11476 {
11477 /* There can be only one. */
11478 if (sections->loc.s.section != NULL)
11479 return 0;
11480 sections->loc.s.section = sectp;
11481 sections->loc.size = bfd_section_size (sectp);
11482 }
11483 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11484 {
11485 /* There can be only one. */
11486 if (sections->macinfo.s.section != NULL)
11487 return 0;
11488 sections->macinfo.s.section = sectp;
11489 sections->macinfo.size = bfd_section_size (sectp);
11490 }
11491 else if (section_is_p (sectp->name, &names->macro_dwo))
11492 {
11493 /* There can be only one. */
11494 if (sections->macro.s.section != NULL)
11495 return 0;
11496 sections->macro.s.section = sectp;
11497 sections->macro.size = bfd_section_size (sectp);
11498 }
11499 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11500 {
11501 /* There can be only one. */
11502 if (sections->str_offsets.s.section != NULL)
11503 return 0;
11504 sections->str_offsets.s.section = sectp;
11505 sections->str_offsets.size = bfd_section_size (sectp);
11506 }
11507 else
11508 {
11509 /* No other kind of section is valid. */
11510 return 0;
11511 }
11512
11513 return 1;
11514 }
11515
11516 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11517 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11518 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11519 This is for DWP version 1 files. */
11520
11521 static struct dwo_unit *
11522 create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11523 struct dwp_file *dwp_file,
11524 uint32_t unit_index,
11525 const char *comp_dir,
11526 ULONGEST signature, int is_debug_types)
11527 {
11528 struct objfile *objfile = dwarf2_per_objfile->objfile;
11529 const struct dwp_hash_table *dwp_htab =
11530 is_debug_types ? dwp_file->tus : dwp_file->cus;
11531 bfd *dbfd = dwp_file->dbfd.get ();
11532 const char *kind = is_debug_types ? "TU" : "CU";
11533 struct dwo_file *dwo_file;
11534 struct dwo_unit *dwo_unit;
11535 struct virtual_v1_dwo_sections sections;
11536 void **dwo_file_slot;
11537 int i;
11538
11539 gdb_assert (dwp_file->version == 1);
11540
11541 if (dwarf_read_debug)
11542 {
11543 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
11544 kind,
11545 pulongest (unit_index), hex_string (signature),
11546 dwp_file->name);
11547 }
11548
11549 /* Fetch the sections of this DWO unit.
11550 Put a limit on the number of sections we look for so that bad data
11551 doesn't cause us to loop forever. */
11552
11553 #define MAX_NR_V1_DWO_SECTIONS \
11554 (1 /* .debug_info or .debug_types */ \
11555 + 1 /* .debug_abbrev */ \
11556 + 1 /* .debug_line */ \
11557 + 1 /* .debug_loc */ \
11558 + 1 /* .debug_str_offsets */ \
11559 + 1 /* .debug_macro or .debug_macinfo */ \
11560 + 1 /* trailing zero */)
11561
11562 memset (&sections, 0, sizeof (sections));
11563
11564 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
11565 {
11566 asection *sectp;
11567 uint32_t section_nr =
11568 read_4_bytes (dbfd,
11569 dwp_htab->section_pool.v1.indices
11570 + (unit_index + i) * sizeof (uint32_t));
11571
11572 if (section_nr == 0)
11573 break;
11574 if (section_nr >= dwp_file->num_sections)
11575 {
11576 error (_("Dwarf Error: bad DWP hash table, section number too large"
11577 " [in module %s]"),
11578 dwp_file->name);
11579 }
11580
11581 sectp = dwp_file->elf_sections[section_nr];
11582 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
11583 {
11584 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11585 " [in module %s]"),
11586 dwp_file->name);
11587 }
11588 }
11589
11590 if (i < 2
11591 || sections.info_or_types.empty ()
11592 || sections.abbrev.empty ())
11593 {
11594 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11595 " [in module %s]"),
11596 dwp_file->name);
11597 }
11598 if (i == MAX_NR_V1_DWO_SECTIONS)
11599 {
11600 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11601 " [in module %s]"),
11602 dwp_file->name);
11603 }
11604
11605 /* It's easier for the rest of the code if we fake a struct dwo_file and
11606 have dwo_unit "live" in that. At least for now.
11607
11608 The DWP file can be made up of a random collection of CUs and TUs.
11609 However, for each CU + set of TUs that came from the same original DWO
11610 file, we can combine them back into a virtual DWO file to save space
11611 (fewer struct dwo_file objects to allocate). Remember that for really
11612 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11613
11614 std::string virtual_dwo_name =
11615 string_printf ("virtual-dwo/%d-%d-%d-%d",
11616 sections.abbrev.get_id (),
11617 sections.line.get_id (),
11618 sections.loc.get_id (),
11619 sections.str_offsets.get_id ());
11620 /* Can we use an existing virtual DWO file? */
11621 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11622 virtual_dwo_name.c_str (),
11623 comp_dir);
11624 /* Create one if necessary. */
11625 if (*dwo_file_slot == NULL)
11626 {
11627 if (dwarf_read_debug)
11628 {
11629 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
11630 virtual_dwo_name.c_str ());
11631 }
11632 dwo_file = new struct dwo_file;
11633 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
11634 virtual_dwo_name);
11635 dwo_file->comp_dir = comp_dir;
11636 dwo_file->sections.abbrev = sections.abbrev;
11637 dwo_file->sections.line = sections.line;
11638 dwo_file->sections.loc = sections.loc;
11639 dwo_file->sections.macinfo = sections.macinfo;
11640 dwo_file->sections.macro = sections.macro;
11641 dwo_file->sections.str_offsets = sections.str_offsets;
11642 /* The "str" section is global to the entire DWP file. */
11643 dwo_file->sections.str = dwp_file->sections.str;
11644 /* The info or types section is assigned below to dwo_unit,
11645 there's no need to record it in dwo_file.
11646 Also, we can't simply record type sections in dwo_file because
11647 we record a pointer into the vector in dwo_unit. As we collect more
11648 types we'll grow the vector and eventually have to reallocate space
11649 for it, invalidating all copies of pointers into the previous
11650 contents. */
11651 *dwo_file_slot = dwo_file;
11652 }
11653 else
11654 {
11655 if (dwarf_read_debug)
11656 {
11657 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
11658 virtual_dwo_name.c_str ());
11659 }
11660 dwo_file = (struct dwo_file *) *dwo_file_slot;
11661 }
11662
11663 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11664 dwo_unit->dwo_file = dwo_file;
11665 dwo_unit->signature = signature;
11666 dwo_unit->section =
11667 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
11668 *dwo_unit->section = sections.info_or_types;
11669 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11670
11671 return dwo_unit;
11672 }
11673
11674 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
11675 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
11676 piece within that section used by a TU/CU, return a virtual section
11677 of just that piece. */
11678
11679 static struct dwarf2_section_info
11680 create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
11681 struct dwarf2_section_info *section,
11682 bfd_size_type offset, bfd_size_type size)
11683 {
11684 struct dwarf2_section_info result;
11685 asection *sectp;
11686
11687 gdb_assert (section != NULL);
11688 gdb_assert (!section->is_virtual);
11689
11690 memset (&result, 0, sizeof (result));
11691 result.s.containing_section = section;
11692 result.is_virtual = true;
11693
11694 if (size == 0)
11695 return result;
11696
11697 sectp = section->get_bfd_section ();
11698
11699 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11700 bounds of the real section. This is a pretty-rare event, so just
11701 flag an error (easier) instead of a warning and trying to cope. */
11702 if (sectp == NULL
11703 || offset + size > bfd_section_size (sectp))
11704 {
11705 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
11706 " in section %s [in module %s]"),
11707 sectp ? bfd_section_name (sectp) : "<unknown>",
11708 objfile_name (dwarf2_per_objfile->objfile));
11709 }
11710
11711 result.virtual_offset = offset;
11712 result.size = size;
11713 return result;
11714 }
11715
11716 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11717 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11718 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11719 This is for DWP version 2 files. */
11720
11721 static struct dwo_unit *
11722 create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11723 struct dwp_file *dwp_file,
11724 uint32_t unit_index,
11725 const char *comp_dir,
11726 ULONGEST signature, int is_debug_types)
11727 {
11728 struct objfile *objfile = dwarf2_per_objfile->objfile;
11729 const struct dwp_hash_table *dwp_htab =
11730 is_debug_types ? dwp_file->tus : dwp_file->cus;
11731 bfd *dbfd = dwp_file->dbfd.get ();
11732 const char *kind = is_debug_types ? "TU" : "CU";
11733 struct dwo_file *dwo_file;
11734 struct dwo_unit *dwo_unit;
11735 struct virtual_v2_dwo_sections sections;
11736 void **dwo_file_slot;
11737 int i;
11738
11739 gdb_assert (dwp_file->version == 2);
11740
11741 if (dwarf_read_debug)
11742 {
11743 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
11744 kind,
11745 pulongest (unit_index), hex_string (signature),
11746 dwp_file->name);
11747 }
11748
11749 /* Fetch the section offsets of this DWO unit. */
11750
11751 memset (&sections, 0, sizeof (sections));
11752
11753 for (i = 0; i < dwp_htab->nr_columns; ++i)
11754 {
11755 uint32_t offset = read_4_bytes (dbfd,
11756 dwp_htab->section_pool.v2.offsets
11757 + (((unit_index - 1) * dwp_htab->nr_columns
11758 + i)
11759 * sizeof (uint32_t)));
11760 uint32_t size = read_4_bytes (dbfd,
11761 dwp_htab->section_pool.v2.sizes
11762 + (((unit_index - 1) * dwp_htab->nr_columns
11763 + i)
11764 * sizeof (uint32_t)));
11765
11766 switch (dwp_htab->section_pool.v2.section_ids[i])
11767 {
11768 case DW_SECT_INFO:
11769 case DW_SECT_TYPES:
11770 sections.info_or_types_offset = offset;
11771 sections.info_or_types_size = size;
11772 break;
11773 case DW_SECT_ABBREV:
11774 sections.abbrev_offset = offset;
11775 sections.abbrev_size = size;
11776 break;
11777 case DW_SECT_LINE:
11778 sections.line_offset = offset;
11779 sections.line_size = size;
11780 break;
11781 case DW_SECT_LOC:
11782 sections.loc_offset = offset;
11783 sections.loc_size = size;
11784 break;
11785 case DW_SECT_STR_OFFSETS:
11786 sections.str_offsets_offset = offset;
11787 sections.str_offsets_size = size;
11788 break;
11789 case DW_SECT_MACINFO:
11790 sections.macinfo_offset = offset;
11791 sections.macinfo_size = size;
11792 break;
11793 case DW_SECT_MACRO:
11794 sections.macro_offset = offset;
11795 sections.macro_size = size;
11796 break;
11797 }
11798 }
11799
11800 /* It's easier for the rest of the code if we fake a struct dwo_file and
11801 have dwo_unit "live" in that. At least for now.
11802
11803 The DWP file can be made up of a random collection of CUs and TUs.
11804 However, for each CU + set of TUs that came from the same original DWO
11805 file, we can combine them back into a virtual DWO file to save space
11806 (fewer struct dwo_file objects to allocate). Remember that for really
11807 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11808
11809 std::string virtual_dwo_name =
11810 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
11811 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
11812 (long) (sections.line_size ? sections.line_offset : 0),
11813 (long) (sections.loc_size ? sections.loc_offset : 0),
11814 (long) (sections.str_offsets_size
11815 ? sections.str_offsets_offset : 0));
11816 /* Can we use an existing virtual DWO file? */
11817 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11818 virtual_dwo_name.c_str (),
11819 comp_dir);
11820 /* Create one if necessary. */
11821 if (*dwo_file_slot == NULL)
11822 {
11823 if (dwarf_read_debug)
11824 {
11825 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
11826 virtual_dwo_name.c_str ());
11827 }
11828 dwo_file = new struct dwo_file;
11829 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
11830 virtual_dwo_name);
11831 dwo_file->comp_dir = comp_dir;
11832 dwo_file->sections.abbrev =
11833 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
11834 sections.abbrev_offset, sections.abbrev_size);
11835 dwo_file->sections.line =
11836 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
11837 sections.line_offset, sections.line_size);
11838 dwo_file->sections.loc =
11839 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
11840 sections.loc_offset, sections.loc_size);
11841 dwo_file->sections.macinfo =
11842 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
11843 sections.macinfo_offset, sections.macinfo_size);
11844 dwo_file->sections.macro =
11845 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
11846 sections.macro_offset, sections.macro_size);
11847 dwo_file->sections.str_offsets =
11848 create_dwp_v2_section (dwarf2_per_objfile,
11849 &dwp_file->sections.str_offsets,
11850 sections.str_offsets_offset,
11851 sections.str_offsets_size);
11852 /* The "str" section is global to the entire DWP file. */
11853 dwo_file->sections.str = dwp_file->sections.str;
11854 /* The info or types section is assigned below to dwo_unit,
11855 there's no need to record it in dwo_file.
11856 Also, we can't simply record type sections in dwo_file because
11857 we record a pointer into the vector in dwo_unit. As we collect more
11858 types we'll grow the vector and eventually have to reallocate space
11859 for it, invalidating all copies of pointers into the previous
11860 contents. */
11861 *dwo_file_slot = dwo_file;
11862 }
11863 else
11864 {
11865 if (dwarf_read_debug)
11866 {
11867 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
11868 virtual_dwo_name.c_str ());
11869 }
11870 dwo_file = (struct dwo_file *) *dwo_file_slot;
11871 }
11872
11873 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11874 dwo_unit->dwo_file = dwo_file;
11875 dwo_unit->signature = signature;
11876 dwo_unit->section =
11877 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
11878 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
11879 is_debug_types
11880 ? &dwp_file->sections.types
11881 : &dwp_file->sections.info,
11882 sections.info_or_types_offset,
11883 sections.info_or_types_size);
11884 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11885
11886 return dwo_unit;
11887 }
11888
11889 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
11890 Returns NULL if the signature isn't found. */
11891
11892 static struct dwo_unit *
11893 lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
11894 struct dwp_file *dwp_file, const char *comp_dir,
11895 ULONGEST signature, int is_debug_types)
11896 {
11897 const struct dwp_hash_table *dwp_htab =
11898 is_debug_types ? dwp_file->tus : dwp_file->cus;
11899 bfd *dbfd = dwp_file->dbfd.get ();
11900 uint32_t mask = dwp_htab->nr_slots - 1;
11901 uint32_t hash = signature & mask;
11902 uint32_t hash2 = ((signature >> 32) & mask) | 1;
11903 unsigned int i;
11904 void **slot;
11905 struct dwo_unit find_dwo_cu;
11906
11907 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
11908 find_dwo_cu.signature = signature;
11909 slot = htab_find_slot (is_debug_types
11910 ? dwp_file->loaded_tus.get ()
11911 : dwp_file->loaded_cus.get (),
11912 &find_dwo_cu, INSERT);
11913
11914 if (*slot != NULL)
11915 return (struct dwo_unit *) *slot;
11916
11917 /* Use a for loop so that we don't loop forever on bad debug info. */
11918 for (i = 0; i < dwp_htab->nr_slots; ++i)
11919 {
11920 ULONGEST signature_in_table;
11921
11922 signature_in_table =
11923 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
11924 if (signature_in_table == signature)
11925 {
11926 uint32_t unit_index =
11927 read_4_bytes (dbfd,
11928 dwp_htab->unit_table + hash * sizeof (uint32_t));
11929
11930 if (dwp_file->version == 1)
11931 {
11932 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
11933 dwp_file, unit_index,
11934 comp_dir, signature,
11935 is_debug_types);
11936 }
11937 else
11938 {
11939 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
11940 dwp_file, unit_index,
11941 comp_dir, signature,
11942 is_debug_types);
11943 }
11944 return (struct dwo_unit *) *slot;
11945 }
11946 if (signature_in_table == 0)
11947 return NULL;
11948 hash = (hash + hash2) & mask;
11949 }
11950
11951 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
11952 " [in module %s]"),
11953 dwp_file->name);
11954 }
11955
11956 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
11957 Open the file specified by FILE_NAME and hand it off to BFD for
11958 preliminary analysis. Return a newly initialized bfd *, which
11959 includes a canonicalized copy of FILE_NAME.
11960 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
11961 SEARCH_CWD is true if the current directory is to be searched.
11962 It will be searched before debug-file-directory.
11963 If successful, the file is added to the bfd include table of the
11964 objfile's bfd (see gdb_bfd_record_inclusion).
11965 If unable to find/open the file, return NULL.
11966 NOTE: This function is derived from symfile_bfd_open. */
11967
11968 static gdb_bfd_ref_ptr
11969 try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
11970 const char *file_name, int is_dwp, int search_cwd)
11971 {
11972 int desc;
11973 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
11974 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
11975 to debug_file_directory. */
11976 const char *search_path;
11977 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
11978
11979 gdb::unique_xmalloc_ptr<char> search_path_holder;
11980 if (search_cwd)
11981 {
11982 if (*debug_file_directory != '\0')
11983 {
11984 search_path_holder.reset (concat (".", dirname_separator_string,
11985 debug_file_directory,
11986 (char *) NULL));
11987 search_path = search_path_holder.get ();
11988 }
11989 else
11990 search_path = ".";
11991 }
11992 else
11993 search_path = debug_file_directory;
11994
11995 openp_flags flags = OPF_RETURN_REALPATH;
11996 if (is_dwp)
11997 flags |= OPF_SEARCH_IN_PATH;
11998
11999 gdb::unique_xmalloc_ptr<char> absolute_name;
12000 desc = openp (search_path, flags, file_name,
12001 O_RDONLY | O_BINARY, &absolute_name);
12002 if (desc < 0)
12003 return NULL;
12004
12005 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12006 gnutarget, desc));
12007 if (sym_bfd == NULL)
12008 return NULL;
12009 bfd_set_cacheable (sym_bfd.get (), 1);
12010
12011 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12012 return NULL;
12013
12014 /* Success. Record the bfd as having been included by the objfile's bfd.
12015 This is important because things like demangled_names_hash lives in the
12016 objfile's per_bfd space and may have references to things like symbol
12017 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12018 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
12019
12020 return sym_bfd;
12021 }
12022
12023 /* Try to open DWO file FILE_NAME.
12024 COMP_DIR is the DW_AT_comp_dir attribute.
12025 The result is the bfd handle of the file.
12026 If there is a problem finding or opening the file, return NULL.
12027 Upon success, the canonicalized path of the file is stored in the bfd,
12028 same as symfile_bfd_open. */
12029
12030 static gdb_bfd_ref_ptr
12031 open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12032 const char *file_name, const char *comp_dir)
12033 {
12034 if (IS_ABSOLUTE_PATH (file_name))
12035 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12036 0 /*is_dwp*/, 0 /*search_cwd*/);
12037
12038 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12039
12040 if (comp_dir != NULL)
12041 {
12042 gdb::unique_xmalloc_ptr<char> path_to_try
12043 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
12044
12045 /* NOTE: If comp_dir is a relative path, this will also try the
12046 search path, which seems useful. */
12047 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
12048 path_to_try.get (),
12049 0 /*is_dwp*/,
12050 1 /*search_cwd*/));
12051 if (abfd != NULL)
12052 return abfd;
12053 }
12054
12055 /* That didn't work, try debug-file-directory, which, despite its name,
12056 is a list of paths. */
12057
12058 if (*debug_file_directory == '\0')
12059 return NULL;
12060
12061 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12062 0 /*is_dwp*/, 1 /*search_cwd*/);
12063 }
12064
12065 /* This function is mapped across the sections and remembers the offset and
12066 size of each of the DWO debugging sections we are interested in. */
12067
12068 static void
12069 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12070 {
12071 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
12072 const struct dwop_section_names *names = &dwop_section_names;
12073
12074 if (section_is_p (sectp->name, &names->abbrev_dwo))
12075 {
12076 dwo_sections->abbrev.s.section = sectp;
12077 dwo_sections->abbrev.size = bfd_section_size (sectp);
12078 }
12079 else if (section_is_p (sectp->name, &names->info_dwo))
12080 {
12081 dwo_sections->info.s.section = sectp;
12082 dwo_sections->info.size = bfd_section_size (sectp);
12083 }
12084 else if (section_is_p (sectp->name, &names->line_dwo))
12085 {
12086 dwo_sections->line.s.section = sectp;
12087 dwo_sections->line.size = bfd_section_size (sectp);
12088 }
12089 else if (section_is_p (sectp->name, &names->loc_dwo))
12090 {
12091 dwo_sections->loc.s.section = sectp;
12092 dwo_sections->loc.size = bfd_section_size (sectp);
12093 }
12094 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12095 {
12096 dwo_sections->macinfo.s.section = sectp;
12097 dwo_sections->macinfo.size = bfd_section_size (sectp);
12098 }
12099 else if (section_is_p (sectp->name, &names->macro_dwo))
12100 {
12101 dwo_sections->macro.s.section = sectp;
12102 dwo_sections->macro.size = bfd_section_size (sectp);
12103 }
12104 else if (section_is_p (sectp->name, &names->str_dwo))
12105 {
12106 dwo_sections->str.s.section = sectp;
12107 dwo_sections->str.size = bfd_section_size (sectp);
12108 }
12109 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12110 {
12111 dwo_sections->str_offsets.s.section = sectp;
12112 dwo_sections->str_offsets.size = bfd_section_size (sectp);
12113 }
12114 else if (section_is_p (sectp->name, &names->types_dwo))
12115 {
12116 struct dwarf2_section_info type_section;
12117
12118 memset (&type_section, 0, sizeof (type_section));
12119 type_section.s.section = sectp;
12120 type_section.size = bfd_section_size (sectp);
12121 dwo_sections->types.push_back (type_section);
12122 }
12123 }
12124
12125 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12126 by PER_CU. This is for the non-DWP case.
12127 The result is NULL if DWO_NAME can't be found. */
12128
12129 static struct dwo_file *
12130 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
12131 const char *dwo_name, const char *comp_dir)
12132 {
12133 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
12134
12135 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
12136 if (dbfd == NULL)
12137 {
12138 if (dwarf_read_debug)
12139 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12140 return NULL;
12141 }
12142
12143 dwo_file_up dwo_file (new struct dwo_file);
12144 dwo_file->dwo_name = dwo_name;
12145 dwo_file->comp_dir = comp_dir;
12146 dwo_file->dbfd = std::move (dbfd);
12147
12148 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
12149 &dwo_file->sections);
12150
12151 create_cus_hash_table (dwarf2_per_objfile, per_cu->cu, *dwo_file,
12152 dwo_file->sections.info, dwo_file->cus);
12153
12154 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
12155 dwo_file->sections.types, dwo_file->tus);
12156
12157 if (dwarf_read_debug)
12158 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12159
12160 return dwo_file.release ();
12161 }
12162
12163 /* This function is mapped across the sections and remembers the offset and
12164 size of each of the DWP debugging sections common to version 1 and 2 that
12165 we are interested in. */
12166
12167 static void
12168 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12169 void *dwp_file_ptr)
12170 {
12171 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12172 const struct dwop_section_names *names = &dwop_section_names;
12173 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12174
12175 /* Record the ELF section number for later lookup: this is what the
12176 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12177 gdb_assert (elf_section_nr < dwp_file->num_sections);
12178 dwp_file->elf_sections[elf_section_nr] = sectp;
12179
12180 /* Look for specific sections that we need. */
12181 if (section_is_p (sectp->name, &names->str_dwo))
12182 {
12183 dwp_file->sections.str.s.section = sectp;
12184 dwp_file->sections.str.size = bfd_section_size (sectp);
12185 }
12186 else if (section_is_p (sectp->name, &names->cu_index))
12187 {
12188 dwp_file->sections.cu_index.s.section = sectp;
12189 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
12190 }
12191 else if (section_is_p (sectp->name, &names->tu_index))
12192 {
12193 dwp_file->sections.tu_index.s.section = sectp;
12194 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
12195 }
12196 }
12197
12198 /* This function is mapped across the sections and remembers the offset and
12199 size of each of the DWP version 2 debugging sections that we are interested
12200 in. This is split into a separate function because we don't know if we
12201 have version 1 or 2 until we parse the cu_index/tu_index sections. */
12202
12203 static void
12204 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12205 {
12206 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12207 const struct dwop_section_names *names = &dwop_section_names;
12208 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12209
12210 /* Record the ELF section number for later lookup: this is what the
12211 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12212 gdb_assert (elf_section_nr < dwp_file->num_sections);
12213 dwp_file->elf_sections[elf_section_nr] = sectp;
12214
12215 /* Look for specific sections that we need. */
12216 if (section_is_p (sectp->name, &names->abbrev_dwo))
12217 {
12218 dwp_file->sections.abbrev.s.section = sectp;
12219 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12220 }
12221 else if (section_is_p (sectp->name, &names->info_dwo))
12222 {
12223 dwp_file->sections.info.s.section = sectp;
12224 dwp_file->sections.info.size = bfd_section_size (sectp);
12225 }
12226 else if (section_is_p (sectp->name, &names->line_dwo))
12227 {
12228 dwp_file->sections.line.s.section = sectp;
12229 dwp_file->sections.line.size = bfd_section_size (sectp);
12230 }
12231 else if (section_is_p (sectp->name, &names->loc_dwo))
12232 {
12233 dwp_file->sections.loc.s.section = sectp;
12234 dwp_file->sections.loc.size = bfd_section_size (sectp);
12235 }
12236 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12237 {
12238 dwp_file->sections.macinfo.s.section = sectp;
12239 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
12240 }
12241 else if (section_is_p (sectp->name, &names->macro_dwo))
12242 {
12243 dwp_file->sections.macro.s.section = sectp;
12244 dwp_file->sections.macro.size = bfd_section_size (sectp);
12245 }
12246 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12247 {
12248 dwp_file->sections.str_offsets.s.section = sectp;
12249 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12250 }
12251 else if (section_is_p (sectp->name, &names->types_dwo))
12252 {
12253 dwp_file->sections.types.s.section = sectp;
12254 dwp_file->sections.types.size = bfd_section_size (sectp);
12255 }
12256 }
12257
12258 /* Hash function for dwp_file loaded CUs/TUs. */
12259
12260 static hashval_t
12261 hash_dwp_loaded_cutus (const void *item)
12262 {
12263 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
12264
12265 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12266 return dwo_unit->signature;
12267 }
12268
12269 /* Equality function for dwp_file loaded CUs/TUs. */
12270
12271 static int
12272 eq_dwp_loaded_cutus (const void *a, const void *b)
12273 {
12274 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12275 const struct dwo_unit *dub = (const struct dwo_unit *) b;
12276
12277 return dua->signature == dub->signature;
12278 }
12279
12280 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
12281
12282 static htab_up
12283 allocate_dwp_loaded_cutus_table ()
12284 {
12285 return htab_up (htab_create_alloc (3,
12286 hash_dwp_loaded_cutus,
12287 eq_dwp_loaded_cutus,
12288 NULL, xcalloc, xfree));
12289 }
12290
12291 /* Try to open DWP file FILE_NAME.
12292 The result is the bfd handle of the file.
12293 If there is a problem finding or opening the file, return NULL.
12294 Upon success, the canonicalized path of the file is stored in the bfd,
12295 same as symfile_bfd_open. */
12296
12297 static gdb_bfd_ref_ptr
12298 open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12299 const char *file_name)
12300 {
12301 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
12302 1 /*is_dwp*/,
12303 1 /*search_cwd*/));
12304 if (abfd != NULL)
12305 return abfd;
12306
12307 /* Work around upstream bug 15652.
12308 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12309 [Whether that's a "bug" is debatable, but it is getting in our way.]
12310 We have no real idea where the dwp file is, because gdb's realpath-ing
12311 of the executable's path may have discarded the needed info.
12312 [IWBN if the dwp file name was recorded in the executable, akin to
12313 .gnu_debuglink, but that doesn't exist yet.]
12314 Strip the directory from FILE_NAME and search again. */
12315 if (*debug_file_directory != '\0')
12316 {
12317 /* Don't implicitly search the current directory here.
12318 If the user wants to search "." to handle this case,
12319 it must be added to debug-file-directory. */
12320 return try_open_dwop_file (dwarf2_per_objfile,
12321 lbasename (file_name), 1 /*is_dwp*/,
12322 0 /*search_cwd*/);
12323 }
12324
12325 return NULL;
12326 }
12327
12328 /* Initialize the use of the DWP file for the current objfile.
12329 By convention the name of the DWP file is ${objfile}.dwp.
12330 The result is NULL if it can't be found. */
12331
12332 static std::unique_ptr<struct dwp_file>
12333 open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
12334 {
12335 struct objfile *objfile = dwarf2_per_objfile->objfile;
12336
12337 /* Try to find first .dwp for the binary file before any symbolic links
12338 resolving. */
12339
12340 /* If the objfile is a debug file, find the name of the real binary
12341 file and get the name of dwp file from there. */
12342 std::string dwp_name;
12343 if (objfile->separate_debug_objfile_backlink != NULL)
12344 {
12345 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12346 const char *backlink_basename = lbasename (backlink->original_name);
12347
12348 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
12349 }
12350 else
12351 dwp_name = objfile->original_name;
12352
12353 dwp_name += ".dwp";
12354
12355 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
12356 if (dbfd == NULL
12357 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12358 {
12359 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
12360 dwp_name = objfile_name (objfile);
12361 dwp_name += ".dwp";
12362 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
12363 }
12364
12365 if (dbfd == NULL)
12366 {
12367 if (dwarf_read_debug)
12368 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
12369 return std::unique_ptr<dwp_file> ();
12370 }
12371
12372 const char *name = bfd_get_filename (dbfd.get ());
12373 std::unique_ptr<struct dwp_file> dwp_file
12374 (new struct dwp_file (name, std::move (dbfd)));
12375
12376 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
12377 dwp_file->elf_sections =
12378 OBSTACK_CALLOC (&objfile->objfile_obstack,
12379 dwp_file->num_sections, asection *);
12380
12381 bfd_map_over_sections (dwp_file->dbfd.get (),
12382 dwarf2_locate_common_dwp_sections,
12383 dwp_file.get ());
12384
12385 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12386 0);
12387
12388 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12389 1);
12390
12391 /* The DWP file version is stored in the hash table. Oh well. */
12392 if (dwp_file->cus && dwp_file->tus
12393 && dwp_file->cus->version != dwp_file->tus->version)
12394 {
12395 /* Technically speaking, we should try to limp along, but this is
12396 pretty bizarre. We use pulongest here because that's the established
12397 portability solution (e.g, we cannot use %u for uint32_t). */
12398 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12399 " TU version %s [in DWP file %s]"),
12400 pulongest (dwp_file->cus->version),
12401 pulongest (dwp_file->tus->version), dwp_name.c_str ());
12402 }
12403
12404 if (dwp_file->cus)
12405 dwp_file->version = dwp_file->cus->version;
12406 else if (dwp_file->tus)
12407 dwp_file->version = dwp_file->tus->version;
12408 else
12409 dwp_file->version = 2;
12410
12411 if (dwp_file->version == 2)
12412 bfd_map_over_sections (dwp_file->dbfd.get (),
12413 dwarf2_locate_v2_dwp_sections,
12414 dwp_file.get ());
12415
12416 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
12417 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
12418
12419 if (dwarf_read_debug)
12420 {
12421 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
12422 fprintf_unfiltered (gdb_stdlog,
12423 " %s CUs, %s TUs\n",
12424 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12425 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
12426 }
12427
12428 return dwp_file;
12429 }
12430
12431 /* Wrapper around open_and_init_dwp_file, only open it once. */
12432
12433 static struct dwp_file *
12434 get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
12435 {
12436 if (! dwarf2_per_objfile->dwp_checked)
12437 {
12438 dwarf2_per_objfile->dwp_file
12439 = open_and_init_dwp_file (dwarf2_per_objfile);
12440 dwarf2_per_objfile->dwp_checked = 1;
12441 }
12442 return dwarf2_per_objfile->dwp_file.get ();
12443 }
12444
12445 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12446 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12447 or in the DWP file for the objfile, referenced by THIS_UNIT.
12448 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
12449 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12450
12451 This is called, for example, when wanting to read a variable with a
12452 complex location. Therefore we don't want to do file i/o for every call.
12453 Therefore we don't want to look for a DWO file on every call.
12454 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12455 then we check if we've already seen DWO_NAME, and only THEN do we check
12456 for a DWO file.
12457
12458 The result is a pointer to the dwo_unit object or NULL if we didn't find it
12459 (dwo_id mismatch or couldn't find the DWO/DWP file). */
12460
12461 static struct dwo_unit *
12462 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
12463 const char *dwo_name, const char *comp_dir,
12464 ULONGEST signature, int is_debug_types)
12465 {
12466 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
12467 struct objfile *objfile = dwarf2_per_objfile->objfile;
12468 const char *kind = is_debug_types ? "TU" : "CU";
12469 void **dwo_file_slot;
12470 struct dwo_file *dwo_file;
12471 struct dwp_file *dwp_file;
12472
12473 /* First see if there's a DWP file.
12474 If we have a DWP file but didn't find the DWO inside it, don't
12475 look for the original DWO file. It makes gdb behave differently
12476 depending on whether one is debugging in the build tree. */
12477
12478 dwp_file = get_dwp_file (dwarf2_per_objfile);
12479 if (dwp_file != NULL)
12480 {
12481 const struct dwp_hash_table *dwp_htab =
12482 is_debug_types ? dwp_file->tus : dwp_file->cus;
12483
12484 if (dwp_htab != NULL)
12485 {
12486 struct dwo_unit *dwo_cutu =
12487 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
12488 signature, is_debug_types);
12489
12490 if (dwo_cutu != NULL)
12491 {
12492 if (dwarf_read_debug)
12493 {
12494 fprintf_unfiltered (gdb_stdlog,
12495 "Virtual DWO %s %s found: @%s\n",
12496 kind, hex_string (signature),
12497 host_address_to_string (dwo_cutu));
12498 }
12499 return dwo_cutu;
12500 }
12501 }
12502 }
12503 else
12504 {
12505 /* No DWP file, look for the DWO file. */
12506
12507 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12508 dwo_name, comp_dir);
12509 if (*dwo_file_slot == NULL)
12510 {
12511 /* Read in the file and build a table of the CUs/TUs it contains. */
12512 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
12513 }
12514 /* NOTE: This will be NULL if unable to open the file. */
12515 dwo_file = (struct dwo_file *) *dwo_file_slot;
12516
12517 if (dwo_file != NULL)
12518 {
12519 struct dwo_unit *dwo_cutu = NULL;
12520
12521 if (is_debug_types && dwo_file->tus)
12522 {
12523 struct dwo_unit find_dwo_cutu;
12524
12525 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12526 find_dwo_cutu.signature = signature;
12527 dwo_cutu
12528 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12529 &find_dwo_cutu);
12530 }
12531 else if (!is_debug_types && dwo_file->cus)
12532 {
12533 struct dwo_unit find_dwo_cutu;
12534
12535 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12536 find_dwo_cutu.signature = signature;
12537 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
12538 &find_dwo_cutu);
12539 }
12540
12541 if (dwo_cutu != NULL)
12542 {
12543 if (dwarf_read_debug)
12544 {
12545 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
12546 kind, dwo_name, hex_string (signature),
12547 host_address_to_string (dwo_cutu));
12548 }
12549 return dwo_cutu;
12550 }
12551 }
12552 }
12553
12554 /* We didn't find it. This could mean a dwo_id mismatch, or
12555 someone deleted the DWO/DWP file, or the search path isn't set up
12556 correctly to find the file. */
12557
12558 if (dwarf_read_debug)
12559 {
12560 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
12561 kind, dwo_name, hex_string (signature));
12562 }
12563
12564 /* This is a warning and not a complaint because it can be caused by
12565 pilot error (e.g., user accidentally deleting the DWO). */
12566 {
12567 /* Print the name of the DWP file if we looked there, helps the user
12568 better diagnose the problem. */
12569 std::string dwp_text;
12570
12571 if (dwp_file != NULL)
12572 dwp_text = string_printf (" [in DWP file %s]",
12573 lbasename (dwp_file->name));
12574
12575 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
12576 " [in module %s]"),
12577 kind, dwo_name, hex_string (signature),
12578 dwp_text.c_str (),
12579 this_unit->is_debug_types ? "TU" : "CU",
12580 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
12581 }
12582 return NULL;
12583 }
12584
12585 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12586 See lookup_dwo_cutu_unit for details. */
12587
12588 static struct dwo_unit *
12589 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
12590 const char *dwo_name, const char *comp_dir,
12591 ULONGEST signature)
12592 {
12593 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
12594 }
12595
12596 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12597 See lookup_dwo_cutu_unit for details. */
12598
12599 static struct dwo_unit *
12600 lookup_dwo_type_unit (struct signatured_type *this_tu,
12601 const char *dwo_name, const char *comp_dir)
12602 {
12603 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
12604 }
12605
12606 /* Traversal function for queue_and_load_all_dwo_tus. */
12607
12608 static int
12609 queue_and_load_dwo_tu (void **slot, void *info)
12610 {
12611 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
12612 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
12613 ULONGEST signature = dwo_unit->signature;
12614 struct signatured_type *sig_type =
12615 lookup_dwo_signatured_type (per_cu->cu, signature);
12616
12617 if (sig_type != NULL)
12618 {
12619 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
12620
12621 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12622 a real dependency of PER_CU on SIG_TYPE. That is detected later
12623 while processing PER_CU. */
12624 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
12625 load_full_type_unit (sig_cu);
12626 per_cu->imported_symtabs_push (sig_cu);
12627 }
12628
12629 return 1;
12630 }
12631
12632 /* Queue all TUs contained in the DWO of PER_CU to be read in.
12633 The DWO may have the only definition of the type, though it may not be
12634 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12635 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12636
12637 static void
12638 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
12639 {
12640 struct dwo_unit *dwo_unit;
12641 struct dwo_file *dwo_file;
12642
12643 gdb_assert (!per_cu->is_debug_types);
12644 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
12645 gdb_assert (per_cu->cu != NULL);
12646
12647 dwo_unit = per_cu->cu->dwo_unit;
12648 gdb_assert (dwo_unit != NULL);
12649
12650 dwo_file = dwo_unit->dwo_file;
12651 if (dwo_file->tus != NULL)
12652 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu,
12653 per_cu);
12654 }
12655
12656 /* Read in various DIEs. */
12657
12658 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
12659 Inherit only the children of the DW_AT_abstract_origin DIE not being
12660 already referenced by DW_AT_abstract_origin from the children of the
12661 current DIE. */
12662
12663 static void
12664 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
12665 {
12666 struct die_info *child_die;
12667 sect_offset *offsetp;
12668 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
12669 struct die_info *origin_die;
12670 /* Iterator of the ORIGIN_DIE children. */
12671 struct die_info *origin_child_die;
12672 struct attribute *attr;
12673 struct dwarf2_cu *origin_cu;
12674 struct pending **origin_previous_list_in_scope;
12675
12676 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12677 if (!attr)
12678 return;
12679
12680 /* Note that following die references may follow to a die in a
12681 different cu. */
12682
12683 origin_cu = cu;
12684 origin_die = follow_die_ref (die, attr, &origin_cu);
12685
12686 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
12687 symbols in. */
12688 origin_previous_list_in_scope = origin_cu->list_in_scope;
12689 origin_cu->list_in_scope = cu->list_in_scope;
12690
12691 if (die->tag != origin_die->tag
12692 && !(die->tag == DW_TAG_inlined_subroutine
12693 && origin_die->tag == DW_TAG_subprogram))
12694 complaint (_("DIE %s and its abstract origin %s have different tags"),
12695 sect_offset_str (die->sect_off),
12696 sect_offset_str (origin_die->sect_off));
12697
12698 std::vector<sect_offset> offsets;
12699
12700 for (child_die = die->child;
12701 child_die && child_die->tag;
12702 child_die = sibling_die (child_die))
12703 {
12704 struct die_info *child_origin_die;
12705 struct dwarf2_cu *child_origin_cu;
12706
12707 /* We are trying to process concrete instance entries:
12708 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
12709 it's not relevant to our analysis here. i.e. detecting DIEs that are
12710 present in the abstract instance but not referenced in the concrete
12711 one. */
12712 if (child_die->tag == DW_TAG_call_site
12713 || child_die->tag == DW_TAG_GNU_call_site)
12714 continue;
12715
12716 /* For each CHILD_DIE, find the corresponding child of
12717 ORIGIN_DIE. If there is more than one layer of
12718 DW_AT_abstract_origin, follow them all; there shouldn't be,
12719 but GCC versions at least through 4.4 generate this (GCC PR
12720 40573). */
12721 child_origin_die = child_die;
12722 child_origin_cu = cu;
12723 while (1)
12724 {
12725 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
12726 child_origin_cu);
12727 if (attr == NULL)
12728 break;
12729 child_origin_die = follow_die_ref (child_origin_die, attr,
12730 &child_origin_cu);
12731 }
12732
12733 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
12734 counterpart may exist. */
12735 if (child_origin_die != child_die)
12736 {
12737 if (child_die->tag != child_origin_die->tag
12738 && !(child_die->tag == DW_TAG_inlined_subroutine
12739 && child_origin_die->tag == DW_TAG_subprogram))
12740 complaint (_("Child DIE %s and its abstract origin %s have "
12741 "different tags"),
12742 sect_offset_str (child_die->sect_off),
12743 sect_offset_str (child_origin_die->sect_off));
12744 if (child_origin_die->parent != origin_die)
12745 complaint (_("Child DIE %s and its abstract origin %s have "
12746 "different parents"),
12747 sect_offset_str (child_die->sect_off),
12748 sect_offset_str (child_origin_die->sect_off));
12749 else
12750 offsets.push_back (child_origin_die->sect_off);
12751 }
12752 }
12753 std::sort (offsets.begin (), offsets.end ());
12754 sect_offset *offsets_end = offsets.data () + offsets.size ();
12755 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
12756 if (offsetp[-1] == *offsetp)
12757 complaint (_("Multiple children of DIE %s refer "
12758 "to DIE %s as their abstract origin"),
12759 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
12760
12761 offsetp = offsets.data ();
12762 origin_child_die = origin_die->child;
12763 while (origin_child_die && origin_child_die->tag)
12764 {
12765 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
12766 while (offsetp < offsets_end
12767 && *offsetp < origin_child_die->sect_off)
12768 offsetp++;
12769 if (offsetp >= offsets_end
12770 || *offsetp > origin_child_die->sect_off)
12771 {
12772 /* Found that ORIGIN_CHILD_DIE is really not referenced.
12773 Check whether we're already processing ORIGIN_CHILD_DIE.
12774 This can happen with mutually referenced abstract_origins.
12775 PR 16581. */
12776 if (!origin_child_die->in_process)
12777 process_die (origin_child_die, origin_cu);
12778 }
12779 origin_child_die = sibling_die (origin_child_die);
12780 }
12781 origin_cu->list_in_scope = origin_previous_list_in_scope;
12782
12783 if (cu != origin_cu)
12784 compute_delayed_physnames (origin_cu);
12785 }
12786
12787 static void
12788 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
12789 {
12790 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
12791 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12792 struct context_stack *newobj;
12793 CORE_ADDR lowpc;
12794 CORE_ADDR highpc;
12795 struct die_info *child_die;
12796 struct attribute *attr, *call_line, *call_file;
12797 const char *name;
12798 CORE_ADDR baseaddr;
12799 struct block *block;
12800 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
12801 std::vector<struct symbol *> template_args;
12802 struct template_symbol *templ_func = NULL;
12803
12804 if (inlined_func)
12805 {
12806 /* If we do not have call site information, we can't show the
12807 caller of this inlined function. That's too confusing, so
12808 only use the scope for local variables. */
12809 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
12810 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
12811 if (call_line == NULL || call_file == NULL)
12812 {
12813 read_lexical_block_scope (die, cu);
12814 return;
12815 }
12816 }
12817
12818 baseaddr = objfile->text_section_offset ();
12819
12820 name = dwarf2_name (die, cu);
12821
12822 /* Ignore functions with missing or empty names. These are actually
12823 illegal according to the DWARF standard. */
12824 if (name == NULL)
12825 {
12826 complaint (_("missing name for subprogram DIE at %s"),
12827 sect_offset_str (die->sect_off));
12828 return;
12829 }
12830
12831 /* Ignore functions with missing or invalid low and high pc attributes. */
12832 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
12833 <= PC_BOUNDS_INVALID)
12834 {
12835 attr = dwarf2_attr (die, DW_AT_external, cu);
12836 if (!attr || !DW_UNSND (attr))
12837 complaint (_("cannot get low and high bounds "
12838 "for subprogram DIE at %s"),
12839 sect_offset_str (die->sect_off));
12840 return;
12841 }
12842
12843 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12844 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
12845
12846 /* If we have any template arguments, then we must allocate a
12847 different sort of symbol. */
12848 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
12849 {
12850 if (child_die->tag == DW_TAG_template_type_param
12851 || child_die->tag == DW_TAG_template_value_param)
12852 {
12853 templ_func = allocate_template_symbol (objfile);
12854 templ_func->subclass = SYMBOL_TEMPLATE;
12855 break;
12856 }
12857 }
12858
12859 newobj = cu->get_builder ()->push_context (0, lowpc);
12860 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
12861 (struct symbol *) templ_func);
12862
12863 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
12864 set_objfile_main_name (objfile, newobj->name->linkage_name (),
12865 cu->language);
12866
12867 /* If there is a location expression for DW_AT_frame_base, record
12868 it. */
12869 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
12870 if (attr != nullptr)
12871 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
12872
12873 /* If there is a location for the static link, record it. */
12874 newobj->static_link = NULL;
12875 attr = dwarf2_attr (die, DW_AT_static_link, cu);
12876 if (attr != nullptr)
12877 {
12878 newobj->static_link
12879 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
12880 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
12881 cu->per_cu->addr_type ());
12882 }
12883
12884 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
12885
12886 if (die->child != NULL)
12887 {
12888 child_die = die->child;
12889 while (child_die && child_die->tag)
12890 {
12891 if (child_die->tag == DW_TAG_template_type_param
12892 || child_die->tag == DW_TAG_template_value_param)
12893 {
12894 struct symbol *arg = new_symbol (child_die, NULL, cu);
12895
12896 if (arg != NULL)
12897 template_args.push_back (arg);
12898 }
12899 else
12900 process_die (child_die, cu);
12901 child_die = sibling_die (child_die);
12902 }
12903 }
12904
12905 inherit_abstract_dies (die, cu);
12906
12907 /* If we have a DW_AT_specification, we might need to import using
12908 directives from the context of the specification DIE. See the
12909 comment in determine_prefix. */
12910 if (cu->language == language_cplus
12911 && dwarf2_attr (die, DW_AT_specification, cu))
12912 {
12913 struct dwarf2_cu *spec_cu = cu;
12914 struct die_info *spec_die = die_specification (die, &spec_cu);
12915
12916 while (spec_die)
12917 {
12918 child_die = spec_die->child;
12919 while (child_die && child_die->tag)
12920 {
12921 if (child_die->tag == DW_TAG_imported_module)
12922 process_die (child_die, spec_cu);
12923 child_die = sibling_die (child_die);
12924 }
12925
12926 /* In some cases, GCC generates specification DIEs that
12927 themselves contain DW_AT_specification attributes. */
12928 spec_die = die_specification (spec_die, &spec_cu);
12929 }
12930 }
12931
12932 struct context_stack cstk = cu->get_builder ()->pop_context ();
12933 /* Make a block for the local symbols within. */
12934 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
12935 cstk.static_link, lowpc, highpc);
12936
12937 /* For C++, set the block's scope. */
12938 if ((cu->language == language_cplus
12939 || cu->language == language_fortran
12940 || cu->language == language_d
12941 || cu->language == language_rust)
12942 && cu->processing_has_namespace_info)
12943 block_set_scope (block, determine_prefix (die, cu),
12944 &objfile->objfile_obstack);
12945
12946 /* If we have address ranges, record them. */
12947 dwarf2_record_block_ranges (die, block, baseaddr, cu);
12948
12949 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
12950
12951 /* Attach template arguments to function. */
12952 if (!template_args.empty ())
12953 {
12954 gdb_assert (templ_func != NULL);
12955
12956 templ_func->n_template_arguments = template_args.size ();
12957 templ_func->template_arguments
12958 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
12959 templ_func->n_template_arguments);
12960 memcpy (templ_func->template_arguments,
12961 template_args.data (),
12962 (templ_func->n_template_arguments * sizeof (struct symbol *)));
12963
12964 /* Make sure that the symtab is set on the new symbols. Even
12965 though they don't appear in this symtab directly, other parts
12966 of gdb assume that symbols do, and this is reasonably
12967 true. */
12968 for (symbol *sym : template_args)
12969 symbol_set_symtab (sym, symbol_symtab (templ_func));
12970 }
12971
12972 /* In C++, we can have functions nested inside functions (e.g., when
12973 a function declares a class that has methods). This means that
12974 when we finish processing a function scope, we may need to go
12975 back to building a containing block's symbol lists. */
12976 *cu->get_builder ()->get_local_symbols () = cstk.locals;
12977 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
12978
12979 /* If we've finished processing a top-level function, subsequent
12980 symbols go in the file symbol list. */
12981 if (cu->get_builder ()->outermost_context_p ())
12982 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
12983 }
12984
12985 /* Process all the DIES contained within a lexical block scope. Start
12986 a new scope, process the dies, and then close the scope. */
12987
12988 static void
12989 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
12990 {
12991 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
12992 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12993 CORE_ADDR lowpc, highpc;
12994 struct die_info *child_die;
12995 CORE_ADDR baseaddr;
12996
12997 baseaddr = objfile->text_section_offset ();
12998
12999 /* Ignore blocks with missing or invalid low and high pc attributes. */
13000 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13001 as multiple lexical blocks? Handling children in a sane way would
13002 be nasty. Might be easier to properly extend generic blocks to
13003 describe ranges. */
13004 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13005 {
13006 case PC_BOUNDS_NOT_PRESENT:
13007 /* DW_TAG_lexical_block has no attributes, process its children as if
13008 there was no wrapping by that DW_TAG_lexical_block.
13009 GCC does no longer produces such DWARF since GCC r224161. */
13010 for (child_die = die->child;
13011 child_die != NULL && child_die->tag;
13012 child_die = sibling_die (child_die))
13013 process_die (child_die, cu);
13014 return;
13015 case PC_BOUNDS_INVALID:
13016 return;
13017 }
13018 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13019 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13020
13021 cu->get_builder ()->push_context (0, lowpc);
13022 if (die->child != NULL)
13023 {
13024 child_die = die->child;
13025 while (child_die && child_die->tag)
13026 {
13027 process_die (child_die, cu);
13028 child_die = sibling_die (child_die);
13029 }
13030 }
13031 inherit_abstract_dies (die, cu);
13032 struct context_stack cstk = cu->get_builder ()->pop_context ();
13033
13034 if (*cu->get_builder ()->get_local_symbols () != NULL
13035 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13036 {
13037 struct block *block
13038 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13039 cstk.start_addr, highpc);
13040
13041 /* Note that recording ranges after traversing children, as we
13042 do here, means that recording a parent's ranges entails
13043 walking across all its children's ranges as they appear in
13044 the address map, which is quadratic behavior.
13045
13046 It would be nicer to record the parent's ranges before
13047 traversing its children, simply overriding whatever you find
13048 there. But since we don't even decide whether to create a
13049 block until after we've traversed its children, that's hard
13050 to do. */
13051 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13052 }
13053 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13054 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13055 }
13056
13057 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13058
13059 static void
13060 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13061 {
13062 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13063 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13064 CORE_ADDR pc, baseaddr;
13065 struct attribute *attr;
13066 struct call_site *call_site, call_site_local;
13067 void **slot;
13068 int nparams;
13069 struct die_info *child_die;
13070
13071 baseaddr = objfile->text_section_offset ();
13072
13073 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13074 if (attr == NULL)
13075 {
13076 /* This was a pre-DWARF-5 GNU extension alias
13077 for DW_AT_call_return_pc. */
13078 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13079 }
13080 if (!attr)
13081 {
13082 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13083 "DIE %s [in module %s]"),
13084 sect_offset_str (die->sect_off), objfile_name (objfile));
13085 return;
13086 }
13087 pc = attr->value_as_address () + baseaddr;
13088 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13089
13090 if (cu->call_site_htab == NULL)
13091 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13092 NULL, &objfile->objfile_obstack,
13093 hashtab_obstack_allocate, NULL);
13094 call_site_local.pc = pc;
13095 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13096 if (*slot != NULL)
13097 {
13098 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13099 "DIE %s [in module %s]"),
13100 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13101 objfile_name (objfile));
13102 return;
13103 }
13104
13105 /* Count parameters at the caller. */
13106
13107 nparams = 0;
13108 for (child_die = die->child; child_die && child_die->tag;
13109 child_die = sibling_die (child_die))
13110 {
13111 if (child_die->tag != DW_TAG_call_site_parameter
13112 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13113 {
13114 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
13115 "DW_TAG_call_site child DIE %s [in module %s]"),
13116 child_die->tag, sect_offset_str (child_die->sect_off),
13117 objfile_name (objfile));
13118 continue;
13119 }
13120
13121 nparams++;
13122 }
13123
13124 call_site
13125 = ((struct call_site *)
13126 obstack_alloc (&objfile->objfile_obstack,
13127 sizeof (*call_site)
13128 + (sizeof (*call_site->parameter) * (nparams - 1))));
13129 *slot = call_site;
13130 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13131 call_site->pc = pc;
13132
13133 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13134 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
13135 {
13136 struct die_info *func_die;
13137
13138 /* Skip also over DW_TAG_inlined_subroutine. */
13139 for (func_die = die->parent;
13140 func_die && func_die->tag != DW_TAG_subprogram
13141 && func_die->tag != DW_TAG_subroutine_type;
13142 func_die = func_die->parent);
13143
13144 /* DW_AT_call_all_calls is a superset
13145 of DW_AT_call_all_tail_calls. */
13146 if (func_die
13147 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13148 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
13149 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
13150 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13151 {
13152 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13153 not complete. But keep CALL_SITE for look ups via call_site_htab,
13154 both the initial caller containing the real return address PC and
13155 the final callee containing the current PC of a chain of tail
13156 calls do not need to have the tail call list complete. But any
13157 function candidate for a virtual tail call frame searched via
13158 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13159 determined unambiguously. */
13160 }
13161 else
13162 {
13163 struct type *func_type = NULL;
13164
13165 if (func_die)
13166 func_type = get_die_type (func_die, cu);
13167 if (func_type != NULL)
13168 {
13169 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
13170
13171 /* Enlist this call site to the function. */
13172 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13173 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13174 }
13175 else
13176 complaint (_("Cannot find function owning DW_TAG_call_site "
13177 "DIE %s [in module %s]"),
13178 sect_offset_str (die->sect_off), objfile_name (objfile));
13179 }
13180 }
13181
13182 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13183 if (attr == NULL)
13184 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13185 if (attr == NULL)
13186 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
13187 if (attr == NULL)
13188 {
13189 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13190 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13191 }
13192 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
13193 if (!attr || (attr->form_is_block () && DW_BLOCK (attr)->size == 0))
13194 /* Keep NULL DWARF_BLOCK. */;
13195 else if (attr->form_is_block ())
13196 {
13197 struct dwarf2_locexpr_baton *dlbaton;
13198
13199 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
13200 dlbaton->data = DW_BLOCK (attr)->data;
13201 dlbaton->size = DW_BLOCK (attr)->size;
13202 dlbaton->per_cu = cu->per_cu;
13203
13204 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13205 }
13206 else if (attr->form_is_ref ())
13207 {
13208 struct dwarf2_cu *target_cu = cu;
13209 struct die_info *target_die;
13210
13211 target_die = follow_die_ref (die, attr, &target_cu);
13212 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
13213 if (die_is_declaration (target_die, target_cu))
13214 {
13215 const char *target_physname;
13216
13217 /* Prefer the mangled name; otherwise compute the demangled one. */
13218 target_physname = dw2_linkage_name (target_die, target_cu);
13219 if (target_physname == NULL)
13220 target_physname = dwarf2_physname (NULL, target_die, target_cu);
13221 if (target_physname == NULL)
13222 complaint (_("DW_AT_call_target target DIE has invalid "
13223 "physname, for referencing DIE %s [in module %s]"),
13224 sect_offset_str (die->sect_off), objfile_name (objfile));
13225 else
13226 SET_FIELD_PHYSNAME (call_site->target, target_physname);
13227 }
13228 else
13229 {
13230 CORE_ADDR lowpc;
13231
13232 /* DW_AT_entry_pc should be preferred. */
13233 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
13234 <= PC_BOUNDS_INVALID)
13235 complaint (_("DW_AT_call_target target DIE has invalid "
13236 "low pc, for referencing DIE %s [in module %s]"),
13237 sect_offset_str (die->sect_off), objfile_name (objfile));
13238 else
13239 {
13240 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13241 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13242 }
13243 }
13244 }
13245 else
13246 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
13247 "block nor reference, for DIE %s [in module %s]"),
13248 sect_offset_str (die->sect_off), objfile_name (objfile));
13249
13250 call_site->per_cu = cu->per_cu;
13251
13252 for (child_die = die->child;
13253 child_die && child_die->tag;
13254 child_die = sibling_die (child_die))
13255 {
13256 struct call_site_parameter *parameter;
13257 struct attribute *loc, *origin;
13258
13259 if (child_die->tag != DW_TAG_call_site_parameter
13260 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13261 {
13262 /* Already printed the complaint above. */
13263 continue;
13264 }
13265
13266 gdb_assert (call_site->parameter_count < nparams);
13267 parameter = &call_site->parameter[call_site->parameter_count];
13268
13269 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13270 specifies DW_TAG_formal_parameter. Value of the data assumed for the
13271 register is contained in DW_AT_call_value. */
13272
13273 loc = dwarf2_attr (child_die, DW_AT_location, cu);
13274 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13275 if (origin == NULL)
13276 {
13277 /* This was a pre-DWARF-5 GNU extension alias
13278 for DW_AT_call_parameter. */
13279 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13280 }
13281 if (loc == NULL && origin != NULL && origin->form_is_ref ())
13282 {
13283 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
13284
13285 sect_offset sect_off
13286 = (sect_offset) dwarf2_get_ref_die_offset (origin);
13287 if (!cu->header.offset_in_cu_p (sect_off))
13288 {
13289 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13290 binding can be done only inside one CU. Such referenced DIE
13291 therefore cannot be even moved to DW_TAG_partial_unit. */
13292 complaint (_("DW_AT_call_parameter offset is not in CU for "
13293 "DW_TAG_call_site child DIE %s [in module %s]"),
13294 sect_offset_str (child_die->sect_off),
13295 objfile_name (objfile));
13296 continue;
13297 }
13298 parameter->u.param_cu_off
13299 = (cu_offset) (sect_off - cu->header.sect_off);
13300 }
13301 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
13302 {
13303 complaint (_("No DW_FORM_block* DW_AT_location for "
13304 "DW_TAG_call_site child DIE %s [in module %s]"),
13305 sect_offset_str (child_die->sect_off), objfile_name (objfile));
13306 continue;
13307 }
13308 else
13309 {
13310 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13311 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
13312 if (parameter->u.dwarf_reg != -1)
13313 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13314 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
13315 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
13316 &parameter->u.fb_offset))
13317 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13318 else
13319 {
13320 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
13321 "for DW_FORM_block* DW_AT_location is supported for "
13322 "DW_TAG_call_site child DIE %s "
13323 "[in module %s]"),
13324 sect_offset_str (child_die->sect_off),
13325 objfile_name (objfile));
13326 continue;
13327 }
13328 }
13329
13330 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13331 if (attr == NULL)
13332 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
13333 if (attr == NULL || !attr->form_is_block ())
13334 {
13335 complaint (_("No DW_FORM_block* DW_AT_call_value for "
13336 "DW_TAG_call_site child DIE %s [in module %s]"),
13337 sect_offset_str (child_die->sect_off),
13338 objfile_name (objfile));
13339 continue;
13340 }
13341 parameter->value = DW_BLOCK (attr)->data;
13342 parameter->value_size = DW_BLOCK (attr)->size;
13343
13344 /* Parameters are not pre-cleared by memset above. */
13345 parameter->data_value = NULL;
13346 parameter->data_value_size = 0;
13347 call_site->parameter_count++;
13348
13349 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13350 if (attr == NULL)
13351 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
13352 if (attr != nullptr)
13353 {
13354 if (!attr->form_is_block ())
13355 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
13356 "DW_TAG_call_site child DIE %s [in module %s]"),
13357 sect_offset_str (child_die->sect_off),
13358 objfile_name (objfile));
13359 else
13360 {
13361 parameter->data_value = DW_BLOCK (attr)->data;
13362 parameter->data_value_size = DW_BLOCK (attr)->size;
13363 }
13364 }
13365 }
13366 }
13367
13368 /* Helper function for read_variable. If DIE represents a virtual
13369 table, then return the type of the concrete object that is
13370 associated with the virtual table. Otherwise, return NULL. */
13371
13372 static struct type *
13373 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13374 {
13375 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13376 if (attr == NULL)
13377 return NULL;
13378
13379 /* Find the type DIE. */
13380 struct die_info *type_die = NULL;
13381 struct dwarf2_cu *type_cu = cu;
13382
13383 if (attr->form_is_ref ())
13384 type_die = follow_die_ref (die, attr, &type_cu);
13385 if (type_die == NULL)
13386 return NULL;
13387
13388 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13389 return NULL;
13390 return die_containing_type (type_die, type_cu);
13391 }
13392
13393 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13394
13395 static void
13396 read_variable (struct die_info *die, struct dwarf2_cu *cu)
13397 {
13398 struct rust_vtable_symbol *storage = NULL;
13399
13400 if (cu->language == language_rust)
13401 {
13402 struct type *containing_type = rust_containing_type (die, cu);
13403
13404 if (containing_type != NULL)
13405 {
13406 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13407
13408 storage = new (&objfile->objfile_obstack) rust_vtable_symbol ();
13409 initialize_objfile_symbol (storage);
13410 storage->concrete_type = containing_type;
13411 storage->subclass = SYMBOL_RUST_VTABLE;
13412 }
13413 }
13414
13415 struct symbol *res = new_symbol (die, NULL, cu, storage);
13416 struct attribute *abstract_origin
13417 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13418 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13419 if (res == NULL && loc && abstract_origin)
13420 {
13421 /* We have a variable without a name, but with a location and an abstract
13422 origin. This may be a concrete instance of an abstract variable
13423 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13424 later. */
13425 struct dwarf2_cu *origin_cu = cu;
13426 struct die_info *origin_die
13427 = follow_die_ref (die, abstract_origin, &origin_cu);
13428 dwarf2_per_objfile *dpo = cu->per_cu->dwarf2_per_objfile;
13429 dpo->abstract_to_concrete[origin_die->sect_off].push_back (die->sect_off);
13430 }
13431 }
13432
13433 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13434 reading .debug_rnglists.
13435 Callback's type should be:
13436 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13437 Return true if the attributes are present and valid, otherwise,
13438 return false. */
13439
13440 template <typename Callback>
13441 static bool
13442 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13443 Callback &&callback)
13444 {
13445 struct dwarf2_per_objfile *dwarf2_per_objfile
13446 = cu->per_cu->dwarf2_per_objfile;
13447 struct objfile *objfile = dwarf2_per_objfile->objfile;
13448 bfd *obfd = objfile->obfd;
13449 /* Base address selection entry. */
13450 CORE_ADDR base;
13451 int found_base;
13452 const gdb_byte *buffer;
13453 CORE_ADDR baseaddr;
13454 bool overflow = false;
13455
13456 found_base = cu->base_known;
13457 base = cu->base_address;
13458
13459 dwarf2_per_objfile->rnglists.read (objfile);
13460 if (offset >= dwarf2_per_objfile->rnglists.size)
13461 {
13462 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13463 offset);
13464 return false;
13465 }
13466 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
13467
13468 baseaddr = objfile->text_section_offset ();
13469
13470 while (1)
13471 {
13472 /* Initialize it due to a false compiler warning. */
13473 CORE_ADDR range_beginning = 0, range_end = 0;
13474 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
13475 + dwarf2_per_objfile->rnglists.size);
13476 unsigned int bytes_read;
13477
13478 if (buffer == buf_end)
13479 {
13480 overflow = true;
13481 break;
13482 }
13483 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13484 switch (rlet)
13485 {
13486 case DW_RLE_end_of_list:
13487 break;
13488 case DW_RLE_base_address:
13489 if (buffer + cu->header.addr_size > buf_end)
13490 {
13491 overflow = true;
13492 break;
13493 }
13494 base = cu->header.read_address (obfd, buffer, &bytes_read);
13495 found_base = 1;
13496 buffer += bytes_read;
13497 break;
13498 case DW_RLE_start_length:
13499 if (buffer + cu->header.addr_size > buf_end)
13500 {
13501 overflow = true;
13502 break;
13503 }
13504 range_beginning = cu->header.read_address (obfd, buffer,
13505 &bytes_read);
13506 buffer += bytes_read;
13507 range_end = (range_beginning
13508 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13509 buffer += bytes_read;
13510 if (buffer > buf_end)
13511 {
13512 overflow = true;
13513 break;
13514 }
13515 break;
13516 case DW_RLE_offset_pair:
13517 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13518 buffer += bytes_read;
13519 if (buffer > buf_end)
13520 {
13521 overflow = true;
13522 break;
13523 }
13524 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13525 buffer += bytes_read;
13526 if (buffer > buf_end)
13527 {
13528 overflow = true;
13529 break;
13530 }
13531 break;
13532 case DW_RLE_start_end:
13533 if (buffer + 2 * cu->header.addr_size > buf_end)
13534 {
13535 overflow = true;
13536 break;
13537 }
13538 range_beginning = cu->header.read_address (obfd, buffer,
13539 &bytes_read);
13540 buffer += bytes_read;
13541 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
13542 buffer += bytes_read;
13543 break;
13544 default:
13545 complaint (_("Invalid .debug_rnglists data (no base address)"));
13546 return false;
13547 }
13548 if (rlet == DW_RLE_end_of_list || overflow)
13549 break;
13550 if (rlet == DW_RLE_base_address)
13551 continue;
13552
13553 if (!found_base)
13554 {
13555 /* We have no valid base address for the ranges
13556 data. */
13557 complaint (_("Invalid .debug_rnglists data (no base address)"));
13558 return false;
13559 }
13560
13561 if (range_beginning > range_end)
13562 {
13563 /* Inverted range entries are invalid. */
13564 complaint (_("Invalid .debug_rnglists data (inverted range)"));
13565 return false;
13566 }
13567
13568 /* Empty range entries have no effect. */
13569 if (range_beginning == range_end)
13570 continue;
13571
13572 range_beginning += base;
13573 range_end += base;
13574
13575 /* A not-uncommon case of bad debug info.
13576 Don't pollute the addrmap with bad data. */
13577 if (range_beginning + baseaddr == 0
13578 && !dwarf2_per_objfile->has_section_at_zero)
13579 {
13580 complaint (_(".debug_rnglists entry has start address of zero"
13581 " [in module %s]"), objfile_name (objfile));
13582 continue;
13583 }
13584
13585 callback (range_beginning, range_end);
13586 }
13587
13588 if (overflow)
13589 {
13590 complaint (_("Offset %d is not terminated "
13591 "for DW_AT_ranges attribute"),
13592 offset);
13593 return false;
13594 }
13595
13596 return true;
13597 }
13598
13599 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13600 Callback's type should be:
13601 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13602 Return 1 if the attributes are present and valid, otherwise, return 0. */
13603
13604 template <typename Callback>
13605 static int
13606 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
13607 Callback &&callback)
13608 {
13609 struct dwarf2_per_objfile *dwarf2_per_objfile
13610 = cu->per_cu->dwarf2_per_objfile;
13611 struct objfile *objfile = dwarf2_per_objfile->objfile;
13612 struct comp_unit_head *cu_header = &cu->header;
13613 bfd *obfd = objfile->obfd;
13614 unsigned int addr_size = cu_header->addr_size;
13615 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13616 /* Base address selection entry. */
13617 CORE_ADDR base;
13618 int found_base;
13619 unsigned int dummy;
13620 const gdb_byte *buffer;
13621 CORE_ADDR baseaddr;
13622
13623 if (cu_header->version >= 5)
13624 return dwarf2_rnglists_process (offset, cu, callback);
13625
13626 found_base = cu->base_known;
13627 base = cu->base_address;
13628
13629 dwarf2_per_objfile->ranges.read (objfile);
13630 if (offset >= dwarf2_per_objfile->ranges.size)
13631 {
13632 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13633 offset);
13634 return 0;
13635 }
13636 buffer = dwarf2_per_objfile->ranges.buffer + offset;
13637
13638 baseaddr = objfile->text_section_offset ();
13639
13640 while (1)
13641 {
13642 CORE_ADDR range_beginning, range_end;
13643
13644 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
13645 buffer += addr_size;
13646 range_end = cu->header.read_address (obfd, buffer, &dummy);
13647 buffer += addr_size;
13648 offset += 2 * addr_size;
13649
13650 /* An end of list marker is a pair of zero addresses. */
13651 if (range_beginning == 0 && range_end == 0)
13652 /* Found the end of list entry. */
13653 break;
13654
13655 /* Each base address selection entry is a pair of 2 values.
13656 The first is the largest possible address, the second is
13657 the base address. Check for a base address here. */
13658 if ((range_beginning & mask) == mask)
13659 {
13660 /* If we found the largest possible address, then we already
13661 have the base address in range_end. */
13662 base = range_end;
13663 found_base = 1;
13664 continue;
13665 }
13666
13667 if (!found_base)
13668 {
13669 /* We have no valid base address for the ranges
13670 data. */
13671 complaint (_("Invalid .debug_ranges data (no base address)"));
13672 return 0;
13673 }
13674
13675 if (range_beginning > range_end)
13676 {
13677 /* Inverted range entries are invalid. */
13678 complaint (_("Invalid .debug_ranges data (inverted range)"));
13679 return 0;
13680 }
13681
13682 /* Empty range entries have no effect. */
13683 if (range_beginning == range_end)
13684 continue;
13685
13686 range_beginning += base;
13687 range_end += base;
13688
13689 /* A not-uncommon case of bad debug info.
13690 Don't pollute the addrmap with bad data. */
13691 if (range_beginning + baseaddr == 0
13692 && !dwarf2_per_objfile->has_section_at_zero)
13693 {
13694 complaint (_(".debug_ranges entry has start address of zero"
13695 " [in module %s]"), objfile_name (objfile));
13696 continue;
13697 }
13698
13699 callback (range_beginning, range_end);
13700 }
13701
13702 return 1;
13703 }
13704
13705 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
13706 Return 1 if the attributes are present and valid, otherwise, return 0.
13707 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
13708
13709 static int
13710 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
13711 CORE_ADDR *high_return, struct dwarf2_cu *cu,
13712 dwarf2_psymtab *ranges_pst)
13713 {
13714 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13715 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13716 const CORE_ADDR baseaddr = objfile->text_section_offset ();
13717 int low_set = 0;
13718 CORE_ADDR low = 0;
13719 CORE_ADDR high = 0;
13720 int retval;
13721
13722 retval = dwarf2_ranges_process (offset, cu,
13723 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
13724 {
13725 if (ranges_pst != NULL)
13726 {
13727 CORE_ADDR lowpc;
13728 CORE_ADDR highpc;
13729
13730 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13731 range_beginning + baseaddr)
13732 - baseaddr);
13733 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13734 range_end + baseaddr)
13735 - baseaddr);
13736 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
13737 lowpc, highpc - 1, ranges_pst);
13738 }
13739
13740 /* FIXME: This is recording everything as a low-high
13741 segment of consecutive addresses. We should have a
13742 data structure for discontiguous block ranges
13743 instead. */
13744 if (! low_set)
13745 {
13746 low = range_beginning;
13747 high = range_end;
13748 low_set = 1;
13749 }
13750 else
13751 {
13752 if (range_beginning < low)
13753 low = range_beginning;
13754 if (range_end > high)
13755 high = range_end;
13756 }
13757 });
13758 if (!retval)
13759 return 0;
13760
13761 if (! low_set)
13762 /* If the first entry is an end-of-list marker, the range
13763 describes an empty scope, i.e. no instructions. */
13764 return 0;
13765
13766 if (low_return)
13767 *low_return = low;
13768 if (high_return)
13769 *high_return = high;
13770 return 1;
13771 }
13772
13773 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
13774 definition for the return value. *LOWPC and *HIGHPC are set iff
13775 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
13776
13777 static enum pc_bounds_kind
13778 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
13779 CORE_ADDR *highpc, struct dwarf2_cu *cu,
13780 dwarf2_psymtab *pst)
13781 {
13782 struct dwarf2_per_objfile *dwarf2_per_objfile
13783 = cu->per_cu->dwarf2_per_objfile;
13784 struct attribute *attr;
13785 struct attribute *attr_high;
13786 CORE_ADDR low = 0;
13787 CORE_ADDR high = 0;
13788 enum pc_bounds_kind ret;
13789
13790 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13791 if (attr_high)
13792 {
13793 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13794 if (attr != nullptr)
13795 {
13796 low = attr->value_as_address ();
13797 high = attr_high->value_as_address ();
13798 if (cu->header.version >= 4 && attr_high->form_is_constant ())
13799 high += low;
13800 }
13801 else
13802 /* Found high w/o low attribute. */
13803 return PC_BOUNDS_INVALID;
13804
13805 /* Found consecutive range of addresses. */
13806 ret = PC_BOUNDS_HIGH_LOW;
13807 }
13808 else
13809 {
13810 attr = dwarf2_attr (die, DW_AT_ranges, cu);
13811 if (attr != NULL)
13812 {
13813 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
13814 We take advantage of the fact that DW_AT_ranges does not appear
13815 in DW_TAG_compile_unit of DWO files. */
13816 int need_ranges_base = die->tag != DW_TAG_compile_unit;
13817 unsigned int ranges_offset = (DW_UNSND (attr)
13818 + (need_ranges_base
13819 ? cu->ranges_base
13820 : 0));
13821
13822 /* Value of the DW_AT_ranges attribute is the offset in the
13823 .debug_ranges section. */
13824 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
13825 return PC_BOUNDS_INVALID;
13826 /* Found discontinuous range of addresses. */
13827 ret = PC_BOUNDS_RANGES;
13828 }
13829 else
13830 return PC_BOUNDS_NOT_PRESENT;
13831 }
13832
13833 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
13834 if (high <= low)
13835 return PC_BOUNDS_INVALID;
13836
13837 /* When using the GNU linker, .gnu.linkonce. sections are used to
13838 eliminate duplicate copies of functions and vtables and such.
13839 The linker will arbitrarily choose one and discard the others.
13840 The AT_*_pc values for such functions refer to local labels in
13841 these sections. If the section from that file was discarded, the
13842 labels are not in the output, so the relocs get a value of 0.
13843 If this is a discarded function, mark the pc bounds as invalid,
13844 so that GDB will ignore it. */
13845 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
13846 return PC_BOUNDS_INVALID;
13847
13848 *lowpc = low;
13849 if (highpc)
13850 *highpc = high;
13851 return ret;
13852 }
13853
13854 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
13855 its low and high PC addresses. Do nothing if these addresses could not
13856 be determined. Otherwise, set LOWPC to the low address if it is smaller,
13857 and HIGHPC to the high address if greater than HIGHPC. */
13858
13859 static void
13860 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
13861 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13862 struct dwarf2_cu *cu)
13863 {
13864 CORE_ADDR low, high;
13865 struct die_info *child = die->child;
13866
13867 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
13868 {
13869 *lowpc = std::min (*lowpc, low);
13870 *highpc = std::max (*highpc, high);
13871 }
13872
13873 /* If the language does not allow nested subprograms (either inside
13874 subprograms or lexical blocks), we're done. */
13875 if (cu->language != language_ada)
13876 return;
13877
13878 /* Check all the children of the given DIE. If it contains nested
13879 subprograms, then check their pc bounds. Likewise, we need to
13880 check lexical blocks as well, as they may also contain subprogram
13881 definitions. */
13882 while (child && child->tag)
13883 {
13884 if (child->tag == DW_TAG_subprogram
13885 || child->tag == DW_TAG_lexical_block)
13886 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
13887 child = sibling_die (child);
13888 }
13889 }
13890
13891 /* Get the low and high pc's represented by the scope DIE, and store
13892 them in *LOWPC and *HIGHPC. If the correct values can't be
13893 determined, set *LOWPC to -1 and *HIGHPC to 0. */
13894
13895 static void
13896 get_scope_pc_bounds (struct die_info *die,
13897 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13898 struct dwarf2_cu *cu)
13899 {
13900 CORE_ADDR best_low = (CORE_ADDR) -1;
13901 CORE_ADDR best_high = (CORE_ADDR) 0;
13902 CORE_ADDR current_low, current_high;
13903
13904 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
13905 >= PC_BOUNDS_RANGES)
13906 {
13907 best_low = current_low;
13908 best_high = current_high;
13909 }
13910 else
13911 {
13912 struct die_info *child = die->child;
13913
13914 while (child && child->tag)
13915 {
13916 switch (child->tag) {
13917 case DW_TAG_subprogram:
13918 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
13919 break;
13920 case DW_TAG_namespace:
13921 case DW_TAG_module:
13922 /* FIXME: carlton/2004-01-16: Should we do this for
13923 DW_TAG_class_type/DW_TAG_structure_type, too? I think
13924 that current GCC's always emit the DIEs corresponding
13925 to definitions of methods of classes as children of a
13926 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
13927 the DIEs giving the declarations, which could be
13928 anywhere). But I don't see any reason why the
13929 standards says that they have to be there. */
13930 get_scope_pc_bounds (child, &current_low, &current_high, cu);
13931
13932 if (current_low != ((CORE_ADDR) -1))
13933 {
13934 best_low = std::min (best_low, current_low);
13935 best_high = std::max (best_high, current_high);
13936 }
13937 break;
13938 default:
13939 /* Ignore. */
13940 break;
13941 }
13942
13943 child = sibling_die (child);
13944 }
13945 }
13946
13947 *lowpc = best_low;
13948 *highpc = best_high;
13949 }
13950
13951 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
13952 in DIE. */
13953
13954 static void
13955 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
13956 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
13957 {
13958 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13959 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13960 struct attribute *attr;
13961 struct attribute *attr_high;
13962
13963 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13964 if (attr_high)
13965 {
13966 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13967 if (attr != nullptr)
13968 {
13969 CORE_ADDR low = attr->value_as_address ();
13970 CORE_ADDR high = attr_high->value_as_address ();
13971
13972 if (cu->header.version >= 4 && attr_high->form_is_constant ())
13973 high += low;
13974
13975 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
13976 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
13977 cu->get_builder ()->record_block_range (block, low, high - 1);
13978 }
13979 }
13980
13981 attr = dwarf2_attr (die, DW_AT_ranges, cu);
13982 if (attr != nullptr)
13983 {
13984 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
13985 We take advantage of the fact that DW_AT_ranges does not appear
13986 in DW_TAG_compile_unit of DWO files. */
13987 int need_ranges_base = die->tag != DW_TAG_compile_unit;
13988
13989 /* The value of the DW_AT_ranges attribute is the offset of the
13990 address range list in the .debug_ranges section. */
13991 unsigned long offset = (DW_UNSND (attr)
13992 + (need_ranges_base ? cu->ranges_base : 0));
13993
13994 std::vector<blockrange> blockvec;
13995 dwarf2_ranges_process (offset, cu,
13996 [&] (CORE_ADDR start, CORE_ADDR end)
13997 {
13998 start += baseaddr;
13999 end += baseaddr;
14000 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14001 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14002 cu->get_builder ()->record_block_range (block, start, end - 1);
14003 blockvec.emplace_back (start, end);
14004 });
14005
14006 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
14007 }
14008 }
14009
14010 /* Check whether the producer field indicates either of GCC < 4.6, or the
14011 Intel C/C++ compiler, and cache the result in CU. */
14012
14013 static void
14014 check_producer (struct dwarf2_cu *cu)
14015 {
14016 int major, minor;
14017
14018 if (cu->producer == NULL)
14019 {
14020 /* For unknown compilers expect their behavior is DWARF version
14021 compliant.
14022
14023 GCC started to support .debug_types sections by -gdwarf-4 since
14024 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14025 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14026 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14027 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14028 }
14029 else if (producer_is_gcc (cu->producer, &major, &minor))
14030 {
14031 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14032 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14033 }
14034 else if (producer_is_icc (cu->producer, &major, &minor))
14035 {
14036 cu->producer_is_icc = true;
14037 cu->producer_is_icc_lt_14 = major < 14;
14038 }
14039 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14040 cu->producer_is_codewarrior = true;
14041 else
14042 {
14043 /* For other non-GCC compilers, expect their behavior is DWARF version
14044 compliant. */
14045 }
14046
14047 cu->checked_producer = true;
14048 }
14049
14050 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14051 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14052 during 4.6.0 experimental. */
14053
14054 static bool
14055 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14056 {
14057 if (!cu->checked_producer)
14058 check_producer (cu);
14059
14060 return cu->producer_is_gxx_lt_4_6;
14061 }
14062
14063
14064 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14065 with incorrect is_stmt attributes. */
14066
14067 static bool
14068 producer_is_codewarrior (struct dwarf2_cu *cu)
14069 {
14070 if (!cu->checked_producer)
14071 check_producer (cu);
14072
14073 return cu->producer_is_codewarrior;
14074 }
14075
14076 /* Return the default accessibility type if it is not overridden by
14077 DW_AT_accessibility. */
14078
14079 static enum dwarf_access_attribute
14080 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14081 {
14082 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14083 {
14084 /* The default DWARF 2 accessibility for members is public, the default
14085 accessibility for inheritance is private. */
14086
14087 if (die->tag != DW_TAG_inheritance)
14088 return DW_ACCESS_public;
14089 else
14090 return DW_ACCESS_private;
14091 }
14092 else
14093 {
14094 /* DWARF 3+ defines the default accessibility a different way. The same
14095 rules apply now for DW_TAG_inheritance as for the members and it only
14096 depends on the container kind. */
14097
14098 if (die->parent->tag == DW_TAG_class_type)
14099 return DW_ACCESS_private;
14100 else
14101 return DW_ACCESS_public;
14102 }
14103 }
14104
14105 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14106 offset. If the attribute was not found return 0, otherwise return
14107 1. If it was found but could not properly be handled, set *OFFSET
14108 to 0. */
14109
14110 static int
14111 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14112 LONGEST *offset)
14113 {
14114 struct attribute *attr;
14115
14116 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14117 if (attr != NULL)
14118 {
14119 *offset = 0;
14120
14121 /* Note that we do not check for a section offset first here.
14122 This is because DW_AT_data_member_location is new in DWARF 4,
14123 so if we see it, we can assume that a constant form is really
14124 a constant and not a section offset. */
14125 if (attr->form_is_constant ())
14126 *offset = dwarf2_get_attr_constant_value (attr, 0);
14127 else if (attr->form_is_section_offset ())
14128 dwarf2_complex_location_expr_complaint ();
14129 else if (attr->form_is_block ())
14130 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14131 else
14132 dwarf2_complex_location_expr_complaint ();
14133
14134 return 1;
14135 }
14136
14137 return 0;
14138 }
14139
14140 /* Add an aggregate field to the field list. */
14141
14142 static void
14143 dwarf2_add_field (struct field_info *fip, struct die_info *die,
14144 struct dwarf2_cu *cu)
14145 {
14146 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14147 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14148 struct nextfield *new_field;
14149 struct attribute *attr;
14150 struct field *fp;
14151 const char *fieldname = "";
14152
14153 if (die->tag == DW_TAG_inheritance)
14154 {
14155 fip->baseclasses.emplace_back ();
14156 new_field = &fip->baseclasses.back ();
14157 }
14158 else
14159 {
14160 fip->fields.emplace_back ();
14161 new_field = &fip->fields.back ();
14162 }
14163
14164 fip->nfields++;
14165
14166 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14167 if (attr != nullptr)
14168 new_field->accessibility = DW_UNSND (attr);
14169 else
14170 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
14171 if (new_field->accessibility != DW_ACCESS_public)
14172 fip->non_public_fields = 1;
14173
14174 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14175 if (attr != nullptr)
14176 new_field->virtuality = DW_UNSND (attr);
14177 else
14178 new_field->virtuality = DW_VIRTUALITY_none;
14179
14180 fp = &new_field->field;
14181
14182 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
14183 {
14184 LONGEST offset;
14185
14186 /* Data member other than a C++ static data member. */
14187
14188 /* Get type of field. */
14189 fp->type = die_type (die, cu);
14190
14191 SET_FIELD_BITPOS (*fp, 0);
14192
14193 /* Get bit size of field (zero if none). */
14194 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
14195 if (attr != nullptr)
14196 {
14197 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
14198 }
14199 else
14200 {
14201 FIELD_BITSIZE (*fp) = 0;
14202 }
14203
14204 /* Get bit offset of field. */
14205 if (handle_data_member_location (die, cu, &offset))
14206 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
14207 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
14208 if (attr != nullptr)
14209 {
14210 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
14211 {
14212 /* For big endian bits, the DW_AT_bit_offset gives the
14213 additional bit offset from the MSB of the containing
14214 anonymous object to the MSB of the field. We don't
14215 have to do anything special since we don't need to
14216 know the size of the anonymous object. */
14217 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
14218 }
14219 else
14220 {
14221 /* For little endian bits, compute the bit offset to the
14222 MSB of the anonymous object, subtract off the number of
14223 bits from the MSB of the field to the MSB of the
14224 object, and then subtract off the number of bits of
14225 the field itself. The result is the bit offset of
14226 the LSB of the field. */
14227 int anonymous_size;
14228 int bit_offset = DW_UNSND (attr);
14229
14230 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14231 if (attr != nullptr)
14232 {
14233 /* The size of the anonymous object containing
14234 the bit field is explicit, so use the
14235 indicated size (in bytes). */
14236 anonymous_size = DW_UNSND (attr);
14237 }
14238 else
14239 {
14240 /* The size of the anonymous object containing
14241 the bit field must be inferred from the type
14242 attribute of the data member containing the
14243 bit field. */
14244 anonymous_size = TYPE_LENGTH (fp->type);
14245 }
14246 SET_FIELD_BITPOS (*fp,
14247 (FIELD_BITPOS (*fp)
14248 + anonymous_size * bits_per_byte
14249 - bit_offset - FIELD_BITSIZE (*fp)));
14250 }
14251 }
14252 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14253 if (attr != NULL)
14254 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
14255 + dwarf2_get_attr_constant_value (attr, 0)));
14256
14257 /* Get name of field. */
14258 fieldname = dwarf2_name (die, cu);
14259 if (fieldname == NULL)
14260 fieldname = "";
14261
14262 /* The name is already allocated along with this objfile, so we don't
14263 need to duplicate it for the type. */
14264 fp->name = fieldname;
14265
14266 /* Change accessibility for artificial fields (e.g. virtual table
14267 pointer or virtual base class pointer) to private. */
14268 if (dwarf2_attr (die, DW_AT_artificial, cu))
14269 {
14270 FIELD_ARTIFICIAL (*fp) = 1;
14271 new_field->accessibility = DW_ACCESS_private;
14272 fip->non_public_fields = 1;
14273 }
14274 }
14275 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
14276 {
14277 /* C++ static member. */
14278
14279 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14280 is a declaration, but all versions of G++ as of this writing
14281 (so through at least 3.2.1) incorrectly generate
14282 DW_TAG_variable tags. */
14283
14284 const char *physname;
14285
14286 /* Get name of field. */
14287 fieldname = dwarf2_name (die, cu);
14288 if (fieldname == NULL)
14289 return;
14290
14291 attr = dwarf2_attr (die, DW_AT_const_value, cu);
14292 if (attr
14293 /* Only create a symbol if this is an external value.
14294 new_symbol checks this and puts the value in the global symbol
14295 table, which we want. If it is not external, new_symbol
14296 will try to put the value in cu->list_in_scope which is wrong. */
14297 && dwarf2_flag_true_p (die, DW_AT_external, cu))
14298 {
14299 /* A static const member, not much different than an enum as far as
14300 we're concerned, except that we can support more types. */
14301 new_symbol (die, NULL, cu);
14302 }
14303
14304 /* Get physical name. */
14305 physname = dwarf2_physname (fieldname, die, cu);
14306
14307 /* The name is already allocated along with this objfile, so we don't
14308 need to duplicate it for the type. */
14309 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
14310 FIELD_TYPE (*fp) = die_type (die, cu);
14311 FIELD_NAME (*fp) = fieldname;
14312 }
14313 else if (die->tag == DW_TAG_inheritance)
14314 {
14315 LONGEST offset;
14316
14317 /* C++ base class field. */
14318 if (handle_data_member_location (die, cu, &offset))
14319 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
14320 FIELD_BITSIZE (*fp) = 0;
14321 FIELD_TYPE (*fp) = die_type (die, cu);
14322 FIELD_NAME (*fp) = TYPE_NAME (fp->type);
14323 }
14324 else if (die->tag == DW_TAG_variant_part)
14325 {
14326 /* process_structure_scope will treat this DIE as a union. */
14327 process_structure_scope (die, cu);
14328
14329 /* The variant part is relative to the start of the enclosing
14330 structure. */
14331 SET_FIELD_BITPOS (*fp, 0);
14332 fp->type = get_die_type (die, cu);
14333 fp->artificial = 1;
14334 fp->name = "<<variant>>";
14335
14336 /* Normally a DW_TAG_variant_part won't have a size, but our
14337 representation requires one, so set it to the maximum of the
14338 child sizes, being sure to account for the offset at which
14339 each child is seen. */
14340 if (TYPE_LENGTH (fp->type) == 0)
14341 {
14342 unsigned max = 0;
14343 for (int i = 0; i < TYPE_NFIELDS (fp->type); ++i)
14344 {
14345 unsigned len = ((TYPE_FIELD_BITPOS (fp->type, i) + 7) / 8
14346 + TYPE_LENGTH (TYPE_FIELD_TYPE (fp->type, i)));
14347 if (len > max)
14348 max = len;
14349 }
14350 TYPE_LENGTH (fp->type) = max;
14351 }
14352 }
14353 else
14354 gdb_assert_not_reached ("missing case in dwarf2_add_field");
14355 }
14356
14357 /* Can the type given by DIE define another type? */
14358
14359 static bool
14360 type_can_define_types (const struct die_info *die)
14361 {
14362 switch (die->tag)
14363 {
14364 case DW_TAG_typedef:
14365 case DW_TAG_class_type:
14366 case DW_TAG_structure_type:
14367 case DW_TAG_union_type:
14368 case DW_TAG_enumeration_type:
14369 return true;
14370
14371 default:
14372 return false;
14373 }
14374 }
14375
14376 /* Add a type definition defined in the scope of the FIP's class. */
14377
14378 static void
14379 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14380 struct dwarf2_cu *cu)
14381 {
14382 struct decl_field fp;
14383 memset (&fp, 0, sizeof (fp));
14384
14385 gdb_assert (type_can_define_types (die));
14386
14387 /* Get name of field. NULL is okay here, meaning an anonymous type. */
14388 fp.name = dwarf2_name (die, cu);
14389 fp.type = read_type_die (die, cu);
14390
14391 /* Save accessibility. */
14392 enum dwarf_access_attribute accessibility;
14393 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14394 if (attr != NULL)
14395 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14396 else
14397 accessibility = dwarf2_default_access_attribute (die, cu);
14398 switch (accessibility)
14399 {
14400 case DW_ACCESS_public:
14401 /* The assumed value if neither private nor protected. */
14402 break;
14403 case DW_ACCESS_private:
14404 fp.is_private = 1;
14405 break;
14406 case DW_ACCESS_protected:
14407 fp.is_protected = 1;
14408 break;
14409 default:
14410 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
14411 }
14412
14413 if (die->tag == DW_TAG_typedef)
14414 fip->typedef_field_list.push_back (fp);
14415 else
14416 fip->nested_types_list.push_back (fp);
14417 }
14418
14419 /* Create the vector of fields, and attach it to the type. */
14420
14421 static void
14422 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
14423 struct dwarf2_cu *cu)
14424 {
14425 int nfields = fip->nfields;
14426
14427 /* Record the field count, allocate space for the array of fields,
14428 and create blank accessibility bitfields if necessary. */
14429 TYPE_NFIELDS (type) = nfields;
14430 TYPE_FIELDS (type) = (struct field *)
14431 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
14432
14433 if (fip->non_public_fields && cu->language != language_ada)
14434 {
14435 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14436
14437 TYPE_FIELD_PRIVATE_BITS (type) =
14438 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14439 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14440
14441 TYPE_FIELD_PROTECTED_BITS (type) =
14442 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14443 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14444
14445 TYPE_FIELD_IGNORE_BITS (type) =
14446 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14447 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
14448 }
14449
14450 /* If the type has baseclasses, allocate and clear a bit vector for
14451 TYPE_FIELD_VIRTUAL_BITS. */
14452 if (!fip->baseclasses.empty () && cu->language != language_ada)
14453 {
14454 int num_bytes = B_BYTES (fip->baseclasses.size ());
14455 unsigned char *pointer;
14456
14457 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14458 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
14459 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
14460 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
14461 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
14462 }
14463
14464 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
14465 {
14466 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
14467
14468 for (int index = 0; index < nfields; ++index)
14469 {
14470 struct nextfield &field = fip->fields[index];
14471
14472 if (field.variant.is_discriminant)
14473 di->discriminant_index = index;
14474 else if (field.variant.default_branch)
14475 di->default_index = index;
14476 else
14477 di->discriminants[index] = field.variant.discriminant_value;
14478 }
14479 }
14480
14481 /* Copy the saved-up fields into the field vector. */
14482 for (int i = 0; i < nfields; ++i)
14483 {
14484 struct nextfield &field
14485 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
14486 : fip->fields[i - fip->baseclasses.size ()]);
14487
14488 TYPE_FIELD (type, i) = field.field;
14489 switch (field.accessibility)
14490 {
14491 case DW_ACCESS_private:
14492 if (cu->language != language_ada)
14493 SET_TYPE_FIELD_PRIVATE (type, i);
14494 break;
14495
14496 case DW_ACCESS_protected:
14497 if (cu->language != language_ada)
14498 SET_TYPE_FIELD_PROTECTED (type, i);
14499 break;
14500
14501 case DW_ACCESS_public:
14502 break;
14503
14504 default:
14505 /* Unknown accessibility. Complain and treat it as public. */
14506 {
14507 complaint (_("unsupported accessibility %d"),
14508 field.accessibility);
14509 }
14510 break;
14511 }
14512 if (i < fip->baseclasses.size ())
14513 {
14514 switch (field.virtuality)
14515 {
14516 case DW_VIRTUALITY_virtual:
14517 case DW_VIRTUALITY_pure_virtual:
14518 if (cu->language == language_ada)
14519 error (_("unexpected virtuality in component of Ada type"));
14520 SET_TYPE_FIELD_VIRTUAL (type, i);
14521 break;
14522 }
14523 }
14524 }
14525 }
14526
14527 /* Return true if this member function is a constructor, false
14528 otherwise. */
14529
14530 static int
14531 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
14532 {
14533 const char *fieldname;
14534 const char *type_name;
14535 int len;
14536
14537 if (die->parent == NULL)
14538 return 0;
14539
14540 if (die->parent->tag != DW_TAG_structure_type
14541 && die->parent->tag != DW_TAG_union_type
14542 && die->parent->tag != DW_TAG_class_type)
14543 return 0;
14544
14545 fieldname = dwarf2_name (die, cu);
14546 type_name = dwarf2_name (die->parent, cu);
14547 if (fieldname == NULL || type_name == NULL)
14548 return 0;
14549
14550 len = strlen (fieldname);
14551 return (strncmp (fieldname, type_name, len) == 0
14552 && (type_name[len] == '\0' || type_name[len] == '<'));
14553 }
14554
14555 /* Check if the given VALUE is a recognized enum
14556 dwarf_defaulted_attribute constant according to DWARF5 spec,
14557 Table 7.24. */
14558
14559 static bool
14560 is_valid_DW_AT_defaulted (ULONGEST value)
14561 {
14562 switch (value)
14563 {
14564 case DW_DEFAULTED_no:
14565 case DW_DEFAULTED_in_class:
14566 case DW_DEFAULTED_out_of_class:
14567 return true;
14568 }
14569
14570 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
14571 return false;
14572 }
14573
14574 /* Add a member function to the proper fieldlist. */
14575
14576 static void
14577 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
14578 struct type *type, struct dwarf2_cu *cu)
14579 {
14580 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14581 struct attribute *attr;
14582 int i;
14583 struct fnfieldlist *flp = nullptr;
14584 struct fn_field *fnp;
14585 const char *fieldname;
14586 struct type *this_type;
14587 enum dwarf_access_attribute accessibility;
14588
14589 if (cu->language == language_ada)
14590 error (_("unexpected member function in Ada type"));
14591
14592 /* Get name of member function. */
14593 fieldname = dwarf2_name (die, cu);
14594 if (fieldname == NULL)
14595 return;
14596
14597 /* Look up member function name in fieldlist. */
14598 for (i = 0; i < fip->fnfieldlists.size (); i++)
14599 {
14600 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
14601 {
14602 flp = &fip->fnfieldlists[i];
14603 break;
14604 }
14605 }
14606
14607 /* Create a new fnfieldlist if necessary. */
14608 if (flp == nullptr)
14609 {
14610 fip->fnfieldlists.emplace_back ();
14611 flp = &fip->fnfieldlists.back ();
14612 flp->name = fieldname;
14613 i = fip->fnfieldlists.size () - 1;
14614 }
14615
14616 /* Create a new member function field and add it to the vector of
14617 fnfieldlists. */
14618 flp->fnfields.emplace_back ();
14619 fnp = &flp->fnfields.back ();
14620
14621 /* Delay processing of the physname until later. */
14622 if (cu->language == language_cplus)
14623 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
14624 die, cu);
14625 else
14626 {
14627 const char *physname = dwarf2_physname (fieldname, die, cu);
14628 fnp->physname = physname ? physname : "";
14629 }
14630
14631 fnp->type = alloc_type (objfile);
14632 this_type = read_type_die (die, cu);
14633 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
14634 {
14635 int nparams = TYPE_NFIELDS (this_type);
14636
14637 /* TYPE is the domain of this method, and THIS_TYPE is the type
14638 of the method itself (TYPE_CODE_METHOD). */
14639 smash_to_method_type (fnp->type, type,
14640 TYPE_TARGET_TYPE (this_type),
14641 TYPE_FIELDS (this_type),
14642 TYPE_NFIELDS (this_type),
14643 TYPE_VARARGS (this_type));
14644
14645 /* Handle static member functions.
14646 Dwarf2 has no clean way to discern C++ static and non-static
14647 member functions. G++ helps GDB by marking the first
14648 parameter for non-static member functions (which is the this
14649 pointer) as artificial. We obtain this information from
14650 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
14651 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
14652 fnp->voffset = VOFFSET_STATIC;
14653 }
14654 else
14655 complaint (_("member function type missing for '%s'"),
14656 dwarf2_full_name (fieldname, die, cu));
14657
14658 /* Get fcontext from DW_AT_containing_type if present. */
14659 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
14660 fnp->fcontext = die_containing_type (die, cu);
14661
14662 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
14663 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
14664
14665 /* Get accessibility. */
14666 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14667 if (attr != nullptr)
14668 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14669 else
14670 accessibility = dwarf2_default_access_attribute (die, cu);
14671 switch (accessibility)
14672 {
14673 case DW_ACCESS_private:
14674 fnp->is_private = 1;
14675 break;
14676 case DW_ACCESS_protected:
14677 fnp->is_protected = 1;
14678 break;
14679 }
14680
14681 /* Check for artificial methods. */
14682 attr = dwarf2_attr (die, DW_AT_artificial, cu);
14683 if (attr && DW_UNSND (attr) != 0)
14684 fnp->is_artificial = 1;
14685
14686 /* Check for defaulted methods. */
14687 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
14688 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
14689 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
14690
14691 /* Check for deleted methods. */
14692 attr = dwarf2_attr (die, DW_AT_deleted, cu);
14693 if (attr != nullptr && DW_UNSND (attr) != 0)
14694 fnp->is_deleted = 1;
14695
14696 fnp->is_constructor = dwarf2_is_constructor (die, cu);
14697
14698 /* Get index in virtual function table if it is a virtual member
14699 function. For older versions of GCC, this is an offset in the
14700 appropriate virtual table, as specified by DW_AT_containing_type.
14701 For everyone else, it is an expression to be evaluated relative
14702 to the object address. */
14703
14704 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
14705 if (attr != nullptr)
14706 {
14707 if (attr->form_is_block () && DW_BLOCK (attr)->size > 0)
14708 {
14709 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
14710 {
14711 /* Old-style GCC. */
14712 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
14713 }
14714 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
14715 || (DW_BLOCK (attr)->size > 1
14716 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
14717 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
14718 {
14719 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
14720 if ((fnp->voffset % cu->header.addr_size) != 0)
14721 dwarf2_complex_location_expr_complaint ();
14722 else
14723 fnp->voffset /= cu->header.addr_size;
14724 fnp->voffset += 2;
14725 }
14726 else
14727 dwarf2_complex_location_expr_complaint ();
14728
14729 if (!fnp->fcontext)
14730 {
14731 /* If there is no `this' field and no DW_AT_containing_type,
14732 we cannot actually find a base class context for the
14733 vtable! */
14734 if (TYPE_NFIELDS (this_type) == 0
14735 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
14736 {
14737 complaint (_("cannot determine context for virtual member "
14738 "function \"%s\" (offset %s)"),
14739 fieldname, sect_offset_str (die->sect_off));
14740 }
14741 else
14742 {
14743 fnp->fcontext
14744 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
14745 }
14746 }
14747 }
14748 else if (attr->form_is_section_offset ())
14749 {
14750 dwarf2_complex_location_expr_complaint ();
14751 }
14752 else
14753 {
14754 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
14755 fieldname);
14756 }
14757 }
14758 else
14759 {
14760 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14761 if (attr && DW_UNSND (attr))
14762 {
14763 /* GCC does this, as of 2008-08-25; PR debug/37237. */
14764 complaint (_("Member function \"%s\" (offset %s) is virtual "
14765 "but the vtable offset is not specified"),
14766 fieldname, sect_offset_str (die->sect_off));
14767 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14768 TYPE_CPLUS_DYNAMIC (type) = 1;
14769 }
14770 }
14771 }
14772
14773 /* Create the vector of member function fields, and attach it to the type. */
14774
14775 static void
14776 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
14777 struct dwarf2_cu *cu)
14778 {
14779 if (cu->language == language_ada)
14780 error (_("unexpected member functions in Ada type"));
14781
14782 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14783 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
14784 TYPE_ALLOC (type,
14785 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
14786
14787 for (int i = 0; i < fip->fnfieldlists.size (); i++)
14788 {
14789 struct fnfieldlist &nf = fip->fnfieldlists[i];
14790 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
14791
14792 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
14793 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
14794 fn_flp->fn_fields = (struct fn_field *)
14795 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
14796
14797 for (int k = 0; k < nf.fnfields.size (); ++k)
14798 fn_flp->fn_fields[k] = nf.fnfields[k];
14799 }
14800
14801 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
14802 }
14803
14804 /* Returns non-zero if NAME is the name of a vtable member in CU's
14805 language, zero otherwise. */
14806 static int
14807 is_vtable_name (const char *name, struct dwarf2_cu *cu)
14808 {
14809 static const char vptr[] = "_vptr";
14810
14811 /* Look for the C++ form of the vtable. */
14812 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
14813 return 1;
14814
14815 return 0;
14816 }
14817
14818 /* GCC outputs unnamed structures that are really pointers to member
14819 functions, with the ABI-specified layout. If TYPE describes
14820 such a structure, smash it into a member function type.
14821
14822 GCC shouldn't do this; it should just output pointer to member DIEs.
14823 This is GCC PR debug/28767. */
14824
14825 static void
14826 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
14827 {
14828 struct type *pfn_type, *self_type, *new_type;
14829
14830 /* Check for a structure with no name and two children. */
14831 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
14832 return;
14833
14834 /* Check for __pfn and __delta members. */
14835 if (TYPE_FIELD_NAME (type, 0) == NULL
14836 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
14837 || TYPE_FIELD_NAME (type, 1) == NULL
14838 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
14839 return;
14840
14841 /* Find the type of the method. */
14842 pfn_type = TYPE_FIELD_TYPE (type, 0);
14843 if (pfn_type == NULL
14844 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
14845 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
14846 return;
14847
14848 /* Look for the "this" argument. */
14849 pfn_type = TYPE_TARGET_TYPE (pfn_type);
14850 if (TYPE_NFIELDS (pfn_type) == 0
14851 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
14852 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
14853 return;
14854
14855 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
14856 new_type = alloc_type (objfile);
14857 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
14858 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
14859 TYPE_VARARGS (pfn_type));
14860 smash_to_methodptr_type (type, new_type);
14861 }
14862
14863 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
14864 appropriate error checking and issuing complaints if there is a
14865 problem. */
14866
14867 static ULONGEST
14868 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
14869 {
14870 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
14871
14872 if (attr == nullptr)
14873 return 0;
14874
14875 if (!attr->form_is_constant ())
14876 {
14877 complaint (_("DW_AT_alignment must have constant form"
14878 " - DIE at %s [in module %s]"),
14879 sect_offset_str (die->sect_off),
14880 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14881 return 0;
14882 }
14883
14884 ULONGEST align;
14885 if (attr->form == DW_FORM_sdata)
14886 {
14887 LONGEST val = DW_SND (attr);
14888 if (val < 0)
14889 {
14890 complaint (_("DW_AT_alignment value must not be negative"
14891 " - DIE at %s [in module %s]"),
14892 sect_offset_str (die->sect_off),
14893 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14894 return 0;
14895 }
14896 align = val;
14897 }
14898 else
14899 align = DW_UNSND (attr);
14900
14901 if (align == 0)
14902 {
14903 complaint (_("DW_AT_alignment value must not be zero"
14904 " - DIE at %s [in module %s]"),
14905 sect_offset_str (die->sect_off),
14906 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14907 return 0;
14908 }
14909 if ((align & (align - 1)) != 0)
14910 {
14911 complaint (_("DW_AT_alignment value must be a power of 2"
14912 " - DIE at %s [in module %s]"),
14913 sect_offset_str (die->sect_off),
14914 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14915 return 0;
14916 }
14917
14918 return align;
14919 }
14920
14921 /* If the DIE has a DW_AT_alignment attribute, use its value to set
14922 the alignment for TYPE. */
14923
14924 static void
14925 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
14926 struct type *type)
14927 {
14928 if (!set_type_align (type, get_alignment (cu, die)))
14929 complaint (_("DW_AT_alignment value too large"
14930 " - DIE at %s [in module %s]"),
14931 sect_offset_str (die->sect_off),
14932 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14933 }
14934
14935 /* Check if the given VALUE is a valid enum dwarf_calling_convention
14936 constant for a type, according to DWARF5 spec, Table 5.5. */
14937
14938 static bool
14939 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
14940 {
14941 switch (value)
14942 {
14943 case DW_CC_normal:
14944 case DW_CC_pass_by_reference:
14945 case DW_CC_pass_by_value:
14946 return true;
14947
14948 default:
14949 complaint (_("unrecognized DW_AT_calling_convention value "
14950 "(%s) for a type"), pulongest (value));
14951 return false;
14952 }
14953 }
14954
14955 /* Check if the given VALUE is a valid enum dwarf_calling_convention
14956 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
14957 also according to GNU-specific values (see include/dwarf2.h). */
14958
14959 static bool
14960 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
14961 {
14962 switch (value)
14963 {
14964 case DW_CC_normal:
14965 case DW_CC_program:
14966 case DW_CC_nocall:
14967 return true;
14968
14969 case DW_CC_GNU_renesas_sh:
14970 case DW_CC_GNU_borland_fastcall_i386:
14971 case DW_CC_GDB_IBM_OpenCL:
14972 return true;
14973
14974 default:
14975 complaint (_("unrecognized DW_AT_calling_convention value "
14976 "(%s) for a subroutine"), pulongest (value));
14977 return false;
14978 }
14979 }
14980
14981 /* Called when we find the DIE that starts a structure or union scope
14982 (definition) to create a type for the structure or union. Fill in
14983 the type's name and general properties; the members will not be
14984 processed until process_structure_scope. A symbol table entry for
14985 the type will also not be done until process_structure_scope (assuming
14986 the type has a name).
14987
14988 NOTE: we need to call these functions regardless of whether or not the
14989 DIE has a DW_AT_name attribute, since it might be an anonymous
14990 structure or union. This gets the type entered into our set of
14991 user defined types. */
14992
14993 static struct type *
14994 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
14995 {
14996 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14997 struct type *type;
14998 struct attribute *attr;
14999 const char *name;
15000
15001 /* If the definition of this type lives in .debug_types, read that type.
15002 Don't follow DW_AT_specification though, that will take us back up
15003 the chain and we want to go down. */
15004 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
15005 if (attr != nullptr)
15006 {
15007 type = get_DW_AT_signature_type (die, attr, cu);
15008
15009 /* The type's CU may not be the same as CU.
15010 Ensure TYPE is recorded with CU in die_type_hash. */
15011 return set_die_type (die, type, cu);
15012 }
15013
15014 type = alloc_type (objfile);
15015 INIT_CPLUS_SPECIFIC (type);
15016
15017 name = dwarf2_name (die, cu);
15018 if (name != NULL)
15019 {
15020 if (cu->language == language_cplus
15021 || cu->language == language_d
15022 || cu->language == language_rust)
15023 {
15024 const char *full_name = dwarf2_full_name (name, die, cu);
15025
15026 /* dwarf2_full_name might have already finished building the DIE's
15027 type. If so, there is no need to continue. */
15028 if (get_die_type (die, cu) != NULL)
15029 return get_die_type (die, cu);
15030
15031 TYPE_NAME (type) = full_name;
15032 }
15033 else
15034 {
15035 /* The name is already allocated along with this objfile, so
15036 we don't need to duplicate it for the type. */
15037 TYPE_NAME (type) = name;
15038 }
15039 }
15040
15041 if (die->tag == DW_TAG_structure_type)
15042 {
15043 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15044 }
15045 else if (die->tag == DW_TAG_union_type)
15046 {
15047 TYPE_CODE (type) = TYPE_CODE_UNION;
15048 }
15049 else if (die->tag == DW_TAG_variant_part)
15050 {
15051 TYPE_CODE (type) = TYPE_CODE_UNION;
15052 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
15053 }
15054 else
15055 {
15056 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15057 }
15058
15059 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15060 TYPE_DECLARED_CLASS (type) = 1;
15061
15062 /* Store the calling convention in the type if it's available in
15063 the die. Otherwise the calling convention remains set to
15064 the default value DW_CC_normal. */
15065 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15066 if (attr != nullptr
15067 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15068 {
15069 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15070 TYPE_CPLUS_CALLING_CONVENTION (type)
15071 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15072 }
15073
15074 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15075 if (attr != nullptr)
15076 {
15077 if (attr->form_is_constant ())
15078 TYPE_LENGTH (type) = DW_UNSND (attr);
15079 else
15080 {
15081 /* For the moment, dynamic type sizes are not supported
15082 by GDB's struct type. The actual size is determined
15083 on-demand when resolving the type of a given object,
15084 so set the type's length to zero for now. Otherwise,
15085 we record an expression as the length, and that expression
15086 could lead to a very large value, which could eventually
15087 lead to us trying to allocate that much memory when creating
15088 a value of that type. */
15089 TYPE_LENGTH (type) = 0;
15090 }
15091 }
15092 else
15093 {
15094 TYPE_LENGTH (type) = 0;
15095 }
15096
15097 maybe_set_alignment (cu, die, type);
15098
15099 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15100 {
15101 /* ICC<14 does not output the required DW_AT_declaration on
15102 incomplete types, but gives them a size of zero. */
15103 TYPE_STUB (type) = 1;
15104 }
15105 else
15106 TYPE_STUB_SUPPORTED (type) = 1;
15107
15108 if (die_is_declaration (die, cu))
15109 TYPE_STUB (type) = 1;
15110 else if (attr == NULL && die->child == NULL
15111 && producer_is_realview (cu->producer))
15112 /* RealView does not output the required DW_AT_declaration
15113 on incomplete types. */
15114 TYPE_STUB (type) = 1;
15115
15116 /* We need to add the type field to the die immediately so we don't
15117 infinitely recurse when dealing with pointers to the structure
15118 type within the structure itself. */
15119 set_die_type (die, type, cu);
15120
15121 /* set_die_type should be already done. */
15122 set_descriptive_type (type, die, cu);
15123
15124 return type;
15125 }
15126
15127 /* A helper for process_structure_scope that handles a single member
15128 DIE. */
15129
15130 static void
15131 handle_struct_member_die (struct die_info *child_die, struct type *type,
15132 struct field_info *fi,
15133 std::vector<struct symbol *> *template_args,
15134 struct dwarf2_cu *cu)
15135 {
15136 if (child_die->tag == DW_TAG_member
15137 || child_die->tag == DW_TAG_variable
15138 || child_die->tag == DW_TAG_variant_part)
15139 {
15140 /* NOTE: carlton/2002-11-05: A C++ static data member
15141 should be a DW_TAG_member that is a declaration, but
15142 all versions of G++ as of this writing (so through at
15143 least 3.2.1) incorrectly generate DW_TAG_variable
15144 tags for them instead. */
15145 dwarf2_add_field (fi, child_die, cu);
15146 }
15147 else if (child_die->tag == DW_TAG_subprogram)
15148 {
15149 /* Rust doesn't have member functions in the C++ sense.
15150 However, it does emit ordinary functions as children
15151 of a struct DIE. */
15152 if (cu->language == language_rust)
15153 read_func_scope (child_die, cu);
15154 else
15155 {
15156 /* C++ member function. */
15157 dwarf2_add_member_fn (fi, child_die, type, cu);
15158 }
15159 }
15160 else if (child_die->tag == DW_TAG_inheritance)
15161 {
15162 /* C++ base class field. */
15163 dwarf2_add_field (fi, child_die, cu);
15164 }
15165 else if (type_can_define_types (child_die))
15166 dwarf2_add_type_defn (fi, child_die, cu);
15167 else if (child_die->tag == DW_TAG_template_type_param
15168 || child_die->tag == DW_TAG_template_value_param)
15169 {
15170 struct symbol *arg = new_symbol (child_die, NULL, cu);
15171
15172 if (arg != NULL)
15173 template_args->push_back (arg);
15174 }
15175 else if (child_die->tag == DW_TAG_variant)
15176 {
15177 /* In a variant we want to get the discriminant and also add a
15178 field for our sole member child. */
15179 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
15180
15181 for (die_info *variant_child = child_die->child;
15182 variant_child != NULL;
15183 variant_child = sibling_die (variant_child))
15184 {
15185 if (variant_child->tag == DW_TAG_member)
15186 {
15187 handle_struct_member_die (variant_child, type, fi,
15188 template_args, cu);
15189 /* Only handle the one. */
15190 break;
15191 }
15192 }
15193
15194 /* We don't handle this but we might as well report it if we see
15195 it. */
15196 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
15197 complaint (_("DW_AT_discr_list is not supported yet"
15198 " - DIE at %s [in module %s]"),
15199 sect_offset_str (child_die->sect_off),
15200 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15201
15202 /* The first field was just added, so we can stash the
15203 discriminant there. */
15204 gdb_assert (!fi->fields.empty ());
15205 if (discr == NULL)
15206 fi->fields.back ().variant.default_branch = true;
15207 else
15208 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
15209 }
15210 }
15211
15212 /* Finish creating a structure or union type, including filling in
15213 its members and creating a symbol for it. */
15214
15215 static void
15216 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15217 {
15218 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15219 struct die_info *child_die;
15220 struct type *type;
15221
15222 type = get_die_type (die, cu);
15223 if (type == NULL)
15224 type = read_structure_type (die, cu);
15225
15226 /* When reading a DW_TAG_variant_part, we need to notice when we
15227 read the discriminant member, so we can record it later in the
15228 discriminant_info. */
15229 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
15230 sect_offset discr_offset {};
15231 bool has_template_parameters = false;
15232
15233 if (is_variant_part)
15234 {
15235 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15236 if (discr == NULL)
15237 {
15238 /* Maybe it's a univariant form, an extension we support.
15239 In this case arrange not to check the offset. */
15240 is_variant_part = false;
15241 }
15242 else if (discr->form_is_ref ())
15243 {
15244 struct dwarf2_cu *target_cu = cu;
15245 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15246
15247 discr_offset = target_die->sect_off;
15248 }
15249 else
15250 {
15251 complaint (_("DW_AT_discr does not have DIE reference form"
15252 " - DIE at %s [in module %s]"),
15253 sect_offset_str (die->sect_off),
15254 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15255 is_variant_part = false;
15256 }
15257 }
15258
15259 if (die->child != NULL && ! die_is_declaration (die, cu))
15260 {
15261 struct field_info fi;
15262 std::vector<struct symbol *> template_args;
15263
15264 child_die = die->child;
15265
15266 while (child_die && child_die->tag)
15267 {
15268 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
15269
15270 if (is_variant_part && discr_offset == child_die->sect_off)
15271 fi.fields.back ().variant.is_discriminant = true;
15272
15273 child_die = sibling_die (child_die);
15274 }
15275
15276 /* Attach template arguments to type. */
15277 if (!template_args.empty ())
15278 {
15279 has_template_parameters = true;
15280 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15281 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
15282 TYPE_TEMPLATE_ARGUMENTS (type)
15283 = XOBNEWVEC (&objfile->objfile_obstack,
15284 struct symbol *,
15285 TYPE_N_TEMPLATE_ARGUMENTS (type));
15286 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
15287 template_args.data (),
15288 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15289 * sizeof (struct symbol *)));
15290 }
15291
15292 /* Attach fields and member functions to the type. */
15293 if (fi.nfields)
15294 dwarf2_attach_fields_to_type (&fi, type, cu);
15295 if (!fi.fnfieldlists.empty ())
15296 {
15297 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
15298
15299 /* Get the type which refers to the base class (possibly this
15300 class itself) which contains the vtable pointer for the current
15301 class from the DW_AT_containing_type attribute. This use of
15302 DW_AT_containing_type is a GNU extension. */
15303
15304 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15305 {
15306 struct type *t = die_containing_type (die, cu);
15307
15308 set_type_vptr_basetype (type, t);
15309 if (type == t)
15310 {
15311 int i;
15312
15313 /* Our own class provides vtbl ptr. */
15314 for (i = TYPE_NFIELDS (t) - 1;
15315 i >= TYPE_N_BASECLASSES (t);
15316 --i)
15317 {
15318 const char *fieldname = TYPE_FIELD_NAME (t, i);
15319
15320 if (is_vtable_name (fieldname, cu))
15321 {
15322 set_type_vptr_fieldno (type, i);
15323 break;
15324 }
15325 }
15326
15327 /* Complain if virtual function table field not found. */
15328 if (i < TYPE_N_BASECLASSES (t))
15329 complaint (_("virtual function table pointer "
15330 "not found when defining class '%s'"),
15331 TYPE_NAME (type) ? TYPE_NAME (type) : "");
15332 }
15333 else
15334 {
15335 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
15336 }
15337 }
15338 else if (cu->producer
15339 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
15340 {
15341 /* The IBM XLC compiler does not provide direct indication
15342 of the containing type, but the vtable pointer is
15343 always named __vfp. */
15344
15345 int i;
15346
15347 for (i = TYPE_NFIELDS (type) - 1;
15348 i >= TYPE_N_BASECLASSES (type);
15349 --i)
15350 {
15351 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15352 {
15353 set_type_vptr_fieldno (type, i);
15354 set_type_vptr_basetype (type, type);
15355 break;
15356 }
15357 }
15358 }
15359 }
15360
15361 /* Copy fi.typedef_field_list linked list elements content into the
15362 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
15363 if (!fi.typedef_field_list.empty ())
15364 {
15365 int count = fi.typedef_field_list.size ();
15366
15367 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15368 TYPE_TYPEDEF_FIELD_ARRAY (type)
15369 = ((struct decl_field *)
15370 TYPE_ALLOC (type,
15371 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15372 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
15373
15374 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15375 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
15376 }
15377
15378 /* Copy fi.nested_types_list linked list elements content into the
15379 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
15380 if (!fi.nested_types_list.empty () && cu->language != language_ada)
15381 {
15382 int count = fi.nested_types_list.size ();
15383
15384 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15385 TYPE_NESTED_TYPES_ARRAY (type)
15386 = ((struct decl_field *)
15387 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15388 TYPE_NESTED_TYPES_COUNT (type) = count;
15389
15390 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15391 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
15392 }
15393 }
15394
15395 quirk_gcc_member_function_pointer (type, objfile);
15396 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
15397 cu->rust_unions.push_back (type);
15398
15399 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15400 snapshots) has been known to create a die giving a declaration
15401 for a class that has, as a child, a die giving a definition for a
15402 nested class. So we have to process our children even if the
15403 current die is a declaration. Normally, of course, a declaration
15404 won't have any children at all. */
15405
15406 child_die = die->child;
15407
15408 while (child_die != NULL && child_die->tag)
15409 {
15410 if (child_die->tag == DW_TAG_member
15411 || child_die->tag == DW_TAG_variable
15412 || child_die->tag == DW_TAG_inheritance
15413 || child_die->tag == DW_TAG_template_value_param
15414 || child_die->tag == DW_TAG_template_type_param)
15415 {
15416 /* Do nothing. */
15417 }
15418 else
15419 process_die (child_die, cu);
15420
15421 child_die = sibling_die (child_die);
15422 }
15423
15424 /* Do not consider external references. According to the DWARF standard,
15425 these DIEs are identified by the fact that they have no byte_size
15426 attribute, and a declaration attribute. */
15427 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15428 || !die_is_declaration (die, cu))
15429 {
15430 struct symbol *sym = new_symbol (die, type, cu);
15431
15432 if (has_template_parameters)
15433 {
15434 struct symtab *symtab;
15435 if (sym != nullptr)
15436 symtab = symbol_symtab (sym);
15437 else if (cu->line_header != nullptr)
15438 {
15439 /* Any related symtab will do. */
15440 symtab
15441 = cu->line_header->file_names ()[0].symtab;
15442 }
15443 else
15444 {
15445 symtab = nullptr;
15446 complaint (_("could not find suitable "
15447 "symtab for template parameter"
15448 " - DIE at %s [in module %s]"),
15449 sect_offset_str (die->sect_off),
15450 objfile_name (objfile));
15451 }
15452
15453 if (symtab != nullptr)
15454 {
15455 /* Make sure that the symtab is set on the new symbols.
15456 Even though they don't appear in this symtab directly,
15457 other parts of gdb assume that symbols do, and this is
15458 reasonably true. */
15459 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
15460 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
15461 }
15462 }
15463 }
15464 }
15465
15466 /* Assuming DIE is an enumeration type, and TYPE is its associated type,
15467 update TYPE using some information only available in DIE's children. */
15468
15469 static void
15470 update_enumeration_type_from_children (struct die_info *die,
15471 struct type *type,
15472 struct dwarf2_cu *cu)
15473 {
15474 struct die_info *child_die;
15475 int unsigned_enum = 1;
15476 int flag_enum = 1;
15477
15478 auto_obstack obstack;
15479
15480 for (child_die = die->child;
15481 child_die != NULL && child_die->tag;
15482 child_die = sibling_die (child_die))
15483 {
15484 struct attribute *attr;
15485 LONGEST value;
15486 const gdb_byte *bytes;
15487 struct dwarf2_locexpr_baton *baton;
15488 const char *name;
15489
15490 if (child_die->tag != DW_TAG_enumerator)
15491 continue;
15492
15493 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
15494 if (attr == NULL)
15495 continue;
15496
15497 name = dwarf2_name (child_die, cu);
15498 if (name == NULL)
15499 name = "<anonymous enumerator>";
15500
15501 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
15502 &value, &bytes, &baton);
15503 if (value < 0)
15504 {
15505 unsigned_enum = 0;
15506 flag_enum = 0;
15507 }
15508 else
15509 {
15510 if (count_one_bits_ll (value) >= 2)
15511 flag_enum = 0;
15512 }
15513
15514 /* If we already know that the enum type is neither unsigned, nor
15515 a flag type, no need to look at the rest of the enumerates. */
15516 if (!unsigned_enum && !flag_enum)
15517 break;
15518 }
15519
15520 if (unsigned_enum)
15521 TYPE_UNSIGNED (type) = 1;
15522 if (flag_enum)
15523 TYPE_FLAG_ENUM (type) = 1;
15524 }
15525
15526 /* Given a DW_AT_enumeration_type die, set its type. We do not
15527 complete the type's fields yet, or create any symbols. */
15528
15529 static struct type *
15530 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
15531 {
15532 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15533 struct type *type;
15534 struct attribute *attr;
15535 const char *name;
15536
15537 /* If the definition of this type lives in .debug_types, read that type.
15538 Don't follow DW_AT_specification though, that will take us back up
15539 the chain and we want to go down. */
15540 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
15541 if (attr != nullptr)
15542 {
15543 type = get_DW_AT_signature_type (die, attr, cu);
15544
15545 /* The type's CU may not be the same as CU.
15546 Ensure TYPE is recorded with CU in die_type_hash. */
15547 return set_die_type (die, type, cu);
15548 }
15549
15550 type = alloc_type (objfile);
15551
15552 TYPE_CODE (type) = TYPE_CODE_ENUM;
15553 name = dwarf2_full_name (NULL, die, cu);
15554 if (name != NULL)
15555 TYPE_NAME (type) = name;
15556
15557 attr = dwarf2_attr (die, DW_AT_type, cu);
15558 if (attr != NULL)
15559 {
15560 struct type *underlying_type = die_type (die, cu);
15561
15562 TYPE_TARGET_TYPE (type) = underlying_type;
15563 }
15564
15565 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15566 if (attr != nullptr)
15567 {
15568 TYPE_LENGTH (type) = DW_UNSND (attr);
15569 }
15570 else
15571 {
15572 TYPE_LENGTH (type) = 0;
15573 }
15574
15575 maybe_set_alignment (cu, die, type);
15576
15577 /* The enumeration DIE can be incomplete. In Ada, any type can be
15578 declared as private in the package spec, and then defined only
15579 inside the package body. Such types are known as Taft Amendment
15580 Types. When another package uses such a type, an incomplete DIE
15581 may be generated by the compiler. */
15582 if (die_is_declaration (die, cu))
15583 TYPE_STUB (type) = 1;
15584
15585 /* Finish the creation of this type by using the enum's children.
15586 We must call this even when the underlying type has been provided
15587 so that we can determine if we're looking at a "flag" enum. */
15588 update_enumeration_type_from_children (die, type, cu);
15589
15590 /* If this type has an underlying type that is not a stub, then we
15591 may use its attributes. We always use the "unsigned" attribute
15592 in this situation, because ordinarily we guess whether the type
15593 is unsigned -- but the guess can be wrong and the underlying type
15594 can tell us the reality. However, we defer to a local size
15595 attribute if one exists, because this lets the compiler override
15596 the underlying type if needed. */
15597 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
15598 {
15599 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
15600 if (TYPE_LENGTH (type) == 0)
15601 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
15602 if (TYPE_RAW_ALIGN (type) == 0
15603 && TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)) != 0)
15604 set_type_align (type, TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)));
15605 }
15606
15607 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
15608
15609 return set_die_type (die, type, cu);
15610 }
15611
15612 /* Given a pointer to a die which begins an enumeration, process all
15613 the dies that define the members of the enumeration, and create the
15614 symbol for the enumeration type.
15615
15616 NOTE: We reverse the order of the element list. */
15617
15618 static void
15619 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
15620 {
15621 struct type *this_type;
15622
15623 this_type = get_die_type (die, cu);
15624 if (this_type == NULL)
15625 this_type = read_enumeration_type (die, cu);
15626
15627 if (die->child != NULL)
15628 {
15629 struct die_info *child_die;
15630 struct symbol *sym;
15631 std::vector<struct field> fields;
15632 const char *name;
15633
15634 child_die = die->child;
15635 while (child_die && child_die->tag)
15636 {
15637 if (child_die->tag != DW_TAG_enumerator)
15638 {
15639 process_die (child_die, cu);
15640 }
15641 else
15642 {
15643 name = dwarf2_name (child_die, cu);
15644 if (name)
15645 {
15646 sym = new_symbol (child_die, this_type, cu);
15647
15648 fields.emplace_back ();
15649 struct field &field = fields.back ();
15650
15651 FIELD_NAME (field) = sym->linkage_name ();
15652 FIELD_TYPE (field) = NULL;
15653 SET_FIELD_ENUMVAL (field, SYMBOL_VALUE (sym));
15654 FIELD_BITSIZE (field) = 0;
15655 }
15656 }
15657
15658 child_die = sibling_die (child_die);
15659 }
15660
15661 if (!fields.empty ())
15662 {
15663 TYPE_NFIELDS (this_type) = fields.size ();
15664 TYPE_FIELDS (this_type) = (struct field *)
15665 TYPE_ALLOC (this_type, sizeof (struct field) * fields.size ());
15666 memcpy (TYPE_FIELDS (this_type), fields.data (),
15667 sizeof (struct field) * fields.size ());
15668 }
15669 }
15670
15671 /* If we are reading an enum from a .debug_types unit, and the enum
15672 is a declaration, and the enum is not the signatured type in the
15673 unit, then we do not want to add a symbol for it. Adding a
15674 symbol would in some cases obscure the true definition of the
15675 enum, giving users an incomplete type when the definition is
15676 actually available. Note that we do not want to do this for all
15677 enums which are just declarations, because C++0x allows forward
15678 enum declarations. */
15679 if (cu->per_cu->is_debug_types
15680 && die_is_declaration (die, cu))
15681 {
15682 struct signatured_type *sig_type;
15683
15684 sig_type = (struct signatured_type *) cu->per_cu;
15685 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
15686 if (sig_type->type_offset_in_section != die->sect_off)
15687 return;
15688 }
15689
15690 new_symbol (die, this_type, cu);
15691 }
15692
15693 /* Extract all information from a DW_TAG_array_type DIE and put it in
15694 the DIE's type field. For now, this only handles one dimensional
15695 arrays. */
15696
15697 static struct type *
15698 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
15699 {
15700 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15701 struct die_info *child_die;
15702 struct type *type;
15703 struct type *element_type, *range_type, *index_type;
15704 struct attribute *attr;
15705 const char *name;
15706 struct dynamic_prop *byte_stride_prop = NULL;
15707 unsigned int bit_stride = 0;
15708
15709 element_type = die_type (die, cu);
15710
15711 /* The die_type call above may have already set the type for this DIE. */
15712 type = get_die_type (die, cu);
15713 if (type)
15714 return type;
15715
15716 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
15717 if (attr != NULL)
15718 {
15719 int stride_ok;
15720 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
15721
15722 byte_stride_prop
15723 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
15724 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
15725 prop_type);
15726 if (!stride_ok)
15727 {
15728 complaint (_("unable to read array DW_AT_byte_stride "
15729 " - DIE at %s [in module %s]"),
15730 sect_offset_str (die->sect_off),
15731 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15732 /* Ignore this attribute. We will likely not be able to print
15733 arrays of this type correctly, but there is little we can do
15734 to help if we cannot read the attribute's value. */
15735 byte_stride_prop = NULL;
15736 }
15737 }
15738
15739 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
15740 if (attr != NULL)
15741 bit_stride = DW_UNSND (attr);
15742
15743 /* Irix 6.2 native cc creates array types without children for
15744 arrays with unspecified length. */
15745 if (die->child == NULL)
15746 {
15747 index_type = objfile_type (objfile)->builtin_int;
15748 range_type = create_static_range_type (NULL, index_type, 0, -1);
15749 type = create_array_type_with_stride (NULL, element_type, range_type,
15750 byte_stride_prop, bit_stride);
15751 return set_die_type (die, type, cu);
15752 }
15753
15754 std::vector<struct type *> range_types;
15755 child_die = die->child;
15756 while (child_die && child_die->tag)
15757 {
15758 if (child_die->tag == DW_TAG_subrange_type)
15759 {
15760 struct type *child_type = read_type_die (child_die, cu);
15761
15762 if (child_type != NULL)
15763 {
15764 /* The range type was succesfully read. Save it for the
15765 array type creation. */
15766 range_types.push_back (child_type);
15767 }
15768 }
15769 child_die = sibling_die (child_die);
15770 }
15771
15772 /* Dwarf2 dimensions are output from left to right, create the
15773 necessary array types in backwards order. */
15774
15775 type = element_type;
15776
15777 if (read_array_order (die, cu) == DW_ORD_col_major)
15778 {
15779 int i = 0;
15780
15781 while (i < range_types.size ())
15782 type = create_array_type_with_stride (NULL, type, range_types[i++],
15783 byte_stride_prop, bit_stride);
15784 }
15785 else
15786 {
15787 size_t ndim = range_types.size ();
15788 while (ndim-- > 0)
15789 type = create_array_type_with_stride (NULL, type, range_types[ndim],
15790 byte_stride_prop, bit_stride);
15791 }
15792
15793 /* Understand Dwarf2 support for vector types (like they occur on
15794 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
15795 array type. This is not part of the Dwarf2/3 standard yet, but a
15796 custom vendor extension. The main difference between a regular
15797 array and the vector variant is that vectors are passed by value
15798 to functions. */
15799 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
15800 if (attr != nullptr)
15801 make_vector_type (type);
15802
15803 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
15804 implementation may choose to implement triple vectors using this
15805 attribute. */
15806 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15807 if (attr != nullptr)
15808 {
15809 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
15810 TYPE_LENGTH (type) = DW_UNSND (attr);
15811 else
15812 complaint (_("DW_AT_byte_size for array type smaller "
15813 "than the total size of elements"));
15814 }
15815
15816 name = dwarf2_name (die, cu);
15817 if (name)
15818 TYPE_NAME (type) = name;
15819
15820 maybe_set_alignment (cu, die, type);
15821
15822 /* Install the type in the die. */
15823 set_die_type (die, type, cu);
15824
15825 /* set_die_type should be already done. */
15826 set_descriptive_type (type, die, cu);
15827
15828 return type;
15829 }
15830
15831 static enum dwarf_array_dim_ordering
15832 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
15833 {
15834 struct attribute *attr;
15835
15836 attr = dwarf2_attr (die, DW_AT_ordering, cu);
15837
15838 if (attr != nullptr)
15839 return (enum dwarf_array_dim_ordering) DW_SND (attr);
15840
15841 /* GNU F77 is a special case, as at 08/2004 array type info is the
15842 opposite order to the dwarf2 specification, but data is still
15843 laid out as per normal fortran.
15844
15845 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
15846 version checking. */
15847
15848 if (cu->language == language_fortran
15849 && cu->producer && strstr (cu->producer, "GNU F77"))
15850 {
15851 return DW_ORD_row_major;
15852 }
15853
15854 switch (cu->language_defn->la_array_ordering)
15855 {
15856 case array_column_major:
15857 return DW_ORD_col_major;
15858 case array_row_major:
15859 default:
15860 return DW_ORD_row_major;
15861 };
15862 }
15863
15864 /* Extract all information from a DW_TAG_set_type DIE and put it in
15865 the DIE's type field. */
15866
15867 static struct type *
15868 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
15869 {
15870 struct type *domain_type, *set_type;
15871 struct attribute *attr;
15872
15873 domain_type = die_type (die, cu);
15874
15875 /* The die_type call above may have already set the type for this DIE. */
15876 set_type = get_die_type (die, cu);
15877 if (set_type)
15878 return set_type;
15879
15880 set_type = create_set_type (NULL, domain_type);
15881
15882 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15883 if (attr != nullptr)
15884 TYPE_LENGTH (set_type) = DW_UNSND (attr);
15885
15886 maybe_set_alignment (cu, die, set_type);
15887
15888 return set_die_type (die, set_type, cu);
15889 }
15890
15891 /* A helper for read_common_block that creates a locexpr baton.
15892 SYM is the symbol which we are marking as computed.
15893 COMMON_DIE is the DIE for the common block.
15894 COMMON_LOC is the location expression attribute for the common
15895 block itself.
15896 MEMBER_LOC is the location expression attribute for the particular
15897 member of the common block that we are processing.
15898 CU is the CU from which the above come. */
15899
15900 static void
15901 mark_common_block_symbol_computed (struct symbol *sym,
15902 struct die_info *common_die,
15903 struct attribute *common_loc,
15904 struct attribute *member_loc,
15905 struct dwarf2_cu *cu)
15906 {
15907 struct dwarf2_per_objfile *dwarf2_per_objfile
15908 = cu->per_cu->dwarf2_per_objfile;
15909 struct objfile *objfile = dwarf2_per_objfile->objfile;
15910 struct dwarf2_locexpr_baton *baton;
15911 gdb_byte *ptr;
15912 unsigned int cu_off;
15913 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
15914 LONGEST offset = 0;
15915
15916 gdb_assert (common_loc && member_loc);
15917 gdb_assert (common_loc->form_is_block ());
15918 gdb_assert (member_loc->form_is_block ()
15919 || member_loc->form_is_constant ());
15920
15921 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
15922 baton->per_cu = cu->per_cu;
15923 gdb_assert (baton->per_cu);
15924
15925 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
15926
15927 if (member_loc->form_is_constant ())
15928 {
15929 offset = dwarf2_get_attr_constant_value (member_loc, 0);
15930 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
15931 }
15932 else
15933 baton->size += DW_BLOCK (member_loc)->size;
15934
15935 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
15936 baton->data = ptr;
15937
15938 *ptr++ = DW_OP_call4;
15939 cu_off = common_die->sect_off - cu->per_cu->sect_off;
15940 store_unsigned_integer (ptr, 4, byte_order, cu_off);
15941 ptr += 4;
15942
15943 if (member_loc->form_is_constant ())
15944 {
15945 *ptr++ = DW_OP_addr;
15946 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
15947 ptr += cu->header.addr_size;
15948 }
15949 else
15950 {
15951 /* We have to copy the data here, because DW_OP_call4 will only
15952 use a DW_AT_location attribute. */
15953 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
15954 ptr += DW_BLOCK (member_loc)->size;
15955 }
15956
15957 *ptr++ = DW_OP_plus;
15958 gdb_assert (ptr - baton->data == baton->size);
15959
15960 SYMBOL_LOCATION_BATON (sym) = baton;
15961 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
15962 }
15963
15964 /* Create appropriate locally-scoped variables for all the
15965 DW_TAG_common_block entries. Also create a struct common_block
15966 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
15967 is used to separate the common blocks name namespace from regular
15968 variable names. */
15969
15970 static void
15971 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
15972 {
15973 struct attribute *attr;
15974
15975 attr = dwarf2_attr (die, DW_AT_location, cu);
15976 if (attr != nullptr)
15977 {
15978 /* Support the .debug_loc offsets. */
15979 if (attr->form_is_block ())
15980 {
15981 /* Ok. */
15982 }
15983 else if (attr->form_is_section_offset ())
15984 {
15985 dwarf2_complex_location_expr_complaint ();
15986 attr = NULL;
15987 }
15988 else
15989 {
15990 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
15991 "common block member");
15992 attr = NULL;
15993 }
15994 }
15995
15996 if (die->child != NULL)
15997 {
15998 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15999 struct die_info *child_die;
16000 size_t n_entries = 0, size;
16001 struct common_block *common_block;
16002 struct symbol *sym;
16003
16004 for (child_die = die->child;
16005 child_die && child_die->tag;
16006 child_die = sibling_die (child_die))
16007 ++n_entries;
16008
16009 size = (sizeof (struct common_block)
16010 + (n_entries - 1) * sizeof (struct symbol *));
16011 common_block
16012 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16013 size);
16014 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16015 common_block->n_entries = 0;
16016
16017 for (child_die = die->child;
16018 child_die && child_die->tag;
16019 child_die = sibling_die (child_die))
16020 {
16021 /* Create the symbol in the DW_TAG_common_block block in the current
16022 symbol scope. */
16023 sym = new_symbol (child_die, NULL, cu);
16024 if (sym != NULL)
16025 {
16026 struct attribute *member_loc;
16027
16028 common_block->contents[common_block->n_entries++] = sym;
16029
16030 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16031 cu);
16032 if (member_loc)
16033 {
16034 /* GDB has handled this for a long time, but it is
16035 not specified by DWARF. It seems to have been
16036 emitted by gfortran at least as recently as:
16037 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16038 complaint (_("Variable in common block has "
16039 "DW_AT_data_member_location "
16040 "- DIE at %s [in module %s]"),
16041 sect_offset_str (child_die->sect_off),
16042 objfile_name (objfile));
16043
16044 if (member_loc->form_is_section_offset ())
16045 dwarf2_complex_location_expr_complaint ();
16046 else if (member_loc->form_is_constant ()
16047 || member_loc->form_is_block ())
16048 {
16049 if (attr != nullptr)
16050 mark_common_block_symbol_computed (sym, die, attr,
16051 member_loc, cu);
16052 }
16053 else
16054 dwarf2_complex_location_expr_complaint ();
16055 }
16056 }
16057 }
16058
16059 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16060 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
16061 }
16062 }
16063
16064 /* Create a type for a C++ namespace. */
16065
16066 static struct type *
16067 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16068 {
16069 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16070 const char *previous_prefix, *name;
16071 int is_anonymous;
16072 struct type *type;
16073
16074 /* For extensions, reuse the type of the original namespace. */
16075 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16076 {
16077 struct die_info *ext_die;
16078 struct dwarf2_cu *ext_cu = cu;
16079
16080 ext_die = dwarf2_extension (die, &ext_cu);
16081 type = read_type_die (ext_die, ext_cu);
16082
16083 /* EXT_CU may not be the same as CU.
16084 Ensure TYPE is recorded with CU in die_type_hash. */
16085 return set_die_type (die, type, cu);
16086 }
16087
16088 name = namespace_name (die, &is_anonymous, cu);
16089
16090 /* Now build the name of the current namespace. */
16091
16092 previous_prefix = determine_prefix (die, cu);
16093 if (previous_prefix[0] != '\0')
16094 name = typename_concat (&objfile->objfile_obstack,
16095 previous_prefix, name, 0, cu);
16096
16097 /* Create the type. */
16098 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
16099
16100 return set_die_type (die, type, cu);
16101 }
16102
16103 /* Read a namespace scope. */
16104
16105 static void
16106 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16107 {
16108 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16109 int is_anonymous;
16110
16111 /* Add a symbol associated to this if we haven't seen the namespace
16112 before. Also, add a using directive if it's an anonymous
16113 namespace. */
16114
16115 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
16116 {
16117 struct type *type;
16118
16119 type = read_type_die (die, cu);
16120 new_symbol (die, type, cu);
16121
16122 namespace_name (die, &is_anonymous, cu);
16123 if (is_anonymous)
16124 {
16125 const char *previous_prefix = determine_prefix (die, cu);
16126
16127 std::vector<const char *> excludes;
16128 add_using_directive (using_directives (cu),
16129 previous_prefix, TYPE_NAME (type), NULL,
16130 NULL, excludes, 0, &objfile->objfile_obstack);
16131 }
16132 }
16133
16134 if (die->child != NULL)
16135 {
16136 struct die_info *child_die = die->child;
16137
16138 while (child_die && child_die->tag)
16139 {
16140 process_die (child_die, cu);
16141 child_die = sibling_die (child_die);
16142 }
16143 }
16144 }
16145
16146 /* Read a Fortran module as type. This DIE can be only a declaration used for
16147 imported module. Still we need that type as local Fortran "use ... only"
16148 declaration imports depend on the created type in determine_prefix. */
16149
16150 static struct type *
16151 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16152 {
16153 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16154 const char *module_name;
16155 struct type *type;
16156
16157 module_name = dwarf2_name (die, cu);
16158 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
16159
16160 return set_die_type (die, type, cu);
16161 }
16162
16163 /* Read a Fortran module. */
16164
16165 static void
16166 read_module (struct die_info *die, struct dwarf2_cu *cu)
16167 {
16168 struct die_info *child_die = die->child;
16169 struct type *type;
16170
16171 type = read_type_die (die, cu);
16172 new_symbol (die, type, cu);
16173
16174 while (child_die && child_die->tag)
16175 {
16176 process_die (child_die, cu);
16177 child_die = sibling_die (child_die);
16178 }
16179 }
16180
16181 /* Return the name of the namespace represented by DIE. Set
16182 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16183 namespace. */
16184
16185 static const char *
16186 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
16187 {
16188 struct die_info *current_die;
16189 const char *name = NULL;
16190
16191 /* Loop through the extensions until we find a name. */
16192
16193 for (current_die = die;
16194 current_die != NULL;
16195 current_die = dwarf2_extension (die, &cu))
16196 {
16197 /* We don't use dwarf2_name here so that we can detect the absence
16198 of a name -> anonymous namespace. */
16199 name = dwarf2_string_attr (die, DW_AT_name, cu);
16200
16201 if (name != NULL)
16202 break;
16203 }
16204
16205 /* Is it an anonymous namespace? */
16206
16207 *is_anonymous = (name == NULL);
16208 if (*is_anonymous)
16209 name = CP_ANONYMOUS_NAMESPACE_STR;
16210
16211 return name;
16212 }
16213
16214 /* Extract all information from a DW_TAG_pointer_type DIE and add to
16215 the user defined type vector. */
16216
16217 static struct type *
16218 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
16219 {
16220 struct gdbarch *gdbarch
16221 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
16222 struct comp_unit_head *cu_header = &cu->header;
16223 struct type *type;
16224 struct attribute *attr_byte_size;
16225 struct attribute *attr_address_class;
16226 int byte_size, addr_class;
16227 struct type *target_type;
16228
16229 target_type = die_type (die, cu);
16230
16231 /* The die_type call above may have already set the type for this DIE. */
16232 type = get_die_type (die, cu);
16233 if (type)
16234 return type;
16235
16236 type = lookup_pointer_type (target_type);
16237
16238 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
16239 if (attr_byte_size)
16240 byte_size = DW_UNSND (attr_byte_size);
16241 else
16242 byte_size = cu_header->addr_size;
16243
16244 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
16245 if (attr_address_class)
16246 addr_class = DW_UNSND (attr_address_class);
16247 else
16248 addr_class = DW_ADDR_none;
16249
16250 ULONGEST alignment = get_alignment (cu, die);
16251
16252 /* If the pointer size, alignment, or address class is different
16253 than the default, create a type variant marked as such and set
16254 the length accordingly. */
16255 if (TYPE_LENGTH (type) != byte_size
16256 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16257 && alignment != TYPE_RAW_ALIGN (type))
16258 || addr_class != DW_ADDR_none)
16259 {
16260 if (gdbarch_address_class_type_flags_p (gdbarch))
16261 {
16262 int type_flags;
16263
16264 type_flags = gdbarch_address_class_type_flags
16265 (gdbarch, byte_size, addr_class);
16266 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16267 == 0);
16268 type = make_type_with_address_space (type, type_flags);
16269 }
16270 else if (TYPE_LENGTH (type) != byte_size)
16271 {
16272 complaint (_("invalid pointer size %d"), byte_size);
16273 }
16274 else if (TYPE_RAW_ALIGN (type) != alignment)
16275 {
16276 complaint (_("Invalid DW_AT_alignment"
16277 " - DIE at %s [in module %s]"),
16278 sect_offset_str (die->sect_off),
16279 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16280 }
16281 else
16282 {
16283 /* Should we also complain about unhandled address classes? */
16284 }
16285 }
16286
16287 TYPE_LENGTH (type) = byte_size;
16288 set_type_align (type, alignment);
16289 return set_die_type (die, type, cu);
16290 }
16291
16292 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16293 the user defined type vector. */
16294
16295 static struct type *
16296 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
16297 {
16298 struct type *type;
16299 struct type *to_type;
16300 struct type *domain;
16301
16302 to_type = die_type (die, cu);
16303 domain = die_containing_type (die, cu);
16304
16305 /* The calls above may have already set the type for this DIE. */
16306 type = get_die_type (die, cu);
16307 if (type)
16308 return type;
16309
16310 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
16311 type = lookup_methodptr_type (to_type);
16312 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
16313 {
16314 struct type *new_type
16315 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
16316
16317 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
16318 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
16319 TYPE_VARARGS (to_type));
16320 type = lookup_methodptr_type (new_type);
16321 }
16322 else
16323 type = lookup_memberptr_type (to_type, domain);
16324
16325 return set_die_type (die, type, cu);
16326 }
16327
16328 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
16329 the user defined type vector. */
16330
16331 static struct type *
16332 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16333 enum type_code refcode)
16334 {
16335 struct comp_unit_head *cu_header = &cu->header;
16336 struct type *type, *target_type;
16337 struct attribute *attr;
16338
16339 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16340
16341 target_type = die_type (die, cu);
16342
16343 /* The die_type call above may have already set the type for this DIE. */
16344 type = get_die_type (die, cu);
16345 if (type)
16346 return type;
16347
16348 type = lookup_reference_type (target_type, refcode);
16349 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16350 if (attr != nullptr)
16351 {
16352 TYPE_LENGTH (type) = DW_UNSND (attr);
16353 }
16354 else
16355 {
16356 TYPE_LENGTH (type) = cu_header->addr_size;
16357 }
16358 maybe_set_alignment (cu, die, type);
16359 return set_die_type (die, type, cu);
16360 }
16361
16362 /* Add the given cv-qualifiers to the element type of the array. GCC
16363 outputs DWARF type qualifiers that apply to an array, not the
16364 element type. But GDB relies on the array element type to carry
16365 the cv-qualifiers. This mimics section 6.7.3 of the C99
16366 specification. */
16367
16368 static struct type *
16369 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16370 struct type *base_type, int cnst, int voltl)
16371 {
16372 struct type *el_type, *inner_array;
16373
16374 base_type = copy_type (base_type);
16375 inner_array = base_type;
16376
16377 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
16378 {
16379 TYPE_TARGET_TYPE (inner_array) =
16380 copy_type (TYPE_TARGET_TYPE (inner_array));
16381 inner_array = TYPE_TARGET_TYPE (inner_array);
16382 }
16383
16384 el_type = TYPE_TARGET_TYPE (inner_array);
16385 cnst |= TYPE_CONST (el_type);
16386 voltl |= TYPE_VOLATILE (el_type);
16387 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16388
16389 return set_die_type (die, base_type, cu);
16390 }
16391
16392 static struct type *
16393 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
16394 {
16395 struct type *base_type, *cv_type;
16396
16397 base_type = die_type (die, cu);
16398
16399 /* The die_type call above may have already set the type for this DIE. */
16400 cv_type = get_die_type (die, cu);
16401 if (cv_type)
16402 return cv_type;
16403
16404 /* In case the const qualifier is applied to an array type, the element type
16405 is so qualified, not the array type (section 6.7.3 of C99). */
16406 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
16407 return add_array_cv_type (die, cu, base_type, 1, 0);
16408
16409 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16410 return set_die_type (die, cv_type, cu);
16411 }
16412
16413 static struct type *
16414 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
16415 {
16416 struct type *base_type, *cv_type;
16417
16418 base_type = die_type (die, cu);
16419
16420 /* The die_type call above may have already set the type for this DIE. */
16421 cv_type = get_die_type (die, cu);
16422 if (cv_type)
16423 return cv_type;
16424
16425 /* In case the volatile qualifier is applied to an array type, the
16426 element type is so qualified, not the array type (section 6.7.3
16427 of C99). */
16428 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
16429 return add_array_cv_type (die, cu, base_type, 0, 1);
16430
16431 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
16432 return set_die_type (die, cv_type, cu);
16433 }
16434
16435 /* Handle DW_TAG_restrict_type. */
16436
16437 static struct type *
16438 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
16439 {
16440 struct type *base_type, *cv_type;
16441
16442 base_type = die_type (die, cu);
16443
16444 /* The die_type call above may have already set the type for this DIE. */
16445 cv_type = get_die_type (die, cu);
16446 if (cv_type)
16447 return cv_type;
16448
16449 cv_type = make_restrict_type (base_type);
16450 return set_die_type (die, cv_type, cu);
16451 }
16452
16453 /* Handle DW_TAG_atomic_type. */
16454
16455 static struct type *
16456 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
16457 {
16458 struct type *base_type, *cv_type;
16459
16460 base_type = die_type (die, cu);
16461
16462 /* The die_type call above may have already set the type for this DIE. */
16463 cv_type = get_die_type (die, cu);
16464 if (cv_type)
16465 return cv_type;
16466
16467 cv_type = make_atomic_type (base_type);
16468 return set_die_type (die, cv_type, cu);
16469 }
16470
16471 /* Extract all information from a DW_TAG_string_type DIE and add to
16472 the user defined type vector. It isn't really a user defined type,
16473 but it behaves like one, with other DIE's using an AT_user_def_type
16474 attribute to reference it. */
16475
16476 static struct type *
16477 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
16478 {
16479 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16480 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16481 struct type *type, *range_type, *index_type, *char_type;
16482 struct attribute *attr;
16483 struct dynamic_prop prop;
16484 bool length_is_constant = true;
16485 LONGEST length;
16486
16487 /* There are a couple of places where bit sizes might be made use of
16488 when parsing a DW_TAG_string_type, however, no producer that we know
16489 of make use of these. Handling bit sizes that are a multiple of the
16490 byte size is easy enough, but what about other bit sizes? Lets deal
16491 with that problem when we have to. Warn about these attributes being
16492 unsupported, then parse the type and ignore them like we always
16493 have. */
16494 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
16495 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
16496 {
16497 static bool warning_printed = false;
16498 if (!warning_printed)
16499 {
16500 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
16501 "currently supported on DW_TAG_string_type."));
16502 warning_printed = true;
16503 }
16504 }
16505
16506 attr = dwarf2_attr (die, DW_AT_string_length, cu);
16507 if (attr != nullptr && !attr->form_is_constant ())
16508 {
16509 /* The string length describes the location at which the length of
16510 the string can be found. The size of the length field can be
16511 specified with one of the attributes below. */
16512 struct type *prop_type;
16513 struct attribute *len
16514 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
16515 if (len == nullptr)
16516 len = dwarf2_attr (die, DW_AT_byte_size, cu);
16517 if (len != nullptr && len->form_is_constant ())
16518 {
16519 /* Pass 0 as the default as we know this attribute is constant
16520 and the default value will not be returned. */
16521 LONGEST sz = dwarf2_get_attr_constant_value (len, 0);
16522 prop_type = cu->per_cu->int_type (sz, true);
16523 }
16524 else
16525 {
16526 /* If the size is not specified then we assume it is the size of
16527 an address on this target. */
16528 prop_type = cu->per_cu->addr_sized_int_type (true);
16529 }
16530
16531 /* Convert the attribute into a dynamic property. */
16532 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
16533 length = 1;
16534 else
16535 length_is_constant = false;
16536 }
16537 else if (attr != nullptr)
16538 {
16539 /* This DW_AT_string_length just contains the length with no
16540 indirection. There's no need to create a dynamic property in this
16541 case. Pass 0 for the default value as we know it will not be
16542 returned in this case. */
16543 length = dwarf2_get_attr_constant_value (attr, 0);
16544 }
16545 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
16546 {
16547 /* We don't currently support non-constant byte sizes for strings. */
16548 length = dwarf2_get_attr_constant_value (attr, 1);
16549 }
16550 else
16551 {
16552 /* Use 1 as a fallback length if we have nothing else. */
16553 length = 1;
16554 }
16555
16556 index_type = objfile_type (objfile)->builtin_int;
16557 if (length_is_constant)
16558 range_type = create_static_range_type (NULL, index_type, 1, length);
16559 else
16560 {
16561 struct dynamic_prop low_bound;
16562
16563 low_bound.kind = PROP_CONST;
16564 low_bound.data.const_val = 1;
16565 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
16566 }
16567 char_type = language_string_char_type (cu->language_defn, gdbarch);
16568 type = create_string_type (NULL, char_type, range_type);
16569
16570 return set_die_type (die, type, cu);
16571 }
16572
16573 /* Assuming that DIE corresponds to a function, returns nonzero
16574 if the function is prototyped. */
16575
16576 static int
16577 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
16578 {
16579 struct attribute *attr;
16580
16581 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
16582 if (attr && (DW_UNSND (attr) != 0))
16583 return 1;
16584
16585 /* The DWARF standard implies that the DW_AT_prototyped attribute
16586 is only meaningful for C, but the concept also extends to other
16587 languages that allow unprototyped functions (Eg: Objective C).
16588 For all other languages, assume that functions are always
16589 prototyped. */
16590 if (cu->language != language_c
16591 && cu->language != language_objc
16592 && cu->language != language_opencl)
16593 return 1;
16594
16595 /* RealView does not emit DW_AT_prototyped. We can not distinguish
16596 prototyped and unprototyped functions; default to prototyped,
16597 since that is more common in modern code (and RealView warns
16598 about unprototyped functions). */
16599 if (producer_is_realview (cu->producer))
16600 return 1;
16601
16602 return 0;
16603 }
16604
16605 /* Handle DIES due to C code like:
16606
16607 struct foo
16608 {
16609 int (*funcp)(int a, long l);
16610 int b;
16611 };
16612
16613 ('funcp' generates a DW_TAG_subroutine_type DIE). */
16614
16615 static struct type *
16616 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
16617 {
16618 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16619 struct type *type; /* Type that this function returns. */
16620 struct type *ftype; /* Function that returns above type. */
16621 struct attribute *attr;
16622
16623 type = die_type (die, cu);
16624
16625 /* The die_type call above may have already set the type for this DIE. */
16626 ftype = get_die_type (die, cu);
16627 if (ftype)
16628 return ftype;
16629
16630 ftype = lookup_function_type (type);
16631
16632 if (prototyped_function_p (die, cu))
16633 TYPE_PROTOTYPED (ftype) = 1;
16634
16635 /* Store the calling convention in the type if it's available in
16636 the subroutine die. Otherwise set the calling convention to
16637 the default value DW_CC_normal. */
16638 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
16639 if (attr != nullptr
16640 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
16641 TYPE_CALLING_CONVENTION (ftype)
16642 = (enum dwarf_calling_convention) (DW_UNSND (attr));
16643 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
16644 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
16645 else
16646 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
16647
16648 /* Record whether the function returns normally to its caller or not
16649 if the DWARF producer set that information. */
16650 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
16651 if (attr && (DW_UNSND (attr) != 0))
16652 TYPE_NO_RETURN (ftype) = 1;
16653
16654 /* We need to add the subroutine type to the die immediately so
16655 we don't infinitely recurse when dealing with parameters
16656 declared as the same subroutine type. */
16657 set_die_type (die, ftype, cu);
16658
16659 if (die->child != NULL)
16660 {
16661 struct type *void_type = objfile_type (objfile)->builtin_void;
16662 struct die_info *child_die;
16663 int nparams, iparams;
16664
16665 /* Count the number of parameters.
16666 FIXME: GDB currently ignores vararg functions, but knows about
16667 vararg member functions. */
16668 nparams = 0;
16669 child_die = die->child;
16670 while (child_die && child_die->tag)
16671 {
16672 if (child_die->tag == DW_TAG_formal_parameter)
16673 nparams++;
16674 else if (child_die->tag == DW_TAG_unspecified_parameters)
16675 TYPE_VARARGS (ftype) = 1;
16676 child_die = sibling_die (child_die);
16677 }
16678
16679 /* Allocate storage for parameters and fill them in. */
16680 TYPE_NFIELDS (ftype) = nparams;
16681 TYPE_FIELDS (ftype) = (struct field *)
16682 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
16683
16684 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
16685 even if we error out during the parameters reading below. */
16686 for (iparams = 0; iparams < nparams; iparams++)
16687 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
16688
16689 iparams = 0;
16690 child_die = die->child;
16691 while (child_die && child_die->tag)
16692 {
16693 if (child_die->tag == DW_TAG_formal_parameter)
16694 {
16695 struct type *arg_type;
16696
16697 /* DWARF version 2 has no clean way to discern C++
16698 static and non-static member functions. G++ helps
16699 GDB by marking the first parameter for non-static
16700 member functions (which is the this pointer) as
16701 artificial. We pass this information to
16702 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
16703
16704 DWARF version 3 added DW_AT_object_pointer, which GCC
16705 4.5 does not yet generate. */
16706 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
16707 if (attr != nullptr)
16708 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
16709 else
16710 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
16711 arg_type = die_type (child_die, cu);
16712
16713 /* RealView does not mark THIS as const, which the testsuite
16714 expects. GCC marks THIS as const in method definitions,
16715 but not in the class specifications (GCC PR 43053). */
16716 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
16717 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
16718 {
16719 int is_this = 0;
16720 struct dwarf2_cu *arg_cu = cu;
16721 const char *name = dwarf2_name (child_die, cu);
16722
16723 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
16724 if (attr != nullptr)
16725 {
16726 /* If the compiler emits this, use it. */
16727 if (follow_die_ref (die, attr, &arg_cu) == child_die)
16728 is_this = 1;
16729 }
16730 else if (name && strcmp (name, "this") == 0)
16731 /* Function definitions will have the argument names. */
16732 is_this = 1;
16733 else if (name == NULL && iparams == 0)
16734 /* Declarations may not have the names, so like
16735 elsewhere in GDB, assume an artificial first
16736 argument is "this". */
16737 is_this = 1;
16738
16739 if (is_this)
16740 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
16741 arg_type, 0);
16742 }
16743
16744 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
16745 iparams++;
16746 }
16747 child_die = sibling_die (child_die);
16748 }
16749 }
16750
16751 return ftype;
16752 }
16753
16754 static struct type *
16755 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
16756 {
16757 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16758 const char *name = NULL;
16759 struct type *this_type, *target_type;
16760
16761 name = dwarf2_full_name (NULL, die, cu);
16762 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
16763 TYPE_TARGET_STUB (this_type) = 1;
16764 set_die_type (die, this_type, cu);
16765 target_type = die_type (die, cu);
16766 if (target_type != this_type)
16767 TYPE_TARGET_TYPE (this_type) = target_type;
16768 else
16769 {
16770 /* Self-referential typedefs are, it seems, not allowed by the DWARF
16771 spec and cause infinite loops in GDB. */
16772 complaint (_("Self-referential DW_TAG_typedef "
16773 "- DIE at %s [in module %s]"),
16774 sect_offset_str (die->sect_off), objfile_name (objfile));
16775 TYPE_TARGET_TYPE (this_type) = NULL;
16776 }
16777 return this_type;
16778 }
16779
16780 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
16781 (which may be different from NAME) to the architecture back-end to allow
16782 it to guess the correct format if necessary. */
16783
16784 static struct type *
16785 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
16786 const char *name_hint, enum bfd_endian byte_order)
16787 {
16788 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16789 const struct floatformat **format;
16790 struct type *type;
16791
16792 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
16793 if (format)
16794 type = init_float_type (objfile, bits, name, format, byte_order);
16795 else
16796 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
16797
16798 return type;
16799 }
16800
16801 /* Allocate an integer type of size BITS and name NAME. */
16802
16803 static struct type *
16804 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
16805 int bits, int unsigned_p, const char *name)
16806 {
16807 struct type *type;
16808
16809 /* Versions of Intel's C Compiler generate an integer type called "void"
16810 instead of using DW_TAG_unspecified_type. This has been seen on
16811 at least versions 14, 17, and 18. */
16812 if (bits == 0 && producer_is_icc (cu) && name != nullptr
16813 && strcmp (name, "void") == 0)
16814 type = objfile_type (objfile)->builtin_void;
16815 else
16816 type = init_integer_type (objfile, bits, unsigned_p, name);
16817
16818 return type;
16819 }
16820
16821 /* Initialise and return a floating point type of size BITS suitable for
16822 use as a component of a complex number. The NAME_HINT is passed through
16823 when initialising the floating point type and is the name of the complex
16824 type.
16825
16826 As DWARF doesn't currently provide an explicit name for the components
16827 of a complex number, but it can be helpful to have these components
16828 named, we try to select a suitable name based on the size of the
16829 component. */
16830 static struct type *
16831 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
16832 struct objfile *objfile,
16833 int bits, const char *name_hint,
16834 enum bfd_endian byte_order)
16835 {
16836 gdbarch *gdbarch = get_objfile_arch (objfile);
16837 struct type *tt = nullptr;
16838
16839 /* Try to find a suitable floating point builtin type of size BITS.
16840 We're going to use the name of this type as the name for the complex
16841 target type that we are about to create. */
16842 switch (cu->language)
16843 {
16844 case language_fortran:
16845 switch (bits)
16846 {
16847 case 32:
16848 tt = builtin_f_type (gdbarch)->builtin_real;
16849 break;
16850 case 64:
16851 tt = builtin_f_type (gdbarch)->builtin_real_s8;
16852 break;
16853 case 96: /* The x86-32 ABI specifies 96-bit long double. */
16854 case 128:
16855 tt = builtin_f_type (gdbarch)->builtin_real_s16;
16856 break;
16857 }
16858 break;
16859 default:
16860 switch (bits)
16861 {
16862 case 32:
16863 tt = builtin_type (gdbarch)->builtin_float;
16864 break;
16865 case 64:
16866 tt = builtin_type (gdbarch)->builtin_double;
16867 break;
16868 case 96: /* The x86-32 ABI specifies 96-bit long double. */
16869 case 128:
16870 tt = builtin_type (gdbarch)->builtin_long_double;
16871 break;
16872 }
16873 break;
16874 }
16875
16876 /* If the type we found doesn't match the size we were looking for, then
16877 pretend we didn't find a type at all, the complex target type we
16878 create will then be nameless. */
16879 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
16880 tt = nullptr;
16881
16882 const char *name = (tt == nullptr) ? nullptr : TYPE_NAME (tt);
16883 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
16884 }
16885
16886 /* Find a representation of a given base type and install
16887 it in the TYPE field of the die. */
16888
16889 static struct type *
16890 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
16891 {
16892 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16893 struct type *type;
16894 struct attribute *attr;
16895 int encoding = 0, bits = 0;
16896 const char *name;
16897 gdbarch *arch;
16898
16899 attr = dwarf2_attr (die, DW_AT_encoding, cu);
16900 if (attr != nullptr)
16901 encoding = DW_UNSND (attr);
16902 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16903 if (attr != nullptr)
16904 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
16905 name = dwarf2_name (die, cu);
16906 if (!name)
16907 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
16908
16909 arch = get_objfile_arch (objfile);
16910 enum bfd_endian byte_order = gdbarch_byte_order (arch);
16911
16912 attr = dwarf2_attr (die, DW_AT_endianity, cu);
16913 if (attr)
16914 {
16915 int endianity = DW_UNSND (attr);
16916
16917 switch (endianity)
16918 {
16919 case DW_END_big:
16920 byte_order = BFD_ENDIAN_BIG;
16921 break;
16922 case DW_END_little:
16923 byte_order = BFD_ENDIAN_LITTLE;
16924 break;
16925 default:
16926 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
16927 break;
16928 }
16929 }
16930
16931 switch (encoding)
16932 {
16933 case DW_ATE_address:
16934 /* Turn DW_ATE_address into a void * pointer. */
16935 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
16936 type = init_pointer_type (objfile, bits, name, type);
16937 break;
16938 case DW_ATE_boolean:
16939 type = init_boolean_type (objfile, bits, 1, name);
16940 break;
16941 case DW_ATE_complex_float:
16942 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
16943 byte_order);
16944 type = init_complex_type (objfile, name, type);
16945 break;
16946 case DW_ATE_decimal_float:
16947 type = init_decfloat_type (objfile, bits, name);
16948 break;
16949 case DW_ATE_float:
16950 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
16951 break;
16952 case DW_ATE_signed:
16953 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
16954 break;
16955 case DW_ATE_unsigned:
16956 if (cu->language == language_fortran
16957 && name
16958 && startswith (name, "character("))
16959 type = init_character_type (objfile, bits, 1, name);
16960 else
16961 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
16962 break;
16963 case DW_ATE_signed_char:
16964 if (cu->language == language_ada || cu->language == language_m2
16965 || cu->language == language_pascal
16966 || cu->language == language_fortran)
16967 type = init_character_type (objfile, bits, 0, name);
16968 else
16969 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
16970 break;
16971 case DW_ATE_unsigned_char:
16972 if (cu->language == language_ada || cu->language == language_m2
16973 || cu->language == language_pascal
16974 || cu->language == language_fortran
16975 || cu->language == language_rust)
16976 type = init_character_type (objfile, bits, 1, name);
16977 else
16978 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
16979 break;
16980 case DW_ATE_UTF:
16981 {
16982 if (bits == 16)
16983 type = builtin_type (arch)->builtin_char16;
16984 else if (bits == 32)
16985 type = builtin_type (arch)->builtin_char32;
16986 else
16987 {
16988 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
16989 bits);
16990 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
16991 }
16992 return set_die_type (die, type, cu);
16993 }
16994 break;
16995
16996 default:
16997 complaint (_("unsupported DW_AT_encoding: '%s'"),
16998 dwarf_type_encoding_name (encoding));
16999 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17000 break;
17001 }
17002
17003 if (name && strcmp (name, "char") == 0)
17004 TYPE_NOSIGN (type) = 1;
17005
17006 maybe_set_alignment (cu, die, type);
17007
17008 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
17009
17010 return set_die_type (die, type, cu);
17011 }
17012
17013 /* Parse dwarf attribute if it's a block, reference or constant and put the
17014 resulting value of the attribute into struct bound_prop.
17015 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17016
17017 static int
17018 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17019 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17020 struct type *default_type)
17021 {
17022 struct dwarf2_property_baton *baton;
17023 struct obstack *obstack
17024 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
17025
17026 gdb_assert (default_type != NULL);
17027
17028 if (attr == NULL || prop == NULL)
17029 return 0;
17030
17031 if (attr->form_is_block ())
17032 {
17033 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17034 baton->property_type = default_type;
17035 baton->locexpr.per_cu = cu->per_cu;
17036 baton->locexpr.size = DW_BLOCK (attr)->size;
17037 baton->locexpr.data = DW_BLOCK (attr)->data;
17038 switch (attr->name)
17039 {
17040 case DW_AT_string_length:
17041 baton->locexpr.is_reference = true;
17042 break;
17043 default:
17044 baton->locexpr.is_reference = false;
17045 break;
17046 }
17047 prop->data.baton = baton;
17048 prop->kind = PROP_LOCEXPR;
17049 gdb_assert (prop->data.baton != NULL);
17050 }
17051 else if (attr->form_is_ref ())
17052 {
17053 struct dwarf2_cu *target_cu = cu;
17054 struct die_info *target_die;
17055 struct attribute *target_attr;
17056
17057 target_die = follow_die_ref (die, attr, &target_cu);
17058 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17059 if (target_attr == NULL)
17060 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17061 target_cu);
17062 if (target_attr == NULL)
17063 return 0;
17064
17065 switch (target_attr->name)
17066 {
17067 case DW_AT_location:
17068 if (target_attr->form_is_section_offset ())
17069 {
17070 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17071 baton->property_type = die_type (target_die, target_cu);
17072 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17073 prop->data.baton = baton;
17074 prop->kind = PROP_LOCLIST;
17075 gdb_assert (prop->data.baton != NULL);
17076 }
17077 else if (target_attr->form_is_block ())
17078 {
17079 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17080 baton->property_type = die_type (target_die, target_cu);
17081 baton->locexpr.per_cu = cu->per_cu;
17082 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17083 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17084 baton->locexpr.is_reference = true;
17085 prop->data.baton = baton;
17086 prop->kind = PROP_LOCEXPR;
17087 gdb_assert (prop->data.baton != NULL);
17088 }
17089 else
17090 {
17091 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17092 "dynamic property");
17093 return 0;
17094 }
17095 break;
17096 case DW_AT_data_member_location:
17097 {
17098 LONGEST offset;
17099
17100 if (!handle_data_member_location (target_die, target_cu,
17101 &offset))
17102 return 0;
17103
17104 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17105 baton->property_type = read_type_die (target_die->parent,
17106 target_cu);
17107 baton->offset_info.offset = offset;
17108 baton->offset_info.type = die_type (target_die, target_cu);
17109 prop->data.baton = baton;
17110 prop->kind = PROP_ADDR_OFFSET;
17111 break;
17112 }
17113 }
17114 }
17115 else if (attr->form_is_constant ())
17116 {
17117 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
17118 prop->kind = PROP_CONST;
17119 }
17120 else
17121 {
17122 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17123 dwarf2_name (die, cu));
17124 return 0;
17125 }
17126
17127 return 1;
17128 }
17129
17130 /* See read.h. */
17131
17132 struct type *
17133 dwarf2_per_cu_data::int_type (int size_in_bytes, bool unsigned_p) const
17134 {
17135 struct objfile *objfile = dwarf2_per_objfile->objfile;
17136 struct type *int_type;
17137
17138 /* Helper macro to examine the various builtin types. */
17139 #define TRY_TYPE(F) \
17140 int_type = (unsigned_p \
17141 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17142 : objfile_type (objfile)->builtin_ ## F); \
17143 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
17144 return int_type
17145
17146 TRY_TYPE (char);
17147 TRY_TYPE (short);
17148 TRY_TYPE (int);
17149 TRY_TYPE (long);
17150 TRY_TYPE (long_long);
17151
17152 #undef TRY_TYPE
17153
17154 gdb_assert_not_reached ("unable to find suitable integer type");
17155 }
17156
17157 /* See read.h. */
17158
17159 struct type *
17160 dwarf2_per_cu_data::addr_sized_int_type (bool unsigned_p) const
17161 {
17162 int addr_size = this->addr_size ();
17163 return int_type (addr_size, unsigned_p);
17164 }
17165
17166 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17167 present (which is valid) then compute the default type based on the
17168 compilation units address size. */
17169
17170 static struct type *
17171 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17172 {
17173 struct type *index_type = die_type (die, cu);
17174
17175 /* Dwarf-2 specifications explicitly allows to create subrange types
17176 without specifying a base type.
17177 In that case, the base type must be set to the type of
17178 the lower bound, upper bound or count, in that order, if any of these
17179 three attributes references an object that has a type.
17180 If no base type is found, the Dwarf-2 specifications say that
17181 a signed integer type of size equal to the size of an address should
17182 be used.
17183 For the following C code: `extern char gdb_int [];'
17184 GCC produces an empty range DIE.
17185 FIXME: muller/2010-05-28: Possible references to object for low bound,
17186 high bound or count are not yet handled by this code. */
17187 if (TYPE_CODE (index_type) == TYPE_CODE_VOID)
17188 index_type = cu->per_cu->addr_sized_int_type (false);
17189
17190 return index_type;
17191 }
17192
17193 /* Read the given DW_AT_subrange DIE. */
17194
17195 static struct type *
17196 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17197 {
17198 struct type *base_type, *orig_base_type;
17199 struct type *range_type;
17200 struct attribute *attr;
17201 struct dynamic_prop low, high;
17202 int low_default_is_valid;
17203 int high_bound_is_count = 0;
17204 const char *name;
17205 ULONGEST negative_mask;
17206
17207 orig_base_type = read_subrange_index_type (die, cu);
17208
17209 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17210 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17211 creating the range type, but we use the result of check_typedef
17212 when examining properties of the type. */
17213 base_type = check_typedef (orig_base_type);
17214
17215 /* The die_type call above may have already set the type for this DIE. */
17216 range_type = get_die_type (die, cu);
17217 if (range_type)
17218 return range_type;
17219
17220 low.kind = PROP_CONST;
17221 high.kind = PROP_CONST;
17222 high.data.const_val = 0;
17223
17224 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17225 omitting DW_AT_lower_bound. */
17226 switch (cu->language)
17227 {
17228 case language_c:
17229 case language_cplus:
17230 low.data.const_val = 0;
17231 low_default_is_valid = 1;
17232 break;
17233 case language_fortran:
17234 low.data.const_val = 1;
17235 low_default_is_valid = 1;
17236 break;
17237 case language_d:
17238 case language_objc:
17239 case language_rust:
17240 low.data.const_val = 0;
17241 low_default_is_valid = (cu->header.version >= 4);
17242 break;
17243 case language_ada:
17244 case language_m2:
17245 case language_pascal:
17246 low.data.const_val = 1;
17247 low_default_is_valid = (cu->header.version >= 4);
17248 break;
17249 default:
17250 low.data.const_val = 0;
17251 low_default_is_valid = 0;
17252 break;
17253 }
17254
17255 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
17256 if (attr != nullptr)
17257 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
17258 else if (!low_default_is_valid)
17259 complaint (_("Missing DW_AT_lower_bound "
17260 "- DIE at %s [in module %s]"),
17261 sect_offset_str (die->sect_off),
17262 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17263
17264 struct attribute *attr_ub, *attr_count;
17265 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
17266 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17267 {
17268 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
17269 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17270 {
17271 /* If bounds are constant do the final calculation here. */
17272 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17273 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17274 else
17275 high_bound_is_count = 1;
17276 }
17277 else
17278 {
17279 if (attr_ub != NULL)
17280 complaint (_("Unresolved DW_AT_upper_bound "
17281 "- DIE at %s [in module %s]"),
17282 sect_offset_str (die->sect_off),
17283 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17284 if (attr_count != NULL)
17285 complaint (_("Unresolved DW_AT_count "
17286 "- DIE at %s [in module %s]"),
17287 sect_offset_str (die->sect_off),
17288 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17289 }
17290 }
17291
17292 LONGEST bias = 0;
17293 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
17294 if (bias_attr != nullptr && bias_attr->form_is_constant ())
17295 bias = dwarf2_get_attr_constant_value (bias_attr, 0);
17296
17297 /* Normally, the DWARF producers are expected to use a signed
17298 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17299 But this is unfortunately not always the case, as witnessed
17300 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17301 is used instead. To work around that ambiguity, we treat
17302 the bounds as signed, and thus sign-extend their values, when
17303 the base type is signed. */
17304 negative_mask =
17305 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
17306 if (low.kind == PROP_CONST
17307 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17308 low.data.const_val |= negative_mask;
17309 if (high.kind == PROP_CONST
17310 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17311 high.data.const_val |= negative_mask;
17312
17313 /* Check for bit and byte strides. */
17314 struct dynamic_prop byte_stride_prop;
17315 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
17316 if (attr_byte_stride != nullptr)
17317 {
17318 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
17319 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
17320 prop_type);
17321 }
17322
17323 struct dynamic_prop bit_stride_prop;
17324 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
17325 if (attr_bit_stride != nullptr)
17326 {
17327 /* It only makes sense to have either a bit or byte stride. */
17328 if (attr_byte_stride != nullptr)
17329 {
17330 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
17331 "- DIE at %s [in module %s]"),
17332 sect_offset_str (die->sect_off),
17333 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17334 attr_bit_stride = nullptr;
17335 }
17336 else
17337 {
17338 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
17339 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
17340 prop_type);
17341 }
17342 }
17343
17344 if (attr_byte_stride != nullptr
17345 || attr_bit_stride != nullptr)
17346 {
17347 bool byte_stride_p = (attr_byte_stride != nullptr);
17348 struct dynamic_prop *stride
17349 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
17350
17351 range_type
17352 = create_range_type_with_stride (NULL, orig_base_type, &low,
17353 &high, bias, stride, byte_stride_p);
17354 }
17355 else
17356 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
17357
17358 if (high_bound_is_count)
17359 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17360
17361 /* Ada expects an empty array on no boundary attributes. */
17362 if (attr == NULL && cu->language != language_ada)
17363 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
17364
17365 name = dwarf2_name (die, cu);
17366 if (name)
17367 TYPE_NAME (range_type) = name;
17368
17369 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17370 if (attr != nullptr)
17371 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17372
17373 maybe_set_alignment (cu, die, range_type);
17374
17375 set_die_type (die, range_type, cu);
17376
17377 /* set_die_type should be already done. */
17378 set_descriptive_type (range_type, die, cu);
17379
17380 return range_type;
17381 }
17382
17383 static struct type *
17384 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17385 {
17386 struct type *type;
17387
17388 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
17389 NULL);
17390 TYPE_NAME (type) = dwarf2_name (die, cu);
17391
17392 /* In Ada, an unspecified type is typically used when the description
17393 of the type is deferred to a different unit. When encountering
17394 such a type, we treat it as a stub, and try to resolve it later on,
17395 when needed. */
17396 if (cu->language == language_ada)
17397 TYPE_STUB (type) = 1;
17398
17399 return set_die_type (die, type, cu);
17400 }
17401
17402 /* Read a single die and all its descendents. Set the die's sibling
17403 field to NULL; set other fields in the die correctly, and set all
17404 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17405 location of the info_ptr after reading all of those dies. PARENT
17406 is the parent of the die in question. */
17407
17408 static struct die_info *
17409 read_die_and_children (const struct die_reader_specs *reader,
17410 const gdb_byte *info_ptr,
17411 const gdb_byte **new_info_ptr,
17412 struct die_info *parent)
17413 {
17414 struct die_info *die;
17415 const gdb_byte *cur_ptr;
17416
17417 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
17418 if (die == NULL)
17419 {
17420 *new_info_ptr = cur_ptr;
17421 return NULL;
17422 }
17423 store_in_ref_table (die, reader->cu);
17424
17425 if (die->has_children)
17426 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
17427 else
17428 {
17429 die->child = NULL;
17430 *new_info_ptr = cur_ptr;
17431 }
17432
17433 die->sibling = NULL;
17434 die->parent = parent;
17435 return die;
17436 }
17437
17438 /* Read a die, all of its descendents, and all of its siblings; set
17439 all of the fields of all of the dies correctly. Arguments are as
17440 in read_die_and_children. */
17441
17442 static struct die_info *
17443 read_die_and_siblings_1 (const struct die_reader_specs *reader,
17444 const gdb_byte *info_ptr,
17445 const gdb_byte **new_info_ptr,
17446 struct die_info *parent)
17447 {
17448 struct die_info *first_die, *last_sibling;
17449 const gdb_byte *cur_ptr;
17450
17451 cur_ptr = info_ptr;
17452 first_die = last_sibling = NULL;
17453
17454 while (1)
17455 {
17456 struct die_info *die
17457 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
17458
17459 if (die == NULL)
17460 {
17461 *new_info_ptr = cur_ptr;
17462 return first_die;
17463 }
17464
17465 if (!first_die)
17466 first_die = die;
17467 else
17468 last_sibling->sibling = die;
17469
17470 last_sibling = die;
17471 }
17472 }
17473
17474 /* Read a die, all of its descendents, and all of its siblings; set
17475 all of the fields of all of the dies correctly. Arguments are as
17476 in read_die_and_children.
17477 This the main entry point for reading a DIE and all its children. */
17478
17479 static struct die_info *
17480 read_die_and_siblings (const struct die_reader_specs *reader,
17481 const gdb_byte *info_ptr,
17482 const gdb_byte **new_info_ptr,
17483 struct die_info *parent)
17484 {
17485 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17486 new_info_ptr, parent);
17487
17488 if (dwarf_die_debug)
17489 {
17490 fprintf_unfiltered (gdb_stdlog,
17491 "Read die from %s@0x%x of %s:\n",
17492 reader->die_section->get_name (),
17493 (unsigned) (info_ptr - reader->die_section->buffer),
17494 bfd_get_filename (reader->abfd));
17495 dump_die (die, dwarf_die_debug);
17496 }
17497
17498 return die;
17499 }
17500
17501 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17502 attributes.
17503 The caller is responsible for filling in the extra attributes
17504 and updating (*DIEP)->num_attrs.
17505 Set DIEP to point to a newly allocated die with its information,
17506 except for its child, sibling, and parent fields. */
17507
17508 static const gdb_byte *
17509 read_full_die_1 (const struct die_reader_specs *reader,
17510 struct die_info **diep, const gdb_byte *info_ptr,
17511 int num_extra_attrs)
17512 {
17513 unsigned int abbrev_number, bytes_read, i;
17514 struct abbrev_info *abbrev;
17515 struct die_info *die;
17516 struct dwarf2_cu *cu = reader->cu;
17517 bfd *abfd = reader->abfd;
17518
17519 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
17520 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17521 info_ptr += bytes_read;
17522 if (!abbrev_number)
17523 {
17524 *diep = NULL;
17525 return info_ptr;
17526 }
17527
17528 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
17529 if (!abbrev)
17530 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
17531 abbrev_number,
17532 bfd_get_filename (abfd));
17533
17534 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
17535 die->sect_off = sect_off;
17536 die->tag = abbrev->tag;
17537 die->abbrev = abbrev_number;
17538 die->has_children = abbrev->has_children;
17539
17540 /* Make the result usable.
17541 The caller needs to update num_attrs after adding the extra
17542 attributes. */
17543 die->num_attrs = abbrev->num_attrs;
17544
17545 std::vector<int> indexes_that_need_reprocess;
17546 for (i = 0; i < abbrev->num_attrs; ++i)
17547 {
17548 bool need_reprocess;
17549 info_ptr =
17550 read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
17551 info_ptr, &need_reprocess);
17552 if (need_reprocess)
17553 indexes_that_need_reprocess.push_back (i);
17554 }
17555
17556 struct attribute *attr = dwarf2_attr_no_follow (die, DW_AT_str_offsets_base);
17557 if (attr != nullptr)
17558 cu->str_offsets_base = DW_UNSND (attr);
17559
17560 auto maybe_addr_base = lookup_addr_base(die);
17561 if (maybe_addr_base.has_value ())
17562 cu->addr_base = *maybe_addr_base;
17563 for (int index : indexes_that_need_reprocess)
17564 read_attribute_reprocess (reader, &die->attrs[index]);
17565 *diep = die;
17566 return info_ptr;
17567 }
17568
17569 /* Read a die and all its attributes.
17570 Set DIEP to point to a newly allocated die with its information,
17571 except for its child, sibling, and parent fields. */
17572
17573 static const gdb_byte *
17574 read_full_die (const struct die_reader_specs *reader,
17575 struct die_info **diep, const gdb_byte *info_ptr)
17576 {
17577 const gdb_byte *result;
17578
17579 result = read_full_die_1 (reader, diep, info_ptr, 0);
17580
17581 if (dwarf_die_debug)
17582 {
17583 fprintf_unfiltered (gdb_stdlog,
17584 "Read die from %s@0x%x of %s:\n",
17585 reader->die_section->get_name (),
17586 (unsigned) (info_ptr - reader->die_section->buffer),
17587 bfd_get_filename (reader->abfd));
17588 dump_die (*diep, dwarf_die_debug);
17589 }
17590
17591 return result;
17592 }
17593 \f
17594
17595 /* Returns nonzero if TAG represents a type that we might generate a partial
17596 symbol for. */
17597
17598 static int
17599 is_type_tag_for_partial (int tag)
17600 {
17601 switch (tag)
17602 {
17603 #if 0
17604 /* Some types that would be reasonable to generate partial symbols for,
17605 that we don't at present. */
17606 case DW_TAG_array_type:
17607 case DW_TAG_file_type:
17608 case DW_TAG_ptr_to_member_type:
17609 case DW_TAG_set_type:
17610 case DW_TAG_string_type:
17611 case DW_TAG_subroutine_type:
17612 #endif
17613 case DW_TAG_base_type:
17614 case DW_TAG_class_type:
17615 case DW_TAG_interface_type:
17616 case DW_TAG_enumeration_type:
17617 case DW_TAG_structure_type:
17618 case DW_TAG_subrange_type:
17619 case DW_TAG_typedef:
17620 case DW_TAG_union_type:
17621 return 1;
17622 default:
17623 return 0;
17624 }
17625 }
17626
17627 /* Load all DIEs that are interesting for partial symbols into memory. */
17628
17629 static struct partial_die_info *
17630 load_partial_dies (const struct die_reader_specs *reader,
17631 const gdb_byte *info_ptr, int building_psymtab)
17632 {
17633 struct dwarf2_cu *cu = reader->cu;
17634 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17635 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
17636 unsigned int bytes_read;
17637 unsigned int load_all = 0;
17638 int nesting_level = 1;
17639
17640 parent_die = NULL;
17641 last_die = NULL;
17642
17643 gdb_assert (cu->per_cu != NULL);
17644 if (cu->per_cu->load_all_dies)
17645 load_all = 1;
17646
17647 cu->partial_dies
17648 = htab_create_alloc_ex (cu->header.length / 12,
17649 partial_die_hash,
17650 partial_die_eq,
17651 NULL,
17652 &cu->comp_unit_obstack,
17653 hashtab_obstack_allocate,
17654 dummy_obstack_deallocate);
17655
17656 while (1)
17657 {
17658 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
17659
17660 /* A NULL abbrev means the end of a series of children. */
17661 if (abbrev == NULL)
17662 {
17663 if (--nesting_level == 0)
17664 return first_die;
17665
17666 info_ptr += bytes_read;
17667 last_die = parent_die;
17668 parent_die = parent_die->die_parent;
17669 continue;
17670 }
17671
17672 /* Check for template arguments. We never save these; if
17673 they're seen, we just mark the parent, and go on our way. */
17674 if (parent_die != NULL
17675 && cu->language == language_cplus
17676 && (abbrev->tag == DW_TAG_template_type_param
17677 || abbrev->tag == DW_TAG_template_value_param))
17678 {
17679 parent_die->has_template_arguments = 1;
17680
17681 if (!load_all)
17682 {
17683 /* We don't need a partial DIE for the template argument. */
17684 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
17685 continue;
17686 }
17687 }
17688
17689 /* We only recurse into c++ subprograms looking for template arguments.
17690 Skip their other children. */
17691 if (!load_all
17692 && cu->language == language_cplus
17693 && parent_die != NULL
17694 && parent_die->tag == DW_TAG_subprogram)
17695 {
17696 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
17697 continue;
17698 }
17699
17700 /* Check whether this DIE is interesting enough to save. Normally
17701 we would not be interested in members here, but there may be
17702 later variables referencing them via DW_AT_specification (for
17703 static members). */
17704 if (!load_all
17705 && !is_type_tag_for_partial (abbrev->tag)
17706 && abbrev->tag != DW_TAG_constant
17707 && abbrev->tag != DW_TAG_enumerator
17708 && abbrev->tag != DW_TAG_subprogram
17709 && abbrev->tag != DW_TAG_inlined_subroutine
17710 && abbrev->tag != DW_TAG_lexical_block
17711 && abbrev->tag != DW_TAG_variable
17712 && abbrev->tag != DW_TAG_namespace
17713 && abbrev->tag != DW_TAG_module
17714 && abbrev->tag != DW_TAG_member
17715 && abbrev->tag != DW_TAG_imported_unit
17716 && abbrev->tag != DW_TAG_imported_declaration)
17717 {
17718 /* Otherwise we skip to the next sibling, if any. */
17719 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
17720 continue;
17721 }
17722
17723 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
17724 abbrev);
17725
17726 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
17727
17728 /* This two-pass algorithm for processing partial symbols has a
17729 high cost in cache pressure. Thus, handle some simple cases
17730 here which cover the majority of C partial symbols. DIEs
17731 which neither have specification tags in them, nor could have
17732 specification tags elsewhere pointing at them, can simply be
17733 processed and discarded.
17734
17735 This segment is also optional; scan_partial_symbols and
17736 add_partial_symbol will handle these DIEs if we chain
17737 them in normally. When compilers which do not emit large
17738 quantities of duplicate debug information are more common,
17739 this code can probably be removed. */
17740
17741 /* Any complete simple types at the top level (pretty much all
17742 of them, for a language without namespaces), can be processed
17743 directly. */
17744 if (parent_die == NULL
17745 && pdi.has_specification == 0
17746 && pdi.is_declaration == 0
17747 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
17748 || pdi.tag == DW_TAG_base_type
17749 || pdi.tag == DW_TAG_subrange_type))
17750 {
17751 if (building_psymtab && pdi.name != NULL)
17752 add_psymbol_to_list (pdi.name, false,
17753 VAR_DOMAIN, LOC_TYPEDEF, -1,
17754 psymbol_placement::STATIC,
17755 0, cu->language, objfile);
17756 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
17757 continue;
17758 }
17759
17760 /* The exception for DW_TAG_typedef with has_children above is
17761 a workaround of GCC PR debug/47510. In the case of this complaint
17762 type_name_or_error will error on such types later.
17763
17764 GDB skipped children of DW_TAG_typedef by the shortcut above and then
17765 it could not find the child DIEs referenced later, this is checked
17766 above. In correct DWARF DW_TAG_typedef should have no children. */
17767
17768 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
17769 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
17770 "- DIE at %s [in module %s]"),
17771 sect_offset_str (pdi.sect_off), objfile_name (objfile));
17772
17773 /* If we're at the second level, and we're an enumerator, and
17774 our parent has no specification (meaning possibly lives in a
17775 namespace elsewhere), then we can add the partial symbol now
17776 instead of queueing it. */
17777 if (pdi.tag == DW_TAG_enumerator
17778 && parent_die != NULL
17779 && parent_die->die_parent == NULL
17780 && parent_die->tag == DW_TAG_enumeration_type
17781 && parent_die->has_specification == 0)
17782 {
17783 if (pdi.name == NULL)
17784 complaint (_("malformed enumerator DIE ignored"));
17785 else if (building_psymtab)
17786 add_psymbol_to_list (pdi.name, false,
17787 VAR_DOMAIN, LOC_CONST, -1,
17788 cu->language == language_cplus
17789 ? psymbol_placement::GLOBAL
17790 : psymbol_placement::STATIC,
17791 0, cu->language, objfile);
17792
17793 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
17794 continue;
17795 }
17796
17797 struct partial_die_info *part_die
17798 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
17799
17800 /* We'll save this DIE so link it in. */
17801 part_die->die_parent = parent_die;
17802 part_die->die_sibling = NULL;
17803 part_die->die_child = NULL;
17804
17805 if (last_die && last_die == parent_die)
17806 last_die->die_child = part_die;
17807 else if (last_die)
17808 last_die->die_sibling = part_die;
17809
17810 last_die = part_die;
17811
17812 if (first_die == NULL)
17813 first_die = part_die;
17814
17815 /* Maybe add the DIE to the hash table. Not all DIEs that we
17816 find interesting need to be in the hash table, because we
17817 also have the parent/sibling/child chains; only those that we
17818 might refer to by offset later during partial symbol reading.
17819
17820 For now this means things that might have be the target of a
17821 DW_AT_specification, DW_AT_abstract_origin, or
17822 DW_AT_extension. DW_AT_extension will refer only to
17823 namespaces; DW_AT_abstract_origin refers to functions (and
17824 many things under the function DIE, but we do not recurse
17825 into function DIEs during partial symbol reading) and
17826 possibly variables as well; DW_AT_specification refers to
17827 declarations. Declarations ought to have the DW_AT_declaration
17828 flag. It happens that GCC forgets to put it in sometimes, but
17829 only for functions, not for types.
17830
17831 Adding more things than necessary to the hash table is harmless
17832 except for the performance cost. Adding too few will result in
17833 wasted time in find_partial_die, when we reread the compilation
17834 unit with load_all_dies set. */
17835
17836 if (load_all
17837 || abbrev->tag == DW_TAG_constant
17838 || abbrev->tag == DW_TAG_subprogram
17839 || abbrev->tag == DW_TAG_variable
17840 || abbrev->tag == DW_TAG_namespace
17841 || part_die->is_declaration)
17842 {
17843 void **slot;
17844
17845 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
17846 to_underlying (part_die->sect_off),
17847 INSERT);
17848 *slot = part_die;
17849 }
17850
17851 /* For some DIEs we want to follow their children (if any). For C
17852 we have no reason to follow the children of structures; for other
17853 languages we have to, so that we can get at method physnames
17854 to infer fully qualified class names, for DW_AT_specification,
17855 and for C++ template arguments. For C++, we also look one level
17856 inside functions to find template arguments (if the name of the
17857 function does not already contain the template arguments).
17858
17859 For Ada and Fortran, we need to scan the children of subprograms
17860 and lexical blocks as well because these languages allow the
17861 definition of nested entities that could be interesting for the
17862 debugger, such as nested subprograms for instance. */
17863 if (last_die->has_children
17864 && (load_all
17865 || last_die->tag == DW_TAG_namespace
17866 || last_die->tag == DW_TAG_module
17867 || last_die->tag == DW_TAG_enumeration_type
17868 || (cu->language == language_cplus
17869 && last_die->tag == DW_TAG_subprogram
17870 && (last_die->name == NULL
17871 || strchr (last_die->name, '<') == NULL))
17872 || (cu->language != language_c
17873 && (last_die->tag == DW_TAG_class_type
17874 || last_die->tag == DW_TAG_interface_type
17875 || last_die->tag == DW_TAG_structure_type
17876 || last_die->tag == DW_TAG_union_type))
17877 || ((cu->language == language_ada
17878 || cu->language == language_fortran)
17879 && (last_die->tag == DW_TAG_subprogram
17880 || last_die->tag == DW_TAG_lexical_block))))
17881 {
17882 nesting_level++;
17883 parent_die = last_die;
17884 continue;
17885 }
17886
17887 /* Otherwise we skip to the next sibling, if any. */
17888 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
17889
17890 /* Back to the top, do it again. */
17891 }
17892 }
17893
17894 partial_die_info::partial_die_info (sect_offset sect_off_,
17895 struct abbrev_info *abbrev)
17896 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
17897 {
17898 }
17899
17900 /* Read a minimal amount of information into the minimal die structure.
17901 INFO_PTR should point just after the initial uleb128 of a DIE. */
17902
17903 const gdb_byte *
17904 partial_die_info::read (const struct die_reader_specs *reader,
17905 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
17906 {
17907 struct dwarf2_cu *cu = reader->cu;
17908 struct dwarf2_per_objfile *dwarf2_per_objfile
17909 = cu->per_cu->dwarf2_per_objfile;
17910 unsigned int i;
17911 int has_low_pc_attr = 0;
17912 int has_high_pc_attr = 0;
17913 int high_pc_relative = 0;
17914
17915 std::vector<struct attribute> attr_vec (abbrev.num_attrs);
17916 for (i = 0; i < abbrev.num_attrs; ++i)
17917 {
17918 bool need_reprocess;
17919 info_ptr = read_attribute (reader, &attr_vec[i], &abbrev.attrs[i],
17920 info_ptr, &need_reprocess);
17921 /* String and address offsets that need to do the reprocessing have
17922 already been read at this point, so there is no need to wait until
17923 the loop terminates to do the reprocessing. */
17924 if (need_reprocess)
17925 read_attribute_reprocess (reader, &attr_vec[i]);
17926 attribute &attr = attr_vec[i];
17927 /* Store the data if it is of an attribute we want to keep in a
17928 partial symbol table. */
17929 switch (attr.name)
17930 {
17931 case DW_AT_name:
17932 switch (tag)
17933 {
17934 case DW_TAG_compile_unit:
17935 case DW_TAG_partial_unit:
17936 case DW_TAG_type_unit:
17937 /* Compilation units have a DW_AT_name that is a filename, not
17938 a source language identifier. */
17939 case DW_TAG_enumeration_type:
17940 case DW_TAG_enumerator:
17941 /* These tags always have simple identifiers already; no need
17942 to canonicalize them. */
17943 name = DW_STRING (&attr);
17944 break;
17945 default:
17946 {
17947 struct objfile *objfile = dwarf2_per_objfile->objfile;
17948
17949 name
17950 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
17951 &objfile->per_bfd->storage_obstack);
17952 }
17953 break;
17954 }
17955 break;
17956 case DW_AT_linkage_name:
17957 case DW_AT_MIPS_linkage_name:
17958 /* Note that both forms of linkage name might appear. We
17959 assume they will be the same, and we only store the last
17960 one we see. */
17961 linkage_name = DW_STRING (&attr);
17962 break;
17963 case DW_AT_low_pc:
17964 has_low_pc_attr = 1;
17965 lowpc = attr.value_as_address ();
17966 break;
17967 case DW_AT_high_pc:
17968 has_high_pc_attr = 1;
17969 highpc = attr.value_as_address ();
17970 if (cu->header.version >= 4 && attr.form_is_constant ())
17971 high_pc_relative = 1;
17972 break;
17973 case DW_AT_location:
17974 /* Support the .debug_loc offsets. */
17975 if (attr.form_is_block ())
17976 {
17977 d.locdesc = DW_BLOCK (&attr);
17978 }
17979 else if (attr.form_is_section_offset ())
17980 {
17981 dwarf2_complex_location_expr_complaint ();
17982 }
17983 else
17984 {
17985 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17986 "partial symbol information");
17987 }
17988 break;
17989 case DW_AT_external:
17990 is_external = DW_UNSND (&attr);
17991 break;
17992 case DW_AT_declaration:
17993 is_declaration = DW_UNSND (&attr);
17994 break;
17995 case DW_AT_type:
17996 has_type = 1;
17997 break;
17998 case DW_AT_abstract_origin:
17999 case DW_AT_specification:
18000 case DW_AT_extension:
18001 has_specification = 1;
18002 spec_offset = dwarf2_get_ref_die_offset (&attr);
18003 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18004 || cu->per_cu->is_dwz);
18005 break;
18006 case DW_AT_sibling:
18007 /* Ignore absolute siblings, they might point outside of
18008 the current compile unit. */
18009 if (attr.form == DW_FORM_ref_addr)
18010 complaint (_("ignoring absolute DW_AT_sibling"));
18011 else
18012 {
18013 const gdb_byte *buffer = reader->buffer;
18014 sect_offset off = dwarf2_get_ref_die_offset (&attr);
18015 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
18016
18017 if (sibling_ptr < info_ptr)
18018 complaint (_("DW_AT_sibling points backwards"));
18019 else if (sibling_ptr > reader->buffer_end)
18020 dwarf2_section_buffer_overflow_complaint (reader->die_section);
18021 else
18022 sibling = sibling_ptr;
18023 }
18024 break;
18025 case DW_AT_byte_size:
18026 has_byte_size = 1;
18027 break;
18028 case DW_AT_const_value:
18029 has_const_value = 1;
18030 break;
18031 case DW_AT_calling_convention:
18032 /* DWARF doesn't provide a way to identify a program's source-level
18033 entry point. DW_AT_calling_convention attributes are only meant
18034 to describe functions' calling conventions.
18035
18036 However, because it's a necessary piece of information in
18037 Fortran, and before DWARF 4 DW_CC_program was the only
18038 piece of debugging information whose definition refers to
18039 a 'main program' at all, several compilers marked Fortran
18040 main programs with DW_CC_program --- even when those
18041 functions use the standard calling conventions.
18042
18043 Although DWARF now specifies a way to provide this
18044 information, we support this practice for backward
18045 compatibility. */
18046 if (DW_UNSND (&attr) == DW_CC_program
18047 && cu->language == language_fortran)
18048 main_subprogram = 1;
18049 break;
18050 case DW_AT_inline:
18051 if (DW_UNSND (&attr) == DW_INL_inlined
18052 || DW_UNSND (&attr) == DW_INL_declared_inlined)
18053 may_be_inlined = 1;
18054 break;
18055
18056 case DW_AT_import:
18057 if (tag == DW_TAG_imported_unit)
18058 {
18059 d.sect_off = dwarf2_get_ref_die_offset (&attr);
18060 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18061 || cu->per_cu->is_dwz);
18062 }
18063 break;
18064
18065 case DW_AT_main_subprogram:
18066 main_subprogram = DW_UNSND (&attr);
18067 break;
18068
18069 case DW_AT_ranges:
18070 {
18071 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18072 but that requires a full DIE, so instead we just
18073 reimplement it. */
18074 int need_ranges_base = tag != DW_TAG_compile_unit;
18075 unsigned int ranges_offset = (DW_UNSND (&attr)
18076 + (need_ranges_base
18077 ? cu->ranges_base
18078 : 0));
18079
18080 /* Value of the DW_AT_ranges attribute is the offset in the
18081 .debug_ranges section. */
18082 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18083 nullptr))
18084 has_pc_info = 1;
18085 }
18086 break;
18087
18088 default:
18089 break;
18090 }
18091 }
18092
18093 /* For Ada, if both the name and the linkage name appear, we prefer
18094 the latter. This lets "catch exception" work better, regardless
18095 of the order in which the name and linkage name were emitted.
18096 Really, though, this is just a workaround for the fact that gdb
18097 doesn't store both the name and the linkage name. */
18098 if (cu->language == language_ada && linkage_name != nullptr)
18099 name = linkage_name;
18100
18101 if (high_pc_relative)
18102 highpc += lowpc;
18103
18104 if (has_low_pc_attr && has_high_pc_attr)
18105 {
18106 /* When using the GNU linker, .gnu.linkonce. sections are used to
18107 eliminate duplicate copies of functions and vtables and such.
18108 The linker will arbitrarily choose one and discard the others.
18109 The AT_*_pc values for such functions refer to local labels in
18110 these sections. If the section from that file was discarded, the
18111 labels are not in the output, so the relocs get a value of 0.
18112 If this is a discarded function, mark the pc bounds as invalid,
18113 so that GDB will ignore it. */
18114 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
18115 {
18116 struct objfile *objfile = dwarf2_per_objfile->objfile;
18117 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18118
18119 complaint (_("DW_AT_low_pc %s is zero "
18120 "for DIE at %s [in module %s]"),
18121 paddress (gdbarch, lowpc),
18122 sect_offset_str (sect_off),
18123 objfile_name (objfile));
18124 }
18125 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
18126 else if (lowpc >= highpc)
18127 {
18128 struct objfile *objfile = dwarf2_per_objfile->objfile;
18129 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18130
18131 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
18132 "for DIE at %s [in module %s]"),
18133 paddress (gdbarch, lowpc),
18134 paddress (gdbarch, highpc),
18135 sect_offset_str (sect_off),
18136 objfile_name (objfile));
18137 }
18138 else
18139 has_pc_info = 1;
18140 }
18141
18142 return info_ptr;
18143 }
18144
18145 /* Find a cached partial DIE at OFFSET in CU. */
18146
18147 struct partial_die_info *
18148 dwarf2_cu::find_partial_die (sect_offset sect_off)
18149 {
18150 struct partial_die_info *lookup_die = NULL;
18151 struct partial_die_info part_die (sect_off);
18152
18153 lookup_die = ((struct partial_die_info *)
18154 htab_find_with_hash (partial_dies, &part_die,
18155 to_underlying (sect_off)));
18156
18157 return lookup_die;
18158 }
18159
18160 /* Find a partial DIE at OFFSET, which may or may not be in CU,
18161 except in the case of .debug_types DIEs which do not reference
18162 outside their CU (they do however referencing other types via
18163 DW_FORM_ref_sig8). */
18164
18165 static const struct cu_partial_die_info
18166 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
18167 {
18168 struct dwarf2_per_objfile *dwarf2_per_objfile
18169 = cu->per_cu->dwarf2_per_objfile;
18170 struct objfile *objfile = dwarf2_per_objfile->objfile;
18171 struct dwarf2_per_cu_data *per_cu = NULL;
18172 struct partial_die_info *pd = NULL;
18173
18174 if (offset_in_dwz == cu->per_cu->is_dwz
18175 && cu->header.offset_in_cu_p (sect_off))
18176 {
18177 pd = cu->find_partial_die (sect_off);
18178 if (pd != NULL)
18179 return { cu, pd };
18180 /* We missed recording what we needed.
18181 Load all dies and try again. */
18182 per_cu = cu->per_cu;
18183 }
18184 else
18185 {
18186 /* TUs don't reference other CUs/TUs (except via type signatures). */
18187 if (cu->per_cu->is_debug_types)
18188 {
18189 error (_("Dwarf Error: Type Unit at offset %s contains"
18190 " external reference to offset %s [in module %s].\n"),
18191 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
18192 bfd_get_filename (objfile->obfd));
18193 }
18194 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
18195 dwarf2_per_objfile);
18196
18197 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18198 load_partial_comp_unit (per_cu);
18199
18200 per_cu->cu->last_used = 0;
18201 pd = per_cu->cu->find_partial_die (sect_off);
18202 }
18203
18204 /* If we didn't find it, and not all dies have been loaded,
18205 load them all and try again. */
18206
18207 if (pd == NULL && per_cu->load_all_dies == 0)
18208 {
18209 per_cu->load_all_dies = 1;
18210
18211 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18212 THIS_CU->cu may already be in use. So we can't just free it and
18213 replace its DIEs with the ones we read in. Instead, we leave those
18214 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18215 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18216 set. */
18217 load_partial_comp_unit (per_cu);
18218
18219 pd = per_cu->cu->find_partial_die (sect_off);
18220 }
18221
18222 if (pd == NULL)
18223 internal_error (__FILE__, __LINE__,
18224 _("could not find partial DIE %s "
18225 "in cache [from module %s]\n"),
18226 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
18227 return { per_cu->cu, pd };
18228 }
18229
18230 /* See if we can figure out if the class lives in a namespace. We do
18231 this by looking for a member function; its demangled name will
18232 contain namespace info, if there is any. */
18233
18234 static void
18235 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18236 struct dwarf2_cu *cu)
18237 {
18238 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18239 what template types look like, because the demangler
18240 frequently doesn't give the same name as the debug info. We
18241 could fix this by only using the demangled name to get the
18242 prefix (but see comment in read_structure_type). */
18243
18244 struct partial_die_info *real_pdi;
18245 struct partial_die_info *child_pdi;
18246
18247 /* If this DIE (this DIE's specification, if any) has a parent, then
18248 we should not do this. We'll prepend the parent's fully qualified
18249 name when we create the partial symbol. */
18250
18251 real_pdi = struct_pdi;
18252 while (real_pdi->has_specification)
18253 {
18254 auto res = find_partial_die (real_pdi->spec_offset,
18255 real_pdi->spec_is_dwz, cu);
18256 real_pdi = res.pdi;
18257 cu = res.cu;
18258 }
18259
18260 if (real_pdi->die_parent != NULL)
18261 return;
18262
18263 for (child_pdi = struct_pdi->die_child;
18264 child_pdi != NULL;
18265 child_pdi = child_pdi->die_sibling)
18266 {
18267 if (child_pdi->tag == DW_TAG_subprogram
18268 && child_pdi->linkage_name != NULL)
18269 {
18270 gdb::unique_xmalloc_ptr<char> actual_class_name
18271 (language_class_name_from_physname (cu->language_defn,
18272 child_pdi->linkage_name));
18273 if (actual_class_name != NULL)
18274 {
18275 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18276 struct_pdi->name
18277 = obstack_strdup (&objfile->per_bfd->storage_obstack,
18278 actual_class_name.get ());
18279 }
18280 break;
18281 }
18282 }
18283 }
18284
18285 void
18286 partial_die_info::fixup (struct dwarf2_cu *cu)
18287 {
18288 /* Once we've fixed up a die, there's no point in doing so again.
18289 This also avoids a memory leak if we were to call
18290 guess_partial_die_structure_name multiple times. */
18291 if (fixup_called)
18292 return;
18293
18294 /* If we found a reference attribute and the DIE has no name, try
18295 to find a name in the referred to DIE. */
18296
18297 if (name == NULL && has_specification)
18298 {
18299 struct partial_die_info *spec_die;
18300
18301 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18302 spec_die = res.pdi;
18303 cu = res.cu;
18304
18305 spec_die->fixup (cu);
18306
18307 if (spec_die->name)
18308 {
18309 name = spec_die->name;
18310
18311 /* Copy DW_AT_external attribute if it is set. */
18312 if (spec_die->is_external)
18313 is_external = spec_die->is_external;
18314 }
18315 }
18316
18317 /* Set default names for some unnamed DIEs. */
18318
18319 if (name == NULL && tag == DW_TAG_namespace)
18320 name = CP_ANONYMOUS_NAMESPACE_STR;
18321
18322 /* If there is no parent die to provide a namespace, and there are
18323 children, see if we can determine the namespace from their linkage
18324 name. */
18325 if (cu->language == language_cplus
18326 && !cu->per_cu->dwarf2_per_objfile->types.empty ()
18327 && die_parent == NULL
18328 && has_children
18329 && (tag == DW_TAG_class_type
18330 || tag == DW_TAG_structure_type
18331 || tag == DW_TAG_union_type))
18332 guess_partial_die_structure_name (this, cu);
18333
18334 /* GCC might emit a nameless struct or union that has a linkage
18335 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
18336 if (name == NULL
18337 && (tag == DW_TAG_class_type
18338 || tag == DW_TAG_interface_type
18339 || tag == DW_TAG_structure_type
18340 || tag == DW_TAG_union_type)
18341 && linkage_name != NULL)
18342 {
18343 gdb::unique_xmalloc_ptr<char> demangled
18344 (gdb_demangle (linkage_name, DMGL_TYPES));
18345 if (demangled != nullptr)
18346 {
18347 const char *base;
18348
18349 /* Strip any leading namespaces/classes, keep only the base name.
18350 DW_AT_name for named DIEs does not contain the prefixes. */
18351 base = strrchr (demangled.get (), ':');
18352 if (base && base > demangled.get () && base[-1] == ':')
18353 base++;
18354 else
18355 base = demangled.get ();
18356
18357 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18358 name = obstack_strdup (&objfile->per_bfd->storage_obstack, base);
18359 }
18360 }
18361
18362 fixup_called = 1;
18363 }
18364
18365 /* Process the attributes that had to be skipped in the first round. These
18366 attributes are the ones that need str_offsets_base or addr_base attributes.
18367 They could not have been processed in the first round, because at the time
18368 the values of str_offsets_base or addr_base may not have been known. */
18369 void read_attribute_reprocess (const struct die_reader_specs *reader,
18370 struct attribute *attr)
18371 {
18372 struct dwarf2_cu *cu = reader->cu;
18373 switch (attr->form)
18374 {
18375 case DW_FORM_addrx:
18376 case DW_FORM_GNU_addr_index:
18377 DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
18378 break;
18379 case DW_FORM_strx:
18380 case DW_FORM_strx1:
18381 case DW_FORM_strx2:
18382 case DW_FORM_strx3:
18383 case DW_FORM_strx4:
18384 case DW_FORM_GNU_str_index:
18385 {
18386 unsigned int str_index = DW_UNSND (attr);
18387 if (reader->dwo_file != NULL)
18388 {
18389 DW_STRING (attr) = read_dwo_str_index (reader, str_index);
18390 DW_STRING_IS_CANONICAL (attr) = 0;
18391 }
18392 else
18393 {
18394 DW_STRING (attr) = read_stub_str_index (cu, str_index);
18395 DW_STRING_IS_CANONICAL (attr) = 0;
18396 }
18397 break;
18398 }
18399 default:
18400 gdb_assert_not_reached (_("Unexpected DWARF form."));
18401 }
18402 }
18403
18404 /* Read an attribute value described by an attribute form. */
18405
18406 static const gdb_byte *
18407 read_attribute_value (const struct die_reader_specs *reader,
18408 struct attribute *attr, unsigned form,
18409 LONGEST implicit_const, const gdb_byte *info_ptr,
18410 bool *need_reprocess)
18411 {
18412 struct dwarf2_cu *cu = reader->cu;
18413 struct dwarf2_per_objfile *dwarf2_per_objfile
18414 = cu->per_cu->dwarf2_per_objfile;
18415 struct objfile *objfile = dwarf2_per_objfile->objfile;
18416 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18417 bfd *abfd = reader->abfd;
18418 struct comp_unit_head *cu_header = &cu->header;
18419 unsigned int bytes_read;
18420 struct dwarf_block *blk;
18421 *need_reprocess = false;
18422
18423 attr->form = (enum dwarf_form) form;
18424 switch (form)
18425 {
18426 case DW_FORM_ref_addr:
18427 if (cu->header.version == 2)
18428 DW_UNSND (attr) = cu->header.read_address (abfd, info_ptr,
18429 &bytes_read);
18430 else
18431 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr,
18432 &bytes_read);
18433 info_ptr += bytes_read;
18434 break;
18435 case DW_FORM_GNU_ref_alt:
18436 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
18437 info_ptr += bytes_read;
18438 break;
18439 case DW_FORM_addr:
18440 DW_ADDR (attr) = cu->header.read_address (abfd, info_ptr, &bytes_read);
18441 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
18442 info_ptr += bytes_read;
18443 break;
18444 case DW_FORM_block2:
18445 blk = dwarf_alloc_block (cu);
18446 blk->size = read_2_bytes (abfd, info_ptr);
18447 info_ptr += 2;
18448 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18449 info_ptr += blk->size;
18450 DW_BLOCK (attr) = blk;
18451 break;
18452 case DW_FORM_block4:
18453 blk = dwarf_alloc_block (cu);
18454 blk->size = read_4_bytes (abfd, info_ptr);
18455 info_ptr += 4;
18456 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18457 info_ptr += blk->size;
18458 DW_BLOCK (attr) = blk;
18459 break;
18460 case DW_FORM_data2:
18461 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
18462 info_ptr += 2;
18463 break;
18464 case DW_FORM_data4:
18465 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
18466 info_ptr += 4;
18467 break;
18468 case DW_FORM_data8:
18469 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
18470 info_ptr += 8;
18471 break;
18472 case DW_FORM_data16:
18473 blk = dwarf_alloc_block (cu);
18474 blk->size = 16;
18475 blk->data = read_n_bytes (abfd, info_ptr, 16);
18476 info_ptr += 16;
18477 DW_BLOCK (attr) = blk;
18478 break;
18479 case DW_FORM_sec_offset:
18480 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
18481 info_ptr += bytes_read;
18482 break;
18483 case DW_FORM_string:
18484 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
18485 DW_STRING_IS_CANONICAL (attr) = 0;
18486 info_ptr += bytes_read;
18487 break;
18488 case DW_FORM_strp:
18489 if (!cu->per_cu->is_dwz)
18490 {
18491 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
18492 abfd, info_ptr, cu_header,
18493 &bytes_read);
18494 DW_STRING_IS_CANONICAL (attr) = 0;
18495 info_ptr += bytes_read;
18496 break;
18497 }
18498 /* FALLTHROUGH */
18499 case DW_FORM_line_strp:
18500 if (!cu->per_cu->is_dwz)
18501 {
18502 DW_STRING (attr) = read_indirect_line_string (dwarf2_per_objfile,
18503 abfd, info_ptr,
18504 cu_header, &bytes_read);
18505 DW_STRING_IS_CANONICAL (attr) = 0;
18506 info_ptr += bytes_read;
18507 break;
18508 }
18509 /* FALLTHROUGH */
18510 case DW_FORM_GNU_strp_alt:
18511 {
18512 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
18513 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
18514 &bytes_read);
18515
18516 DW_STRING (attr) = read_indirect_string_from_dwz (objfile,
18517 dwz, str_offset);
18518 DW_STRING_IS_CANONICAL (attr) = 0;
18519 info_ptr += bytes_read;
18520 }
18521 break;
18522 case DW_FORM_exprloc:
18523 case DW_FORM_block:
18524 blk = dwarf_alloc_block (cu);
18525 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18526 info_ptr += bytes_read;
18527 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18528 info_ptr += blk->size;
18529 DW_BLOCK (attr) = blk;
18530 break;
18531 case DW_FORM_block1:
18532 blk = dwarf_alloc_block (cu);
18533 blk->size = read_1_byte (abfd, info_ptr);
18534 info_ptr += 1;
18535 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18536 info_ptr += blk->size;
18537 DW_BLOCK (attr) = blk;
18538 break;
18539 case DW_FORM_data1:
18540 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18541 info_ptr += 1;
18542 break;
18543 case DW_FORM_flag:
18544 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18545 info_ptr += 1;
18546 break;
18547 case DW_FORM_flag_present:
18548 DW_UNSND (attr) = 1;
18549 break;
18550 case DW_FORM_sdata:
18551 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
18552 info_ptr += bytes_read;
18553 break;
18554 case DW_FORM_udata:
18555 case DW_FORM_rnglistx:
18556 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18557 info_ptr += bytes_read;
18558 break;
18559 case DW_FORM_ref1:
18560 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18561 + read_1_byte (abfd, info_ptr));
18562 info_ptr += 1;
18563 break;
18564 case DW_FORM_ref2:
18565 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18566 + read_2_bytes (abfd, info_ptr));
18567 info_ptr += 2;
18568 break;
18569 case DW_FORM_ref4:
18570 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18571 + read_4_bytes (abfd, info_ptr));
18572 info_ptr += 4;
18573 break;
18574 case DW_FORM_ref8:
18575 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18576 + read_8_bytes (abfd, info_ptr));
18577 info_ptr += 8;
18578 break;
18579 case DW_FORM_ref_sig8:
18580 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
18581 info_ptr += 8;
18582 break;
18583 case DW_FORM_ref_udata:
18584 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18585 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
18586 info_ptr += bytes_read;
18587 break;
18588 case DW_FORM_indirect:
18589 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18590 info_ptr += bytes_read;
18591 if (form == DW_FORM_implicit_const)
18592 {
18593 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
18594 info_ptr += bytes_read;
18595 }
18596 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
18597 info_ptr, need_reprocess);
18598 break;
18599 case DW_FORM_implicit_const:
18600 DW_SND (attr) = implicit_const;
18601 break;
18602 case DW_FORM_addrx:
18603 case DW_FORM_GNU_addr_index:
18604 *need_reprocess = true;
18605 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18606 info_ptr += bytes_read;
18607 break;
18608 case DW_FORM_strx:
18609 case DW_FORM_strx1:
18610 case DW_FORM_strx2:
18611 case DW_FORM_strx3:
18612 case DW_FORM_strx4:
18613 case DW_FORM_GNU_str_index:
18614 {
18615 ULONGEST str_index;
18616 if (form == DW_FORM_strx1)
18617 {
18618 str_index = read_1_byte (abfd, info_ptr);
18619 info_ptr += 1;
18620 }
18621 else if (form == DW_FORM_strx2)
18622 {
18623 str_index = read_2_bytes (abfd, info_ptr);
18624 info_ptr += 2;
18625 }
18626 else if (form == DW_FORM_strx3)
18627 {
18628 str_index = read_3_bytes (abfd, info_ptr);
18629 info_ptr += 3;
18630 }
18631 else if (form == DW_FORM_strx4)
18632 {
18633 str_index = read_4_bytes (abfd, info_ptr);
18634 info_ptr += 4;
18635 }
18636 else
18637 {
18638 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18639 info_ptr += bytes_read;
18640 }
18641 *need_reprocess = true;
18642 DW_UNSND (attr) = str_index;
18643 }
18644 break;
18645 default:
18646 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
18647 dwarf_form_name (form),
18648 bfd_get_filename (abfd));
18649 }
18650
18651 /* Super hack. */
18652 if (cu->per_cu->is_dwz && attr->form_is_ref ())
18653 attr->form = DW_FORM_GNU_ref_alt;
18654
18655 /* We have seen instances where the compiler tried to emit a byte
18656 size attribute of -1 which ended up being encoded as an unsigned
18657 0xffffffff. Although 0xffffffff is technically a valid size value,
18658 an object of this size seems pretty unlikely so we can relatively
18659 safely treat these cases as if the size attribute was invalid and
18660 treat them as zero by default. */
18661 if (attr->name == DW_AT_byte_size
18662 && form == DW_FORM_data4
18663 && DW_UNSND (attr) >= 0xffffffff)
18664 {
18665 complaint
18666 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
18667 hex_string (DW_UNSND (attr)));
18668 DW_UNSND (attr) = 0;
18669 }
18670
18671 return info_ptr;
18672 }
18673
18674 /* Read an attribute described by an abbreviated attribute. */
18675
18676 static const gdb_byte *
18677 read_attribute (const struct die_reader_specs *reader,
18678 struct attribute *attr, struct attr_abbrev *abbrev,
18679 const gdb_byte *info_ptr, bool *need_reprocess)
18680 {
18681 attr->name = abbrev->name;
18682 return read_attribute_value (reader, attr, abbrev->form,
18683 abbrev->implicit_const, info_ptr,
18684 need_reprocess);
18685 }
18686
18687 /* Cover function for read_initial_length.
18688 Returns the length of the object at BUF, and stores the size of the
18689 initial length in *BYTES_READ and stores the size that offsets will be in
18690 *OFFSET_SIZE.
18691 If the initial length size is not equivalent to that specified in
18692 CU_HEADER then issue a complaint.
18693 This is useful when reading non-comp-unit headers. */
18694
18695 static LONGEST
18696 read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
18697 const struct comp_unit_head *cu_header,
18698 unsigned int *bytes_read,
18699 unsigned int *offset_size)
18700 {
18701 LONGEST length = read_initial_length (abfd, buf, bytes_read);
18702
18703 gdb_assert (cu_header->initial_length_size == 4
18704 || cu_header->initial_length_size == 8
18705 || cu_header->initial_length_size == 12);
18706
18707 if (cu_header->initial_length_size != *bytes_read)
18708 complaint (_("intermixed 32-bit and 64-bit DWARF sections"));
18709
18710 *offset_size = (*bytes_read == 4) ? 4 : 8;
18711 return length;
18712 }
18713
18714 /* Return pointer to string at section SECT offset STR_OFFSET with error
18715 reporting strings FORM_NAME and SECT_NAME. */
18716
18717 static const char *
18718 read_indirect_string_at_offset_from (struct objfile *objfile,
18719 bfd *abfd, LONGEST str_offset,
18720 struct dwarf2_section_info *sect,
18721 const char *form_name,
18722 const char *sect_name)
18723 {
18724 sect->read (objfile);
18725 if (sect->buffer == NULL)
18726 error (_("%s used without %s section [in module %s]"),
18727 form_name, sect_name, bfd_get_filename (abfd));
18728 if (str_offset >= sect->size)
18729 error (_("%s pointing outside of %s section [in module %s]"),
18730 form_name, sect_name, bfd_get_filename (abfd));
18731 gdb_assert (HOST_CHAR_BIT == 8);
18732 if (sect->buffer[str_offset] == '\0')
18733 return NULL;
18734 return (const char *) (sect->buffer + str_offset);
18735 }
18736
18737 /* Return pointer to string at .debug_str offset STR_OFFSET. */
18738
18739 static const char *
18740 read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
18741 bfd *abfd, LONGEST str_offset)
18742 {
18743 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
18744 abfd, str_offset,
18745 &dwarf2_per_objfile->str,
18746 "DW_FORM_strp", ".debug_str");
18747 }
18748
18749 /* Return pointer to string at .debug_line_str offset STR_OFFSET. */
18750
18751 static const char *
18752 read_indirect_line_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
18753 bfd *abfd, LONGEST str_offset)
18754 {
18755 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
18756 abfd, str_offset,
18757 &dwarf2_per_objfile->line_str,
18758 "DW_FORM_line_strp",
18759 ".debug_line_str");
18760 }
18761
18762 /* Read a string at offset STR_OFFSET in the .debug_str section from
18763 the .dwz file DWZ. Throw an error if the offset is too large. If
18764 the string consists of a single NUL byte, return NULL; otherwise
18765 return a pointer to the string. */
18766
18767 static const char *
18768 read_indirect_string_from_dwz (struct objfile *objfile, struct dwz_file *dwz,
18769 LONGEST str_offset)
18770 {
18771 dwz->str.read (objfile);
18772
18773 if (dwz->str.buffer == NULL)
18774 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
18775 "section [in module %s]"),
18776 bfd_get_filename (dwz->dwz_bfd.get ()));
18777 if (str_offset >= dwz->str.size)
18778 error (_("DW_FORM_GNU_strp_alt pointing outside of "
18779 ".debug_str section [in module %s]"),
18780 bfd_get_filename (dwz->dwz_bfd.get ()));
18781 gdb_assert (HOST_CHAR_BIT == 8);
18782 if (dwz->str.buffer[str_offset] == '\0')
18783 return NULL;
18784 return (const char *) (dwz->str.buffer + str_offset);
18785 }
18786
18787 /* Return pointer to string at .debug_str offset as read from BUF.
18788 BUF is assumed to be in a compilation unit described by CU_HEADER.
18789 Return *BYTES_READ_PTR count of bytes read from BUF. */
18790
18791 static const char *
18792 read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
18793 const gdb_byte *buf,
18794 const struct comp_unit_head *cu_header,
18795 unsigned int *bytes_read_ptr)
18796 {
18797 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
18798
18799 return read_indirect_string_at_offset (dwarf2_per_objfile, abfd, str_offset);
18800 }
18801
18802 /* Return pointer to string at .debug_line_str offset as read from BUF.
18803 BUF is assumed to be in a compilation unit described by CU_HEADER.
18804 Return *BYTES_READ_PTR count of bytes read from BUF. */
18805
18806 static const char *
18807 read_indirect_line_string (struct dwarf2_per_objfile *dwarf2_per_objfile,
18808 bfd *abfd, const gdb_byte *buf,
18809 const struct comp_unit_head *cu_header,
18810 unsigned int *bytes_read_ptr)
18811 {
18812 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
18813
18814 return read_indirect_line_string_at_offset (dwarf2_per_objfile, abfd,
18815 str_offset);
18816 }
18817
18818 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
18819 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
18820 ADDR_SIZE is the size of addresses from the CU header. */
18821
18822 static CORE_ADDR
18823 read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
18824 unsigned int addr_index, gdb::optional<ULONGEST> addr_base,
18825 int addr_size)
18826 {
18827 struct objfile *objfile = dwarf2_per_objfile->objfile;
18828 bfd *abfd = objfile->obfd;
18829 const gdb_byte *info_ptr;
18830 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
18831
18832 dwarf2_per_objfile->addr.read (objfile);
18833 if (dwarf2_per_objfile->addr.buffer == NULL)
18834 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
18835 objfile_name (objfile));
18836 if (addr_base_or_zero + addr_index * addr_size
18837 >= dwarf2_per_objfile->addr.size)
18838 error (_("DW_FORM_addr_index pointing outside of "
18839 ".debug_addr section [in module %s]"),
18840 objfile_name (objfile));
18841 info_ptr = (dwarf2_per_objfile->addr.buffer
18842 + addr_base_or_zero + addr_index * addr_size);
18843 if (addr_size == 4)
18844 return bfd_get_32 (abfd, info_ptr);
18845 else
18846 return bfd_get_64 (abfd, info_ptr);
18847 }
18848
18849 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
18850
18851 static CORE_ADDR
18852 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
18853 {
18854 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
18855 cu->addr_base, cu->header.addr_size);
18856 }
18857
18858 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
18859
18860 static CORE_ADDR
18861 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
18862 unsigned int *bytes_read)
18863 {
18864 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
18865 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
18866
18867 return read_addr_index (cu, addr_index);
18868 }
18869
18870 /* Given an index in .debug_addr, fetch the value.
18871 NOTE: This can be called during dwarf expression evaluation,
18872 long after the debug information has been read, and thus per_cu->cu
18873 may no longer exist. */
18874
18875 CORE_ADDR
18876 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
18877 unsigned int addr_index)
18878 {
18879 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
18880 struct dwarf2_cu *cu = per_cu->cu;
18881 gdb::optional<ULONGEST> addr_base;
18882 int addr_size;
18883
18884 /* We need addr_base and addr_size.
18885 If we don't have PER_CU->cu, we have to get it.
18886 Nasty, but the alternative is storing the needed info in PER_CU,
18887 which at this point doesn't seem justified: it's not clear how frequently
18888 it would get used and it would increase the size of every PER_CU.
18889 Entry points like dwarf2_per_cu_addr_size do a similar thing
18890 so we're not in uncharted territory here.
18891 Alas we need to be a bit more complicated as addr_base is contained
18892 in the DIE.
18893
18894 We don't need to read the entire CU(/TU).
18895 We just need the header and top level die.
18896
18897 IWBN to use the aging mechanism to let us lazily later discard the CU.
18898 For now we skip this optimization. */
18899
18900 if (cu != NULL)
18901 {
18902 addr_base = cu->addr_base;
18903 addr_size = cu->header.addr_size;
18904 }
18905 else
18906 {
18907 cutu_reader reader (per_cu, NULL, 0, false);
18908 addr_base = reader.cu->addr_base;
18909 addr_size = reader.cu->header.addr_size;
18910 }
18911
18912 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
18913 addr_size);
18914 }
18915
18916 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
18917 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
18918 DWO file. */
18919
18920 static const char *
18921 read_str_index (struct dwarf2_cu *cu,
18922 struct dwarf2_section_info *str_section,
18923 struct dwarf2_section_info *str_offsets_section,
18924 ULONGEST str_offsets_base, ULONGEST str_index)
18925 {
18926 struct dwarf2_per_objfile *dwarf2_per_objfile
18927 = cu->per_cu->dwarf2_per_objfile;
18928 struct objfile *objfile = dwarf2_per_objfile->objfile;
18929 const char *objf_name = objfile_name (objfile);
18930 bfd *abfd = objfile->obfd;
18931 const gdb_byte *info_ptr;
18932 ULONGEST str_offset;
18933 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
18934
18935 str_section->read (objfile);
18936 str_offsets_section->read (objfile);
18937 if (str_section->buffer == NULL)
18938 error (_("%s used without %s section"
18939 " in CU at offset %s [in module %s]"),
18940 form_name, str_section->get_name (),
18941 sect_offset_str (cu->header.sect_off), objf_name);
18942 if (str_offsets_section->buffer == NULL)
18943 error (_("%s used without %s section"
18944 " in CU at offset %s [in module %s]"),
18945 form_name, str_section->get_name (),
18946 sect_offset_str (cu->header.sect_off), objf_name);
18947 info_ptr = (str_offsets_section->buffer
18948 + str_offsets_base
18949 + str_index * cu->header.offset_size);
18950 if (cu->header.offset_size == 4)
18951 str_offset = bfd_get_32 (abfd, info_ptr);
18952 else
18953 str_offset = bfd_get_64 (abfd, info_ptr);
18954 if (str_offset >= str_section->size)
18955 error (_("Offset from %s pointing outside of"
18956 " .debug_str.dwo section in CU at offset %s [in module %s]"),
18957 form_name, sect_offset_str (cu->header.sect_off), objf_name);
18958 return (const char *) (str_section->buffer + str_offset);
18959 }
18960
18961 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
18962
18963 static const char *
18964 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
18965 {
18966 ULONGEST str_offsets_base = reader->cu->header.version >= 5
18967 ? reader->cu->header.addr_size : 0;
18968 return read_str_index (reader->cu,
18969 &reader->dwo_file->sections.str,
18970 &reader->dwo_file->sections.str_offsets,
18971 str_offsets_base, str_index);
18972 }
18973
18974 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
18975
18976 static const char *
18977 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
18978 {
18979 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18980 const char *objf_name = objfile_name (objfile);
18981 static const char form_name[] = "DW_FORM_GNU_str_index";
18982 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
18983
18984 if (!cu->str_offsets_base.has_value ())
18985 error (_("%s used in Fission stub without %s"
18986 " in CU at offset 0x%lx [in module %s]"),
18987 form_name, str_offsets_attr_name,
18988 (long) cu->header.offset_size, objf_name);
18989
18990 return read_str_index (cu,
18991 &cu->per_cu->dwarf2_per_objfile->str,
18992 &cu->per_cu->dwarf2_per_objfile->str_offsets,
18993 *cu->str_offsets_base, str_index);
18994 }
18995
18996 /* Return the length of an LEB128 number in BUF. */
18997
18998 static int
18999 leb128_size (const gdb_byte *buf)
19000 {
19001 const gdb_byte *begin = buf;
19002 gdb_byte byte;
19003
19004 while (1)
19005 {
19006 byte = *buf++;
19007 if ((byte & 128) == 0)
19008 return buf - begin;
19009 }
19010 }
19011
19012 static void
19013 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
19014 {
19015 switch (lang)
19016 {
19017 case DW_LANG_C89:
19018 case DW_LANG_C99:
19019 case DW_LANG_C11:
19020 case DW_LANG_C:
19021 case DW_LANG_UPC:
19022 cu->language = language_c;
19023 break;
19024 case DW_LANG_Java:
19025 case DW_LANG_C_plus_plus:
19026 case DW_LANG_C_plus_plus_11:
19027 case DW_LANG_C_plus_plus_14:
19028 cu->language = language_cplus;
19029 break;
19030 case DW_LANG_D:
19031 cu->language = language_d;
19032 break;
19033 case DW_LANG_Fortran77:
19034 case DW_LANG_Fortran90:
19035 case DW_LANG_Fortran95:
19036 case DW_LANG_Fortran03:
19037 case DW_LANG_Fortran08:
19038 cu->language = language_fortran;
19039 break;
19040 case DW_LANG_Go:
19041 cu->language = language_go;
19042 break;
19043 case DW_LANG_Mips_Assembler:
19044 cu->language = language_asm;
19045 break;
19046 case DW_LANG_Ada83:
19047 case DW_LANG_Ada95:
19048 cu->language = language_ada;
19049 break;
19050 case DW_LANG_Modula2:
19051 cu->language = language_m2;
19052 break;
19053 case DW_LANG_Pascal83:
19054 cu->language = language_pascal;
19055 break;
19056 case DW_LANG_ObjC:
19057 cu->language = language_objc;
19058 break;
19059 case DW_LANG_Rust:
19060 case DW_LANG_Rust_old:
19061 cu->language = language_rust;
19062 break;
19063 case DW_LANG_Cobol74:
19064 case DW_LANG_Cobol85:
19065 default:
19066 cu->language = language_minimal;
19067 break;
19068 }
19069 cu->language_defn = language_def (cu->language);
19070 }
19071
19072 /* Return the named attribute or NULL if not there. */
19073
19074 static struct attribute *
19075 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19076 {
19077 for (;;)
19078 {
19079 unsigned int i;
19080 struct attribute *spec = NULL;
19081
19082 for (i = 0; i < die->num_attrs; ++i)
19083 {
19084 if (die->attrs[i].name == name)
19085 return &die->attrs[i];
19086 if (die->attrs[i].name == DW_AT_specification
19087 || die->attrs[i].name == DW_AT_abstract_origin)
19088 spec = &die->attrs[i];
19089 }
19090
19091 if (!spec)
19092 break;
19093
19094 die = follow_die_ref (die, spec, &cu);
19095 }
19096
19097 return NULL;
19098 }
19099
19100 /* Return the named attribute or NULL if not there,
19101 but do not follow DW_AT_specification, etc.
19102 This is for use in contexts where we're reading .debug_types dies.
19103 Following DW_AT_specification, DW_AT_abstract_origin will take us
19104 back up the chain, and we want to go down. */
19105
19106 static struct attribute *
19107 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
19108 {
19109 unsigned int i;
19110
19111 for (i = 0; i < die->num_attrs; ++i)
19112 if (die->attrs[i].name == name)
19113 return &die->attrs[i];
19114
19115 return NULL;
19116 }
19117
19118 /* Return the string associated with a string-typed attribute, or NULL if it
19119 is either not found or is of an incorrect type. */
19120
19121 static const char *
19122 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19123 {
19124 struct attribute *attr;
19125 const char *str = NULL;
19126
19127 attr = dwarf2_attr (die, name, cu);
19128
19129 if (attr != NULL)
19130 {
19131 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
19132 || attr->form == DW_FORM_string
19133 || attr->form == DW_FORM_strx
19134 || attr->form == DW_FORM_strx1
19135 || attr->form == DW_FORM_strx2
19136 || attr->form == DW_FORM_strx3
19137 || attr->form == DW_FORM_strx4
19138 || attr->form == DW_FORM_GNU_str_index
19139 || attr->form == DW_FORM_GNU_strp_alt)
19140 str = DW_STRING (attr);
19141 else
19142 complaint (_("string type expected for attribute %s for "
19143 "DIE at %s in module %s"),
19144 dwarf_attr_name (name), sect_offset_str (die->sect_off),
19145 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
19146 }
19147
19148 return str;
19149 }
19150
19151 /* Return the dwo name or NULL if not present. If present, it is in either
19152 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
19153 static const char *
19154 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
19155 {
19156 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
19157 if (dwo_name == nullptr)
19158 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
19159 return dwo_name;
19160 }
19161
19162 /* Return non-zero iff the attribute NAME is defined for the given DIE,
19163 and holds a non-zero value. This function should only be used for
19164 DW_FORM_flag or DW_FORM_flag_present attributes. */
19165
19166 static int
19167 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19168 {
19169 struct attribute *attr = dwarf2_attr (die, name, cu);
19170
19171 return (attr && DW_UNSND (attr));
19172 }
19173
19174 static int
19175 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
19176 {
19177 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19178 which value is non-zero. However, we have to be careful with
19179 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19180 (via dwarf2_flag_true_p) follows this attribute. So we may
19181 end up accidently finding a declaration attribute that belongs
19182 to a different DIE referenced by the specification attribute,
19183 even though the given DIE does not have a declaration attribute. */
19184 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19185 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
19186 }
19187
19188 /* Return the die giving the specification for DIE, if there is
19189 one. *SPEC_CU is the CU containing DIE on input, and the CU
19190 containing the return value on output. If there is no
19191 specification, but there is an abstract origin, that is
19192 returned. */
19193
19194 static struct die_info *
19195 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
19196 {
19197 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19198 *spec_cu);
19199
19200 if (spec_attr == NULL)
19201 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19202
19203 if (spec_attr == NULL)
19204 return NULL;
19205 else
19206 return follow_die_ref (die, spec_attr, spec_cu);
19207 }
19208
19209 /* Stub for free_line_header to match void * callback types. */
19210
19211 static void
19212 free_line_header_voidp (void *arg)
19213 {
19214 struct line_header *lh = (struct line_header *) arg;
19215
19216 delete lh;
19217 }
19218
19219 /* A convenience function to find the proper .debug_line section for a CU. */
19220
19221 static struct dwarf2_section_info *
19222 get_debug_line_section (struct dwarf2_cu *cu)
19223 {
19224 struct dwarf2_section_info *section;
19225 struct dwarf2_per_objfile *dwarf2_per_objfile
19226 = cu->per_cu->dwarf2_per_objfile;
19227
19228 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19229 DWO file. */
19230 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19231 section = &cu->dwo_unit->dwo_file->sections.line;
19232 else if (cu->per_cu->is_dwz)
19233 {
19234 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
19235
19236 section = &dwz->line;
19237 }
19238 else
19239 section = &dwarf2_per_objfile->line;
19240
19241 return section;
19242 }
19243
19244 /* Read directory or file name entry format, starting with byte of
19245 format count entries, ULEB128 pairs of entry formats, ULEB128 of
19246 entries count and the entries themselves in the described entry
19247 format. */
19248
19249 static void
19250 read_formatted_entries (struct dwarf2_per_objfile *dwarf2_per_objfile,
19251 bfd *abfd, const gdb_byte **bufp,
19252 struct line_header *lh,
19253 const struct comp_unit_head *cu_header,
19254 void (*callback) (struct line_header *lh,
19255 const char *name,
19256 dir_index d_index,
19257 unsigned int mod_time,
19258 unsigned int length))
19259 {
19260 gdb_byte format_count, formati;
19261 ULONGEST data_count, datai;
19262 const gdb_byte *buf = *bufp;
19263 const gdb_byte *format_header_data;
19264 unsigned int bytes_read;
19265
19266 format_count = read_1_byte (abfd, buf);
19267 buf += 1;
19268 format_header_data = buf;
19269 for (formati = 0; formati < format_count; formati++)
19270 {
19271 read_unsigned_leb128 (abfd, buf, &bytes_read);
19272 buf += bytes_read;
19273 read_unsigned_leb128 (abfd, buf, &bytes_read);
19274 buf += bytes_read;
19275 }
19276
19277 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
19278 buf += bytes_read;
19279 for (datai = 0; datai < data_count; datai++)
19280 {
19281 const gdb_byte *format = format_header_data;
19282 struct file_entry fe;
19283
19284 for (formati = 0; formati < format_count; formati++)
19285 {
19286 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
19287 format += bytes_read;
19288
19289 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
19290 format += bytes_read;
19291
19292 gdb::optional<const char *> string;
19293 gdb::optional<unsigned int> uint;
19294
19295 switch (form)
19296 {
19297 case DW_FORM_string:
19298 string.emplace (read_direct_string (abfd, buf, &bytes_read));
19299 buf += bytes_read;
19300 break;
19301
19302 case DW_FORM_line_strp:
19303 string.emplace (read_indirect_line_string (dwarf2_per_objfile,
19304 abfd, buf,
19305 cu_header,
19306 &bytes_read));
19307 buf += bytes_read;
19308 break;
19309
19310 case DW_FORM_data1:
19311 uint.emplace (read_1_byte (abfd, buf));
19312 buf += 1;
19313 break;
19314
19315 case DW_FORM_data2:
19316 uint.emplace (read_2_bytes (abfd, buf));
19317 buf += 2;
19318 break;
19319
19320 case DW_FORM_data4:
19321 uint.emplace (read_4_bytes (abfd, buf));
19322 buf += 4;
19323 break;
19324
19325 case DW_FORM_data8:
19326 uint.emplace (read_8_bytes (abfd, buf));
19327 buf += 8;
19328 break;
19329
19330 case DW_FORM_data16:
19331 /* This is used for MD5, but file_entry does not record MD5s. */
19332 buf += 16;
19333 break;
19334
19335 case DW_FORM_udata:
19336 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
19337 buf += bytes_read;
19338 break;
19339
19340 case DW_FORM_block:
19341 /* It is valid only for DW_LNCT_timestamp which is ignored by
19342 current GDB. */
19343 break;
19344 }
19345
19346 switch (content_type)
19347 {
19348 case DW_LNCT_path:
19349 if (string.has_value ())
19350 fe.name = *string;
19351 break;
19352 case DW_LNCT_directory_index:
19353 if (uint.has_value ())
19354 fe.d_index = (dir_index) *uint;
19355 break;
19356 case DW_LNCT_timestamp:
19357 if (uint.has_value ())
19358 fe.mod_time = *uint;
19359 break;
19360 case DW_LNCT_size:
19361 if (uint.has_value ())
19362 fe.length = *uint;
19363 break;
19364 case DW_LNCT_MD5:
19365 break;
19366 default:
19367 complaint (_("Unknown format content type %s"),
19368 pulongest (content_type));
19369 }
19370 }
19371
19372 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
19373 }
19374
19375 *bufp = buf;
19376 }
19377
19378 /* Read the statement program header starting at OFFSET in
19379 .debug_line, or .debug_line.dwo. Return a pointer
19380 to a struct line_header, allocated using xmalloc.
19381 Returns NULL if there is a problem reading the header, e.g., if it
19382 has a version we don't understand.
19383
19384 NOTE: the strings in the include directory and file name tables of
19385 the returned object point into the dwarf line section buffer,
19386 and must not be freed. */
19387
19388 static line_header_up
19389 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
19390 {
19391 const gdb_byte *line_ptr;
19392 unsigned int bytes_read, offset_size;
19393 int i;
19394 const char *cur_dir, *cur_file;
19395 struct dwarf2_section_info *section;
19396 bfd *abfd;
19397 struct dwarf2_per_objfile *dwarf2_per_objfile
19398 = cu->per_cu->dwarf2_per_objfile;
19399
19400 section = get_debug_line_section (cu);
19401 section->read (dwarf2_per_objfile->objfile);
19402 if (section->buffer == NULL)
19403 {
19404 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19405 complaint (_("missing .debug_line.dwo section"));
19406 else
19407 complaint (_("missing .debug_line section"));
19408 return 0;
19409 }
19410
19411 /* We can't do this until we know the section is non-empty.
19412 Only then do we know we have such a section. */
19413 abfd = section->get_bfd_owner ();
19414
19415 /* Make sure that at least there's room for the total_length field.
19416 That could be 12 bytes long, but we're just going to fudge that. */
19417 if (to_underlying (sect_off) + 4 >= section->size)
19418 {
19419 dwarf2_statement_list_fits_in_line_number_section_complaint ();
19420 return 0;
19421 }
19422
19423 line_header_up lh (new line_header ());
19424
19425 lh->sect_off = sect_off;
19426 lh->offset_in_dwz = cu->per_cu->is_dwz;
19427
19428 line_ptr = section->buffer + to_underlying (sect_off);
19429
19430 /* Read in the header. */
19431 lh->total_length =
19432 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
19433 &bytes_read, &offset_size);
19434 line_ptr += bytes_read;
19435
19436 const gdb_byte *start_here = line_ptr;
19437
19438 if (line_ptr + lh->total_length > (section->buffer + section->size))
19439 {
19440 dwarf2_statement_list_fits_in_line_number_section_complaint ();
19441 return 0;
19442 }
19443 lh->statement_program_end = start_here + lh->total_length;
19444 lh->version = read_2_bytes (abfd, line_ptr);
19445 line_ptr += 2;
19446 if (lh->version > 5)
19447 {
19448 /* This is a version we don't understand. The format could have
19449 changed in ways we don't handle properly so just punt. */
19450 complaint (_("unsupported version in .debug_line section"));
19451 return NULL;
19452 }
19453 if (lh->version >= 5)
19454 {
19455 gdb_byte segment_selector_size;
19456
19457 /* Skip address size. */
19458 read_1_byte (abfd, line_ptr);
19459 line_ptr += 1;
19460
19461 segment_selector_size = read_1_byte (abfd, line_ptr);
19462 line_ptr += 1;
19463 if (segment_selector_size != 0)
19464 {
19465 complaint (_("unsupported segment selector size %u "
19466 "in .debug_line section"),
19467 segment_selector_size);
19468 return NULL;
19469 }
19470 }
19471 lh->header_length = read_offset (abfd, line_ptr, offset_size);
19472 line_ptr += offset_size;
19473 lh->statement_program_start = line_ptr + lh->header_length;
19474 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
19475 line_ptr += 1;
19476 if (lh->version >= 4)
19477 {
19478 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
19479 line_ptr += 1;
19480 }
19481 else
19482 lh->maximum_ops_per_instruction = 1;
19483
19484 if (lh->maximum_ops_per_instruction == 0)
19485 {
19486 lh->maximum_ops_per_instruction = 1;
19487 complaint (_("invalid maximum_ops_per_instruction "
19488 "in `.debug_line' section"));
19489 }
19490
19491 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
19492 line_ptr += 1;
19493 lh->line_base = read_1_signed_byte (abfd, line_ptr);
19494 line_ptr += 1;
19495 lh->line_range = read_1_byte (abfd, line_ptr);
19496 line_ptr += 1;
19497 lh->opcode_base = read_1_byte (abfd, line_ptr);
19498 line_ptr += 1;
19499 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
19500
19501 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
19502 for (i = 1; i < lh->opcode_base; ++i)
19503 {
19504 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
19505 line_ptr += 1;
19506 }
19507
19508 if (lh->version >= 5)
19509 {
19510 /* Read directory table. */
19511 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
19512 &cu->header,
19513 [] (struct line_header *header, const char *name,
19514 dir_index d_index, unsigned int mod_time,
19515 unsigned int length)
19516 {
19517 header->add_include_dir (name);
19518 });
19519
19520 /* Read file name table. */
19521 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
19522 &cu->header,
19523 [] (struct line_header *header, const char *name,
19524 dir_index d_index, unsigned int mod_time,
19525 unsigned int length)
19526 {
19527 header->add_file_name (name, d_index, mod_time, length);
19528 });
19529 }
19530 else
19531 {
19532 /* Read directory table. */
19533 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
19534 {
19535 line_ptr += bytes_read;
19536 lh->add_include_dir (cur_dir);
19537 }
19538 line_ptr += bytes_read;
19539
19540 /* Read file name table. */
19541 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
19542 {
19543 unsigned int mod_time, length;
19544 dir_index d_index;
19545
19546 line_ptr += bytes_read;
19547 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19548 line_ptr += bytes_read;
19549 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19550 line_ptr += bytes_read;
19551 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19552 line_ptr += bytes_read;
19553
19554 lh->add_file_name (cur_file, d_index, mod_time, length);
19555 }
19556 line_ptr += bytes_read;
19557 }
19558
19559 if (line_ptr > (section->buffer + section->size))
19560 complaint (_("line number info header doesn't "
19561 "fit in `.debug_line' section"));
19562
19563 return lh;
19564 }
19565
19566 /* Subroutine of dwarf_decode_lines to simplify it.
19567 Return the file name of the psymtab for the given file_entry.
19568 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
19569 If space for the result is malloc'd, *NAME_HOLDER will be set.
19570 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
19571
19572 static const char *
19573 psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
19574 const dwarf2_psymtab *pst,
19575 const char *comp_dir,
19576 gdb::unique_xmalloc_ptr<char> *name_holder)
19577 {
19578 const char *include_name = fe.name;
19579 const char *include_name_to_compare = include_name;
19580 const char *pst_filename;
19581 int file_is_pst;
19582
19583 const char *dir_name = fe.include_dir (lh);
19584
19585 gdb::unique_xmalloc_ptr<char> hold_compare;
19586 if (!IS_ABSOLUTE_PATH (include_name)
19587 && (dir_name != NULL || comp_dir != NULL))
19588 {
19589 /* Avoid creating a duplicate psymtab for PST.
19590 We do this by comparing INCLUDE_NAME and PST_FILENAME.
19591 Before we do the comparison, however, we need to account
19592 for DIR_NAME and COMP_DIR.
19593 First prepend dir_name (if non-NULL). If we still don't
19594 have an absolute path prepend comp_dir (if non-NULL).
19595 However, the directory we record in the include-file's
19596 psymtab does not contain COMP_DIR (to match the
19597 corresponding symtab(s)).
19598
19599 Example:
19600
19601 bash$ cd /tmp
19602 bash$ gcc -g ./hello.c
19603 include_name = "hello.c"
19604 dir_name = "."
19605 DW_AT_comp_dir = comp_dir = "/tmp"
19606 DW_AT_name = "./hello.c"
19607
19608 */
19609
19610 if (dir_name != NULL)
19611 {
19612 name_holder->reset (concat (dir_name, SLASH_STRING,
19613 include_name, (char *) NULL));
19614 include_name = name_holder->get ();
19615 include_name_to_compare = include_name;
19616 }
19617 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
19618 {
19619 hold_compare.reset (concat (comp_dir, SLASH_STRING,
19620 include_name, (char *) NULL));
19621 include_name_to_compare = hold_compare.get ();
19622 }
19623 }
19624
19625 pst_filename = pst->filename;
19626 gdb::unique_xmalloc_ptr<char> copied_name;
19627 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
19628 {
19629 copied_name.reset (concat (pst->dirname, SLASH_STRING,
19630 pst_filename, (char *) NULL));
19631 pst_filename = copied_name.get ();
19632 }
19633
19634 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
19635
19636 if (file_is_pst)
19637 return NULL;
19638 return include_name;
19639 }
19640
19641 /* State machine to track the state of the line number program. */
19642
19643 class lnp_state_machine
19644 {
19645 public:
19646 /* Initialize a machine state for the start of a line number
19647 program. */
19648 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
19649 bool record_lines_p);
19650
19651 file_entry *current_file ()
19652 {
19653 /* lh->file_names is 0-based, but the file name numbers in the
19654 statement program are 1-based. */
19655 return m_line_header->file_name_at (m_file);
19656 }
19657
19658 /* Record the line in the state machine. END_SEQUENCE is true if
19659 we're processing the end of a sequence. */
19660 void record_line (bool end_sequence);
19661
19662 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
19663 nop-out rest of the lines in this sequence. */
19664 void check_line_address (struct dwarf2_cu *cu,
19665 const gdb_byte *line_ptr,
19666 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
19667
19668 void handle_set_discriminator (unsigned int discriminator)
19669 {
19670 m_discriminator = discriminator;
19671 m_line_has_non_zero_discriminator |= discriminator != 0;
19672 }
19673
19674 /* Handle DW_LNE_set_address. */
19675 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
19676 {
19677 m_op_index = 0;
19678 address += baseaddr;
19679 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
19680 }
19681
19682 /* Handle DW_LNS_advance_pc. */
19683 void handle_advance_pc (CORE_ADDR adjust);
19684
19685 /* Handle a special opcode. */
19686 void handle_special_opcode (unsigned char op_code);
19687
19688 /* Handle DW_LNS_advance_line. */
19689 void handle_advance_line (int line_delta)
19690 {
19691 advance_line (line_delta);
19692 }
19693
19694 /* Handle DW_LNS_set_file. */
19695 void handle_set_file (file_name_index file);
19696
19697 /* Handle DW_LNS_negate_stmt. */
19698 void handle_negate_stmt ()
19699 {
19700 m_is_stmt = !m_is_stmt;
19701 }
19702
19703 /* Handle DW_LNS_const_add_pc. */
19704 void handle_const_add_pc ();
19705
19706 /* Handle DW_LNS_fixed_advance_pc. */
19707 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
19708 {
19709 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19710 m_op_index = 0;
19711 }
19712
19713 /* Handle DW_LNS_copy. */
19714 void handle_copy ()
19715 {
19716 record_line (false);
19717 m_discriminator = 0;
19718 }
19719
19720 /* Handle DW_LNE_end_sequence. */
19721 void handle_end_sequence ()
19722 {
19723 m_currently_recording_lines = true;
19724 }
19725
19726 private:
19727 /* Advance the line by LINE_DELTA. */
19728 void advance_line (int line_delta)
19729 {
19730 m_line += line_delta;
19731
19732 if (line_delta != 0)
19733 m_line_has_non_zero_discriminator = m_discriminator != 0;
19734 }
19735
19736 struct dwarf2_cu *m_cu;
19737
19738 gdbarch *m_gdbarch;
19739
19740 /* True if we're recording lines.
19741 Otherwise we're building partial symtabs and are just interested in
19742 finding include files mentioned by the line number program. */
19743 bool m_record_lines_p;
19744
19745 /* The line number header. */
19746 line_header *m_line_header;
19747
19748 /* These are part of the standard DWARF line number state machine,
19749 and initialized according to the DWARF spec. */
19750
19751 unsigned char m_op_index = 0;
19752 /* The line table index of the current file. */
19753 file_name_index m_file = 1;
19754 unsigned int m_line = 1;
19755
19756 /* These are initialized in the constructor. */
19757
19758 CORE_ADDR m_address;
19759 bool m_is_stmt;
19760 unsigned int m_discriminator;
19761
19762 /* Additional bits of state we need to track. */
19763
19764 /* The last file that we called dwarf2_start_subfile for.
19765 This is only used for TLLs. */
19766 unsigned int m_last_file = 0;
19767 /* The last file a line number was recorded for. */
19768 struct subfile *m_last_subfile = NULL;
19769
19770 /* When true, record the lines we decode. */
19771 bool m_currently_recording_lines = false;
19772
19773 /* The last line number that was recorded, used to coalesce
19774 consecutive entries for the same line. This can happen, for
19775 example, when discriminators are present. PR 17276. */
19776 unsigned int m_last_line = 0;
19777 bool m_line_has_non_zero_discriminator = false;
19778 };
19779
19780 void
19781 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
19782 {
19783 CORE_ADDR addr_adj = (((m_op_index + adjust)
19784 / m_line_header->maximum_ops_per_instruction)
19785 * m_line_header->minimum_instruction_length);
19786 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19787 m_op_index = ((m_op_index + adjust)
19788 % m_line_header->maximum_ops_per_instruction);
19789 }
19790
19791 void
19792 lnp_state_machine::handle_special_opcode (unsigned char op_code)
19793 {
19794 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
19795 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
19796 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
19797 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
19798 / m_line_header->maximum_ops_per_instruction)
19799 * m_line_header->minimum_instruction_length);
19800 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19801 m_op_index = ((m_op_index + adj_opcode_d)
19802 % m_line_header->maximum_ops_per_instruction);
19803
19804 int line_delta = m_line_header->line_base + adj_opcode_r;
19805 advance_line (line_delta);
19806 record_line (false);
19807 m_discriminator = 0;
19808 }
19809
19810 void
19811 lnp_state_machine::handle_set_file (file_name_index file)
19812 {
19813 m_file = file;
19814
19815 const file_entry *fe = current_file ();
19816 if (fe == NULL)
19817 dwarf2_debug_line_missing_file_complaint ();
19818 else if (m_record_lines_p)
19819 {
19820 const char *dir = fe->include_dir (m_line_header);
19821
19822 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
19823 m_line_has_non_zero_discriminator = m_discriminator != 0;
19824 dwarf2_start_subfile (m_cu, fe->name, dir);
19825 }
19826 }
19827
19828 void
19829 lnp_state_machine::handle_const_add_pc ()
19830 {
19831 CORE_ADDR adjust
19832 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
19833
19834 CORE_ADDR addr_adj
19835 = (((m_op_index + adjust)
19836 / m_line_header->maximum_ops_per_instruction)
19837 * m_line_header->minimum_instruction_length);
19838
19839 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19840 m_op_index = ((m_op_index + adjust)
19841 % m_line_header->maximum_ops_per_instruction);
19842 }
19843
19844 /* Return non-zero if we should add LINE to the line number table.
19845 LINE is the line to add, LAST_LINE is the last line that was added,
19846 LAST_SUBFILE is the subfile for LAST_LINE.
19847 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
19848 had a non-zero discriminator.
19849
19850 We have to be careful in the presence of discriminators.
19851 E.g., for this line:
19852
19853 for (i = 0; i < 100000; i++);
19854
19855 clang can emit four line number entries for that one line,
19856 each with a different discriminator.
19857 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
19858
19859 However, we want gdb to coalesce all four entries into one.
19860 Otherwise the user could stepi into the middle of the line and
19861 gdb would get confused about whether the pc really was in the
19862 middle of the line.
19863
19864 Things are further complicated by the fact that two consecutive
19865 line number entries for the same line is a heuristic used by gcc
19866 to denote the end of the prologue. So we can't just discard duplicate
19867 entries, we have to be selective about it. The heuristic we use is
19868 that we only collapse consecutive entries for the same line if at least
19869 one of those entries has a non-zero discriminator. PR 17276.
19870
19871 Note: Addresses in the line number state machine can never go backwards
19872 within one sequence, thus this coalescing is ok. */
19873
19874 static int
19875 dwarf_record_line_p (struct dwarf2_cu *cu,
19876 unsigned int line, unsigned int last_line,
19877 int line_has_non_zero_discriminator,
19878 struct subfile *last_subfile)
19879 {
19880 if (cu->get_builder ()->get_current_subfile () != last_subfile)
19881 return 1;
19882 if (line != last_line)
19883 return 1;
19884 /* Same line for the same file that we've seen already.
19885 As a last check, for pr 17276, only record the line if the line
19886 has never had a non-zero discriminator. */
19887 if (!line_has_non_zero_discriminator)
19888 return 1;
19889 return 0;
19890 }
19891
19892 /* Use the CU's builder to record line number LINE beginning at
19893 address ADDRESS in the line table of subfile SUBFILE. */
19894
19895 static void
19896 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
19897 unsigned int line, CORE_ADDR address,
19898 struct dwarf2_cu *cu)
19899 {
19900 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
19901
19902 if (dwarf_line_debug)
19903 {
19904 fprintf_unfiltered (gdb_stdlog,
19905 "Recording line %u, file %s, address %s\n",
19906 line, lbasename (subfile->name),
19907 paddress (gdbarch, address));
19908 }
19909
19910 if (cu != nullptr)
19911 cu->get_builder ()->record_line (subfile, line, addr);
19912 }
19913
19914 /* Subroutine of dwarf_decode_lines_1 to simplify it.
19915 Mark the end of a set of line number records.
19916 The arguments are the same as for dwarf_record_line_1.
19917 If SUBFILE is NULL the request is ignored. */
19918
19919 static void
19920 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
19921 CORE_ADDR address, struct dwarf2_cu *cu)
19922 {
19923 if (subfile == NULL)
19924 return;
19925
19926 if (dwarf_line_debug)
19927 {
19928 fprintf_unfiltered (gdb_stdlog,
19929 "Finishing current line, file %s, address %s\n",
19930 lbasename (subfile->name),
19931 paddress (gdbarch, address));
19932 }
19933
19934 dwarf_record_line_1 (gdbarch, subfile, 0, address, cu);
19935 }
19936
19937 void
19938 lnp_state_machine::record_line (bool end_sequence)
19939 {
19940 if (dwarf_line_debug)
19941 {
19942 fprintf_unfiltered (gdb_stdlog,
19943 "Processing actual line %u: file %u,"
19944 " address %s, is_stmt %u, discrim %u%s\n",
19945 m_line, m_file,
19946 paddress (m_gdbarch, m_address),
19947 m_is_stmt, m_discriminator,
19948 (end_sequence ? "\t(end sequence)" : ""));
19949 }
19950
19951 file_entry *fe = current_file ();
19952
19953 if (fe == NULL)
19954 dwarf2_debug_line_missing_file_complaint ();
19955 /* For now we ignore lines not starting on an instruction boundary.
19956 But not when processing end_sequence for compatibility with the
19957 previous version of the code. */
19958 else if (m_op_index == 0 || end_sequence)
19959 {
19960 fe->included_p = 1;
19961 if (m_record_lines_p
19962 && (producer_is_codewarrior (m_cu) || m_is_stmt || end_sequence))
19963 {
19964 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
19965 || end_sequence)
19966 {
19967 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
19968 m_currently_recording_lines ? m_cu : nullptr);
19969 }
19970
19971 if (!end_sequence)
19972 {
19973 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
19974 m_line_has_non_zero_discriminator,
19975 m_last_subfile))
19976 {
19977 buildsym_compunit *builder = m_cu->get_builder ();
19978 dwarf_record_line_1 (m_gdbarch,
19979 builder->get_current_subfile (),
19980 m_line, m_address,
19981 m_currently_recording_lines ? m_cu : nullptr);
19982 }
19983 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
19984 m_last_line = m_line;
19985 }
19986 }
19987 }
19988 }
19989
19990 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
19991 line_header *lh, bool record_lines_p)
19992 {
19993 m_cu = cu;
19994 m_gdbarch = arch;
19995 m_record_lines_p = record_lines_p;
19996 m_line_header = lh;
19997
19998 m_currently_recording_lines = true;
19999
20000 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20001 was a line entry for it so that the backend has a chance to adjust it
20002 and also record it in case it needs it. This is currently used by MIPS
20003 code, cf. `mips_adjust_dwarf2_line'. */
20004 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20005 m_is_stmt = lh->default_is_stmt;
20006 m_discriminator = 0;
20007 }
20008
20009 void
20010 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20011 const gdb_byte *line_ptr,
20012 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
20013 {
20014 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
20015 the pc range of the CU. However, we restrict the test to only ADDRESS
20016 values of zero to preserve GDB's previous behaviour which is to handle
20017 the specific case of a function being GC'd by the linker. */
20018
20019 if (address == 0 && address < unrelocated_lowpc)
20020 {
20021 /* This line table is for a function which has been
20022 GCd by the linker. Ignore it. PR gdb/12528 */
20023
20024 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20025 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20026
20027 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
20028 line_offset, objfile_name (objfile));
20029 m_currently_recording_lines = false;
20030 /* Note: m_currently_recording_lines is left as false until we see
20031 DW_LNE_end_sequence. */
20032 }
20033 }
20034
20035 /* Subroutine of dwarf_decode_lines to simplify it.
20036 Process the line number information in LH.
20037 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20038 program in order to set included_p for every referenced header. */
20039
20040 static void
20041 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20042 const int decode_for_pst_p, CORE_ADDR lowpc)
20043 {
20044 const gdb_byte *line_ptr, *extended_end;
20045 const gdb_byte *line_end;
20046 unsigned int bytes_read, extended_len;
20047 unsigned char op_code, extended_op;
20048 CORE_ADDR baseaddr;
20049 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20050 bfd *abfd = objfile->obfd;
20051 struct gdbarch *gdbarch = get_objfile_arch (objfile);
20052 /* True if we're recording line info (as opposed to building partial
20053 symtabs and just interested in finding include files mentioned by
20054 the line number program). */
20055 bool record_lines_p = !decode_for_pst_p;
20056
20057 baseaddr = objfile->text_section_offset ();
20058
20059 line_ptr = lh->statement_program_start;
20060 line_end = lh->statement_program_end;
20061
20062 /* Read the statement sequences until there's nothing left. */
20063 while (line_ptr < line_end)
20064 {
20065 /* The DWARF line number program state machine. Reset the state
20066 machine at the start of each sequence. */
20067 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
20068 bool end_sequence = false;
20069
20070 if (record_lines_p)
20071 {
20072 /* Start a subfile for the current file of the state
20073 machine. */
20074 const file_entry *fe = state_machine.current_file ();
20075
20076 if (fe != NULL)
20077 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
20078 }
20079
20080 /* Decode the table. */
20081 while (line_ptr < line_end && !end_sequence)
20082 {
20083 op_code = read_1_byte (abfd, line_ptr);
20084 line_ptr += 1;
20085
20086 if (op_code >= lh->opcode_base)
20087 {
20088 /* Special opcode. */
20089 state_machine.handle_special_opcode (op_code);
20090 }
20091 else switch (op_code)
20092 {
20093 case DW_LNS_extended_op:
20094 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20095 &bytes_read);
20096 line_ptr += bytes_read;
20097 extended_end = line_ptr + extended_len;
20098 extended_op = read_1_byte (abfd, line_ptr);
20099 line_ptr += 1;
20100 switch (extended_op)
20101 {
20102 case DW_LNE_end_sequence:
20103 state_machine.handle_end_sequence ();
20104 end_sequence = true;
20105 break;
20106 case DW_LNE_set_address:
20107 {
20108 CORE_ADDR address
20109 = cu->header.read_address (abfd, line_ptr, &bytes_read);
20110 line_ptr += bytes_read;
20111
20112 state_machine.check_line_address (cu, line_ptr,
20113 lowpc - baseaddr, address);
20114 state_machine.handle_set_address (baseaddr, address);
20115 }
20116 break;
20117 case DW_LNE_define_file:
20118 {
20119 const char *cur_file;
20120 unsigned int mod_time, length;
20121 dir_index dindex;
20122
20123 cur_file = read_direct_string (abfd, line_ptr,
20124 &bytes_read);
20125 line_ptr += bytes_read;
20126 dindex = (dir_index)
20127 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20128 line_ptr += bytes_read;
20129 mod_time =
20130 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20131 line_ptr += bytes_read;
20132 length =
20133 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20134 line_ptr += bytes_read;
20135 lh->add_file_name (cur_file, dindex, mod_time, length);
20136 }
20137 break;
20138 case DW_LNE_set_discriminator:
20139 {
20140 /* The discriminator is not interesting to the
20141 debugger; just ignore it. We still need to
20142 check its value though:
20143 if there are consecutive entries for the same
20144 (non-prologue) line we want to coalesce them.
20145 PR 17276. */
20146 unsigned int discr
20147 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20148 line_ptr += bytes_read;
20149
20150 state_machine.handle_set_discriminator (discr);
20151 }
20152 break;
20153 default:
20154 complaint (_("mangled .debug_line section"));
20155 return;
20156 }
20157 /* Make sure that we parsed the extended op correctly. If e.g.
20158 we expected a different address size than the producer used,
20159 we may have read the wrong number of bytes. */
20160 if (line_ptr != extended_end)
20161 {
20162 complaint (_("mangled .debug_line section"));
20163 return;
20164 }
20165 break;
20166 case DW_LNS_copy:
20167 state_machine.handle_copy ();
20168 break;
20169 case DW_LNS_advance_pc:
20170 {
20171 CORE_ADDR adjust
20172 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20173 line_ptr += bytes_read;
20174
20175 state_machine.handle_advance_pc (adjust);
20176 }
20177 break;
20178 case DW_LNS_advance_line:
20179 {
20180 int line_delta
20181 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
20182 line_ptr += bytes_read;
20183
20184 state_machine.handle_advance_line (line_delta);
20185 }
20186 break;
20187 case DW_LNS_set_file:
20188 {
20189 file_name_index file
20190 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20191 &bytes_read);
20192 line_ptr += bytes_read;
20193
20194 state_machine.handle_set_file (file);
20195 }
20196 break;
20197 case DW_LNS_set_column:
20198 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20199 line_ptr += bytes_read;
20200 break;
20201 case DW_LNS_negate_stmt:
20202 state_machine.handle_negate_stmt ();
20203 break;
20204 case DW_LNS_set_basic_block:
20205 break;
20206 /* Add to the address register of the state machine the
20207 address increment value corresponding to special opcode
20208 255. I.e., this value is scaled by the minimum
20209 instruction length since special opcode 255 would have
20210 scaled the increment. */
20211 case DW_LNS_const_add_pc:
20212 state_machine.handle_const_add_pc ();
20213 break;
20214 case DW_LNS_fixed_advance_pc:
20215 {
20216 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
20217 line_ptr += 2;
20218
20219 state_machine.handle_fixed_advance_pc (addr_adj);
20220 }
20221 break;
20222 default:
20223 {
20224 /* Unknown standard opcode, ignore it. */
20225 int i;
20226
20227 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
20228 {
20229 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20230 line_ptr += bytes_read;
20231 }
20232 }
20233 }
20234 }
20235
20236 if (!end_sequence)
20237 dwarf2_debug_line_missing_end_sequence_complaint ();
20238
20239 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20240 in which case we still finish recording the last line). */
20241 state_machine.record_line (true);
20242 }
20243 }
20244
20245 /* Decode the Line Number Program (LNP) for the given line_header
20246 structure and CU. The actual information extracted and the type
20247 of structures created from the LNP depends on the value of PST.
20248
20249 1. If PST is NULL, then this procedure uses the data from the program
20250 to create all necessary symbol tables, and their linetables.
20251
20252 2. If PST is not NULL, this procedure reads the program to determine
20253 the list of files included by the unit represented by PST, and
20254 builds all the associated partial symbol tables.
20255
20256 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20257 It is used for relative paths in the line table.
20258 NOTE: When processing partial symtabs (pst != NULL),
20259 comp_dir == pst->dirname.
20260
20261 NOTE: It is important that psymtabs have the same file name (via strcmp)
20262 as the corresponding symtab. Since COMP_DIR is not used in the name of the
20263 symtab we don't use it in the name of the psymtabs we create.
20264 E.g. expand_line_sal requires this when finding psymtabs to expand.
20265 A good testcase for this is mb-inline.exp.
20266
20267 LOWPC is the lowest address in CU (or 0 if not known).
20268
20269 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20270 for its PC<->lines mapping information. Otherwise only the filename
20271 table is read in. */
20272
20273 static void
20274 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
20275 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
20276 CORE_ADDR lowpc, int decode_mapping)
20277 {
20278 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20279 const int decode_for_pst_p = (pst != NULL);
20280
20281 if (decode_mapping)
20282 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
20283
20284 if (decode_for_pst_p)
20285 {
20286 /* Now that we're done scanning the Line Header Program, we can
20287 create the psymtab of each included file. */
20288 for (auto &file_entry : lh->file_names ())
20289 if (file_entry.included_p == 1)
20290 {
20291 gdb::unique_xmalloc_ptr<char> name_holder;
20292 const char *include_name =
20293 psymtab_include_file_name (lh, file_entry, pst,
20294 comp_dir, &name_holder);
20295 if (include_name != NULL)
20296 dwarf2_create_include_psymtab (include_name, pst, objfile);
20297 }
20298 }
20299 else
20300 {
20301 /* Make sure a symtab is created for every file, even files
20302 which contain only variables (i.e. no code with associated
20303 line numbers). */
20304 buildsym_compunit *builder = cu->get_builder ();
20305 struct compunit_symtab *cust = builder->get_compunit_symtab ();
20306
20307 for (auto &fe : lh->file_names ())
20308 {
20309 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
20310 if (builder->get_current_subfile ()->symtab == NULL)
20311 {
20312 builder->get_current_subfile ()->symtab
20313 = allocate_symtab (cust,
20314 builder->get_current_subfile ()->name);
20315 }
20316 fe.symtab = builder->get_current_subfile ()->symtab;
20317 }
20318 }
20319 }
20320
20321 /* Start a subfile for DWARF. FILENAME is the name of the file and
20322 DIRNAME the name of the source directory which contains FILENAME
20323 or NULL if not known.
20324 This routine tries to keep line numbers from identical absolute and
20325 relative file names in a common subfile.
20326
20327 Using the `list' example from the GDB testsuite, which resides in
20328 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
20329 of /srcdir/list0.c yields the following debugging information for list0.c:
20330
20331 DW_AT_name: /srcdir/list0.c
20332 DW_AT_comp_dir: /compdir
20333 files.files[0].name: list0.h
20334 files.files[0].dir: /srcdir
20335 files.files[1].name: list0.c
20336 files.files[1].dir: /srcdir
20337
20338 The line number information for list0.c has to end up in a single
20339 subfile, so that `break /srcdir/list0.c:1' works as expected.
20340 start_subfile will ensure that this happens provided that we pass the
20341 concatenation of files.files[1].dir and files.files[1].name as the
20342 subfile's name. */
20343
20344 static void
20345 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
20346 const char *dirname)
20347 {
20348 gdb::unique_xmalloc_ptr<char> copy;
20349
20350 /* In order not to lose the line information directory,
20351 we concatenate it to the filename when it makes sense.
20352 Note that the Dwarf3 standard says (speaking of filenames in line
20353 information): ``The directory index is ignored for file names
20354 that represent full path names''. Thus ignoring dirname in the
20355 `else' branch below isn't an issue. */
20356
20357 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
20358 {
20359 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
20360 filename = copy.get ();
20361 }
20362
20363 cu->get_builder ()->start_subfile (filename);
20364 }
20365
20366 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
20367 buildsym_compunit constructor. */
20368
20369 struct compunit_symtab *
20370 dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
20371 CORE_ADDR low_pc)
20372 {
20373 gdb_assert (m_builder == nullptr);
20374
20375 m_builder.reset (new struct buildsym_compunit
20376 (per_cu->dwarf2_per_objfile->objfile,
20377 name, comp_dir, language, low_pc));
20378
20379 list_in_scope = get_builder ()->get_file_symbols ();
20380
20381 get_builder ()->record_debugformat ("DWARF 2");
20382 get_builder ()->record_producer (producer);
20383
20384 processing_has_namespace_info = false;
20385
20386 return get_builder ()->get_compunit_symtab ();
20387 }
20388
20389 static void
20390 var_decode_location (struct attribute *attr, struct symbol *sym,
20391 struct dwarf2_cu *cu)
20392 {
20393 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20394 struct comp_unit_head *cu_header = &cu->header;
20395
20396 /* NOTE drow/2003-01-30: There used to be a comment and some special
20397 code here to turn a symbol with DW_AT_external and a
20398 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
20399 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
20400 with some versions of binutils) where shared libraries could have
20401 relocations against symbols in their debug information - the
20402 minimal symbol would have the right address, but the debug info
20403 would not. It's no longer necessary, because we will explicitly
20404 apply relocations when we read in the debug information now. */
20405
20406 /* A DW_AT_location attribute with no contents indicates that a
20407 variable has been optimized away. */
20408 if (attr->form_is_block () && DW_BLOCK (attr)->size == 0)
20409 {
20410 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
20411 return;
20412 }
20413
20414 /* Handle one degenerate form of location expression specially, to
20415 preserve GDB's previous behavior when section offsets are
20416 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
20417 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
20418
20419 if (attr->form_is_block ()
20420 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
20421 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
20422 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
20423 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
20424 && (DW_BLOCK (attr)->size
20425 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
20426 {
20427 unsigned int dummy;
20428
20429 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
20430 SET_SYMBOL_VALUE_ADDRESS
20431 (sym, cu->header.read_address (objfile->obfd,
20432 DW_BLOCK (attr)->data + 1,
20433 &dummy));
20434 else
20435 SET_SYMBOL_VALUE_ADDRESS
20436 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
20437 &dummy));
20438 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
20439 fixup_symbol_section (sym, objfile);
20440 SET_SYMBOL_VALUE_ADDRESS
20441 (sym,
20442 SYMBOL_VALUE_ADDRESS (sym)
20443 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
20444 return;
20445 }
20446
20447 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
20448 expression evaluator, and use LOC_COMPUTED only when necessary
20449 (i.e. when the value of a register or memory location is
20450 referenced, or a thread-local block, etc.). Then again, it might
20451 not be worthwhile. I'm assuming that it isn't unless performance
20452 or memory numbers show me otherwise. */
20453
20454 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
20455
20456 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
20457 cu->has_loclist = true;
20458 }
20459
20460 /* Given a pointer to a DWARF information entry, figure out if we need
20461 to make a symbol table entry for it, and if so, create a new entry
20462 and return a pointer to it.
20463 If TYPE is NULL, determine symbol type from the die, otherwise
20464 used the passed type.
20465 If SPACE is not NULL, use it to hold the new symbol. If it is
20466 NULL, allocate a new symbol on the objfile's obstack. */
20467
20468 static struct symbol *
20469 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
20470 struct symbol *space)
20471 {
20472 struct dwarf2_per_objfile *dwarf2_per_objfile
20473 = cu->per_cu->dwarf2_per_objfile;
20474 struct objfile *objfile = dwarf2_per_objfile->objfile;
20475 struct gdbarch *gdbarch = get_objfile_arch (objfile);
20476 struct symbol *sym = NULL;
20477 const char *name;
20478 struct attribute *attr = NULL;
20479 struct attribute *attr2 = NULL;
20480 CORE_ADDR baseaddr;
20481 struct pending **list_to_add = NULL;
20482
20483 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
20484
20485 baseaddr = objfile->text_section_offset ();
20486
20487 name = dwarf2_name (die, cu);
20488 if (name)
20489 {
20490 const char *linkagename;
20491 int suppress_add = 0;
20492
20493 if (space)
20494 sym = space;
20495 else
20496 sym = allocate_symbol (objfile);
20497 OBJSTAT (objfile, n_syms++);
20498
20499 /* Cache this symbol's name and the name's demangled form (if any). */
20500 sym->set_language (cu->language, &objfile->objfile_obstack);
20501 linkagename = dwarf2_physname (name, die, cu);
20502 sym->compute_and_set_names (linkagename, false, objfile->per_bfd);
20503
20504 /* Fortran does not have mangling standard and the mangling does differ
20505 between gfortran, iFort etc. */
20506 if (cu->language == language_fortran
20507 && symbol_get_demangled_name (sym) == NULL)
20508 symbol_set_demangled_name (sym,
20509 dwarf2_full_name (name, die, cu),
20510 NULL);
20511
20512 /* Default assumptions.
20513 Use the passed type or decode it from the die. */
20514 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20515 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
20516 if (type != NULL)
20517 SYMBOL_TYPE (sym) = type;
20518 else
20519 SYMBOL_TYPE (sym) = die_type (die, cu);
20520 attr = dwarf2_attr (die,
20521 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
20522 cu);
20523 if (attr != nullptr)
20524 {
20525 SYMBOL_LINE (sym) = DW_UNSND (attr);
20526 }
20527
20528 attr = dwarf2_attr (die,
20529 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
20530 cu);
20531 if (attr != nullptr)
20532 {
20533 file_name_index file_index = (file_name_index) DW_UNSND (attr);
20534 struct file_entry *fe;
20535
20536 if (cu->line_header != NULL)
20537 fe = cu->line_header->file_name_at (file_index);
20538 else
20539 fe = NULL;
20540
20541 if (fe == NULL)
20542 complaint (_("file index out of range"));
20543 else
20544 symbol_set_symtab (sym, fe->symtab);
20545 }
20546
20547 switch (die->tag)
20548 {
20549 case DW_TAG_label:
20550 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
20551 if (attr != nullptr)
20552 {
20553 CORE_ADDR addr;
20554
20555 addr = attr->value_as_address ();
20556 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
20557 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
20558 }
20559 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
20560 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
20561 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
20562 add_symbol_to_list (sym, cu->list_in_scope);
20563 break;
20564 case DW_TAG_subprogram:
20565 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20566 finish_block. */
20567 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20568 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20569 if ((attr2 && (DW_UNSND (attr2) != 0))
20570 || cu->language == language_ada
20571 || cu->language == language_fortran)
20572 {
20573 /* Subprograms marked external are stored as a global symbol.
20574 Ada and Fortran subprograms, whether marked external or
20575 not, are always stored as a global symbol, because we want
20576 to be able to access them globally. For instance, we want
20577 to be able to break on a nested subprogram without having
20578 to specify the context. */
20579 list_to_add = cu->get_builder ()->get_global_symbols ();
20580 }
20581 else
20582 {
20583 list_to_add = cu->list_in_scope;
20584 }
20585 break;
20586 case DW_TAG_inlined_subroutine:
20587 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20588 finish_block. */
20589 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20590 SYMBOL_INLINED (sym) = 1;
20591 list_to_add = cu->list_in_scope;
20592 break;
20593 case DW_TAG_template_value_param:
20594 suppress_add = 1;
20595 /* Fall through. */
20596 case DW_TAG_constant:
20597 case DW_TAG_variable:
20598 case DW_TAG_member:
20599 /* Compilation with minimal debug info may result in
20600 variables with missing type entries. Change the
20601 misleading `void' type to something sensible. */
20602 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
20603 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
20604
20605 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20606 /* In the case of DW_TAG_member, we should only be called for
20607 static const members. */
20608 if (die->tag == DW_TAG_member)
20609 {
20610 /* dwarf2_add_field uses die_is_declaration,
20611 so we do the same. */
20612 gdb_assert (die_is_declaration (die, cu));
20613 gdb_assert (attr);
20614 }
20615 if (attr != nullptr)
20616 {
20617 dwarf2_const_value (attr, sym, cu);
20618 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20619 if (!suppress_add)
20620 {
20621 if (attr2 && (DW_UNSND (attr2) != 0))
20622 list_to_add = cu->get_builder ()->get_global_symbols ();
20623 else
20624 list_to_add = cu->list_in_scope;
20625 }
20626 break;
20627 }
20628 attr = dwarf2_attr (die, DW_AT_location, cu);
20629 if (attr != nullptr)
20630 {
20631 var_decode_location (attr, sym, cu);
20632 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20633
20634 /* Fortran explicitly imports any global symbols to the local
20635 scope by DW_TAG_common_block. */
20636 if (cu->language == language_fortran && die->parent
20637 && die->parent->tag == DW_TAG_common_block)
20638 attr2 = NULL;
20639
20640 if (SYMBOL_CLASS (sym) == LOC_STATIC
20641 && SYMBOL_VALUE_ADDRESS (sym) == 0
20642 && !dwarf2_per_objfile->has_section_at_zero)
20643 {
20644 /* When a static variable is eliminated by the linker,
20645 the corresponding debug information is not stripped
20646 out, but the variable address is set to null;
20647 do not add such variables into symbol table. */
20648 }
20649 else if (attr2 && (DW_UNSND (attr2) != 0))
20650 {
20651 if (SYMBOL_CLASS (sym) == LOC_STATIC
20652 && (objfile->flags & OBJF_MAINLINE) == 0
20653 && dwarf2_per_objfile->can_copy)
20654 {
20655 /* A global static variable might be subject to
20656 copy relocation. We first check for a local
20657 minsym, though, because maybe the symbol was
20658 marked hidden, in which case this would not
20659 apply. */
20660 bound_minimal_symbol found
20661 = (lookup_minimal_symbol_linkage
20662 (sym->linkage_name (), objfile));
20663 if (found.minsym != nullptr)
20664 sym->maybe_copied = 1;
20665 }
20666
20667 /* A variable with DW_AT_external is never static,
20668 but it may be block-scoped. */
20669 list_to_add
20670 = ((cu->list_in_scope
20671 == cu->get_builder ()->get_file_symbols ())
20672 ? cu->get_builder ()->get_global_symbols ()
20673 : cu->list_in_scope);
20674 }
20675 else
20676 list_to_add = cu->list_in_scope;
20677 }
20678 else
20679 {
20680 /* We do not know the address of this symbol.
20681 If it is an external symbol and we have type information
20682 for it, enter the symbol as a LOC_UNRESOLVED symbol.
20683 The address of the variable will then be determined from
20684 the minimal symbol table whenever the variable is
20685 referenced. */
20686 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20687
20688 /* Fortran explicitly imports any global symbols to the local
20689 scope by DW_TAG_common_block. */
20690 if (cu->language == language_fortran && die->parent
20691 && die->parent->tag == DW_TAG_common_block)
20692 {
20693 /* SYMBOL_CLASS doesn't matter here because
20694 read_common_block is going to reset it. */
20695 if (!suppress_add)
20696 list_to_add = cu->list_in_scope;
20697 }
20698 else if (attr2 && (DW_UNSND (attr2) != 0)
20699 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
20700 {
20701 /* A variable with DW_AT_external is never static, but it
20702 may be block-scoped. */
20703 list_to_add
20704 = ((cu->list_in_scope
20705 == cu->get_builder ()->get_file_symbols ())
20706 ? cu->get_builder ()->get_global_symbols ()
20707 : cu->list_in_scope);
20708
20709 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
20710 }
20711 else if (!die_is_declaration (die, cu))
20712 {
20713 /* Use the default LOC_OPTIMIZED_OUT class. */
20714 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
20715 if (!suppress_add)
20716 list_to_add = cu->list_in_scope;
20717 }
20718 }
20719 break;
20720 case DW_TAG_formal_parameter:
20721 {
20722 /* If we are inside a function, mark this as an argument. If
20723 not, we might be looking at an argument to an inlined function
20724 when we do not have enough information to show inlined frames;
20725 pretend it's a local variable in that case so that the user can
20726 still see it. */
20727 struct context_stack *curr
20728 = cu->get_builder ()->get_current_context_stack ();
20729 if (curr != nullptr && curr->name != nullptr)
20730 SYMBOL_IS_ARGUMENT (sym) = 1;
20731 attr = dwarf2_attr (die, DW_AT_location, cu);
20732 if (attr != nullptr)
20733 {
20734 var_decode_location (attr, sym, cu);
20735 }
20736 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20737 if (attr != nullptr)
20738 {
20739 dwarf2_const_value (attr, sym, cu);
20740 }
20741
20742 list_to_add = cu->list_in_scope;
20743 }
20744 break;
20745 case DW_TAG_unspecified_parameters:
20746 /* From varargs functions; gdb doesn't seem to have any
20747 interest in this information, so just ignore it for now.
20748 (FIXME?) */
20749 break;
20750 case DW_TAG_template_type_param:
20751 suppress_add = 1;
20752 /* Fall through. */
20753 case DW_TAG_class_type:
20754 case DW_TAG_interface_type:
20755 case DW_TAG_structure_type:
20756 case DW_TAG_union_type:
20757 case DW_TAG_set_type:
20758 case DW_TAG_enumeration_type:
20759 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20760 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
20761
20762 {
20763 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
20764 really ever be static objects: otherwise, if you try
20765 to, say, break of a class's method and you're in a file
20766 which doesn't mention that class, it won't work unless
20767 the check for all static symbols in lookup_symbol_aux
20768 saves you. See the OtherFileClass tests in
20769 gdb.c++/namespace.exp. */
20770
20771 if (!suppress_add)
20772 {
20773 buildsym_compunit *builder = cu->get_builder ();
20774 list_to_add
20775 = (cu->list_in_scope == builder->get_file_symbols ()
20776 && cu->language == language_cplus
20777 ? builder->get_global_symbols ()
20778 : cu->list_in_scope);
20779
20780 /* The semantics of C++ state that "struct foo {
20781 ... }" also defines a typedef for "foo". */
20782 if (cu->language == language_cplus
20783 || cu->language == language_ada
20784 || cu->language == language_d
20785 || cu->language == language_rust)
20786 {
20787 /* The symbol's name is already allocated along
20788 with this objfile, so we don't need to
20789 duplicate it for the type. */
20790 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
20791 TYPE_NAME (SYMBOL_TYPE (sym)) = sym->search_name ();
20792 }
20793 }
20794 }
20795 break;
20796 case DW_TAG_typedef:
20797 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20798 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20799 list_to_add = cu->list_in_scope;
20800 break;
20801 case DW_TAG_base_type:
20802 case DW_TAG_subrange_type:
20803 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20804 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20805 list_to_add = cu->list_in_scope;
20806 break;
20807 case DW_TAG_enumerator:
20808 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20809 if (attr != nullptr)
20810 {
20811 dwarf2_const_value (attr, sym, cu);
20812 }
20813 {
20814 /* NOTE: carlton/2003-11-10: See comment above in the
20815 DW_TAG_class_type, etc. block. */
20816
20817 list_to_add
20818 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
20819 && cu->language == language_cplus
20820 ? cu->get_builder ()->get_global_symbols ()
20821 : cu->list_in_scope);
20822 }
20823 break;
20824 case DW_TAG_imported_declaration:
20825 case DW_TAG_namespace:
20826 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20827 list_to_add = cu->get_builder ()->get_global_symbols ();
20828 break;
20829 case DW_TAG_module:
20830 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20831 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
20832 list_to_add = cu->get_builder ()->get_global_symbols ();
20833 break;
20834 case DW_TAG_common_block:
20835 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
20836 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
20837 add_symbol_to_list (sym, cu->list_in_scope);
20838 break;
20839 default:
20840 /* Not a tag we recognize. Hopefully we aren't processing
20841 trash data, but since we must specifically ignore things
20842 we don't recognize, there is nothing else we should do at
20843 this point. */
20844 complaint (_("unsupported tag: '%s'"),
20845 dwarf_tag_name (die->tag));
20846 break;
20847 }
20848
20849 if (suppress_add)
20850 {
20851 sym->hash_next = objfile->template_symbols;
20852 objfile->template_symbols = sym;
20853 list_to_add = NULL;
20854 }
20855
20856 if (list_to_add != NULL)
20857 add_symbol_to_list (sym, list_to_add);
20858
20859 /* For the benefit of old versions of GCC, check for anonymous
20860 namespaces based on the demangled name. */
20861 if (!cu->processing_has_namespace_info
20862 && cu->language == language_cplus)
20863 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
20864 }
20865 return (sym);
20866 }
20867
20868 /* Given an attr with a DW_FORM_dataN value in host byte order,
20869 zero-extend it as appropriate for the symbol's type. The DWARF
20870 standard (v4) is not entirely clear about the meaning of using
20871 DW_FORM_dataN for a constant with a signed type, where the type is
20872 wider than the data. The conclusion of a discussion on the DWARF
20873 list was that this is unspecified. We choose to always zero-extend
20874 because that is the interpretation long in use by GCC. */
20875
20876 static gdb_byte *
20877 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
20878 struct dwarf2_cu *cu, LONGEST *value, int bits)
20879 {
20880 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20881 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
20882 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
20883 LONGEST l = DW_UNSND (attr);
20884
20885 if (bits < sizeof (*value) * 8)
20886 {
20887 l &= ((LONGEST) 1 << bits) - 1;
20888 *value = l;
20889 }
20890 else if (bits == sizeof (*value) * 8)
20891 *value = l;
20892 else
20893 {
20894 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
20895 store_unsigned_integer (bytes, bits / 8, byte_order, l);
20896 return bytes;
20897 }
20898
20899 return NULL;
20900 }
20901
20902 /* Read a constant value from an attribute. Either set *VALUE, or if
20903 the value does not fit in *VALUE, set *BYTES - either already
20904 allocated on the objfile obstack, or newly allocated on OBSTACK,
20905 or, set *BATON, if we translated the constant to a location
20906 expression. */
20907
20908 static void
20909 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
20910 const char *name, struct obstack *obstack,
20911 struct dwarf2_cu *cu,
20912 LONGEST *value, const gdb_byte **bytes,
20913 struct dwarf2_locexpr_baton **baton)
20914 {
20915 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20916 struct comp_unit_head *cu_header = &cu->header;
20917 struct dwarf_block *blk;
20918 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
20919 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
20920
20921 *value = 0;
20922 *bytes = NULL;
20923 *baton = NULL;
20924
20925 switch (attr->form)
20926 {
20927 case DW_FORM_addr:
20928 case DW_FORM_addrx:
20929 case DW_FORM_GNU_addr_index:
20930 {
20931 gdb_byte *data;
20932
20933 if (TYPE_LENGTH (type) != cu_header->addr_size)
20934 dwarf2_const_value_length_mismatch_complaint (name,
20935 cu_header->addr_size,
20936 TYPE_LENGTH (type));
20937 /* Symbols of this form are reasonably rare, so we just
20938 piggyback on the existing location code rather than writing
20939 a new implementation of symbol_computed_ops. */
20940 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
20941 (*baton)->per_cu = cu->per_cu;
20942 gdb_assert ((*baton)->per_cu);
20943
20944 (*baton)->size = 2 + cu_header->addr_size;
20945 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
20946 (*baton)->data = data;
20947
20948 data[0] = DW_OP_addr;
20949 store_unsigned_integer (&data[1], cu_header->addr_size,
20950 byte_order, DW_ADDR (attr));
20951 data[cu_header->addr_size + 1] = DW_OP_stack_value;
20952 }
20953 break;
20954 case DW_FORM_string:
20955 case DW_FORM_strp:
20956 case DW_FORM_strx:
20957 case DW_FORM_GNU_str_index:
20958 case DW_FORM_GNU_strp_alt:
20959 /* DW_STRING is already allocated on the objfile obstack, point
20960 directly to it. */
20961 *bytes = (const gdb_byte *) DW_STRING (attr);
20962 break;
20963 case DW_FORM_block1:
20964 case DW_FORM_block2:
20965 case DW_FORM_block4:
20966 case DW_FORM_block:
20967 case DW_FORM_exprloc:
20968 case DW_FORM_data16:
20969 blk = DW_BLOCK (attr);
20970 if (TYPE_LENGTH (type) != blk->size)
20971 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
20972 TYPE_LENGTH (type));
20973 *bytes = blk->data;
20974 break;
20975
20976 /* The DW_AT_const_value attributes are supposed to carry the
20977 symbol's value "represented as it would be on the target
20978 architecture." By the time we get here, it's already been
20979 converted to host endianness, so we just need to sign- or
20980 zero-extend it as appropriate. */
20981 case DW_FORM_data1:
20982 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
20983 break;
20984 case DW_FORM_data2:
20985 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
20986 break;
20987 case DW_FORM_data4:
20988 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
20989 break;
20990 case DW_FORM_data8:
20991 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
20992 break;
20993
20994 case DW_FORM_sdata:
20995 case DW_FORM_implicit_const:
20996 *value = DW_SND (attr);
20997 break;
20998
20999 case DW_FORM_udata:
21000 *value = DW_UNSND (attr);
21001 break;
21002
21003 default:
21004 complaint (_("unsupported const value attribute form: '%s'"),
21005 dwarf_form_name (attr->form));
21006 *value = 0;
21007 break;
21008 }
21009 }
21010
21011
21012 /* Copy constant value from an attribute to a symbol. */
21013
21014 static void
21015 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
21016 struct dwarf2_cu *cu)
21017 {
21018 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21019 LONGEST value;
21020 const gdb_byte *bytes;
21021 struct dwarf2_locexpr_baton *baton;
21022
21023 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
21024 sym->print_name (),
21025 &objfile->objfile_obstack, cu,
21026 &value, &bytes, &baton);
21027
21028 if (baton != NULL)
21029 {
21030 SYMBOL_LOCATION_BATON (sym) = baton;
21031 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
21032 }
21033 else if (bytes != NULL)
21034 {
21035 SYMBOL_VALUE_BYTES (sym) = bytes;
21036 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
21037 }
21038 else
21039 {
21040 SYMBOL_VALUE (sym) = value;
21041 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
21042 }
21043 }
21044
21045 /* Return the type of the die in question using its DW_AT_type attribute. */
21046
21047 static struct type *
21048 die_type (struct die_info *die, struct dwarf2_cu *cu)
21049 {
21050 struct attribute *type_attr;
21051
21052 type_attr = dwarf2_attr (die, DW_AT_type, cu);
21053 if (!type_attr)
21054 {
21055 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21056 /* A missing DW_AT_type represents a void type. */
21057 return objfile_type (objfile)->builtin_void;
21058 }
21059
21060 return lookup_die_type (die, type_attr, cu);
21061 }
21062
21063 /* True iff CU's producer generates GNAT Ada auxiliary information
21064 that allows to find parallel types through that information instead
21065 of having to do expensive parallel lookups by type name. */
21066
21067 static int
21068 need_gnat_info (struct dwarf2_cu *cu)
21069 {
21070 /* Assume that the Ada compiler was GNAT, which always produces
21071 the auxiliary information. */
21072 return (cu->language == language_ada);
21073 }
21074
21075 /* Return the auxiliary type of the die in question using its
21076 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21077 attribute is not present. */
21078
21079 static struct type *
21080 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21081 {
21082 struct attribute *type_attr;
21083
21084 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21085 if (!type_attr)
21086 return NULL;
21087
21088 return lookup_die_type (die, type_attr, cu);
21089 }
21090
21091 /* If DIE has a descriptive_type attribute, then set the TYPE's
21092 descriptive type accordingly. */
21093
21094 static void
21095 set_descriptive_type (struct type *type, struct die_info *die,
21096 struct dwarf2_cu *cu)
21097 {
21098 struct type *descriptive_type = die_descriptive_type (die, cu);
21099
21100 if (descriptive_type)
21101 {
21102 ALLOCATE_GNAT_AUX_TYPE (type);
21103 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21104 }
21105 }
21106
21107 /* Return the containing type of the die in question using its
21108 DW_AT_containing_type attribute. */
21109
21110 static struct type *
21111 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
21112 {
21113 struct attribute *type_attr;
21114 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21115
21116 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
21117 if (!type_attr)
21118 error (_("Dwarf Error: Problem turning containing type into gdb type "
21119 "[in module %s]"), objfile_name (objfile));
21120
21121 return lookup_die_type (die, type_attr, cu);
21122 }
21123
21124 /* Return an error marker type to use for the ill formed type in DIE/CU. */
21125
21126 static struct type *
21127 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21128 {
21129 struct dwarf2_per_objfile *dwarf2_per_objfile
21130 = cu->per_cu->dwarf2_per_objfile;
21131 struct objfile *objfile = dwarf2_per_objfile->objfile;
21132 char *saved;
21133
21134 std::string message
21135 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
21136 objfile_name (objfile),
21137 sect_offset_str (cu->header.sect_off),
21138 sect_offset_str (die->sect_off));
21139 saved = obstack_strdup (&objfile->objfile_obstack, message);
21140
21141 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
21142 }
21143
21144 /* Look up the type of DIE in CU using its type attribute ATTR.
21145 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21146 DW_AT_containing_type.
21147 If there is no type substitute an error marker. */
21148
21149 static struct type *
21150 lookup_die_type (struct die_info *die, const struct attribute *attr,
21151 struct dwarf2_cu *cu)
21152 {
21153 struct dwarf2_per_objfile *dwarf2_per_objfile
21154 = cu->per_cu->dwarf2_per_objfile;
21155 struct objfile *objfile = dwarf2_per_objfile->objfile;
21156 struct type *this_type;
21157
21158 gdb_assert (attr->name == DW_AT_type
21159 || attr->name == DW_AT_GNAT_descriptive_type
21160 || attr->name == DW_AT_containing_type);
21161
21162 /* First see if we have it cached. */
21163
21164 if (attr->form == DW_FORM_GNU_ref_alt)
21165 {
21166 struct dwarf2_per_cu_data *per_cu;
21167 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
21168
21169 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
21170 dwarf2_per_objfile);
21171 this_type = get_die_type_at_offset (sect_off, per_cu);
21172 }
21173 else if (attr->form_is_ref ())
21174 {
21175 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
21176
21177 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
21178 }
21179 else if (attr->form == DW_FORM_ref_sig8)
21180 {
21181 ULONGEST signature = DW_SIGNATURE (attr);
21182
21183 return get_signatured_type (die, signature, cu);
21184 }
21185 else
21186 {
21187 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
21188 " at %s [in module %s]"),
21189 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
21190 objfile_name (objfile));
21191 return build_error_marker_type (cu, die);
21192 }
21193
21194 /* If not cached we need to read it in. */
21195
21196 if (this_type == NULL)
21197 {
21198 struct die_info *type_die = NULL;
21199 struct dwarf2_cu *type_cu = cu;
21200
21201 if (attr->form_is_ref ())
21202 type_die = follow_die_ref (die, attr, &type_cu);
21203 if (type_die == NULL)
21204 return build_error_marker_type (cu, die);
21205 /* If we find the type now, it's probably because the type came
21206 from an inter-CU reference and the type's CU got expanded before
21207 ours. */
21208 this_type = read_type_die (type_die, type_cu);
21209 }
21210
21211 /* If we still don't have a type use an error marker. */
21212
21213 if (this_type == NULL)
21214 return build_error_marker_type (cu, die);
21215
21216 return this_type;
21217 }
21218
21219 /* Return the type in DIE, CU.
21220 Returns NULL for invalid types.
21221
21222 This first does a lookup in die_type_hash,
21223 and only reads the die in if necessary.
21224
21225 NOTE: This can be called when reading in partial or full symbols. */
21226
21227 static struct type *
21228 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
21229 {
21230 struct type *this_type;
21231
21232 this_type = get_die_type (die, cu);
21233 if (this_type)
21234 return this_type;
21235
21236 return read_type_die_1 (die, cu);
21237 }
21238
21239 /* Read the type in DIE, CU.
21240 Returns NULL for invalid types. */
21241
21242 static struct type *
21243 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21244 {
21245 struct type *this_type = NULL;
21246
21247 switch (die->tag)
21248 {
21249 case DW_TAG_class_type:
21250 case DW_TAG_interface_type:
21251 case DW_TAG_structure_type:
21252 case DW_TAG_union_type:
21253 this_type = read_structure_type (die, cu);
21254 break;
21255 case DW_TAG_enumeration_type:
21256 this_type = read_enumeration_type (die, cu);
21257 break;
21258 case DW_TAG_subprogram:
21259 case DW_TAG_subroutine_type:
21260 case DW_TAG_inlined_subroutine:
21261 this_type = read_subroutine_type (die, cu);
21262 break;
21263 case DW_TAG_array_type:
21264 this_type = read_array_type (die, cu);
21265 break;
21266 case DW_TAG_set_type:
21267 this_type = read_set_type (die, cu);
21268 break;
21269 case DW_TAG_pointer_type:
21270 this_type = read_tag_pointer_type (die, cu);
21271 break;
21272 case DW_TAG_ptr_to_member_type:
21273 this_type = read_tag_ptr_to_member_type (die, cu);
21274 break;
21275 case DW_TAG_reference_type:
21276 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
21277 break;
21278 case DW_TAG_rvalue_reference_type:
21279 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
21280 break;
21281 case DW_TAG_const_type:
21282 this_type = read_tag_const_type (die, cu);
21283 break;
21284 case DW_TAG_volatile_type:
21285 this_type = read_tag_volatile_type (die, cu);
21286 break;
21287 case DW_TAG_restrict_type:
21288 this_type = read_tag_restrict_type (die, cu);
21289 break;
21290 case DW_TAG_string_type:
21291 this_type = read_tag_string_type (die, cu);
21292 break;
21293 case DW_TAG_typedef:
21294 this_type = read_typedef (die, cu);
21295 break;
21296 case DW_TAG_subrange_type:
21297 this_type = read_subrange_type (die, cu);
21298 break;
21299 case DW_TAG_base_type:
21300 this_type = read_base_type (die, cu);
21301 break;
21302 case DW_TAG_unspecified_type:
21303 this_type = read_unspecified_type (die, cu);
21304 break;
21305 case DW_TAG_namespace:
21306 this_type = read_namespace_type (die, cu);
21307 break;
21308 case DW_TAG_module:
21309 this_type = read_module_type (die, cu);
21310 break;
21311 case DW_TAG_atomic_type:
21312 this_type = read_tag_atomic_type (die, cu);
21313 break;
21314 default:
21315 complaint (_("unexpected tag in read_type_die: '%s'"),
21316 dwarf_tag_name (die->tag));
21317 break;
21318 }
21319
21320 return this_type;
21321 }
21322
21323 /* See if we can figure out if the class lives in a namespace. We do
21324 this by looking for a member function; its demangled name will
21325 contain namespace info, if there is any.
21326 Return the computed name or NULL.
21327 Space for the result is allocated on the objfile's obstack.
21328 This is the full-die version of guess_partial_die_structure_name.
21329 In this case we know DIE has no useful parent. */
21330
21331 static const char *
21332 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
21333 {
21334 struct die_info *spec_die;
21335 struct dwarf2_cu *spec_cu;
21336 struct die_info *child;
21337 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21338
21339 spec_cu = cu;
21340 spec_die = die_specification (die, &spec_cu);
21341 if (spec_die != NULL)
21342 {
21343 die = spec_die;
21344 cu = spec_cu;
21345 }
21346
21347 for (child = die->child;
21348 child != NULL;
21349 child = child->sibling)
21350 {
21351 if (child->tag == DW_TAG_subprogram)
21352 {
21353 const char *linkage_name = dw2_linkage_name (child, cu);
21354
21355 if (linkage_name != NULL)
21356 {
21357 gdb::unique_xmalloc_ptr<char> actual_name
21358 (language_class_name_from_physname (cu->language_defn,
21359 linkage_name));
21360 const char *name = NULL;
21361
21362 if (actual_name != NULL)
21363 {
21364 const char *die_name = dwarf2_name (die, cu);
21365
21366 if (die_name != NULL
21367 && strcmp (die_name, actual_name.get ()) != 0)
21368 {
21369 /* Strip off the class name from the full name.
21370 We want the prefix. */
21371 int die_name_len = strlen (die_name);
21372 int actual_name_len = strlen (actual_name.get ());
21373 const char *ptr = actual_name.get ();
21374
21375 /* Test for '::' as a sanity check. */
21376 if (actual_name_len > die_name_len + 2
21377 && ptr[actual_name_len - die_name_len - 1] == ':')
21378 name = obstack_strndup (
21379 &objfile->per_bfd->storage_obstack,
21380 ptr, actual_name_len - die_name_len - 2);
21381 }
21382 }
21383 return name;
21384 }
21385 }
21386 }
21387
21388 return NULL;
21389 }
21390
21391 /* GCC might emit a nameless typedef that has a linkage name. Determine the
21392 prefix part in such case. See
21393 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21394
21395 static const char *
21396 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
21397 {
21398 struct attribute *attr;
21399 const char *base;
21400
21401 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
21402 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
21403 return NULL;
21404
21405 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
21406 return NULL;
21407
21408 attr = dw2_linkage_name_attr (die, cu);
21409 if (attr == NULL || DW_STRING (attr) == NULL)
21410 return NULL;
21411
21412 /* dwarf2_name had to be already called. */
21413 gdb_assert (DW_STRING_IS_CANONICAL (attr));
21414
21415 /* Strip the base name, keep any leading namespaces/classes. */
21416 base = strrchr (DW_STRING (attr), ':');
21417 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
21418 return "";
21419
21420 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21421 return obstack_strndup (&objfile->per_bfd->storage_obstack,
21422 DW_STRING (attr),
21423 &base[-1] - DW_STRING (attr));
21424 }
21425
21426 /* Return the name of the namespace/class that DIE is defined within,
21427 or "" if we can't tell. The caller should not xfree the result.
21428
21429 For example, if we're within the method foo() in the following
21430 code:
21431
21432 namespace N {
21433 class C {
21434 void foo () {
21435 }
21436 };
21437 }
21438
21439 then determine_prefix on foo's die will return "N::C". */
21440
21441 static const char *
21442 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
21443 {
21444 struct dwarf2_per_objfile *dwarf2_per_objfile
21445 = cu->per_cu->dwarf2_per_objfile;
21446 struct die_info *parent, *spec_die;
21447 struct dwarf2_cu *spec_cu;
21448 struct type *parent_type;
21449 const char *retval;
21450
21451 if (cu->language != language_cplus
21452 && cu->language != language_fortran && cu->language != language_d
21453 && cu->language != language_rust)
21454 return "";
21455
21456 retval = anonymous_struct_prefix (die, cu);
21457 if (retval)
21458 return retval;
21459
21460 /* We have to be careful in the presence of DW_AT_specification.
21461 For example, with GCC 3.4, given the code
21462
21463 namespace N {
21464 void foo() {
21465 // Definition of N::foo.
21466 }
21467 }
21468
21469 then we'll have a tree of DIEs like this:
21470
21471 1: DW_TAG_compile_unit
21472 2: DW_TAG_namespace // N
21473 3: DW_TAG_subprogram // declaration of N::foo
21474 4: DW_TAG_subprogram // definition of N::foo
21475 DW_AT_specification // refers to die #3
21476
21477 Thus, when processing die #4, we have to pretend that we're in
21478 the context of its DW_AT_specification, namely the contex of die
21479 #3. */
21480 spec_cu = cu;
21481 spec_die = die_specification (die, &spec_cu);
21482 if (spec_die == NULL)
21483 parent = die->parent;
21484 else
21485 {
21486 parent = spec_die->parent;
21487 cu = spec_cu;
21488 }
21489
21490 if (parent == NULL)
21491 return "";
21492 else if (parent->building_fullname)
21493 {
21494 const char *name;
21495 const char *parent_name;
21496
21497 /* It has been seen on RealView 2.2 built binaries,
21498 DW_TAG_template_type_param types actually _defined_ as
21499 children of the parent class:
21500
21501 enum E {};
21502 template class <class Enum> Class{};
21503 Class<enum E> class_e;
21504
21505 1: DW_TAG_class_type (Class)
21506 2: DW_TAG_enumeration_type (E)
21507 3: DW_TAG_enumerator (enum1:0)
21508 3: DW_TAG_enumerator (enum2:1)
21509 ...
21510 2: DW_TAG_template_type_param
21511 DW_AT_type DW_FORM_ref_udata (E)
21512
21513 Besides being broken debug info, it can put GDB into an
21514 infinite loop. Consider:
21515
21516 When we're building the full name for Class<E>, we'll start
21517 at Class, and go look over its template type parameters,
21518 finding E. We'll then try to build the full name of E, and
21519 reach here. We're now trying to build the full name of E,
21520 and look over the parent DIE for containing scope. In the
21521 broken case, if we followed the parent DIE of E, we'd again
21522 find Class, and once again go look at its template type
21523 arguments, etc., etc. Simply don't consider such parent die
21524 as source-level parent of this die (it can't be, the language
21525 doesn't allow it), and break the loop here. */
21526 name = dwarf2_name (die, cu);
21527 parent_name = dwarf2_name (parent, cu);
21528 complaint (_("template param type '%s' defined within parent '%s'"),
21529 name ? name : "<unknown>",
21530 parent_name ? parent_name : "<unknown>");
21531 return "";
21532 }
21533 else
21534 switch (parent->tag)
21535 {
21536 case DW_TAG_namespace:
21537 parent_type = read_type_die (parent, cu);
21538 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
21539 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
21540 Work around this problem here. */
21541 if (cu->language == language_cplus
21542 && strcmp (TYPE_NAME (parent_type), "::") == 0)
21543 return "";
21544 /* We give a name to even anonymous namespaces. */
21545 return TYPE_NAME (parent_type);
21546 case DW_TAG_class_type:
21547 case DW_TAG_interface_type:
21548 case DW_TAG_structure_type:
21549 case DW_TAG_union_type:
21550 case DW_TAG_module:
21551 parent_type = read_type_die (parent, cu);
21552 if (TYPE_NAME (parent_type) != NULL)
21553 return TYPE_NAME (parent_type);
21554 else
21555 /* An anonymous structure is only allowed non-static data
21556 members; no typedefs, no member functions, et cetera.
21557 So it does not need a prefix. */
21558 return "";
21559 case DW_TAG_compile_unit:
21560 case DW_TAG_partial_unit:
21561 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
21562 if (cu->language == language_cplus
21563 && !dwarf2_per_objfile->types.empty ()
21564 && die->child != NULL
21565 && (die->tag == DW_TAG_class_type
21566 || die->tag == DW_TAG_structure_type
21567 || die->tag == DW_TAG_union_type))
21568 {
21569 const char *name = guess_full_die_structure_name (die, cu);
21570 if (name != NULL)
21571 return name;
21572 }
21573 return "";
21574 case DW_TAG_subprogram:
21575 /* Nested subroutines in Fortran get a prefix with the name
21576 of the parent's subroutine. */
21577 if (cu->language == language_fortran)
21578 {
21579 if ((die->tag == DW_TAG_subprogram)
21580 && (dwarf2_name (parent, cu) != NULL))
21581 return dwarf2_name (parent, cu);
21582 }
21583 return determine_prefix (parent, cu);
21584 case DW_TAG_enumeration_type:
21585 parent_type = read_type_die (parent, cu);
21586 if (TYPE_DECLARED_CLASS (parent_type))
21587 {
21588 if (TYPE_NAME (parent_type) != NULL)
21589 return TYPE_NAME (parent_type);
21590 return "";
21591 }
21592 /* Fall through. */
21593 default:
21594 return determine_prefix (parent, cu);
21595 }
21596 }
21597
21598 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
21599 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
21600 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
21601 an obconcat, otherwise allocate storage for the result. The CU argument is
21602 used to determine the language and hence, the appropriate separator. */
21603
21604 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
21605
21606 static char *
21607 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
21608 int physname, struct dwarf2_cu *cu)
21609 {
21610 const char *lead = "";
21611 const char *sep;
21612
21613 if (suffix == NULL || suffix[0] == '\0'
21614 || prefix == NULL || prefix[0] == '\0')
21615 sep = "";
21616 else if (cu->language == language_d)
21617 {
21618 /* For D, the 'main' function could be defined in any module, but it
21619 should never be prefixed. */
21620 if (strcmp (suffix, "D main") == 0)
21621 {
21622 prefix = "";
21623 sep = "";
21624 }
21625 else
21626 sep = ".";
21627 }
21628 else if (cu->language == language_fortran && physname)
21629 {
21630 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
21631 DW_AT_MIPS_linkage_name is preferred and used instead. */
21632
21633 lead = "__";
21634 sep = "_MOD_";
21635 }
21636 else
21637 sep = "::";
21638
21639 if (prefix == NULL)
21640 prefix = "";
21641 if (suffix == NULL)
21642 suffix = "";
21643
21644 if (obs == NULL)
21645 {
21646 char *retval
21647 = ((char *)
21648 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
21649
21650 strcpy (retval, lead);
21651 strcat (retval, prefix);
21652 strcat (retval, sep);
21653 strcat (retval, suffix);
21654 return retval;
21655 }
21656 else
21657 {
21658 /* We have an obstack. */
21659 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
21660 }
21661 }
21662
21663 /* Return sibling of die, NULL if no sibling. */
21664
21665 static struct die_info *
21666 sibling_die (struct die_info *die)
21667 {
21668 return die->sibling;
21669 }
21670
21671 /* Get name of a die, return NULL if not found. */
21672
21673 static const char *
21674 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
21675 struct obstack *obstack)
21676 {
21677 if (name && cu->language == language_cplus)
21678 {
21679 std::string canon_name = cp_canonicalize_string (name);
21680
21681 if (!canon_name.empty ())
21682 {
21683 if (canon_name != name)
21684 name = obstack_strdup (obstack, canon_name);
21685 }
21686 }
21687
21688 return name;
21689 }
21690
21691 /* Get name of a die, return NULL if not found.
21692 Anonymous namespaces are converted to their magic string. */
21693
21694 static const char *
21695 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
21696 {
21697 struct attribute *attr;
21698 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21699
21700 attr = dwarf2_attr (die, DW_AT_name, cu);
21701 if ((!attr || !DW_STRING (attr))
21702 && die->tag != DW_TAG_namespace
21703 && die->tag != DW_TAG_class_type
21704 && die->tag != DW_TAG_interface_type
21705 && die->tag != DW_TAG_structure_type
21706 && die->tag != DW_TAG_union_type)
21707 return NULL;
21708
21709 switch (die->tag)
21710 {
21711 case DW_TAG_compile_unit:
21712 case DW_TAG_partial_unit:
21713 /* Compilation units have a DW_AT_name that is a filename, not
21714 a source language identifier. */
21715 case DW_TAG_enumeration_type:
21716 case DW_TAG_enumerator:
21717 /* These tags always have simple identifiers already; no need
21718 to canonicalize them. */
21719 return DW_STRING (attr);
21720
21721 case DW_TAG_namespace:
21722 if (attr != NULL && DW_STRING (attr) != NULL)
21723 return DW_STRING (attr);
21724 return CP_ANONYMOUS_NAMESPACE_STR;
21725
21726 case DW_TAG_class_type:
21727 case DW_TAG_interface_type:
21728 case DW_TAG_structure_type:
21729 case DW_TAG_union_type:
21730 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
21731 structures or unions. These were of the form "._%d" in GCC 4.1,
21732 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
21733 and GCC 4.4. We work around this problem by ignoring these. */
21734 if (attr && DW_STRING (attr)
21735 && (startswith (DW_STRING (attr), "._")
21736 || startswith (DW_STRING (attr), "<anonymous")))
21737 return NULL;
21738
21739 /* GCC might emit a nameless typedef that has a linkage name. See
21740 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21741 if (!attr || DW_STRING (attr) == NULL)
21742 {
21743 attr = dw2_linkage_name_attr (die, cu);
21744 if (attr == NULL || DW_STRING (attr) == NULL)
21745 return NULL;
21746
21747 /* Avoid demangling DW_STRING (attr) the second time on a second
21748 call for the same DIE. */
21749 if (!DW_STRING_IS_CANONICAL (attr))
21750 {
21751 gdb::unique_xmalloc_ptr<char> demangled
21752 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
21753 if (demangled == nullptr)
21754 return nullptr;
21755
21756 const char *base;
21757
21758 /* FIXME: we already did this for the partial symbol... */
21759 DW_STRING (attr)
21760 = obstack_strdup (&objfile->per_bfd->storage_obstack,
21761 demangled.get ());
21762 DW_STRING_IS_CANONICAL (attr) = 1;
21763
21764 /* Strip any leading namespaces/classes, keep only the base name.
21765 DW_AT_name for named DIEs does not contain the prefixes. */
21766 base = strrchr (DW_STRING (attr), ':');
21767 if (base && base > DW_STRING (attr) && base[-1] == ':')
21768 return &base[1];
21769 else
21770 return DW_STRING (attr);
21771 }
21772 }
21773 break;
21774
21775 default:
21776 break;
21777 }
21778
21779 if (!DW_STRING_IS_CANONICAL (attr))
21780 {
21781 DW_STRING (attr)
21782 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
21783 &objfile->per_bfd->storage_obstack);
21784 DW_STRING_IS_CANONICAL (attr) = 1;
21785 }
21786 return DW_STRING (attr);
21787 }
21788
21789 /* Return the die that this die in an extension of, or NULL if there
21790 is none. *EXT_CU is the CU containing DIE on input, and the CU
21791 containing the return value on output. */
21792
21793 static struct die_info *
21794 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
21795 {
21796 struct attribute *attr;
21797
21798 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
21799 if (attr == NULL)
21800 return NULL;
21801
21802 return follow_die_ref (die, attr, ext_cu);
21803 }
21804
21805 /* A convenience function that returns an "unknown" DWARF name,
21806 including the value of V. STR is the name of the entity being
21807 printed, e.g., "TAG". */
21808
21809 static const char *
21810 dwarf_unknown (const char *str, unsigned v)
21811 {
21812 char *cell = get_print_cell ();
21813 xsnprintf (cell, PRINT_CELL_SIZE, "DW_%s_<unknown: %u>", str, v);
21814 return cell;
21815 }
21816
21817 /* Convert a DIE tag into its string name. */
21818
21819 static const char *
21820 dwarf_tag_name (unsigned tag)
21821 {
21822 const char *name = get_DW_TAG_name (tag);
21823
21824 if (name == NULL)
21825 return dwarf_unknown ("TAG", tag);
21826
21827 return name;
21828 }
21829
21830 /* Convert a DWARF attribute code into its string name. */
21831
21832 static const char *
21833 dwarf_attr_name (unsigned attr)
21834 {
21835 const char *name;
21836
21837 #ifdef MIPS /* collides with DW_AT_HP_block_index */
21838 if (attr == DW_AT_MIPS_fde)
21839 return "DW_AT_MIPS_fde";
21840 #else
21841 if (attr == DW_AT_HP_block_index)
21842 return "DW_AT_HP_block_index";
21843 #endif
21844
21845 name = get_DW_AT_name (attr);
21846
21847 if (name == NULL)
21848 return dwarf_unknown ("AT", attr);
21849
21850 return name;
21851 }
21852
21853 /* Convert a DWARF value form code into its string name. */
21854
21855 static const char *
21856 dwarf_form_name (unsigned form)
21857 {
21858 const char *name = get_DW_FORM_name (form);
21859
21860 if (name == NULL)
21861 return dwarf_unknown ("FORM", form);
21862
21863 return name;
21864 }
21865
21866 static const char *
21867 dwarf_bool_name (unsigned mybool)
21868 {
21869 if (mybool)
21870 return "TRUE";
21871 else
21872 return "FALSE";
21873 }
21874
21875 /* Convert a DWARF type code into its string name. */
21876
21877 static const char *
21878 dwarf_type_encoding_name (unsigned enc)
21879 {
21880 const char *name = get_DW_ATE_name (enc);
21881
21882 if (name == NULL)
21883 return dwarf_unknown ("ATE", enc);
21884
21885 return name;
21886 }
21887
21888 static void
21889 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
21890 {
21891 unsigned int i;
21892
21893 print_spaces (indent, f);
21894 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
21895 dwarf_tag_name (die->tag), die->abbrev,
21896 sect_offset_str (die->sect_off));
21897
21898 if (die->parent != NULL)
21899 {
21900 print_spaces (indent, f);
21901 fprintf_unfiltered (f, " parent at offset: %s\n",
21902 sect_offset_str (die->parent->sect_off));
21903 }
21904
21905 print_spaces (indent, f);
21906 fprintf_unfiltered (f, " has children: %s\n",
21907 dwarf_bool_name (die->child != NULL));
21908
21909 print_spaces (indent, f);
21910 fprintf_unfiltered (f, " attributes:\n");
21911
21912 for (i = 0; i < die->num_attrs; ++i)
21913 {
21914 print_spaces (indent, f);
21915 fprintf_unfiltered (f, " %s (%s) ",
21916 dwarf_attr_name (die->attrs[i].name),
21917 dwarf_form_name (die->attrs[i].form));
21918
21919 switch (die->attrs[i].form)
21920 {
21921 case DW_FORM_addr:
21922 case DW_FORM_addrx:
21923 case DW_FORM_GNU_addr_index:
21924 fprintf_unfiltered (f, "address: ");
21925 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
21926 break;
21927 case DW_FORM_block2:
21928 case DW_FORM_block4:
21929 case DW_FORM_block:
21930 case DW_FORM_block1:
21931 fprintf_unfiltered (f, "block: size %s",
21932 pulongest (DW_BLOCK (&die->attrs[i])->size));
21933 break;
21934 case DW_FORM_exprloc:
21935 fprintf_unfiltered (f, "expression: size %s",
21936 pulongest (DW_BLOCK (&die->attrs[i])->size));
21937 break;
21938 case DW_FORM_data16:
21939 fprintf_unfiltered (f, "constant of 16 bytes");
21940 break;
21941 case DW_FORM_ref_addr:
21942 fprintf_unfiltered (f, "ref address: ");
21943 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21944 break;
21945 case DW_FORM_GNU_ref_alt:
21946 fprintf_unfiltered (f, "alt ref address: ");
21947 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21948 break;
21949 case DW_FORM_ref1:
21950 case DW_FORM_ref2:
21951 case DW_FORM_ref4:
21952 case DW_FORM_ref8:
21953 case DW_FORM_ref_udata:
21954 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
21955 (long) (DW_UNSND (&die->attrs[i])));
21956 break;
21957 case DW_FORM_data1:
21958 case DW_FORM_data2:
21959 case DW_FORM_data4:
21960 case DW_FORM_data8:
21961 case DW_FORM_udata:
21962 case DW_FORM_sdata:
21963 fprintf_unfiltered (f, "constant: %s",
21964 pulongest (DW_UNSND (&die->attrs[i])));
21965 break;
21966 case DW_FORM_sec_offset:
21967 fprintf_unfiltered (f, "section offset: %s",
21968 pulongest (DW_UNSND (&die->attrs[i])));
21969 break;
21970 case DW_FORM_ref_sig8:
21971 fprintf_unfiltered (f, "signature: %s",
21972 hex_string (DW_SIGNATURE (&die->attrs[i])));
21973 break;
21974 case DW_FORM_string:
21975 case DW_FORM_strp:
21976 case DW_FORM_line_strp:
21977 case DW_FORM_strx:
21978 case DW_FORM_GNU_str_index:
21979 case DW_FORM_GNU_strp_alt:
21980 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
21981 DW_STRING (&die->attrs[i])
21982 ? DW_STRING (&die->attrs[i]) : "",
21983 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
21984 break;
21985 case DW_FORM_flag:
21986 if (DW_UNSND (&die->attrs[i]))
21987 fprintf_unfiltered (f, "flag: TRUE");
21988 else
21989 fprintf_unfiltered (f, "flag: FALSE");
21990 break;
21991 case DW_FORM_flag_present:
21992 fprintf_unfiltered (f, "flag: TRUE");
21993 break;
21994 case DW_FORM_indirect:
21995 /* The reader will have reduced the indirect form to
21996 the "base form" so this form should not occur. */
21997 fprintf_unfiltered (f,
21998 "unexpected attribute form: DW_FORM_indirect");
21999 break;
22000 case DW_FORM_implicit_const:
22001 fprintf_unfiltered (f, "constant: %s",
22002 plongest (DW_SND (&die->attrs[i])));
22003 break;
22004 default:
22005 fprintf_unfiltered (f, "unsupported attribute form: %d.",
22006 die->attrs[i].form);
22007 break;
22008 }
22009 fprintf_unfiltered (f, "\n");
22010 }
22011 }
22012
22013 static void
22014 dump_die_for_error (struct die_info *die)
22015 {
22016 dump_die_shallow (gdb_stderr, 0, die);
22017 }
22018
22019 static void
22020 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22021 {
22022 int indent = level * 4;
22023
22024 gdb_assert (die != NULL);
22025
22026 if (level >= max_level)
22027 return;
22028
22029 dump_die_shallow (f, indent, die);
22030
22031 if (die->child != NULL)
22032 {
22033 print_spaces (indent, f);
22034 fprintf_unfiltered (f, " Children:");
22035 if (level + 1 < max_level)
22036 {
22037 fprintf_unfiltered (f, "\n");
22038 dump_die_1 (f, level + 1, max_level, die->child);
22039 }
22040 else
22041 {
22042 fprintf_unfiltered (f,
22043 " [not printed, max nesting level reached]\n");
22044 }
22045 }
22046
22047 if (die->sibling != NULL && level > 0)
22048 {
22049 dump_die_1 (f, level, max_level, die->sibling);
22050 }
22051 }
22052
22053 /* This is called from the pdie macro in gdbinit.in.
22054 It's not static so gcc will keep a copy callable from gdb. */
22055
22056 void
22057 dump_die (struct die_info *die, int max_level)
22058 {
22059 dump_die_1 (gdb_stdlog, 0, max_level, die);
22060 }
22061
22062 static void
22063 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
22064 {
22065 void **slot;
22066
22067 slot = htab_find_slot_with_hash (cu->die_hash, die,
22068 to_underlying (die->sect_off),
22069 INSERT);
22070
22071 *slot = die;
22072 }
22073
22074 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
22075 required kind. */
22076
22077 static sect_offset
22078 dwarf2_get_ref_die_offset (const struct attribute *attr)
22079 {
22080 if (attr->form_is_ref ())
22081 return (sect_offset) DW_UNSND (attr);
22082
22083 complaint (_("unsupported die ref attribute form: '%s'"),
22084 dwarf_form_name (attr->form));
22085 return {};
22086 }
22087
22088 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
22089 * the value held by the attribute is not constant. */
22090
22091 static LONGEST
22092 dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
22093 {
22094 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
22095 return DW_SND (attr);
22096 else if (attr->form == DW_FORM_udata
22097 || attr->form == DW_FORM_data1
22098 || attr->form == DW_FORM_data2
22099 || attr->form == DW_FORM_data4
22100 || attr->form == DW_FORM_data8)
22101 return DW_UNSND (attr);
22102 else
22103 {
22104 /* For DW_FORM_data16 see attribute::form_is_constant. */
22105 complaint (_("Attribute value is not a constant (%s)"),
22106 dwarf_form_name (attr->form));
22107 return default_value;
22108 }
22109 }
22110
22111 /* Follow reference or signature attribute ATTR of SRC_DIE.
22112 On entry *REF_CU is the CU of SRC_DIE.
22113 On exit *REF_CU is the CU of the result. */
22114
22115 static struct die_info *
22116 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
22117 struct dwarf2_cu **ref_cu)
22118 {
22119 struct die_info *die;
22120
22121 if (attr->form_is_ref ())
22122 die = follow_die_ref (src_die, attr, ref_cu);
22123 else if (attr->form == DW_FORM_ref_sig8)
22124 die = follow_die_sig (src_die, attr, ref_cu);
22125 else
22126 {
22127 dump_die_for_error (src_die);
22128 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
22129 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
22130 }
22131
22132 return die;
22133 }
22134
22135 /* Follow reference OFFSET.
22136 On entry *REF_CU is the CU of the source die referencing OFFSET.
22137 On exit *REF_CU is the CU of the result.
22138 Returns NULL if OFFSET is invalid. */
22139
22140 static struct die_info *
22141 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
22142 struct dwarf2_cu **ref_cu)
22143 {
22144 struct die_info temp_die;
22145 struct dwarf2_cu *target_cu, *cu = *ref_cu;
22146 struct dwarf2_per_objfile *dwarf2_per_objfile
22147 = cu->per_cu->dwarf2_per_objfile;
22148
22149 gdb_assert (cu->per_cu != NULL);
22150
22151 target_cu = cu;
22152
22153 if (cu->per_cu->is_debug_types)
22154 {
22155 /* .debug_types CUs cannot reference anything outside their CU.
22156 If they need to, they have to reference a signatured type via
22157 DW_FORM_ref_sig8. */
22158 if (!cu->header.offset_in_cu_p (sect_off))
22159 return NULL;
22160 }
22161 else if (offset_in_dwz != cu->per_cu->is_dwz
22162 || !cu->header.offset_in_cu_p (sect_off))
22163 {
22164 struct dwarf2_per_cu_data *per_cu;
22165
22166 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
22167 dwarf2_per_objfile);
22168
22169 /* If necessary, add it to the queue and load its DIEs. */
22170 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
22171 load_full_comp_unit (per_cu, false, cu->language);
22172
22173 target_cu = per_cu->cu;
22174 }
22175 else if (cu->dies == NULL)
22176 {
22177 /* We're loading full DIEs during partial symbol reading. */
22178 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
22179 load_full_comp_unit (cu->per_cu, false, language_minimal);
22180 }
22181
22182 *ref_cu = target_cu;
22183 temp_die.sect_off = sect_off;
22184
22185 if (target_cu != cu)
22186 target_cu->ancestor = cu;
22187
22188 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
22189 &temp_die,
22190 to_underlying (sect_off));
22191 }
22192
22193 /* Follow reference attribute ATTR of SRC_DIE.
22194 On entry *REF_CU is the CU of SRC_DIE.
22195 On exit *REF_CU is the CU of the result. */
22196
22197 static struct die_info *
22198 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
22199 struct dwarf2_cu **ref_cu)
22200 {
22201 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
22202 struct dwarf2_cu *cu = *ref_cu;
22203 struct die_info *die;
22204
22205 die = follow_die_offset (sect_off,
22206 (attr->form == DW_FORM_GNU_ref_alt
22207 || cu->per_cu->is_dwz),
22208 ref_cu);
22209 if (!die)
22210 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22211 "at %s [in module %s]"),
22212 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
22213 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
22214
22215 return die;
22216 }
22217
22218 /* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
22219 Returned value is intended for DW_OP_call*. Returned
22220 dwarf2_locexpr_baton->data has lifetime of
22221 PER_CU->DWARF2_PER_OBJFILE->OBJFILE. */
22222
22223 struct dwarf2_locexpr_baton
22224 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
22225 struct dwarf2_per_cu_data *per_cu,
22226 CORE_ADDR (*get_frame_pc) (void *baton),
22227 void *baton, bool resolve_abstract_p)
22228 {
22229 struct dwarf2_cu *cu;
22230 struct die_info *die;
22231 struct attribute *attr;
22232 struct dwarf2_locexpr_baton retval;
22233 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
22234 struct objfile *objfile = dwarf2_per_objfile->objfile;
22235
22236 if (per_cu->cu == NULL)
22237 load_cu (per_cu, false);
22238 cu = per_cu->cu;
22239 if (cu == NULL)
22240 {
22241 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22242 Instead just throw an error, not much else we can do. */
22243 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22244 sect_offset_str (sect_off), objfile_name (objfile));
22245 }
22246
22247 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22248 if (!die)
22249 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22250 sect_offset_str (sect_off), objfile_name (objfile));
22251
22252 attr = dwarf2_attr (die, DW_AT_location, cu);
22253 if (!attr && resolve_abstract_p
22254 && (dwarf2_per_objfile->abstract_to_concrete.find (die->sect_off)
22255 != dwarf2_per_objfile->abstract_to_concrete.end ()))
22256 {
22257 CORE_ADDR pc = (*get_frame_pc) (baton);
22258 CORE_ADDR baseaddr = objfile->text_section_offset ();
22259 struct gdbarch *gdbarch = get_objfile_arch (objfile);
22260
22261 for (const auto &cand_off
22262 : dwarf2_per_objfile->abstract_to_concrete[die->sect_off])
22263 {
22264 struct dwarf2_cu *cand_cu = cu;
22265 struct die_info *cand
22266 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
22267 if (!cand
22268 || !cand->parent
22269 || cand->parent->tag != DW_TAG_subprogram)
22270 continue;
22271
22272 CORE_ADDR pc_low, pc_high;
22273 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
22274 if (pc_low == ((CORE_ADDR) -1))
22275 continue;
22276 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
22277 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
22278 if (!(pc_low <= pc && pc < pc_high))
22279 continue;
22280
22281 die = cand;
22282 attr = dwarf2_attr (die, DW_AT_location, cu);
22283 break;
22284 }
22285 }
22286
22287 if (!attr)
22288 {
22289 /* DWARF: "If there is no such attribute, then there is no effect.".
22290 DATA is ignored if SIZE is 0. */
22291
22292 retval.data = NULL;
22293 retval.size = 0;
22294 }
22295 else if (attr->form_is_section_offset ())
22296 {
22297 struct dwarf2_loclist_baton loclist_baton;
22298 CORE_ADDR pc = (*get_frame_pc) (baton);
22299 size_t size;
22300
22301 fill_in_loclist_baton (cu, &loclist_baton, attr);
22302
22303 retval.data = dwarf2_find_location_expression (&loclist_baton,
22304 &size, pc);
22305 retval.size = size;
22306 }
22307 else
22308 {
22309 if (!attr->form_is_block ())
22310 error (_("Dwarf Error: DIE at %s referenced in module %s "
22311 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
22312 sect_offset_str (sect_off), objfile_name (objfile));
22313
22314 retval.data = DW_BLOCK (attr)->data;
22315 retval.size = DW_BLOCK (attr)->size;
22316 }
22317 retval.per_cu = cu->per_cu;
22318
22319 age_cached_comp_units (dwarf2_per_objfile);
22320
22321 return retval;
22322 }
22323
22324 /* Like dwarf2_fetch_die_loc_sect_off, but take a CU
22325 offset. */
22326
22327 struct dwarf2_locexpr_baton
22328 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
22329 struct dwarf2_per_cu_data *per_cu,
22330 CORE_ADDR (*get_frame_pc) (void *baton),
22331 void *baton)
22332 {
22333 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
22334
22335 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
22336 }
22337
22338 /* Write a constant of a given type as target-ordered bytes into
22339 OBSTACK. */
22340
22341 static const gdb_byte *
22342 write_constant_as_bytes (struct obstack *obstack,
22343 enum bfd_endian byte_order,
22344 struct type *type,
22345 ULONGEST value,
22346 LONGEST *len)
22347 {
22348 gdb_byte *result;
22349
22350 *len = TYPE_LENGTH (type);
22351 result = (gdb_byte *) obstack_alloc (obstack, *len);
22352 store_unsigned_integer (result, *len, byte_order, value);
22353
22354 return result;
22355 }
22356
22357 /* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
22358 pointer to the constant bytes and set LEN to the length of the
22359 data. If memory is needed, allocate it on OBSTACK. If the DIE
22360 does not have a DW_AT_const_value, return NULL. */
22361
22362 const gdb_byte *
22363 dwarf2_fetch_constant_bytes (sect_offset sect_off,
22364 struct dwarf2_per_cu_data *per_cu,
22365 struct obstack *obstack,
22366 LONGEST *len)
22367 {
22368 struct dwarf2_cu *cu;
22369 struct die_info *die;
22370 struct attribute *attr;
22371 const gdb_byte *result = NULL;
22372 struct type *type;
22373 LONGEST value;
22374 enum bfd_endian byte_order;
22375 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
22376
22377 if (per_cu->cu == NULL)
22378 load_cu (per_cu, false);
22379 cu = per_cu->cu;
22380 if (cu == NULL)
22381 {
22382 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22383 Instead just throw an error, not much else we can do. */
22384 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22385 sect_offset_str (sect_off), objfile_name (objfile));
22386 }
22387
22388 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22389 if (!die)
22390 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22391 sect_offset_str (sect_off), objfile_name (objfile));
22392
22393 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22394 if (attr == NULL)
22395 return NULL;
22396
22397 byte_order = (bfd_big_endian (objfile->obfd)
22398 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22399
22400 switch (attr->form)
22401 {
22402 case DW_FORM_addr:
22403 case DW_FORM_addrx:
22404 case DW_FORM_GNU_addr_index:
22405 {
22406 gdb_byte *tem;
22407
22408 *len = cu->header.addr_size;
22409 tem = (gdb_byte *) obstack_alloc (obstack, *len);
22410 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
22411 result = tem;
22412 }
22413 break;
22414 case DW_FORM_string:
22415 case DW_FORM_strp:
22416 case DW_FORM_strx:
22417 case DW_FORM_GNU_str_index:
22418 case DW_FORM_GNU_strp_alt:
22419 /* DW_STRING is already allocated on the objfile obstack, point
22420 directly to it. */
22421 result = (const gdb_byte *) DW_STRING (attr);
22422 *len = strlen (DW_STRING (attr));
22423 break;
22424 case DW_FORM_block1:
22425 case DW_FORM_block2:
22426 case DW_FORM_block4:
22427 case DW_FORM_block:
22428 case DW_FORM_exprloc:
22429 case DW_FORM_data16:
22430 result = DW_BLOCK (attr)->data;
22431 *len = DW_BLOCK (attr)->size;
22432 break;
22433
22434 /* The DW_AT_const_value attributes are supposed to carry the
22435 symbol's value "represented as it would be on the target
22436 architecture." By the time we get here, it's already been
22437 converted to host endianness, so we just need to sign- or
22438 zero-extend it as appropriate. */
22439 case DW_FORM_data1:
22440 type = die_type (die, cu);
22441 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
22442 if (result == NULL)
22443 result = write_constant_as_bytes (obstack, byte_order,
22444 type, value, len);
22445 break;
22446 case DW_FORM_data2:
22447 type = die_type (die, cu);
22448 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
22449 if (result == NULL)
22450 result = write_constant_as_bytes (obstack, byte_order,
22451 type, value, len);
22452 break;
22453 case DW_FORM_data4:
22454 type = die_type (die, cu);
22455 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
22456 if (result == NULL)
22457 result = write_constant_as_bytes (obstack, byte_order,
22458 type, value, len);
22459 break;
22460 case DW_FORM_data8:
22461 type = die_type (die, cu);
22462 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
22463 if (result == NULL)
22464 result = write_constant_as_bytes (obstack, byte_order,
22465 type, value, len);
22466 break;
22467
22468 case DW_FORM_sdata:
22469 case DW_FORM_implicit_const:
22470 type = die_type (die, cu);
22471 result = write_constant_as_bytes (obstack, byte_order,
22472 type, DW_SND (attr), len);
22473 break;
22474
22475 case DW_FORM_udata:
22476 type = die_type (die, cu);
22477 result = write_constant_as_bytes (obstack, byte_order,
22478 type, DW_UNSND (attr), len);
22479 break;
22480
22481 default:
22482 complaint (_("unsupported const value attribute form: '%s'"),
22483 dwarf_form_name (attr->form));
22484 break;
22485 }
22486
22487 return result;
22488 }
22489
22490 /* Return the type of the die at OFFSET in PER_CU. Return NULL if no
22491 valid type for this die is found. */
22492
22493 struct type *
22494 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
22495 struct dwarf2_per_cu_data *per_cu)
22496 {
22497 struct dwarf2_cu *cu;
22498 struct die_info *die;
22499
22500 if (per_cu->cu == NULL)
22501 load_cu (per_cu, false);
22502 cu = per_cu->cu;
22503 if (!cu)
22504 return NULL;
22505
22506 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22507 if (!die)
22508 return NULL;
22509
22510 return die_type (die, cu);
22511 }
22512
22513 /* Return the type of the DIE at DIE_OFFSET in the CU named by
22514 PER_CU. */
22515
22516 struct type *
22517 dwarf2_get_die_type (cu_offset die_offset,
22518 struct dwarf2_per_cu_data *per_cu)
22519 {
22520 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
22521 return get_die_type_at_offset (die_offset_sect, per_cu);
22522 }
22523
22524 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
22525 On entry *REF_CU is the CU of SRC_DIE.
22526 On exit *REF_CU is the CU of the result.
22527 Returns NULL if the referenced DIE isn't found. */
22528
22529 static struct die_info *
22530 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
22531 struct dwarf2_cu **ref_cu)
22532 {
22533 struct die_info temp_die;
22534 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
22535 struct die_info *die;
22536
22537 /* While it might be nice to assert sig_type->type == NULL here,
22538 we can get here for DW_AT_imported_declaration where we need
22539 the DIE not the type. */
22540
22541 /* If necessary, add it to the queue and load its DIEs. */
22542
22543 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
22544 read_signatured_type (sig_type);
22545
22546 sig_cu = sig_type->per_cu.cu;
22547 gdb_assert (sig_cu != NULL);
22548 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
22549 temp_die.sect_off = sig_type->type_offset_in_section;
22550 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
22551 to_underlying (temp_die.sect_off));
22552 if (die)
22553 {
22554 struct dwarf2_per_objfile *dwarf2_per_objfile
22555 = (*ref_cu)->per_cu->dwarf2_per_objfile;
22556
22557 /* For .gdb_index version 7 keep track of included TUs.
22558 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
22559 if (dwarf2_per_objfile->index_table != NULL
22560 && dwarf2_per_objfile->index_table->version <= 7)
22561 {
22562 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
22563 }
22564
22565 *ref_cu = sig_cu;
22566 if (sig_cu != cu)
22567 sig_cu->ancestor = cu;
22568
22569 return die;
22570 }
22571
22572 return NULL;
22573 }
22574
22575 /* Follow signatured type referenced by ATTR in SRC_DIE.
22576 On entry *REF_CU is the CU of SRC_DIE.
22577 On exit *REF_CU is the CU of the result.
22578 The result is the DIE of the type.
22579 If the referenced type cannot be found an error is thrown. */
22580
22581 static struct die_info *
22582 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
22583 struct dwarf2_cu **ref_cu)
22584 {
22585 ULONGEST signature = DW_SIGNATURE (attr);
22586 struct signatured_type *sig_type;
22587 struct die_info *die;
22588
22589 gdb_assert (attr->form == DW_FORM_ref_sig8);
22590
22591 sig_type = lookup_signatured_type (*ref_cu, signature);
22592 /* sig_type will be NULL if the signatured type is missing from
22593 the debug info. */
22594 if (sig_type == NULL)
22595 {
22596 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22597 " from DIE at %s [in module %s]"),
22598 hex_string (signature), sect_offset_str (src_die->sect_off),
22599 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
22600 }
22601
22602 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
22603 if (die == NULL)
22604 {
22605 dump_die_for_error (src_die);
22606 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22607 " from DIE at %s [in module %s]"),
22608 hex_string (signature), sect_offset_str (src_die->sect_off),
22609 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
22610 }
22611
22612 return die;
22613 }
22614
22615 /* Get the type specified by SIGNATURE referenced in DIE/CU,
22616 reading in and processing the type unit if necessary. */
22617
22618 static struct type *
22619 get_signatured_type (struct die_info *die, ULONGEST signature,
22620 struct dwarf2_cu *cu)
22621 {
22622 struct dwarf2_per_objfile *dwarf2_per_objfile
22623 = cu->per_cu->dwarf2_per_objfile;
22624 struct signatured_type *sig_type;
22625 struct dwarf2_cu *type_cu;
22626 struct die_info *type_die;
22627 struct type *type;
22628
22629 sig_type = lookup_signatured_type (cu, signature);
22630 /* sig_type will be NULL if the signatured type is missing from
22631 the debug info. */
22632 if (sig_type == NULL)
22633 {
22634 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22635 " from DIE at %s [in module %s]"),
22636 hex_string (signature), sect_offset_str (die->sect_off),
22637 objfile_name (dwarf2_per_objfile->objfile));
22638 return build_error_marker_type (cu, die);
22639 }
22640
22641 /* If we already know the type we're done. */
22642 if (sig_type->type != NULL)
22643 return sig_type->type;
22644
22645 type_cu = cu;
22646 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
22647 if (type_die != NULL)
22648 {
22649 /* N.B. We need to call get_die_type to ensure only one type for this DIE
22650 is created. This is important, for example, because for c++ classes
22651 we need TYPE_NAME set which is only done by new_symbol. Blech. */
22652 type = read_type_die (type_die, type_cu);
22653 if (type == NULL)
22654 {
22655 complaint (_("Dwarf Error: Cannot build signatured type %s"
22656 " referenced from DIE at %s [in module %s]"),
22657 hex_string (signature), sect_offset_str (die->sect_off),
22658 objfile_name (dwarf2_per_objfile->objfile));
22659 type = build_error_marker_type (cu, die);
22660 }
22661 }
22662 else
22663 {
22664 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22665 " from DIE at %s [in module %s]"),
22666 hex_string (signature), sect_offset_str (die->sect_off),
22667 objfile_name (dwarf2_per_objfile->objfile));
22668 type = build_error_marker_type (cu, die);
22669 }
22670 sig_type->type = type;
22671
22672 return type;
22673 }
22674
22675 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
22676 reading in and processing the type unit if necessary. */
22677
22678 static struct type *
22679 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
22680 struct dwarf2_cu *cu) /* ARI: editCase function */
22681 {
22682 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
22683 if (attr->form_is_ref ())
22684 {
22685 struct dwarf2_cu *type_cu = cu;
22686 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
22687
22688 return read_type_die (type_die, type_cu);
22689 }
22690 else if (attr->form == DW_FORM_ref_sig8)
22691 {
22692 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
22693 }
22694 else
22695 {
22696 struct dwarf2_per_objfile *dwarf2_per_objfile
22697 = cu->per_cu->dwarf2_per_objfile;
22698
22699 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
22700 " at %s [in module %s]"),
22701 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
22702 objfile_name (dwarf2_per_objfile->objfile));
22703 return build_error_marker_type (cu, die);
22704 }
22705 }
22706
22707 /* Load the DIEs associated with type unit PER_CU into memory. */
22708
22709 static void
22710 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
22711 {
22712 struct signatured_type *sig_type;
22713
22714 /* Caller is responsible for ensuring type_unit_groups don't get here. */
22715 gdb_assert (! per_cu->type_unit_group_p ());
22716
22717 /* We have the per_cu, but we need the signatured_type.
22718 Fortunately this is an easy translation. */
22719 gdb_assert (per_cu->is_debug_types);
22720 sig_type = (struct signatured_type *) per_cu;
22721
22722 gdb_assert (per_cu->cu == NULL);
22723
22724 read_signatured_type (sig_type);
22725
22726 gdb_assert (per_cu->cu != NULL);
22727 }
22728
22729 /* Read in a signatured type and build its CU and DIEs.
22730 If the type is a stub for the real type in a DWO file,
22731 read in the real type from the DWO file as well. */
22732
22733 static void
22734 read_signatured_type (struct signatured_type *sig_type)
22735 {
22736 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
22737
22738 gdb_assert (per_cu->is_debug_types);
22739 gdb_assert (per_cu->cu == NULL);
22740
22741 cutu_reader reader (per_cu, NULL, 0, false);
22742
22743 if (!reader.dummy_p)
22744 {
22745 struct dwarf2_cu *cu = reader.cu;
22746 const gdb_byte *info_ptr = reader.info_ptr;
22747
22748 gdb_assert (cu->die_hash == NULL);
22749 cu->die_hash =
22750 htab_create_alloc_ex (cu->header.length / 12,
22751 die_hash,
22752 die_eq,
22753 NULL,
22754 &cu->comp_unit_obstack,
22755 hashtab_obstack_allocate,
22756 dummy_obstack_deallocate);
22757
22758 if (reader.comp_unit_die->has_children)
22759 reader.comp_unit_die->child
22760 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
22761 reader.comp_unit_die);
22762 cu->dies = reader.comp_unit_die;
22763 /* comp_unit_die is not stored in die_hash, no need. */
22764
22765 /* We try not to read any attributes in this function, because
22766 not all CUs needed for references have been loaded yet, and
22767 symbol table processing isn't initialized. But we have to
22768 set the CU language, or we won't be able to build types
22769 correctly. Similarly, if we do not read the producer, we can
22770 not apply producer-specific interpretation. */
22771 prepare_one_comp_unit (cu, cu->dies, language_minimal);
22772
22773 reader.keep ();
22774 }
22775
22776 sig_type->per_cu.tu_read = 1;
22777 }
22778
22779 /* Decode simple location descriptions.
22780 Given a pointer to a dwarf block that defines a location, compute
22781 the location and return the value.
22782
22783 NOTE drow/2003-11-18: This function is called in two situations
22784 now: for the address of static or global variables (partial symbols
22785 only) and for offsets into structures which are expected to be
22786 (more or less) constant. The partial symbol case should go away,
22787 and only the constant case should remain. That will let this
22788 function complain more accurately. A few special modes are allowed
22789 without complaint for global variables (for instance, global
22790 register values and thread-local values).
22791
22792 A location description containing no operations indicates that the
22793 object is optimized out. The return value is 0 for that case.
22794 FIXME drow/2003-11-16: No callers check for this case any more; soon all
22795 callers will only want a very basic result and this can become a
22796 complaint.
22797
22798 Note that stack[0] is unused except as a default error return. */
22799
22800 static CORE_ADDR
22801 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
22802 {
22803 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22804 size_t i;
22805 size_t size = blk->size;
22806 const gdb_byte *data = blk->data;
22807 CORE_ADDR stack[64];
22808 int stacki;
22809 unsigned int bytes_read, unsnd;
22810 gdb_byte op;
22811
22812 i = 0;
22813 stacki = 0;
22814 stack[stacki] = 0;
22815 stack[++stacki] = 0;
22816
22817 while (i < size)
22818 {
22819 op = data[i++];
22820 switch (op)
22821 {
22822 case DW_OP_lit0:
22823 case DW_OP_lit1:
22824 case DW_OP_lit2:
22825 case DW_OP_lit3:
22826 case DW_OP_lit4:
22827 case DW_OP_lit5:
22828 case DW_OP_lit6:
22829 case DW_OP_lit7:
22830 case DW_OP_lit8:
22831 case DW_OP_lit9:
22832 case DW_OP_lit10:
22833 case DW_OP_lit11:
22834 case DW_OP_lit12:
22835 case DW_OP_lit13:
22836 case DW_OP_lit14:
22837 case DW_OP_lit15:
22838 case DW_OP_lit16:
22839 case DW_OP_lit17:
22840 case DW_OP_lit18:
22841 case DW_OP_lit19:
22842 case DW_OP_lit20:
22843 case DW_OP_lit21:
22844 case DW_OP_lit22:
22845 case DW_OP_lit23:
22846 case DW_OP_lit24:
22847 case DW_OP_lit25:
22848 case DW_OP_lit26:
22849 case DW_OP_lit27:
22850 case DW_OP_lit28:
22851 case DW_OP_lit29:
22852 case DW_OP_lit30:
22853 case DW_OP_lit31:
22854 stack[++stacki] = op - DW_OP_lit0;
22855 break;
22856
22857 case DW_OP_reg0:
22858 case DW_OP_reg1:
22859 case DW_OP_reg2:
22860 case DW_OP_reg3:
22861 case DW_OP_reg4:
22862 case DW_OP_reg5:
22863 case DW_OP_reg6:
22864 case DW_OP_reg7:
22865 case DW_OP_reg8:
22866 case DW_OP_reg9:
22867 case DW_OP_reg10:
22868 case DW_OP_reg11:
22869 case DW_OP_reg12:
22870 case DW_OP_reg13:
22871 case DW_OP_reg14:
22872 case DW_OP_reg15:
22873 case DW_OP_reg16:
22874 case DW_OP_reg17:
22875 case DW_OP_reg18:
22876 case DW_OP_reg19:
22877 case DW_OP_reg20:
22878 case DW_OP_reg21:
22879 case DW_OP_reg22:
22880 case DW_OP_reg23:
22881 case DW_OP_reg24:
22882 case DW_OP_reg25:
22883 case DW_OP_reg26:
22884 case DW_OP_reg27:
22885 case DW_OP_reg28:
22886 case DW_OP_reg29:
22887 case DW_OP_reg30:
22888 case DW_OP_reg31:
22889 stack[++stacki] = op - DW_OP_reg0;
22890 if (i < size)
22891 dwarf2_complex_location_expr_complaint ();
22892 break;
22893
22894 case DW_OP_regx:
22895 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
22896 i += bytes_read;
22897 stack[++stacki] = unsnd;
22898 if (i < size)
22899 dwarf2_complex_location_expr_complaint ();
22900 break;
22901
22902 case DW_OP_addr:
22903 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
22904 &bytes_read);
22905 i += bytes_read;
22906 break;
22907
22908 case DW_OP_const1u:
22909 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
22910 i += 1;
22911 break;
22912
22913 case DW_OP_const1s:
22914 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
22915 i += 1;
22916 break;
22917
22918 case DW_OP_const2u:
22919 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
22920 i += 2;
22921 break;
22922
22923 case DW_OP_const2s:
22924 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
22925 i += 2;
22926 break;
22927
22928 case DW_OP_const4u:
22929 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
22930 i += 4;
22931 break;
22932
22933 case DW_OP_const4s:
22934 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
22935 i += 4;
22936 break;
22937
22938 case DW_OP_const8u:
22939 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
22940 i += 8;
22941 break;
22942
22943 case DW_OP_constu:
22944 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
22945 &bytes_read);
22946 i += bytes_read;
22947 break;
22948
22949 case DW_OP_consts:
22950 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
22951 i += bytes_read;
22952 break;
22953
22954 case DW_OP_dup:
22955 stack[stacki + 1] = stack[stacki];
22956 stacki++;
22957 break;
22958
22959 case DW_OP_plus:
22960 stack[stacki - 1] += stack[stacki];
22961 stacki--;
22962 break;
22963
22964 case DW_OP_plus_uconst:
22965 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
22966 &bytes_read);
22967 i += bytes_read;
22968 break;
22969
22970 case DW_OP_minus:
22971 stack[stacki - 1] -= stack[stacki];
22972 stacki--;
22973 break;
22974
22975 case DW_OP_deref:
22976 /* If we're not the last op, then we definitely can't encode
22977 this using GDB's address_class enum. This is valid for partial
22978 global symbols, although the variable's address will be bogus
22979 in the psymtab. */
22980 if (i < size)
22981 dwarf2_complex_location_expr_complaint ();
22982 break;
22983
22984 case DW_OP_GNU_push_tls_address:
22985 case DW_OP_form_tls_address:
22986 /* The top of the stack has the offset from the beginning
22987 of the thread control block at which the variable is located. */
22988 /* Nothing should follow this operator, so the top of stack would
22989 be returned. */
22990 /* This is valid for partial global symbols, but the variable's
22991 address will be bogus in the psymtab. Make it always at least
22992 non-zero to not look as a variable garbage collected by linker
22993 which have DW_OP_addr 0. */
22994 if (i < size)
22995 dwarf2_complex_location_expr_complaint ();
22996 stack[stacki]++;
22997 break;
22998
22999 case DW_OP_GNU_uninit:
23000 break;
23001
23002 case DW_OP_addrx:
23003 case DW_OP_GNU_addr_index:
23004 case DW_OP_GNU_const_index:
23005 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23006 &bytes_read);
23007 i += bytes_read;
23008 break;
23009
23010 default:
23011 {
23012 const char *name = get_DW_OP_name (op);
23013
23014 if (name)
23015 complaint (_("unsupported stack op: '%s'"),
23016 name);
23017 else
23018 complaint (_("unsupported stack op: '%02x'"),
23019 op);
23020 }
23021
23022 return (stack[stacki]);
23023 }
23024
23025 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23026 outside of the allocated space. Also enforce minimum>0. */
23027 if (stacki >= ARRAY_SIZE (stack) - 1)
23028 {
23029 complaint (_("location description stack overflow"));
23030 return 0;
23031 }
23032
23033 if (stacki <= 0)
23034 {
23035 complaint (_("location description stack underflow"));
23036 return 0;
23037 }
23038 }
23039 return (stack[stacki]);
23040 }
23041
23042 /* memory allocation interface */
23043
23044 static struct dwarf_block *
23045 dwarf_alloc_block (struct dwarf2_cu *cu)
23046 {
23047 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
23048 }
23049
23050 static struct die_info *
23051 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
23052 {
23053 struct die_info *die;
23054 size_t size = sizeof (struct die_info);
23055
23056 if (num_attrs > 1)
23057 size += (num_attrs - 1) * sizeof (struct attribute);
23058
23059 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
23060 memset (die, 0, sizeof (struct die_info));
23061 return (die);
23062 }
23063
23064 \f
23065 /* Macro support. */
23066
23067 static struct macro_source_file *
23068 macro_start_file (struct dwarf2_cu *cu,
23069 int file, int line,
23070 struct macro_source_file *current_file,
23071 struct line_header *lh)
23072 {
23073 /* File name relative to the compilation directory of this source file. */
23074 gdb::unique_xmalloc_ptr<char> file_name = lh->file_file_name (file);
23075
23076 if (! current_file)
23077 {
23078 /* Note: We don't create a macro table for this compilation unit
23079 at all until we actually get a filename. */
23080 struct macro_table *macro_table = cu->get_builder ()->get_macro_table ();
23081
23082 /* If we have no current file, then this must be the start_file
23083 directive for the compilation unit's main source file. */
23084 current_file = macro_set_main (macro_table, file_name.get ());
23085 macro_define_special (macro_table);
23086 }
23087 else
23088 current_file = macro_include (current_file, line, file_name.get ());
23089
23090 return current_file;
23091 }
23092
23093 static const char *
23094 consume_improper_spaces (const char *p, const char *body)
23095 {
23096 if (*p == ' ')
23097 {
23098 complaint (_("macro definition contains spaces "
23099 "in formal argument list:\n`%s'"),
23100 body);
23101
23102 while (*p == ' ')
23103 p++;
23104 }
23105
23106 return p;
23107 }
23108
23109
23110 static void
23111 parse_macro_definition (struct macro_source_file *file, int line,
23112 const char *body)
23113 {
23114 const char *p;
23115
23116 /* The body string takes one of two forms. For object-like macro
23117 definitions, it should be:
23118
23119 <macro name> " " <definition>
23120
23121 For function-like macro definitions, it should be:
23122
23123 <macro name> "() " <definition>
23124 or
23125 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
23126
23127 Spaces may appear only where explicitly indicated, and in the
23128 <definition>.
23129
23130 The Dwarf 2 spec says that an object-like macro's name is always
23131 followed by a space, but versions of GCC around March 2002 omit
23132 the space when the macro's definition is the empty string.
23133
23134 The Dwarf 2 spec says that there should be no spaces between the
23135 formal arguments in a function-like macro's formal argument list,
23136 but versions of GCC around March 2002 include spaces after the
23137 commas. */
23138
23139
23140 /* Find the extent of the macro name. The macro name is terminated
23141 by either a space or null character (for an object-like macro) or
23142 an opening paren (for a function-like macro). */
23143 for (p = body; *p; p++)
23144 if (*p == ' ' || *p == '(')
23145 break;
23146
23147 if (*p == ' ' || *p == '\0')
23148 {
23149 /* It's an object-like macro. */
23150 int name_len = p - body;
23151 std::string name (body, name_len);
23152 const char *replacement;
23153
23154 if (*p == ' ')
23155 replacement = body + name_len + 1;
23156 else
23157 {
23158 dwarf2_macro_malformed_definition_complaint (body);
23159 replacement = body + name_len;
23160 }
23161
23162 macro_define_object (file, line, name.c_str (), replacement);
23163 }
23164 else if (*p == '(')
23165 {
23166 /* It's a function-like macro. */
23167 std::string name (body, p - body);
23168 int argc = 0;
23169 int argv_size = 1;
23170 char **argv = XNEWVEC (char *, argv_size);
23171
23172 p++;
23173
23174 p = consume_improper_spaces (p, body);
23175
23176 /* Parse the formal argument list. */
23177 while (*p && *p != ')')
23178 {
23179 /* Find the extent of the current argument name. */
23180 const char *arg_start = p;
23181
23182 while (*p && *p != ',' && *p != ')' && *p != ' ')
23183 p++;
23184
23185 if (! *p || p == arg_start)
23186 dwarf2_macro_malformed_definition_complaint (body);
23187 else
23188 {
23189 /* Make sure argv has room for the new argument. */
23190 if (argc >= argv_size)
23191 {
23192 argv_size *= 2;
23193 argv = XRESIZEVEC (char *, argv, argv_size);
23194 }
23195
23196 argv[argc++] = savestring (arg_start, p - arg_start);
23197 }
23198
23199 p = consume_improper_spaces (p, body);
23200
23201 /* Consume the comma, if present. */
23202 if (*p == ',')
23203 {
23204 p++;
23205
23206 p = consume_improper_spaces (p, body);
23207 }
23208 }
23209
23210 if (*p == ')')
23211 {
23212 p++;
23213
23214 if (*p == ' ')
23215 /* Perfectly formed definition, no complaints. */
23216 macro_define_function (file, line, name.c_str (),
23217 argc, (const char **) argv,
23218 p + 1);
23219 else if (*p == '\0')
23220 {
23221 /* Complain, but do define it. */
23222 dwarf2_macro_malformed_definition_complaint (body);
23223 macro_define_function (file, line, name.c_str (),
23224 argc, (const char **) argv,
23225 p);
23226 }
23227 else
23228 /* Just complain. */
23229 dwarf2_macro_malformed_definition_complaint (body);
23230 }
23231 else
23232 /* Just complain. */
23233 dwarf2_macro_malformed_definition_complaint (body);
23234
23235 {
23236 int i;
23237
23238 for (i = 0; i < argc; i++)
23239 xfree (argv[i]);
23240 }
23241 xfree (argv);
23242 }
23243 else
23244 dwarf2_macro_malformed_definition_complaint (body);
23245 }
23246
23247 /* Skip some bytes from BYTES according to the form given in FORM.
23248 Returns the new pointer. */
23249
23250 static const gdb_byte *
23251 skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
23252 enum dwarf_form form,
23253 unsigned int offset_size,
23254 struct dwarf2_section_info *section)
23255 {
23256 unsigned int bytes_read;
23257
23258 switch (form)
23259 {
23260 case DW_FORM_data1:
23261 case DW_FORM_flag:
23262 ++bytes;
23263 break;
23264
23265 case DW_FORM_data2:
23266 bytes += 2;
23267 break;
23268
23269 case DW_FORM_data4:
23270 bytes += 4;
23271 break;
23272
23273 case DW_FORM_data8:
23274 bytes += 8;
23275 break;
23276
23277 case DW_FORM_data16:
23278 bytes += 16;
23279 break;
23280
23281 case DW_FORM_string:
23282 read_direct_string (abfd, bytes, &bytes_read);
23283 bytes += bytes_read;
23284 break;
23285
23286 case DW_FORM_sec_offset:
23287 case DW_FORM_strp:
23288 case DW_FORM_GNU_strp_alt:
23289 bytes += offset_size;
23290 break;
23291
23292 case DW_FORM_block:
23293 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
23294 bytes += bytes_read;
23295 break;
23296
23297 case DW_FORM_block1:
23298 bytes += 1 + read_1_byte (abfd, bytes);
23299 break;
23300 case DW_FORM_block2:
23301 bytes += 2 + read_2_bytes (abfd, bytes);
23302 break;
23303 case DW_FORM_block4:
23304 bytes += 4 + read_4_bytes (abfd, bytes);
23305 break;
23306
23307 case DW_FORM_addrx:
23308 case DW_FORM_sdata:
23309 case DW_FORM_strx:
23310 case DW_FORM_udata:
23311 case DW_FORM_GNU_addr_index:
23312 case DW_FORM_GNU_str_index:
23313 bytes = gdb_skip_leb128 (bytes, buffer_end);
23314 if (bytes == NULL)
23315 {
23316 dwarf2_section_buffer_overflow_complaint (section);
23317 return NULL;
23318 }
23319 break;
23320
23321 case DW_FORM_implicit_const:
23322 break;
23323
23324 default:
23325 {
23326 complaint (_("invalid form 0x%x in `%s'"),
23327 form, section->get_name ());
23328 return NULL;
23329 }
23330 }
23331
23332 return bytes;
23333 }
23334
23335 /* A helper for dwarf_decode_macros that handles skipping an unknown
23336 opcode. Returns an updated pointer to the macro data buffer; or,
23337 on error, issues a complaint and returns NULL. */
23338
23339 static const gdb_byte *
23340 skip_unknown_opcode (unsigned int opcode,
23341 const gdb_byte **opcode_definitions,
23342 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
23343 bfd *abfd,
23344 unsigned int offset_size,
23345 struct dwarf2_section_info *section)
23346 {
23347 unsigned int bytes_read, i;
23348 unsigned long arg;
23349 const gdb_byte *defn;
23350
23351 if (opcode_definitions[opcode] == NULL)
23352 {
23353 complaint (_("unrecognized DW_MACFINO opcode 0x%x"),
23354 opcode);
23355 return NULL;
23356 }
23357
23358 defn = opcode_definitions[opcode];
23359 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
23360 defn += bytes_read;
23361
23362 for (i = 0; i < arg; ++i)
23363 {
23364 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
23365 (enum dwarf_form) defn[i], offset_size,
23366 section);
23367 if (mac_ptr == NULL)
23368 {
23369 /* skip_form_bytes already issued the complaint. */
23370 return NULL;
23371 }
23372 }
23373
23374 return mac_ptr;
23375 }
23376
23377 /* A helper function which parses the header of a macro section.
23378 If the macro section is the extended (for now called "GNU") type,
23379 then this updates *OFFSET_SIZE. Returns a pointer to just after
23380 the header, or issues a complaint and returns NULL on error. */
23381
23382 static const gdb_byte *
23383 dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
23384 bfd *abfd,
23385 const gdb_byte *mac_ptr,
23386 unsigned int *offset_size,
23387 int section_is_gnu)
23388 {
23389 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
23390
23391 if (section_is_gnu)
23392 {
23393 unsigned int version, flags;
23394
23395 version = read_2_bytes (abfd, mac_ptr);
23396 if (version != 4 && version != 5)
23397 {
23398 complaint (_("unrecognized version `%d' in .debug_macro section"),
23399 version);
23400 return NULL;
23401 }
23402 mac_ptr += 2;
23403
23404 flags = read_1_byte (abfd, mac_ptr);
23405 ++mac_ptr;
23406 *offset_size = (flags & 1) ? 8 : 4;
23407
23408 if ((flags & 2) != 0)
23409 /* We don't need the line table offset. */
23410 mac_ptr += *offset_size;
23411
23412 /* Vendor opcode descriptions. */
23413 if ((flags & 4) != 0)
23414 {
23415 unsigned int i, count;
23416
23417 count = read_1_byte (abfd, mac_ptr);
23418 ++mac_ptr;
23419 for (i = 0; i < count; ++i)
23420 {
23421 unsigned int opcode, bytes_read;
23422 unsigned long arg;
23423
23424 opcode = read_1_byte (abfd, mac_ptr);
23425 ++mac_ptr;
23426 opcode_definitions[opcode] = mac_ptr;
23427 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23428 mac_ptr += bytes_read;
23429 mac_ptr += arg;
23430 }
23431 }
23432 }
23433
23434 return mac_ptr;
23435 }
23436
23437 /* A helper for dwarf_decode_macros that handles the GNU extensions,
23438 including DW_MACRO_import. */
23439
23440 static void
23441 dwarf_decode_macro_bytes (struct dwarf2_cu *cu,
23442 bfd *abfd,
23443 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
23444 struct macro_source_file *current_file,
23445 struct line_header *lh,
23446 struct dwarf2_section_info *section,
23447 int section_is_gnu, int section_is_dwz,
23448 unsigned int offset_size,
23449 htab_t include_hash)
23450 {
23451 struct dwarf2_per_objfile *dwarf2_per_objfile
23452 = cu->per_cu->dwarf2_per_objfile;
23453 struct objfile *objfile = dwarf2_per_objfile->objfile;
23454 enum dwarf_macro_record_type macinfo_type;
23455 int at_commandline;
23456 const gdb_byte *opcode_definitions[256];
23457
23458 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
23459 &offset_size, section_is_gnu);
23460 if (mac_ptr == NULL)
23461 {
23462 /* We already issued a complaint. */
23463 return;
23464 }
23465
23466 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
23467 GDB is still reading the definitions from command line. First
23468 DW_MACINFO_start_file will need to be ignored as it was already executed
23469 to create CURRENT_FILE for the main source holding also the command line
23470 definitions. On first met DW_MACINFO_start_file this flag is reset to
23471 normally execute all the remaining DW_MACINFO_start_file macinfos. */
23472
23473 at_commandline = 1;
23474
23475 do
23476 {
23477 /* Do we at least have room for a macinfo type byte? */
23478 if (mac_ptr >= mac_end)
23479 {
23480 dwarf2_section_buffer_overflow_complaint (section);
23481 break;
23482 }
23483
23484 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
23485 mac_ptr++;
23486
23487 /* Note that we rely on the fact that the corresponding GNU and
23488 DWARF constants are the same. */
23489 DIAGNOSTIC_PUSH
23490 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
23491 switch (macinfo_type)
23492 {
23493 /* A zero macinfo type indicates the end of the macro
23494 information. */
23495 case 0:
23496 break;
23497
23498 case DW_MACRO_define:
23499 case DW_MACRO_undef:
23500 case DW_MACRO_define_strp:
23501 case DW_MACRO_undef_strp:
23502 case DW_MACRO_define_sup:
23503 case DW_MACRO_undef_sup:
23504 {
23505 unsigned int bytes_read;
23506 int line;
23507 const char *body;
23508 int is_define;
23509
23510 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23511 mac_ptr += bytes_read;
23512
23513 if (macinfo_type == DW_MACRO_define
23514 || macinfo_type == DW_MACRO_undef)
23515 {
23516 body = read_direct_string (abfd, mac_ptr, &bytes_read);
23517 mac_ptr += bytes_read;
23518 }
23519 else
23520 {
23521 LONGEST str_offset;
23522
23523 str_offset = read_offset (abfd, mac_ptr, offset_size);
23524 mac_ptr += offset_size;
23525
23526 if (macinfo_type == DW_MACRO_define_sup
23527 || macinfo_type == DW_MACRO_undef_sup
23528 || section_is_dwz)
23529 {
23530 struct dwz_file *dwz
23531 = dwarf2_get_dwz_file (dwarf2_per_objfile);
23532
23533 body = read_indirect_string_from_dwz (objfile,
23534 dwz, str_offset);
23535 }
23536 else
23537 body = read_indirect_string_at_offset (dwarf2_per_objfile,
23538 abfd, str_offset);
23539 }
23540
23541 is_define = (macinfo_type == DW_MACRO_define
23542 || macinfo_type == DW_MACRO_define_strp
23543 || macinfo_type == DW_MACRO_define_sup);
23544 if (! current_file)
23545 {
23546 /* DWARF violation as no main source is present. */
23547 complaint (_("debug info with no main source gives macro %s "
23548 "on line %d: %s"),
23549 is_define ? _("definition") : _("undefinition"),
23550 line, body);
23551 break;
23552 }
23553 if ((line == 0 && !at_commandline)
23554 || (line != 0 && at_commandline))
23555 complaint (_("debug info gives %s macro %s with %s line %d: %s"),
23556 at_commandline ? _("command-line") : _("in-file"),
23557 is_define ? _("definition") : _("undefinition"),
23558 line == 0 ? _("zero") : _("non-zero"), line, body);
23559
23560 if (body == NULL)
23561 {
23562 /* Fedora's rpm-build's "debugedit" binary
23563 corrupted .debug_macro sections.
23564
23565 For more info, see
23566 https://bugzilla.redhat.com/show_bug.cgi?id=1708786 */
23567 complaint (_("debug info gives %s invalid macro %s "
23568 "without body (corrupted?) at line %d "
23569 "on file %s"),
23570 at_commandline ? _("command-line") : _("in-file"),
23571 is_define ? _("definition") : _("undefinition"),
23572 line, current_file->filename);
23573 }
23574 else if (is_define)
23575 parse_macro_definition (current_file, line, body);
23576 else
23577 {
23578 gdb_assert (macinfo_type == DW_MACRO_undef
23579 || macinfo_type == DW_MACRO_undef_strp
23580 || macinfo_type == DW_MACRO_undef_sup);
23581 macro_undef (current_file, line, body);
23582 }
23583 }
23584 break;
23585
23586 case DW_MACRO_start_file:
23587 {
23588 unsigned int bytes_read;
23589 int line, file;
23590
23591 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23592 mac_ptr += bytes_read;
23593 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23594 mac_ptr += bytes_read;
23595
23596 if ((line == 0 && !at_commandline)
23597 || (line != 0 && at_commandline))
23598 complaint (_("debug info gives source %d included "
23599 "from %s at %s line %d"),
23600 file, at_commandline ? _("command-line") : _("file"),
23601 line == 0 ? _("zero") : _("non-zero"), line);
23602
23603 if (at_commandline)
23604 {
23605 /* This DW_MACRO_start_file was executed in the
23606 pass one. */
23607 at_commandline = 0;
23608 }
23609 else
23610 current_file = macro_start_file (cu, file, line, current_file,
23611 lh);
23612 }
23613 break;
23614
23615 case DW_MACRO_end_file:
23616 if (! current_file)
23617 complaint (_("macro debug info has an unmatched "
23618 "`close_file' directive"));
23619 else
23620 {
23621 current_file = current_file->included_by;
23622 if (! current_file)
23623 {
23624 enum dwarf_macro_record_type next_type;
23625
23626 /* GCC circa March 2002 doesn't produce the zero
23627 type byte marking the end of the compilation
23628 unit. Complain if it's not there, but exit no
23629 matter what. */
23630
23631 /* Do we at least have room for a macinfo type byte? */
23632 if (mac_ptr >= mac_end)
23633 {
23634 dwarf2_section_buffer_overflow_complaint (section);
23635 return;
23636 }
23637
23638 /* We don't increment mac_ptr here, so this is just
23639 a look-ahead. */
23640 next_type
23641 = (enum dwarf_macro_record_type) read_1_byte (abfd,
23642 mac_ptr);
23643 if (next_type != 0)
23644 complaint (_("no terminating 0-type entry for "
23645 "macros in `.debug_macinfo' section"));
23646
23647 return;
23648 }
23649 }
23650 break;
23651
23652 case DW_MACRO_import:
23653 case DW_MACRO_import_sup:
23654 {
23655 LONGEST offset;
23656 void **slot;
23657 bfd *include_bfd = abfd;
23658 struct dwarf2_section_info *include_section = section;
23659 const gdb_byte *include_mac_end = mac_end;
23660 int is_dwz = section_is_dwz;
23661 const gdb_byte *new_mac_ptr;
23662
23663 offset = read_offset (abfd, mac_ptr, offset_size);
23664 mac_ptr += offset_size;
23665
23666 if (macinfo_type == DW_MACRO_import_sup)
23667 {
23668 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
23669
23670 dwz->macro.read (objfile);
23671
23672 include_section = &dwz->macro;
23673 include_bfd = include_section->get_bfd_owner ();
23674 include_mac_end = dwz->macro.buffer + dwz->macro.size;
23675 is_dwz = 1;
23676 }
23677
23678 new_mac_ptr = include_section->buffer + offset;
23679 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
23680
23681 if (*slot != NULL)
23682 {
23683 /* This has actually happened; see
23684 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
23685 complaint (_("recursive DW_MACRO_import in "
23686 ".debug_macro section"));
23687 }
23688 else
23689 {
23690 *slot = (void *) new_mac_ptr;
23691
23692 dwarf_decode_macro_bytes (cu, include_bfd, new_mac_ptr,
23693 include_mac_end, current_file, lh,
23694 section, section_is_gnu, is_dwz,
23695 offset_size, include_hash);
23696
23697 htab_remove_elt (include_hash, (void *) new_mac_ptr);
23698 }
23699 }
23700 break;
23701
23702 case DW_MACINFO_vendor_ext:
23703 if (!section_is_gnu)
23704 {
23705 unsigned int bytes_read;
23706
23707 /* This reads the constant, but since we don't recognize
23708 any vendor extensions, we ignore it. */
23709 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23710 mac_ptr += bytes_read;
23711 read_direct_string (abfd, mac_ptr, &bytes_read);
23712 mac_ptr += bytes_read;
23713
23714 /* We don't recognize any vendor extensions. */
23715 break;
23716 }
23717 /* FALLTHROUGH */
23718
23719 default:
23720 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
23721 mac_ptr, mac_end, abfd, offset_size,
23722 section);
23723 if (mac_ptr == NULL)
23724 return;
23725 break;
23726 }
23727 DIAGNOSTIC_POP
23728 } while (macinfo_type != 0);
23729 }
23730
23731 static void
23732 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
23733 int section_is_gnu)
23734 {
23735 struct dwarf2_per_objfile *dwarf2_per_objfile
23736 = cu->per_cu->dwarf2_per_objfile;
23737 struct objfile *objfile = dwarf2_per_objfile->objfile;
23738 struct line_header *lh = cu->line_header;
23739 bfd *abfd;
23740 const gdb_byte *mac_ptr, *mac_end;
23741 struct macro_source_file *current_file = 0;
23742 enum dwarf_macro_record_type macinfo_type;
23743 unsigned int offset_size = cu->header.offset_size;
23744 const gdb_byte *opcode_definitions[256];
23745 void **slot;
23746 struct dwarf2_section_info *section;
23747 const char *section_name;
23748
23749 if (cu->dwo_unit != NULL)
23750 {
23751 if (section_is_gnu)
23752 {
23753 section = &cu->dwo_unit->dwo_file->sections.macro;
23754 section_name = ".debug_macro.dwo";
23755 }
23756 else
23757 {
23758 section = &cu->dwo_unit->dwo_file->sections.macinfo;
23759 section_name = ".debug_macinfo.dwo";
23760 }
23761 }
23762 else
23763 {
23764 if (section_is_gnu)
23765 {
23766 section = &dwarf2_per_objfile->macro;
23767 section_name = ".debug_macro";
23768 }
23769 else
23770 {
23771 section = &dwarf2_per_objfile->macinfo;
23772 section_name = ".debug_macinfo";
23773 }
23774 }
23775
23776 section->read (objfile);
23777 if (section->buffer == NULL)
23778 {
23779 complaint (_("missing %s section"), section_name);
23780 return;
23781 }
23782 abfd = section->get_bfd_owner ();
23783
23784 /* First pass: Find the name of the base filename.
23785 This filename is needed in order to process all macros whose definition
23786 (or undefinition) comes from the command line. These macros are defined
23787 before the first DW_MACINFO_start_file entry, and yet still need to be
23788 associated to the base file.
23789
23790 To determine the base file name, we scan the macro definitions until we
23791 reach the first DW_MACINFO_start_file entry. We then initialize
23792 CURRENT_FILE accordingly so that any macro definition found before the
23793 first DW_MACINFO_start_file can still be associated to the base file. */
23794
23795 mac_ptr = section->buffer + offset;
23796 mac_end = section->buffer + section->size;
23797
23798 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
23799 &offset_size, section_is_gnu);
23800 if (mac_ptr == NULL)
23801 {
23802 /* We already issued a complaint. */
23803 return;
23804 }
23805
23806 do
23807 {
23808 /* Do we at least have room for a macinfo type byte? */
23809 if (mac_ptr >= mac_end)
23810 {
23811 /* Complaint is printed during the second pass as GDB will probably
23812 stop the first pass earlier upon finding
23813 DW_MACINFO_start_file. */
23814 break;
23815 }
23816
23817 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
23818 mac_ptr++;
23819
23820 /* Note that we rely on the fact that the corresponding GNU and
23821 DWARF constants are the same. */
23822 DIAGNOSTIC_PUSH
23823 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
23824 switch (macinfo_type)
23825 {
23826 /* A zero macinfo type indicates the end of the macro
23827 information. */
23828 case 0:
23829 break;
23830
23831 case DW_MACRO_define:
23832 case DW_MACRO_undef:
23833 /* Only skip the data by MAC_PTR. */
23834 {
23835 unsigned int bytes_read;
23836
23837 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23838 mac_ptr += bytes_read;
23839 read_direct_string (abfd, mac_ptr, &bytes_read);
23840 mac_ptr += bytes_read;
23841 }
23842 break;
23843
23844 case DW_MACRO_start_file:
23845 {
23846 unsigned int bytes_read;
23847 int line, file;
23848
23849 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23850 mac_ptr += bytes_read;
23851 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23852 mac_ptr += bytes_read;
23853
23854 current_file = macro_start_file (cu, file, line, current_file, lh);
23855 }
23856 break;
23857
23858 case DW_MACRO_end_file:
23859 /* No data to skip by MAC_PTR. */
23860 break;
23861
23862 case DW_MACRO_define_strp:
23863 case DW_MACRO_undef_strp:
23864 case DW_MACRO_define_sup:
23865 case DW_MACRO_undef_sup:
23866 {
23867 unsigned int bytes_read;
23868
23869 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23870 mac_ptr += bytes_read;
23871 mac_ptr += offset_size;
23872 }
23873 break;
23874
23875 case DW_MACRO_import:
23876 case DW_MACRO_import_sup:
23877 /* Note that, according to the spec, a transparent include
23878 chain cannot call DW_MACRO_start_file. So, we can just
23879 skip this opcode. */
23880 mac_ptr += offset_size;
23881 break;
23882
23883 case DW_MACINFO_vendor_ext:
23884 /* Only skip the data by MAC_PTR. */
23885 if (!section_is_gnu)
23886 {
23887 unsigned int bytes_read;
23888
23889 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23890 mac_ptr += bytes_read;
23891 read_direct_string (abfd, mac_ptr, &bytes_read);
23892 mac_ptr += bytes_read;
23893 }
23894 /* FALLTHROUGH */
23895
23896 default:
23897 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
23898 mac_ptr, mac_end, abfd, offset_size,
23899 section);
23900 if (mac_ptr == NULL)
23901 return;
23902 break;
23903 }
23904 DIAGNOSTIC_POP
23905 } while (macinfo_type != 0 && current_file == NULL);
23906
23907 /* Second pass: Process all entries.
23908
23909 Use the AT_COMMAND_LINE flag to determine whether we are still processing
23910 command-line macro definitions/undefinitions. This flag is unset when we
23911 reach the first DW_MACINFO_start_file entry. */
23912
23913 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
23914 htab_eq_pointer,
23915 NULL, xcalloc, xfree));
23916 mac_ptr = section->buffer + offset;
23917 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
23918 *slot = (void *) mac_ptr;
23919 dwarf_decode_macro_bytes (cu, abfd, mac_ptr, mac_end,
23920 current_file, lh, section,
23921 section_is_gnu, 0, offset_size,
23922 include_hash.get ());
23923 }
23924
23925 /* Return the .debug_loc section to use for CU.
23926 For DWO files use .debug_loc.dwo. */
23927
23928 static struct dwarf2_section_info *
23929 cu_debug_loc_section (struct dwarf2_cu *cu)
23930 {
23931 struct dwarf2_per_objfile *dwarf2_per_objfile
23932 = cu->per_cu->dwarf2_per_objfile;
23933
23934 if (cu->dwo_unit)
23935 {
23936 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
23937
23938 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
23939 }
23940 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
23941 : &dwarf2_per_objfile->loc);
23942 }
23943
23944 /* A helper function that fills in a dwarf2_loclist_baton. */
23945
23946 static void
23947 fill_in_loclist_baton (struct dwarf2_cu *cu,
23948 struct dwarf2_loclist_baton *baton,
23949 const struct attribute *attr)
23950 {
23951 struct dwarf2_per_objfile *dwarf2_per_objfile
23952 = cu->per_cu->dwarf2_per_objfile;
23953 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23954
23955 section->read (dwarf2_per_objfile->objfile);
23956
23957 baton->per_cu = cu->per_cu;
23958 gdb_assert (baton->per_cu);
23959 /* We don't know how long the location list is, but make sure we
23960 don't run off the edge of the section. */
23961 baton->size = section->size - DW_UNSND (attr);
23962 baton->data = section->buffer + DW_UNSND (attr);
23963 baton->base_address = cu->base_address;
23964 baton->from_dwo = cu->dwo_unit != NULL;
23965 }
23966
23967 static void
23968 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
23969 struct dwarf2_cu *cu, int is_block)
23970 {
23971 struct dwarf2_per_objfile *dwarf2_per_objfile
23972 = cu->per_cu->dwarf2_per_objfile;
23973 struct objfile *objfile = dwarf2_per_objfile->objfile;
23974 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23975
23976 if (attr->form_is_section_offset ()
23977 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
23978 the section. If so, fall through to the complaint in the
23979 other branch. */
23980 && DW_UNSND (attr) < section->get_size (objfile))
23981 {
23982 struct dwarf2_loclist_baton *baton;
23983
23984 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
23985
23986 fill_in_loclist_baton (cu, baton, attr);
23987
23988 if (cu->base_known == 0)
23989 complaint (_("Location list used without "
23990 "specifying the CU base address."));
23991
23992 SYMBOL_ACLASS_INDEX (sym) = (is_block
23993 ? dwarf2_loclist_block_index
23994 : dwarf2_loclist_index);
23995 SYMBOL_LOCATION_BATON (sym) = baton;
23996 }
23997 else
23998 {
23999 struct dwarf2_locexpr_baton *baton;
24000
24001 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
24002 baton->per_cu = cu->per_cu;
24003 gdb_assert (baton->per_cu);
24004
24005 if (attr->form_is_block ())
24006 {
24007 /* Note that we're just copying the block's data pointer
24008 here, not the actual data. We're still pointing into the
24009 info_buffer for SYM's objfile; right now we never release
24010 that buffer, but when we do clean up properly this may
24011 need to change. */
24012 baton->size = DW_BLOCK (attr)->size;
24013 baton->data = DW_BLOCK (attr)->data;
24014 }
24015 else
24016 {
24017 dwarf2_invalid_attrib_class_complaint ("location description",
24018 sym->natural_name ());
24019 baton->size = 0;
24020 }
24021
24022 SYMBOL_ACLASS_INDEX (sym) = (is_block
24023 ? dwarf2_locexpr_block_index
24024 : dwarf2_locexpr_index);
24025 SYMBOL_LOCATION_BATON (sym) = baton;
24026 }
24027 }
24028
24029 /* See read.h. */
24030
24031 struct objfile *
24032 dwarf2_per_cu_data::objfile () const
24033 {
24034 struct objfile *objfile = dwarf2_per_objfile->objfile;
24035
24036 /* Return the master objfile, so that we can report and look up the
24037 correct file containing this variable. */
24038 if (objfile->separate_debug_objfile_backlink)
24039 objfile = objfile->separate_debug_objfile_backlink;
24040
24041 return objfile;
24042 }
24043
24044 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
24045 (CU_HEADERP is unused in such case) or prepare a temporary copy at
24046 CU_HEADERP first. */
24047
24048 static const struct comp_unit_head *
24049 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
24050 const struct dwarf2_per_cu_data *per_cu)
24051 {
24052 const gdb_byte *info_ptr;
24053
24054 if (per_cu->cu)
24055 return &per_cu->cu->header;
24056
24057 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
24058
24059 memset (cu_headerp, 0, sizeof (*cu_headerp));
24060 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
24061 rcuh_kind::COMPILE);
24062
24063 return cu_headerp;
24064 }
24065
24066 /* See read.h. */
24067
24068 int
24069 dwarf2_per_cu_data::addr_size () const
24070 {
24071 struct comp_unit_head cu_header_local;
24072 const struct comp_unit_head *cu_headerp;
24073
24074 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
24075
24076 return cu_headerp->addr_size;
24077 }
24078
24079 /* See read.h. */
24080
24081 int
24082 dwarf2_per_cu_data::offset_size () const
24083 {
24084 struct comp_unit_head cu_header_local;
24085 const struct comp_unit_head *cu_headerp;
24086
24087 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
24088
24089 return cu_headerp->offset_size;
24090 }
24091
24092 /* See read.h. */
24093
24094 int
24095 dwarf2_per_cu_data::ref_addr_size () const
24096 {
24097 struct comp_unit_head cu_header_local;
24098 const struct comp_unit_head *cu_headerp;
24099
24100 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
24101
24102 if (cu_headerp->version == 2)
24103 return cu_headerp->addr_size;
24104 else
24105 return cu_headerp->offset_size;
24106 }
24107
24108 /* See read.h. */
24109
24110 CORE_ADDR
24111 dwarf2_per_cu_data::text_offset () const
24112 {
24113 struct objfile *objfile = dwarf2_per_objfile->objfile;
24114
24115 return objfile->text_section_offset ();
24116 }
24117
24118 /* See read.h. */
24119
24120 struct type *
24121 dwarf2_per_cu_data::addr_type () const
24122 {
24123 struct objfile *objfile = dwarf2_per_objfile->objfile;
24124 struct type *void_type = objfile_type (objfile)->builtin_void;
24125 struct type *addr_type = lookup_pointer_type (void_type);
24126 int addr_size = this->addr_size ();
24127
24128 if (TYPE_LENGTH (addr_type) == addr_size)
24129 return addr_type;
24130
24131 addr_type = addr_sized_int_type (TYPE_UNSIGNED (addr_type));
24132 return addr_type;
24133 }
24134
24135 /* A helper function for dwarf2_find_containing_comp_unit that returns
24136 the index of the result, and that searches a vector. It will
24137 return a result even if the offset in question does not actually
24138 occur in any CU. This is separate so that it can be unit
24139 tested. */
24140
24141 static int
24142 dwarf2_find_containing_comp_unit
24143 (sect_offset sect_off,
24144 unsigned int offset_in_dwz,
24145 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
24146 {
24147 int low, high;
24148
24149 low = 0;
24150 high = all_comp_units.size () - 1;
24151 while (high > low)
24152 {
24153 struct dwarf2_per_cu_data *mid_cu;
24154 int mid = low + (high - low) / 2;
24155
24156 mid_cu = all_comp_units[mid];
24157 if (mid_cu->is_dwz > offset_in_dwz
24158 || (mid_cu->is_dwz == offset_in_dwz
24159 && mid_cu->sect_off + mid_cu->length > sect_off))
24160 high = mid;
24161 else
24162 low = mid + 1;
24163 }
24164 gdb_assert (low == high);
24165 return low;
24166 }
24167
24168 /* Locate the .debug_info compilation unit from CU's objfile which contains
24169 the DIE at OFFSET. Raises an error on failure. */
24170
24171 static struct dwarf2_per_cu_data *
24172 dwarf2_find_containing_comp_unit (sect_offset sect_off,
24173 unsigned int offset_in_dwz,
24174 struct dwarf2_per_objfile *dwarf2_per_objfile)
24175 {
24176 int low
24177 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
24178 dwarf2_per_objfile->all_comp_units);
24179 struct dwarf2_per_cu_data *this_cu
24180 = dwarf2_per_objfile->all_comp_units[low];
24181
24182 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
24183 {
24184 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
24185 error (_("Dwarf Error: could not find partial DIE containing "
24186 "offset %s [in module %s]"),
24187 sect_offset_str (sect_off),
24188 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
24189
24190 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
24191 <= sect_off);
24192 return dwarf2_per_objfile->all_comp_units[low-1];
24193 }
24194 else
24195 {
24196 if (low == dwarf2_per_objfile->all_comp_units.size () - 1
24197 && sect_off >= this_cu->sect_off + this_cu->length)
24198 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
24199 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
24200 return this_cu;
24201 }
24202 }
24203
24204 #if GDB_SELF_TEST
24205
24206 namespace selftests {
24207 namespace find_containing_comp_unit {
24208
24209 static void
24210 run_test ()
24211 {
24212 struct dwarf2_per_cu_data one {};
24213 struct dwarf2_per_cu_data two {};
24214 struct dwarf2_per_cu_data three {};
24215 struct dwarf2_per_cu_data four {};
24216
24217 one.length = 5;
24218 two.sect_off = sect_offset (one.length);
24219 two.length = 7;
24220
24221 three.length = 5;
24222 three.is_dwz = 1;
24223 four.sect_off = sect_offset (three.length);
24224 four.length = 7;
24225 four.is_dwz = 1;
24226
24227 std::vector<dwarf2_per_cu_data *> units;
24228 units.push_back (&one);
24229 units.push_back (&two);
24230 units.push_back (&three);
24231 units.push_back (&four);
24232
24233 int result;
24234
24235 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
24236 SELF_CHECK (units[result] == &one);
24237 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
24238 SELF_CHECK (units[result] == &one);
24239 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
24240 SELF_CHECK (units[result] == &two);
24241
24242 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
24243 SELF_CHECK (units[result] == &three);
24244 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
24245 SELF_CHECK (units[result] == &three);
24246 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
24247 SELF_CHECK (units[result] == &four);
24248 }
24249
24250 }
24251 }
24252
24253 #endif /* GDB_SELF_TEST */
24254
24255 /* Initialize dwarf2_cu CU, owned by PER_CU. */
24256
24257 dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
24258 : per_cu (per_cu_),
24259 mark (false),
24260 has_loclist (false),
24261 checked_producer (false),
24262 producer_is_gxx_lt_4_6 (false),
24263 producer_is_gcc_lt_4_3 (false),
24264 producer_is_icc (false),
24265 producer_is_icc_lt_14 (false),
24266 producer_is_codewarrior (false),
24267 processing_has_namespace_info (false)
24268 {
24269 per_cu->cu = this;
24270 }
24271
24272 /* Destroy a dwarf2_cu. */
24273
24274 dwarf2_cu::~dwarf2_cu ()
24275 {
24276 per_cu->cu = NULL;
24277 }
24278
24279 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
24280
24281 static void
24282 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
24283 enum language pretend_language)
24284 {
24285 struct attribute *attr;
24286
24287 /* Set the language we're debugging. */
24288 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
24289 if (attr != nullptr)
24290 set_cu_language (DW_UNSND (attr), cu);
24291 else
24292 {
24293 cu->language = pretend_language;
24294 cu->language_defn = language_def (cu->language);
24295 }
24296
24297 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
24298 }
24299
24300 /* Increase the age counter on each cached compilation unit, and free
24301 any that are too old. */
24302
24303 static void
24304 age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
24305 {
24306 struct dwarf2_per_cu_data *per_cu, **last_chain;
24307
24308 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
24309 per_cu = dwarf2_per_objfile->read_in_chain;
24310 while (per_cu != NULL)
24311 {
24312 per_cu->cu->last_used ++;
24313 if (per_cu->cu->last_used <= dwarf_max_cache_age)
24314 dwarf2_mark (per_cu->cu);
24315 per_cu = per_cu->cu->read_in_chain;
24316 }
24317
24318 per_cu = dwarf2_per_objfile->read_in_chain;
24319 last_chain = &dwarf2_per_objfile->read_in_chain;
24320 while (per_cu != NULL)
24321 {
24322 struct dwarf2_per_cu_data *next_cu;
24323
24324 next_cu = per_cu->cu->read_in_chain;
24325
24326 if (!per_cu->cu->mark)
24327 {
24328 delete per_cu->cu;
24329 *last_chain = next_cu;
24330 }
24331 else
24332 last_chain = &per_cu->cu->read_in_chain;
24333
24334 per_cu = next_cu;
24335 }
24336 }
24337
24338 /* Remove a single compilation unit from the cache. */
24339
24340 static void
24341 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
24342 {
24343 struct dwarf2_per_cu_data *per_cu, **last_chain;
24344 struct dwarf2_per_objfile *dwarf2_per_objfile
24345 = target_per_cu->dwarf2_per_objfile;
24346
24347 per_cu = dwarf2_per_objfile->read_in_chain;
24348 last_chain = &dwarf2_per_objfile->read_in_chain;
24349 while (per_cu != NULL)
24350 {
24351 struct dwarf2_per_cu_data *next_cu;
24352
24353 next_cu = per_cu->cu->read_in_chain;
24354
24355 if (per_cu == target_per_cu)
24356 {
24357 delete per_cu->cu;
24358 per_cu->cu = NULL;
24359 *last_chain = next_cu;
24360 break;
24361 }
24362 else
24363 last_chain = &per_cu->cu->read_in_chain;
24364
24365 per_cu = next_cu;
24366 }
24367 }
24368
24369 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
24370 We store these in a hash table separate from the DIEs, and preserve them
24371 when the DIEs are flushed out of cache.
24372
24373 The CU "per_cu" pointer is needed because offset alone is not enough to
24374 uniquely identify the type. A file may have multiple .debug_types sections,
24375 or the type may come from a DWO file. Furthermore, while it's more logical
24376 to use per_cu->section+offset, with Fission the section with the data is in
24377 the DWO file but we don't know that section at the point we need it.
24378 We have to use something in dwarf2_per_cu_data (or the pointer to it)
24379 because we can enter the lookup routine, get_die_type_at_offset, from
24380 outside this file, and thus won't necessarily have PER_CU->cu.
24381 Fortunately, PER_CU is stable for the life of the objfile. */
24382
24383 struct dwarf2_per_cu_offset_and_type
24384 {
24385 const struct dwarf2_per_cu_data *per_cu;
24386 sect_offset sect_off;
24387 struct type *type;
24388 };
24389
24390 /* Hash function for a dwarf2_per_cu_offset_and_type. */
24391
24392 static hashval_t
24393 per_cu_offset_and_type_hash (const void *item)
24394 {
24395 const struct dwarf2_per_cu_offset_and_type *ofs
24396 = (const struct dwarf2_per_cu_offset_and_type *) item;
24397
24398 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
24399 }
24400
24401 /* Equality function for a dwarf2_per_cu_offset_and_type. */
24402
24403 static int
24404 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
24405 {
24406 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
24407 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
24408 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
24409 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
24410
24411 return (ofs_lhs->per_cu == ofs_rhs->per_cu
24412 && ofs_lhs->sect_off == ofs_rhs->sect_off);
24413 }
24414
24415 /* Set the type associated with DIE to TYPE. Save it in CU's hash
24416 table if necessary. For convenience, return TYPE.
24417
24418 The DIEs reading must have careful ordering to:
24419 * Not cause infinite loops trying to read in DIEs as a prerequisite for
24420 reading current DIE.
24421 * Not trying to dereference contents of still incompletely read in types
24422 while reading in other DIEs.
24423 * Enable referencing still incompletely read in types just by a pointer to
24424 the type without accessing its fields.
24425
24426 Therefore caller should follow these rules:
24427 * Try to fetch any prerequisite types we may need to build this DIE type
24428 before building the type and calling set_die_type.
24429 * After building type call set_die_type for current DIE as soon as
24430 possible before fetching more types to complete the current type.
24431 * Make the type as complete as possible before fetching more types. */
24432
24433 static struct type *
24434 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
24435 {
24436 struct dwarf2_per_objfile *dwarf2_per_objfile
24437 = cu->per_cu->dwarf2_per_objfile;
24438 struct dwarf2_per_cu_offset_and_type **slot, ofs;
24439 struct objfile *objfile = dwarf2_per_objfile->objfile;
24440 struct attribute *attr;
24441 struct dynamic_prop prop;
24442
24443 /* For Ada types, make sure that the gnat-specific data is always
24444 initialized (if not already set). There are a few types where
24445 we should not be doing so, because the type-specific area is
24446 already used to hold some other piece of info (eg: TYPE_CODE_FLT
24447 where the type-specific area is used to store the floatformat).
24448 But this is not a problem, because the gnat-specific information
24449 is actually not needed for these types. */
24450 if (need_gnat_info (cu)
24451 && TYPE_CODE (type) != TYPE_CODE_FUNC
24452 && TYPE_CODE (type) != TYPE_CODE_FLT
24453 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
24454 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
24455 && TYPE_CODE (type) != TYPE_CODE_METHOD
24456 && !HAVE_GNAT_AUX_INFO (type))
24457 INIT_GNAT_SPECIFIC (type);
24458
24459 /* Read DW_AT_allocated and set in type. */
24460 attr = dwarf2_attr (die, DW_AT_allocated, cu);
24461 if (attr != NULL && attr->form_is_block ())
24462 {
24463 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
24464 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
24465 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
24466 }
24467 else if (attr != NULL)
24468 {
24469 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
24470 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
24471 sect_offset_str (die->sect_off));
24472 }
24473
24474 /* Read DW_AT_associated and set in type. */
24475 attr = dwarf2_attr (die, DW_AT_associated, cu);
24476 if (attr != NULL && attr->form_is_block ())
24477 {
24478 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
24479 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
24480 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
24481 }
24482 else if (attr != NULL)
24483 {
24484 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
24485 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
24486 sect_offset_str (die->sect_off));
24487 }
24488
24489 /* Read DW_AT_data_location and set in type. */
24490 attr = dwarf2_attr (die, DW_AT_data_location, cu);
24491 if (attr_to_dynamic_prop (attr, die, cu, &prop,
24492 cu->per_cu->addr_type ()))
24493 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
24494
24495 if (dwarf2_per_objfile->die_type_hash == NULL)
24496 dwarf2_per_objfile->die_type_hash
24497 = htab_up (htab_create_alloc (127,
24498 per_cu_offset_and_type_hash,
24499 per_cu_offset_and_type_eq,
24500 NULL, xcalloc, xfree));
24501
24502 ofs.per_cu = cu->per_cu;
24503 ofs.sect_off = die->sect_off;
24504 ofs.type = type;
24505 slot = (struct dwarf2_per_cu_offset_and_type **)
24506 htab_find_slot (dwarf2_per_objfile->die_type_hash.get (), &ofs, INSERT);
24507 if (*slot)
24508 complaint (_("A problem internal to GDB: DIE %s has type already set"),
24509 sect_offset_str (die->sect_off));
24510 *slot = XOBNEW (&objfile->objfile_obstack,
24511 struct dwarf2_per_cu_offset_and_type);
24512 **slot = ofs;
24513 return type;
24514 }
24515
24516 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
24517 or return NULL if the die does not have a saved type. */
24518
24519 static struct type *
24520 get_die_type_at_offset (sect_offset sect_off,
24521 struct dwarf2_per_cu_data *per_cu)
24522 {
24523 struct dwarf2_per_cu_offset_and_type *slot, ofs;
24524 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
24525
24526 if (dwarf2_per_objfile->die_type_hash == NULL)
24527 return NULL;
24528
24529 ofs.per_cu = per_cu;
24530 ofs.sect_off = sect_off;
24531 slot = ((struct dwarf2_per_cu_offset_and_type *)
24532 htab_find (dwarf2_per_objfile->die_type_hash.get (), &ofs));
24533 if (slot)
24534 return slot->type;
24535 else
24536 return NULL;
24537 }
24538
24539 /* Look up the type for DIE in CU in die_type_hash,
24540 or return NULL if DIE does not have a saved type. */
24541
24542 static struct type *
24543 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
24544 {
24545 return get_die_type_at_offset (die->sect_off, cu->per_cu);
24546 }
24547
24548 /* Add a dependence relationship from CU to REF_PER_CU. */
24549
24550 static void
24551 dwarf2_add_dependence (struct dwarf2_cu *cu,
24552 struct dwarf2_per_cu_data *ref_per_cu)
24553 {
24554 void **slot;
24555
24556 if (cu->dependencies == NULL)
24557 cu->dependencies
24558 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
24559 NULL, &cu->comp_unit_obstack,
24560 hashtab_obstack_allocate,
24561 dummy_obstack_deallocate);
24562
24563 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
24564 if (*slot == NULL)
24565 *slot = ref_per_cu;
24566 }
24567
24568 /* Subroutine of dwarf2_mark to pass to htab_traverse.
24569 Set the mark field in every compilation unit in the
24570 cache that we must keep because we are keeping CU. */
24571
24572 static int
24573 dwarf2_mark_helper (void **slot, void *data)
24574 {
24575 struct dwarf2_per_cu_data *per_cu;
24576
24577 per_cu = (struct dwarf2_per_cu_data *) *slot;
24578
24579 /* cu->dependencies references may not yet have been ever read if QUIT aborts
24580 reading of the chain. As such dependencies remain valid it is not much
24581 useful to track and undo them during QUIT cleanups. */
24582 if (per_cu->cu == NULL)
24583 return 1;
24584
24585 if (per_cu->cu->mark)
24586 return 1;
24587 per_cu->cu->mark = true;
24588
24589 if (per_cu->cu->dependencies != NULL)
24590 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
24591
24592 return 1;
24593 }
24594
24595 /* Set the mark field in CU and in every other compilation unit in the
24596 cache that we must keep because we are keeping CU. */
24597
24598 static void
24599 dwarf2_mark (struct dwarf2_cu *cu)
24600 {
24601 if (cu->mark)
24602 return;
24603 cu->mark = true;
24604 if (cu->dependencies != NULL)
24605 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
24606 }
24607
24608 static void
24609 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
24610 {
24611 while (per_cu)
24612 {
24613 per_cu->cu->mark = false;
24614 per_cu = per_cu->cu->read_in_chain;
24615 }
24616 }
24617
24618 /* Trivial hash function for partial_die_info: the hash value of a DIE
24619 is its offset in .debug_info for this objfile. */
24620
24621 static hashval_t
24622 partial_die_hash (const void *item)
24623 {
24624 const struct partial_die_info *part_die
24625 = (const struct partial_die_info *) item;
24626
24627 return to_underlying (part_die->sect_off);
24628 }
24629
24630 /* Trivial comparison function for partial_die_info structures: two DIEs
24631 are equal if they have the same offset. */
24632
24633 static int
24634 partial_die_eq (const void *item_lhs, const void *item_rhs)
24635 {
24636 const struct partial_die_info *part_die_lhs
24637 = (const struct partial_die_info *) item_lhs;
24638 const struct partial_die_info *part_die_rhs
24639 = (const struct partial_die_info *) item_rhs;
24640
24641 return part_die_lhs->sect_off == part_die_rhs->sect_off;
24642 }
24643
24644 struct cmd_list_element *set_dwarf_cmdlist;
24645 struct cmd_list_element *show_dwarf_cmdlist;
24646
24647 static void
24648 set_dwarf_cmd (const char *args, int from_tty)
24649 {
24650 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
24651 gdb_stdout);
24652 }
24653
24654 static void
24655 show_dwarf_cmd (const char *args, int from_tty)
24656 {
24657 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
24658 }
24659
24660 static void
24661 show_check_physname (struct ui_file *file, int from_tty,
24662 struct cmd_list_element *c, const char *value)
24663 {
24664 fprintf_filtered (file,
24665 _("Whether to check \"physname\" is %s.\n"),
24666 value);
24667 }
24668
24669 void _initialize_dwarf2_read ();
24670 void
24671 _initialize_dwarf2_read ()
24672 {
24673 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
24674 Set DWARF specific variables.\n\
24675 Configure DWARF variables such as the cache size."),
24676 &set_dwarf_cmdlist, "maintenance set dwarf ",
24677 0/*allow-unknown*/, &maintenance_set_cmdlist);
24678
24679 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
24680 Show DWARF specific variables.\n\
24681 Show DWARF variables such as the cache size."),
24682 &show_dwarf_cmdlist, "maintenance show dwarf ",
24683 0/*allow-unknown*/, &maintenance_show_cmdlist);
24684
24685 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
24686 &dwarf_max_cache_age, _("\
24687 Set the upper bound on the age of cached DWARF compilation units."), _("\
24688 Show the upper bound on the age of cached DWARF compilation units."), _("\
24689 A higher limit means that cached compilation units will be stored\n\
24690 in memory longer, and more total memory will be used. Zero disables\n\
24691 caching, which can slow down startup."),
24692 NULL,
24693 show_dwarf_max_cache_age,
24694 &set_dwarf_cmdlist,
24695 &show_dwarf_cmdlist);
24696
24697 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
24698 Set debugging of the DWARF reader."), _("\
24699 Show debugging of the DWARF reader."), _("\
24700 When enabled (non-zero), debugging messages are printed during DWARF\n\
24701 reading and symtab expansion. A value of 1 (one) provides basic\n\
24702 information. A value greater than 1 provides more verbose information."),
24703 NULL,
24704 NULL,
24705 &setdebuglist, &showdebuglist);
24706
24707 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
24708 Set debugging of the DWARF DIE reader."), _("\
24709 Show debugging of the DWARF DIE reader."), _("\
24710 When enabled (non-zero), DIEs are dumped after they are read in.\n\
24711 The value is the maximum depth to print."),
24712 NULL,
24713 NULL,
24714 &setdebuglist, &showdebuglist);
24715
24716 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
24717 Set debugging of the dwarf line reader."), _("\
24718 Show debugging of the dwarf line reader."), _("\
24719 When enabled (non-zero), line number entries are dumped as they are read in.\n\
24720 A value of 1 (one) provides basic information.\n\
24721 A value greater than 1 provides more verbose information."),
24722 NULL,
24723 NULL,
24724 &setdebuglist, &showdebuglist);
24725
24726 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
24727 Set cross-checking of \"physname\" code against demangler."), _("\
24728 Show cross-checking of \"physname\" code against demangler."), _("\
24729 When enabled, GDB's internal \"physname\" code is checked against\n\
24730 the demangler."),
24731 NULL, show_check_physname,
24732 &setdebuglist, &showdebuglist);
24733
24734 add_setshow_boolean_cmd ("use-deprecated-index-sections",
24735 no_class, &use_deprecated_index_sections, _("\
24736 Set whether to use deprecated gdb_index sections."), _("\
24737 Show whether to use deprecated gdb_index sections."), _("\
24738 When enabled, deprecated .gdb_index sections are used anyway.\n\
24739 Normally they are ignored either because of a missing feature or\n\
24740 performance issue.\n\
24741 Warning: This option must be enabled before gdb reads the file."),
24742 NULL,
24743 NULL,
24744 &setlist, &showlist);
24745
24746 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
24747 &dwarf2_locexpr_funcs);
24748 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
24749 &dwarf2_loclist_funcs);
24750
24751 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
24752 &dwarf2_block_frame_base_locexpr_funcs);
24753 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
24754 &dwarf2_block_frame_base_loclist_funcs);
24755
24756 #if GDB_SELF_TEST
24757 selftests::register_test ("dw2_expand_symtabs_matching",
24758 selftests::dw2_expand_symtabs_matching::run_test);
24759 selftests::register_test ("dwarf2_find_containing_comp_unit",
24760 selftests::find_containing_comp_unit::run_test);
24761 #endif
24762 }
This page took 0.503305 seconds and 5 git commands to generate.