Move DWARF-constant stringifying code to new file
[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 "dwarf2/dwz.h"
41 #include "dwarf2/macro.h"
42 #include "dwarf2/die.h"
43 #include "dwarf2/stringify.h"
44 #include "bfd.h"
45 #include "elf-bfd.h"
46 #include "symtab.h"
47 #include "gdbtypes.h"
48 #include "objfiles.h"
49 #include "dwarf2.h"
50 #include "buildsym.h"
51 #include "demangle.h"
52 #include "gdb-demangle.h"
53 #include "filenames.h" /* for DOSish file names */
54 #include "language.h"
55 #include "complaints.h"
56 #include "dwarf2/expr.h"
57 #include "dwarf2/loc.h"
58 #include "cp-support.h"
59 #include "hashtab.h"
60 #include "command.h"
61 #include "gdbcmd.h"
62 #include "block.h"
63 #include "addrmap.h"
64 #include "typeprint.h"
65 #include "psympriv.h"
66 #include "c-lang.h"
67 #include "go-lang.h"
68 #include "valprint.h"
69 #include "gdbcore.h" /* for gnutarget */
70 #include "gdb/gdb-index.h"
71 #include "gdb_bfd.h"
72 #include "f-lang.h"
73 #include "source.h"
74 #include "build-id.h"
75 #include "namespace.h"
76 #include "gdbsupport/function-view.h"
77 #include "gdbsupport/gdb_optional.h"
78 #include "gdbsupport/underlying.h"
79 #include "gdbsupport/hash_enum.h"
80 #include "filename-seen-cache.h"
81 #include "producer.h"
82 #include <fcntl.h>
83 #include <algorithm>
84 #include <unordered_map>
85 #include "gdbsupport/selftest.h"
86 #include "rust-lang.h"
87 #include "gdbsupport/pathstuff.h"
88 #include "count-one-bits.h"
89 #include "debuginfod-support.h"
90
91 /* When == 1, print basic high level tracing messages.
92 When > 1, be more verbose.
93 This is in contrast to the low level DIE reading of dwarf_die_debug. */
94 static unsigned int dwarf_read_debug = 0;
95
96 /* When non-zero, dump DIEs after they are read in. */
97 static unsigned int dwarf_die_debug = 0;
98
99 /* When non-zero, dump line number entries as they are read in. */
100 unsigned int dwarf_line_debug = 0;
101
102 /* When true, cross-check physname against demangler. */
103 static bool check_physname = false;
104
105 /* When true, do not reject deprecated .gdb_index sections. */
106 static bool use_deprecated_index_sections = false;
107
108 static const struct objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
109
110 /* The "aclass" indices for various kinds of computed DWARF symbols. */
111
112 static int dwarf2_locexpr_index;
113 static int dwarf2_loclist_index;
114 static int dwarf2_locexpr_block_index;
115 static int dwarf2_loclist_block_index;
116
117 /* An index into a (C++) symbol name component in a symbol name as
118 recorded in the mapped_index's symbol table. For each C++ symbol
119 in the symbol table, we record one entry for the start of each
120 component in the symbol in a table of name components, and then
121 sort the table, in order to be able to binary search symbol names,
122 ignoring leading namespaces, both completion and regular look up.
123 For example, for symbol "A::B::C", we'll have an entry that points
124 to "A::B::C", another that points to "B::C", and another for "C".
125 Note that function symbols in GDB index have no parameter
126 information, just the function/method names. You can convert a
127 name_component to a "const char *" using the
128 'mapped_index::symbol_name_at(offset_type)' method. */
129
130 struct name_component
131 {
132 /* Offset in the symbol name where the component starts. Stored as
133 a (32-bit) offset instead of a pointer to save memory and improve
134 locality on 64-bit architectures. */
135 offset_type name_offset;
136
137 /* The symbol's index in the symbol and constant pool tables of a
138 mapped_index. */
139 offset_type idx;
140 };
141
142 /* Base class containing bits shared by both .gdb_index and
143 .debug_name indexes. */
144
145 struct mapped_index_base
146 {
147 mapped_index_base () = default;
148 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
149
150 /* The name_component table (a sorted vector). See name_component's
151 description above. */
152 std::vector<name_component> name_components;
153
154 /* How NAME_COMPONENTS is sorted. */
155 enum case_sensitivity name_components_casing;
156
157 /* Return the number of names in the symbol table. */
158 virtual size_t symbol_name_count () const = 0;
159
160 /* Get the name of the symbol at IDX in the symbol table. */
161 virtual const char *symbol_name_at (offset_type idx) const = 0;
162
163 /* Return whether the name at IDX in the symbol table should be
164 ignored. */
165 virtual bool symbol_name_slot_invalid (offset_type idx) const
166 {
167 return false;
168 }
169
170 /* Build the symbol name component sorted vector, if we haven't
171 yet. */
172 void build_name_components ();
173
174 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
175 possible matches for LN_NO_PARAMS in the name component
176 vector. */
177 std::pair<std::vector<name_component>::const_iterator,
178 std::vector<name_component>::const_iterator>
179 find_name_components_bounds (const lookup_name_info &ln_no_params,
180 enum language lang) const;
181
182 /* Prevent deleting/destroying via a base class pointer. */
183 protected:
184 ~mapped_index_base() = default;
185 };
186
187 /* A description of the mapped index. The file format is described in
188 a comment by the code that writes the index. */
189 struct mapped_index final : public mapped_index_base
190 {
191 /* A slot/bucket in the symbol table hash. */
192 struct symbol_table_slot
193 {
194 const offset_type name;
195 const offset_type vec;
196 };
197
198 /* Index data format version. */
199 int version = 0;
200
201 /* The address table data. */
202 gdb::array_view<const gdb_byte> address_table;
203
204 /* The symbol table, implemented as a hash table. */
205 gdb::array_view<symbol_table_slot> symbol_table;
206
207 /* A pointer to the constant pool. */
208 const char *constant_pool = nullptr;
209
210 bool symbol_name_slot_invalid (offset_type idx) const override
211 {
212 const auto &bucket = this->symbol_table[idx];
213 return bucket.name == 0 && bucket.vec == 0;
214 }
215
216 /* Convenience method to get at the name of the symbol at IDX in the
217 symbol table. */
218 const char *symbol_name_at (offset_type idx) const override
219 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
220
221 size_t symbol_name_count () const override
222 { return this->symbol_table.size (); }
223 };
224
225 /* A description of the mapped .debug_names.
226 Uninitialized map has CU_COUNT 0. */
227 struct mapped_debug_names final : public mapped_index_base
228 {
229 mapped_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile_)
230 : dwarf2_per_objfile (dwarf2_per_objfile_)
231 {}
232
233 struct dwarf2_per_objfile *dwarf2_per_objfile;
234 bfd_endian dwarf5_byte_order;
235 bool dwarf5_is_dwarf64;
236 bool augmentation_is_gdb;
237 uint8_t offset_size;
238 uint32_t cu_count = 0;
239 uint32_t tu_count, bucket_count, name_count;
240 const gdb_byte *cu_table_reordered, *tu_table_reordered;
241 const uint32_t *bucket_table_reordered, *hash_table_reordered;
242 const gdb_byte *name_table_string_offs_reordered;
243 const gdb_byte *name_table_entry_offs_reordered;
244 const gdb_byte *entry_pool;
245
246 struct index_val
247 {
248 ULONGEST dwarf_tag;
249 struct attr
250 {
251 /* Attribute name DW_IDX_*. */
252 ULONGEST dw_idx;
253
254 /* Attribute form DW_FORM_*. */
255 ULONGEST form;
256
257 /* Value if FORM is DW_FORM_implicit_const. */
258 LONGEST implicit_const;
259 };
260 std::vector<attr> attr_vec;
261 };
262
263 std::unordered_map<ULONGEST, index_val> abbrev_map;
264
265 const char *namei_to_name (uint32_t namei) const;
266
267 /* Implementation of the mapped_index_base virtual interface, for
268 the name_components cache. */
269
270 const char *symbol_name_at (offset_type idx) const override
271 { return namei_to_name (idx); }
272
273 size_t symbol_name_count () const override
274 { return this->name_count; }
275 };
276
277 /* See dwarf2read.h. */
278
279 dwarf2_per_objfile *
280 get_dwarf2_per_objfile (struct objfile *objfile)
281 {
282 return dwarf2_objfile_data_key.get (objfile);
283 }
284
285 /* Default names of the debugging sections. */
286
287 /* Note that if the debugging section has been compressed, it might
288 have a name like .zdebug_info. */
289
290 static const struct dwarf2_debug_sections dwarf2_elf_names =
291 {
292 { ".debug_info", ".zdebug_info" },
293 { ".debug_abbrev", ".zdebug_abbrev" },
294 { ".debug_line", ".zdebug_line" },
295 { ".debug_loc", ".zdebug_loc" },
296 { ".debug_loclists", ".zdebug_loclists" },
297 { ".debug_macinfo", ".zdebug_macinfo" },
298 { ".debug_macro", ".zdebug_macro" },
299 { ".debug_str", ".zdebug_str" },
300 { ".debug_str_offsets", ".zdebug_str_offsets" },
301 { ".debug_line_str", ".zdebug_line_str" },
302 { ".debug_ranges", ".zdebug_ranges" },
303 { ".debug_rnglists", ".zdebug_rnglists" },
304 { ".debug_types", ".zdebug_types" },
305 { ".debug_addr", ".zdebug_addr" },
306 { ".debug_frame", ".zdebug_frame" },
307 { ".eh_frame", NULL },
308 { ".gdb_index", ".zgdb_index" },
309 { ".debug_names", ".zdebug_names" },
310 { ".debug_aranges", ".zdebug_aranges" },
311 23
312 };
313
314 /* List of DWO/DWP sections. */
315
316 static const struct dwop_section_names
317 {
318 struct dwarf2_section_names abbrev_dwo;
319 struct dwarf2_section_names info_dwo;
320 struct dwarf2_section_names line_dwo;
321 struct dwarf2_section_names loc_dwo;
322 struct dwarf2_section_names loclists_dwo;
323 struct dwarf2_section_names macinfo_dwo;
324 struct dwarf2_section_names macro_dwo;
325 struct dwarf2_section_names str_dwo;
326 struct dwarf2_section_names str_offsets_dwo;
327 struct dwarf2_section_names types_dwo;
328 struct dwarf2_section_names cu_index;
329 struct dwarf2_section_names tu_index;
330 }
331 dwop_section_names =
332 {
333 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
334 { ".debug_info.dwo", ".zdebug_info.dwo" },
335 { ".debug_line.dwo", ".zdebug_line.dwo" },
336 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
337 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
338 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
339 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
340 { ".debug_str.dwo", ".zdebug_str.dwo" },
341 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
342 { ".debug_types.dwo", ".zdebug_types.dwo" },
343 { ".debug_cu_index", ".zdebug_cu_index" },
344 { ".debug_tu_index", ".zdebug_tu_index" },
345 };
346
347 /* local data types */
348
349 /* Type used for delaying computation of method physnames.
350 See comments for compute_delayed_physnames. */
351 struct delayed_method_info
352 {
353 /* The type to which the method is attached, i.e., its parent class. */
354 struct type *type;
355
356 /* The index of the method in the type's function fieldlists. */
357 int fnfield_index;
358
359 /* The index of the method in the fieldlist. */
360 int index;
361
362 /* The name of the DIE. */
363 const char *name;
364
365 /* The DIE associated with this method. */
366 struct die_info *die;
367 };
368
369 /* Internal state when decoding a particular compilation unit. */
370 struct dwarf2_cu
371 {
372 explicit dwarf2_cu (struct dwarf2_per_cu_data *per_cu);
373 ~dwarf2_cu ();
374
375 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
376
377 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
378 Create the set of symtabs used by this TU, or if this TU is sharing
379 symtabs with another TU and the symtabs have already been created
380 then restore those symtabs in the line header.
381 We don't need the pc/line-number mapping for type units. */
382 void setup_type_unit_groups (struct die_info *die);
383
384 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
385 buildsym_compunit constructor. */
386 struct compunit_symtab *start_symtab (const char *name,
387 const char *comp_dir,
388 CORE_ADDR low_pc);
389
390 /* Reset the builder. */
391 void reset_builder () { m_builder.reset (); }
392
393 /* The header of the compilation unit. */
394 struct comp_unit_head header {};
395
396 /* Base address of this compilation unit. */
397 gdb::optional<CORE_ADDR> base_address;
398
399 /* The language we are debugging. */
400 enum language language = language_unknown;
401 const struct language_defn *language_defn = nullptr;
402
403 const char *producer = nullptr;
404
405 private:
406 /* The symtab builder for this CU. This is only non-NULL when full
407 symbols are being read. */
408 std::unique_ptr<buildsym_compunit> m_builder;
409
410 public:
411 /* The generic symbol table building routines have separate lists for
412 file scope symbols and all all other scopes (local scopes). So
413 we need to select the right one to pass to add_symbol_to_list().
414 We do it by keeping a pointer to the correct list in list_in_scope.
415
416 FIXME: The original dwarf code just treated the file scope as the
417 first local scope, and all other local scopes as nested local
418 scopes, and worked fine. Check to see if we really need to
419 distinguish these in buildsym.c. */
420 struct pending **list_in_scope = nullptr;
421
422 /* Hash table holding all the loaded partial DIEs
423 with partial_die->offset.SECT_OFF as hash. */
424 htab_t partial_dies = nullptr;
425
426 /* Storage for things with the same lifetime as this read-in compilation
427 unit, including partial DIEs. */
428 auto_obstack comp_unit_obstack;
429
430 /* When multiple dwarf2_cu structures are living in memory, this field
431 chains them all together, so that they can be released efficiently.
432 We will probably also want a generation counter so that most-recently-used
433 compilation units are cached... */
434 struct dwarf2_per_cu_data *read_in_chain = nullptr;
435
436 /* Backlink to our per_cu entry. */
437 struct dwarf2_per_cu_data *per_cu;
438
439 /* How many compilation units ago was this CU last referenced? */
440 int last_used = 0;
441
442 /* A hash table of DIE cu_offset for following references with
443 die_info->offset.sect_off as hash. */
444 htab_t die_hash = nullptr;
445
446 /* Full DIEs if read in. */
447 struct die_info *dies = nullptr;
448
449 /* A set of pointers to dwarf2_per_cu_data objects for compilation
450 units referenced by this one. Only set during full symbol processing;
451 partial symbol tables do not have dependencies. */
452 htab_t dependencies = nullptr;
453
454 /* Header data from the line table, during full symbol processing. */
455 struct line_header *line_header = nullptr;
456 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
457 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
458 this is the DW_TAG_compile_unit die for this CU. We'll hold on
459 to the line header as long as this DIE is being processed. See
460 process_die_scope. */
461 die_info *line_header_die_owner = nullptr;
462
463 /* A list of methods which need to have physnames computed
464 after all type information has been read. */
465 std::vector<delayed_method_info> method_list;
466
467 /* To be copied to symtab->call_site_htab. */
468 htab_t call_site_htab = nullptr;
469
470 /* Non-NULL if this CU came from a DWO file.
471 There is an invariant here that is important to remember:
472 Except for attributes copied from the top level DIE in the "main"
473 (or "stub") file in preparation for reading the DWO file
474 (e.g., DW_AT_addr_base), we KISS: there is only *one* CU.
475 Either there isn't a DWO file (in which case this is NULL and the point
476 is moot), or there is and either we're not going to read it (in which
477 case this is NULL) or there is and we are reading it (in which case this
478 is non-NULL). */
479 struct dwo_unit *dwo_unit = nullptr;
480
481 /* The DW_AT_addr_base (DW_AT_GNU_addr_base) attribute if present.
482 Note this value comes from the Fission stub CU/TU's DIE. */
483 gdb::optional<ULONGEST> addr_base;
484
485 /* The DW_AT_rnglists_base attribute if present.
486 Note this value comes from the Fission stub CU/TU's DIE.
487 Also note that the value is zero in the non-DWO case so this value can
488 be used without needing to know whether DWO files are in use or not.
489 N.B. This does not apply to DW_AT_ranges appearing in
490 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
491 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
492 DW_AT_rnglists_base *would* have to be applied, and we'd have to care
493 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
494 ULONGEST ranges_base = 0;
495
496 /* When reading debug info generated by older versions of rustc, we
497 have to rewrite some union types to be struct types with a
498 variant part. This rewriting must be done after the CU is fully
499 read in, because otherwise at the point of rewriting some struct
500 type might not have been fully processed. So, we keep a list of
501 all such types here and process them after expansion. */
502 std::vector<struct type *> rust_unions;
503
504 /* The DW_AT_str_offsets_base attribute if present. For DWARF 4 version DWO
505 files, the value is implicitly zero. For DWARF 5 version DWO files, the
506 value is often implicit and is the size of the header of
507 .debug_str_offsets section (8 or 4, depending on the address size). */
508 gdb::optional<ULONGEST> str_offsets_base;
509
510 /* Mark used when releasing cached dies. */
511 bool mark : 1;
512
513 /* This CU references .debug_loc. See the symtab->locations_valid field.
514 This test is imperfect as there may exist optimized debug code not using
515 any location list and still facing inlining issues if handled as
516 unoptimized code. For a future better test see GCC PR other/32998. */
517 bool has_loclist : 1;
518
519 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
520 if all the producer_is_* fields are valid. This information is cached
521 because profiling CU expansion showed excessive time spent in
522 producer_is_gxx_lt_4_6. */
523 bool checked_producer : 1;
524 bool producer_is_gxx_lt_4_6 : 1;
525 bool producer_is_gcc_lt_4_3 : 1;
526 bool producer_is_icc : 1;
527 bool producer_is_icc_lt_14 : 1;
528 bool producer_is_codewarrior : 1;
529
530 /* When true, the file that we're processing is known to have
531 debugging info for C++ namespaces. GCC 3.3.x did not produce
532 this information, but later versions do. */
533
534 bool processing_has_namespace_info : 1;
535
536 struct partial_die_info *find_partial_die (sect_offset sect_off);
537
538 /* If this CU was inherited by another CU (via specification,
539 abstract_origin, etc), this is the ancestor CU. */
540 dwarf2_cu *ancestor;
541
542 /* Get the buildsym_compunit for this CU. */
543 buildsym_compunit *get_builder ()
544 {
545 /* If this CU has a builder associated with it, use that. */
546 if (m_builder != nullptr)
547 return m_builder.get ();
548
549 /* Otherwise, search ancestors for a valid builder. */
550 if (ancestor != nullptr)
551 return ancestor->get_builder ();
552
553 return nullptr;
554 }
555 };
556
557 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
558 This includes type_unit_group and quick_file_names. */
559
560 struct stmt_list_hash
561 {
562 /* The DWO unit this table is from or NULL if there is none. */
563 struct dwo_unit *dwo_unit;
564
565 /* Offset in .debug_line or .debug_line.dwo. */
566 sect_offset line_sect_off;
567 };
568
569 /* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
570 an object of this type. */
571
572 struct type_unit_group
573 {
574 /* dwarf2read.c's main "handle" on a TU symtab.
575 To simplify things we create an artificial CU that "includes" all the
576 type units using this stmt_list so that the rest of the code still has
577 a "per_cu" handle on the symtab. */
578 struct dwarf2_per_cu_data per_cu;
579
580 /* The TUs that share this DW_AT_stmt_list entry.
581 This is added to while parsing type units to build partial symtabs,
582 and is deleted afterwards and not used again. */
583 std::vector<signatured_type *> *tus;
584
585 /* The compunit symtab.
586 Type units in a group needn't all be defined in the same source file,
587 so we create an essentially anonymous symtab as the compunit symtab. */
588 struct compunit_symtab *compunit_symtab;
589
590 /* The data used to construct the hash key. */
591 struct stmt_list_hash hash;
592
593 /* The symbol tables for this TU (obtained from the files listed in
594 DW_AT_stmt_list).
595 WARNING: The order of entries here must match the order of entries
596 in the line header. After the first TU using this type_unit_group, the
597 line header for the subsequent TUs is recreated from this. This is done
598 because we need to use the same symtabs for each TU using the same
599 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
600 there's no guarantee the line header doesn't have duplicate entries. */
601 struct symtab **symtabs;
602 };
603
604 /* These sections are what may appear in a (real or virtual) DWO file. */
605
606 struct dwo_sections
607 {
608 struct dwarf2_section_info abbrev;
609 struct dwarf2_section_info line;
610 struct dwarf2_section_info loc;
611 struct dwarf2_section_info loclists;
612 struct dwarf2_section_info macinfo;
613 struct dwarf2_section_info macro;
614 struct dwarf2_section_info str;
615 struct dwarf2_section_info str_offsets;
616 /* In the case of a virtual DWO file, these two are unused. */
617 struct dwarf2_section_info info;
618 std::vector<dwarf2_section_info> types;
619 };
620
621 /* CUs/TUs in DWP/DWO files. */
622
623 struct dwo_unit
624 {
625 /* Backlink to the containing struct dwo_file. */
626 struct dwo_file *dwo_file;
627
628 /* The "id" that distinguishes this CU/TU.
629 .debug_info calls this "dwo_id", .debug_types calls this "signature".
630 Since signatures came first, we stick with it for consistency. */
631 ULONGEST signature;
632
633 /* The section this CU/TU lives in, in the DWO file. */
634 struct dwarf2_section_info *section;
635
636 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
637 sect_offset sect_off;
638 unsigned int length;
639
640 /* For types, offset in the type's DIE of the type defined by this TU. */
641 cu_offset type_offset_in_tu;
642 };
643
644 /* include/dwarf2.h defines the DWP section codes.
645 It defines a max value but it doesn't define a min value, which we
646 use for error checking, so provide one. */
647
648 enum dwp_v2_section_ids
649 {
650 DW_SECT_MIN = 1
651 };
652
653 /* Data for one DWO file.
654
655 This includes virtual DWO files (a virtual DWO file is a DWO file as it
656 appears in a DWP file). DWP files don't really have DWO files per se -
657 comdat folding of types "loses" the DWO file they came from, and from
658 a high level view DWP files appear to contain a mass of random types.
659 However, to maintain consistency with the non-DWP case we pretend DWP
660 files contain virtual DWO files, and we assign each TU with one virtual
661 DWO file (generally based on the line and abbrev section offsets -
662 a heuristic that seems to work in practice). */
663
664 struct dwo_file
665 {
666 dwo_file () = default;
667 DISABLE_COPY_AND_ASSIGN (dwo_file);
668
669 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
670 For virtual DWO files the name is constructed from the section offsets
671 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
672 from related CU+TUs. */
673 const char *dwo_name = nullptr;
674
675 /* The DW_AT_comp_dir attribute. */
676 const char *comp_dir = nullptr;
677
678 /* The bfd, when the file is open. Otherwise this is NULL.
679 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
680 gdb_bfd_ref_ptr dbfd;
681
682 /* The sections that make up this DWO file.
683 Remember that for virtual DWO files in DWP V2, these are virtual
684 sections (for lack of a better name). */
685 struct dwo_sections sections {};
686
687 /* The CUs in the file.
688 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
689 an extension to handle LLVM's Link Time Optimization output (where
690 multiple source files may be compiled into a single object/dwo pair). */
691 htab_up cus;
692
693 /* Table of TUs in the file.
694 Each element is a struct dwo_unit. */
695 htab_up tus;
696 };
697
698 /* These sections are what may appear in a DWP file. */
699
700 struct dwp_sections
701 {
702 /* These are used by both DWP version 1 and 2. */
703 struct dwarf2_section_info str;
704 struct dwarf2_section_info cu_index;
705 struct dwarf2_section_info tu_index;
706
707 /* These are only used by DWP version 2 files.
708 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
709 sections are referenced by section number, and are not recorded here.
710 In DWP version 2 there is at most one copy of all these sections, each
711 section being (effectively) comprised of the concatenation of all of the
712 individual sections that exist in the version 1 format.
713 To keep the code simple we treat each of these concatenated pieces as a
714 section itself (a virtual section?). */
715 struct dwarf2_section_info abbrev;
716 struct dwarf2_section_info info;
717 struct dwarf2_section_info line;
718 struct dwarf2_section_info loc;
719 struct dwarf2_section_info macinfo;
720 struct dwarf2_section_info macro;
721 struct dwarf2_section_info str_offsets;
722 struct dwarf2_section_info types;
723 };
724
725 /* These sections are what may appear in a virtual DWO file in DWP version 1.
726 A virtual DWO file is a DWO file as it appears in a DWP file. */
727
728 struct virtual_v1_dwo_sections
729 {
730 struct dwarf2_section_info abbrev;
731 struct dwarf2_section_info line;
732 struct dwarf2_section_info loc;
733 struct dwarf2_section_info macinfo;
734 struct dwarf2_section_info macro;
735 struct dwarf2_section_info str_offsets;
736 /* Each DWP hash table entry records one CU or one TU.
737 That is recorded here, and copied to dwo_unit.section. */
738 struct dwarf2_section_info info_or_types;
739 };
740
741 /* Similar to virtual_v1_dwo_sections, but for DWP version 2.
742 In version 2, the sections of the DWO files are concatenated together
743 and stored in one section of that name. Thus each ELF section contains
744 several "virtual" sections. */
745
746 struct virtual_v2_dwo_sections
747 {
748 bfd_size_type abbrev_offset;
749 bfd_size_type abbrev_size;
750
751 bfd_size_type line_offset;
752 bfd_size_type line_size;
753
754 bfd_size_type loc_offset;
755 bfd_size_type loc_size;
756
757 bfd_size_type macinfo_offset;
758 bfd_size_type macinfo_size;
759
760 bfd_size_type macro_offset;
761 bfd_size_type macro_size;
762
763 bfd_size_type str_offsets_offset;
764 bfd_size_type str_offsets_size;
765
766 /* Each DWP hash table entry records one CU or one TU.
767 That is recorded here, and copied to dwo_unit.section. */
768 bfd_size_type info_or_types_offset;
769 bfd_size_type info_or_types_size;
770 };
771
772 /* Contents of DWP hash tables. */
773
774 struct dwp_hash_table
775 {
776 uint32_t version, nr_columns;
777 uint32_t nr_units, nr_slots;
778 const gdb_byte *hash_table, *unit_table;
779 union
780 {
781 struct
782 {
783 const gdb_byte *indices;
784 } v1;
785 struct
786 {
787 /* This is indexed by column number and gives the id of the section
788 in that column. */
789 #define MAX_NR_V2_DWO_SECTIONS \
790 (1 /* .debug_info or .debug_types */ \
791 + 1 /* .debug_abbrev */ \
792 + 1 /* .debug_line */ \
793 + 1 /* .debug_loc */ \
794 + 1 /* .debug_str_offsets */ \
795 + 1 /* .debug_macro or .debug_macinfo */)
796 int section_ids[MAX_NR_V2_DWO_SECTIONS];
797 const gdb_byte *offsets;
798 const gdb_byte *sizes;
799 } v2;
800 } section_pool;
801 };
802
803 /* Data for one DWP file. */
804
805 struct dwp_file
806 {
807 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
808 : name (name_),
809 dbfd (std::move (abfd))
810 {
811 }
812
813 /* Name of the file. */
814 const char *name;
815
816 /* File format version. */
817 int version = 0;
818
819 /* The bfd. */
820 gdb_bfd_ref_ptr dbfd;
821
822 /* Section info for this file. */
823 struct dwp_sections sections {};
824
825 /* Table of CUs in the file. */
826 const struct dwp_hash_table *cus = nullptr;
827
828 /* Table of TUs in the file. */
829 const struct dwp_hash_table *tus = nullptr;
830
831 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
832 htab_up loaded_cus;
833 htab_up loaded_tus;
834
835 /* Table to map ELF section numbers to their sections.
836 This is only needed for the DWP V1 file format. */
837 unsigned int num_sections = 0;
838 asection **elf_sections = nullptr;
839 };
840
841 /* Struct used to pass misc. parameters to read_die_and_children, et
842 al. which are used for both .debug_info and .debug_types dies.
843 All parameters here are unchanging for the life of the call. This
844 struct exists to abstract away the constant parameters of die reading. */
845
846 struct die_reader_specs
847 {
848 /* The bfd of die_section. */
849 bfd* abfd;
850
851 /* The CU of the DIE we are parsing. */
852 struct dwarf2_cu *cu;
853
854 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
855 struct dwo_file *dwo_file;
856
857 /* The section the die comes from.
858 This is either .debug_info or .debug_types, or the .dwo variants. */
859 struct dwarf2_section_info *die_section;
860
861 /* die_section->buffer. */
862 const gdb_byte *buffer;
863
864 /* The end of the buffer. */
865 const gdb_byte *buffer_end;
866
867 /* The abbreviation table to use when reading the DIEs. */
868 struct abbrev_table *abbrev_table;
869 };
870
871 /* A subclass of die_reader_specs that holds storage and has complex
872 constructor and destructor behavior. */
873
874 class cutu_reader : public die_reader_specs
875 {
876 public:
877
878 cutu_reader (struct dwarf2_per_cu_data *this_cu,
879 struct abbrev_table *abbrev_table,
880 int use_existing_cu,
881 bool skip_partial);
882
883 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
884 struct dwarf2_cu *parent_cu = nullptr,
885 struct dwo_file *dwo_file = nullptr);
886
887 DISABLE_COPY_AND_ASSIGN (cutu_reader);
888
889 const gdb_byte *info_ptr = nullptr;
890 struct die_info *comp_unit_die = nullptr;
891 bool dummy_p = false;
892
893 /* Release the new CU, putting it on the chain. This cannot be done
894 for dummy CUs. */
895 void keep ();
896
897 private:
898 void init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
899 int use_existing_cu);
900
901 struct dwarf2_per_cu_data *m_this_cu;
902 std::unique_ptr<dwarf2_cu> m_new_cu;
903
904 /* The ordinary abbreviation table. */
905 abbrev_table_up m_abbrev_table_holder;
906
907 /* The DWO abbreviation table. */
908 abbrev_table_up m_dwo_abbrev_table;
909 };
910
911 /* When we construct a partial symbol table entry we only
912 need this much information. */
913 struct partial_die_info : public allocate_on_obstack
914 {
915 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
916
917 /* Disable assign but still keep copy ctor, which is needed
918 load_partial_dies. */
919 partial_die_info& operator=(const partial_die_info& rhs) = delete;
920
921 /* Adjust the partial die before generating a symbol for it. This
922 function may set the is_external flag or change the DIE's
923 name. */
924 void fixup (struct dwarf2_cu *cu);
925
926 /* Read a minimal amount of information into the minimal die
927 structure. */
928 const gdb_byte *read (const struct die_reader_specs *reader,
929 const struct abbrev_info &abbrev,
930 const gdb_byte *info_ptr);
931
932 /* Offset of this DIE. */
933 const sect_offset sect_off;
934
935 /* DWARF-2 tag for this DIE. */
936 const ENUM_BITFIELD(dwarf_tag) tag : 16;
937
938 /* Assorted flags describing the data found in this DIE. */
939 const unsigned int has_children : 1;
940
941 unsigned int is_external : 1;
942 unsigned int is_declaration : 1;
943 unsigned int has_type : 1;
944 unsigned int has_specification : 1;
945 unsigned int has_pc_info : 1;
946 unsigned int may_be_inlined : 1;
947
948 /* This DIE has been marked DW_AT_main_subprogram. */
949 unsigned int main_subprogram : 1;
950
951 /* Flag set if the SCOPE field of this structure has been
952 computed. */
953 unsigned int scope_set : 1;
954
955 /* Flag set if the DIE has a byte_size attribute. */
956 unsigned int has_byte_size : 1;
957
958 /* Flag set if the DIE has a DW_AT_const_value attribute. */
959 unsigned int has_const_value : 1;
960
961 /* Flag set if any of the DIE's children are template arguments. */
962 unsigned int has_template_arguments : 1;
963
964 /* Flag set if fixup has been called on this die. */
965 unsigned int fixup_called : 1;
966
967 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
968 unsigned int is_dwz : 1;
969
970 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
971 unsigned int spec_is_dwz : 1;
972
973 /* The name of this DIE. Normally the value of DW_AT_name, but
974 sometimes a default name for unnamed DIEs. */
975 const char *name = nullptr;
976
977 /* The linkage name, if present. */
978 const char *linkage_name = nullptr;
979
980 /* The scope to prepend to our children. This is generally
981 allocated on the comp_unit_obstack, so will disappear
982 when this compilation unit leaves the cache. */
983 const char *scope = nullptr;
984
985 /* Some data associated with the partial DIE. The tag determines
986 which field is live. */
987 union
988 {
989 /* The location description associated with this DIE, if any. */
990 struct dwarf_block *locdesc;
991 /* The offset of an import, for DW_TAG_imported_unit. */
992 sect_offset sect_off;
993 } d {};
994
995 /* If HAS_PC_INFO, the PC range associated with this DIE. */
996 CORE_ADDR lowpc = 0;
997 CORE_ADDR highpc = 0;
998
999 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1000 DW_AT_sibling, if any. */
1001 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1002 could return DW_AT_sibling values to its caller load_partial_dies. */
1003 const gdb_byte *sibling = nullptr;
1004
1005 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1006 DW_AT_specification (or DW_AT_abstract_origin or
1007 DW_AT_extension). */
1008 sect_offset spec_offset {};
1009
1010 /* Pointers to this DIE's parent, first child, and next sibling,
1011 if any. */
1012 struct partial_die_info *die_parent = nullptr;
1013 struct partial_die_info *die_child = nullptr;
1014 struct partial_die_info *die_sibling = nullptr;
1015
1016 friend struct partial_die_info *
1017 dwarf2_cu::find_partial_die (sect_offset sect_off);
1018
1019 private:
1020 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1021 partial_die_info (sect_offset sect_off)
1022 : partial_die_info (sect_off, DW_TAG_padding, 0)
1023 {
1024 }
1025
1026 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1027 int has_children_)
1028 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1029 {
1030 is_external = 0;
1031 is_declaration = 0;
1032 has_type = 0;
1033 has_specification = 0;
1034 has_pc_info = 0;
1035 may_be_inlined = 0;
1036 main_subprogram = 0;
1037 scope_set = 0;
1038 has_byte_size = 0;
1039 has_const_value = 0;
1040 has_template_arguments = 0;
1041 fixup_called = 0;
1042 is_dwz = 0;
1043 spec_is_dwz = 0;
1044 }
1045 };
1046
1047 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1048 but this would require a corresponding change in unpack_field_as_long
1049 and friends. */
1050 static int bits_per_byte = 8;
1051
1052 /* When reading a variant or variant part, we track a bit more
1053 information about the field, and store it in an object of this
1054 type. */
1055
1056 struct variant_field
1057 {
1058 /* If we see a DW_TAG_variant, then this will be the discriminant
1059 value. */
1060 ULONGEST discriminant_value;
1061 /* If we see a DW_TAG_variant, then this will be set if this is the
1062 default branch. */
1063 bool default_branch;
1064 /* While reading a DW_TAG_variant_part, this will be set if this
1065 field is the discriminant. */
1066 bool is_discriminant;
1067 };
1068
1069 struct nextfield
1070 {
1071 int accessibility = 0;
1072 int virtuality = 0;
1073 /* Extra information to describe a variant or variant part. */
1074 struct variant_field variant {};
1075 struct field field {};
1076 };
1077
1078 struct fnfieldlist
1079 {
1080 const char *name = nullptr;
1081 std::vector<struct fn_field> fnfields;
1082 };
1083
1084 /* The routines that read and process dies for a C struct or C++ class
1085 pass lists of data member fields and lists of member function fields
1086 in an instance of a field_info structure, as defined below. */
1087 struct field_info
1088 {
1089 /* List of data member and baseclasses fields. */
1090 std::vector<struct nextfield> fields;
1091 std::vector<struct nextfield> baseclasses;
1092
1093 /* Set if the accessibility of one of the fields is not public. */
1094 int non_public_fields = 0;
1095
1096 /* Member function fieldlist array, contains name of possibly overloaded
1097 member function, number of overloaded member functions and a pointer
1098 to the head of the member function field chain. */
1099 std::vector<struct fnfieldlist> fnfieldlists;
1100
1101 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1102 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1103 std::vector<struct decl_field> typedef_field_list;
1104
1105 /* Nested types defined by this class and the number of elements in this
1106 list. */
1107 std::vector<struct decl_field> nested_types_list;
1108
1109 /* Return the total number of fields (including baseclasses). */
1110 int nfields () const
1111 {
1112 return fields.size () + baseclasses.size ();
1113 }
1114 };
1115
1116 /* Loaded secondary compilation units are kept in memory until they
1117 have not been referenced for the processing of this many
1118 compilation units. Set this to zero to disable caching. Cache
1119 sizes of up to at least twenty will improve startup time for
1120 typical inter-CU-reference binaries, at an obvious memory cost. */
1121 static int dwarf_max_cache_age = 5;
1122 static void
1123 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1124 struct cmd_list_element *c, const char *value)
1125 {
1126 fprintf_filtered (file, _("The upper bound on the age of cached "
1127 "DWARF compilation units is %s.\n"),
1128 value);
1129 }
1130 \f
1131 /* local function prototypes */
1132
1133 static void dwarf2_find_base_address (struct die_info *die,
1134 struct dwarf2_cu *cu);
1135
1136 static dwarf2_psymtab *create_partial_symtab
1137 (struct dwarf2_per_cu_data *per_cu, const char *name);
1138
1139 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1140 const gdb_byte *info_ptr,
1141 struct die_info *type_unit_die);
1142
1143 static void dwarf2_build_psymtabs_hard
1144 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1145
1146 static void scan_partial_symbols (struct partial_die_info *,
1147 CORE_ADDR *, CORE_ADDR *,
1148 int, struct dwarf2_cu *);
1149
1150 static void add_partial_symbol (struct partial_die_info *,
1151 struct dwarf2_cu *);
1152
1153 static void add_partial_namespace (struct partial_die_info *pdi,
1154 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1155 int set_addrmap, struct dwarf2_cu *cu);
1156
1157 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1158 CORE_ADDR *highpc, int set_addrmap,
1159 struct dwarf2_cu *cu);
1160
1161 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1162 struct dwarf2_cu *cu);
1163
1164 static void add_partial_subprogram (struct partial_die_info *pdi,
1165 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1166 int need_pc, struct dwarf2_cu *cu);
1167
1168 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1169
1170 static struct partial_die_info *load_partial_dies
1171 (const struct die_reader_specs *, const gdb_byte *, int);
1172
1173 /* A pair of partial_die_info and compilation unit. */
1174 struct cu_partial_die_info
1175 {
1176 /* The compilation unit of the partial_die_info. */
1177 struct dwarf2_cu *cu;
1178 /* A partial_die_info. */
1179 struct partial_die_info *pdi;
1180
1181 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1182 : cu (cu),
1183 pdi (pdi)
1184 { /* Nothing. */ }
1185
1186 private:
1187 cu_partial_die_info () = delete;
1188 };
1189
1190 static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1191 struct dwarf2_cu *);
1192
1193 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1194 struct attribute *, struct attr_abbrev *,
1195 const gdb_byte *, bool *need_reprocess);
1196
1197 static void read_attribute_reprocess (const struct die_reader_specs *reader,
1198 struct attribute *attr);
1199
1200 static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
1201
1202 static sect_offset read_abbrev_offset
1203 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1204 struct dwarf2_section_info *, sect_offset);
1205
1206 static const char *read_indirect_string
1207 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1208 const struct comp_unit_head *, unsigned int *);
1209
1210 static const char *read_indirect_string_at_offset
1211 (struct dwarf2_per_objfile *dwarf2_per_objfile, LONGEST str_offset);
1212
1213 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1214 const gdb_byte *,
1215 unsigned int *);
1216
1217 static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1218 ULONGEST str_index);
1219
1220 static const char *read_stub_str_index (struct dwarf2_cu *cu,
1221 ULONGEST str_index);
1222
1223 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1224
1225 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1226 struct dwarf2_cu *);
1227
1228 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1229 struct dwarf2_cu *cu);
1230
1231 static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1232
1233 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1234 struct dwarf2_cu *cu);
1235
1236 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1237
1238 static struct die_info *die_specification (struct die_info *die,
1239 struct dwarf2_cu **);
1240
1241 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1242 struct dwarf2_cu *cu);
1243
1244 static void dwarf_decode_lines (struct line_header *, const char *,
1245 struct dwarf2_cu *, dwarf2_psymtab *,
1246 CORE_ADDR, int decode_mapping);
1247
1248 static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1249 const char *);
1250
1251 static struct symbol *new_symbol (struct die_info *, struct type *,
1252 struct dwarf2_cu *, struct symbol * = NULL);
1253
1254 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1255 struct dwarf2_cu *);
1256
1257 static void dwarf2_const_value_attr (const struct attribute *attr,
1258 struct type *type,
1259 const char *name,
1260 struct obstack *obstack,
1261 struct dwarf2_cu *cu, LONGEST *value,
1262 const gdb_byte **bytes,
1263 struct dwarf2_locexpr_baton **baton);
1264
1265 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1266
1267 static int need_gnat_info (struct dwarf2_cu *);
1268
1269 static struct type *die_descriptive_type (struct die_info *,
1270 struct dwarf2_cu *);
1271
1272 static void set_descriptive_type (struct type *, struct die_info *,
1273 struct dwarf2_cu *);
1274
1275 static struct type *die_containing_type (struct die_info *,
1276 struct dwarf2_cu *);
1277
1278 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1279 struct dwarf2_cu *);
1280
1281 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1282
1283 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1284
1285 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1286
1287 static char *typename_concat (struct obstack *obs, const char *prefix,
1288 const char *suffix, int physname,
1289 struct dwarf2_cu *cu);
1290
1291 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1292
1293 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1294
1295 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1296
1297 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1298
1299 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1300
1301 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1302
1303 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1304 struct dwarf2_cu *, dwarf2_psymtab *);
1305
1306 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1307 values. Keep the items ordered with increasing constraints compliance. */
1308 enum pc_bounds_kind
1309 {
1310 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1311 PC_BOUNDS_NOT_PRESENT,
1312
1313 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1314 were present but they do not form a valid range of PC addresses. */
1315 PC_BOUNDS_INVALID,
1316
1317 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1318 PC_BOUNDS_RANGES,
1319
1320 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1321 PC_BOUNDS_HIGH_LOW,
1322 };
1323
1324 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1325 CORE_ADDR *, CORE_ADDR *,
1326 struct dwarf2_cu *,
1327 dwarf2_psymtab *);
1328
1329 static void get_scope_pc_bounds (struct die_info *,
1330 CORE_ADDR *, CORE_ADDR *,
1331 struct dwarf2_cu *);
1332
1333 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1334 CORE_ADDR, struct dwarf2_cu *);
1335
1336 static void dwarf2_add_field (struct field_info *, struct die_info *,
1337 struct dwarf2_cu *);
1338
1339 static void dwarf2_attach_fields_to_type (struct field_info *,
1340 struct type *, struct dwarf2_cu *);
1341
1342 static void dwarf2_add_member_fn (struct field_info *,
1343 struct die_info *, struct type *,
1344 struct dwarf2_cu *);
1345
1346 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1347 struct type *,
1348 struct dwarf2_cu *);
1349
1350 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1351
1352 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1353
1354 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1355
1356 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1357
1358 static struct using_direct **using_directives (struct dwarf2_cu *cu);
1359
1360 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1361
1362 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1363
1364 static struct type *read_module_type (struct die_info *die,
1365 struct dwarf2_cu *cu);
1366
1367 static const char *namespace_name (struct die_info *die,
1368 int *is_anonymous, struct dwarf2_cu *);
1369
1370 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1371
1372 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1373
1374 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1375 struct dwarf2_cu *);
1376
1377 static struct die_info *read_die_and_siblings_1
1378 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1379 struct die_info *);
1380
1381 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1382 const gdb_byte *info_ptr,
1383 const gdb_byte **new_info_ptr,
1384 struct die_info *parent);
1385
1386 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1387 struct die_info **, const gdb_byte *,
1388 int);
1389
1390 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1391 struct die_info **, const gdb_byte *);
1392
1393 static void process_die (struct die_info *, struct dwarf2_cu *);
1394
1395 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1396 struct objfile *);
1397
1398 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1399
1400 static const char *dwarf2_full_name (const char *name,
1401 struct die_info *die,
1402 struct dwarf2_cu *cu);
1403
1404 static const char *dwarf2_physname (const char *name, struct die_info *die,
1405 struct dwarf2_cu *cu);
1406
1407 static struct die_info *dwarf2_extension (struct die_info *die,
1408 struct dwarf2_cu **);
1409
1410 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1411
1412 static void dump_die_for_error (struct die_info *);
1413
1414 static void dump_die_1 (struct ui_file *, int level, int max_level,
1415 struct die_info *);
1416
1417 /*static*/ void dump_die (struct die_info *, int max_level);
1418
1419 static void store_in_ref_table (struct die_info *,
1420 struct dwarf2_cu *);
1421
1422 static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
1423
1424 static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
1425
1426 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1427 const struct attribute *,
1428 struct dwarf2_cu **);
1429
1430 static struct die_info *follow_die_ref (struct die_info *,
1431 const struct attribute *,
1432 struct dwarf2_cu **);
1433
1434 static struct die_info *follow_die_sig (struct die_info *,
1435 const struct attribute *,
1436 struct dwarf2_cu **);
1437
1438 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1439 struct dwarf2_cu *);
1440
1441 static struct type *get_DW_AT_signature_type (struct die_info *,
1442 const struct attribute *,
1443 struct dwarf2_cu *);
1444
1445 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1446
1447 static void read_signatured_type (struct signatured_type *);
1448
1449 static int attr_to_dynamic_prop (const struct attribute *attr,
1450 struct die_info *die, struct dwarf2_cu *cu,
1451 struct dynamic_prop *prop, struct type *type);
1452
1453 /* memory allocation interface */
1454
1455 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1456
1457 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1458
1459 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1460
1461 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1462 struct dwarf2_loclist_baton *baton,
1463 const struct attribute *attr);
1464
1465 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1466 struct symbol *sym,
1467 struct dwarf2_cu *cu,
1468 int is_block);
1469
1470 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1471 const gdb_byte *info_ptr,
1472 struct abbrev_info *abbrev);
1473
1474 static hashval_t partial_die_hash (const void *item);
1475
1476 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1477
1478 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1479 (sect_offset sect_off, unsigned int offset_in_dwz,
1480 struct dwarf2_per_objfile *dwarf2_per_objfile);
1481
1482 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1483 struct die_info *comp_unit_die,
1484 enum language pretend_language);
1485
1486 static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1487
1488 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1489
1490 static struct type *set_die_type (struct die_info *, struct type *,
1491 struct dwarf2_cu *);
1492
1493 static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1494
1495 static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1496
1497 static void load_full_comp_unit (struct dwarf2_per_cu_data *, bool,
1498 enum language);
1499
1500 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1501 enum language);
1502
1503 static void process_full_type_unit (struct dwarf2_per_cu_data *,
1504 enum language);
1505
1506 static void dwarf2_add_dependence (struct dwarf2_cu *,
1507 struct dwarf2_per_cu_data *);
1508
1509 static void dwarf2_mark (struct dwarf2_cu *);
1510
1511 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1512
1513 static struct type *get_die_type_at_offset (sect_offset,
1514 struct dwarf2_per_cu_data *);
1515
1516 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1517
1518 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1519 enum language pretend_language);
1520
1521 static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
1522
1523 /* Class, the destructor of which frees all allocated queue entries. This
1524 will only have work to do if an error was thrown while processing the
1525 dwarf. If no error was thrown then the queue entries should have all
1526 been processed, and freed, as we went along. */
1527
1528 class dwarf2_queue_guard
1529 {
1530 public:
1531 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1532 : m_per_objfile (per_objfile)
1533 {
1534 }
1535
1536 /* Free any entries remaining on the queue. There should only be
1537 entries left if we hit an error while processing the dwarf. */
1538 ~dwarf2_queue_guard ()
1539 {
1540 /* Ensure that no memory is allocated by the queue. */
1541 std::queue<dwarf2_queue_item> empty;
1542 std::swap (m_per_objfile->queue, empty);
1543 }
1544
1545 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
1546
1547 private:
1548 dwarf2_per_objfile *m_per_objfile;
1549 };
1550
1551 dwarf2_queue_item::~dwarf2_queue_item ()
1552 {
1553 /* Anything still marked queued is likely to be in an
1554 inconsistent state, so discard it. */
1555 if (per_cu->queued)
1556 {
1557 if (per_cu->cu != NULL)
1558 free_one_cached_comp_unit (per_cu);
1559 per_cu->queued = 0;
1560 }
1561 }
1562
1563 /* The return type of find_file_and_directory. Note, the enclosed
1564 string pointers are only valid while this object is valid. */
1565
1566 struct file_and_directory
1567 {
1568 /* The filename. This is never NULL. */
1569 const char *name;
1570
1571 /* The compilation directory. NULL if not known. If we needed to
1572 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1573 points directly to the DW_AT_comp_dir string attribute owned by
1574 the obstack that owns the DIE. */
1575 const char *comp_dir;
1576
1577 /* If we needed to build a new string for comp_dir, this is what
1578 owns the storage. */
1579 std::string comp_dir_storage;
1580 };
1581
1582 static file_and_directory find_file_and_directory (struct die_info *die,
1583 struct dwarf2_cu *cu);
1584
1585 static htab_up allocate_signatured_type_table ();
1586
1587 static htab_up allocate_dwo_unit_table ();
1588
1589 static struct dwo_unit *lookup_dwo_unit_in_dwp
1590 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1591 struct dwp_file *dwp_file, const char *comp_dir,
1592 ULONGEST signature, int is_debug_types);
1593
1594 static struct dwp_file *get_dwp_file
1595 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1596
1597 static struct dwo_unit *lookup_dwo_comp_unit
1598 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
1599
1600 static struct dwo_unit *lookup_dwo_type_unit
1601 (struct signatured_type *, const char *, const char *);
1602
1603 static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1604
1605 /* A unique pointer to a dwo_file. */
1606
1607 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
1608
1609 static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
1610
1611 static void check_producer (struct dwarf2_cu *cu);
1612
1613 static void free_line_header_voidp (void *arg);
1614 \f
1615 /* Various complaints about symbol reading that don't abort the process. */
1616
1617 static void
1618 dwarf2_debug_line_missing_file_complaint (void)
1619 {
1620 complaint (_(".debug_line section has line data without a file"));
1621 }
1622
1623 static void
1624 dwarf2_debug_line_missing_end_sequence_complaint (void)
1625 {
1626 complaint (_(".debug_line section has line "
1627 "program sequence without an end"));
1628 }
1629
1630 static void
1631 dwarf2_complex_location_expr_complaint (void)
1632 {
1633 complaint (_("location expression too complex"));
1634 }
1635
1636 static void
1637 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1638 int arg3)
1639 {
1640 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
1641 arg1, arg2, arg3);
1642 }
1643
1644 static void
1645 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1646 {
1647 complaint (_("invalid attribute class or form for '%s' in '%s'"),
1648 arg1, arg2);
1649 }
1650
1651 /* Hash function for line_header_hash. */
1652
1653 static hashval_t
1654 line_header_hash (const struct line_header *ofs)
1655 {
1656 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
1657 }
1658
1659 /* Hash function for htab_create_alloc_ex for line_header_hash. */
1660
1661 static hashval_t
1662 line_header_hash_voidp (const void *item)
1663 {
1664 const struct line_header *ofs = (const struct line_header *) item;
1665
1666 return line_header_hash (ofs);
1667 }
1668
1669 /* Equality function for line_header_hash. */
1670
1671 static int
1672 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1673 {
1674 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1675 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
1676
1677 return (ofs_lhs->sect_off == ofs_rhs->sect_off
1678 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1679 }
1680
1681 \f
1682
1683 /* See declaration. */
1684
1685 dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
1686 const dwarf2_debug_sections *names,
1687 bool can_copy_)
1688 : objfile (objfile_),
1689 can_copy (can_copy_)
1690 {
1691 if (names == NULL)
1692 names = &dwarf2_elf_names;
1693
1694 bfd *obfd = objfile->obfd;
1695
1696 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1697 locate_sections (obfd, sec, *names);
1698 }
1699
1700 dwarf2_per_objfile::~dwarf2_per_objfile ()
1701 {
1702 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
1703 free_cached_comp_units ();
1704
1705 for (dwarf2_per_cu_data *per_cu : all_comp_units)
1706 per_cu->imported_symtabs_free ();
1707
1708 for (signatured_type *sig_type : all_type_units)
1709 sig_type->per_cu.imported_symtabs_free ();
1710
1711 /* Everything else should be on the objfile obstack. */
1712 }
1713
1714 /* See declaration. */
1715
1716 void
1717 dwarf2_per_objfile::free_cached_comp_units ()
1718 {
1719 dwarf2_per_cu_data *per_cu = read_in_chain;
1720 dwarf2_per_cu_data **last_chain = &read_in_chain;
1721 while (per_cu != NULL)
1722 {
1723 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
1724
1725 delete per_cu->cu;
1726 *last_chain = next_cu;
1727 per_cu = next_cu;
1728 }
1729 }
1730
1731 /* A helper class that calls free_cached_comp_units on
1732 destruction. */
1733
1734 class free_cached_comp_units
1735 {
1736 public:
1737
1738 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1739 : m_per_objfile (per_objfile)
1740 {
1741 }
1742
1743 ~free_cached_comp_units ()
1744 {
1745 m_per_objfile->free_cached_comp_units ();
1746 }
1747
1748 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1749
1750 private:
1751
1752 dwarf2_per_objfile *m_per_objfile;
1753 };
1754
1755 /* Try to locate the sections we need for DWARF 2 debugging
1756 information and return true if we have enough to do something.
1757 NAMES points to the dwarf2 section names, or is NULL if the standard
1758 ELF names are used. CAN_COPY is true for formats where symbol
1759 interposition is possible and so symbol values must follow copy
1760 relocation rules. */
1761
1762 int
1763 dwarf2_has_info (struct objfile *objfile,
1764 const struct dwarf2_debug_sections *names,
1765 bool can_copy)
1766 {
1767 if (objfile->flags & OBJF_READNEVER)
1768 return 0;
1769
1770 struct dwarf2_per_objfile *dwarf2_per_objfile
1771 = get_dwarf2_per_objfile (objfile);
1772
1773 if (dwarf2_per_objfile == NULL)
1774 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile,
1775 names,
1776 can_copy);
1777
1778 return (!dwarf2_per_objfile->info.is_virtual
1779 && dwarf2_per_objfile->info.s.section != NULL
1780 && !dwarf2_per_objfile->abbrev.is_virtual
1781 && dwarf2_per_objfile->abbrev.s.section != NULL);
1782 }
1783
1784 /* When loading sections, we look either for uncompressed section or for
1785 compressed section names. */
1786
1787 static int
1788 section_is_p (const char *section_name,
1789 const struct dwarf2_section_names *names)
1790 {
1791 if (names->normal != NULL
1792 && strcmp (section_name, names->normal) == 0)
1793 return 1;
1794 if (names->compressed != NULL
1795 && strcmp (section_name, names->compressed) == 0)
1796 return 1;
1797 return 0;
1798 }
1799
1800 /* See declaration. */
1801
1802 void
1803 dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
1804 const dwarf2_debug_sections &names)
1805 {
1806 flagword aflag = bfd_section_flags (sectp);
1807
1808 if ((aflag & SEC_HAS_CONTENTS) == 0)
1809 {
1810 }
1811 else if (elf_section_data (sectp)->this_hdr.sh_size
1812 > bfd_get_file_size (abfd))
1813 {
1814 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1815 warning (_("Discarding section %s which has a section size (%s"
1816 ") larger than the file size [in module %s]"),
1817 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1818 bfd_get_filename (abfd));
1819 }
1820 else if (section_is_p (sectp->name, &names.info))
1821 {
1822 this->info.s.section = sectp;
1823 this->info.size = bfd_section_size (sectp);
1824 }
1825 else if (section_is_p (sectp->name, &names.abbrev))
1826 {
1827 this->abbrev.s.section = sectp;
1828 this->abbrev.size = bfd_section_size (sectp);
1829 }
1830 else if (section_is_p (sectp->name, &names.line))
1831 {
1832 this->line.s.section = sectp;
1833 this->line.size = bfd_section_size (sectp);
1834 }
1835 else if (section_is_p (sectp->name, &names.loc))
1836 {
1837 this->loc.s.section = sectp;
1838 this->loc.size = bfd_section_size (sectp);
1839 }
1840 else if (section_is_p (sectp->name, &names.loclists))
1841 {
1842 this->loclists.s.section = sectp;
1843 this->loclists.size = bfd_section_size (sectp);
1844 }
1845 else if (section_is_p (sectp->name, &names.macinfo))
1846 {
1847 this->macinfo.s.section = sectp;
1848 this->macinfo.size = bfd_section_size (sectp);
1849 }
1850 else if (section_is_p (sectp->name, &names.macro))
1851 {
1852 this->macro.s.section = sectp;
1853 this->macro.size = bfd_section_size (sectp);
1854 }
1855 else if (section_is_p (sectp->name, &names.str))
1856 {
1857 this->str.s.section = sectp;
1858 this->str.size = bfd_section_size (sectp);
1859 }
1860 else if (section_is_p (sectp->name, &names.str_offsets))
1861 {
1862 this->str_offsets.s.section = sectp;
1863 this->str_offsets.size = bfd_section_size (sectp);
1864 }
1865 else if (section_is_p (sectp->name, &names.line_str))
1866 {
1867 this->line_str.s.section = sectp;
1868 this->line_str.size = bfd_section_size (sectp);
1869 }
1870 else if (section_is_p (sectp->name, &names.addr))
1871 {
1872 this->addr.s.section = sectp;
1873 this->addr.size = bfd_section_size (sectp);
1874 }
1875 else if (section_is_p (sectp->name, &names.frame))
1876 {
1877 this->frame.s.section = sectp;
1878 this->frame.size = bfd_section_size (sectp);
1879 }
1880 else if (section_is_p (sectp->name, &names.eh_frame))
1881 {
1882 this->eh_frame.s.section = sectp;
1883 this->eh_frame.size = bfd_section_size (sectp);
1884 }
1885 else if (section_is_p (sectp->name, &names.ranges))
1886 {
1887 this->ranges.s.section = sectp;
1888 this->ranges.size = bfd_section_size (sectp);
1889 }
1890 else if (section_is_p (sectp->name, &names.rnglists))
1891 {
1892 this->rnglists.s.section = sectp;
1893 this->rnglists.size = bfd_section_size (sectp);
1894 }
1895 else if (section_is_p (sectp->name, &names.types))
1896 {
1897 struct dwarf2_section_info type_section;
1898
1899 memset (&type_section, 0, sizeof (type_section));
1900 type_section.s.section = sectp;
1901 type_section.size = bfd_section_size (sectp);
1902
1903 this->types.push_back (type_section);
1904 }
1905 else if (section_is_p (sectp->name, &names.gdb_index))
1906 {
1907 this->gdb_index.s.section = sectp;
1908 this->gdb_index.size = bfd_section_size (sectp);
1909 }
1910 else if (section_is_p (sectp->name, &names.debug_names))
1911 {
1912 this->debug_names.s.section = sectp;
1913 this->debug_names.size = bfd_section_size (sectp);
1914 }
1915 else if (section_is_p (sectp->name, &names.debug_aranges))
1916 {
1917 this->debug_aranges.s.section = sectp;
1918 this->debug_aranges.size = bfd_section_size (sectp);
1919 }
1920
1921 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
1922 && bfd_section_vma (sectp) == 0)
1923 this->has_section_at_zero = true;
1924 }
1925
1926 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1927 SECTION_NAME. */
1928
1929 void
1930 dwarf2_get_section_info (struct objfile *objfile,
1931 enum dwarf2_section_enum sect,
1932 asection **sectp, const gdb_byte **bufp,
1933 bfd_size_type *sizep)
1934 {
1935 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
1936 struct dwarf2_section_info *info;
1937
1938 /* We may see an objfile without any DWARF, in which case we just
1939 return nothing. */
1940 if (data == NULL)
1941 {
1942 *sectp = NULL;
1943 *bufp = NULL;
1944 *sizep = 0;
1945 return;
1946 }
1947 switch (sect)
1948 {
1949 case DWARF2_DEBUG_FRAME:
1950 info = &data->frame;
1951 break;
1952 case DWARF2_EH_FRAME:
1953 info = &data->eh_frame;
1954 break;
1955 default:
1956 gdb_assert_not_reached ("unexpected section");
1957 }
1958
1959 info->read (objfile);
1960
1961 *sectp = info->get_bfd_section ();
1962 *bufp = info->buffer;
1963 *sizep = info->size;
1964 }
1965
1966 /* A helper function to find the sections for a .dwz file. */
1967
1968 static void
1969 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
1970 {
1971 struct dwz_file *dwz_file = (struct dwz_file *) arg;
1972
1973 /* Note that we only support the standard ELF names, because .dwz
1974 is ELF-only (at the time of writing). */
1975 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
1976 {
1977 dwz_file->abbrev.s.section = sectp;
1978 dwz_file->abbrev.size = bfd_section_size (sectp);
1979 }
1980 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
1981 {
1982 dwz_file->info.s.section = sectp;
1983 dwz_file->info.size = bfd_section_size (sectp);
1984 }
1985 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
1986 {
1987 dwz_file->str.s.section = sectp;
1988 dwz_file->str.size = bfd_section_size (sectp);
1989 }
1990 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
1991 {
1992 dwz_file->line.s.section = sectp;
1993 dwz_file->line.size = bfd_section_size (sectp);
1994 }
1995 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
1996 {
1997 dwz_file->macro.s.section = sectp;
1998 dwz_file->macro.size = bfd_section_size (sectp);
1999 }
2000 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2001 {
2002 dwz_file->gdb_index.s.section = sectp;
2003 dwz_file->gdb_index.size = bfd_section_size (sectp);
2004 }
2005 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2006 {
2007 dwz_file->debug_names.s.section = sectp;
2008 dwz_file->debug_names.size = bfd_section_size (sectp);
2009 }
2010 }
2011
2012 /* See dwarf2read.h. */
2013
2014 struct dwz_file *
2015 dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
2016 {
2017 const char *filename;
2018 bfd_size_type buildid_len_arg;
2019 size_t buildid_len;
2020 bfd_byte *buildid;
2021
2022 if (dwarf2_per_objfile->dwz_file != NULL)
2023 return dwarf2_per_objfile->dwz_file.get ();
2024
2025 bfd_set_error (bfd_error_no_error);
2026 gdb::unique_xmalloc_ptr<char> data
2027 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2028 &buildid_len_arg, &buildid));
2029 if (data == NULL)
2030 {
2031 if (bfd_get_error () == bfd_error_no_error)
2032 return NULL;
2033 error (_("could not read '.gnu_debugaltlink' section: %s"),
2034 bfd_errmsg (bfd_get_error ()));
2035 }
2036
2037 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
2038
2039 buildid_len = (size_t) buildid_len_arg;
2040
2041 filename = data.get ();
2042
2043 std::string abs_storage;
2044 if (!IS_ABSOLUTE_PATH (filename))
2045 {
2046 gdb::unique_xmalloc_ptr<char> abs
2047 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
2048
2049 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
2050 filename = abs_storage.c_str ();
2051 }
2052
2053 /* First try the file name given in the section. If that doesn't
2054 work, try to use the build-id instead. */
2055 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
2056 if (dwz_bfd != NULL)
2057 {
2058 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2059 dwz_bfd.reset (nullptr);
2060 }
2061
2062 if (dwz_bfd == NULL)
2063 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2064
2065 if (dwz_bfd == nullptr)
2066 {
2067 gdb::unique_xmalloc_ptr<char> alt_filename;
2068 const char *origname = dwarf2_per_objfile->objfile->original_name;
2069
2070 scoped_fd fd (debuginfod_debuginfo_query (buildid,
2071 buildid_len,
2072 origname,
2073 &alt_filename));
2074
2075 if (fd.get () >= 0)
2076 {
2077 /* File successfully retrieved from server. */
2078 dwz_bfd = gdb_bfd_open (alt_filename.get (), gnutarget, -1);
2079
2080 if (dwz_bfd == nullptr)
2081 warning (_("File \"%s\" from debuginfod cannot be opened as bfd"),
2082 alt_filename.get ());
2083 else if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2084 dwz_bfd.reset (nullptr);
2085 }
2086 }
2087
2088 if (dwz_bfd == NULL)
2089 error (_("could not find '.gnu_debugaltlink' file for %s"),
2090 objfile_name (dwarf2_per_objfile->objfile));
2091
2092 std::unique_ptr<struct dwz_file> result
2093 (new struct dwz_file (std::move (dwz_bfd)));
2094
2095 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2096 result.get ());
2097
2098 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd,
2099 result->dwz_bfd.get ());
2100 dwarf2_per_objfile->dwz_file = std::move (result);
2101 return dwarf2_per_objfile->dwz_file.get ();
2102 }
2103 \f
2104 /* DWARF quick_symbols_functions support. */
2105
2106 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2107 unique line tables, so we maintain a separate table of all .debug_line
2108 derived entries to support the sharing.
2109 All the quick functions need is the list of file names. We discard the
2110 line_header when we're done and don't need to record it here. */
2111 struct quick_file_names
2112 {
2113 /* The data used to construct the hash key. */
2114 struct stmt_list_hash hash;
2115
2116 /* The number of entries in file_names, real_names. */
2117 unsigned int num_file_names;
2118
2119 /* The file names from the line table, after being run through
2120 file_full_name. */
2121 const char **file_names;
2122
2123 /* The file names from the line table after being run through
2124 gdb_realpath. These are computed lazily. */
2125 const char **real_names;
2126 };
2127
2128 /* When using the index (and thus not using psymtabs), each CU has an
2129 object of this type. This is used to hold information needed by
2130 the various "quick" methods. */
2131 struct dwarf2_per_cu_quick_data
2132 {
2133 /* The file table. This can be NULL if there was no file table
2134 or it's currently not read in.
2135 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2136 struct quick_file_names *file_names;
2137
2138 /* The corresponding symbol table. This is NULL if symbols for this
2139 CU have not yet been read. */
2140 struct compunit_symtab *compunit_symtab;
2141
2142 /* A temporary mark bit used when iterating over all CUs in
2143 expand_symtabs_matching. */
2144 unsigned int mark : 1;
2145
2146 /* True if we've tried to read the file table and found there isn't one.
2147 There will be no point in trying to read it again next time. */
2148 unsigned int no_file_data : 1;
2149 };
2150
2151 /* Utility hash function for a stmt_list_hash. */
2152
2153 static hashval_t
2154 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2155 {
2156 hashval_t v = 0;
2157
2158 if (stmt_list_hash->dwo_unit != NULL)
2159 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2160 v += to_underlying (stmt_list_hash->line_sect_off);
2161 return v;
2162 }
2163
2164 /* Utility equality function for a stmt_list_hash. */
2165
2166 static int
2167 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2168 const struct stmt_list_hash *rhs)
2169 {
2170 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2171 return 0;
2172 if (lhs->dwo_unit != NULL
2173 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2174 return 0;
2175
2176 return lhs->line_sect_off == rhs->line_sect_off;
2177 }
2178
2179 /* Hash function for a quick_file_names. */
2180
2181 static hashval_t
2182 hash_file_name_entry (const void *e)
2183 {
2184 const struct quick_file_names *file_data
2185 = (const struct quick_file_names *) e;
2186
2187 return hash_stmt_list_entry (&file_data->hash);
2188 }
2189
2190 /* Equality function for a quick_file_names. */
2191
2192 static int
2193 eq_file_name_entry (const void *a, const void *b)
2194 {
2195 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2196 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2197
2198 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2199 }
2200
2201 /* Delete function for a quick_file_names. */
2202
2203 static void
2204 delete_file_name_entry (void *e)
2205 {
2206 struct quick_file_names *file_data = (struct quick_file_names *) e;
2207 int i;
2208
2209 for (i = 0; i < file_data->num_file_names; ++i)
2210 {
2211 xfree ((void*) file_data->file_names[i]);
2212 if (file_data->real_names)
2213 xfree ((void*) file_data->real_names[i]);
2214 }
2215
2216 /* The space for the struct itself lives on objfile_obstack,
2217 so we don't free it here. */
2218 }
2219
2220 /* Create a quick_file_names hash table. */
2221
2222 static htab_up
2223 create_quick_file_names_table (unsigned int nr_initial_entries)
2224 {
2225 return htab_up (htab_create_alloc (nr_initial_entries,
2226 hash_file_name_entry, eq_file_name_entry,
2227 delete_file_name_entry, xcalloc, xfree));
2228 }
2229
2230 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2231 have to be created afterwards. You should call age_cached_comp_units after
2232 processing PER_CU->CU. dw2_setup must have been already called. */
2233
2234 static void
2235 load_cu (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2236 {
2237 if (per_cu->is_debug_types)
2238 load_full_type_unit (per_cu);
2239 else
2240 load_full_comp_unit (per_cu, skip_partial, language_minimal);
2241
2242 if (per_cu->cu == NULL)
2243 return; /* Dummy CU. */
2244
2245 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2246 }
2247
2248 /* Read in the symbols for PER_CU. */
2249
2250 static void
2251 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2252 {
2253 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2254
2255 /* Skip type_unit_groups, reading the type units they contain
2256 is handled elsewhere. */
2257 if (per_cu->type_unit_group_p ())
2258 return;
2259
2260 /* The destructor of dwarf2_queue_guard frees any entries left on
2261 the queue. After this point we're guaranteed to leave this function
2262 with the dwarf queue empty. */
2263 dwarf2_queue_guard q_guard (dwarf2_per_objfile);
2264
2265 if (dwarf2_per_objfile->using_index
2266 ? per_cu->v.quick->compunit_symtab == NULL
2267 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2268 {
2269 queue_comp_unit (per_cu, language_minimal);
2270 load_cu (per_cu, skip_partial);
2271
2272 /* If we just loaded a CU from a DWO, and we're working with an index
2273 that may badly handle TUs, load all the TUs in that DWO as well.
2274 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2275 if (!per_cu->is_debug_types
2276 && per_cu->cu != NULL
2277 && per_cu->cu->dwo_unit != NULL
2278 && dwarf2_per_objfile->index_table != NULL
2279 && dwarf2_per_objfile->index_table->version <= 7
2280 /* DWP files aren't supported yet. */
2281 && get_dwp_file (dwarf2_per_objfile) == NULL)
2282 queue_and_load_all_dwo_tus (per_cu);
2283 }
2284
2285 process_queue (dwarf2_per_objfile);
2286
2287 /* Age the cache, releasing compilation units that have not
2288 been used recently. */
2289 age_cached_comp_units (dwarf2_per_objfile);
2290 }
2291
2292 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2293 the objfile from which this CU came. Returns the resulting symbol
2294 table. */
2295
2296 static struct compunit_symtab *
2297 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2298 {
2299 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2300
2301 gdb_assert (dwarf2_per_objfile->using_index);
2302 if (!per_cu->v.quick->compunit_symtab)
2303 {
2304 free_cached_comp_units freer (dwarf2_per_objfile);
2305 scoped_restore decrementer = increment_reading_symtab ();
2306 dw2_do_instantiate_symtab (per_cu, skip_partial);
2307 process_cu_includes (dwarf2_per_objfile);
2308 }
2309
2310 return per_cu->v.quick->compunit_symtab;
2311 }
2312
2313 /* See declaration. */
2314
2315 dwarf2_per_cu_data *
2316 dwarf2_per_objfile::get_cutu (int index)
2317 {
2318 if (index >= this->all_comp_units.size ())
2319 {
2320 index -= this->all_comp_units.size ();
2321 gdb_assert (index < this->all_type_units.size ());
2322 return &this->all_type_units[index]->per_cu;
2323 }
2324
2325 return this->all_comp_units[index];
2326 }
2327
2328 /* See declaration. */
2329
2330 dwarf2_per_cu_data *
2331 dwarf2_per_objfile::get_cu (int index)
2332 {
2333 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
2334
2335 return this->all_comp_units[index];
2336 }
2337
2338 /* See declaration. */
2339
2340 signatured_type *
2341 dwarf2_per_objfile::get_tu (int index)
2342 {
2343 gdb_assert (index >= 0 && index < this->all_type_units.size ());
2344
2345 return this->all_type_units[index];
2346 }
2347
2348 /* Return a new dwarf2_per_cu_data allocated on OBJFILE's
2349 objfile_obstack, and constructed with the specified field
2350 values. */
2351
2352 static dwarf2_per_cu_data *
2353 create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2354 struct dwarf2_section_info *section,
2355 int is_dwz,
2356 sect_offset sect_off, ULONGEST length)
2357 {
2358 struct objfile *objfile = dwarf2_per_objfile->objfile;
2359 dwarf2_per_cu_data *the_cu
2360 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2361 struct dwarf2_per_cu_data);
2362 the_cu->sect_off = sect_off;
2363 the_cu->length = length;
2364 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
2365 the_cu->section = section;
2366 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2367 struct dwarf2_per_cu_quick_data);
2368 the_cu->is_dwz = is_dwz;
2369 return the_cu;
2370 }
2371
2372 /* A helper for create_cus_from_index that handles a given list of
2373 CUs. */
2374
2375 static void
2376 create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2377 const gdb_byte *cu_list, offset_type n_elements,
2378 struct dwarf2_section_info *section,
2379 int is_dwz)
2380 {
2381 for (offset_type i = 0; i < n_elements; i += 2)
2382 {
2383 gdb_static_assert (sizeof (ULONGEST) >= 8);
2384
2385 sect_offset sect_off
2386 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2387 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2388 cu_list += 2 * 8;
2389
2390 dwarf2_per_cu_data *per_cu
2391 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
2392 sect_off, length);
2393 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
2394 }
2395 }
2396
2397 /* Read the CU list from the mapped index, and use it to create all
2398 the CU objects for this objfile. */
2399
2400 static void
2401 create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2402 const gdb_byte *cu_list, offset_type cu_list_elements,
2403 const gdb_byte *dwz_list, offset_type dwz_elements)
2404 {
2405 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
2406 dwarf2_per_objfile->all_comp_units.reserve
2407 ((cu_list_elements + dwz_elements) / 2);
2408
2409 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
2410 &dwarf2_per_objfile->info, 0);
2411
2412 if (dwz_elements == 0)
2413 return;
2414
2415 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
2416 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
2417 &dwz->info, 1);
2418 }
2419
2420 /* Create the signatured type hash table from the index. */
2421
2422 static void
2423 create_signatured_type_table_from_index
2424 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2425 struct dwarf2_section_info *section,
2426 const gdb_byte *bytes,
2427 offset_type elements)
2428 {
2429 struct objfile *objfile = dwarf2_per_objfile->objfile;
2430
2431 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
2432 dwarf2_per_objfile->all_type_units.reserve (elements / 3);
2433
2434 htab_up sig_types_hash = allocate_signatured_type_table ();
2435
2436 for (offset_type i = 0; i < elements; i += 3)
2437 {
2438 struct signatured_type *sig_type;
2439 ULONGEST signature;
2440 void **slot;
2441 cu_offset type_offset_in_tu;
2442
2443 gdb_static_assert (sizeof (ULONGEST) >= 8);
2444 sect_offset sect_off
2445 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2446 type_offset_in_tu
2447 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2448 BFD_ENDIAN_LITTLE);
2449 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2450 bytes += 3 * 8;
2451
2452 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2453 struct signatured_type);
2454 sig_type->signature = signature;
2455 sig_type->type_offset_in_tu = type_offset_in_tu;
2456 sig_type->per_cu.is_debug_types = 1;
2457 sig_type->per_cu.section = section;
2458 sig_type->per_cu.sect_off = sect_off;
2459 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
2460 sig_type->per_cu.v.quick
2461 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2462 struct dwarf2_per_cu_quick_data);
2463
2464 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2465 *slot = sig_type;
2466
2467 dwarf2_per_objfile->all_type_units.push_back (sig_type);
2468 }
2469
2470 dwarf2_per_objfile->signatured_types = std::move (sig_types_hash);
2471 }
2472
2473 /* Create the signatured type hash table from .debug_names. */
2474
2475 static void
2476 create_signatured_type_table_from_debug_names
2477 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2478 const mapped_debug_names &map,
2479 struct dwarf2_section_info *section,
2480 struct dwarf2_section_info *abbrev_section)
2481 {
2482 struct objfile *objfile = dwarf2_per_objfile->objfile;
2483
2484 section->read (objfile);
2485 abbrev_section->read (objfile);
2486
2487 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
2488 dwarf2_per_objfile->all_type_units.reserve (map.tu_count);
2489
2490 htab_up sig_types_hash = allocate_signatured_type_table ();
2491
2492 for (uint32_t i = 0; i < map.tu_count; ++i)
2493 {
2494 struct signatured_type *sig_type;
2495 void **slot;
2496
2497 sect_offset sect_off
2498 = (sect_offset) (extract_unsigned_integer
2499 (map.tu_table_reordered + i * map.offset_size,
2500 map.offset_size,
2501 map.dwarf5_byte_order));
2502
2503 comp_unit_head cu_header;
2504 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
2505 abbrev_section,
2506 section->buffer + to_underlying (sect_off),
2507 rcuh_kind::TYPE);
2508
2509 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2510 struct signatured_type);
2511 sig_type->signature = cu_header.signature;
2512 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2513 sig_type->per_cu.is_debug_types = 1;
2514 sig_type->per_cu.section = section;
2515 sig_type->per_cu.sect_off = sect_off;
2516 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
2517 sig_type->per_cu.v.quick
2518 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2519 struct dwarf2_per_cu_quick_data);
2520
2521 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2522 *slot = sig_type;
2523
2524 dwarf2_per_objfile->all_type_units.push_back (sig_type);
2525 }
2526
2527 dwarf2_per_objfile->signatured_types = std::move (sig_types_hash);
2528 }
2529
2530 /* Read the address map data from the mapped index, and use it to
2531 populate the objfile's psymtabs_addrmap. */
2532
2533 static void
2534 create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2535 struct mapped_index *index)
2536 {
2537 struct objfile *objfile = dwarf2_per_objfile->objfile;
2538 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2539 const gdb_byte *iter, *end;
2540 struct addrmap *mutable_map;
2541 CORE_ADDR baseaddr;
2542
2543 auto_obstack temp_obstack;
2544
2545 mutable_map = addrmap_create_mutable (&temp_obstack);
2546
2547 iter = index->address_table.data ();
2548 end = iter + index->address_table.size ();
2549
2550 baseaddr = objfile->text_section_offset ();
2551
2552 while (iter < end)
2553 {
2554 ULONGEST hi, lo, cu_index;
2555 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2556 iter += 8;
2557 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2558 iter += 8;
2559 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2560 iter += 4;
2561
2562 if (lo > hi)
2563 {
2564 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
2565 hex_string (lo), hex_string (hi));
2566 continue;
2567 }
2568
2569 if (cu_index >= dwarf2_per_objfile->all_comp_units.size ())
2570 {
2571 complaint (_(".gdb_index address table has invalid CU number %u"),
2572 (unsigned) cu_index);
2573 continue;
2574 }
2575
2576 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2577 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
2578 addrmap_set_empty (mutable_map, lo, hi - 1,
2579 dwarf2_per_objfile->get_cu (cu_index));
2580 }
2581
2582 objfile->partial_symtabs->psymtabs_addrmap
2583 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2584 }
2585
2586 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
2587 populate the objfile's psymtabs_addrmap. */
2588
2589 static void
2590 create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
2591 struct dwarf2_section_info *section)
2592 {
2593 struct objfile *objfile = dwarf2_per_objfile->objfile;
2594 bfd *abfd = objfile->obfd;
2595 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2596 const CORE_ADDR baseaddr = objfile->text_section_offset ();
2597
2598 auto_obstack temp_obstack;
2599 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2600
2601 std::unordered_map<sect_offset,
2602 dwarf2_per_cu_data *,
2603 gdb::hash_enum<sect_offset>>
2604 debug_info_offset_to_per_cu;
2605 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
2606 {
2607 const auto insertpair
2608 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2609 if (!insertpair.second)
2610 {
2611 warning (_("Section .debug_aranges in %s has duplicate "
2612 "debug_info_offset %s, ignoring .debug_aranges."),
2613 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
2614 return;
2615 }
2616 }
2617
2618 section->read (objfile);
2619
2620 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2621
2622 const gdb_byte *addr = section->buffer;
2623
2624 while (addr < section->buffer + section->size)
2625 {
2626 const gdb_byte *const entry_addr = addr;
2627 unsigned int bytes_read;
2628
2629 const LONGEST entry_length = read_initial_length (abfd, addr,
2630 &bytes_read);
2631 addr += bytes_read;
2632
2633 const gdb_byte *const entry_end = addr + entry_length;
2634 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2635 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2636 if (addr + entry_length > section->buffer + section->size)
2637 {
2638 warning (_("Section .debug_aranges in %s entry at offset %s "
2639 "length %s exceeds section length %s, "
2640 "ignoring .debug_aranges."),
2641 objfile_name (objfile),
2642 plongest (entry_addr - section->buffer),
2643 plongest (bytes_read + entry_length),
2644 pulongest (section->size));
2645 return;
2646 }
2647
2648 /* The version number. */
2649 const uint16_t version = read_2_bytes (abfd, addr);
2650 addr += 2;
2651 if (version != 2)
2652 {
2653 warning (_("Section .debug_aranges in %s entry at offset %s "
2654 "has unsupported version %d, ignoring .debug_aranges."),
2655 objfile_name (objfile),
2656 plongest (entry_addr - section->buffer), version);
2657 return;
2658 }
2659
2660 const uint64_t debug_info_offset
2661 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2662 addr += offset_size;
2663 const auto per_cu_it
2664 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2665 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2666 {
2667 warning (_("Section .debug_aranges in %s entry at offset %s "
2668 "debug_info_offset %s does not exists, "
2669 "ignoring .debug_aranges."),
2670 objfile_name (objfile),
2671 plongest (entry_addr - section->buffer),
2672 pulongest (debug_info_offset));
2673 return;
2674 }
2675 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2676
2677 const uint8_t address_size = *addr++;
2678 if (address_size < 1 || address_size > 8)
2679 {
2680 warning (_("Section .debug_aranges in %s entry at offset %s "
2681 "address_size %u is invalid, ignoring .debug_aranges."),
2682 objfile_name (objfile),
2683 plongest (entry_addr - section->buffer), address_size);
2684 return;
2685 }
2686
2687 const uint8_t segment_selector_size = *addr++;
2688 if (segment_selector_size != 0)
2689 {
2690 warning (_("Section .debug_aranges in %s entry at offset %s "
2691 "segment_selector_size %u is not supported, "
2692 "ignoring .debug_aranges."),
2693 objfile_name (objfile),
2694 plongest (entry_addr - section->buffer),
2695 segment_selector_size);
2696 return;
2697 }
2698
2699 /* Must pad to an alignment boundary that is twice the address
2700 size. It is undocumented by the DWARF standard but GCC does
2701 use it. */
2702 for (size_t padding = ((-(addr - section->buffer))
2703 & (2 * address_size - 1));
2704 padding > 0; padding--)
2705 if (*addr++ != 0)
2706 {
2707 warning (_("Section .debug_aranges in %s entry at offset %s "
2708 "padding is not zero, ignoring .debug_aranges."),
2709 objfile_name (objfile),
2710 plongest (entry_addr - section->buffer));
2711 return;
2712 }
2713
2714 for (;;)
2715 {
2716 if (addr + 2 * address_size > entry_end)
2717 {
2718 warning (_("Section .debug_aranges in %s entry at offset %s "
2719 "address list is not properly terminated, "
2720 "ignoring .debug_aranges."),
2721 objfile_name (objfile),
2722 plongest (entry_addr - section->buffer));
2723 return;
2724 }
2725 ULONGEST start = extract_unsigned_integer (addr, address_size,
2726 dwarf5_byte_order);
2727 addr += address_size;
2728 ULONGEST length = extract_unsigned_integer (addr, address_size,
2729 dwarf5_byte_order);
2730 addr += address_size;
2731 if (start == 0 && length == 0)
2732 break;
2733 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
2734 {
2735 /* Symbol was eliminated due to a COMDAT group. */
2736 continue;
2737 }
2738 ULONGEST end = start + length;
2739 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2740 - baseaddr);
2741 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2742 - baseaddr);
2743 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2744 }
2745 }
2746
2747 objfile->partial_symtabs->psymtabs_addrmap
2748 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2749 }
2750
2751 /* Find a slot in the mapped index INDEX for the object named NAME.
2752 If NAME is found, set *VEC_OUT to point to the CU vector in the
2753 constant pool and return true. If NAME cannot be found, return
2754 false. */
2755
2756 static bool
2757 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2758 offset_type **vec_out)
2759 {
2760 offset_type hash;
2761 offset_type slot, step;
2762 int (*cmp) (const char *, const char *);
2763
2764 gdb::unique_xmalloc_ptr<char> without_params;
2765 if (current_language->la_language == language_cplus
2766 || current_language->la_language == language_fortran
2767 || current_language->la_language == language_d)
2768 {
2769 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2770 not contain any. */
2771
2772 if (strchr (name, '(') != NULL)
2773 {
2774 without_params = cp_remove_params (name);
2775
2776 if (without_params != NULL)
2777 name = without_params.get ();
2778 }
2779 }
2780
2781 /* Index version 4 did not support case insensitive searches. But the
2782 indices for case insensitive languages are built in lowercase, therefore
2783 simulate our NAME being searched is also lowercased. */
2784 hash = mapped_index_string_hash ((index->version == 4
2785 && case_sensitivity == case_sensitive_off
2786 ? 5 : index->version),
2787 name);
2788
2789 slot = hash & (index->symbol_table.size () - 1);
2790 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
2791 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2792
2793 for (;;)
2794 {
2795 const char *str;
2796
2797 const auto &bucket = index->symbol_table[slot];
2798 if (bucket.name == 0 && bucket.vec == 0)
2799 return false;
2800
2801 str = index->constant_pool + MAYBE_SWAP (bucket.name);
2802 if (!cmp (name, str))
2803 {
2804 *vec_out = (offset_type *) (index->constant_pool
2805 + MAYBE_SWAP (bucket.vec));
2806 return true;
2807 }
2808
2809 slot = (slot + step) & (index->symbol_table.size () - 1);
2810 }
2811 }
2812
2813 /* A helper function that reads the .gdb_index from BUFFER and fills
2814 in MAP. FILENAME is the name of the file containing the data;
2815 it is used for error reporting. DEPRECATED_OK is true if it is
2816 ok to use deprecated sections.
2817
2818 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2819 out parameters that are filled in with information about the CU and
2820 TU lists in the section.
2821
2822 Returns true if all went well, false otherwise. */
2823
2824 static bool
2825 read_gdb_index_from_buffer (struct objfile *objfile,
2826 const char *filename,
2827 bool deprecated_ok,
2828 gdb::array_view<const gdb_byte> buffer,
2829 struct mapped_index *map,
2830 const gdb_byte **cu_list,
2831 offset_type *cu_list_elements,
2832 const gdb_byte **types_list,
2833 offset_type *types_list_elements)
2834 {
2835 const gdb_byte *addr = &buffer[0];
2836
2837 /* Version check. */
2838 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
2839 /* Versions earlier than 3 emitted every copy of a psymbol. This
2840 causes the index to behave very poorly for certain requests. Version 3
2841 contained incomplete addrmap. So, it seems better to just ignore such
2842 indices. */
2843 if (version < 4)
2844 {
2845 static int warning_printed = 0;
2846 if (!warning_printed)
2847 {
2848 warning (_("Skipping obsolete .gdb_index section in %s."),
2849 filename);
2850 warning_printed = 1;
2851 }
2852 return 0;
2853 }
2854 /* Index version 4 uses a different hash function than index version
2855 5 and later.
2856
2857 Versions earlier than 6 did not emit psymbols for inlined
2858 functions. Using these files will cause GDB not to be able to
2859 set breakpoints on inlined functions by name, so we ignore these
2860 indices unless the user has done
2861 "set use-deprecated-index-sections on". */
2862 if (version < 6 && !deprecated_ok)
2863 {
2864 static int warning_printed = 0;
2865 if (!warning_printed)
2866 {
2867 warning (_("\
2868 Skipping deprecated .gdb_index section in %s.\n\
2869 Do \"set use-deprecated-index-sections on\" before the file is read\n\
2870 to use the section anyway."),
2871 filename);
2872 warning_printed = 1;
2873 }
2874 return 0;
2875 }
2876 /* Version 7 indices generated by gold refer to the CU for a symbol instead
2877 of the TU (for symbols coming from TUs),
2878 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
2879 Plus gold-generated indices can have duplicate entries for global symbols,
2880 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
2881 These are just performance bugs, and we can't distinguish gdb-generated
2882 indices from gold-generated ones, so issue no warning here. */
2883
2884 /* Indexes with higher version than the one supported by GDB may be no
2885 longer backward compatible. */
2886 if (version > 8)
2887 return 0;
2888
2889 map->version = version;
2890
2891 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
2892
2893 int i = 0;
2894 *cu_list = addr + MAYBE_SWAP (metadata[i]);
2895 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2896 / 8);
2897 ++i;
2898
2899 *types_list = addr + MAYBE_SWAP (metadata[i]);
2900 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2901 - MAYBE_SWAP (metadata[i]))
2902 / 8);
2903 ++i;
2904
2905 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
2906 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
2907 map->address_table
2908 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
2909 ++i;
2910
2911 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
2912 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
2913 map->symbol_table
2914 = gdb::array_view<mapped_index::symbol_table_slot>
2915 ((mapped_index::symbol_table_slot *) symbol_table,
2916 (mapped_index::symbol_table_slot *) symbol_table_end);
2917
2918 ++i;
2919 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
2920
2921 return 1;
2922 }
2923
2924 /* Callback types for dwarf2_read_gdb_index. */
2925
2926 typedef gdb::function_view
2927 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_objfile *)>
2928 get_gdb_index_contents_ftype;
2929 typedef gdb::function_view
2930 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
2931 get_gdb_index_contents_dwz_ftype;
2932
2933 /* Read .gdb_index. If everything went ok, initialize the "quick"
2934 elements of all the CUs and return 1. Otherwise, return 0. */
2935
2936 static int
2937 dwarf2_read_gdb_index
2938 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2939 get_gdb_index_contents_ftype get_gdb_index_contents,
2940 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
2941 {
2942 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
2943 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
2944 struct dwz_file *dwz;
2945 struct objfile *objfile = dwarf2_per_objfile->objfile;
2946
2947 gdb::array_view<const gdb_byte> main_index_contents
2948 = get_gdb_index_contents (objfile, dwarf2_per_objfile);
2949
2950 if (main_index_contents.empty ())
2951 return 0;
2952
2953 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
2954 if (!read_gdb_index_from_buffer (objfile, objfile_name (objfile),
2955 use_deprecated_index_sections,
2956 main_index_contents, map.get (), &cu_list,
2957 &cu_list_elements, &types_list,
2958 &types_list_elements))
2959 return 0;
2960
2961 /* Don't use the index if it's empty. */
2962 if (map->symbol_table.empty ())
2963 return 0;
2964
2965 /* If there is a .dwz file, read it so we can get its CU list as
2966 well. */
2967 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
2968 if (dwz != NULL)
2969 {
2970 struct mapped_index dwz_map;
2971 const gdb_byte *dwz_types_ignore;
2972 offset_type dwz_types_elements_ignore;
2973
2974 gdb::array_view<const gdb_byte> dwz_index_content
2975 = get_gdb_index_contents_dwz (objfile, dwz);
2976
2977 if (dwz_index_content.empty ())
2978 return 0;
2979
2980 if (!read_gdb_index_from_buffer (objfile,
2981 bfd_get_filename (dwz->dwz_bfd.get ()),
2982 1, dwz_index_content, &dwz_map,
2983 &dwz_list, &dwz_list_elements,
2984 &dwz_types_ignore,
2985 &dwz_types_elements_ignore))
2986 {
2987 warning (_("could not read '.gdb_index' section from %s; skipping"),
2988 bfd_get_filename (dwz->dwz_bfd.get ()));
2989 return 0;
2990 }
2991 }
2992
2993 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
2994 dwz_list, dwz_list_elements);
2995
2996 if (types_list_elements)
2997 {
2998 /* We can only handle a single .debug_types when we have an
2999 index. */
3000 if (dwarf2_per_objfile->types.size () != 1)
3001 return 0;
3002
3003 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
3004
3005 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3006 types_list, types_list_elements);
3007 }
3008
3009 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
3010
3011 dwarf2_per_objfile->index_table = std::move (map);
3012 dwarf2_per_objfile->using_index = 1;
3013 dwarf2_per_objfile->quick_file_names_table =
3014 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
3015
3016 return 1;
3017 }
3018
3019 /* die_reader_func for dw2_get_file_names. */
3020
3021 static void
3022 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3023 const gdb_byte *info_ptr,
3024 struct die_info *comp_unit_die)
3025 {
3026 struct dwarf2_cu *cu = reader->cu;
3027 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3028 struct dwarf2_per_objfile *dwarf2_per_objfile
3029 = cu->per_cu->dwarf2_per_objfile;
3030 struct objfile *objfile = dwarf2_per_objfile->objfile;
3031 struct dwarf2_per_cu_data *lh_cu;
3032 struct attribute *attr;
3033 void **slot;
3034 struct quick_file_names *qfn;
3035
3036 gdb_assert (! this_cu->is_debug_types);
3037
3038 /* Our callers never want to match partial units -- instead they
3039 will match the enclosing full CU. */
3040 if (comp_unit_die->tag == DW_TAG_partial_unit)
3041 {
3042 this_cu->v.quick->no_file_data = 1;
3043 return;
3044 }
3045
3046 lh_cu = this_cu;
3047 slot = NULL;
3048
3049 line_header_up lh;
3050 sect_offset line_offset {};
3051
3052 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3053 if (attr != nullptr)
3054 {
3055 struct quick_file_names find_entry;
3056
3057 line_offset = (sect_offset) DW_UNSND (attr);
3058
3059 /* We may have already read in this line header (TU line header sharing).
3060 If we have we're done. */
3061 find_entry.hash.dwo_unit = cu->dwo_unit;
3062 find_entry.hash.line_sect_off = line_offset;
3063 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table.get (),
3064 &find_entry, INSERT);
3065 if (*slot != NULL)
3066 {
3067 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3068 return;
3069 }
3070
3071 lh = dwarf_decode_line_header (line_offset, cu);
3072 }
3073 if (lh == NULL)
3074 {
3075 lh_cu->v.quick->no_file_data = 1;
3076 return;
3077 }
3078
3079 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
3080 qfn->hash.dwo_unit = cu->dwo_unit;
3081 qfn->hash.line_sect_off = line_offset;
3082 gdb_assert (slot != NULL);
3083 *slot = qfn;
3084
3085 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3086
3087 int offset = 0;
3088 if (strcmp (fnd.name, "<unknown>") != 0)
3089 ++offset;
3090
3091 qfn->num_file_names = offset + lh->file_names_size ();
3092 qfn->file_names =
3093 XOBNEWVEC (&objfile->objfile_obstack, const char *, qfn->num_file_names);
3094 if (offset != 0)
3095 qfn->file_names[0] = xstrdup (fnd.name);
3096 for (int i = 0; i < lh->file_names_size (); ++i)
3097 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3098 fnd.comp_dir).release ();
3099 qfn->real_names = NULL;
3100
3101 lh_cu->v.quick->file_names = qfn;
3102 }
3103
3104 /* A helper for the "quick" functions which attempts to read the line
3105 table for THIS_CU. */
3106
3107 static struct quick_file_names *
3108 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
3109 {
3110 /* This should never be called for TUs. */
3111 gdb_assert (! this_cu->is_debug_types);
3112 /* Nor type unit groups. */
3113 gdb_assert (! this_cu->type_unit_group_p ());
3114
3115 if (this_cu->v.quick->file_names != NULL)
3116 return this_cu->v.quick->file_names;
3117 /* If we know there is no line data, no point in looking again. */
3118 if (this_cu->v.quick->no_file_data)
3119 return NULL;
3120
3121 cutu_reader reader (this_cu);
3122 if (!reader.dummy_p)
3123 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die);
3124
3125 if (this_cu->v.quick->no_file_data)
3126 return NULL;
3127 return this_cu->v.quick->file_names;
3128 }
3129
3130 /* A helper for the "quick" functions which computes and caches the
3131 real path for a given file name from the line table. */
3132
3133 static const char *
3134 dw2_get_real_path (struct objfile *objfile,
3135 struct quick_file_names *qfn, int index)
3136 {
3137 if (qfn->real_names == NULL)
3138 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
3139 qfn->num_file_names, const char *);
3140
3141 if (qfn->real_names[index] == NULL)
3142 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3143
3144 return qfn->real_names[index];
3145 }
3146
3147 static struct symtab *
3148 dw2_find_last_source_symtab (struct objfile *objfile)
3149 {
3150 struct dwarf2_per_objfile *dwarf2_per_objfile
3151 = get_dwarf2_per_objfile (objfile);
3152 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->all_comp_units.back ();
3153 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, false);
3154
3155 if (cust == NULL)
3156 return NULL;
3157
3158 return compunit_primary_filetab (cust);
3159 }
3160
3161 /* Traversal function for dw2_forget_cached_source_info. */
3162
3163 static int
3164 dw2_free_cached_file_names (void **slot, void *info)
3165 {
3166 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3167
3168 if (file_data->real_names)
3169 {
3170 int i;
3171
3172 for (i = 0; i < file_data->num_file_names; ++i)
3173 {
3174 xfree ((void*) file_data->real_names[i]);
3175 file_data->real_names[i] = NULL;
3176 }
3177 }
3178
3179 return 1;
3180 }
3181
3182 static void
3183 dw2_forget_cached_source_info (struct objfile *objfile)
3184 {
3185 struct dwarf2_per_objfile *dwarf2_per_objfile
3186 = get_dwarf2_per_objfile (objfile);
3187
3188 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table.get (),
3189 dw2_free_cached_file_names, NULL);
3190 }
3191
3192 /* Helper function for dw2_map_symtabs_matching_filename that expands
3193 the symtabs and calls the iterator. */
3194
3195 static int
3196 dw2_map_expand_apply (struct objfile *objfile,
3197 struct dwarf2_per_cu_data *per_cu,
3198 const char *name, const char *real_path,
3199 gdb::function_view<bool (symtab *)> callback)
3200 {
3201 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3202
3203 /* Don't visit already-expanded CUs. */
3204 if (per_cu->v.quick->compunit_symtab)
3205 return 0;
3206
3207 /* This may expand more than one symtab, and we want to iterate over
3208 all of them. */
3209 dw2_instantiate_symtab (per_cu, false);
3210
3211 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3212 last_made, callback);
3213 }
3214
3215 /* Implementation of the map_symtabs_matching_filename method. */
3216
3217 static bool
3218 dw2_map_symtabs_matching_filename
3219 (struct objfile *objfile, const char *name, const char *real_path,
3220 gdb::function_view<bool (symtab *)> callback)
3221 {
3222 const char *name_basename = lbasename (name);
3223 struct dwarf2_per_objfile *dwarf2_per_objfile
3224 = get_dwarf2_per_objfile (objfile);
3225
3226 /* The rule is CUs specify all the files, including those used by
3227 any TU, so there's no need to scan TUs here. */
3228
3229 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3230 {
3231 /* We only need to look at symtabs not already expanded. */
3232 if (per_cu->v.quick->compunit_symtab)
3233 continue;
3234
3235 quick_file_names *file_data = dw2_get_file_names (per_cu);
3236 if (file_data == NULL)
3237 continue;
3238
3239 for (int j = 0; j < file_data->num_file_names; ++j)
3240 {
3241 const char *this_name = file_data->file_names[j];
3242 const char *this_real_name;
3243
3244 if (compare_filenames_for_search (this_name, name))
3245 {
3246 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3247 callback))
3248 return true;
3249 continue;
3250 }
3251
3252 /* Before we invoke realpath, which can get expensive when many
3253 files are involved, do a quick comparison of the basenames. */
3254 if (! basenames_may_differ
3255 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3256 continue;
3257
3258 this_real_name = dw2_get_real_path (objfile, file_data, j);
3259 if (compare_filenames_for_search (this_real_name, name))
3260 {
3261 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3262 callback))
3263 return true;
3264 continue;
3265 }
3266
3267 if (real_path != NULL)
3268 {
3269 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3270 gdb_assert (IS_ABSOLUTE_PATH (name));
3271 if (this_real_name != NULL
3272 && FILENAME_CMP (real_path, this_real_name) == 0)
3273 {
3274 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3275 callback))
3276 return true;
3277 continue;
3278 }
3279 }
3280 }
3281 }
3282
3283 return false;
3284 }
3285
3286 /* Struct used to manage iterating over all CUs looking for a symbol. */
3287
3288 struct dw2_symtab_iterator
3289 {
3290 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3291 struct dwarf2_per_objfile *dwarf2_per_objfile;
3292 /* If set, only look for symbols that match that block. Valid values are
3293 GLOBAL_BLOCK and STATIC_BLOCK. */
3294 gdb::optional<block_enum> block_index;
3295 /* The kind of symbol we're looking for. */
3296 domain_enum domain;
3297 /* The list of CUs from the index entry of the symbol,
3298 or NULL if not found. */
3299 offset_type *vec;
3300 /* The next element in VEC to look at. */
3301 int next;
3302 /* The number of elements in VEC, or zero if there is no match. */
3303 int length;
3304 /* Have we seen a global version of the symbol?
3305 If so we can ignore all further global instances.
3306 This is to work around gold/15646, inefficient gold-generated
3307 indices. */
3308 int global_seen;
3309 };
3310
3311 /* Initialize the index symtab iterator ITER. */
3312
3313 static void
3314 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3315 struct dwarf2_per_objfile *dwarf2_per_objfile,
3316 gdb::optional<block_enum> block_index,
3317 domain_enum domain,
3318 const char *name)
3319 {
3320 iter->dwarf2_per_objfile = dwarf2_per_objfile;
3321 iter->block_index = block_index;
3322 iter->domain = domain;
3323 iter->next = 0;
3324 iter->global_seen = 0;
3325
3326 mapped_index *index = dwarf2_per_objfile->index_table.get ();
3327
3328 /* index is NULL if OBJF_READNOW. */
3329 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
3330 iter->length = MAYBE_SWAP (*iter->vec);
3331 else
3332 {
3333 iter->vec = NULL;
3334 iter->length = 0;
3335 }
3336 }
3337
3338 /* Return the next matching CU or NULL if there are no more. */
3339
3340 static struct dwarf2_per_cu_data *
3341 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3342 {
3343 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3344
3345 for ( ; iter->next < iter->length; ++iter->next)
3346 {
3347 offset_type cu_index_and_attrs =
3348 MAYBE_SWAP (iter->vec[iter->next + 1]);
3349 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3350 gdb_index_symbol_kind symbol_kind =
3351 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3352 /* Only check the symbol attributes if they're present.
3353 Indices prior to version 7 don't record them,
3354 and indices >= 7 may elide them for certain symbols
3355 (gold does this). */
3356 int attrs_valid =
3357 (dwarf2_per_objfile->index_table->version >= 7
3358 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3359
3360 /* Don't crash on bad data. */
3361 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
3362 + dwarf2_per_objfile->all_type_units.size ()))
3363 {
3364 complaint (_(".gdb_index entry has bad CU index"
3365 " [in module %s]"),
3366 objfile_name (dwarf2_per_objfile->objfile));
3367 continue;
3368 }
3369
3370 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
3371
3372 /* Skip if already read in. */
3373 if (per_cu->v.quick->compunit_symtab)
3374 continue;
3375
3376 /* Check static vs global. */
3377 if (attrs_valid)
3378 {
3379 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3380
3381 if (iter->block_index.has_value ())
3382 {
3383 bool want_static = *iter->block_index == STATIC_BLOCK;
3384
3385 if (is_static != want_static)
3386 continue;
3387 }
3388
3389 /* Work around gold/15646. */
3390 if (!is_static && iter->global_seen)
3391 continue;
3392 if (!is_static)
3393 iter->global_seen = 1;
3394 }
3395
3396 /* Only check the symbol's kind if it has one. */
3397 if (attrs_valid)
3398 {
3399 switch (iter->domain)
3400 {
3401 case VAR_DOMAIN:
3402 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3403 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3404 /* Some types are also in VAR_DOMAIN. */
3405 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3406 continue;
3407 break;
3408 case STRUCT_DOMAIN:
3409 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3410 continue;
3411 break;
3412 case LABEL_DOMAIN:
3413 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3414 continue;
3415 break;
3416 case MODULE_DOMAIN:
3417 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3418 continue;
3419 break;
3420 default:
3421 break;
3422 }
3423 }
3424
3425 ++iter->next;
3426 return per_cu;
3427 }
3428
3429 return NULL;
3430 }
3431
3432 static struct compunit_symtab *
3433 dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
3434 const char *name, domain_enum domain)
3435 {
3436 struct compunit_symtab *stab_best = NULL;
3437 struct dwarf2_per_objfile *dwarf2_per_objfile
3438 = get_dwarf2_per_objfile (objfile);
3439
3440 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3441
3442 struct dw2_symtab_iterator iter;
3443 struct dwarf2_per_cu_data *per_cu;
3444
3445 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
3446
3447 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3448 {
3449 struct symbol *sym, *with_opaque = NULL;
3450 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
3451 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
3452 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
3453
3454 sym = block_find_symbol (block, name, domain,
3455 block_find_non_opaque_type_preferred,
3456 &with_opaque);
3457
3458 /* Some caution must be observed with overloaded functions
3459 and methods, since the index will not contain any overload
3460 information (but NAME might contain it). */
3461
3462 if (sym != NULL
3463 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3464 return stab;
3465 if (with_opaque != NULL
3466 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3467 stab_best = stab;
3468
3469 /* Keep looking through other CUs. */
3470 }
3471
3472 return stab_best;
3473 }
3474
3475 static void
3476 dw2_print_stats (struct objfile *objfile)
3477 {
3478 struct dwarf2_per_objfile *dwarf2_per_objfile
3479 = get_dwarf2_per_objfile (objfile);
3480 int total = (dwarf2_per_objfile->all_comp_units.size ()
3481 + dwarf2_per_objfile->all_type_units.size ());
3482 int count = 0;
3483
3484 for (int i = 0; i < total; ++i)
3485 {
3486 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
3487
3488 if (!per_cu->v.quick->compunit_symtab)
3489 ++count;
3490 }
3491 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3492 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3493 }
3494
3495 /* This dumps minimal information about the index.
3496 It is called via "mt print objfiles".
3497 One use is to verify .gdb_index has been loaded by the
3498 gdb.dwarf2/gdb-index.exp testcase. */
3499
3500 static void
3501 dw2_dump (struct objfile *objfile)
3502 {
3503 struct dwarf2_per_objfile *dwarf2_per_objfile
3504 = get_dwarf2_per_objfile (objfile);
3505
3506 gdb_assert (dwarf2_per_objfile->using_index);
3507 printf_filtered (".gdb_index:");
3508 if (dwarf2_per_objfile->index_table != NULL)
3509 {
3510 printf_filtered (" version %d\n",
3511 dwarf2_per_objfile->index_table->version);
3512 }
3513 else
3514 printf_filtered (" faked for \"readnow\"\n");
3515 printf_filtered ("\n");
3516 }
3517
3518 static void
3519 dw2_expand_symtabs_for_function (struct objfile *objfile,
3520 const char *func_name)
3521 {
3522 struct dwarf2_per_objfile *dwarf2_per_objfile
3523 = get_dwarf2_per_objfile (objfile);
3524
3525 struct dw2_symtab_iterator iter;
3526 struct dwarf2_per_cu_data *per_cu;
3527
3528 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
3529
3530 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3531 dw2_instantiate_symtab (per_cu, false);
3532
3533 }
3534
3535 static void
3536 dw2_expand_all_symtabs (struct objfile *objfile)
3537 {
3538 struct dwarf2_per_objfile *dwarf2_per_objfile
3539 = get_dwarf2_per_objfile (objfile);
3540 int total_units = (dwarf2_per_objfile->all_comp_units.size ()
3541 + dwarf2_per_objfile->all_type_units.size ());
3542
3543 for (int i = 0; i < total_units; ++i)
3544 {
3545 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
3546
3547 /* We don't want to directly expand a partial CU, because if we
3548 read it with the wrong language, then assertion failures can
3549 be triggered later on. See PR symtab/23010. So, tell
3550 dw2_instantiate_symtab to skip partial CUs -- any important
3551 partial CU will be read via DW_TAG_imported_unit anyway. */
3552 dw2_instantiate_symtab (per_cu, true);
3553 }
3554 }
3555
3556 static void
3557 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3558 const char *fullname)
3559 {
3560 struct dwarf2_per_objfile *dwarf2_per_objfile
3561 = get_dwarf2_per_objfile (objfile);
3562
3563 /* We don't need to consider type units here.
3564 This is only called for examining code, e.g. expand_line_sal.
3565 There can be an order of magnitude (or more) more type units
3566 than comp units, and we avoid them if we can. */
3567
3568 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3569 {
3570 /* We only need to look at symtabs not already expanded. */
3571 if (per_cu->v.quick->compunit_symtab)
3572 continue;
3573
3574 quick_file_names *file_data = dw2_get_file_names (per_cu);
3575 if (file_data == NULL)
3576 continue;
3577
3578 for (int j = 0; j < file_data->num_file_names; ++j)
3579 {
3580 const char *this_fullname = file_data->file_names[j];
3581
3582 if (filename_cmp (this_fullname, fullname) == 0)
3583 {
3584 dw2_instantiate_symtab (per_cu, false);
3585 break;
3586 }
3587 }
3588 }
3589 }
3590
3591 static void
3592 dw2_map_matching_symbols
3593 (struct objfile *objfile,
3594 const lookup_name_info &name, domain_enum domain,
3595 int global,
3596 gdb::function_view<symbol_found_callback_ftype> callback,
3597 symbol_compare_ftype *ordered_compare)
3598 {
3599 /* Currently unimplemented; used for Ada. The function can be called if the
3600 current language is Ada for a non-Ada objfile using GNU index. As Ada
3601 does not look for non-Ada symbols this function should just return. */
3602 }
3603
3604 /* Starting from a search name, return the string that finds the upper
3605 bound of all strings that start with SEARCH_NAME in a sorted name
3606 list. Returns the empty string to indicate that the upper bound is
3607 the end of the list. */
3608
3609 static std::string
3610 make_sort_after_prefix_name (const char *search_name)
3611 {
3612 /* When looking to complete "func", we find the upper bound of all
3613 symbols that start with "func" by looking for where we'd insert
3614 the closest string that would follow "func" in lexicographical
3615 order. Usually, that's "func"-with-last-character-incremented,
3616 i.e. "fund". Mind non-ASCII characters, though. Usually those
3617 will be UTF-8 multi-byte sequences, but we can't be certain.
3618 Especially mind the 0xff character, which is a valid character in
3619 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3620 rule out compilers allowing it in identifiers. Note that
3621 conveniently, strcmp/strcasecmp are specified to compare
3622 characters interpreted as unsigned char. So what we do is treat
3623 the whole string as a base 256 number composed of a sequence of
3624 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3625 to 0, and carries 1 to the following more-significant position.
3626 If the very first character in SEARCH_NAME ends up incremented
3627 and carries/overflows, then the upper bound is the end of the
3628 list. The string after the empty string is also the empty
3629 string.
3630
3631 Some examples of this operation:
3632
3633 SEARCH_NAME => "+1" RESULT
3634
3635 "abc" => "abd"
3636 "ab\xff" => "ac"
3637 "\xff" "a" "\xff" => "\xff" "b"
3638 "\xff" => ""
3639 "\xff\xff" => ""
3640 "" => ""
3641
3642 Then, with these symbols for example:
3643
3644 func
3645 func1
3646 fund
3647
3648 completing "func" looks for symbols between "func" and
3649 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3650 which finds "func" and "func1", but not "fund".
3651
3652 And with:
3653
3654 funcÿ (Latin1 'ÿ' [0xff])
3655 funcÿ1
3656 fund
3657
3658 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3659 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3660
3661 And with:
3662
3663 ÿÿ (Latin1 'ÿ' [0xff])
3664 ÿÿ1
3665
3666 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3667 the end of the list.
3668 */
3669 std::string after = search_name;
3670 while (!after.empty () && (unsigned char) after.back () == 0xff)
3671 after.pop_back ();
3672 if (!after.empty ())
3673 after.back () = (unsigned char) after.back () + 1;
3674 return after;
3675 }
3676
3677 /* See declaration. */
3678
3679 std::pair<std::vector<name_component>::const_iterator,
3680 std::vector<name_component>::const_iterator>
3681 mapped_index_base::find_name_components_bounds
3682 (const lookup_name_info &lookup_name_without_params, language lang) const
3683 {
3684 auto *name_cmp
3685 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3686
3687 const char *lang_name
3688 = lookup_name_without_params.language_lookup_name (lang).c_str ();
3689
3690 /* Comparison function object for lower_bound that matches against a
3691 given symbol name. */
3692 auto lookup_compare_lower = [&] (const name_component &elem,
3693 const char *name)
3694 {
3695 const char *elem_qualified = this->symbol_name_at (elem.idx);
3696 const char *elem_name = elem_qualified + elem.name_offset;
3697 return name_cmp (elem_name, name) < 0;
3698 };
3699
3700 /* Comparison function object for upper_bound that matches against a
3701 given symbol name. */
3702 auto lookup_compare_upper = [&] (const char *name,
3703 const name_component &elem)
3704 {
3705 const char *elem_qualified = this->symbol_name_at (elem.idx);
3706 const char *elem_name = elem_qualified + elem.name_offset;
3707 return name_cmp (name, elem_name) < 0;
3708 };
3709
3710 auto begin = this->name_components.begin ();
3711 auto end = this->name_components.end ();
3712
3713 /* Find the lower bound. */
3714 auto lower = [&] ()
3715 {
3716 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3717 return begin;
3718 else
3719 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3720 } ();
3721
3722 /* Find the upper bound. */
3723 auto upper = [&] ()
3724 {
3725 if (lookup_name_without_params.completion_mode ())
3726 {
3727 /* In completion mode, we want UPPER to point past all
3728 symbols names that have the same prefix. I.e., with
3729 these symbols, and completing "func":
3730
3731 function << lower bound
3732 function1
3733 other_function << upper bound
3734
3735 We find the upper bound by looking for the insertion
3736 point of "func"-with-last-character-incremented,
3737 i.e. "fund". */
3738 std::string after = make_sort_after_prefix_name (lang_name);
3739 if (after.empty ())
3740 return end;
3741 return std::lower_bound (lower, end, after.c_str (),
3742 lookup_compare_lower);
3743 }
3744 else
3745 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3746 } ();
3747
3748 return {lower, upper};
3749 }
3750
3751 /* See declaration. */
3752
3753 void
3754 mapped_index_base::build_name_components ()
3755 {
3756 if (!this->name_components.empty ())
3757 return;
3758
3759 this->name_components_casing = case_sensitivity;
3760 auto *name_cmp
3761 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3762
3763 /* The code below only knows how to break apart components of C++
3764 symbol names (and other languages that use '::' as
3765 namespace/module separator) and Ada symbol names. */
3766 auto count = this->symbol_name_count ();
3767 for (offset_type idx = 0; idx < count; idx++)
3768 {
3769 if (this->symbol_name_slot_invalid (idx))
3770 continue;
3771
3772 const char *name = this->symbol_name_at (idx);
3773
3774 /* Add each name component to the name component table. */
3775 unsigned int previous_len = 0;
3776
3777 if (strstr (name, "::") != nullptr)
3778 {
3779 for (unsigned int current_len = cp_find_first_component (name);
3780 name[current_len] != '\0';
3781 current_len += cp_find_first_component (name + current_len))
3782 {
3783 gdb_assert (name[current_len] == ':');
3784 this->name_components.push_back ({previous_len, idx});
3785 /* Skip the '::'. */
3786 current_len += 2;
3787 previous_len = current_len;
3788 }
3789 }
3790 else
3791 {
3792 /* Handle the Ada encoded (aka mangled) form here. */
3793 for (const char *iter = strstr (name, "__");
3794 iter != nullptr;
3795 iter = strstr (iter, "__"))
3796 {
3797 this->name_components.push_back ({previous_len, idx});
3798 iter += 2;
3799 previous_len = iter - name;
3800 }
3801 }
3802
3803 this->name_components.push_back ({previous_len, idx});
3804 }
3805
3806 /* Sort name_components elements by name. */
3807 auto name_comp_compare = [&] (const name_component &left,
3808 const name_component &right)
3809 {
3810 const char *left_qualified = this->symbol_name_at (left.idx);
3811 const char *right_qualified = this->symbol_name_at (right.idx);
3812
3813 const char *left_name = left_qualified + left.name_offset;
3814 const char *right_name = right_qualified + right.name_offset;
3815
3816 return name_cmp (left_name, right_name) < 0;
3817 };
3818
3819 std::sort (this->name_components.begin (),
3820 this->name_components.end (),
3821 name_comp_compare);
3822 }
3823
3824 /* Helper for dw2_expand_symtabs_matching that works with a
3825 mapped_index_base instead of the containing objfile. This is split
3826 to a separate function in order to be able to unit test the
3827 name_components matching using a mock mapped_index_base. For each
3828 symbol name that matches, calls MATCH_CALLBACK, passing it the
3829 symbol's index in the mapped_index_base symbol table. */
3830
3831 static void
3832 dw2_expand_symtabs_matching_symbol
3833 (mapped_index_base &index,
3834 const lookup_name_info &lookup_name_in,
3835 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3836 enum search_domain kind,
3837 gdb::function_view<bool (offset_type)> match_callback)
3838 {
3839 lookup_name_info lookup_name_without_params
3840 = lookup_name_in.make_ignore_params ();
3841
3842 /* Build the symbol name component sorted vector, if we haven't
3843 yet. */
3844 index.build_name_components ();
3845
3846 /* The same symbol may appear more than once in the range though.
3847 E.g., if we're looking for symbols that complete "w", and we have
3848 a symbol named "w1::w2", we'll find the two name components for
3849 that same symbol in the range. To be sure we only call the
3850 callback once per symbol, we first collect the symbol name
3851 indexes that matched in a temporary vector and ignore
3852 duplicates. */
3853 std::vector<offset_type> matches;
3854
3855 struct name_and_matcher
3856 {
3857 symbol_name_matcher_ftype *matcher;
3858 const std::string &name;
3859
3860 bool operator== (const name_and_matcher &other) const
3861 {
3862 return matcher == other.matcher && name == other.name;
3863 }
3864 };
3865
3866 /* A vector holding all the different symbol name matchers, for all
3867 languages. */
3868 std::vector<name_and_matcher> matchers;
3869
3870 for (int i = 0; i < nr_languages; i++)
3871 {
3872 enum language lang_e = (enum language) i;
3873
3874 const language_defn *lang = language_def (lang_e);
3875 symbol_name_matcher_ftype *name_matcher
3876 = get_symbol_name_matcher (lang, lookup_name_without_params);
3877
3878 name_and_matcher key {
3879 name_matcher,
3880 lookup_name_without_params.language_lookup_name (lang_e)
3881 };
3882
3883 /* Don't insert the same comparison routine more than once.
3884 Note that we do this linear walk. This is not a problem in
3885 practice because the number of supported languages is
3886 low. */
3887 if (std::find (matchers.begin (), matchers.end (), key)
3888 != matchers.end ())
3889 continue;
3890 matchers.push_back (std::move (key));
3891
3892 auto bounds
3893 = index.find_name_components_bounds (lookup_name_without_params,
3894 lang_e);
3895
3896 /* Now for each symbol name in range, check to see if we have a name
3897 match, and if so, call the MATCH_CALLBACK callback. */
3898
3899 for (; bounds.first != bounds.second; ++bounds.first)
3900 {
3901 const char *qualified = index.symbol_name_at (bounds.first->idx);
3902
3903 if (!name_matcher (qualified, lookup_name_without_params, NULL)
3904 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
3905 continue;
3906
3907 matches.push_back (bounds.first->idx);
3908 }
3909 }
3910
3911 std::sort (matches.begin (), matches.end ());
3912
3913 /* Finally call the callback, once per match. */
3914 ULONGEST prev = -1;
3915 for (offset_type idx : matches)
3916 {
3917 if (prev != idx)
3918 {
3919 if (!match_callback (idx))
3920 break;
3921 prev = idx;
3922 }
3923 }
3924
3925 /* Above we use a type wider than idx's for 'prev', since 0 and
3926 (offset_type)-1 are both possible values. */
3927 static_assert (sizeof (prev) > sizeof (offset_type), "");
3928 }
3929
3930 #if GDB_SELF_TEST
3931
3932 namespace selftests { namespace dw2_expand_symtabs_matching {
3933
3934 /* A mock .gdb_index/.debug_names-like name index table, enough to
3935 exercise dw2_expand_symtabs_matching_symbol, which works with the
3936 mapped_index_base interface. Builds an index from the symbol list
3937 passed as parameter to the constructor. */
3938 class mock_mapped_index : public mapped_index_base
3939 {
3940 public:
3941 mock_mapped_index (gdb::array_view<const char *> symbols)
3942 : m_symbol_table (symbols)
3943 {}
3944
3945 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
3946
3947 /* Return the number of names in the symbol table. */
3948 size_t symbol_name_count () const override
3949 {
3950 return m_symbol_table.size ();
3951 }
3952
3953 /* Get the name of the symbol at IDX in the symbol table. */
3954 const char *symbol_name_at (offset_type idx) const override
3955 {
3956 return m_symbol_table[idx];
3957 }
3958
3959 private:
3960 gdb::array_view<const char *> m_symbol_table;
3961 };
3962
3963 /* Convenience function that converts a NULL pointer to a "<null>"
3964 string, to pass to print routines. */
3965
3966 static const char *
3967 string_or_null (const char *str)
3968 {
3969 return str != NULL ? str : "<null>";
3970 }
3971
3972 /* Check if a lookup_name_info built from
3973 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
3974 index. EXPECTED_LIST is the list of expected matches, in expected
3975 matching order. If no match expected, then an empty list is
3976 specified. Returns true on success. On failure prints a warning
3977 indicating the file:line that failed, and returns false. */
3978
3979 static bool
3980 check_match (const char *file, int line,
3981 mock_mapped_index &mock_index,
3982 const char *name, symbol_name_match_type match_type,
3983 bool completion_mode,
3984 std::initializer_list<const char *> expected_list)
3985 {
3986 lookup_name_info lookup_name (name, match_type, completion_mode);
3987
3988 bool matched = true;
3989
3990 auto mismatch = [&] (const char *expected_str,
3991 const char *got)
3992 {
3993 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
3994 "expected=\"%s\", got=\"%s\"\n"),
3995 file, line,
3996 (match_type == symbol_name_match_type::FULL
3997 ? "FULL" : "WILD"),
3998 name, string_or_null (expected_str), string_or_null (got));
3999 matched = false;
4000 };
4001
4002 auto expected_it = expected_list.begin ();
4003 auto expected_end = expected_list.end ();
4004
4005 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4006 NULL, ALL_DOMAIN,
4007 [&] (offset_type idx)
4008 {
4009 const char *matched_name = mock_index.symbol_name_at (idx);
4010 const char *expected_str
4011 = expected_it == expected_end ? NULL : *expected_it++;
4012
4013 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4014 mismatch (expected_str, matched_name);
4015 return true;
4016 });
4017
4018 const char *expected_str
4019 = expected_it == expected_end ? NULL : *expected_it++;
4020 if (expected_str != NULL)
4021 mismatch (expected_str, NULL);
4022
4023 return matched;
4024 }
4025
4026 /* The symbols added to the mock mapped_index for testing (in
4027 canonical form). */
4028 static const char *test_symbols[] = {
4029 "function",
4030 "std::bar",
4031 "std::zfunction",
4032 "std::zfunction2",
4033 "w1::w2",
4034 "ns::foo<char*>",
4035 "ns::foo<int>",
4036 "ns::foo<long>",
4037 "ns2::tmpl<int>::foo2",
4038 "(anonymous namespace)::A::B::C",
4039
4040 /* These are used to check that the increment-last-char in the
4041 matching algorithm for completion doesn't match "t1_fund" when
4042 completing "t1_func". */
4043 "t1_func",
4044 "t1_func1",
4045 "t1_fund",
4046 "t1_fund1",
4047
4048 /* A UTF-8 name with multi-byte sequences to make sure that
4049 cp-name-parser understands this as a single identifier ("função"
4050 is "function" in PT). */
4051 u8"u8função",
4052
4053 /* \377 (0xff) is Latin1 'ÿ'. */
4054 "yfunc\377",
4055
4056 /* \377 (0xff) is Latin1 'ÿ'. */
4057 "\377",
4058 "\377\377123",
4059
4060 /* A name with all sorts of complications. Starts with "z" to make
4061 it easier for the completion tests below. */
4062 #define Z_SYM_NAME \
4063 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4064 "::tuple<(anonymous namespace)::ui*, " \
4065 "std::default_delete<(anonymous namespace)::ui>, void>"
4066
4067 Z_SYM_NAME
4068 };
4069
4070 /* Returns true if the mapped_index_base::find_name_component_bounds
4071 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4072 in completion mode. */
4073
4074 static bool
4075 check_find_bounds_finds (mapped_index_base &index,
4076 const char *search_name,
4077 gdb::array_view<const char *> expected_syms)
4078 {
4079 lookup_name_info lookup_name (search_name,
4080 symbol_name_match_type::FULL, true);
4081
4082 auto bounds = index.find_name_components_bounds (lookup_name,
4083 language_cplus);
4084
4085 size_t distance = std::distance (bounds.first, bounds.second);
4086 if (distance != expected_syms.size ())
4087 return false;
4088
4089 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4090 {
4091 auto nc_elem = bounds.first + exp_elem;
4092 const char *qualified = index.symbol_name_at (nc_elem->idx);
4093 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4094 return false;
4095 }
4096
4097 return true;
4098 }
4099
4100 /* Test the lower-level mapped_index::find_name_component_bounds
4101 method. */
4102
4103 static void
4104 test_mapped_index_find_name_component_bounds ()
4105 {
4106 mock_mapped_index mock_index (test_symbols);
4107
4108 mock_index.build_name_components ();
4109
4110 /* Test the lower-level mapped_index::find_name_component_bounds
4111 method in completion mode. */
4112 {
4113 static const char *expected_syms[] = {
4114 "t1_func",
4115 "t1_func1",
4116 };
4117
4118 SELF_CHECK (check_find_bounds_finds (mock_index,
4119 "t1_func", expected_syms));
4120 }
4121
4122 /* Check that the increment-last-char in the name matching algorithm
4123 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4124 {
4125 static const char *expected_syms1[] = {
4126 "\377",
4127 "\377\377123",
4128 };
4129 SELF_CHECK (check_find_bounds_finds (mock_index,
4130 "\377", expected_syms1));
4131
4132 static const char *expected_syms2[] = {
4133 "\377\377123",
4134 };
4135 SELF_CHECK (check_find_bounds_finds (mock_index,
4136 "\377\377", expected_syms2));
4137 }
4138 }
4139
4140 /* Test dw2_expand_symtabs_matching_symbol. */
4141
4142 static void
4143 test_dw2_expand_symtabs_matching_symbol ()
4144 {
4145 mock_mapped_index mock_index (test_symbols);
4146
4147 /* We let all tests run until the end even if some fails, for debug
4148 convenience. */
4149 bool any_mismatch = false;
4150
4151 /* Create the expected symbols list (an initializer_list). Needed
4152 because lists have commas, and we need to pass them to CHECK,
4153 which is a macro. */
4154 #define EXPECT(...) { __VA_ARGS__ }
4155
4156 /* Wrapper for check_match that passes down the current
4157 __FILE__/__LINE__. */
4158 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4159 any_mismatch |= !check_match (__FILE__, __LINE__, \
4160 mock_index, \
4161 NAME, MATCH_TYPE, COMPLETION_MODE, \
4162 EXPECTED_LIST)
4163
4164 /* Identity checks. */
4165 for (const char *sym : test_symbols)
4166 {
4167 /* Should be able to match all existing symbols. */
4168 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4169 EXPECT (sym));
4170
4171 /* Should be able to match all existing symbols with
4172 parameters. */
4173 std::string with_params = std::string (sym) + "(int)";
4174 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4175 EXPECT (sym));
4176
4177 /* Should be able to match all existing symbols with
4178 parameters and qualifiers. */
4179 with_params = std::string (sym) + " ( int ) const";
4180 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4181 EXPECT (sym));
4182
4183 /* This should really find sym, but cp-name-parser.y doesn't
4184 know about lvalue/rvalue qualifiers yet. */
4185 with_params = std::string (sym) + " ( int ) &&";
4186 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4187 {});
4188 }
4189
4190 /* Check that the name matching algorithm for completion doesn't get
4191 confused with Latin1 'ÿ' / 0xff. */
4192 {
4193 static const char str[] = "\377";
4194 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4195 EXPECT ("\377", "\377\377123"));
4196 }
4197
4198 /* Check that the increment-last-char in the matching algorithm for
4199 completion doesn't match "t1_fund" when completing "t1_func". */
4200 {
4201 static const char str[] = "t1_func";
4202 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4203 EXPECT ("t1_func", "t1_func1"));
4204 }
4205
4206 /* Check that completion mode works at each prefix of the expected
4207 symbol name. */
4208 {
4209 static const char str[] = "function(int)";
4210 size_t len = strlen (str);
4211 std::string lookup;
4212
4213 for (size_t i = 1; i < len; i++)
4214 {
4215 lookup.assign (str, i);
4216 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4217 EXPECT ("function"));
4218 }
4219 }
4220
4221 /* While "w" is a prefix of both components, the match function
4222 should still only be called once. */
4223 {
4224 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4225 EXPECT ("w1::w2"));
4226 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4227 EXPECT ("w1::w2"));
4228 }
4229
4230 /* Same, with a "complicated" symbol. */
4231 {
4232 static const char str[] = Z_SYM_NAME;
4233 size_t len = strlen (str);
4234 std::string lookup;
4235
4236 for (size_t i = 1; i < len; i++)
4237 {
4238 lookup.assign (str, i);
4239 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4240 EXPECT (Z_SYM_NAME));
4241 }
4242 }
4243
4244 /* In FULL mode, an incomplete symbol doesn't match. */
4245 {
4246 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4247 {});
4248 }
4249
4250 /* A complete symbol with parameters matches any overload, since the
4251 index has no overload info. */
4252 {
4253 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4254 EXPECT ("std::zfunction", "std::zfunction2"));
4255 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4256 EXPECT ("std::zfunction", "std::zfunction2"));
4257 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4258 EXPECT ("std::zfunction", "std::zfunction2"));
4259 }
4260
4261 /* Check that whitespace is ignored appropriately. A symbol with a
4262 template argument list. */
4263 {
4264 static const char expected[] = "ns::foo<int>";
4265 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4266 EXPECT (expected));
4267 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4268 EXPECT (expected));
4269 }
4270
4271 /* Check that whitespace is ignored appropriately. A symbol with a
4272 template argument list that includes a pointer. */
4273 {
4274 static const char expected[] = "ns::foo<char*>";
4275 /* Try both completion and non-completion modes. */
4276 static const bool completion_mode[2] = {false, true};
4277 for (size_t i = 0; i < 2; i++)
4278 {
4279 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4280 completion_mode[i], EXPECT (expected));
4281 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4282 completion_mode[i], EXPECT (expected));
4283
4284 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4285 completion_mode[i], EXPECT (expected));
4286 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4287 completion_mode[i], EXPECT (expected));
4288 }
4289 }
4290
4291 {
4292 /* Check method qualifiers are ignored. */
4293 static const char expected[] = "ns::foo<char*>";
4294 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4295 symbol_name_match_type::FULL, true, EXPECT (expected));
4296 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4297 symbol_name_match_type::FULL, true, EXPECT (expected));
4298 CHECK_MATCH ("foo < char * > ( int ) const",
4299 symbol_name_match_type::WILD, true, EXPECT (expected));
4300 CHECK_MATCH ("foo < char * > ( int ) &&",
4301 symbol_name_match_type::WILD, true, EXPECT (expected));
4302 }
4303
4304 /* Test lookup names that don't match anything. */
4305 {
4306 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4307 {});
4308
4309 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4310 {});
4311 }
4312
4313 /* Some wild matching tests, exercising "(anonymous namespace)",
4314 which should not be confused with a parameter list. */
4315 {
4316 static const char *syms[] = {
4317 "A::B::C",
4318 "B::C",
4319 "C",
4320 "A :: B :: C ( int )",
4321 "B :: C ( int )",
4322 "C ( int )",
4323 };
4324
4325 for (const char *s : syms)
4326 {
4327 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4328 EXPECT ("(anonymous namespace)::A::B::C"));
4329 }
4330 }
4331
4332 {
4333 static const char expected[] = "ns2::tmpl<int>::foo2";
4334 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4335 EXPECT (expected));
4336 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4337 EXPECT (expected));
4338 }
4339
4340 SELF_CHECK (!any_mismatch);
4341
4342 #undef EXPECT
4343 #undef CHECK_MATCH
4344 }
4345
4346 static void
4347 run_test ()
4348 {
4349 test_mapped_index_find_name_component_bounds ();
4350 test_dw2_expand_symtabs_matching_symbol ();
4351 }
4352
4353 }} // namespace selftests::dw2_expand_symtabs_matching
4354
4355 #endif /* GDB_SELF_TEST */
4356
4357 /* If FILE_MATCHER is NULL or if PER_CU has
4358 dwarf2_per_cu_quick_data::MARK set (see
4359 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4360 EXPANSION_NOTIFY on it. */
4361
4362 static void
4363 dw2_expand_symtabs_matching_one
4364 (struct dwarf2_per_cu_data *per_cu,
4365 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4366 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4367 {
4368 if (file_matcher == NULL || per_cu->v.quick->mark)
4369 {
4370 bool symtab_was_null
4371 = (per_cu->v.quick->compunit_symtab == NULL);
4372
4373 dw2_instantiate_symtab (per_cu, false);
4374
4375 if (expansion_notify != NULL
4376 && symtab_was_null
4377 && per_cu->v.quick->compunit_symtab != NULL)
4378 expansion_notify (per_cu->v.quick->compunit_symtab);
4379 }
4380 }
4381
4382 /* Helper for dw2_expand_matching symtabs. Called on each symbol
4383 matched, to expand corresponding CUs that were marked. IDX is the
4384 index of the symbol name that matched. */
4385
4386 static void
4387 dw2_expand_marked_cus
4388 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
4389 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4390 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4391 search_domain kind)
4392 {
4393 offset_type *vec, vec_len, vec_idx;
4394 bool global_seen = false;
4395 mapped_index &index = *dwarf2_per_objfile->index_table;
4396
4397 vec = (offset_type *) (index.constant_pool
4398 + MAYBE_SWAP (index.symbol_table[idx].vec));
4399 vec_len = MAYBE_SWAP (vec[0]);
4400 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4401 {
4402 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4403 /* This value is only valid for index versions >= 7. */
4404 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4405 gdb_index_symbol_kind symbol_kind =
4406 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4407 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4408 /* Only check the symbol attributes if they're present.
4409 Indices prior to version 7 don't record them,
4410 and indices >= 7 may elide them for certain symbols
4411 (gold does this). */
4412 int attrs_valid =
4413 (index.version >= 7
4414 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4415
4416 /* Work around gold/15646. */
4417 if (attrs_valid)
4418 {
4419 if (!is_static && global_seen)
4420 continue;
4421 if (!is_static)
4422 global_seen = true;
4423 }
4424
4425 /* Only check the symbol's kind if it has one. */
4426 if (attrs_valid)
4427 {
4428 switch (kind)
4429 {
4430 case VARIABLES_DOMAIN:
4431 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4432 continue;
4433 break;
4434 case FUNCTIONS_DOMAIN:
4435 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4436 continue;
4437 break;
4438 case TYPES_DOMAIN:
4439 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4440 continue;
4441 break;
4442 case MODULES_DOMAIN:
4443 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4444 continue;
4445 break;
4446 default:
4447 break;
4448 }
4449 }
4450
4451 /* Don't crash on bad data. */
4452 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
4453 + dwarf2_per_objfile->all_type_units.size ()))
4454 {
4455 complaint (_(".gdb_index entry has bad CU index"
4456 " [in module %s]"),
4457 objfile_name (dwarf2_per_objfile->objfile));
4458 continue;
4459 }
4460
4461 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
4462 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
4463 expansion_notify);
4464 }
4465 }
4466
4467 /* If FILE_MATCHER is non-NULL, set all the
4468 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4469 that match FILE_MATCHER. */
4470
4471 static void
4472 dw_expand_symtabs_matching_file_matcher
4473 (struct dwarf2_per_objfile *dwarf2_per_objfile,
4474 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
4475 {
4476 if (file_matcher == NULL)
4477 return;
4478
4479 objfile *const objfile = dwarf2_per_objfile->objfile;
4480
4481 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4482 htab_eq_pointer,
4483 NULL, xcalloc, xfree));
4484 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4485 htab_eq_pointer,
4486 NULL, xcalloc, xfree));
4487
4488 /* The rule is CUs specify all the files, including those used by
4489 any TU, so there's no need to scan TUs here. */
4490
4491 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4492 {
4493 QUIT;
4494
4495 per_cu->v.quick->mark = 0;
4496
4497 /* We only need to look at symtabs not already expanded. */
4498 if (per_cu->v.quick->compunit_symtab)
4499 continue;
4500
4501 quick_file_names *file_data = dw2_get_file_names (per_cu);
4502 if (file_data == NULL)
4503 continue;
4504
4505 if (htab_find (visited_not_found.get (), file_data) != NULL)
4506 continue;
4507 else if (htab_find (visited_found.get (), file_data) != NULL)
4508 {
4509 per_cu->v.quick->mark = 1;
4510 continue;
4511 }
4512
4513 for (int j = 0; j < file_data->num_file_names; ++j)
4514 {
4515 const char *this_real_name;
4516
4517 if (file_matcher (file_data->file_names[j], false))
4518 {
4519 per_cu->v.quick->mark = 1;
4520 break;
4521 }
4522
4523 /* Before we invoke realpath, which can get expensive when many
4524 files are involved, do a quick comparison of the basenames. */
4525 if (!basenames_may_differ
4526 && !file_matcher (lbasename (file_data->file_names[j]),
4527 true))
4528 continue;
4529
4530 this_real_name = dw2_get_real_path (objfile, file_data, j);
4531 if (file_matcher (this_real_name, false))
4532 {
4533 per_cu->v.quick->mark = 1;
4534 break;
4535 }
4536 }
4537
4538 void **slot = htab_find_slot (per_cu->v.quick->mark
4539 ? visited_found.get ()
4540 : visited_not_found.get (),
4541 file_data, INSERT);
4542 *slot = file_data;
4543 }
4544 }
4545
4546 static void
4547 dw2_expand_symtabs_matching
4548 (struct objfile *objfile,
4549 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4550 const lookup_name_info &lookup_name,
4551 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4552 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4553 enum search_domain kind)
4554 {
4555 struct dwarf2_per_objfile *dwarf2_per_objfile
4556 = get_dwarf2_per_objfile (objfile);
4557
4558 /* index_table is NULL if OBJF_READNOW. */
4559 if (!dwarf2_per_objfile->index_table)
4560 return;
4561
4562 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
4563
4564 mapped_index &index = *dwarf2_per_objfile->index_table;
4565
4566 dw2_expand_symtabs_matching_symbol (index, lookup_name,
4567 symbol_matcher,
4568 kind, [&] (offset_type idx)
4569 {
4570 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
4571 expansion_notify, kind);
4572 return true;
4573 });
4574 }
4575
4576 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4577 symtab. */
4578
4579 static struct compunit_symtab *
4580 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4581 CORE_ADDR pc)
4582 {
4583 int i;
4584
4585 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4586 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4587 return cust;
4588
4589 if (cust->includes == NULL)
4590 return NULL;
4591
4592 for (i = 0; cust->includes[i]; ++i)
4593 {
4594 struct compunit_symtab *s = cust->includes[i];
4595
4596 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4597 if (s != NULL)
4598 return s;
4599 }
4600
4601 return NULL;
4602 }
4603
4604 static struct compunit_symtab *
4605 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4606 struct bound_minimal_symbol msymbol,
4607 CORE_ADDR pc,
4608 struct obj_section *section,
4609 int warn_if_readin)
4610 {
4611 struct dwarf2_per_cu_data *data;
4612 struct compunit_symtab *result;
4613
4614 if (!objfile->partial_symtabs->psymtabs_addrmap)
4615 return NULL;
4616
4617 CORE_ADDR baseaddr = objfile->text_section_offset ();
4618 data = (struct dwarf2_per_cu_data *) addrmap_find
4619 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
4620 if (!data)
4621 return NULL;
4622
4623 if (warn_if_readin && data->v.quick->compunit_symtab)
4624 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4625 paddress (get_objfile_arch (objfile), pc));
4626
4627 result
4628 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data,
4629 false),
4630 pc);
4631 gdb_assert (result != NULL);
4632 return result;
4633 }
4634
4635 static void
4636 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4637 void *data, int need_fullname)
4638 {
4639 struct dwarf2_per_objfile *dwarf2_per_objfile
4640 = get_dwarf2_per_objfile (objfile);
4641
4642 if (!dwarf2_per_objfile->filenames_cache)
4643 {
4644 dwarf2_per_objfile->filenames_cache.emplace ();
4645
4646 htab_up visited (htab_create_alloc (10,
4647 htab_hash_pointer, htab_eq_pointer,
4648 NULL, xcalloc, xfree));
4649
4650 /* The rule is CUs specify all the files, including those used
4651 by any TU, so there's no need to scan TUs here. We can
4652 ignore file names coming from already-expanded CUs. */
4653
4654 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4655 {
4656 if (per_cu->v.quick->compunit_symtab)
4657 {
4658 void **slot = htab_find_slot (visited.get (),
4659 per_cu->v.quick->file_names,
4660 INSERT);
4661
4662 *slot = per_cu->v.quick->file_names;
4663 }
4664 }
4665
4666 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4667 {
4668 /* We only need to look at symtabs not already expanded. */
4669 if (per_cu->v.quick->compunit_symtab)
4670 continue;
4671
4672 quick_file_names *file_data = dw2_get_file_names (per_cu);
4673 if (file_data == NULL)
4674 continue;
4675
4676 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
4677 if (*slot)
4678 {
4679 /* Already visited. */
4680 continue;
4681 }
4682 *slot = file_data;
4683
4684 for (int j = 0; j < file_data->num_file_names; ++j)
4685 {
4686 const char *filename = file_data->file_names[j];
4687 dwarf2_per_objfile->filenames_cache->seen (filename);
4688 }
4689 }
4690 }
4691
4692 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
4693 {
4694 gdb::unique_xmalloc_ptr<char> this_real_name;
4695
4696 if (need_fullname)
4697 this_real_name = gdb_realpath (filename);
4698 (*fun) (filename, this_real_name.get (), data);
4699 });
4700 }
4701
4702 static int
4703 dw2_has_symbols (struct objfile *objfile)
4704 {
4705 return 1;
4706 }
4707
4708 const struct quick_symbol_functions dwarf2_gdb_index_functions =
4709 {
4710 dw2_has_symbols,
4711 dw2_find_last_source_symtab,
4712 dw2_forget_cached_source_info,
4713 dw2_map_symtabs_matching_filename,
4714 dw2_lookup_symbol,
4715 dw2_print_stats,
4716 dw2_dump,
4717 dw2_expand_symtabs_for_function,
4718 dw2_expand_all_symtabs,
4719 dw2_expand_symtabs_with_fullname,
4720 dw2_map_matching_symbols,
4721 dw2_expand_symtabs_matching,
4722 dw2_find_pc_sect_compunit_symtab,
4723 NULL,
4724 dw2_map_symbol_filenames
4725 };
4726
4727 /* DWARF-5 debug_names reader. */
4728
4729 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4730 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4731
4732 /* A helper function that reads the .debug_names section in SECTION
4733 and fills in MAP. FILENAME is the name of the file containing the
4734 section; it is used for error reporting.
4735
4736 Returns true if all went well, false otherwise. */
4737
4738 static bool
4739 read_debug_names_from_section (struct objfile *objfile,
4740 const char *filename,
4741 struct dwarf2_section_info *section,
4742 mapped_debug_names &map)
4743 {
4744 if (section->empty ())
4745 return false;
4746
4747 /* Older elfutils strip versions could keep the section in the main
4748 executable while splitting it for the separate debug info file. */
4749 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4750 return false;
4751
4752 section->read (objfile);
4753
4754 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
4755
4756 const gdb_byte *addr = section->buffer;
4757
4758 bfd *const abfd = section->get_bfd_owner ();
4759
4760 unsigned int bytes_read;
4761 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4762 addr += bytes_read;
4763
4764 map.dwarf5_is_dwarf64 = bytes_read != 4;
4765 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4766 if (bytes_read + length != section->size)
4767 {
4768 /* There may be multiple per-CU indices. */
4769 warning (_("Section .debug_names in %s length %s does not match "
4770 "section length %s, ignoring .debug_names."),
4771 filename, plongest (bytes_read + length),
4772 pulongest (section->size));
4773 return false;
4774 }
4775
4776 /* The version number. */
4777 uint16_t version = read_2_bytes (abfd, addr);
4778 addr += 2;
4779 if (version != 5)
4780 {
4781 warning (_("Section .debug_names in %s has unsupported version %d, "
4782 "ignoring .debug_names."),
4783 filename, version);
4784 return false;
4785 }
4786
4787 /* Padding. */
4788 uint16_t padding = read_2_bytes (abfd, addr);
4789 addr += 2;
4790 if (padding != 0)
4791 {
4792 warning (_("Section .debug_names in %s has unsupported padding %d, "
4793 "ignoring .debug_names."),
4794 filename, padding);
4795 return false;
4796 }
4797
4798 /* comp_unit_count - The number of CUs in the CU list. */
4799 map.cu_count = read_4_bytes (abfd, addr);
4800 addr += 4;
4801
4802 /* local_type_unit_count - The number of TUs in the local TU
4803 list. */
4804 map.tu_count = read_4_bytes (abfd, addr);
4805 addr += 4;
4806
4807 /* foreign_type_unit_count - The number of TUs in the foreign TU
4808 list. */
4809 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
4810 addr += 4;
4811 if (foreign_tu_count != 0)
4812 {
4813 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
4814 "ignoring .debug_names."),
4815 filename, static_cast<unsigned long> (foreign_tu_count));
4816 return false;
4817 }
4818
4819 /* bucket_count - The number of hash buckets in the hash lookup
4820 table. */
4821 map.bucket_count = read_4_bytes (abfd, addr);
4822 addr += 4;
4823
4824 /* name_count - The number of unique names in the index. */
4825 map.name_count = read_4_bytes (abfd, addr);
4826 addr += 4;
4827
4828 /* abbrev_table_size - The size in bytes of the abbreviations
4829 table. */
4830 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
4831 addr += 4;
4832
4833 /* augmentation_string_size - The size in bytes of the augmentation
4834 string. This value is rounded up to a multiple of 4. */
4835 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
4836 addr += 4;
4837 map.augmentation_is_gdb = ((augmentation_string_size
4838 == sizeof (dwarf5_augmentation))
4839 && memcmp (addr, dwarf5_augmentation,
4840 sizeof (dwarf5_augmentation)) == 0);
4841 augmentation_string_size += (-augmentation_string_size) & 3;
4842 addr += augmentation_string_size;
4843
4844 /* List of CUs */
4845 map.cu_table_reordered = addr;
4846 addr += map.cu_count * map.offset_size;
4847
4848 /* List of Local TUs */
4849 map.tu_table_reordered = addr;
4850 addr += map.tu_count * map.offset_size;
4851
4852 /* Hash Lookup Table */
4853 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4854 addr += map.bucket_count * 4;
4855 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4856 addr += map.name_count * 4;
4857
4858 /* Name Table */
4859 map.name_table_string_offs_reordered = addr;
4860 addr += map.name_count * map.offset_size;
4861 map.name_table_entry_offs_reordered = addr;
4862 addr += map.name_count * map.offset_size;
4863
4864 const gdb_byte *abbrev_table_start = addr;
4865 for (;;)
4866 {
4867 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
4868 addr += bytes_read;
4869 if (index_num == 0)
4870 break;
4871
4872 const auto insertpair
4873 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
4874 if (!insertpair.second)
4875 {
4876 warning (_("Section .debug_names in %s has duplicate index %s, "
4877 "ignoring .debug_names."),
4878 filename, pulongest (index_num));
4879 return false;
4880 }
4881 mapped_debug_names::index_val &indexval = insertpair.first->second;
4882 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
4883 addr += bytes_read;
4884
4885 for (;;)
4886 {
4887 mapped_debug_names::index_val::attr attr;
4888 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
4889 addr += bytes_read;
4890 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
4891 addr += bytes_read;
4892 if (attr.form == DW_FORM_implicit_const)
4893 {
4894 attr.implicit_const = read_signed_leb128 (abfd, addr,
4895 &bytes_read);
4896 addr += bytes_read;
4897 }
4898 if (attr.dw_idx == 0 && attr.form == 0)
4899 break;
4900 indexval.attr_vec.push_back (std::move (attr));
4901 }
4902 }
4903 if (addr != abbrev_table_start + abbrev_table_size)
4904 {
4905 warning (_("Section .debug_names in %s has abbreviation_table "
4906 "of size %s vs. written as %u, ignoring .debug_names."),
4907 filename, plongest (addr - abbrev_table_start),
4908 abbrev_table_size);
4909 return false;
4910 }
4911 map.entry_pool = addr;
4912
4913 return true;
4914 }
4915
4916 /* A helper for create_cus_from_debug_names that handles the MAP's CU
4917 list. */
4918
4919 static void
4920 create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
4921 const mapped_debug_names &map,
4922 dwarf2_section_info &section,
4923 bool is_dwz)
4924 {
4925 sect_offset sect_off_prev;
4926 for (uint32_t i = 0; i <= map.cu_count; ++i)
4927 {
4928 sect_offset sect_off_next;
4929 if (i < map.cu_count)
4930 {
4931 sect_off_next
4932 = (sect_offset) (extract_unsigned_integer
4933 (map.cu_table_reordered + i * map.offset_size,
4934 map.offset_size,
4935 map.dwarf5_byte_order));
4936 }
4937 else
4938 sect_off_next = (sect_offset) section.size;
4939 if (i >= 1)
4940 {
4941 const ULONGEST length = sect_off_next - sect_off_prev;
4942 dwarf2_per_cu_data *per_cu
4943 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
4944 sect_off_prev, length);
4945 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
4946 }
4947 sect_off_prev = sect_off_next;
4948 }
4949 }
4950
4951 /* Read the CU list from the mapped index, and use it to create all
4952 the CU objects for this dwarf2_per_objfile. */
4953
4954 static void
4955 create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
4956 const mapped_debug_names &map,
4957 const mapped_debug_names &dwz_map)
4958 {
4959 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
4960 dwarf2_per_objfile->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
4961
4962 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
4963 dwarf2_per_objfile->info,
4964 false /* is_dwz */);
4965
4966 if (dwz_map.cu_count == 0)
4967 return;
4968
4969 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4970 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
4971 true /* is_dwz */);
4972 }
4973
4974 /* Read .debug_names. If everything went ok, initialize the "quick"
4975 elements of all the CUs and return true. Otherwise, return false. */
4976
4977 static bool
4978 dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
4979 {
4980 std::unique_ptr<mapped_debug_names> map
4981 (new mapped_debug_names (dwarf2_per_objfile));
4982 mapped_debug_names dwz_map (dwarf2_per_objfile);
4983 struct objfile *objfile = dwarf2_per_objfile->objfile;
4984
4985 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
4986 &dwarf2_per_objfile->debug_names,
4987 *map))
4988 return false;
4989
4990 /* Don't use the index if it's empty. */
4991 if (map->name_count == 0)
4992 return false;
4993
4994 /* If there is a .dwz file, read it so we can get its CU list as
4995 well. */
4996 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4997 if (dwz != NULL)
4998 {
4999 if (!read_debug_names_from_section (objfile,
5000 bfd_get_filename (dwz->dwz_bfd.get ()),
5001 &dwz->debug_names, dwz_map))
5002 {
5003 warning (_("could not read '.debug_names' section from %s; skipping"),
5004 bfd_get_filename (dwz->dwz_bfd.get ()));
5005 return false;
5006 }
5007 }
5008
5009 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
5010
5011 if (map->tu_count != 0)
5012 {
5013 /* We can only handle a single .debug_types when we have an
5014 index. */
5015 if (dwarf2_per_objfile->types.size () != 1)
5016 return false;
5017
5018 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
5019
5020 create_signatured_type_table_from_debug_names
5021 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->abbrev);
5022 }
5023
5024 create_addrmap_from_aranges (dwarf2_per_objfile,
5025 &dwarf2_per_objfile->debug_aranges);
5026
5027 dwarf2_per_objfile->debug_names_table = std::move (map);
5028 dwarf2_per_objfile->using_index = 1;
5029 dwarf2_per_objfile->quick_file_names_table =
5030 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
5031
5032 return true;
5033 }
5034
5035 /* Type used to manage iterating over all CUs looking for a symbol for
5036 .debug_names. */
5037
5038 class dw2_debug_names_iterator
5039 {
5040 public:
5041 dw2_debug_names_iterator (const mapped_debug_names &map,
5042 gdb::optional<block_enum> block_index,
5043 domain_enum domain,
5044 const char *name)
5045 : m_map (map), m_block_index (block_index), m_domain (domain),
5046 m_addr (find_vec_in_debug_names (map, name))
5047 {}
5048
5049 dw2_debug_names_iterator (const mapped_debug_names &map,
5050 search_domain search, uint32_t namei)
5051 : m_map (map),
5052 m_search (search),
5053 m_addr (find_vec_in_debug_names (map, namei))
5054 {}
5055
5056 dw2_debug_names_iterator (const mapped_debug_names &map,
5057 block_enum block_index, domain_enum domain,
5058 uint32_t namei)
5059 : m_map (map), m_block_index (block_index), m_domain (domain),
5060 m_addr (find_vec_in_debug_names (map, namei))
5061 {}
5062
5063 /* Return the next matching CU or NULL if there are no more. */
5064 dwarf2_per_cu_data *next ();
5065
5066 private:
5067 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5068 const char *name);
5069 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5070 uint32_t namei);
5071
5072 /* The internalized form of .debug_names. */
5073 const mapped_debug_names &m_map;
5074
5075 /* If set, only look for symbols that match that block. Valid values are
5076 GLOBAL_BLOCK and STATIC_BLOCK. */
5077 const gdb::optional<block_enum> m_block_index;
5078
5079 /* The kind of symbol we're looking for. */
5080 const domain_enum m_domain = UNDEF_DOMAIN;
5081 const search_domain m_search = ALL_DOMAIN;
5082
5083 /* The list of CUs from the index entry of the symbol, or NULL if
5084 not found. */
5085 const gdb_byte *m_addr;
5086 };
5087
5088 const char *
5089 mapped_debug_names::namei_to_name (uint32_t namei) const
5090 {
5091 const ULONGEST namei_string_offs
5092 = extract_unsigned_integer ((name_table_string_offs_reordered
5093 + namei * offset_size),
5094 offset_size,
5095 dwarf5_byte_order);
5096 return read_indirect_string_at_offset (dwarf2_per_objfile,
5097 namei_string_offs);
5098 }
5099
5100 /* Find a slot in .debug_names for the object named NAME. If NAME is
5101 found, return pointer to its pool data. If NAME cannot be found,
5102 return NULL. */
5103
5104 const gdb_byte *
5105 dw2_debug_names_iterator::find_vec_in_debug_names
5106 (const mapped_debug_names &map, const char *name)
5107 {
5108 int (*cmp) (const char *, const char *);
5109
5110 gdb::unique_xmalloc_ptr<char> without_params;
5111 if (current_language->la_language == language_cplus
5112 || current_language->la_language == language_fortran
5113 || current_language->la_language == language_d)
5114 {
5115 /* NAME is already canonical. Drop any qualifiers as
5116 .debug_names does not contain any. */
5117
5118 if (strchr (name, '(') != NULL)
5119 {
5120 without_params = cp_remove_params (name);
5121 if (without_params != NULL)
5122 name = without_params.get ();
5123 }
5124 }
5125
5126 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5127
5128 const uint32_t full_hash = dwarf5_djb_hash (name);
5129 uint32_t namei
5130 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5131 (map.bucket_table_reordered
5132 + (full_hash % map.bucket_count)), 4,
5133 map.dwarf5_byte_order);
5134 if (namei == 0)
5135 return NULL;
5136 --namei;
5137 if (namei >= map.name_count)
5138 {
5139 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5140 "[in module %s]"),
5141 namei, map.name_count,
5142 objfile_name (map.dwarf2_per_objfile->objfile));
5143 return NULL;
5144 }
5145
5146 for (;;)
5147 {
5148 const uint32_t namei_full_hash
5149 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5150 (map.hash_table_reordered + namei), 4,
5151 map.dwarf5_byte_order);
5152 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5153 return NULL;
5154
5155 if (full_hash == namei_full_hash)
5156 {
5157 const char *const namei_string = map.namei_to_name (namei);
5158
5159 #if 0 /* An expensive sanity check. */
5160 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5161 {
5162 complaint (_("Wrong .debug_names hash for string at index %u "
5163 "[in module %s]"),
5164 namei, objfile_name (dwarf2_per_objfile->objfile));
5165 return NULL;
5166 }
5167 #endif
5168
5169 if (cmp (namei_string, name) == 0)
5170 {
5171 const ULONGEST namei_entry_offs
5172 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5173 + namei * map.offset_size),
5174 map.offset_size, map.dwarf5_byte_order);
5175 return map.entry_pool + namei_entry_offs;
5176 }
5177 }
5178
5179 ++namei;
5180 if (namei >= map.name_count)
5181 return NULL;
5182 }
5183 }
5184
5185 const gdb_byte *
5186 dw2_debug_names_iterator::find_vec_in_debug_names
5187 (const mapped_debug_names &map, uint32_t namei)
5188 {
5189 if (namei >= map.name_count)
5190 {
5191 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5192 "[in module %s]"),
5193 namei, map.name_count,
5194 objfile_name (map.dwarf2_per_objfile->objfile));
5195 return NULL;
5196 }
5197
5198 const ULONGEST namei_entry_offs
5199 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5200 + namei * map.offset_size),
5201 map.offset_size, map.dwarf5_byte_order);
5202 return map.entry_pool + namei_entry_offs;
5203 }
5204
5205 /* See dw2_debug_names_iterator. */
5206
5207 dwarf2_per_cu_data *
5208 dw2_debug_names_iterator::next ()
5209 {
5210 if (m_addr == NULL)
5211 return NULL;
5212
5213 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5214 struct objfile *objfile = dwarf2_per_objfile->objfile;
5215 bfd *const abfd = objfile->obfd;
5216
5217 again:
5218
5219 unsigned int bytes_read;
5220 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5221 m_addr += bytes_read;
5222 if (abbrev == 0)
5223 return NULL;
5224
5225 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5226 if (indexval_it == m_map.abbrev_map.cend ())
5227 {
5228 complaint (_("Wrong .debug_names undefined abbrev code %s "
5229 "[in module %s]"),
5230 pulongest (abbrev), objfile_name (objfile));
5231 return NULL;
5232 }
5233 const mapped_debug_names::index_val &indexval = indexval_it->second;
5234 enum class symbol_linkage {
5235 unknown,
5236 static_,
5237 extern_,
5238 } symbol_linkage_ = symbol_linkage::unknown;
5239 dwarf2_per_cu_data *per_cu = NULL;
5240 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5241 {
5242 ULONGEST ull;
5243 switch (attr.form)
5244 {
5245 case DW_FORM_implicit_const:
5246 ull = attr.implicit_const;
5247 break;
5248 case DW_FORM_flag_present:
5249 ull = 1;
5250 break;
5251 case DW_FORM_udata:
5252 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5253 m_addr += bytes_read;
5254 break;
5255 default:
5256 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5257 dwarf_form_name (attr.form),
5258 objfile_name (objfile));
5259 return NULL;
5260 }
5261 switch (attr.dw_idx)
5262 {
5263 case DW_IDX_compile_unit:
5264 /* Don't crash on bad data. */
5265 if (ull >= dwarf2_per_objfile->all_comp_units.size ())
5266 {
5267 complaint (_(".debug_names entry has bad CU index %s"
5268 " [in module %s]"),
5269 pulongest (ull),
5270 objfile_name (dwarf2_per_objfile->objfile));
5271 continue;
5272 }
5273 per_cu = dwarf2_per_objfile->get_cutu (ull);
5274 break;
5275 case DW_IDX_type_unit:
5276 /* Don't crash on bad data. */
5277 if (ull >= dwarf2_per_objfile->all_type_units.size ())
5278 {
5279 complaint (_(".debug_names entry has bad TU index %s"
5280 " [in module %s]"),
5281 pulongest (ull),
5282 objfile_name (dwarf2_per_objfile->objfile));
5283 continue;
5284 }
5285 per_cu = &dwarf2_per_objfile->get_tu (ull)->per_cu;
5286 break;
5287 case DW_IDX_GNU_internal:
5288 if (!m_map.augmentation_is_gdb)
5289 break;
5290 symbol_linkage_ = symbol_linkage::static_;
5291 break;
5292 case DW_IDX_GNU_external:
5293 if (!m_map.augmentation_is_gdb)
5294 break;
5295 symbol_linkage_ = symbol_linkage::extern_;
5296 break;
5297 }
5298 }
5299
5300 /* Skip if already read in. */
5301 if (per_cu->v.quick->compunit_symtab)
5302 goto again;
5303
5304 /* Check static vs global. */
5305 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
5306 {
5307 const bool want_static = *m_block_index == STATIC_BLOCK;
5308 const bool symbol_is_static =
5309 symbol_linkage_ == symbol_linkage::static_;
5310 if (want_static != symbol_is_static)
5311 goto again;
5312 }
5313
5314 /* Match dw2_symtab_iter_next, symbol_kind
5315 and debug_names::psymbol_tag. */
5316 switch (m_domain)
5317 {
5318 case VAR_DOMAIN:
5319 switch (indexval.dwarf_tag)
5320 {
5321 case DW_TAG_variable:
5322 case DW_TAG_subprogram:
5323 /* Some types are also in VAR_DOMAIN. */
5324 case DW_TAG_typedef:
5325 case DW_TAG_structure_type:
5326 break;
5327 default:
5328 goto again;
5329 }
5330 break;
5331 case STRUCT_DOMAIN:
5332 switch (indexval.dwarf_tag)
5333 {
5334 case DW_TAG_typedef:
5335 case DW_TAG_structure_type:
5336 break;
5337 default:
5338 goto again;
5339 }
5340 break;
5341 case LABEL_DOMAIN:
5342 switch (indexval.dwarf_tag)
5343 {
5344 case 0:
5345 case DW_TAG_variable:
5346 break;
5347 default:
5348 goto again;
5349 }
5350 break;
5351 case MODULE_DOMAIN:
5352 switch (indexval.dwarf_tag)
5353 {
5354 case DW_TAG_module:
5355 break;
5356 default:
5357 goto again;
5358 }
5359 break;
5360 default:
5361 break;
5362 }
5363
5364 /* Match dw2_expand_symtabs_matching, symbol_kind and
5365 debug_names::psymbol_tag. */
5366 switch (m_search)
5367 {
5368 case VARIABLES_DOMAIN:
5369 switch (indexval.dwarf_tag)
5370 {
5371 case DW_TAG_variable:
5372 break;
5373 default:
5374 goto again;
5375 }
5376 break;
5377 case FUNCTIONS_DOMAIN:
5378 switch (indexval.dwarf_tag)
5379 {
5380 case DW_TAG_subprogram:
5381 break;
5382 default:
5383 goto again;
5384 }
5385 break;
5386 case TYPES_DOMAIN:
5387 switch (indexval.dwarf_tag)
5388 {
5389 case DW_TAG_typedef:
5390 case DW_TAG_structure_type:
5391 break;
5392 default:
5393 goto again;
5394 }
5395 break;
5396 case MODULES_DOMAIN:
5397 switch (indexval.dwarf_tag)
5398 {
5399 case DW_TAG_module:
5400 break;
5401 default:
5402 goto again;
5403 }
5404 default:
5405 break;
5406 }
5407
5408 return per_cu;
5409 }
5410
5411 static struct compunit_symtab *
5412 dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
5413 const char *name, domain_enum domain)
5414 {
5415 struct dwarf2_per_objfile *dwarf2_per_objfile
5416 = get_dwarf2_per_objfile (objfile);
5417
5418 const auto &mapp = dwarf2_per_objfile->debug_names_table;
5419 if (!mapp)
5420 {
5421 /* index is NULL if OBJF_READNOW. */
5422 return NULL;
5423 }
5424 const auto &map = *mapp;
5425
5426 dw2_debug_names_iterator iter (map, block_index, domain, name);
5427
5428 struct compunit_symtab *stab_best = NULL;
5429 struct dwarf2_per_cu_data *per_cu;
5430 while ((per_cu = iter.next ()) != NULL)
5431 {
5432 struct symbol *sym, *with_opaque = NULL;
5433 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
5434 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
5435 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
5436
5437 sym = block_find_symbol (block, name, domain,
5438 block_find_non_opaque_type_preferred,
5439 &with_opaque);
5440
5441 /* Some caution must be observed with overloaded functions and
5442 methods, since the index will not contain any overload
5443 information (but NAME might contain it). */
5444
5445 if (sym != NULL
5446 && strcmp_iw (sym->search_name (), name) == 0)
5447 return stab;
5448 if (with_opaque != NULL
5449 && strcmp_iw (with_opaque->search_name (), name) == 0)
5450 stab_best = stab;
5451
5452 /* Keep looking through other CUs. */
5453 }
5454
5455 return stab_best;
5456 }
5457
5458 /* This dumps minimal information about .debug_names. It is called
5459 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5460 uses this to verify that .debug_names has been loaded. */
5461
5462 static void
5463 dw2_debug_names_dump (struct objfile *objfile)
5464 {
5465 struct dwarf2_per_objfile *dwarf2_per_objfile
5466 = get_dwarf2_per_objfile (objfile);
5467
5468 gdb_assert (dwarf2_per_objfile->using_index);
5469 printf_filtered (".debug_names:");
5470 if (dwarf2_per_objfile->debug_names_table)
5471 printf_filtered (" exists\n");
5472 else
5473 printf_filtered (" faked for \"readnow\"\n");
5474 printf_filtered ("\n");
5475 }
5476
5477 static void
5478 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5479 const char *func_name)
5480 {
5481 struct dwarf2_per_objfile *dwarf2_per_objfile
5482 = get_dwarf2_per_objfile (objfile);
5483
5484 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
5485 if (dwarf2_per_objfile->debug_names_table)
5486 {
5487 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5488
5489 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
5490
5491 struct dwarf2_per_cu_data *per_cu;
5492 while ((per_cu = iter.next ()) != NULL)
5493 dw2_instantiate_symtab (per_cu, false);
5494 }
5495 }
5496
5497 static void
5498 dw2_debug_names_map_matching_symbols
5499 (struct objfile *objfile,
5500 const lookup_name_info &name, domain_enum domain,
5501 int global,
5502 gdb::function_view<symbol_found_callback_ftype> callback,
5503 symbol_compare_ftype *ordered_compare)
5504 {
5505 struct dwarf2_per_objfile *dwarf2_per_objfile
5506 = get_dwarf2_per_objfile (objfile);
5507
5508 /* debug_names_table is NULL if OBJF_READNOW. */
5509 if (!dwarf2_per_objfile->debug_names_table)
5510 return;
5511
5512 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5513 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5514
5515 const char *match_name = name.ada ().lookup_name ().c_str ();
5516 auto matcher = [&] (const char *symname)
5517 {
5518 if (ordered_compare == nullptr)
5519 return true;
5520 return ordered_compare (symname, match_name) == 0;
5521 };
5522
5523 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5524 [&] (offset_type namei)
5525 {
5526 /* The name was matched, now expand corresponding CUs that were
5527 marked. */
5528 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
5529
5530 struct dwarf2_per_cu_data *per_cu;
5531 while ((per_cu = iter.next ()) != NULL)
5532 dw2_expand_symtabs_matching_one (per_cu, nullptr, nullptr);
5533 return true;
5534 });
5535
5536 /* It's a shame we couldn't do this inside the
5537 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5538 that have already been expanded. Instead, this loop matches what
5539 the psymtab code does. */
5540 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5541 {
5542 struct compunit_symtab *cust = per_cu->v.quick->compunit_symtab;
5543 if (cust != nullptr)
5544 {
5545 const struct block *block
5546 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
5547 if (!iterate_over_symbols_terminated (block, name,
5548 domain, callback))
5549 break;
5550 }
5551 }
5552 }
5553
5554 static void
5555 dw2_debug_names_expand_symtabs_matching
5556 (struct objfile *objfile,
5557 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5558 const lookup_name_info &lookup_name,
5559 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5560 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5561 enum search_domain kind)
5562 {
5563 struct dwarf2_per_objfile *dwarf2_per_objfile
5564 = get_dwarf2_per_objfile (objfile);
5565
5566 /* debug_names_table is NULL if OBJF_READNOW. */
5567 if (!dwarf2_per_objfile->debug_names_table)
5568 return;
5569
5570 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
5571
5572 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5573
5574 dw2_expand_symtabs_matching_symbol (map, lookup_name,
5575 symbol_matcher,
5576 kind, [&] (offset_type namei)
5577 {
5578 /* The name was matched, now expand corresponding CUs that were
5579 marked. */
5580 dw2_debug_names_iterator iter (map, kind, namei);
5581
5582 struct dwarf2_per_cu_data *per_cu;
5583 while ((per_cu = iter.next ()) != NULL)
5584 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5585 expansion_notify);
5586 return true;
5587 });
5588 }
5589
5590 const struct quick_symbol_functions dwarf2_debug_names_functions =
5591 {
5592 dw2_has_symbols,
5593 dw2_find_last_source_symtab,
5594 dw2_forget_cached_source_info,
5595 dw2_map_symtabs_matching_filename,
5596 dw2_debug_names_lookup_symbol,
5597 dw2_print_stats,
5598 dw2_debug_names_dump,
5599 dw2_debug_names_expand_symtabs_for_function,
5600 dw2_expand_all_symtabs,
5601 dw2_expand_symtabs_with_fullname,
5602 dw2_debug_names_map_matching_symbols,
5603 dw2_debug_names_expand_symtabs_matching,
5604 dw2_find_pc_sect_compunit_symtab,
5605 NULL,
5606 dw2_map_symbol_filenames
5607 };
5608
5609 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5610 to either a dwarf2_per_objfile or dwz_file object. */
5611
5612 template <typename T>
5613 static gdb::array_view<const gdb_byte>
5614 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5615 {
5616 dwarf2_section_info *section = &section_owner->gdb_index;
5617
5618 if (section->empty ())
5619 return {};
5620
5621 /* Older elfutils strip versions could keep the section in the main
5622 executable while splitting it for the separate debug info file. */
5623 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5624 return {};
5625
5626 section->read (obj);
5627
5628 /* dwarf2_section_info::size is a bfd_size_type, while
5629 gdb::array_view works with size_t. On 32-bit hosts, with
5630 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5631 is 32-bit. So we need an explicit narrowing conversion here.
5632 This is fine, because it's impossible to allocate or mmap an
5633 array/buffer larger than what size_t can represent. */
5634 return gdb::make_array_view (section->buffer, section->size);
5635 }
5636
5637 /* Lookup the index cache for the contents of the index associated to
5638 DWARF2_OBJ. */
5639
5640 static gdb::array_view<const gdb_byte>
5641 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_objfile *dwarf2_obj)
5642 {
5643 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5644 if (build_id == nullptr)
5645 return {};
5646
5647 return global_index_cache.lookup_gdb_index (build_id,
5648 &dwarf2_obj->index_cache_res);
5649 }
5650
5651 /* Same as the above, but for DWZ. */
5652
5653 static gdb::array_view<const gdb_byte>
5654 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5655 {
5656 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5657 if (build_id == nullptr)
5658 return {};
5659
5660 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5661 }
5662
5663 /* See symfile.h. */
5664
5665 bool
5666 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
5667 {
5668 struct dwarf2_per_objfile *dwarf2_per_objfile
5669 = get_dwarf2_per_objfile (objfile);
5670
5671 /* If we're about to read full symbols, don't bother with the
5672 indices. In this case we also don't care if some other debug
5673 format is making psymtabs, because they are all about to be
5674 expanded anyway. */
5675 if ((objfile->flags & OBJF_READNOW))
5676 {
5677 dwarf2_per_objfile->using_index = 1;
5678 create_all_comp_units (dwarf2_per_objfile);
5679 create_all_type_units (dwarf2_per_objfile);
5680 dwarf2_per_objfile->quick_file_names_table
5681 = create_quick_file_names_table
5682 (dwarf2_per_objfile->all_comp_units.size ());
5683
5684 for (int i = 0; i < (dwarf2_per_objfile->all_comp_units.size ()
5685 + dwarf2_per_objfile->all_type_units.size ()); ++i)
5686 {
5687 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
5688
5689 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5690 struct dwarf2_per_cu_quick_data);
5691 }
5692
5693 /* Return 1 so that gdb sees the "quick" functions. However,
5694 these functions will be no-ops because we will have expanded
5695 all symtabs. */
5696 *index_kind = dw_index_kind::GDB_INDEX;
5697 return true;
5698 }
5699
5700 if (dwarf2_read_debug_names (dwarf2_per_objfile))
5701 {
5702 *index_kind = dw_index_kind::DEBUG_NAMES;
5703 return true;
5704 }
5705
5706 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5707 get_gdb_index_contents_from_section<struct dwarf2_per_objfile>,
5708 get_gdb_index_contents_from_section<dwz_file>))
5709 {
5710 *index_kind = dw_index_kind::GDB_INDEX;
5711 return true;
5712 }
5713
5714 /* ... otherwise, try to find the index in the index cache. */
5715 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5716 get_gdb_index_contents_from_cache,
5717 get_gdb_index_contents_from_cache_dwz))
5718 {
5719 global_index_cache.hit ();
5720 *index_kind = dw_index_kind::GDB_INDEX;
5721 return true;
5722 }
5723
5724 global_index_cache.miss ();
5725 return false;
5726 }
5727
5728 \f
5729
5730 /* Build a partial symbol table. */
5731
5732 void
5733 dwarf2_build_psymtabs (struct objfile *objfile)
5734 {
5735 struct dwarf2_per_objfile *dwarf2_per_objfile
5736 = get_dwarf2_per_objfile (objfile);
5737
5738 init_psymbol_list (objfile, 1024);
5739
5740 try
5741 {
5742 /* This isn't really ideal: all the data we allocate on the
5743 objfile's obstack is still uselessly kept around. However,
5744 freeing it seems unsafe. */
5745 psymtab_discarder psymtabs (objfile);
5746 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
5747 psymtabs.keep ();
5748
5749 /* (maybe) store an index in the cache. */
5750 global_index_cache.store (dwarf2_per_objfile);
5751 }
5752 catch (const gdb_exception_error &except)
5753 {
5754 exception_print (gdb_stderr, except);
5755 }
5756 }
5757
5758 /* Find the base address of the compilation unit for range lists and
5759 location lists. It will normally be specified by DW_AT_low_pc.
5760 In DWARF-3 draft 4, the base address could be overridden by
5761 DW_AT_entry_pc. It's been removed, but GCC still uses this for
5762 compilation units with discontinuous ranges. */
5763
5764 static void
5765 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
5766 {
5767 struct attribute *attr;
5768
5769 cu->base_address.reset ();
5770
5771 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
5772 if (attr != nullptr)
5773 cu->base_address = attr->value_as_address ();
5774 else
5775 {
5776 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5777 if (attr != nullptr)
5778 cu->base_address = attr->value_as_address ();
5779 }
5780 }
5781
5782 /* Helper function that returns the proper abbrev section for
5783 THIS_CU. */
5784
5785 static struct dwarf2_section_info *
5786 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
5787 {
5788 struct dwarf2_section_info *abbrev;
5789 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
5790
5791 if (this_cu->is_dwz)
5792 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
5793 else
5794 abbrev = &dwarf2_per_objfile->abbrev;
5795
5796 return abbrev;
5797 }
5798
5799 /* Fetch the abbreviation table offset from a comp or type unit header. */
5800
5801 static sect_offset
5802 read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
5803 struct dwarf2_section_info *section,
5804 sect_offset sect_off)
5805 {
5806 bfd *abfd = section->get_bfd_owner ();
5807 const gdb_byte *info_ptr;
5808 unsigned int initial_length_size, offset_size;
5809 uint16_t version;
5810
5811 section->read (dwarf2_per_objfile->objfile);
5812 info_ptr = section->buffer + to_underlying (sect_off);
5813 read_initial_length (abfd, info_ptr, &initial_length_size);
5814 offset_size = initial_length_size == 4 ? 4 : 8;
5815 info_ptr += initial_length_size;
5816
5817 version = read_2_bytes (abfd, info_ptr);
5818 info_ptr += 2;
5819 if (version >= 5)
5820 {
5821 /* Skip unit type and address size. */
5822 info_ptr += 2;
5823 }
5824
5825 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
5826 }
5827
5828 /* A partial symtab that is used only for include files. */
5829 struct dwarf2_include_psymtab : public partial_symtab
5830 {
5831 dwarf2_include_psymtab (const char *filename, struct objfile *objfile)
5832 : partial_symtab (filename, objfile)
5833 {
5834 }
5835
5836 void read_symtab (struct objfile *objfile) override
5837 {
5838 expand_psymtab (objfile);
5839 }
5840
5841 void expand_psymtab (struct objfile *objfile) override
5842 {
5843 if (m_readin)
5844 return;
5845 /* It's an include file, no symbols to read for it.
5846 Everything is in the parent symtab. */
5847 read_dependencies (objfile);
5848 m_readin = true;
5849 }
5850
5851 bool readin_p () const override
5852 {
5853 return m_readin;
5854 }
5855
5856 struct compunit_symtab *get_compunit_symtab () const override
5857 {
5858 return nullptr;
5859 }
5860
5861 private:
5862
5863 bool m_readin = false;
5864 };
5865
5866 /* Allocate a new partial symtab for file named NAME and mark this new
5867 partial symtab as being an include of PST. */
5868
5869 static void
5870 dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
5871 struct objfile *objfile)
5872 {
5873 dwarf2_include_psymtab *subpst = new dwarf2_include_psymtab (name, objfile);
5874
5875 if (!IS_ABSOLUTE_PATH (subpst->filename))
5876 {
5877 /* It shares objfile->objfile_obstack. */
5878 subpst->dirname = pst->dirname;
5879 }
5880
5881 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
5882 subpst->dependencies[0] = pst;
5883 subpst->number_of_dependencies = 1;
5884 }
5885
5886 /* Read the Line Number Program data and extract the list of files
5887 included by the source file represented by PST. Build an include
5888 partial symtab for each of these included files. */
5889
5890 static void
5891 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
5892 struct die_info *die,
5893 dwarf2_psymtab *pst)
5894 {
5895 line_header_up lh;
5896 struct attribute *attr;
5897
5898 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5899 if (attr != nullptr)
5900 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
5901 if (lh == NULL)
5902 return; /* No linetable, so no includes. */
5903
5904 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
5905 that we pass in the raw text_low here; that is ok because we're
5906 only decoding the line table to make include partial symtabs, and
5907 so the addresses aren't really used. */
5908 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
5909 pst->raw_text_low (), 1);
5910 }
5911
5912 static hashval_t
5913 hash_signatured_type (const void *item)
5914 {
5915 const struct signatured_type *sig_type
5916 = (const struct signatured_type *) item;
5917
5918 /* This drops the top 32 bits of the signature, but is ok for a hash. */
5919 return sig_type->signature;
5920 }
5921
5922 static int
5923 eq_signatured_type (const void *item_lhs, const void *item_rhs)
5924 {
5925 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
5926 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
5927
5928 return lhs->signature == rhs->signature;
5929 }
5930
5931 /* Allocate a hash table for signatured types. */
5932
5933 static htab_up
5934 allocate_signatured_type_table ()
5935 {
5936 return htab_up (htab_create_alloc (41,
5937 hash_signatured_type,
5938 eq_signatured_type,
5939 NULL, xcalloc, xfree));
5940 }
5941
5942 /* A helper function to add a signatured type CU to a table. */
5943
5944 static int
5945 add_signatured_type_cu_to_table (void **slot, void *datum)
5946 {
5947 struct signatured_type *sigt = (struct signatured_type *) *slot;
5948 std::vector<signatured_type *> *all_type_units
5949 = (std::vector<signatured_type *> *) datum;
5950
5951 all_type_units->push_back (sigt);
5952
5953 return 1;
5954 }
5955
5956 /* A helper for create_debug_types_hash_table. Read types from SECTION
5957 and fill them into TYPES_HTAB. It will process only type units,
5958 therefore DW_UT_type. */
5959
5960 static void
5961 create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
5962 struct dwo_file *dwo_file,
5963 dwarf2_section_info *section, htab_up &types_htab,
5964 rcuh_kind section_kind)
5965 {
5966 struct objfile *objfile = dwarf2_per_objfile->objfile;
5967 struct dwarf2_section_info *abbrev_section;
5968 bfd *abfd;
5969 const gdb_byte *info_ptr, *end_ptr;
5970
5971 abbrev_section = (dwo_file != NULL
5972 ? &dwo_file->sections.abbrev
5973 : &dwarf2_per_objfile->abbrev);
5974
5975 if (dwarf_read_debug)
5976 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
5977 section->get_name (),
5978 abbrev_section->get_file_name ());
5979
5980 section->read (objfile);
5981 info_ptr = section->buffer;
5982
5983 if (info_ptr == NULL)
5984 return;
5985
5986 /* We can't set abfd until now because the section may be empty or
5987 not present, in which case the bfd is unknown. */
5988 abfd = section->get_bfd_owner ();
5989
5990 /* We don't use cutu_reader here because we don't need to read
5991 any dies: the signature is in the header. */
5992
5993 end_ptr = info_ptr + section->size;
5994 while (info_ptr < end_ptr)
5995 {
5996 struct signatured_type *sig_type;
5997 struct dwo_unit *dwo_tu;
5998 void **slot;
5999 const gdb_byte *ptr = info_ptr;
6000 struct comp_unit_head header;
6001 unsigned int length;
6002
6003 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6004
6005 /* Initialize it due to a false compiler warning. */
6006 header.signature = -1;
6007 header.type_cu_offset_in_tu = (cu_offset) -1;
6008
6009 /* We need to read the type's signature in order to build the hash
6010 table, but we don't need anything else just yet. */
6011
6012 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
6013 abbrev_section, ptr, section_kind);
6014
6015 length = header.get_length ();
6016
6017 /* Skip dummy type units. */
6018 if (ptr >= info_ptr + length
6019 || peek_abbrev_code (abfd, ptr) == 0
6020 || header.unit_type != DW_UT_type)
6021 {
6022 info_ptr += length;
6023 continue;
6024 }
6025
6026 if (types_htab == NULL)
6027 {
6028 if (dwo_file)
6029 types_htab = allocate_dwo_unit_table ();
6030 else
6031 types_htab = allocate_signatured_type_table ();
6032 }
6033
6034 if (dwo_file)
6035 {
6036 sig_type = NULL;
6037 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6038 struct dwo_unit);
6039 dwo_tu->dwo_file = dwo_file;
6040 dwo_tu->signature = header.signature;
6041 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6042 dwo_tu->section = section;
6043 dwo_tu->sect_off = sect_off;
6044 dwo_tu->length = length;
6045 }
6046 else
6047 {
6048 /* N.B.: type_offset is not usable if this type uses a DWO file.
6049 The real type_offset is in the DWO file. */
6050 dwo_tu = NULL;
6051 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6052 struct signatured_type);
6053 sig_type->signature = header.signature;
6054 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6055 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6056 sig_type->per_cu.is_debug_types = 1;
6057 sig_type->per_cu.section = section;
6058 sig_type->per_cu.sect_off = sect_off;
6059 sig_type->per_cu.length = length;
6060 }
6061
6062 slot = htab_find_slot (types_htab.get (),
6063 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6064 INSERT);
6065 gdb_assert (slot != NULL);
6066 if (*slot != NULL)
6067 {
6068 sect_offset dup_sect_off;
6069
6070 if (dwo_file)
6071 {
6072 const struct dwo_unit *dup_tu
6073 = (const struct dwo_unit *) *slot;
6074
6075 dup_sect_off = dup_tu->sect_off;
6076 }
6077 else
6078 {
6079 const struct signatured_type *dup_tu
6080 = (const struct signatured_type *) *slot;
6081
6082 dup_sect_off = dup_tu->per_cu.sect_off;
6083 }
6084
6085 complaint (_("debug type entry at offset %s is duplicate to"
6086 " the entry at offset %s, signature %s"),
6087 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
6088 hex_string (header.signature));
6089 }
6090 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
6091
6092 if (dwarf_read_debug > 1)
6093 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6094 sect_offset_str (sect_off),
6095 hex_string (header.signature));
6096
6097 info_ptr += length;
6098 }
6099 }
6100
6101 /* Create the hash table of all entries in the .debug_types
6102 (or .debug_types.dwo) section(s).
6103 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6104 otherwise it is NULL.
6105
6106 The result is a pointer to the hash table or NULL if there are no types.
6107
6108 Note: This function processes DWO files only, not DWP files. */
6109
6110 static void
6111 create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6112 struct dwo_file *dwo_file,
6113 gdb::array_view<dwarf2_section_info> type_sections,
6114 htab_up &types_htab)
6115 {
6116 for (dwarf2_section_info &section : type_sections)
6117 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
6118 types_htab, rcuh_kind::TYPE);
6119 }
6120
6121 /* Create the hash table of all entries in the .debug_types section,
6122 and initialize all_type_units.
6123 The result is zero if there is an error (e.g. missing .debug_types section),
6124 otherwise non-zero. */
6125
6126 static int
6127 create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6128 {
6129 htab_up types_htab;
6130
6131 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6132 &dwarf2_per_objfile->info, types_htab,
6133 rcuh_kind::COMPILE);
6134 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6135 dwarf2_per_objfile->types, types_htab);
6136 if (types_htab == NULL)
6137 {
6138 dwarf2_per_objfile->signatured_types = NULL;
6139 return 0;
6140 }
6141
6142 dwarf2_per_objfile->signatured_types = std::move (types_htab);
6143
6144 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
6145 dwarf2_per_objfile->all_type_units.reserve
6146 (htab_elements (dwarf2_per_objfile->signatured_types.get ()));
6147
6148 htab_traverse_noresize (dwarf2_per_objfile->signatured_types.get (),
6149 add_signatured_type_cu_to_table,
6150 &dwarf2_per_objfile->all_type_units);
6151
6152 return 1;
6153 }
6154
6155 /* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6156 If SLOT is non-NULL, it is the entry to use in the hash table.
6157 Otherwise we find one. */
6158
6159 static struct signatured_type *
6160 add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6161 void **slot)
6162 {
6163 struct objfile *objfile = dwarf2_per_objfile->objfile;
6164
6165 if (dwarf2_per_objfile->all_type_units.size ()
6166 == dwarf2_per_objfile->all_type_units.capacity ())
6167 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
6168
6169 signatured_type *sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6170 struct signatured_type);
6171
6172 dwarf2_per_objfile->all_type_units.push_back (sig_type);
6173 sig_type->signature = sig;
6174 sig_type->per_cu.is_debug_types = 1;
6175 if (dwarf2_per_objfile->using_index)
6176 {
6177 sig_type->per_cu.v.quick =
6178 OBSTACK_ZALLOC (&objfile->objfile_obstack,
6179 struct dwarf2_per_cu_quick_data);
6180 }
6181
6182 if (slot == NULL)
6183 {
6184 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6185 sig_type, INSERT);
6186 }
6187 gdb_assert (*slot == NULL);
6188 *slot = sig_type;
6189 /* The rest of sig_type must be filled in by the caller. */
6190 return sig_type;
6191 }
6192
6193 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6194 Fill in SIG_ENTRY with DWO_ENTRY. */
6195
6196 static void
6197 fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
6198 struct signatured_type *sig_entry,
6199 struct dwo_unit *dwo_entry)
6200 {
6201 /* Make sure we're not clobbering something we don't expect to. */
6202 gdb_assert (! sig_entry->per_cu.queued);
6203 gdb_assert (sig_entry->per_cu.cu == NULL);
6204 if (dwarf2_per_objfile->using_index)
6205 {
6206 gdb_assert (sig_entry->per_cu.v.quick != NULL);
6207 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6208 }
6209 else
6210 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
6211 gdb_assert (sig_entry->signature == dwo_entry->signature);
6212 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
6213 gdb_assert (sig_entry->type_unit_group == NULL);
6214 gdb_assert (sig_entry->dwo_unit == NULL);
6215
6216 sig_entry->per_cu.section = dwo_entry->section;
6217 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
6218 sig_entry->per_cu.length = dwo_entry->length;
6219 sig_entry->per_cu.reading_dwo_directly = 1;
6220 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6221 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6222 sig_entry->dwo_unit = dwo_entry;
6223 }
6224
6225 /* Subroutine of lookup_signatured_type.
6226 If we haven't read the TU yet, create the signatured_type data structure
6227 for a TU to be read in directly from a DWO file, bypassing the stub.
6228 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6229 using .gdb_index, then when reading a CU we want to stay in the DWO file
6230 containing that CU. Otherwise we could end up reading several other DWO
6231 files (due to comdat folding) to process the transitive closure of all the
6232 mentioned TUs, and that can be slow. The current DWO file will have every
6233 type signature that it needs.
6234 We only do this for .gdb_index because in the psymtab case we already have
6235 to read all the DWOs to build the type unit groups. */
6236
6237 static struct signatured_type *
6238 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6239 {
6240 struct dwarf2_per_objfile *dwarf2_per_objfile
6241 = cu->per_cu->dwarf2_per_objfile;
6242 struct dwo_file *dwo_file;
6243 struct dwo_unit find_dwo_entry, *dwo_entry;
6244 struct signatured_type find_sig_entry, *sig_entry;
6245 void **slot;
6246
6247 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6248
6249 /* If TU skeletons have been removed then we may not have read in any
6250 TUs yet. */
6251 if (dwarf2_per_objfile->signatured_types == NULL)
6252 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
6253
6254 /* We only ever need to read in one copy of a signatured type.
6255 Use the global signatured_types array to do our own comdat-folding
6256 of types. If this is the first time we're reading this TU, and
6257 the TU has an entry in .gdb_index, replace the recorded data from
6258 .gdb_index with this TU. */
6259
6260 find_sig_entry.signature = sig;
6261 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6262 &find_sig_entry, INSERT);
6263 sig_entry = (struct signatured_type *) *slot;
6264
6265 /* We can get here with the TU already read, *or* in the process of being
6266 read. Don't reassign the global entry to point to this DWO if that's
6267 the case. Also note that if the TU is already being read, it may not
6268 have come from a DWO, the program may be a mix of Fission-compiled
6269 code and non-Fission-compiled code. */
6270
6271 /* Have we already tried to read this TU?
6272 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6273 needn't exist in the global table yet). */
6274 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
6275 return sig_entry;
6276
6277 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6278 dwo_unit of the TU itself. */
6279 dwo_file = cu->dwo_unit->dwo_file;
6280
6281 /* Ok, this is the first time we're reading this TU. */
6282 if (dwo_file->tus == NULL)
6283 return NULL;
6284 find_dwo_entry.signature = sig;
6285 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6286 &find_dwo_entry);
6287 if (dwo_entry == NULL)
6288 return NULL;
6289
6290 /* If the global table doesn't have an entry for this TU, add one. */
6291 if (sig_entry == NULL)
6292 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6293
6294 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6295 sig_entry->per_cu.tu_read = 1;
6296 return sig_entry;
6297 }
6298
6299 /* Subroutine of lookup_signatured_type.
6300 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6301 then try the DWP file. If the TU stub (skeleton) has been removed then
6302 it won't be in .gdb_index. */
6303
6304 static struct signatured_type *
6305 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6306 {
6307 struct dwarf2_per_objfile *dwarf2_per_objfile
6308 = cu->per_cu->dwarf2_per_objfile;
6309 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
6310 struct dwo_unit *dwo_entry;
6311 struct signatured_type find_sig_entry, *sig_entry;
6312 void **slot;
6313
6314 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6315 gdb_assert (dwp_file != NULL);
6316
6317 /* If TU skeletons have been removed then we may not have read in any
6318 TUs yet. */
6319 if (dwarf2_per_objfile->signatured_types == NULL)
6320 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
6321
6322 find_sig_entry.signature = sig;
6323 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6324 &find_sig_entry, INSERT);
6325 sig_entry = (struct signatured_type *) *slot;
6326
6327 /* Have we already tried to read this TU?
6328 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6329 needn't exist in the global table yet). */
6330 if (sig_entry != NULL)
6331 return sig_entry;
6332
6333 if (dwp_file->tus == NULL)
6334 return NULL;
6335 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
6336 sig, 1 /* is_debug_types */);
6337 if (dwo_entry == NULL)
6338 return NULL;
6339
6340 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6341 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6342
6343 return sig_entry;
6344 }
6345
6346 /* Lookup a signature based type for DW_FORM_ref_sig8.
6347 Returns NULL if signature SIG is not present in the table.
6348 It is up to the caller to complain about this. */
6349
6350 static struct signatured_type *
6351 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6352 {
6353 struct dwarf2_per_objfile *dwarf2_per_objfile
6354 = cu->per_cu->dwarf2_per_objfile;
6355
6356 if (cu->dwo_unit
6357 && dwarf2_per_objfile->using_index)
6358 {
6359 /* We're in a DWO/DWP file, and we're using .gdb_index.
6360 These cases require special processing. */
6361 if (get_dwp_file (dwarf2_per_objfile) == NULL)
6362 return lookup_dwo_signatured_type (cu, sig);
6363 else
6364 return lookup_dwp_signatured_type (cu, sig);
6365 }
6366 else
6367 {
6368 struct signatured_type find_entry, *entry;
6369
6370 if (dwarf2_per_objfile->signatured_types == NULL)
6371 return NULL;
6372 find_entry.signature = sig;
6373 entry = ((struct signatured_type *)
6374 htab_find (dwarf2_per_objfile->signatured_types.get (),
6375 &find_entry));
6376 return entry;
6377 }
6378 }
6379
6380 /* Low level DIE reading support. */
6381
6382 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6383
6384 static void
6385 init_cu_die_reader (struct die_reader_specs *reader,
6386 struct dwarf2_cu *cu,
6387 struct dwarf2_section_info *section,
6388 struct dwo_file *dwo_file,
6389 struct abbrev_table *abbrev_table)
6390 {
6391 gdb_assert (section->readin && section->buffer != NULL);
6392 reader->abfd = section->get_bfd_owner ();
6393 reader->cu = cu;
6394 reader->dwo_file = dwo_file;
6395 reader->die_section = section;
6396 reader->buffer = section->buffer;
6397 reader->buffer_end = section->buffer + section->size;
6398 reader->abbrev_table = abbrev_table;
6399 }
6400
6401 /* Subroutine of cutu_reader to simplify it.
6402 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
6403 There's just a lot of work to do, and cutu_reader is big enough
6404 already.
6405
6406 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6407 from it to the DIE in the DWO. If NULL we are skipping the stub.
6408 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6409 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
6410 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6411 STUB_COMP_DIR may be non-NULL.
6412 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
6413 are filled in with the info of the DIE from the DWO file.
6414 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6415 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6416 kept around for at least as long as *RESULT_READER.
6417
6418 The result is non-zero if a valid (non-dummy) DIE was found. */
6419
6420 static int
6421 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
6422 struct dwo_unit *dwo_unit,
6423 struct die_info *stub_comp_unit_die,
6424 const char *stub_comp_dir,
6425 struct die_reader_specs *result_reader,
6426 const gdb_byte **result_info_ptr,
6427 struct die_info **result_comp_unit_die,
6428 abbrev_table_up *result_dwo_abbrev_table)
6429 {
6430 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6431 struct objfile *objfile = dwarf2_per_objfile->objfile;
6432 struct dwarf2_cu *cu = this_cu->cu;
6433 bfd *abfd;
6434 const gdb_byte *begin_info_ptr, *info_ptr;
6435 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6436 int i,num_extra_attrs;
6437 struct dwarf2_section_info *dwo_abbrev_section;
6438 struct die_info *comp_unit_die;
6439
6440 /* At most one of these may be provided. */
6441 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
6442
6443 /* These attributes aren't processed until later:
6444 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
6445 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6446 referenced later. However, these attributes are found in the stub
6447 which we won't have later. In order to not impose this complication
6448 on the rest of the code, we read them here and copy them to the
6449 DWO CU/TU die. */
6450
6451 stmt_list = NULL;
6452 low_pc = NULL;
6453 high_pc = NULL;
6454 ranges = NULL;
6455 comp_dir = NULL;
6456
6457 if (stub_comp_unit_die != NULL)
6458 {
6459 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6460 DWO file. */
6461 if (! this_cu->is_debug_types)
6462 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6463 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6464 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6465 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6466 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6467
6468 cu->addr_base = stub_comp_unit_die->addr_base ();
6469
6470 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
6471 here (if needed). We need the value before we can process
6472 DW_AT_ranges. */
6473 cu->ranges_base = stub_comp_unit_die->ranges_base ();
6474 }
6475 else if (stub_comp_dir != NULL)
6476 {
6477 /* Reconstruct the comp_dir attribute to simplify the code below. */
6478 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
6479 comp_dir->name = DW_AT_comp_dir;
6480 comp_dir->form = DW_FORM_string;
6481 DW_STRING_IS_CANONICAL (comp_dir) = 0;
6482 DW_STRING (comp_dir) = stub_comp_dir;
6483 }
6484
6485 /* Set up for reading the DWO CU/TU. */
6486 cu->dwo_unit = dwo_unit;
6487 dwarf2_section_info *section = dwo_unit->section;
6488 section->read (objfile);
6489 abfd = section->get_bfd_owner ();
6490 begin_info_ptr = info_ptr = (section->buffer
6491 + to_underlying (dwo_unit->sect_off));
6492 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
6493
6494 if (this_cu->is_debug_types)
6495 {
6496 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
6497
6498 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6499 &cu->header, section,
6500 dwo_abbrev_section,
6501 info_ptr, rcuh_kind::TYPE);
6502 /* This is not an assert because it can be caused by bad debug info. */
6503 if (sig_type->signature != cu->header.signature)
6504 {
6505 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
6506 " TU at offset %s [in module %s]"),
6507 hex_string (sig_type->signature),
6508 hex_string (cu->header.signature),
6509 sect_offset_str (dwo_unit->sect_off),
6510 bfd_get_filename (abfd));
6511 }
6512 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6513 /* For DWOs coming from DWP files, we don't know the CU length
6514 nor the type's offset in the TU until now. */
6515 dwo_unit->length = cu->header.get_length ();
6516 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
6517
6518 /* Establish the type offset that can be used to lookup the type.
6519 For DWO files, we don't know it until now. */
6520 sig_type->type_offset_in_section
6521 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
6522 }
6523 else
6524 {
6525 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6526 &cu->header, section,
6527 dwo_abbrev_section,
6528 info_ptr, rcuh_kind::COMPILE);
6529 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6530 /* For DWOs coming from DWP files, we don't know the CU length
6531 until now. */
6532 dwo_unit->length = cu->header.get_length ();
6533 }
6534
6535 *result_dwo_abbrev_table
6536 = abbrev_table::read (objfile, dwo_abbrev_section,
6537 cu->header.abbrev_sect_off);
6538 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6539 result_dwo_abbrev_table->get ());
6540
6541 /* Read in the die, but leave space to copy over the attributes
6542 from the stub. This has the benefit of simplifying the rest of
6543 the code - all the work to maintain the illusion of a single
6544 DW_TAG_{compile,type}_unit DIE is done here. */
6545 num_extra_attrs = ((stmt_list != NULL)
6546 + (low_pc != NULL)
6547 + (high_pc != NULL)
6548 + (ranges != NULL)
6549 + (comp_dir != NULL));
6550 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
6551 num_extra_attrs);
6552
6553 /* Copy over the attributes from the stub to the DIE we just read in. */
6554 comp_unit_die = *result_comp_unit_die;
6555 i = comp_unit_die->num_attrs;
6556 if (stmt_list != NULL)
6557 comp_unit_die->attrs[i++] = *stmt_list;
6558 if (low_pc != NULL)
6559 comp_unit_die->attrs[i++] = *low_pc;
6560 if (high_pc != NULL)
6561 comp_unit_die->attrs[i++] = *high_pc;
6562 if (ranges != NULL)
6563 comp_unit_die->attrs[i++] = *ranges;
6564 if (comp_dir != NULL)
6565 comp_unit_die->attrs[i++] = *comp_dir;
6566 comp_unit_die->num_attrs += num_extra_attrs;
6567
6568 if (dwarf_die_debug)
6569 {
6570 fprintf_unfiltered (gdb_stdlog,
6571 "Read die from %s@0x%x of %s:\n",
6572 section->get_name (),
6573 (unsigned) (begin_info_ptr - section->buffer),
6574 bfd_get_filename (abfd));
6575 dump_die (comp_unit_die, dwarf_die_debug);
6576 }
6577
6578 /* Skip dummy compilation units. */
6579 if (info_ptr >= begin_info_ptr + dwo_unit->length
6580 || peek_abbrev_code (abfd, info_ptr) == 0)
6581 return 0;
6582
6583 *result_info_ptr = info_ptr;
6584 return 1;
6585 }
6586
6587 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
6588 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6589 signature is part of the header. */
6590 static gdb::optional<ULONGEST>
6591 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6592 {
6593 if (cu->header.version >= 5)
6594 return cu->header.signature;
6595 struct attribute *attr;
6596 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6597 if (attr == nullptr)
6598 return gdb::optional<ULONGEST> ();
6599 return DW_UNSND (attr);
6600 }
6601
6602 /* Subroutine of cutu_reader to simplify it.
6603 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6604 Returns NULL if the specified DWO unit cannot be found. */
6605
6606 static struct dwo_unit *
6607 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
6608 struct die_info *comp_unit_die,
6609 const char *dwo_name)
6610 {
6611 struct dwarf2_cu *cu = this_cu->cu;
6612 struct dwo_unit *dwo_unit;
6613 const char *comp_dir;
6614
6615 gdb_assert (cu != NULL);
6616
6617 /* Yeah, we look dwo_name up again, but it simplifies the code. */
6618 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6619 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
6620
6621 if (this_cu->is_debug_types)
6622 {
6623 struct signatured_type *sig_type;
6624
6625 /* Since this_cu is the first member of struct signatured_type,
6626 we can go from a pointer to one to a pointer to the other. */
6627 sig_type = (struct signatured_type *) this_cu;
6628 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
6629 }
6630 else
6631 {
6632 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
6633 if (!signature.has_value ())
6634 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6635 " [in module %s]"),
6636 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
6637 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
6638 *signature);
6639 }
6640
6641 return dwo_unit;
6642 }
6643
6644 /* Subroutine of cutu_reader to simplify it.
6645 See it for a description of the parameters.
6646 Read a TU directly from a DWO file, bypassing the stub. */
6647
6648 void
6649 cutu_reader::init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
6650 int use_existing_cu)
6651 {
6652 struct signatured_type *sig_type;
6653
6654 /* Verify we can do the following downcast, and that we have the
6655 data we need. */
6656 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6657 sig_type = (struct signatured_type *) this_cu;
6658 gdb_assert (sig_type->dwo_unit != NULL);
6659
6660 if (use_existing_cu && this_cu->cu != NULL)
6661 {
6662 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6663 /* There's no need to do the rereading_dwo_cu handling that
6664 cutu_reader does since we don't read the stub. */
6665 }
6666 else
6667 {
6668 /* If !use_existing_cu, this_cu->cu must be NULL. */
6669 gdb_assert (this_cu->cu == NULL);
6670 m_new_cu.reset (new dwarf2_cu (this_cu));
6671 }
6672
6673 /* A future optimization, if needed, would be to use an existing
6674 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6675 could share abbrev tables. */
6676
6677 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
6678 NULL /* stub_comp_unit_die */,
6679 sig_type->dwo_unit->dwo_file->comp_dir,
6680 this, &info_ptr,
6681 &comp_unit_die,
6682 &m_dwo_abbrev_table) == 0)
6683 {
6684 /* Dummy die. */
6685 dummy_p = true;
6686 }
6687 }
6688
6689 /* Initialize a CU (or TU) and read its DIEs.
6690 If the CU defers to a DWO file, read the DWO file as well.
6691
6692 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6693 Otherwise the table specified in the comp unit header is read in and used.
6694 This is an optimization for when we already have the abbrev table.
6695
6696 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
6697 Otherwise, a new CU is allocated with xmalloc. */
6698
6699 cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
6700 struct abbrev_table *abbrev_table,
6701 int use_existing_cu,
6702 bool skip_partial)
6703 : die_reader_specs {},
6704 m_this_cu (this_cu)
6705 {
6706 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6707 struct objfile *objfile = dwarf2_per_objfile->objfile;
6708 struct dwarf2_section_info *section = this_cu->section;
6709 bfd *abfd = section->get_bfd_owner ();
6710 struct dwarf2_cu *cu;
6711 const gdb_byte *begin_info_ptr;
6712 struct signatured_type *sig_type = NULL;
6713 struct dwarf2_section_info *abbrev_section;
6714 /* Non-zero if CU currently points to a DWO file and we need to
6715 reread it. When this happens we need to reread the skeleton die
6716 before we can reread the DWO file (this only applies to CUs, not TUs). */
6717 int rereading_dwo_cu = 0;
6718
6719 if (dwarf_die_debug)
6720 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
6721 this_cu->is_debug_types ? "type" : "comp",
6722 sect_offset_str (this_cu->sect_off));
6723
6724 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6725 file (instead of going through the stub), short-circuit all of this. */
6726 if (this_cu->reading_dwo_directly)
6727 {
6728 /* Narrow down the scope of possibilities to have to understand. */
6729 gdb_assert (this_cu->is_debug_types);
6730 gdb_assert (abbrev_table == NULL);
6731 init_tu_and_read_dwo_dies (this_cu, use_existing_cu);
6732 return;
6733 }
6734
6735 /* This is cheap if the section is already read in. */
6736 section->read (objfile);
6737
6738 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
6739
6740 abbrev_section = get_abbrev_section_for_cu (this_cu);
6741
6742 if (use_existing_cu && this_cu->cu != NULL)
6743 {
6744 cu = this_cu->cu;
6745 /* If this CU is from a DWO file we need to start over, we need to
6746 refetch the attributes from the skeleton CU.
6747 This could be optimized by retrieving those attributes from when we
6748 were here the first time: the previous comp_unit_die was stored in
6749 comp_unit_obstack. But there's no data yet that we need this
6750 optimization. */
6751 if (cu->dwo_unit != NULL)
6752 rereading_dwo_cu = 1;
6753 }
6754 else
6755 {
6756 /* If !use_existing_cu, this_cu->cu must be NULL. */
6757 gdb_assert (this_cu->cu == NULL);
6758 m_new_cu.reset (new dwarf2_cu (this_cu));
6759 cu = m_new_cu.get ();
6760 }
6761
6762 /* Get the header. */
6763 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
6764 {
6765 /* We already have the header, there's no need to read it in again. */
6766 info_ptr += to_underlying (cu->header.first_die_cu_offset);
6767 }
6768 else
6769 {
6770 if (this_cu->is_debug_types)
6771 {
6772 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6773 &cu->header, section,
6774 abbrev_section, info_ptr,
6775 rcuh_kind::TYPE);
6776
6777 /* Since per_cu is the first member of struct signatured_type,
6778 we can go from a pointer to one to a pointer to the other. */
6779 sig_type = (struct signatured_type *) this_cu;
6780 gdb_assert (sig_type->signature == cu->header.signature);
6781 gdb_assert (sig_type->type_offset_in_tu
6782 == cu->header.type_cu_offset_in_tu);
6783 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6784
6785 /* LENGTH has not been set yet for type units if we're
6786 using .gdb_index. */
6787 this_cu->length = cu->header.get_length ();
6788
6789 /* Establish the type offset that can be used to lookup the type. */
6790 sig_type->type_offset_in_section =
6791 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
6792
6793 this_cu->dwarf_version = cu->header.version;
6794 }
6795 else
6796 {
6797 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6798 &cu->header, section,
6799 abbrev_section,
6800 info_ptr,
6801 rcuh_kind::COMPILE);
6802
6803 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6804 gdb_assert (this_cu->length == cu->header.get_length ());
6805 this_cu->dwarf_version = cu->header.version;
6806 }
6807 }
6808
6809 /* Skip dummy compilation units. */
6810 if (info_ptr >= begin_info_ptr + this_cu->length
6811 || peek_abbrev_code (abfd, info_ptr) == 0)
6812 {
6813 dummy_p = true;
6814 return;
6815 }
6816
6817 /* If we don't have them yet, read the abbrevs for this compilation unit.
6818 And if we need to read them now, make sure they're freed when we're
6819 done. */
6820 if (abbrev_table != NULL)
6821 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
6822 else
6823 {
6824 m_abbrev_table_holder
6825 = abbrev_table::read (objfile, abbrev_section,
6826 cu->header.abbrev_sect_off);
6827 abbrev_table = m_abbrev_table_holder.get ();
6828 }
6829
6830 /* Read the top level CU/TU die. */
6831 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
6832 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
6833
6834 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
6835 {
6836 dummy_p = true;
6837 return;
6838 }
6839
6840 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
6841 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
6842 table from the DWO file and pass the ownership over to us. It will be
6843 referenced from READER, so we must make sure to free it after we're done
6844 with READER.
6845
6846 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
6847 DWO CU, that this test will fail (the attribute will not be present). */
6848 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6849 if (dwo_name != nullptr)
6850 {
6851 struct dwo_unit *dwo_unit;
6852 struct die_info *dwo_comp_unit_die;
6853
6854 if (comp_unit_die->has_children)
6855 {
6856 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
6857 " has children (offset %s) [in module %s]"),
6858 sect_offset_str (this_cu->sect_off),
6859 bfd_get_filename (abfd));
6860 }
6861 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die, dwo_name);
6862 if (dwo_unit != NULL)
6863 {
6864 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
6865 comp_unit_die, NULL,
6866 this, &info_ptr,
6867 &dwo_comp_unit_die,
6868 &m_dwo_abbrev_table) == 0)
6869 {
6870 /* Dummy die. */
6871 dummy_p = true;
6872 return;
6873 }
6874 comp_unit_die = dwo_comp_unit_die;
6875 }
6876 else
6877 {
6878 /* Yikes, we couldn't find the rest of the DIE, we only have
6879 the stub. A complaint has already been logged. There's
6880 not much more we can do except pass on the stub DIE to
6881 die_reader_func. We don't want to throw an error on bad
6882 debug info. */
6883 }
6884 }
6885 }
6886
6887 void
6888 cutu_reader::keep ()
6889 {
6890 /* Done, clean up. */
6891 gdb_assert (!dummy_p);
6892 if (m_new_cu != NULL)
6893 {
6894 struct dwarf2_per_objfile *dwarf2_per_objfile
6895 = m_this_cu->dwarf2_per_objfile;
6896 /* Link this CU into read_in_chain. */
6897 m_this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
6898 dwarf2_per_objfile->read_in_chain = m_this_cu;
6899 /* The chain owns it now. */
6900 m_new_cu.release ();
6901 }
6902 }
6903
6904 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
6905 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
6906 assumed to have already done the lookup to find the DWO file).
6907
6908 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
6909 THIS_CU->is_debug_types, but nothing else.
6910
6911 We fill in THIS_CU->length.
6912
6913 THIS_CU->cu is always freed when done.
6914 This is done in order to not leave THIS_CU->cu in a state where we have
6915 to care whether it refers to the "main" CU or the DWO CU.
6916
6917 When parent_cu is passed, it is used to provide a default value for
6918 str_offsets_base and addr_base from the parent. */
6919
6920 cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
6921 struct dwarf2_cu *parent_cu,
6922 struct dwo_file *dwo_file)
6923 : die_reader_specs {},
6924 m_this_cu (this_cu)
6925 {
6926 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6927 struct objfile *objfile = dwarf2_per_objfile->objfile;
6928 struct dwarf2_section_info *section = this_cu->section;
6929 bfd *abfd = section->get_bfd_owner ();
6930 struct dwarf2_section_info *abbrev_section;
6931 const gdb_byte *begin_info_ptr, *info_ptr;
6932
6933 if (dwarf_die_debug)
6934 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
6935 this_cu->is_debug_types ? "type" : "comp",
6936 sect_offset_str (this_cu->sect_off));
6937
6938 gdb_assert (this_cu->cu == NULL);
6939
6940 abbrev_section = (dwo_file != NULL
6941 ? &dwo_file->sections.abbrev
6942 : get_abbrev_section_for_cu (this_cu));
6943
6944 /* This is cheap if the section is already read in. */
6945 section->read (objfile);
6946
6947 m_new_cu.reset (new dwarf2_cu (this_cu));
6948
6949 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
6950 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6951 &m_new_cu->header, section,
6952 abbrev_section, info_ptr,
6953 (this_cu->is_debug_types
6954 ? rcuh_kind::TYPE
6955 : rcuh_kind::COMPILE));
6956
6957 if (parent_cu != nullptr)
6958 {
6959 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
6960 m_new_cu->addr_base = parent_cu->addr_base;
6961 }
6962 this_cu->length = m_new_cu->header.get_length ();
6963
6964 /* Skip dummy compilation units. */
6965 if (info_ptr >= begin_info_ptr + this_cu->length
6966 || peek_abbrev_code (abfd, info_ptr) == 0)
6967 {
6968 dummy_p = true;
6969 return;
6970 }
6971
6972 m_abbrev_table_holder
6973 = abbrev_table::read (objfile, abbrev_section,
6974 m_new_cu->header.abbrev_sect_off);
6975
6976 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
6977 m_abbrev_table_holder.get ());
6978 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
6979 }
6980
6981 \f
6982 /* Type Unit Groups.
6983
6984 Type Unit Groups are a way to collapse the set of all TUs (type units) into
6985 a more manageable set. The grouping is done by DW_AT_stmt_list entry
6986 so that all types coming from the same compilation (.o file) are grouped
6987 together. A future step could be to put the types in the same symtab as
6988 the CU the types ultimately came from. */
6989
6990 static hashval_t
6991 hash_type_unit_group (const void *item)
6992 {
6993 const struct type_unit_group *tu_group
6994 = (const struct type_unit_group *) item;
6995
6996 return hash_stmt_list_entry (&tu_group->hash);
6997 }
6998
6999 static int
7000 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
7001 {
7002 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7003 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
7004
7005 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
7006 }
7007
7008 /* Allocate a hash table for type unit groups. */
7009
7010 static htab_up
7011 allocate_type_unit_groups_table ()
7012 {
7013 return htab_up (htab_create_alloc (3,
7014 hash_type_unit_group,
7015 eq_type_unit_group,
7016 NULL, xcalloc, xfree));
7017 }
7018
7019 /* Type units that don't have DW_AT_stmt_list are grouped into their own
7020 partial symtabs. We combine several TUs per psymtab to not let the size
7021 of any one psymtab grow too big. */
7022 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7023 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
7024
7025 /* Helper routine for get_type_unit_group.
7026 Create the type_unit_group object used to hold one or more TUs. */
7027
7028 static struct type_unit_group *
7029 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
7030 {
7031 struct dwarf2_per_objfile *dwarf2_per_objfile
7032 = cu->per_cu->dwarf2_per_objfile;
7033 struct objfile *objfile = dwarf2_per_objfile->objfile;
7034 struct dwarf2_per_cu_data *per_cu;
7035 struct type_unit_group *tu_group;
7036
7037 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7038 struct type_unit_group);
7039 per_cu = &tu_group->per_cu;
7040 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
7041
7042 if (dwarf2_per_objfile->using_index)
7043 {
7044 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7045 struct dwarf2_per_cu_quick_data);
7046 }
7047 else
7048 {
7049 unsigned int line_offset = to_underlying (line_offset_struct);
7050 dwarf2_psymtab *pst;
7051 std::string name;
7052
7053 /* Give the symtab a useful name for debug purposes. */
7054 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7055 name = string_printf ("<type_units_%d>",
7056 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7057 else
7058 name = string_printf ("<type_units_at_0x%x>", line_offset);
7059
7060 pst = create_partial_symtab (per_cu, name.c_str ());
7061 pst->anonymous = true;
7062 }
7063
7064 tu_group->hash.dwo_unit = cu->dwo_unit;
7065 tu_group->hash.line_sect_off = line_offset_struct;
7066
7067 return tu_group;
7068 }
7069
7070 /* Look up the type_unit_group for type unit CU, and create it if necessary.
7071 STMT_LIST is a DW_AT_stmt_list attribute. */
7072
7073 static struct type_unit_group *
7074 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7075 {
7076 struct dwarf2_per_objfile *dwarf2_per_objfile
7077 = cu->per_cu->dwarf2_per_objfile;
7078 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7079 struct type_unit_group *tu_group;
7080 void **slot;
7081 unsigned int line_offset;
7082 struct type_unit_group type_unit_group_for_lookup;
7083
7084 if (dwarf2_per_objfile->type_unit_groups == NULL)
7085 dwarf2_per_objfile->type_unit_groups = allocate_type_unit_groups_table ();
7086
7087 /* Do we need to create a new group, or can we use an existing one? */
7088
7089 if (stmt_list)
7090 {
7091 line_offset = DW_UNSND (stmt_list);
7092 ++tu_stats->nr_symtab_sharers;
7093 }
7094 else
7095 {
7096 /* Ugh, no stmt_list. Rare, but we have to handle it.
7097 We can do various things here like create one group per TU or
7098 spread them over multiple groups to split up the expansion work.
7099 To avoid worst case scenarios (too many groups or too large groups)
7100 we, umm, group them in bunches. */
7101 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7102 | (tu_stats->nr_stmt_less_type_units
7103 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7104 ++tu_stats->nr_stmt_less_type_units;
7105 }
7106
7107 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7108 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7109 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups.get (),
7110 &type_unit_group_for_lookup, INSERT);
7111 if (*slot != NULL)
7112 {
7113 tu_group = (struct type_unit_group *) *slot;
7114 gdb_assert (tu_group != NULL);
7115 }
7116 else
7117 {
7118 sect_offset line_offset_struct = (sect_offset) line_offset;
7119 tu_group = create_type_unit_group (cu, line_offset_struct);
7120 *slot = tu_group;
7121 ++tu_stats->nr_symtabs;
7122 }
7123
7124 return tu_group;
7125 }
7126 \f
7127 /* Partial symbol tables. */
7128
7129 /* Create a psymtab named NAME and assign it to PER_CU.
7130
7131 The caller must fill in the following details:
7132 dirname, textlow, texthigh. */
7133
7134 static dwarf2_psymtab *
7135 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
7136 {
7137 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
7138 dwarf2_psymtab *pst;
7139
7140 pst = new dwarf2_psymtab (name, objfile, 0);
7141
7142 pst->psymtabs_addrmap_supported = true;
7143
7144 /* This is the glue that links PST into GDB's symbol API. */
7145 pst->per_cu_data = per_cu;
7146 per_cu->v.psymtab = pst;
7147
7148 return pst;
7149 }
7150
7151 /* DIE reader function for process_psymtab_comp_unit. */
7152
7153 static void
7154 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
7155 const gdb_byte *info_ptr,
7156 struct die_info *comp_unit_die,
7157 enum language pretend_language)
7158 {
7159 struct dwarf2_cu *cu = reader->cu;
7160 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
7161 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7162 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7163 CORE_ADDR baseaddr;
7164 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7165 dwarf2_psymtab *pst;
7166 enum pc_bounds_kind cu_bounds_kind;
7167 const char *filename;
7168
7169 gdb_assert (! per_cu->is_debug_types);
7170
7171 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
7172
7173 /* Allocate a new partial symbol table structure. */
7174 gdb::unique_xmalloc_ptr<char> debug_filename;
7175 static const char artificial[] = "<artificial>";
7176 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7177 if (filename == NULL)
7178 filename = "";
7179 else if (strcmp (filename, artificial) == 0)
7180 {
7181 debug_filename.reset (concat (artificial, "@",
7182 sect_offset_str (per_cu->sect_off),
7183 (char *) NULL));
7184 filename = debug_filename.get ();
7185 }
7186
7187 pst = create_partial_symtab (per_cu, filename);
7188
7189 /* This must be done before calling dwarf2_build_include_psymtabs. */
7190 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7191
7192 baseaddr = objfile->text_section_offset ();
7193
7194 dwarf2_find_base_address (comp_unit_die, cu);
7195
7196 /* Possibly set the default values of LOWPC and HIGHPC from
7197 `DW_AT_ranges'. */
7198 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7199 &best_highpc, cu, pst);
7200 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
7201 {
7202 CORE_ADDR low
7203 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7204 - baseaddr);
7205 CORE_ADDR high
7206 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7207 - baseaddr - 1);
7208 /* Store the contiguous range if it is not empty; it can be
7209 empty for CUs with no code. */
7210 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7211 low, high, pst);
7212 }
7213
7214 /* Check if comp unit has_children.
7215 If so, read the rest of the partial symbols from this comp unit.
7216 If not, there's no more debug_info for this comp unit. */
7217 if (comp_unit_die->has_children)
7218 {
7219 struct partial_die_info *first_die;
7220 CORE_ADDR lowpc, highpc;
7221
7222 lowpc = ((CORE_ADDR) -1);
7223 highpc = ((CORE_ADDR) 0);
7224
7225 first_die = load_partial_dies (reader, info_ptr, 1);
7226
7227 scan_partial_symbols (first_die, &lowpc, &highpc,
7228 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
7229
7230 /* If we didn't find a lowpc, set it to highpc to avoid
7231 complaints from `maint check'. */
7232 if (lowpc == ((CORE_ADDR) -1))
7233 lowpc = highpc;
7234
7235 /* If the compilation unit didn't have an explicit address range,
7236 then use the information extracted from its child dies. */
7237 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
7238 {
7239 best_lowpc = lowpc;
7240 best_highpc = highpc;
7241 }
7242 }
7243 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
7244 best_lowpc + baseaddr)
7245 - baseaddr);
7246 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
7247 best_highpc + baseaddr)
7248 - baseaddr);
7249
7250 end_psymtab_common (objfile, pst);
7251
7252 if (!cu->per_cu->imported_symtabs_empty ())
7253 {
7254 int i;
7255 int len = cu->per_cu->imported_symtabs_size ();
7256
7257 /* Fill in 'dependencies' here; we fill in 'users' in a
7258 post-pass. */
7259 pst->number_of_dependencies = len;
7260 pst->dependencies
7261 = objfile->partial_symtabs->allocate_dependencies (len);
7262 for (i = 0; i < len; ++i)
7263 {
7264 pst->dependencies[i]
7265 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7266 }
7267
7268 cu->per_cu->imported_symtabs_free ();
7269 }
7270
7271 /* Get the list of files included in the current compilation unit,
7272 and build a psymtab for each of them. */
7273 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7274
7275 if (dwarf_read_debug)
7276 fprintf_unfiltered (gdb_stdlog,
7277 "Psymtab for %s unit @%s: %s - %s"
7278 ", %d global, %d static syms\n",
7279 per_cu->is_debug_types ? "type" : "comp",
7280 sect_offset_str (per_cu->sect_off),
7281 paddress (gdbarch, pst->text_low (objfile)),
7282 paddress (gdbarch, pst->text_high (objfile)),
7283 pst->n_global_syms, pst->n_static_syms);
7284 }
7285
7286 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7287 Process compilation unit THIS_CU for a psymtab. */
7288
7289 static void
7290 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
7291 bool want_partial_unit,
7292 enum language pretend_language)
7293 {
7294 /* If this compilation unit was already read in, free the
7295 cached copy in order to read it in again. This is
7296 necessary because we skipped some symbols when we first
7297 read in the compilation unit (see load_partial_dies).
7298 This problem could be avoided, but the benefit is unclear. */
7299 if (this_cu->cu != NULL)
7300 free_one_cached_comp_unit (this_cu);
7301
7302 cutu_reader reader (this_cu, NULL, 0, false);
7303
7304 switch (reader.comp_unit_die->tag)
7305 {
7306 case DW_TAG_compile_unit:
7307 this_cu->unit_type = DW_UT_compile;
7308 break;
7309 case DW_TAG_partial_unit:
7310 this_cu->unit_type = DW_UT_partial;
7311 break;
7312 default:
7313 abort ();
7314 }
7315
7316 if (reader.dummy_p)
7317 {
7318 /* Nothing. */
7319 }
7320 else if (this_cu->is_debug_types)
7321 build_type_psymtabs_reader (&reader, reader.info_ptr,
7322 reader.comp_unit_die);
7323 else if (want_partial_unit
7324 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
7325 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7326 reader.comp_unit_die,
7327 pretend_language);
7328
7329 this_cu->lang = this_cu->cu->language;
7330
7331 /* Age out any secondary CUs. */
7332 age_cached_comp_units (this_cu->dwarf2_per_objfile);
7333 }
7334
7335 /* Reader function for build_type_psymtabs. */
7336
7337 static void
7338 build_type_psymtabs_reader (const struct die_reader_specs *reader,
7339 const gdb_byte *info_ptr,
7340 struct die_info *type_unit_die)
7341 {
7342 struct dwarf2_per_objfile *dwarf2_per_objfile
7343 = reader->cu->per_cu->dwarf2_per_objfile;
7344 struct objfile *objfile = dwarf2_per_objfile->objfile;
7345 struct dwarf2_cu *cu = reader->cu;
7346 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7347 struct signatured_type *sig_type;
7348 struct type_unit_group *tu_group;
7349 struct attribute *attr;
7350 struct partial_die_info *first_die;
7351 CORE_ADDR lowpc, highpc;
7352 dwarf2_psymtab *pst;
7353
7354 gdb_assert (per_cu->is_debug_types);
7355 sig_type = (struct signatured_type *) per_cu;
7356
7357 if (! type_unit_die->has_children)
7358 return;
7359
7360 attr = type_unit_die->attr (DW_AT_stmt_list);
7361 tu_group = get_type_unit_group (cu, attr);
7362
7363 if (tu_group->tus == nullptr)
7364 tu_group->tus = new std::vector<signatured_type *>;
7365 tu_group->tus->push_back (sig_type);
7366
7367 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7368 pst = create_partial_symtab (per_cu, "");
7369 pst->anonymous = true;
7370
7371 first_die = load_partial_dies (reader, info_ptr, 1);
7372
7373 lowpc = (CORE_ADDR) -1;
7374 highpc = (CORE_ADDR) 0;
7375 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7376
7377 end_psymtab_common (objfile, pst);
7378 }
7379
7380 /* Struct used to sort TUs by their abbreviation table offset. */
7381
7382 struct tu_abbrev_offset
7383 {
7384 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7385 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7386 {}
7387
7388 signatured_type *sig_type;
7389 sect_offset abbrev_offset;
7390 };
7391
7392 /* Helper routine for build_type_psymtabs_1, passed to std::sort. */
7393
7394 static bool
7395 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7396 const struct tu_abbrev_offset &b)
7397 {
7398 return a.abbrev_offset < b.abbrev_offset;
7399 }
7400
7401 /* Efficiently read all the type units.
7402 This does the bulk of the work for build_type_psymtabs.
7403
7404 The efficiency is because we sort TUs by the abbrev table they use and
7405 only read each abbrev table once. In one program there are 200K TUs
7406 sharing 8K abbrev tables.
7407
7408 The main purpose of this function is to support building the
7409 dwarf2_per_objfile->type_unit_groups table.
7410 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7411 can collapse the search space by grouping them by stmt_list.
7412 The savings can be significant, in the same program from above the 200K TUs
7413 share 8K stmt_list tables.
7414
7415 FUNC is expected to call get_type_unit_group, which will create the
7416 struct type_unit_group if necessary and add it to
7417 dwarf2_per_objfile->type_unit_groups. */
7418
7419 static void
7420 build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
7421 {
7422 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7423 abbrev_table_up abbrev_table;
7424 sect_offset abbrev_offset;
7425
7426 /* It's up to the caller to not call us multiple times. */
7427 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
7428
7429 if (dwarf2_per_objfile->all_type_units.empty ())
7430 return;
7431
7432 /* TUs typically share abbrev tables, and there can be way more TUs than
7433 abbrev tables. Sort by abbrev table to reduce the number of times we
7434 read each abbrev table in.
7435 Alternatives are to punt or to maintain a cache of abbrev tables.
7436 This is simpler and efficient enough for now.
7437
7438 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7439 symtab to use). Typically TUs with the same abbrev offset have the same
7440 stmt_list value too so in practice this should work well.
7441
7442 The basic algorithm here is:
7443
7444 sort TUs by abbrev table
7445 for each TU with same abbrev table:
7446 read abbrev table if first user
7447 read TU top level DIE
7448 [IWBN if DWO skeletons had DW_AT_stmt_list]
7449 call FUNC */
7450
7451 if (dwarf_read_debug)
7452 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
7453
7454 /* Sort in a separate table to maintain the order of all_type_units
7455 for .gdb_index: TU indices directly index all_type_units. */
7456 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7457 sorted_by_abbrev.reserve (dwarf2_per_objfile->all_type_units.size ());
7458
7459 for (signatured_type *sig_type : dwarf2_per_objfile->all_type_units)
7460 sorted_by_abbrev.emplace_back
7461 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
7462 sig_type->per_cu.section,
7463 sig_type->per_cu.sect_off));
7464
7465 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7466 sort_tu_by_abbrev_offset);
7467
7468 abbrev_offset = (sect_offset) ~(unsigned) 0;
7469
7470 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
7471 {
7472 /* Switch to the next abbrev table if necessary. */
7473 if (abbrev_table == NULL
7474 || tu.abbrev_offset != abbrev_offset)
7475 {
7476 abbrev_offset = tu.abbrev_offset;
7477 abbrev_table =
7478 abbrev_table::read (dwarf2_per_objfile->objfile,
7479 &dwarf2_per_objfile->abbrev,
7480 abbrev_offset);
7481 ++tu_stats->nr_uniq_abbrev_tables;
7482 }
7483
7484 cutu_reader reader (&tu.sig_type->per_cu, abbrev_table.get (),
7485 0, false);
7486 if (!reader.dummy_p)
7487 build_type_psymtabs_reader (&reader, reader.info_ptr,
7488 reader.comp_unit_die);
7489 }
7490 }
7491
7492 /* Print collected type unit statistics. */
7493
7494 static void
7495 print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
7496 {
7497 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7498
7499 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
7500 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
7501 dwarf2_per_objfile->all_type_units.size ());
7502 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
7503 tu_stats->nr_uniq_abbrev_tables);
7504 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
7505 tu_stats->nr_symtabs);
7506 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
7507 tu_stats->nr_symtab_sharers);
7508 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
7509 tu_stats->nr_stmt_less_type_units);
7510 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
7511 tu_stats->nr_all_type_units_reallocs);
7512 }
7513
7514 /* Traversal function for build_type_psymtabs. */
7515
7516 static int
7517 build_type_psymtab_dependencies (void **slot, void *info)
7518 {
7519 struct dwarf2_per_objfile *dwarf2_per_objfile
7520 = (struct dwarf2_per_objfile *) info;
7521 struct objfile *objfile = dwarf2_per_objfile->objfile;
7522 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
7523 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
7524 dwarf2_psymtab *pst = per_cu->v.psymtab;
7525 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
7526 int i;
7527
7528 gdb_assert (len > 0);
7529 gdb_assert (per_cu->type_unit_group_p ());
7530
7531 pst->number_of_dependencies = len;
7532 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
7533 for (i = 0; i < len; ++i)
7534 {
7535 struct signatured_type *iter = tu_group->tus->at (i);
7536 gdb_assert (iter->per_cu.is_debug_types);
7537 pst->dependencies[i] = iter->per_cu.v.psymtab;
7538 iter->type_unit_group = tu_group;
7539 }
7540
7541 delete tu_group->tus;
7542 tu_group->tus = nullptr;
7543
7544 return 1;
7545 }
7546
7547 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7548 Build partial symbol tables for the .debug_types comp-units. */
7549
7550 static void
7551 build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
7552 {
7553 if (! create_all_type_units (dwarf2_per_objfile))
7554 return;
7555
7556 build_type_psymtabs_1 (dwarf2_per_objfile);
7557 }
7558
7559 /* Traversal function for process_skeletonless_type_unit.
7560 Read a TU in a DWO file and build partial symbols for it. */
7561
7562 static int
7563 process_skeletonless_type_unit (void **slot, void *info)
7564 {
7565 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
7566 struct dwarf2_per_objfile *dwarf2_per_objfile
7567 = (struct dwarf2_per_objfile *) info;
7568 struct signatured_type find_entry, *entry;
7569
7570 /* If this TU doesn't exist in the global table, add it and read it in. */
7571
7572 if (dwarf2_per_objfile->signatured_types == NULL)
7573 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
7574
7575 find_entry.signature = dwo_unit->signature;
7576 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
7577 &find_entry, INSERT);
7578 /* If we've already seen this type there's nothing to do. What's happening
7579 is we're doing our own version of comdat-folding here. */
7580 if (*slot != NULL)
7581 return 1;
7582
7583 /* This does the job that create_all_type_units would have done for
7584 this TU. */
7585 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
7586 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
7587 *slot = entry;
7588
7589 /* This does the job that build_type_psymtabs_1 would have done. */
7590 cutu_reader reader (&entry->per_cu, NULL, 0, false);
7591 if (!reader.dummy_p)
7592 build_type_psymtabs_reader (&reader, reader.info_ptr,
7593 reader.comp_unit_die);
7594
7595 return 1;
7596 }
7597
7598 /* Traversal function for process_skeletonless_type_units. */
7599
7600 static int
7601 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7602 {
7603 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7604
7605 if (dwo_file->tus != NULL)
7606 htab_traverse_noresize (dwo_file->tus.get (),
7607 process_skeletonless_type_unit, info);
7608
7609 return 1;
7610 }
7611
7612 /* Scan all TUs of DWO files, verifying we've processed them.
7613 This is needed in case a TU was emitted without its skeleton.
7614 Note: This can't be done until we know what all the DWO files are. */
7615
7616 static void
7617 process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
7618 {
7619 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
7620 if (get_dwp_file (dwarf2_per_objfile) == NULL
7621 && dwarf2_per_objfile->dwo_files != NULL)
7622 {
7623 htab_traverse_noresize (dwarf2_per_objfile->dwo_files.get (),
7624 process_dwo_file_for_skeletonless_type_units,
7625 dwarf2_per_objfile);
7626 }
7627 }
7628
7629 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
7630
7631 static void
7632 set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
7633 {
7634 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
7635 {
7636 dwarf2_psymtab *pst = per_cu->v.psymtab;
7637
7638 if (pst == NULL)
7639 continue;
7640
7641 for (int j = 0; j < pst->number_of_dependencies; ++j)
7642 {
7643 /* Set the 'user' field only if it is not already set. */
7644 if (pst->dependencies[j]->user == NULL)
7645 pst->dependencies[j]->user = pst;
7646 }
7647 }
7648 }
7649
7650 /* Build the partial symbol table by doing a quick pass through the
7651 .debug_info and .debug_abbrev sections. */
7652
7653 static void
7654 dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
7655 {
7656 struct objfile *objfile = dwarf2_per_objfile->objfile;
7657
7658 if (dwarf_read_debug)
7659 {
7660 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
7661 objfile_name (objfile));
7662 }
7663
7664 scoped_restore restore_reading_psyms
7665 = make_scoped_restore (&dwarf2_per_objfile->reading_partial_symbols,
7666 true);
7667
7668 dwarf2_per_objfile->info.read (objfile);
7669
7670 /* Any cached compilation units will be linked by the per-objfile
7671 read_in_chain. Make sure to free them when we're done. */
7672 free_cached_comp_units freer (dwarf2_per_objfile);
7673
7674 build_type_psymtabs (dwarf2_per_objfile);
7675
7676 create_all_comp_units (dwarf2_per_objfile);
7677
7678 /* Create a temporary address map on a temporary obstack. We later
7679 copy this to the final obstack. */
7680 auto_obstack temp_obstack;
7681
7682 scoped_restore save_psymtabs_addrmap
7683 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
7684 addrmap_create_mutable (&temp_obstack));
7685
7686 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
7687 process_psymtab_comp_unit (per_cu, false, language_minimal);
7688
7689 /* This has to wait until we read the CUs, we need the list of DWOs. */
7690 process_skeletonless_type_units (dwarf2_per_objfile);
7691
7692 /* Now that all TUs have been processed we can fill in the dependencies. */
7693 if (dwarf2_per_objfile->type_unit_groups != NULL)
7694 {
7695 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups.get (),
7696 build_type_psymtab_dependencies, dwarf2_per_objfile);
7697 }
7698
7699 if (dwarf_read_debug)
7700 print_tu_stats (dwarf2_per_objfile);
7701
7702 set_partial_user (dwarf2_per_objfile);
7703
7704 objfile->partial_symtabs->psymtabs_addrmap
7705 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
7706 objfile->partial_symtabs->obstack ());
7707 /* At this point we want to keep the address map. */
7708 save_psymtabs_addrmap.release ();
7709
7710 if (dwarf_read_debug)
7711 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
7712 objfile_name (objfile));
7713 }
7714
7715 /* Load the partial DIEs for a secondary CU into memory.
7716 This is also used when rereading a primary CU with load_all_dies. */
7717
7718 static void
7719 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
7720 {
7721 cutu_reader reader (this_cu, NULL, 1, false);
7722
7723 if (!reader.dummy_p)
7724 {
7725 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
7726 language_minimal);
7727
7728 /* Check if comp unit has_children.
7729 If so, read the rest of the partial symbols from this comp unit.
7730 If not, there's no more debug_info for this comp unit. */
7731 if (reader.comp_unit_die->has_children)
7732 load_partial_dies (&reader, reader.info_ptr, 0);
7733
7734 reader.keep ();
7735 }
7736 }
7737
7738 static void
7739 read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
7740 struct dwarf2_section_info *section,
7741 struct dwarf2_section_info *abbrev_section,
7742 unsigned int is_dwz)
7743 {
7744 const gdb_byte *info_ptr;
7745 struct objfile *objfile = dwarf2_per_objfile->objfile;
7746
7747 if (dwarf_read_debug)
7748 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
7749 section->get_name (),
7750 section->get_file_name ());
7751
7752 section->read (objfile);
7753
7754 info_ptr = section->buffer;
7755
7756 while (info_ptr < section->buffer + section->size)
7757 {
7758 struct dwarf2_per_cu_data *this_cu;
7759
7760 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
7761
7762 comp_unit_head cu_header;
7763 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
7764 abbrev_section, info_ptr,
7765 rcuh_kind::COMPILE);
7766
7767 /* Save the compilation unit for later lookup. */
7768 if (cu_header.unit_type != DW_UT_type)
7769 {
7770 this_cu = XOBNEW (&objfile->objfile_obstack,
7771 struct dwarf2_per_cu_data);
7772 memset (this_cu, 0, sizeof (*this_cu));
7773 }
7774 else
7775 {
7776 auto sig_type = XOBNEW (&objfile->objfile_obstack,
7777 struct signatured_type);
7778 memset (sig_type, 0, sizeof (*sig_type));
7779 sig_type->signature = cu_header.signature;
7780 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
7781 this_cu = &sig_type->per_cu;
7782 }
7783 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
7784 this_cu->sect_off = sect_off;
7785 this_cu->length = cu_header.length + cu_header.initial_length_size;
7786 this_cu->is_dwz = is_dwz;
7787 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
7788 this_cu->section = section;
7789
7790 dwarf2_per_objfile->all_comp_units.push_back (this_cu);
7791
7792 info_ptr = info_ptr + this_cu->length;
7793 }
7794 }
7795
7796 /* Create a list of all compilation units in OBJFILE.
7797 This is only done for -readnow and building partial symtabs. */
7798
7799 static void
7800 create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
7801 {
7802 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
7803 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
7804 &dwarf2_per_objfile->abbrev, 0);
7805
7806 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
7807 if (dwz != NULL)
7808 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
7809 1);
7810 }
7811
7812 /* Process all loaded DIEs for compilation unit CU, starting at
7813 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
7814 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
7815 DW_AT_ranges). See the comments of add_partial_subprogram on how
7816 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
7817
7818 static void
7819 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
7820 CORE_ADDR *highpc, int set_addrmap,
7821 struct dwarf2_cu *cu)
7822 {
7823 struct partial_die_info *pdi;
7824
7825 /* Now, march along the PDI's, descending into ones which have
7826 interesting children but skipping the children of the other ones,
7827 until we reach the end of the compilation unit. */
7828
7829 pdi = first_die;
7830
7831 while (pdi != NULL)
7832 {
7833 pdi->fixup (cu);
7834
7835 /* Anonymous namespaces or modules have no name but have interesting
7836 children, so we need to look at them. Ditto for anonymous
7837 enums. */
7838
7839 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
7840 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
7841 || pdi->tag == DW_TAG_imported_unit
7842 || pdi->tag == DW_TAG_inlined_subroutine)
7843 {
7844 switch (pdi->tag)
7845 {
7846 case DW_TAG_subprogram:
7847 case DW_TAG_inlined_subroutine:
7848 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
7849 break;
7850 case DW_TAG_constant:
7851 case DW_TAG_variable:
7852 case DW_TAG_typedef:
7853 case DW_TAG_union_type:
7854 if (!pdi->is_declaration)
7855 {
7856 add_partial_symbol (pdi, cu);
7857 }
7858 break;
7859 case DW_TAG_class_type:
7860 case DW_TAG_interface_type:
7861 case DW_TAG_structure_type:
7862 if (!pdi->is_declaration)
7863 {
7864 add_partial_symbol (pdi, cu);
7865 }
7866 if ((cu->language == language_rust
7867 || cu->language == language_cplus) && pdi->has_children)
7868 scan_partial_symbols (pdi->die_child, lowpc, highpc,
7869 set_addrmap, cu);
7870 break;
7871 case DW_TAG_enumeration_type:
7872 if (!pdi->is_declaration)
7873 add_partial_enumeration (pdi, cu);
7874 break;
7875 case DW_TAG_base_type:
7876 case DW_TAG_subrange_type:
7877 /* File scope base type definitions are added to the partial
7878 symbol table. */
7879 add_partial_symbol (pdi, cu);
7880 break;
7881 case DW_TAG_namespace:
7882 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
7883 break;
7884 case DW_TAG_module:
7885 if (!pdi->is_declaration)
7886 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
7887 break;
7888 case DW_TAG_imported_unit:
7889 {
7890 struct dwarf2_per_cu_data *per_cu;
7891
7892 /* For now we don't handle imported units in type units. */
7893 if (cu->per_cu->is_debug_types)
7894 {
7895 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7896 " supported in type units [in module %s]"),
7897 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
7898 }
7899
7900 per_cu = dwarf2_find_containing_comp_unit
7901 (pdi->d.sect_off, pdi->is_dwz,
7902 cu->per_cu->dwarf2_per_objfile);
7903
7904 /* Go read the partial unit, if needed. */
7905 if (per_cu->v.psymtab == NULL)
7906 process_psymtab_comp_unit (per_cu, true, cu->language);
7907
7908 cu->per_cu->imported_symtabs_push (per_cu);
7909 }
7910 break;
7911 case DW_TAG_imported_declaration:
7912 add_partial_symbol (pdi, cu);
7913 break;
7914 default:
7915 break;
7916 }
7917 }
7918
7919 /* If the die has a sibling, skip to the sibling. */
7920
7921 pdi = pdi->die_sibling;
7922 }
7923 }
7924
7925 /* Functions used to compute the fully scoped name of a partial DIE.
7926
7927 Normally, this is simple. For C++, the parent DIE's fully scoped
7928 name is concatenated with "::" and the partial DIE's name.
7929 Enumerators are an exception; they use the scope of their parent
7930 enumeration type, i.e. the name of the enumeration type is not
7931 prepended to the enumerator.
7932
7933 There are two complexities. One is DW_AT_specification; in this
7934 case "parent" means the parent of the target of the specification,
7935 instead of the direct parent of the DIE. The other is compilers
7936 which do not emit DW_TAG_namespace; in this case we try to guess
7937 the fully qualified name of structure types from their members'
7938 linkage names. This must be done using the DIE's children rather
7939 than the children of any DW_AT_specification target. We only need
7940 to do this for structures at the top level, i.e. if the target of
7941 any DW_AT_specification (if any; otherwise the DIE itself) does not
7942 have a parent. */
7943
7944 /* Compute the scope prefix associated with PDI's parent, in
7945 compilation unit CU. The result will be allocated on CU's
7946 comp_unit_obstack, or a copy of the already allocated PDI->NAME
7947 field. NULL is returned if no prefix is necessary. */
7948 static const char *
7949 partial_die_parent_scope (struct partial_die_info *pdi,
7950 struct dwarf2_cu *cu)
7951 {
7952 const char *grandparent_scope;
7953 struct partial_die_info *parent, *real_pdi;
7954
7955 /* We need to look at our parent DIE; if we have a DW_AT_specification,
7956 then this means the parent of the specification DIE. */
7957
7958 real_pdi = pdi;
7959 while (real_pdi->has_specification)
7960 {
7961 auto res = find_partial_die (real_pdi->spec_offset,
7962 real_pdi->spec_is_dwz, cu);
7963 real_pdi = res.pdi;
7964 cu = res.cu;
7965 }
7966
7967 parent = real_pdi->die_parent;
7968 if (parent == NULL)
7969 return NULL;
7970
7971 if (parent->scope_set)
7972 return parent->scope;
7973
7974 parent->fixup (cu);
7975
7976 grandparent_scope = partial_die_parent_scope (parent, cu);
7977
7978 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
7979 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
7980 Work around this problem here. */
7981 if (cu->language == language_cplus
7982 && parent->tag == DW_TAG_namespace
7983 && strcmp (parent->name, "::") == 0
7984 && grandparent_scope == NULL)
7985 {
7986 parent->scope = NULL;
7987 parent->scope_set = 1;
7988 return NULL;
7989 }
7990
7991 /* Nested subroutines in Fortran get a prefix. */
7992 if (pdi->tag == DW_TAG_enumerator)
7993 /* Enumerators should not get the name of the enumeration as a prefix. */
7994 parent->scope = grandparent_scope;
7995 else if (parent->tag == DW_TAG_namespace
7996 || parent->tag == DW_TAG_module
7997 || parent->tag == DW_TAG_structure_type
7998 || parent->tag == DW_TAG_class_type
7999 || parent->tag == DW_TAG_interface_type
8000 || parent->tag == DW_TAG_union_type
8001 || parent->tag == DW_TAG_enumeration_type
8002 || (cu->language == language_fortran
8003 && parent->tag == DW_TAG_subprogram
8004 && pdi->tag == DW_TAG_subprogram))
8005 {
8006 if (grandparent_scope == NULL)
8007 parent->scope = parent->name;
8008 else
8009 parent->scope = typename_concat (&cu->comp_unit_obstack,
8010 grandparent_scope,
8011 parent->name, 0, cu);
8012 }
8013 else
8014 {
8015 /* FIXME drow/2004-04-01: What should we be doing with
8016 function-local names? For partial symbols, we should probably be
8017 ignoring them. */
8018 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8019 dwarf_tag_name (parent->tag),
8020 sect_offset_str (pdi->sect_off));
8021 parent->scope = grandparent_scope;
8022 }
8023
8024 parent->scope_set = 1;
8025 return parent->scope;
8026 }
8027
8028 /* Return the fully scoped name associated with PDI, from compilation unit
8029 CU. The result will be allocated with malloc. */
8030
8031 static gdb::unique_xmalloc_ptr<char>
8032 partial_die_full_name (struct partial_die_info *pdi,
8033 struct dwarf2_cu *cu)
8034 {
8035 const char *parent_scope;
8036
8037 /* If this is a template instantiation, we can not work out the
8038 template arguments from partial DIEs. So, unfortunately, we have
8039 to go through the full DIEs. At least any work we do building
8040 types here will be reused if full symbols are loaded later. */
8041 if (pdi->has_template_arguments)
8042 {
8043 pdi->fixup (cu);
8044
8045 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8046 {
8047 struct die_info *die;
8048 struct attribute attr;
8049 struct dwarf2_cu *ref_cu = cu;
8050
8051 /* DW_FORM_ref_addr is using section offset. */
8052 attr.name = (enum dwarf_attribute) 0;
8053 attr.form = DW_FORM_ref_addr;
8054 attr.u.unsnd = to_underlying (pdi->sect_off);
8055 die = follow_die_ref (NULL, &attr, &ref_cu);
8056
8057 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8058 }
8059 }
8060
8061 parent_scope = partial_die_parent_scope (pdi, cu);
8062 if (parent_scope == NULL)
8063 return NULL;
8064 else
8065 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8066 pdi->name, 0, cu));
8067 }
8068
8069 static void
8070 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8071 {
8072 struct dwarf2_per_objfile *dwarf2_per_objfile
8073 = cu->per_cu->dwarf2_per_objfile;
8074 struct objfile *objfile = dwarf2_per_objfile->objfile;
8075 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8076 CORE_ADDR addr = 0;
8077 const char *actual_name = NULL;
8078 CORE_ADDR baseaddr;
8079
8080 baseaddr = objfile->text_section_offset ();
8081
8082 gdb::unique_xmalloc_ptr<char> built_actual_name
8083 = partial_die_full_name (pdi, cu);
8084 if (built_actual_name != NULL)
8085 actual_name = built_actual_name.get ();
8086
8087 if (actual_name == NULL)
8088 actual_name = pdi->name;
8089
8090 switch (pdi->tag)
8091 {
8092 case DW_TAG_inlined_subroutine:
8093 case DW_TAG_subprogram:
8094 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8095 - baseaddr);
8096 if (pdi->is_external
8097 || cu->language == language_ada
8098 || (cu->language == language_fortran
8099 && pdi->die_parent != NULL
8100 && pdi->die_parent->tag == DW_TAG_subprogram))
8101 {
8102 /* Normally, only "external" DIEs are part of the global scope.
8103 But in Ada and Fortran, we want to be able to access nested
8104 procedures globally. So all Ada and Fortran subprograms are
8105 stored in the global scope. */
8106 add_psymbol_to_list (actual_name,
8107 built_actual_name != NULL,
8108 VAR_DOMAIN, LOC_BLOCK,
8109 SECT_OFF_TEXT (objfile),
8110 psymbol_placement::GLOBAL,
8111 addr,
8112 cu->language, objfile);
8113 }
8114 else
8115 {
8116 add_psymbol_to_list (actual_name,
8117 built_actual_name != NULL,
8118 VAR_DOMAIN, LOC_BLOCK,
8119 SECT_OFF_TEXT (objfile),
8120 psymbol_placement::STATIC,
8121 addr, cu->language, objfile);
8122 }
8123
8124 if (pdi->main_subprogram && actual_name != NULL)
8125 set_objfile_main_name (objfile, actual_name, cu->language);
8126 break;
8127 case DW_TAG_constant:
8128 add_psymbol_to_list (actual_name,
8129 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
8130 -1, (pdi->is_external
8131 ? psymbol_placement::GLOBAL
8132 : psymbol_placement::STATIC),
8133 0, cu->language, objfile);
8134 break;
8135 case DW_TAG_variable:
8136 if (pdi->d.locdesc)
8137 addr = decode_locdesc (pdi->d.locdesc, cu);
8138
8139 if (pdi->d.locdesc
8140 && addr == 0
8141 && !dwarf2_per_objfile->has_section_at_zero)
8142 {
8143 /* A global or static variable may also have been stripped
8144 out by the linker if unused, in which case its address
8145 will be nullified; do not add such variables into partial
8146 symbol table then. */
8147 }
8148 else if (pdi->is_external)
8149 {
8150 /* Global Variable.
8151 Don't enter into the minimal symbol tables as there is
8152 a minimal symbol table entry from the ELF symbols already.
8153 Enter into partial symbol table if it has a location
8154 descriptor or a type.
8155 If the location descriptor is missing, new_symbol will create
8156 a LOC_UNRESOLVED symbol, the address of the variable will then
8157 be determined from the minimal symbol table whenever the variable
8158 is referenced.
8159 The address for the partial symbol table entry is not
8160 used by GDB, but it comes in handy for debugging partial symbol
8161 table building. */
8162
8163 if (pdi->d.locdesc || pdi->has_type)
8164 add_psymbol_to_list (actual_name,
8165 built_actual_name != NULL,
8166 VAR_DOMAIN, LOC_STATIC,
8167 SECT_OFF_TEXT (objfile),
8168 psymbol_placement::GLOBAL,
8169 addr, cu->language, objfile);
8170 }
8171 else
8172 {
8173 int has_loc = pdi->d.locdesc != NULL;
8174
8175 /* Static Variable. Skip symbols whose value we cannot know (those
8176 without location descriptors or constant values). */
8177 if (!has_loc && !pdi->has_const_value)
8178 return;
8179
8180 add_psymbol_to_list (actual_name,
8181 built_actual_name != NULL,
8182 VAR_DOMAIN, LOC_STATIC,
8183 SECT_OFF_TEXT (objfile),
8184 psymbol_placement::STATIC,
8185 has_loc ? addr : 0,
8186 cu->language, objfile);
8187 }
8188 break;
8189 case DW_TAG_typedef:
8190 case DW_TAG_base_type:
8191 case DW_TAG_subrange_type:
8192 add_psymbol_to_list (actual_name,
8193 built_actual_name != NULL,
8194 VAR_DOMAIN, LOC_TYPEDEF, -1,
8195 psymbol_placement::STATIC,
8196 0, cu->language, objfile);
8197 break;
8198 case DW_TAG_imported_declaration:
8199 case DW_TAG_namespace:
8200 add_psymbol_to_list (actual_name,
8201 built_actual_name != NULL,
8202 VAR_DOMAIN, LOC_TYPEDEF, -1,
8203 psymbol_placement::GLOBAL,
8204 0, cu->language, objfile);
8205 break;
8206 case DW_TAG_module:
8207 /* With Fortran 77 there might be a "BLOCK DATA" module
8208 available without any name. If so, we skip the module as it
8209 doesn't bring any value. */
8210 if (actual_name != nullptr)
8211 add_psymbol_to_list (actual_name,
8212 built_actual_name != NULL,
8213 MODULE_DOMAIN, LOC_TYPEDEF, -1,
8214 psymbol_placement::GLOBAL,
8215 0, cu->language, objfile);
8216 break;
8217 case DW_TAG_class_type:
8218 case DW_TAG_interface_type:
8219 case DW_TAG_structure_type:
8220 case DW_TAG_union_type:
8221 case DW_TAG_enumeration_type:
8222 /* Skip external references. The DWARF standard says in the section
8223 about "Structure, Union, and Class Type Entries": "An incomplete
8224 structure, union or class type is represented by a structure,
8225 union or class entry that does not have a byte size attribute
8226 and that has a DW_AT_declaration attribute." */
8227 if (!pdi->has_byte_size && pdi->is_declaration)
8228 return;
8229
8230 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8231 static vs. global. */
8232 add_psymbol_to_list (actual_name,
8233 built_actual_name != NULL,
8234 STRUCT_DOMAIN, LOC_TYPEDEF, -1,
8235 cu->language == language_cplus
8236 ? psymbol_placement::GLOBAL
8237 : psymbol_placement::STATIC,
8238 0, cu->language, objfile);
8239
8240 break;
8241 case DW_TAG_enumerator:
8242 add_psymbol_to_list (actual_name,
8243 built_actual_name != NULL,
8244 VAR_DOMAIN, LOC_CONST, -1,
8245 cu->language == language_cplus
8246 ? psymbol_placement::GLOBAL
8247 : psymbol_placement::STATIC,
8248 0, cu->language, objfile);
8249 break;
8250 default:
8251 break;
8252 }
8253 }
8254
8255 /* Read a partial die corresponding to a namespace; also, add a symbol
8256 corresponding to that namespace to the symbol table. NAMESPACE is
8257 the name of the enclosing namespace. */
8258
8259 static void
8260 add_partial_namespace (struct partial_die_info *pdi,
8261 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8262 int set_addrmap, struct dwarf2_cu *cu)
8263 {
8264 /* Add a symbol for the namespace. */
8265
8266 add_partial_symbol (pdi, cu);
8267
8268 /* Now scan partial symbols in that namespace. */
8269
8270 if (pdi->has_children)
8271 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8272 }
8273
8274 /* Read a partial die corresponding to a Fortran module. */
8275
8276 static void
8277 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
8278 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
8279 {
8280 /* Add a symbol for the namespace. */
8281
8282 add_partial_symbol (pdi, cu);
8283
8284 /* Now scan partial symbols in that module. */
8285
8286 if (pdi->has_children)
8287 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8288 }
8289
8290 /* Read a partial die corresponding to a subprogram or an inlined
8291 subprogram and create a partial symbol for that subprogram.
8292 When the CU language allows it, this routine also defines a partial
8293 symbol for each nested subprogram that this subprogram contains.
8294 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8295 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
8296
8297 PDI may also be a lexical block, in which case we simply search
8298 recursively for subprograms defined inside that lexical block.
8299 Again, this is only performed when the CU language allows this
8300 type of definitions. */
8301
8302 static void
8303 add_partial_subprogram (struct partial_die_info *pdi,
8304 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8305 int set_addrmap, struct dwarf2_cu *cu)
8306 {
8307 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
8308 {
8309 if (pdi->has_pc_info)
8310 {
8311 if (pdi->lowpc < *lowpc)
8312 *lowpc = pdi->lowpc;
8313 if (pdi->highpc > *highpc)
8314 *highpc = pdi->highpc;
8315 if (set_addrmap)
8316 {
8317 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
8318 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8319 CORE_ADDR baseaddr;
8320 CORE_ADDR this_highpc;
8321 CORE_ADDR this_lowpc;
8322
8323 baseaddr = objfile->text_section_offset ();
8324 this_lowpc
8325 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8326 pdi->lowpc + baseaddr)
8327 - baseaddr);
8328 this_highpc
8329 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8330 pdi->highpc + baseaddr)
8331 - baseaddr);
8332 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8333 this_lowpc, this_highpc - 1,
8334 cu->per_cu->v.psymtab);
8335 }
8336 }
8337
8338 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8339 {
8340 if (!pdi->is_declaration)
8341 /* Ignore subprogram DIEs that do not have a name, they are
8342 illegal. Do not emit a complaint at this point, we will
8343 do so when we convert this psymtab into a symtab. */
8344 if (pdi->name)
8345 add_partial_symbol (pdi, cu);
8346 }
8347 }
8348
8349 if (! pdi->has_children)
8350 return;
8351
8352 if (cu->language == language_ada || cu->language == language_fortran)
8353 {
8354 pdi = pdi->die_child;
8355 while (pdi != NULL)
8356 {
8357 pdi->fixup (cu);
8358 if (pdi->tag == DW_TAG_subprogram
8359 || pdi->tag == DW_TAG_inlined_subroutine
8360 || pdi->tag == DW_TAG_lexical_block)
8361 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8362 pdi = pdi->die_sibling;
8363 }
8364 }
8365 }
8366
8367 /* Read a partial die corresponding to an enumeration type. */
8368
8369 static void
8370 add_partial_enumeration (struct partial_die_info *enum_pdi,
8371 struct dwarf2_cu *cu)
8372 {
8373 struct partial_die_info *pdi;
8374
8375 if (enum_pdi->name != NULL)
8376 add_partial_symbol (enum_pdi, cu);
8377
8378 pdi = enum_pdi->die_child;
8379 while (pdi)
8380 {
8381 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
8382 complaint (_("malformed enumerator DIE ignored"));
8383 else
8384 add_partial_symbol (pdi, cu);
8385 pdi = pdi->die_sibling;
8386 }
8387 }
8388
8389 /* Return the initial uleb128 in the die at INFO_PTR. */
8390
8391 static unsigned int
8392 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
8393 {
8394 unsigned int bytes_read;
8395
8396 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8397 }
8398
8399 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8400 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8401
8402 Return the corresponding abbrev, or NULL if the number is zero (indicating
8403 an empty DIE). In either case *BYTES_READ will be set to the length of
8404 the initial number. */
8405
8406 static struct abbrev_info *
8407 peek_die_abbrev (const die_reader_specs &reader,
8408 const gdb_byte *info_ptr, unsigned int *bytes_read)
8409 {
8410 dwarf2_cu *cu = reader.cu;
8411 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
8412 unsigned int abbrev_number
8413 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
8414
8415 if (abbrev_number == 0)
8416 return NULL;
8417
8418 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
8419 if (!abbrev)
8420 {
8421 error (_("Dwarf Error: Could not find abbrev number %d in %s"
8422 " at offset %s [in module %s]"),
8423 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
8424 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
8425 }
8426
8427 return abbrev;
8428 }
8429
8430 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8431 Returns a pointer to the end of a series of DIEs, terminated by an empty
8432 DIE. Any children of the skipped DIEs will also be skipped. */
8433
8434 static const gdb_byte *
8435 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
8436 {
8437 while (1)
8438 {
8439 unsigned int bytes_read;
8440 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8441
8442 if (abbrev == NULL)
8443 return info_ptr + bytes_read;
8444 else
8445 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
8446 }
8447 }
8448
8449 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8450 INFO_PTR should point just after the initial uleb128 of a DIE, and the
8451 abbrev corresponding to that skipped uleb128 should be passed in
8452 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8453 children. */
8454
8455 static const gdb_byte *
8456 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
8457 struct abbrev_info *abbrev)
8458 {
8459 unsigned int bytes_read;
8460 struct attribute attr;
8461 bfd *abfd = reader->abfd;
8462 struct dwarf2_cu *cu = reader->cu;
8463 const gdb_byte *buffer = reader->buffer;
8464 const gdb_byte *buffer_end = reader->buffer_end;
8465 unsigned int form, i;
8466
8467 for (i = 0; i < abbrev->num_attrs; i++)
8468 {
8469 /* The only abbrev we care about is DW_AT_sibling. */
8470 if (abbrev->attrs[i].name == DW_AT_sibling)
8471 {
8472 bool ignored;
8473 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr,
8474 &ignored);
8475 if (attr.form == DW_FORM_ref_addr)
8476 complaint (_("ignoring absolute DW_AT_sibling"));
8477 else
8478 {
8479 sect_offset off = dwarf2_get_ref_die_offset (&attr);
8480 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
8481
8482 if (sibling_ptr < info_ptr)
8483 complaint (_("DW_AT_sibling points backwards"));
8484 else if (sibling_ptr > reader->buffer_end)
8485 reader->die_section->overflow_complaint ();
8486 else
8487 return sibling_ptr;
8488 }
8489 }
8490
8491 /* If it isn't DW_AT_sibling, skip this attribute. */
8492 form = abbrev->attrs[i].form;
8493 skip_attribute:
8494 switch (form)
8495 {
8496 case DW_FORM_ref_addr:
8497 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8498 and later it is offset sized. */
8499 if (cu->header.version == 2)
8500 info_ptr += cu->header.addr_size;
8501 else
8502 info_ptr += cu->header.offset_size;
8503 break;
8504 case DW_FORM_GNU_ref_alt:
8505 info_ptr += cu->header.offset_size;
8506 break;
8507 case DW_FORM_addr:
8508 info_ptr += cu->header.addr_size;
8509 break;
8510 case DW_FORM_data1:
8511 case DW_FORM_ref1:
8512 case DW_FORM_flag:
8513 case DW_FORM_strx1:
8514 info_ptr += 1;
8515 break;
8516 case DW_FORM_flag_present:
8517 case DW_FORM_implicit_const:
8518 break;
8519 case DW_FORM_data2:
8520 case DW_FORM_ref2:
8521 case DW_FORM_strx2:
8522 info_ptr += 2;
8523 break;
8524 case DW_FORM_strx3:
8525 info_ptr += 3;
8526 break;
8527 case DW_FORM_data4:
8528 case DW_FORM_ref4:
8529 case DW_FORM_strx4:
8530 info_ptr += 4;
8531 break;
8532 case DW_FORM_data8:
8533 case DW_FORM_ref8:
8534 case DW_FORM_ref_sig8:
8535 info_ptr += 8;
8536 break;
8537 case DW_FORM_data16:
8538 info_ptr += 16;
8539 break;
8540 case DW_FORM_string:
8541 read_direct_string (abfd, info_ptr, &bytes_read);
8542 info_ptr += bytes_read;
8543 break;
8544 case DW_FORM_sec_offset:
8545 case DW_FORM_strp:
8546 case DW_FORM_GNU_strp_alt:
8547 info_ptr += cu->header.offset_size;
8548 break;
8549 case DW_FORM_exprloc:
8550 case DW_FORM_block:
8551 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8552 info_ptr += bytes_read;
8553 break;
8554 case DW_FORM_block1:
8555 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8556 break;
8557 case DW_FORM_block2:
8558 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8559 break;
8560 case DW_FORM_block4:
8561 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8562 break;
8563 case DW_FORM_addrx:
8564 case DW_FORM_strx:
8565 case DW_FORM_sdata:
8566 case DW_FORM_udata:
8567 case DW_FORM_ref_udata:
8568 case DW_FORM_GNU_addr_index:
8569 case DW_FORM_GNU_str_index:
8570 case DW_FORM_rnglistx:
8571 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
8572 break;
8573 case DW_FORM_indirect:
8574 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8575 info_ptr += bytes_read;
8576 /* We need to continue parsing from here, so just go back to
8577 the top. */
8578 goto skip_attribute;
8579
8580 default:
8581 error (_("Dwarf Error: Cannot handle %s "
8582 "in DWARF reader [in module %s]"),
8583 dwarf_form_name (form),
8584 bfd_get_filename (abfd));
8585 }
8586 }
8587
8588 if (abbrev->has_children)
8589 return skip_children (reader, info_ptr);
8590 else
8591 return info_ptr;
8592 }
8593
8594 /* Locate ORIG_PDI's sibling.
8595 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
8596
8597 static const gdb_byte *
8598 locate_pdi_sibling (const struct die_reader_specs *reader,
8599 struct partial_die_info *orig_pdi,
8600 const gdb_byte *info_ptr)
8601 {
8602 /* Do we know the sibling already? */
8603
8604 if (orig_pdi->sibling)
8605 return orig_pdi->sibling;
8606
8607 /* Are there any children to deal with? */
8608
8609 if (!orig_pdi->has_children)
8610 return info_ptr;
8611
8612 /* Skip the children the long way. */
8613
8614 return skip_children (reader, info_ptr);
8615 }
8616
8617 /* Expand this partial symbol table into a full symbol table. SELF is
8618 not NULL. */
8619
8620 void
8621 dwarf2_psymtab::read_symtab (struct objfile *objfile)
8622 {
8623 struct dwarf2_per_objfile *dwarf2_per_objfile
8624 = get_dwarf2_per_objfile (objfile);
8625
8626 gdb_assert (!readin);
8627 /* If this psymtab is constructed from a debug-only objfile, the
8628 has_section_at_zero flag will not necessarily be correct. We
8629 can get the correct value for this flag by looking at the data
8630 associated with the (presumably stripped) associated objfile. */
8631 if (objfile->separate_debug_objfile_backlink)
8632 {
8633 struct dwarf2_per_objfile *dpo_backlink
8634 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
8635
8636 dwarf2_per_objfile->has_section_at_zero
8637 = dpo_backlink->has_section_at_zero;
8638 }
8639
8640 expand_psymtab (objfile);
8641
8642 process_cu_includes (dwarf2_per_objfile);
8643 }
8644 \f
8645 /* Reading in full CUs. */
8646
8647 /* Add PER_CU to the queue. */
8648
8649 static void
8650 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
8651 enum language pretend_language)
8652 {
8653 per_cu->queued = 1;
8654 per_cu->dwarf2_per_objfile->queue.emplace (per_cu, pretend_language);
8655 }
8656
8657 /* If PER_CU is not yet queued, add it to the queue.
8658 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8659 dependency.
8660 The result is non-zero if PER_CU was queued, otherwise the result is zero
8661 meaning either PER_CU is already queued or it is already loaded.
8662
8663 N.B. There is an invariant here that if a CU is queued then it is loaded.
8664 The caller is required to load PER_CU if we return non-zero. */
8665
8666 static int
8667 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
8668 struct dwarf2_per_cu_data *per_cu,
8669 enum language pretend_language)
8670 {
8671 /* We may arrive here during partial symbol reading, if we need full
8672 DIEs to process an unusual case (e.g. template arguments). Do
8673 not queue PER_CU, just tell our caller to load its DIEs. */
8674 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
8675 {
8676 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
8677 return 1;
8678 return 0;
8679 }
8680
8681 /* Mark the dependence relation so that we don't flush PER_CU
8682 too early. */
8683 if (dependent_cu != NULL)
8684 dwarf2_add_dependence (dependent_cu, per_cu);
8685
8686 /* If it's already on the queue, we have nothing to do. */
8687 if (per_cu->queued)
8688 return 0;
8689
8690 /* If the compilation unit is already loaded, just mark it as
8691 used. */
8692 if (per_cu->cu != NULL)
8693 {
8694 per_cu->cu->last_used = 0;
8695 return 0;
8696 }
8697
8698 /* Add it to the queue. */
8699 queue_comp_unit (per_cu, pretend_language);
8700
8701 return 1;
8702 }
8703
8704 /* Process the queue. */
8705
8706 static void
8707 process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
8708 {
8709 if (dwarf_read_debug)
8710 {
8711 fprintf_unfiltered (gdb_stdlog,
8712 "Expanding one or more symtabs of objfile %s ...\n",
8713 objfile_name (dwarf2_per_objfile->objfile));
8714 }
8715
8716 /* The queue starts out with one item, but following a DIE reference
8717 may load a new CU, adding it to the end of the queue. */
8718 while (!dwarf2_per_objfile->queue.empty ())
8719 {
8720 dwarf2_queue_item &item = dwarf2_per_objfile->queue.front ();
8721
8722 if ((dwarf2_per_objfile->using_index
8723 ? !item.per_cu->v.quick->compunit_symtab
8724 : (item.per_cu->v.psymtab && !item.per_cu->v.psymtab->readin))
8725 /* Skip dummy CUs. */
8726 && item.per_cu->cu != NULL)
8727 {
8728 struct dwarf2_per_cu_data *per_cu = item.per_cu;
8729 unsigned int debug_print_threshold;
8730 char buf[100];
8731
8732 if (per_cu->is_debug_types)
8733 {
8734 struct signatured_type *sig_type =
8735 (struct signatured_type *) per_cu;
8736
8737 sprintf (buf, "TU %s at offset %s",
8738 hex_string (sig_type->signature),
8739 sect_offset_str (per_cu->sect_off));
8740 /* There can be 100s of TUs.
8741 Only print them in verbose mode. */
8742 debug_print_threshold = 2;
8743 }
8744 else
8745 {
8746 sprintf (buf, "CU at offset %s",
8747 sect_offset_str (per_cu->sect_off));
8748 debug_print_threshold = 1;
8749 }
8750
8751 if (dwarf_read_debug >= debug_print_threshold)
8752 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
8753
8754 if (per_cu->is_debug_types)
8755 process_full_type_unit (per_cu, item.pretend_language);
8756 else
8757 process_full_comp_unit (per_cu, item.pretend_language);
8758
8759 if (dwarf_read_debug >= debug_print_threshold)
8760 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
8761 }
8762
8763 item.per_cu->queued = 0;
8764 dwarf2_per_objfile->queue.pop ();
8765 }
8766
8767 if (dwarf_read_debug)
8768 {
8769 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
8770 objfile_name (dwarf2_per_objfile->objfile));
8771 }
8772 }
8773
8774 /* Read in full symbols for PST, and anything it depends on. */
8775
8776 void
8777 dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
8778 {
8779 if (readin)
8780 return;
8781
8782 read_dependencies (objfile);
8783
8784 dw2_do_instantiate_symtab (per_cu_data, false);
8785 gdb_assert (get_compunit_symtab () != nullptr);
8786 }
8787
8788 /* Trivial hash function for die_info: the hash value of a DIE
8789 is its offset in .debug_info for this objfile. */
8790
8791 static hashval_t
8792 die_hash (const void *item)
8793 {
8794 const struct die_info *die = (const struct die_info *) item;
8795
8796 return to_underlying (die->sect_off);
8797 }
8798
8799 /* Trivial comparison function for die_info structures: two DIEs
8800 are equal if they have the same offset. */
8801
8802 static int
8803 die_eq (const void *item_lhs, const void *item_rhs)
8804 {
8805 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
8806 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
8807
8808 return die_lhs->sect_off == die_rhs->sect_off;
8809 }
8810
8811 /* Load the DIEs associated with PER_CU into memory. */
8812
8813 static void
8814 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
8815 bool skip_partial,
8816 enum language pretend_language)
8817 {
8818 gdb_assert (! this_cu->is_debug_types);
8819
8820 cutu_reader reader (this_cu, NULL, 1, skip_partial);
8821 if (reader.dummy_p)
8822 return;
8823
8824 struct dwarf2_cu *cu = reader.cu;
8825 const gdb_byte *info_ptr = reader.info_ptr;
8826
8827 gdb_assert (cu->die_hash == NULL);
8828 cu->die_hash =
8829 htab_create_alloc_ex (cu->header.length / 12,
8830 die_hash,
8831 die_eq,
8832 NULL,
8833 &cu->comp_unit_obstack,
8834 hashtab_obstack_allocate,
8835 dummy_obstack_deallocate);
8836
8837 if (reader.comp_unit_die->has_children)
8838 reader.comp_unit_die->child
8839 = read_die_and_siblings (&reader, reader.info_ptr,
8840 &info_ptr, reader.comp_unit_die);
8841 cu->dies = reader.comp_unit_die;
8842 /* comp_unit_die is not stored in die_hash, no need. */
8843
8844 /* We try not to read any attributes in this function, because not
8845 all CUs needed for references have been loaded yet, and symbol
8846 table processing isn't initialized. But we have to set the CU language,
8847 or we won't be able to build types correctly.
8848 Similarly, if we do not read the producer, we can not apply
8849 producer-specific interpretation. */
8850 prepare_one_comp_unit (cu, cu->dies, pretend_language);
8851
8852 reader.keep ();
8853 }
8854
8855 /* Add a DIE to the delayed physname list. */
8856
8857 static void
8858 add_to_method_list (struct type *type, int fnfield_index, int index,
8859 const char *name, struct die_info *die,
8860 struct dwarf2_cu *cu)
8861 {
8862 struct delayed_method_info mi;
8863 mi.type = type;
8864 mi.fnfield_index = fnfield_index;
8865 mi.index = index;
8866 mi.name = name;
8867 mi.die = die;
8868 cu->method_list.push_back (mi);
8869 }
8870
8871 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
8872 "const" / "volatile". If so, decrements LEN by the length of the
8873 modifier and return true. Otherwise return false. */
8874
8875 template<size_t N>
8876 static bool
8877 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
8878 {
8879 size_t mod_len = sizeof (mod) - 1;
8880 if (len > mod_len && startswith (physname + (len - mod_len), mod))
8881 {
8882 len -= mod_len;
8883 return true;
8884 }
8885 return false;
8886 }
8887
8888 /* Compute the physnames of any methods on the CU's method list.
8889
8890 The computation of method physnames is delayed in order to avoid the
8891 (bad) condition that one of the method's formal parameters is of an as yet
8892 incomplete type. */
8893
8894 static void
8895 compute_delayed_physnames (struct dwarf2_cu *cu)
8896 {
8897 /* Only C++ delays computing physnames. */
8898 if (cu->method_list.empty ())
8899 return;
8900 gdb_assert (cu->language == language_cplus);
8901
8902 for (const delayed_method_info &mi : cu->method_list)
8903 {
8904 const char *physname;
8905 struct fn_fieldlist *fn_flp
8906 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
8907 physname = dwarf2_physname (mi.name, mi.die, cu);
8908 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
8909 = physname ? physname : "";
8910
8911 /* Since there's no tag to indicate whether a method is a
8912 const/volatile overload, extract that information out of the
8913 demangled name. */
8914 if (physname != NULL)
8915 {
8916 size_t len = strlen (physname);
8917
8918 while (1)
8919 {
8920 if (physname[len] == ')') /* shortcut */
8921 break;
8922 else if (check_modifier (physname, len, " const"))
8923 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
8924 else if (check_modifier (physname, len, " volatile"))
8925 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
8926 else
8927 break;
8928 }
8929 }
8930 }
8931
8932 /* The list is no longer needed. */
8933 cu->method_list.clear ();
8934 }
8935
8936 /* Go objects should be embedded in a DW_TAG_module DIE,
8937 and it's not clear if/how imported objects will appear.
8938 To keep Go support simple until that's worked out,
8939 go back through what we've read and create something usable.
8940 We could do this while processing each DIE, and feels kinda cleaner,
8941 but that way is more invasive.
8942 This is to, for example, allow the user to type "p var" or "b main"
8943 without having to specify the package name, and allow lookups
8944 of module.object to work in contexts that use the expression
8945 parser. */
8946
8947 static void
8948 fixup_go_packaging (struct dwarf2_cu *cu)
8949 {
8950 gdb::unique_xmalloc_ptr<char> package_name;
8951 struct pending *list;
8952 int i;
8953
8954 for (list = *cu->get_builder ()->get_global_symbols ();
8955 list != NULL;
8956 list = list->next)
8957 {
8958 for (i = 0; i < list->nsyms; ++i)
8959 {
8960 struct symbol *sym = list->symbol[i];
8961
8962 if (sym->language () == language_go
8963 && SYMBOL_CLASS (sym) == LOC_BLOCK)
8964 {
8965 gdb::unique_xmalloc_ptr<char> this_package_name
8966 (go_symbol_package_name (sym));
8967
8968 if (this_package_name == NULL)
8969 continue;
8970 if (package_name == NULL)
8971 package_name = std::move (this_package_name);
8972 else
8973 {
8974 struct objfile *objfile
8975 = cu->per_cu->dwarf2_per_objfile->objfile;
8976 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
8977 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
8978 (symbol_symtab (sym) != NULL
8979 ? symtab_to_filename_for_display
8980 (symbol_symtab (sym))
8981 : objfile_name (objfile)),
8982 this_package_name.get (), package_name.get ());
8983 }
8984 }
8985 }
8986 }
8987
8988 if (package_name != NULL)
8989 {
8990 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
8991 const char *saved_package_name = objfile->intern (package_name.get ());
8992 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
8993 saved_package_name);
8994 struct symbol *sym;
8995
8996 sym = allocate_symbol (objfile);
8997 sym->set_language (language_go, &objfile->objfile_obstack);
8998 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
8999 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9000 e.g., "main" finds the "main" module and not C's main(). */
9001 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9002 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9003 SYMBOL_TYPE (sym) = type;
9004
9005 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
9006 }
9007 }
9008
9009 /* Allocate a fully-qualified name consisting of the two parts on the
9010 obstack. */
9011
9012 static const char *
9013 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9014 {
9015 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9016 }
9017
9018 /* A helper that allocates a struct discriminant_info to attach to a
9019 union type. */
9020
9021 static struct discriminant_info *
9022 alloc_discriminant_info (struct type *type, int discriminant_index,
9023 int default_index)
9024 {
9025 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9026 gdb_assert (discriminant_index == -1
9027 || (discriminant_index >= 0
9028 && discriminant_index < TYPE_NFIELDS (type)));
9029 gdb_assert (default_index == -1
9030 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
9031
9032 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
9033
9034 struct discriminant_info *disc
9035 = ((struct discriminant_info *)
9036 TYPE_ZALLOC (type,
9037 offsetof (struct discriminant_info, discriminants)
9038 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
9039 disc->default_index = default_index;
9040 disc->discriminant_index = discriminant_index;
9041
9042 struct dynamic_prop prop;
9043 prop.kind = PROP_UNDEFINED;
9044 prop.data.baton = disc;
9045
9046 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
9047
9048 return disc;
9049 }
9050
9051 /* Some versions of rustc emitted enums in an unusual way.
9052
9053 Ordinary enums were emitted as unions. The first element of each
9054 structure in the union was named "RUST$ENUM$DISR". This element
9055 held the discriminant.
9056
9057 These versions of Rust also implemented the "non-zero"
9058 optimization. When the enum had two values, and one is empty and
9059 the other holds a pointer that cannot be zero, the pointer is used
9060 as the discriminant, with a zero value meaning the empty variant.
9061 Here, the union's first member is of the form
9062 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9063 where the fieldnos are the indices of the fields that should be
9064 traversed in order to find the field (which may be several fields deep)
9065 and the variantname is the name of the variant of the case when the
9066 field is zero.
9067
9068 This function recognizes whether TYPE is of one of these forms,
9069 and, if so, smashes it to be a variant type. */
9070
9071 static void
9072 quirk_rust_enum (struct type *type, struct objfile *objfile)
9073 {
9074 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9075
9076 /* We don't need to deal with empty enums. */
9077 if (TYPE_NFIELDS (type) == 0)
9078 return;
9079
9080 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9081 if (TYPE_NFIELDS (type) == 1
9082 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9083 {
9084 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9085
9086 /* Decode the field name to find the offset of the
9087 discriminant. */
9088 ULONGEST bit_offset = 0;
9089 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9090 while (name[0] >= '0' && name[0] <= '9')
9091 {
9092 char *tail;
9093 unsigned long index = strtoul (name, &tail, 10);
9094 name = tail;
9095 if (*name != '$'
9096 || index >= TYPE_NFIELDS (field_type)
9097 || (TYPE_FIELD_LOC_KIND (field_type, index)
9098 != FIELD_LOC_KIND_BITPOS))
9099 {
9100 complaint (_("Could not parse Rust enum encoding string \"%s\""
9101 "[in module %s]"),
9102 TYPE_FIELD_NAME (type, 0),
9103 objfile_name (objfile));
9104 return;
9105 }
9106 ++name;
9107
9108 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9109 field_type = TYPE_FIELD_TYPE (field_type, index);
9110 }
9111
9112 /* Make a union to hold the variants. */
9113 struct type *union_type = alloc_type (objfile);
9114 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9115 TYPE_NFIELDS (union_type) = 3;
9116 TYPE_FIELDS (union_type)
9117 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
9118 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9119 set_type_align (union_type, TYPE_RAW_ALIGN (type));
9120
9121 /* Put the discriminant must at index 0. */
9122 TYPE_FIELD_TYPE (union_type, 0) = field_type;
9123 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9124 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9125 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
9126
9127 /* The order of fields doesn't really matter, so put the real
9128 field at index 1 and the data-less field at index 2. */
9129 struct discriminant_info *disc
9130 = alloc_discriminant_info (union_type, 0, 1);
9131 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
9132 TYPE_FIELD_NAME (union_type, 1)
9133 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
9134 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
9135 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9136 TYPE_FIELD_NAME (union_type, 1));
9137
9138 const char *dataless_name
9139 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9140 name);
9141 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9142 dataless_name);
9143 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
9144 /* NAME points into the original discriminant name, which
9145 already has the correct lifetime. */
9146 TYPE_FIELD_NAME (union_type, 2) = name;
9147 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
9148 disc->discriminants[2] = 0;
9149
9150 /* Smash this type to be a structure type. We have to do this
9151 because the type has already been recorded. */
9152 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9153 TYPE_NFIELDS (type) = 1;
9154 TYPE_FIELDS (type)
9155 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
9156
9157 /* Install the variant part. */
9158 TYPE_FIELD_TYPE (type, 0) = union_type;
9159 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9160 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9161 }
9162 /* A union with a single anonymous field is probably an old-style
9163 univariant enum. */
9164 else if (TYPE_NFIELDS (type) == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
9165 {
9166 /* Smash this type to be a structure type. We have to do this
9167 because the type has already been recorded. */
9168 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9169
9170 /* Make a union to hold the variants. */
9171 struct type *union_type = alloc_type (objfile);
9172 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9173 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
9174 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9175 set_type_align (union_type, TYPE_RAW_ALIGN (type));
9176 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
9177
9178 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
9179 const char *variant_name
9180 = rust_last_path_segment (TYPE_NAME (field_type));
9181 TYPE_FIELD_NAME (union_type, 0) = variant_name;
9182 TYPE_NAME (field_type)
9183 = rust_fully_qualify (&objfile->objfile_obstack,
9184 TYPE_NAME (type), variant_name);
9185
9186 /* Install the union in the outer struct type. */
9187 TYPE_NFIELDS (type) = 1;
9188 TYPE_FIELDS (type)
9189 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
9190 TYPE_FIELD_TYPE (type, 0) = union_type;
9191 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9192 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9193
9194 alloc_discriminant_info (union_type, -1, 0);
9195 }
9196 else
9197 {
9198 struct type *disr_type = nullptr;
9199 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
9200 {
9201 disr_type = TYPE_FIELD_TYPE (type, i);
9202
9203 if (TYPE_CODE (disr_type) != TYPE_CODE_STRUCT)
9204 {
9205 /* All fields of a true enum will be structs. */
9206 return;
9207 }
9208 else if (TYPE_NFIELDS (disr_type) == 0)
9209 {
9210 /* Could be data-less variant, so keep going. */
9211 disr_type = nullptr;
9212 }
9213 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9214 "RUST$ENUM$DISR") != 0)
9215 {
9216 /* Not a Rust enum. */
9217 return;
9218 }
9219 else
9220 {
9221 /* Found one. */
9222 break;
9223 }
9224 }
9225
9226 /* If we got here without a discriminant, then it's probably
9227 just a union. */
9228 if (disr_type == nullptr)
9229 return;
9230
9231 /* Smash this type to be a structure type. We have to do this
9232 because the type has already been recorded. */
9233 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9234
9235 /* Make a union to hold the variants. */
9236 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
9237 struct type *union_type = alloc_type (objfile);
9238 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9239 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
9240 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9241 set_type_align (union_type, TYPE_RAW_ALIGN (type));
9242 TYPE_FIELDS (union_type)
9243 = (struct field *) TYPE_ZALLOC (union_type,
9244 (TYPE_NFIELDS (union_type)
9245 * sizeof (struct field)));
9246
9247 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
9248 TYPE_NFIELDS (type) * sizeof (struct field));
9249
9250 /* Install the discriminant at index 0 in the union. */
9251 TYPE_FIELD (union_type, 0) = *disr_field;
9252 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9253 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9254
9255 /* Install the union in the outer struct type. */
9256 TYPE_FIELD_TYPE (type, 0) = union_type;
9257 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9258 TYPE_NFIELDS (type) = 1;
9259
9260 /* Set the size and offset of the union type. */
9261 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9262
9263 /* We need a way to find the correct discriminant given a
9264 variant name. For convenience we build a map here. */
9265 struct type *enum_type = FIELD_TYPE (*disr_field);
9266 std::unordered_map<std::string, ULONGEST> discriminant_map;
9267 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
9268 {
9269 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9270 {
9271 const char *name
9272 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9273 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9274 }
9275 }
9276
9277 int n_fields = TYPE_NFIELDS (union_type);
9278 struct discriminant_info *disc
9279 = alloc_discriminant_info (union_type, 0, -1);
9280 /* Skip the discriminant here. */
9281 for (int i = 1; i < n_fields; ++i)
9282 {
9283 /* Find the final word in the name of this variant's type.
9284 That name can be used to look up the correct
9285 discriminant. */
9286 const char *variant_name
9287 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
9288 i)));
9289
9290 auto iter = discriminant_map.find (variant_name);
9291 if (iter != discriminant_map.end ())
9292 disc->discriminants[i] = iter->second;
9293
9294 /* Remove the discriminant field, if it exists. */
9295 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
9296 if (TYPE_NFIELDS (sub_type) > 0)
9297 {
9298 --TYPE_NFIELDS (sub_type);
9299 ++TYPE_FIELDS (sub_type);
9300 }
9301 TYPE_FIELD_NAME (union_type, i) = variant_name;
9302 TYPE_NAME (sub_type)
9303 = rust_fully_qualify (&objfile->objfile_obstack,
9304 TYPE_NAME (type), variant_name);
9305 }
9306 }
9307 }
9308
9309 /* Rewrite some Rust unions to be structures with variants parts. */
9310
9311 static void
9312 rust_union_quirks (struct dwarf2_cu *cu)
9313 {
9314 gdb_assert (cu->language == language_rust);
9315 for (type *type_ : cu->rust_unions)
9316 quirk_rust_enum (type_, cu->per_cu->dwarf2_per_objfile->objfile);
9317 /* We don't need this any more. */
9318 cu->rust_unions.clear ();
9319 }
9320
9321 /* Return the symtab for PER_CU. This works properly regardless of
9322 whether we're using the index or psymtabs. */
9323
9324 static struct compunit_symtab *
9325 get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
9326 {
9327 return (per_cu->dwarf2_per_objfile->using_index
9328 ? per_cu->v.quick->compunit_symtab
9329 : per_cu->v.psymtab->compunit_symtab);
9330 }
9331
9332 /* A helper function for computing the list of all symbol tables
9333 included by PER_CU. */
9334
9335 static void
9336 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
9337 htab_t all_children, htab_t all_type_symtabs,
9338 struct dwarf2_per_cu_data *per_cu,
9339 struct compunit_symtab *immediate_parent)
9340 {
9341 void **slot;
9342 struct compunit_symtab *cust;
9343
9344 slot = htab_find_slot (all_children, per_cu, INSERT);
9345 if (*slot != NULL)
9346 {
9347 /* This inclusion and its children have been processed. */
9348 return;
9349 }
9350
9351 *slot = per_cu;
9352 /* Only add a CU if it has a symbol table. */
9353 cust = get_compunit_symtab (per_cu);
9354 if (cust != NULL)
9355 {
9356 /* If this is a type unit only add its symbol table if we haven't
9357 seen it yet (type unit per_cu's can share symtabs). */
9358 if (per_cu->is_debug_types)
9359 {
9360 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
9361 if (*slot == NULL)
9362 {
9363 *slot = cust;
9364 result->push_back (cust);
9365 if (cust->user == NULL)
9366 cust->user = immediate_parent;
9367 }
9368 }
9369 else
9370 {
9371 result->push_back (cust);
9372 if (cust->user == NULL)
9373 cust->user = immediate_parent;
9374 }
9375 }
9376
9377 if (!per_cu->imported_symtabs_empty ())
9378 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9379 {
9380 recursively_compute_inclusions (result, all_children,
9381 all_type_symtabs, ptr, cust);
9382 }
9383 }
9384
9385 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
9386 PER_CU. */
9387
9388 static void
9389 compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
9390 {
9391 gdb_assert (! per_cu->is_debug_types);
9392
9393 if (!per_cu->imported_symtabs_empty ())
9394 {
9395 int len;
9396 std::vector<compunit_symtab *> result_symtabs;
9397 htab_t all_children, all_type_symtabs;
9398 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
9399
9400 /* If we don't have a symtab, we can just skip this case. */
9401 if (cust == NULL)
9402 return;
9403
9404 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9405 NULL, xcalloc, xfree);
9406 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9407 NULL, xcalloc, xfree);
9408
9409 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9410 {
9411 recursively_compute_inclusions (&result_symtabs, all_children,
9412 all_type_symtabs, ptr, cust);
9413 }
9414
9415 /* Now we have a transitive closure of all the included symtabs. */
9416 len = result_symtabs.size ();
9417 cust->includes
9418 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
9419 struct compunit_symtab *, len + 1);
9420 memcpy (cust->includes, result_symtabs.data (),
9421 len * sizeof (compunit_symtab *));
9422 cust->includes[len] = NULL;
9423
9424 htab_delete (all_children);
9425 htab_delete (all_type_symtabs);
9426 }
9427 }
9428
9429 /* Compute the 'includes' field for the symtabs of all the CUs we just
9430 read. */
9431
9432 static void
9433 process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
9434 {
9435 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->just_read_cus)
9436 {
9437 if (! iter->is_debug_types)
9438 compute_compunit_symtab_includes (iter);
9439 }
9440
9441 dwarf2_per_objfile->just_read_cus.clear ();
9442 }
9443
9444 /* Generate full symbol information for PER_CU, whose DIEs have
9445 already been loaded into memory. */
9446
9447 static void
9448 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
9449 enum language pretend_language)
9450 {
9451 struct dwarf2_cu *cu = per_cu->cu;
9452 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9453 struct objfile *objfile = dwarf2_per_objfile->objfile;
9454 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9455 CORE_ADDR lowpc, highpc;
9456 struct compunit_symtab *cust;
9457 CORE_ADDR baseaddr;
9458 struct block *static_block;
9459 CORE_ADDR addr;
9460
9461 baseaddr = objfile->text_section_offset ();
9462
9463 /* Clear the list here in case something was left over. */
9464 cu->method_list.clear ();
9465
9466 cu->language = pretend_language;
9467 cu->language_defn = language_def (cu->language);
9468
9469 /* Do line number decoding in read_file_scope () */
9470 process_die (cu->dies, cu);
9471
9472 /* For now fudge the Go package. */
9473 if (cu->language == language_go)
9474 fixup_go_packaging (cu);
9475
9476 /* Now that we have processed all the DIEs in the CU, all the types
9477 should be complete, and it should now be safe to compute all of the
9478 physnames. */
9479 compute_delayed_physnames (cu);
9480
9481 if (cu->language == language_rust)
9482 rust_union_quirks (cu);
9483
9484 /* Some compilers don't define a DW_AT_high_pc attribute for the
9485 compilation unit. If the DW_AT_high_pc is missing, synthesize
9486 it, by scanning the DIE's below the compilation unit. */
9487 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
9488
9489 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
9490 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
9491
9492 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9493 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9494 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9495 addrmap to help ensure it has an accurate map of pc values belonging to
9496 this comp unit. */
9497 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9498
9499 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
9500 SECT_OFF_TEXT (objfile),
9501 0);
9502
9503 if (cust != NULL)
9504 {
9505 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
9506
9507 /* Set symtab language to language from DW_AT_language. If the
9508 compilation is from a C file generated by language preprocessors, do
9509 not set the language if it was already deduced by start_subfile. */
9510 if (!(cu->language == language_c
9511 && COMPUNIT_FILETABS (cust)->language != language_unknown))
9512 COMPUNIT_FILETABS (cust)->language = cu->language;
9513
9514 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9515 produce DW_AT_location with location lists but it can be possibly
9516 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9517 there were bugs in prologue debug info, fixed later in GCC-4.5
9518 by "unwind info for epilogues" patch (which is not directly related).
9519
9520 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9521 needed, it would be wrong due to missing DW_AT_producer there.
9522
9523 Still one can confuse GDB by using non-standard GCC compilation
9524 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
9525 */
9526 if (cu->has_loclist && gcc_4_minor >= 5)
9527 cust->locations_valid = 1;
9528
9529 if (gcc_4_minor >= 5)
9530 cust->epilogue_unwind_valid = 1;
9531
9532 cust->call_site_htab = cu->call_site_htab;
9533 }
9534
9535 if (dwarf2_per_objfile->using_index)
9536 per_cu->v.quick->compunit_symtab = cust;
9537 else
9538 {
9539 dwarf2_psymtab *pst = per_cu->v.psymtab;
9540 pst->compunit_symtab = cust;
9541 pst->readin = true;
9542 }
9543
9544 /* Push it for inclusion processing later. */
9545 dwarf2_per_objfile->just_read_cus.push_back (per_cu);
9546
9547 /* Not needed any more. */
9548 cu->reset_builder ();
9549 }
9550
9551 /* Generate full symbol information for type unit PER_CU, whose DIEs have
9552 already been loaded into memory. */
9553
9554 static void
9555 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
9556 enum language pretend_language)
9557 {
9558 struct dwarf2_cu *cu = per_cu->cu;
9559 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9560 struct objfile *objfile = dwarf2_per_objfile->objfile;
9561 struct compunit_symtab *cust;
9562 struct signatured_type *sig_type;
9563
9564 gdb_assert (per_cu->is_debug_types);
9565 sig_type = (struct signatured_type *) per_cu;
9566
9567 /* Clear the list here in case something was left over. */
9568 cu->method_list.clear ();
9569
9570 cu->language = pretend_language;
9571 cu->language_defn = language_def (cu->language);
9572
9573 /* The symbol tables are set up in read_type_unit_scope. */
9574 process_die (cu->dies, cu);
9575
9576 /* For now fudge the Go package. */
9577 if (cu->language == language_go)
9578 fixup_go_packaging (cu);
9579
9580 /* Now that we have processed all the DIEs in the CU, all the types
9581 should be complete, and it should now be safe to compute all of the
9582 physnames. */
9583 compute_delayed_physnames (cu);
9584
9585 if (cu->language == language_rust)
9586 rust_union_quirks (cu);
9587
9588 /* TUs share symbol tables.
9589 If this is the first TU to use this symtab, complete the construction
9590 of it with end_expandable_symtab. Otherwise, complete the addition of
9591 this TU's symbols to the existing symtab. */
9592 if (sig_type->type_unit_group->compunit_symtab == NULL)
9593 {
9594 buildsym_compunit *builder = cu->get_builder ();
9595 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
9596 sig_type->type_unit_group->compunit_symtab = cust;
9597
9598 if (cust != NULL)
9599 {
9600 /* Set symtab language to language from DW_AT_language. If the
9601 compilation is from a C file generated by language preprocessors,
9602 do not set the language if it was already deduced by
9603 start_subfile. */
9604 if (!(cu->language == language_c
9605 && COMPUNIT_FILETABS (cust)->language != language_c))
9606 COMPUNIT_FILETABS (cust)->language = cu->language;
9607 }
9608 }
9609 else
9610 {
9611 cu->get_builder ()->augment_type_symtab ();
9612 cust = sig_type->type_unit_group->compunit_symtab;
9613 }
9614
9615 if (dwarf2_per_objfile->using_index)
9616 per_cu->v.quick->compunit_symtab = cust;
9617 else
9618 {
9619 dwarf2_psymtab *pst = per_cu->v.psymtab;
9620 pst->compunit_symtab = cust;
9621 pst->readin = true;
9622 }
9623
9624 /* Not needed any more. */
9625 cu->reset_builder ();
9626 }
9627
9628 /* Process an imported unit DIE. */
9629
9630 static void
9631 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9632 {
9633 struct attribute *attr;
9634
9635 /* For now we don't handle imported units in type units. */
9636 if (cu->per_cu->is_debug_types)
9637 {
9638 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9639 " supported in type units [in module %s]"),
9640 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
9641 }
9642
9643 attr = dwarf2_attr (die, DW_AT_import, cu);
9644 if (attr != NULL)
9645 {
9646 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
9647 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
9648 dwarf2_per_cu_data *per_cu
9649 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
9650 cu->per_cu->dwarf2_per_objfile);
9651
9652 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
9653 into another compilation unit, at root level. Regard this as a hint,
9654 and ignore it. */
9655 if (die->parent && die->parent->parent == NULL
9656 && per_cu->unit_type == DW_UT_compile
9657 && per_cu->lang == language_cplus)
9658 return;
9659
9660 /* If necessary, add it to the queue and load its DIEs. */
9661 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
9662 load_full_comp_unit (per_cu, false, cu->language);
9663
9664 cu->per_cu->imported_symtabs_push (per_cu);
9665 }
9666 }
9667
9668 /* RAII object that represents a process_die scope: i.e.,
9669 starts/finishes processing a DIE. */
9670 class process_die_scope
9671 {
9672 public:
9673 process_die_scope (die_info *die, dwarf2_cu *cu)
9674 : m_die (die), m_cu (cu)
9675 {
9676 /* We should only be processing DIEs not already in process. */
9677 gdb_assert (!m_die->in_process);
9678 m_die->in_process = true;
9679 }
9680
9681 ~process_die_scope ()
9682 {
9683 m_die->in_process = false;
9684
9685 /* If we're done processing the DIE for the CU that owns the line
9686 header, we don't need the line header anymore. */
9687 if (m_cu->line_header_die_owner == m_die)
9688 {
9689 delete m_cu->line_header;
9690 m_cu->line_header = NULL;
9691 m_cu->line_header_die_owner = NULL;
9692 }
9693 }
9694
9695 private:
9696 die_info *m_die;
9697 dwarf2_cu *m_cu;
9698 };
9699
9700 /* Process a die and its children. */
9701
9702 static void
9703 process_die (struct die_info *die, struct dwarf2_cu *cu)
9704 {
9705 process_die_scope scope (die, cu);
9706
9707 switch (die->tag)
9708 {
9709 case DW_TAG_padding:
9710 break;
9711 case DW_TAG_compile_unit:
9712 case DW_TAG_partial_unit:
9713 read_file_scope (die, cu);
9714 break;
9715 case DW_TAG_type_unit:
9716 read_type_unit_scope (die, cu);
9717 break;
9718 case DW_TAG_subprogram:
9719 /* Nested subprograms in Fortran get a prefix. */
9720 if (cu->language == language_fortran
9721 && die->parent != NULL
9722 && die->parent->tag == DW_TAG_subprogram)
9723 cu->processing_has_namespace_info = true;
9724 /* Fall through. */
9725 case DW_TAG_inlined_subroutine:
9726 read_func_scope (die, cu);
9727 break;
9728 case DW_TAG_lexical_block:
9729 case DW_TAG_try_block:
9730 case DW_TAG_catch_block:
9731 read_lexical_block_scope (die, cu);
9732 break;
9733 case DW_TAG_call_site:
9734 case DW_TAG_GNU_call_site:
9735 read_call_site_scope (die, cu);
9736 break;
9737 case DW_TAG_class_type:
9738 case DW_TAG_interface_type:
9739 case DW_TAG_structure_type:
9740 case DW_TAG_union_type:
9741 process_structure_scope (die, cu);
9742 break;
9743 case DW_TAG_enumeration_type:
9744 process_enumeration_scope (die, cu);
9745 break;
9746
9747 /* These dies have a type, but processing them does not create
9748 a symbol or recurse to process the children. Therefore we can
9749 read them on-demand through read_type_die. */
9750 case DW_TAG_subroutine_type:
9751 case DW_TAG_set_type:
9752 case DW_TAG_array_type:
9753 case DW_TAG_pointer_type:
9754 case DW_TAG_ptr_to_member_type:
9755 case DW_TAG_reference_type:
9756 case DW_TAG_rvalue_reference_type:
9757 case DW_TAG_string_type:
9758 break;
9759
9760 case DW_TAG_base_type:
9761 case DW_TAG_subrange_type:
9762 case DW_TAG_typedef:
9763 /* Add a typedef symbol for the type definition, if it has a
9764 DW_AT_name. */
9765 new_symbol (die, read_type_die (die, cu), cu);
9766 break;
9767 case DW_TAG_common_block:
9768 read_common_block (die, cu);
9769 break;
9770 case DW_TAG_common_inclusion:
9771 break;
9772 case DW_TAG_namespace:
9773 cu->processing_has_namespace_info = true;
9774 read_namespace (die, cu);
9775 break;
9776 case DW_TAG_module:
9777 cu->processing_has_namespace_info = true;
9778 read_module (die, cu);
9779 break;
9780 case DW_TAG_imported_declaration:
9781 cu->processing_has_namespace_info = true;
9782 if (read_namespace_alias (die, cu))
9783 break;
9784 /* The declaration is not a global namespace alias. */
9785 /* Fall through. */
9786 case DW_TAG_imported_module:
9787 cu->processing_has_namespace_info = true;
9788 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
9789 || cu->language != language_fortran))
9790 complaint (_("Tag '%s' has unexpected children"),
9791 dwarf_tag_name (die->tag));
9792 read_import_statement (die, cu);
9793 break;
9794
9795 case DW_TAG_imported_unit:
9796 process_imported_unit_die (die, cu);
9797 break;
9798
9799 case DW_TAG_variable:
9800 read_variable (die, cu);
9801 break;
9802
9803 default:
9804 new_symbol (die, NULL, cu);
9805 break;
9806 }
9807 }
9808 \f
9809 /* DWARF name computation. */
9810
9811 /* A helper function for dwarf2_compute_name which determines whether DIE
9812 needs to have the name of the scope prepended to the name listed in the
9813 die. */
9814
9815 static int
9816 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
9817 {
9818 struct attribute *attr;
9819
9820 switch (die->tag)
9821 {
9822 case DW_TAG_namespace:
9823 case DW_TAG_typedef:
9824 case DW_TAG_class_type:
9825 case DW_TAG_interface_type:
9826 case DW_TAG_structure_type:
9827 case DW_TAG_union_type:
9828 case DW_TAG_enumeration_type:
9829 case DW_TAG_enumerator:
9830 case DW_TAG_subprogram:
9831 case DW_TAG_inlined_subroutine:
9832 case DW_TAG_member:
9833 case DW_TAG_imported_declaration:
9834 return 1;
9835
9836 case DW_TAG_variable:
9837 case DW_TAG_constant:
9838 /* We only need to prefix "globally" visible variables. These include
9839 any variable marked with DW_AT_external or any variable that
9840 lives in a namespace. [Variables in anonymous namespaces
9841 require prefixing, but they are not DW_AT_external.] */
9842
9843 if (dwarf2_attr (die, DW_AT_specification, cu))
9844 {
9845 struct dwarf2_cu *spec_cu = cu;
9846
9847 return die_needs_namespace (die_specification (die, &spec_cu),
9848 spec_cu);
9849 }
9850
9851 attr = dwarf2_attr (die, DW_AT_external, cu);
9852 if (attr == NULL && die->parent->tag != DW_TAG_namespace
9853 && die->parent->tag != DW_TAG_module)
9854 return 0;
9855 /* A variable in a lexical block of some kind does not need a
9856 namespace, even though in C++ such variables may be external
9857 and have a mangled name. */
9858 if (die->parent->tag == DW_TAG_lexical_block
9859 || die->parent->tag == DW_TAG_try_block
9860 || die->parent->tag == DW_TAG_catch_block
9861 || die->parent->tag == DW_TAG_subprogram)
9862 return 0;
9863 return 1;
9864
9865 default:
9866 return 0;
9867 }
9868 }
9869
9870 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
9871 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9872 defined for the given DIE. */
9873
9874 static struct attribute *
9875 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
9876 {
9877 struct attribute *attr;
9878
9879 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
9880 if (attr == NULL)
9881 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
9882
9883 return attr;
9884 }
9885
9886 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
9887 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9888 defined for the given DIE. */
9889
9890 static const char *
9891 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
9892 {
9893 const char *linkage_name;
9894
9895 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
9896 if (linkage_name == NULL)
9897 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
9898
9899 return linkage_name;
9900 }
9901
9902 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
9903 compute the physname for the object, which include a method's:
9904 - formal parameters (C++),
9905 - receiver type (Go),
9906
9907 The term "physname" is a bit confusing.
9908 For C++, for example, it is the demangled name.
9909 For Go, for example, it's the mangled name.
9910
9911 For Ada, return the DIE's linkage name rather than the fully qualified
9912 name. PHYSNAME is ignored..
9913
9914 The result is allocated on the objfile_obstack and canonicalized. */
9915
9916 static const char *
9917 dwarf2_compute_name (const char *name,
9918 struct die_info *die, struct dwarf2_cu *cu,
9919 int physname)
9920 {
9921 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9922
9923 if (name == NULL)
9924 name = dwarf2_name (die, cu);
9925
9926 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
9927 but otherwise compute it by typename_concat inside GDB.
9928 FIXME: Actually this is not really true, or at least not always true.
9929 It's all very confusing. compute_and_set_names doesn't try to demangle
9930 Fortran names because there is no mangling standard. So new_symbol
9931 will set the demangled name to the result of dwarf2_full_name, and it is
9932 the demangled name that GDB uses if it exists. */
9933 if (cu->language == language_ada
9934 || (cu->language == language_fortran && physname))
9935 {
9936 /* For Ada unit, we prefer the linkage name over the name, as
9937 the former contains the exported name, which the user expects
9938 to be able to reference. Ideally, we want the user to be able
9939 to reference this entity using either natural or linkage name,
9940 but we haven't started looking at this enhancement yet. */
9941 const char *linkage_name = dw2_linkage_name (die, cu);
9942
9943 if (linkage_name != NULL)
9944 return linkage_name;
9945 }
9946
9947 /* These are the only languages we know how to qualify names in. */
9948 if (name != NULL
9949 && (cu->language == language_cplus
9950 || cu->language == language_fortran || cu->language == language_d
9951 || cu->language == language_rust))
9952 {
9953 if (die_needs_namespace (die, cu))
9954 {
9955 const char *prefix;
9956 const char *canonical_name = NULL;
9957
9958 string_file buf;
9959
9960 prefix = determine_prefix (die, cu);
9961 if (*prefix != '\0')
9962 {
9963 gdb::unique_xmalloc_ptr<char> prefixed_name
9964 (typename_concat (NULL, prefix, name, physname, cu));
9965
9966 buf.puts (prefixed_name.get ());
9967 }
9968 else
9969 buf.puts (name);
9970
9971 /* Template parameters may be specified in the DIE's DW_AT_name, or
9972 as children with DW_TAG_template_type_param or
9973 DW_TAG_value_type_param. If the latter, add them to the name
9974 here. If the name already has template parameters, then
9975 skip this step; some versions of GCC emit both, and
9976 it is more efficient to use the pre-computed name.
9977
9978 Something to keep in mind about this process: it is very
9979 unlikely, or in some cases downright impossible, to produce
9980 something that will match the mangled name of a function.
9981 If the definition of the function has the same debug info,
9982 we should be able to match up with it anyway. But fallbacks
9983 using the minimal symbol, for instance to find a method
9984 implemented in a stripped copy of libstdc++, will not work.
9985 If we do not have debug info for the definition, we will have to
9986 match them up some other way.
9987
9988 When we do name matching there is a related problem with function
9989 templates; two instantiated function templates are allowed to
9990 differ only by their return types, which we do not add here. */
9991
9992 if (cu->language == language_cplus && strchr (name, '<') == NULL)
9993 {
9994 struct attribute *attr;
9995 struct die_info *child;
9996 int first = 1;
9997
9998 die->building_fullname = 1;
9999
10000 for (child = die->child; child != NULL; child = child->sibling)
10001 {
10002 struct type *type;
10003 LONGEST value;
10004 const gdb_byte *bytes;
10005 struct dwarf2_locexpr_baton *baton;
10006 struct value *v;
10007
10008 if (child->tag != DW_TAG_template_type_param
10009 && child->tag != DW_TAG_template_value_param)
10010 continue;
10011
10012 if (first)
10013 {
10014 buf.puts ("<");
10015 first = 0;
10016 }
10017 else
10018 buf.puts (", ");
10019
10020 attr = dwarf2_attr (child, DW_AT_type, cu);
10021 if (attr == NULL)
10022 {
10023 complaint (_("template parameter missing DW_AT_type"));
10024 buf.puts ("UNKNOWN_TYPE");
10025 continue;
10026 }
10027 type = die_type (child, cu);
10028
10029 if (child->tag == DW_TAG_template_type_param)
10030 {
10031 c_print_type (type, "", &buf, -1, 0, cu->language,
10032 &type_print_raw_options);
10033 continue;
10034 }
10035
10036 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10037 if (attr == NULL)
10038 {
10039 complaint (_("template parameter missing "
10040 "DW_AT_const_value"));
10041 buf.puts ("UNKNOWN_VALUE");
10042 continue;
10043 }
10044
10045 dwarf2_const_value_attr (attr, type, name,
10046 &cu->comp_unit_obstack, cu,
10047 &value, &bytes, &baton);
10048
10049 if (TYPE_NOSIGN (type))
10050 /* GDB prints characters as NUMBER 'CHAR'. If that's
10051 changed, this can use value_print instead. */
10052 c_printchar (value, type, &buf);
10053 else
10054 {
10055 struct value_print_options opts;
10056
10057 if (baton != NULL)
10058 v = dwarf2_evaluate_loc_desc (type, NULL,
10059 baton->data,
10060 baton->size,
10061 baton->per_cu);
10062 else if (bytes != NULL)
10063 {
10064 v = allocate_value (type);
10065 memcpy (value_contents_writeable (v), bytes,
10066 TYPE_LENGTH (type));
10067 }
10068 else
10069 v = value_from_longest (type, value);
10070
10071 /* Specify decimal so that we do not depend on
10072 the radix. */
10073 get_formatted_print_options (&opts, 'd');
10074 opts.raw = 1;
10075 value_print (v, &buf, &opts);
10076 release_value (v);
10077 }
10078 }
10079
10080 die->building_fullname = 0;
10081
10082 if (!first)
10083 {
10084 /* Close the argument list, with a space if necessary
10085 (nested templates). */
10086 if (!buf.empty () && buf.string ().back () == '>')
10087 buf.puts (" >");
10088 else
10089 buf.puts (">");
10090 }
10091 }
10092
10093 /* For C++ methods, append formal parameter type
10094 information, if PHYSNAME. */
10095
10096 if (physname && die->tag == DW_TAG_subprogram
10097 && cu->language == language_cplus)
10098 {
10099 struct type *type = read_type_die (die, cu);
10100
10101 c_type_print_args (type, &buf, 1, cu->language,
10102 &type_print_raw_options);
10103
10104 if (cu->language == language_cplus)
10105 {
10106 /* Assume that an artificial first parameter is
10107 "this", but do not crash if it is not. RealView
10108 marks unnamed (and thus unused) parameters as
10109 artificial; there is no way to differentiate
10110 the two cases. */
10111 if (TYPE_NFIELDS (type) > 0
10112 && TYPE_FIELD_ARTIFICIAL (type, 0)
10113 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
10114 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10115 0))))
10116 buf.puts (" const");
10117 }
10118 }
10119
10120 const std::string &intermediate_name = buf.string ();
10121
10122 if (cu->language == language_cplus)
10123 canonical_name
10124 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
10125 objfile);
10126
10127 /* If we only computed INTERMEDIATE_NAME, or if
10128 INTERMEDIATE_NAME is already canonical, then we need to
10129 intern it. */
10130 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
10131 name = objfile->intern (intermediate_name);
10132 else
10133 name = canonical_name;
10134 }
10135 }
10136
10137 return name;
10138 }
10139
10140 /* Return the fully qualified name of DIE, based on its DW_AT_name.
10141 If scope qualifiers are appropriate they will be added. The result
10142 will be allocated on the storage_obstack, or NULL if the DIE does
10143 not have a name. NAME may either be from a previous call to
10144 dwarf2_name or NULL.
10145
10146 The output string will be canonicalized (if C++). */
10147
10148 static const char *
10149 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10150 {
10151 return dwarf2_compute_name (name, die, cu, 0);
10152 }
10153
10154 /* Construct a physname for the given DIE in CU. NAME may either be
10155 from a previous call to dwarf2_name or NULL. The result will be
10156 allocated on the objfile_objstack or NULL if the DIE does not have a
10157 name.
10158
10159 The output string will be canonicalized (if C++). */
10160
10161 static const char *
10162 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10163 {
10164 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10165 const char *retval, *mangled = NULL, *canon = NULL;
10166 int need_copy = 1;
10167
10168 /* In this case dwarf2_compute_name is just a shortcut not building anything
10169 on its own. */
10170 if (!die_needs_namespace (die, cu))
10171 return dwarf2_compute_name (name, die, cu, 1);
10172
10173 mangled = dw2_linkage_name (die, cu);
10174
10175 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10176 See https://github.com/rust-lang/rust/issues/32925. */
10177 if (cu->language == language_rust && mangled != NULL
10178 && strchr (mangled, '{') != NULL)
10179 mangled = NULL;
10180
10181 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10182 has computed. */
10183 gdb::unique_xmalloc_ptr<char> demangled;
10184 if (mangled != NULL)
10185 {
10186
10187 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
10188 {
10189 /* Do nothing (do not demangle the symbol name). */
10190 }
10191 else if (cu->language == language_go)
10192 {
10193 /* This is a lie, but we already lie to the caller new_symbol.
10194 new_symbol assumes we return the mangled name.
10195 This just undoes that lie until things are cleaned up. */
10196 }
10197 else
10198 {
10199 /* Use DMGL_RET_DROP for C++ template functions to suppress
10200 their return type. It is easier for GDB users to search
10201 for such functions as `name(params)' than `long name(params)'.
10202 In such case the minimal symbol names do not match the full
10203 symbol names but for template functions there is never a need
10204 to look up their definition from their declaration so
10205 the only disadvantage remains the minimal symbol variant
10206 `long name(params)' does not have the proper inferior type. */
10207 demangled.reset (gdb_demangle (mangled,
10208 (DMGL_PARAMS | DMGL_ANSI
10209 | DMGL_RET_DROP)));
10210 }
10211 if (demangled)
10212 canon = demangled.get ();
10213 else
10214 {
10215 canon = mangled;
10216 need_copy = 0;
10217 }
10218 }
10219
10220 if (canon == NULL || check_physname)
10221 {
10222 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10223
10224 if (canon != NULL && strcmp (physname, canon) != 0)
10225 {
10226 /* It may not mean a bug in GDB. The compiler could also
10227 compute DW_AT_linkage_name incorrectly. But in such case
10228 GDB would need to be bug-to-bug compatible. */
10229
10230 complaint (_("Computed physname <%s> does not match demangled <%s> "
10231 "(from linkage <%s>) - DIE at %s [in module %s]"),
10232 physname, canon, mangled, sect_offset_str (die->sect_off),
10233 objfile_name (objfile));
10234
10235 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10236 is available here - over computed PHYSNAME. It is safer
10237 against both buggy GDB and buggy compilers. */
10238
10239 retval = canon;
10240 }
10241 else
10242 {
10243 retval = physname;
10244 need_copy = 0;
10245 }
10246 }
10247 else
10248 retval = canon;
10249
10250 if (need_copy)
10251 retval = objfile->intern (retval);
10252
10253 return retval;
10254 }
10255
10256 /* Inspect DIE in CU for a namespace alias. If one exists, record
10257 a new symbol for it.
10258
10259 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10260
10261 static int
10262 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10263 {
10264 struct attribute *attr;
10265
10266 /* If the die does not have a name, this is not a namespace
10267 alias. */
10268 attr = dwarf2_attr (die, DW_AT_name, cu);
10269 if (attr != NULL)
10270 {
10271 int num;
10272 struct die_info *d = die;
10273 struct dwarf2_cu *imported_cu = cu;
10274
10275 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10276 keep inspecting DIEs until we hit the underlying import. */
10277 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
10278 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10279 {
10280 attr = dwarf2_attr (d, DW_AT_import, cu);
10281 if (attr == NULL)
10282 break;
10283
10284 d = follow_die_ref (d, attr, &imported_cu);
10285 if (d->tag != DW_TAG_imported_declaration)
10286 break;
10287 }
10288
10289 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10290 {
10291 complaint (_("DIE at %s has too many recursively imported "
10292 "declarations"), sect_offset_str (d->sect_off));
10293 return 0;
10294 }
10295
10296 if (attr != NULL)
10297 {
10298 struct type *type;
10299 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10300
10301 type = get_die_type_at_offset (sect_off, cu->per_cu);
10302 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
10303 {
10304 /* This declaration is a global namespace alias. Add
10305 a symbol for it whose type is the aliased namespace. */
10306 new_symbol (die, type, cu);
10307 return 1;
10308 }
10309 }
10310 }
10311
10312 return 0;
10313 }
10314
10315 /* Return the using directives repository (global or local?) to use in the
10316 current context for CU.
10317
10318 For Ada, imported declarations can materialize renamings, which *may* be
10319 global. However it is impossible (for now?) in DWARF to distinguish
10320 "external" imported declarations and "static" ones. As all imported
10321 declarations seem to be static in all other languages, make them all CU-wide
10322 global only in Ada. */
10323
10324 static struct using_direct **
10325 using_directives (struct dwarf2_cu *cu)
10326 {
10327 if (cu->language == language_ada
10328 && cu->get_builder ()->outermost_context_p ())
10329 return cu->get_builder ()->get_global_using_directives ();
10330 else
10331 return cu->get_builder ()->get_local_using_directives ();
10332 }
10333
10334 /* Read the import statement specified by the given die and record it. */
10335
10336 static void
10337 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10338 {
10339 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10340 struct attribute *import_attr;
10341 struct die_info *imported_die, *child_die;
10342 struct dwarf2_cu *imported_cu;
10343 const char *imported_name;
10344 const char *imported_name_prefix;
10345 const char *canonical_name;
10346 const char *import_alias;
10347 const char *imported_declaration = NULL;
10348 const char *import_prefix;
10349 std::vector<const char *> excludes;
10350
10351 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10352 if (import_attr == NULL)
10353 {
10354 complaint (_("Tag '%s' has no DW_AT_import"),
10355 dwarf_tag_name (die->tag));
10356 return;
10357 }
10358
10359 imported_cu = cu;
10360 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10361 imported_name = dwarf2_name (imported_die, imported_cu);
10362 if (imported_name == NULL)
10363 {
10364 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10365
10366 The import in the following code:
10367 namespace A
10368 {
10369 typedef int B;
10370 }
10371
10372 int main ()
10373 {
10374 using A::B;
10375 B b;
10376 return b;
10377 }
10378
10379 ...
10380 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10381 <52> DW_AT_decl_file : 1
10382 <53> DW_AT_decl_line : 6
10383 <54> DW_AT_import : <0x75>
10384 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10385 <59> DW_AT_name : B
10386 <5b> DW_AT_decl_file : 1
10387 <5c> DW_AT_decl_line : 2
10388 <5d> DW_AT_type : <0x6e>
10389 ...
10390 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10391 <76> DW_AT_byte_size : 4
10392 <77> DW_AT_encoding : 5 (signed)
10393
10394 imports the wrong die ( 0x75 instead of 0x58 ).
10395 This case will be ignored until the gcc bug is fixed. */
10396 return;
10397 }
10398
10399 /* Figure out the local name after import. */
10400 import_alias = dwarf2_name (die, cu);
10401
10402 /* Figure out where the statement is being imported to. */
10403 import_prefix = determine_prefix (die, cu);
10404
10405 /* Figure out what the scope of the imported die is and prepend it
10406 to the name of the imported die. */
10407 imported_name_prefix = determine_prefix (imported_die, imported_cu);
10408
10409 if (imported_die->tag != DW_TAG_namespace
10410 && imported_die->tag != DW_TAG_module)
10411 {
10412 imported_declaration = imported_name;
10413 canonical_name = imported_name_prefix;
10414 }
10415 else if (strlen (imported_name_prefix) > 0)
10416 canonical_name = obconcat (&objfile->objfile_obstack,
10417 imported_name_prefix,
10418 (cu->language == language_d ? "." : "::"),
10419 imported_name, (char *) NULL);
10420 else
10421 canonical_name = imported_name;
10422
10423 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10424 for (child_die = die->child; child_die && child_die->tag;
10425 child_die = child_die->sibling)
10426 {
10427 /* DWARF-4: A Fortran use statement with a “rename list” may be
10428 represented by an imported module entry with an import attribute
10429 referring to the module and owned entries corresponding to those
10430 entities that are renamed as part of being imported. */
10431
10432 if (child_die->tag != DW_TAG_imported_declaration)
10433 {
10434 complaint (_("child DW_TAG_imported_declaration expected "
10435 "- DIE at %s [in module %s]"),
10436 sect_offset_str (child_die->sect_off),
10437 objfile_name (objfile));
10438 continue;
10439 }
10440
10441 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10442 if (import_attr == NULL)
10443 {
10444 complaint (_("Tag '%s' has no DW_AT_import"),
10445 dwarf_tag_name (child_die->tag));
10446 continue;
10447 }
10448
10449 imported_cu = cu;
10450 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10451 &imported_cu);
10452 imported_name = dwarf2_name (imported_die, imported_cu);
10453 if (imported_name == NULL)
10454 {
10455 complaint (_("child DW_TAG_imported_declaration has unknown "
10456 "imported name - DIE at %s [in module %s]"),
10457 sect_offset_str (child_die->sect_off),
10458 objfile_name (objfile));
10459 continue;
10460 }
10461
10462 excludes.push_back (imported_name);
10463
10464 process_die (child_die, cu);
10465 }
10466
10467 add_using_directive (using_directives (cu),
10468 import_prefix,
10469 canonical_name,
10470 import_alias,
10471 imported_declaration,
10472 excludes,
10473 0,
10474 &objfile->objfile_obstack);
10475 }
10476
10477 /* ICC<14 does not output the required DW_AT_declaration on incomplete
10478 types, but gives them a size of zero. Starting with version 14,
10479 ICC is compatible with GCC. */
10480
10481 static bool
10482 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10483 {
10484 if (!cu->checked_producer)
10485 check_producer (cu);
10486
10487 return cu->producer_is_icc_lt_14;
10488 }
10489
10490 /* ICC generates a DW_AT_type for C void functions. This was observed on
10491 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10492 which says that void functions should not have a DW_AT_type. */
10493
10494 static bool
10495 producer_is_icc (struct dwarf2_cu *cu)
10496 {
10497 if (!cu->checked_producer)
10498 check_producer (cu);
10499
10500 return cu->producer_is_icc;
10501 }
10502
10503 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10504 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10505 this, it was first present in GCC release 4.3.0. */
10506
10507 static bool
10508 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10509 {
10510 if (!cu->checked_producer)
10511 check_producer (cu);
10512
10513 return cu->producer_is_gcc_lt_4_3;
10514 }
10515
10516 static file_and_directory
10517 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
10518 {
10519 file_and_directory res;
10520
10521 /* Find the filename. Do not use dwarf2_name here, since the filename
10522 is not a source language identifier. */
10523 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10524 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
10525
10526 if (res.comp_dir == NULL
10527 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10528 && IS_ABSOLUTE_PATH (res.name))
10529 {
10530 res.comp_dir_storage = ldirname (res.name);
10531 if (!res.comp_dir_storage.empty ())
10532 res.comp_dir = res.comp_dir_storage.c_str ();
10533 }
10534 if (res.comp_dir != NULL)
10535 {
10536 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10537 directory, get rid of it. */
10538 const char *cp = strchr (res.comp_dir, ':');
10539
10540 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10541 res.comp_dir = cp + 1;
10542 }
10543
10544 if (res.name == NULL)
10545 res.name = "<unknown>";
10546
10547 return res;
10548 }
10549
10550 /* Handle DW_AT_stmt_list for a compilation unit.
10551 DIE is the DW_TAG_compile_unit die for CU.
10552 COMP_DIR is the compilation directory. LOWPC is passed to
10553 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
10554
10555 static void
10556 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
10557 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
10558 {
10559 struct dwarf2_per_objfile *dwarf2_per_objfile
10560 = cu->per_cu->dwarf2_per_objfile;
10561 struct attribute *attr;
10562 struct line_header line_header_local;
10563 hashval_t line_header_local_hash;
10564 void **slot;
10565 int decode_mapping;
10566
10567 gdb_assert (! cu->per_cu->is_debug_types);
10568
10569 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
10570 if (attr == NULL)
10571 return;
10572
10573 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
10574
10575 /* The line header hash table is only created if needed (it exists to
10576 prevent redundant reading of the line table for partial_units).
10577 If we're given a partial_unit, we'll need it. If we're given a
10578 compile_unit, then use the line header hash table if it's already
10579 created, but don't create one just yet. */
10580
10581 if (dwarf2_per_objfile->line_header_hash == NULL
10582 && die->tag == DW_TAG_partial_unit)
10583 {
10584 dwarf2_per_objfile->line_header_hash
10585 .reset (htab_create_alloc (127, line_header_hash_voidp,
10586 line_header_eq_voidp,
10587 free_line_header_voidp,
10588 xcalloc, xfree));
10589 }
10590
10591 line_header_local.sect_off = line_offset;
10592 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10593 line_header_local_hash = line_header_hash (&line_header_local);
10594 if (dwarf2_per_objfile->line_header_hash != NULL)
10595 {
10596 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash.get (),
10597 &line_header_local,
10598 line_header_local_hash, NO_INSERT);
10599
10600 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10601 is not present in *SLOT (since if there is something in *SLOT then
10602 it will be for a partial_unit). */
10603 if (die->tag == DW_TAG_partial_unit && slot != NULL)
10604 {
10605 gdb_assert (*slot != NULL);
10606 cu->line_header = (struct line_header *) *slot;
10607 return;
10608 }
10609 }
10610
10611 /* dwarf_decode_line_header does not yet provide sufficient information.
10612 We always have to call also dwarf_decode_lines for it. */
10613 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10614 if (lh == NULL)
10615 return;
10616
10617 cu->line_header = lh.release ();
10618 cu->line_header_die_owner = die;
10619
10620 if (dwarf2_per_objfile->line_header_hash == NULL)
10621 slot = NULL;
10622 else
10623 {
10624 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash.get (),
10625 &line_header_local,
10626 line_header_local_hash, INSERT);
10627 gdb_assert (slot != NULL);
10628 }
10629 if (slot != NULL && *slot == NULL)
10630 {
10631 /* This newly decoded line number information unit will be owned
10632 by line_header_hash hash table. */
10633 *slot = cu->line_header;
10634 cu->line_header_die_owner = NULL;
10635 }
10636 else
10637 {
10638 /* We cannot free any current entry in (*slot) as that struct line_header
10639 may be already used by multiple CUs. Create only temporary decoded
10640 line_header for this CU - it may happen at most once for each line
10641 number information unit. And if we're not using line_header_hash
10642 then this is what we want as well. */
10643 gdb_assert (die->tag != DW_TAG_partial_unit);
10644 }
10645 decode_mapping = (die->tag != DW_TAG_partial_unit);
10646 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
10647 decode_mapping);
10648
10649 }
10650
10651 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
10652
10653 static void
10654 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
10655 {
10656 struct dwarf2_per_objfile *dwarf2_per_objfile
10657 = cu->per_cu->dwarf2_per_objfile;
10658 struct objfile *objfile = dwarf2_per_objfile->objfile;
10659 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10660 CORE_ADDR lowpc = ((CORE_ADDR) -1);
10661 CORE_ADDR highpc = ((CORE_ADDR) 0);
10662 struct attribute *attr;
10663 struct die_info *child_die;
10664 CORE_ADDR baseaddr;
10665
10666 prepare_one_comp_unit (cu, die, cu->language);
10667 baseaddr = objfile->text_section_offset ();
10668
10669 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
10670
10671 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10672 from finish_block. */
10673 if (lowpc == ((CORE_ADDR) -1))
10674 lowpc = highpc;
10675 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
10676
10677 file_and_directory fnd = find_file_and_directory (die, cu);
10678
10679 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
10680 standardised yet. As a workaround for the language detection we fall
10681 back to the DW_AT_producer string. */
10682 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
10683 cu->language = language_opencl;
10684
10685 /* Similar hack for Go. */
10686 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
10687 set_cu_language (DW_LANG_Go, cu);
10688
10689 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
10690
10691 /* Decode line number information if present. We do this before
10692 processing child DIEs, so that the line header table is available
10693 for DW_AT_decl_file. */
10694 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
10695
10696 /* Process all dies in compilation unit. */
10697 if (die->child != NULL)
10698 {
10699 child_die = die->child;
10700 while (child_die && child_die->tag)
10701 {
10702 process_die (child_die, cu);
10703 child_die = child_die->sibling;
10704 }
10705 }
10706
10707 /* Decode macro information, if present. Dwarf 2 macro information
10708 refers to information in the line number info statement program
10709 header, so we can only read it if we've read the header
10710 successfully. */
10711 attr = dwarf2_attr (die, DW_AT_macros, cu);
10712 if (attr == NULL)
10713 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
10714 if (attr && cu->line_header)
10715 {
10716 if (dwarf2_attr (die, DW_AT_macro_info, cu))
10717 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
10718
10719 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
10720 }
10721 else
10722 {
10723 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
10724 if (attr && cu->line_header)
10725 {
10726 unsigned int macro_offset = DW_UNSND (attr);
10727
10728 dwarf_decode_macros (cu, macro_offset, 0);
10729 }
10730 }
10731 }
10732
10733 void
10734 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
10735 {
10736 struct type_unit_group *tu_group;
10737 int first_time;
10738 struct attribute *attr;
10739 unsigned int i;
10740 struct signatured_type *sig_type;
10741
10742 gdb_assert (per_cu->is_debug_types);
10743 sig_type = (struct signatured_type *) per_cu;
10744
10745 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
10746
10747 /* If we're using .gdb_index (includes -readnow) then
10748 per_cu->type_unit_group may not have been set up yet. */
10749 if (sig_type->type_unit_group == NULL)
10750 sig_type->type_unit_group = get_type_unit_group (this, attr);
10751 tu_group = sig_type->type_unit_group;
10752
10753 /* If we've already processed this stmt_list there's no real need to
10754 do it again, we could fake it and just recreate the part we need
10755 (file name,index -> symtab mapping). If data shows this optimization
10756 is useful we can do it then. */
10757 first_time = tu_group->compunit_symtab == NULL;
10758
10759 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
10760 debug info. */
10761 line_header_up lh;
10762 if (attr != NULL)
10763 {
10764 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
10765 lh = dwarf_decode_line_header (line_offset, this);
10766 }
10767 if (lh == NULL)
10768 {
10769 if (first_time)
10770 start_symtab ("", NULL, 0);
10771 else
10772 {
10773 gdb_assert (tu_group->symtabs == NULL);
10774 gdb_assert (m_builder == nullptr);
10775 struct compunit_symtab *cust = tu_group->compunit_symtab;
10776 m_builder.reset (new struct buildsym_compunit
10777 (COMPUNIT_OBJFILE (cust), "",
10778 COMPUNIT_DIRNAME (cust),
10779 compunit_language (cust),
10780 0, cust));
10781 }
10782 return;
10783 }
10784
10785 line_header = lh.release ();
10786 line_header_die_owner = die;
10787
10788 if (first_time)
10789 {
10790 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
10791
10792 /* Note: We don't assign tu_group->compunit_symtab yet because we're
10793 still initializing it, and our caller (a few levels up)
10794 process_full_type_unit still needs to know if this is the first
10795 time. */
10796
10797 tu_group->symtabs
10798 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
10799 struct symtab *, line_header->file_names_size ());
10800
10801 auto &file_names = line_header->file_names ();
10802 for (i = 0; i < file_names.size (); ++i)
10803 {
10804 file_entry &fe = file_names[i];
10805 dwarf2_start_subfile (this, fe.name,
10806 fe.include_dir (line_header));
10807 buildsym_compunit *b = get_builder ();
10808 if (b->get_current_subfile ()->symtab == NULL)
10809 {
10810 /* NOTE: start_subfile will recognize when it's been
10811 passed a file it has already seen. So we can't
10812 assume there's a simple mapping from
10813 cu->line_header->file_names to subfiles, plus
10814 cu->line_header->file_names may contain dups. */
10815 b->get_current_subfile ()->symtab
10816 = allocate_symtab (cust, b->get_current_subfile ()->name);
10817 }
10818
10819 fe.symtab = b->get_current_subfile ()->symtab;
10820 tu_group->symtabs[i] = fe.symtab;
10821 }
10822 }
10823 else
10824 {
10825 gdb_assert (m_builder == nullptr);
10826 struct compunit_symtab *cust = tu_group->compunit_symtab;
10827 m_builder.reset (new struct buildsym_compunit
10828 (COMPUNIT_OBJFILE (cust), "",
10829 COMPUNIT_DIRNAME (cust),
10830 compunit_language (cust),
10831 0, cust));
10832
10833 auto &file_names = line_header->file_names ();
10834 for (i = 0; i < file_names.size (); ++i)
10835 {
10836 file_entry &fe = file_names[i];
10837 fe.symtab = tu_group->symtabs[i];
10838 }
10839 }
10840
10841 /* The main symtab is allocated last. Type units don't have DW_AT_name
10842 so they don't have a "real" (so to speak) symtab anyway.
10843 There is later code that will assign the main symtab to all symbols
10844 that don't have one. We need to handle the case of a symbol with a
10845 missing symtab (DW_AT_decl_file) anyway. */
10846 }
10847
10848 /* Process DW_TAG_type_unit.
10849 For TUs we want to skip the first top level sibling if it's not the
10850 actual type being defined by this TU. In this case the first top
10851 level sibling is there to provide context only. */
10852
10853 static void
10854 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
10855 {
10856 struct die_info *child_die;
10857
10858 prepare_one_comp_unit (cu, die, language_minimal);
10859
10860 /* Initialize (or reinitialize) the machinery for building symtabs.
10861 We do this before processing child DIEs, so that the line header table
10862 is available for DW_AT_decl_file. */
10863 cu->setup_type_unit_groups (die);
10864
10865 if (die->child != NULL)
10866 {
10867 child_die = die->child;
10868 while (child_die && child_die->tag)
10869 {
10870 process_die (child_die, cu);
10871 child_die = child_die->sibling;
10872 }
10873 }
10874 }
10875 \f
10876 /* DWO/DWP files.
10877
10878 http://gcc.gnu.org/wiki/DebugFission
10879 http://gcc.gnu.org/wiki/DebugFissionDWP
10880
10881 To simplify handling of both DWO files ("object" files with the DWARF info)
10882 and DWP files (a file with the DWOs packaged up into one file), we treat
10883 DWP files as having a collection of virtual DWO files. */
10884
10885 static hashval_t
10886 hash_dwo_file (const void *item)
10887 {
10888 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
10889 hashval_t hash;
10890
10891 hash = htab_hash_string (dwo_file->dwo_name);
10892 if (dwo_file->comp_dir != NULL)
10893 hash += htab_hash_string (dwo_file->comp_dir);
10894 return hash;
10895 }
10896
10897 static int
10898 eq_dwo_file (const void *item_lhs, const void *item_rhs)
10899 {
10900 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
10901 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
10902
10903 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
10904 return 0;
10905 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
10906 return lhs->comp_dir == rhs->comp_dir;
10907 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
10908 }
10909
10910 /* Allocate a hash table for DWO files. */
10911
10912 static htab_up
10913 allocate_dwo_file_hash_table ()
10914 {
10915 auto delete_dwo_file = [] (void *item)
10916 {
10917 struct dwo_file *dwo_file = (struct dwo_file *) item;
10918
10919 delete dwo_file;
10920 };
10921
10922 return htab_up (htab_create_alloc (41,
10923 hash_dwo_file,
10924 eq_dwo_file,
10925 delete_dwo_file,
10926 xcalloc, xfree));
10927 }
10928
10929 /* Lookup DWO file DWO_NAME. */
10930
10931 static void **
10932 lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
10933 const char *dwo_name,
10934 const char *comp_dir)
10935 {
10936 struct dwo_file find_entry;
10937 void **slot;
10938
10939 if (dwarf2_per_objfile->dwo_files == NULL)
10940 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
10941
10942 find_entry.dwo_name = dwo_name;
10943 find_entry.comp_dir = comp_dir;
10944 slot = htab_find_slot (dwarf2_per_objfile->dwo_files.get (), &find_entry,
10945 INSERT);
10946
10947 return slot;
10948 }
10949
10950 static hashval_t
10951 hash_dwo_unit (const void *item)
10952 {
10953 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
10954
10955 /* This drops the top 32 bits of the id, but is ok for a hash. */
10956 return dwo_unit->signature;
10957 }
10958
10959 static int
10960 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
10961 {
10962 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
10963 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
10964
10965 /* The signature is assumed to be unique within the DWO file.
10966 So while object file CU dwo_id's always have the value zero,
10967 that's OK, assuming each object file DWO file has only one CU,
10968 and that's the rule for now. */
10969 return lhs->signature == rhs->signature;
10970 }
10971
10972 /* Allocate a hash table for DWO CUs,TUs.
10973 There is one of these tables for each of CUs,TUs for each DWO file. */
10974
10975 static htab_up
10976 allocate_dwo_unit_table ()
10977 {
10978 /* Start out with a pretty small number.
10979 Generally DWO files contain only one CU and maybe some TUs. */
10980 return htab_up (htab_create_alloc (3,
10981 hash_dwo_unit,
10982 eq_dwo_unit,
10983 NULL, xcalloc, xfree));
10984 }
10985
10986 /* die_reader_func for create_dwo_cu. */
10987
10988 static void
10989 create_dwo_cu_reader (const struct die_reader_specs *reader,
10990 const gdb_byte *info_ptr,
10991 struct die_info *comp_unit_die,
10992 struct dwo_file *dwo_file,
10993 struct dwo_unit *dwo_unit)
10994 {
10995 struct dwarf2_cu *cu = reader->cu;
10996 sect_offset sect_off = cu->per_cu->sect_off;
10997 struct dwarf2_section_info *section = cu->per_cu->section;
10998
10999 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11000 if (!signature.has_value ())
11001 {
11002 complaint (_("Dwarf Error: debug entry at offset %s is missing"
11003 " its dwo_id [in module %s]"),
11004 sect_offset_str (sect_off), dwo_file->dwo_name);
11005 return;
11006 }
11007
11008 dwo_unit->dwo_file = dwo_file;
11009 dwo_unit->signature = *signature;
11010 dwo_unit->section = section;
11011 dwo_unit->sect_off = sect_off;
11012 dwo_unit->length = cu->per_cu->length;
11013
11014 if (dwarf_read_debug)
11015 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11016 sect_offset_str (sect_off),
11017 hex_string (dwo_unit->signature));
11018 }
11019
11020 /* Create the dwo_units for the CUs in a DWO_FILE.
11021 Note: This function processes DWO files only, not DWP files. */
11022
11023 static void
11024 create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11025 dwarf2_cu *cu, struct dwo_file &dwo_file,
11026 dwarf2_section_info &section, htab_up &cus_htab)
11027 {
11028 struct objfile *objfile = dwarf2_per_objfile->objfile;
11029 const gdb_byte *info_ptr, *end_ptr;
11030
11031 section.read (objfile);
11032 info_ptr = section.buffer;
11033
11034 if (info_ptr == NULL)
11035 return;
11036
11037 if (dwarf_read_debug)
11038 {
11039 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
11040 section.get_name (),
11041 section.get_file_name ());
11042 }
11043
11044 end_ptr = info_ptr + section.size;
11045 while (info_ptr < end_ptr)
11046 {
11047 struct dwarf2_per_cu_data per_cu;
11048 struct dwo_unit read_unit {};
11049 struct dwo_unit *dwo_unit;
11050 void **slot;
11051 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11052
11053 memset (&per_cu, 0, sizeof (per_cu));
11054 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
11055 per_cu.is_debug_types = 0;
11056 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11057 per_cu.section = &section;
11058
11059 cutu_reader reader (&per_cu, cu, &dwo_file);
11060 if (!reader.dummy_p)
11061 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
11062 &dwo_file, &read_unit);
11063 info_ptr += per_cu.length;
11064
11065 // If the unit could not be parsed, skip it.
11066 if (read_unit.dwo_file == NULL)
11067 continue;
11068
11069 if (cus_htab == NULL)
11070 cus_htab = allocate_dwo_unit_table ();
11071
11072 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11073 *dwo_unit = read_unit;
11074 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
11075 gdb_assert (slot != NULL);
11076 if (*slot != NULL)
11077 {
11078 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11079 sect_offset dup_sect_off = dup_cu->sect_off;
11080
11081 complaint (_("debug cu entry at offset %s is duplicate to"
11082 " the entry at offset %s, signature %s"),
11083 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
11084 hex_string (dwo_unit->signature));
11085 }
11086 *slot = (void *)dwo_unit;
11087 }
11088 }
11089
11090 /* DWP file .debug_{cu,tu}_index section format:
11091 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11092
11093 DWP Version 1:
11094
11095 Both index sections have the same format, and serve to map a 64-bit
11096 signature to a set of section numbers. Each section begins with a header,
11097 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11098 indexes, and a pool of 32-bit section numbers. The index sections will be
11099 aligned at 8-byte boundaries in the file.
11100
11101 The index section header consists of:
11102
11103 V, 32 bit version number
11104 -, 32 bits unused
11105 N, 32 bit number of compilation units or type units in the index
11106 M, 32 bit number of slots in the hash table
11107
11108 Numbers are recorded using the byte order of the application binary.
11109
11110 The hash table begins at offset 16 in the section, and consists of an array
11111 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11112 order of the application binary). Unused slots in the hash table are 0.
11113 (We rely on the extreme unlikeliness of a signature being exactly 0.)
11114
11115 The parallel table begins immediately after the hash table
11116 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11117 array of 32-bit indexes (using the byte order of the application binary),
11118 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11119 table contains a 32-bit index into the pool of section numbers. For unused
11120 hash table slots, the corresponding entry in the parallel table will be 0.
11121
11122 The pool of section numbers begins immediately following the hash table
11123 (at offset 16 + 12 * M from the beginning of the section). The pool of
11124 section numbers consists of an array of 32-bit words (using the byte order
11125 of the application binary). Each item in the array is indexed starting
11126 from 0. The hash table entry provides the index of the first section
11127 number in the set. Additional section numbers in the set follow, and the
11128 set is terminated by a 0 entry (section number 0 is not used in ELF).
11129
11130 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11131 section must be the first entry in the set, and the .debug_abbrev.dwo must
11132 be the second entry. Other members of the set may follow in any order.
11133
11134 ---
11135
11136 DWP Version 2:
11137
11138 DWP Version 2 combines all the .debug_info, etc. sections into one,
11139 and the entries in the index tables are now offsets into these sections.
11140 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11141 section.
11142
11143 Index Section Contents:
11144 Header
11145 Hash Table of Signatures dwp_hash_table.hash_table
11146 Parallel Table of Indices dwp_hash_table.unit_table
11147 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
11148 Table of Section Sizes dwp_hash_table.v2.sizes
11149
11150 The index section header consists of:
11151
11152 V, 32 bit version number
11153 L, 32 bit number of columns in the table of section offsets
11154 N, 32 bit number of compilation units or type units in the index
11155 M, 32 bit number of slots in the hash table
11156
11157 Numbers are recorded using the byte order of the application binary.
11158
11159 The hash table has the same format as version 1.
11160 The parallel table of indices has the same format as version 1,
11161 except that the entries are origin-1 indices into the table of sections
11162 offsets and the table of section sizes.
11163
11164 The table of offsets begins immediately following the parallel table
11165 (at offset 16 + 12 * M from the beginning of the section). The table is
11166 a two-dimensional array of 32-bit words (using the byte order of the
11167 application binary), with L columns and N+1 rows, in row-major order.
11168 Each row in the array is indexed starting from 0. The first row provides
11169 a key to the remaining rows: each column in this row provides an identifier
11170 for a debug section, and the offsets in the same column of subsequent rows
11171 refer to that section. The section identifiers are:
11172
11173 DW_SECT_INFO 1 .debug_info.dwo
11174 DW_SECT_TYPES 2 .debug_types.dwo
11175 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11176 DW_SECT_LINE 4 .debug_line.dwo
11177 DW_SECT_LOC 5 .debug_loc.dwo
11178 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11179 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11180 DW_SECT_MACRO 8 .debug_macro.dwo
11181
11182 The offsets provided by the CU and TU index sections are the base offsets
11183 for the contributions made by each CU or TU to the corresponding section
11184 in the package file. Each CU and TU header contains an abbrev_offset
11185 field, used to find the abbreviations table for that CU or TU within the
11186 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11187 be interpreted as relative to the base offset given in the index section.
11188 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11189 should be interpreted as relative to the base offset for .debug_line.dwo,
11190 and offsets into other debug sections obtained from DWARF attributes should
11191 also be interpreted as relative to the corresponding base offset.
11192
11193 The table of sizes begins immediately following the table of offsets.
11194 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11195 with L columns and N rows, in row-major order. Each row in the array is
11196 indexed starting from 1 (row 0 is shared by the two tables).
11197
11198 ---
11199
11200 Hash table lookup is handled the same in version 1 and 2:
11201
11202 We assume that N and M will not exceed 2^32 - 1.
11203 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11204
11205 Given a 64-bit compilation unit signature or a type signature S, an entry
11206 in the hash table is located as follows:
11207
11208 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11209 the low-order k bits all set to 1.
11210
11211 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
11212
11213 3) If the hash table entry at index H matches the signature, use that
11214 entry. If the hash table entry at index H is unused (all zeroes),
11215 terminate the search: the signature is not present in the table.
11216
11217 4) Let H = (H + H') modulo M. Repeat at Step 3.
11218
11219 Because M > N and H' and M are relatively prime, the search is guaranteed
11220 to stop at an unused slot or find the match. */
11221
11222 /* Create a hash table to map DWO IDs to their CU/TU entry in
11223 .debug_{info,types}.dwo in DWP_FILE.
11224 Returns NULL if there isn't one.
11225 Note: This function processes DWP files only, not DWO files. */
11226
11227 static struct dwp_hash_table *
11228 create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11229 struct dwp_file *dwp_file, int is_debug_types)
11230 {
11231 struct objfile *objfile = dwarf2_per_objfile->objfile;
11232 bfd *dbfd = dwp_file->dbfd.get ();
11233 const gdb_byte *index_ptr, *index_end;
11234 struct dwarf2_section_info *index;
11235 uint32_t version, nr_columns, nr_units, nr_slots;
11236 struct dwp_hash_table *htab;
11237
11238 if (is_debug_types)
11239 index = &dwp_file->sections.tu_index;
11240 else
11241 index = &dwp_file->sections.cu_index;
11242
11243 if (index->empty ())
11244 return NULL;
11245 index->read (objfile);
11246
11247 index_ptr = index->buffer;
11248 index_end = index_ptr + index->size;
11249
11250 version = read_4_bytes (dbfd, index_ptr);
11251 index_ptr += 4;
11252 if (version == 2)
11253 nr_columns = read_4_bytes (dbfd, index_ptr);
11254 else
11255 nr_columns = 0;
11256 index_ptr += 4;
11257 nr_units = read_4_bytes (dbfd, index_ptr);
11258 index_ptr += 4;
11259 nr_slots = read_4_bytes (dbfd, index_ptr);
11260 index_ptr += 4;
11261
11262 if (version != 1 && version != 2)
11263 {
11264 error (_("Dwarf Error: unsupported DWP file version (%s)"
11265 " [in module %s]"),
11266 pulongest (version), dwp_file->name);
11267 }
11268 if (nr_slots != (nr_slots & -nr_slots))
11269 {
11270 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
11271 " is not power of 2 [in module %s]"),
11272 pulongest (nr_slots), dwp_file->name);
11273 }
11274
11275 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
11276 htab->version = version;
11277 htab->nr_columns = nr_columns;
11278 htab->nr_units = nr_units;
11279 htab->nr_slots = nr_slots;
11280 htab->hash_table = index_ptr;
11281 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
11282
11283 /* Exit early if the table is empty. */
11284 if (nr_slots == 0 || nr_units == 0
11285 || (version == 2 && nr_columns == 0))
11286 {
11287 /* All must be zero. */
11288 if (nr_slots != 0 || nr_units != 0
11289 || (version == 2 && nr_columns != 0))
11290 {
11291 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
11292 " all zero [in modules %s]"),
11293 dwp_file->name);
11294 }
11295 return htab;
11296 }
11297
11298 if (version == 1)
11299 {
11300 htab->section_pool.v1.indices =
11301 htab->unit_table + sizeof (uint32_t) * nr_slots;
11302 /* It's harder to decide whether the section is too small in v1.
11303 V1 is deprecated anyway so we punt. */
11304 }
11305 else
11306 {
11307 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11308 int *ids = htab->section_pool.v2.section_ids;
11309 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
11310 /* Reverse map for error checking. */
11311 int ids_seen[DW_SECT_MAX + 1];
11312 int i;
11313
11314 if (nr_columns < 2)
11315 {
11316 error (_("Dwarf Error: bad DWP hash table, too few columns"
11317 " in section table [in module %s]"),
11318 dwp_file->name);
11319 }
11320 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11321 {
11322 error (_("Dwarf Error: bad DWP hash table, too many columns"
11323 " in section table [in module %s]"),
11324 dwp_file->name);
11325 }
11326 memset (ids, 255, sizeof_ids);
11327 memset (ids_seen, 255, sizeof (ids_seen));
11328 for (i = 0; i < nr_columns; ++i)
11329 {
11330 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11331
11332 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11333 {
11334 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11335 " in section table [in module %s]"),
11336 id, dwp_file->name);
11337 }
11338 if (ids_seen[id] != -1)
11339 {
11340 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11341 " id %d in section table [in module %s]"),
11342 id, dwp_file->name);
11343 }
11344 ids_seen[id] = i;
11345 ids[i] = id;
11346 }
11347 /* Must have exactly one info or types section. */
11348 if (((ids_seen[DW_SECT_INFO] != -1)
11349 + (ids_seen[DW_SECT_TYPES] != -1))
11350 != 1)
11351 {
11352 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11353 " DWO info/types section [in module %s]"),
11354 dwp_file->name);
11355 }
11356 /* Must have an abbrev section. */
11357 if (ids_seen[DW_SECT_ABBREV] == -1)
11358 {
11359 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11360 " section [in module %s]"),
11361 dwp_file->name);
11362 }
11363 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11364 htab->section_pool.v2.sizes =
11365 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11366 * nr_units * nr_columns);
11367 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11368 * nr_units * nr_columns))
11369 > index_end)
11370 {
11371 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11372 " [in module %s]"),
11373 dwp_file->name);
11374 }
11375 }
11376
11377 return htab;
11378 }
11379
11380 /* Update SECTIONS with the data from SECTP.
11381
11382 This function is like the other "locate" section routines that are
11383 passed to bfd_map_over_sections, but in this context the sections to
11384 read comes from the DWP V1 hash table, not the full ELF section table.
11385
11386 The result is non-zero for success, or zero if an error was found. */
11387
11388 static int
11389 locate_v1_virtual_dwo_sections (asection *sectp,
11390 struct virtual_v1_dwo_sections *sections)
11391 {
11392 const struct dwop_section_names *names = &dwop_section_names;
11393
11394 if (section_is_p (sectp->name, &names->abbrev_dwo))
11395 {
11396 /* There can be only one. */
11397 if (sections->abbrev.s.section != NULL)
11398 return 0;
11399 sections->abbrev.s.section = sectp;
11400 sections->abbrev.size = bfd_section_size (sectp);
11401 }
11402 else if (section_is_p (sectp->name, &names->info_dwo)
11403 || section_is_p (sectp->name, &names->types_dwo))
11404 {
11405 /* There can be only one. */
11406 if (sections->info_or_types.s.section != NULL)
11407 return 0;
11408 sections->info_or_types.s.section = sectp;
11409 sections->info_or_types.size = bfd_section_size (sectp);
11410 }
11411 else if (section_is_p (sectp->name, &names->line_dwo))
11412 {
11413 /* There can be only one. */
11414 if (sections->line.s.section != NULL)
11415 return 0;
11416 sections->line.s.section = sectp;
11417 sections->line.size = bfd_section_size (sectp);
11418 }
11419 else if (section_is_p (sectp->name, &names->loc_dwo))
11420 {
11421 /* There can be only one. */
11422 if (sections->loc.s.section != NULL)
11423 return 0;
11424 sections->loc.s.section = sectp;
11425 sections->loc.size = bfd_section_size (sectp);
11426 }
11427 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11428 {
11429 /* There can be only one. */
11430 if (sections->macinfo.s.section != NULL)
11431 return 0;
11432 sections->macinfo.s.section = sectp;
11433 sections->macinfo.size = bfd_section_size (sectp);
11434 }
11435 else if (section_is_p (sectp->name, &names->macro_dwo))
11436 {
11437 /* There can be only one. */
11438 if (sections->macro.s.section != NULL)
11439 return 0;
11440 sections->macro.s.section = sectp;
11441 sections->macro.size = bfd_section_size (sectp);
11442 }
11443 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11444 {
11445 /* There can be only one. */
11446 if (sections->str_offsets.s.section != NULL)
11447 return 0;
11448 sections->str_offsets.s.section = sectp;
11449 sections->str_offsets.size = bfd_section_size (sectp);
11450 }
11451 else
11452 {
11453 /* No other kind of section is valid. */
11454 return 0;
11455 }
11456
11457 return 1;
11458 }
11459
11460 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11461 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11462 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11463 This is for DWP version 1 files. */
11464
11465 static struct dwo_unit *
11466 create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11467 struct dwp_file *dwp_file,
11468 uint32_t unit_index,
11469 const char *comp_dir,
11470 ULONGEST signature, int is_debug_types)
11471 {
11472 struct objfile *objfile = dwarf2_per_objfile->objfile;
11473 const struct dwp_hash_table *dwp_htab =
11474 is_debug_types ? dwp_file->tus : dwp_file->cus;
11475 bfd *dbfd = dwp_file->dbfd.get ();
11476 const char *kind = is_debug_types ? "TU" : "CU";
11477 struct dwo_file *dwo_file;
11478 struct dwo_unit *dwo_unit;
11479 struct virtual_v1_dwo_sections sections;
11480 void **dwo_file_slot;
11481 int i;
11482
11483 gdb_assert (dwp_file->version == 1);
11484
11485 if (dwarf_read_debug)
11486 {
11487 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
11488 kind,
11489 pulongest (unit_index), hex_string (signature),
11490 dwp_file->name);
11491 }
11492
11493 /* Fetch the sections of this DWO unit.
11494 Put a limit on the number of sections we look for so that bad data
11495 doesn't cause us to loop forever. */
11496
11497 #define MAX_NR_V1_DWO_SECTIONS \
11498 (1 /* .debug_info or .debug_types */ \
11499 + 1 /* .debug_abbrev */ \
11500 + 1 /* .debug_line */ \
11501 + 1 /* .debug_loc */ \
11502 + 1 /* .debug_str_offsets */ \
11503 + 1 /* .debug_macro or .debug_macinfo */ \
11504 + 1 /* trailing zero */)
11505
11506 memset (&sections, 0, sizeof (sections));
11507
11508 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
11509 {
11510 asection *sectp;
11511 uint32_t section_nr =
11512 read_4_bytes (dbfd,
11513 dwp_htab->section_pool.v1.indices
11514 + (unit_index + i) * sizeof (uint32_t));
11515
11516 if (section_nr == 0)
11517 break;
11518 if (section_nr >= dwp_file->num_sections)
11519 {
11520 error (_("Dwarf Error: bad DWP hash table, section number too large"
11521 " [in module %s]"),
11522 dwp_file->name);
11523 }
11524
11525 sectp = dwp_file->elf_sections[section_nr];
11526 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
11527 {
11528 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11529 " [in module %s]"),
11530 dwp_file->name);
11531 }
11532 }
11533
11534 if (i < 2
11535 || sections.info_or_types.empty ()
11536 || sections.abbrev.empty ())
11537 {
11538 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11539 " [in module %s]"),
11540 dwp_file->name);
11541 }
11542 if (i == MAX_NR_V1_DWO_SECTIONS)
11543 {
11544 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11545 " [in module %s]"),
11546 dwp_file->name);
11547 }
11548
11549 /* It's easier for the rest of the code if we fake a struct dwo_file and
11550 have dwo_unit "live" in that. At least for now.
11551
11552 The DWP file can be made up of a random collection of CUs and TUs.
11553 However, for each CU + set of TUs that came from the same original DWO
11554 file, we can combine them back into a virtual DWO file to save space
11555 (fewer struct dwo_file objects to allocate). Remember that for really
11556 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11557
11558 std::string virtual_dwo_name =
11559 string_printf ("virtual-dwo/%d-%d-%d-%d",
11560 sections.abbrev.get_id (),
11561 sections.line.get_id (),
11562 sections.loc.get_id (),
11563 sections.str_offsets.get_id ());
11564 /* Can we use an existing virtual DWO file? */
11565 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11566 virtual_dwo_name.c_str (),
11567 comp_dir);
11568 /* Create one if necessary. */
11569 if (*dwo_file_slot == NULL)
11570 {
11571 if (dwarf_read_debug)
11572 {
11573 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
11574 virtual_dwo_name.c_str ());
11575 }
11576 dwo_file = new struct dwo_file;
11577 dwo_file->dwo_name = objfile->intern (virtual_dwo_name);
11578 dwo_file->comp_dir = comp_dir;
11579 dwo_file->sections.abbrev = sections.abbrev;
11580 dwo_file->sections.line = sections.line;
11581 dwo_file->sections.loc = sections.loc;
11582 dwo_file->sections.macinfo = sections.macinfo;
11583 dwo_file->sections.macro = sections.macro;
11584 dwo_file->sections.str_offsets = sections.str_offsets;
11585 /* The "str" section is global to the entire DWP file. */
11586 dwo_file->sections.str = dwp_file->sections.str;
11587 /* The info or types section is assigned below to dwo_unit,
11588 there's no need to record it in dwo_file.
11589 Also, we can't simply record type sections in dwo_file because
11590 we record a pointer into the vector in dwo_unit. As we collect more
11591 types we'll grow the vector and eventually have to reallocate space
11592 for it, invalidating all copies of pointers into the previous
11593 contents. */
11594 *dwo_file_slot = dwo_file;
11595 }
11596 else
11597 {
11598 if (dwarf_read_debug)
11599 {
11600 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
11601 virtual_dwo_name.c_str ());
11602 }
11603 dwo_file = (struct dwo_file *) *dwo_file_slot;
11604 }
11605
11606 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11607 dwo_unit->dwo_file = dwo_file;
11608 dwo_unit->signature = signature;
11609 dwo_unit->section =
11610 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
11611 *dwo_unit->section = sections.info_or_types;
11612 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11613
11614 return dwo_unit;
11615 }
11616
11617 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
11618 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
11619 piece within that section used by a TU/CU, return a virtual section
11620 of just that piece. */
11621
11622 static struct dwarf2_section_info
11623 create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
11624 struct dwarf2_section_info *section,
11625 bfd_size_type offset, bfd_size_type size)
11626 {
11627 struct dwarf2_section_info result;
11628 asection *sectp;
11629
11630 gdb_assert (section != NULL);
11631 gdb_assert (!section->is_virtual);
11632
11633 memset (&result, 0, sizeof (result));
11634 result.s.containing_section = section;
11635 result.is_virtual = true;
11636
11637 if (size == 0)
11638 return result;
11639
11640 sectp = section->get_bfd_section ();
11641
11642 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11643 bounds of the real section. This is a pretty-rare event, so just
11644 flag an error (easier) instead of a warning and trying to cope. */
11645 if (sectp == NULL
11646 || offset + size > bfd_section_size (sectp))
11647 {
11648 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
11649 " in section %s [in module %s]"),
11650 sectp ? bfd_section_name (sectp) : "<unknown>",
11651 objfile_name (dwarf2_per_objfile->objfile));
11652 }
11653
11654 result.virtual_offset = offset;
11655 result.size = size;
11656 return result;
11657 }
11658
11659 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11660 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11661 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11662 This is for DWP version 2 files. */
11663
11664 static struct dwo_unit *
11665 create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11666 struct dwp_file *dwp_file,
11667 uint32_t unit_index,
11668 const char *comp_dir,
11669 ULONGEST signature, int is_debug_types)
11670 {
11671 struct objfile *objfile = dwarf2_per_objfile->objfile;
11672 const struct dwp_hash_table *dwp_htab =
11673 is_debug_types ? dwp_file->tus : dwp_file->cus;
11674 bfd *dbfd = dwp_file->dbfd.get ();
11675 const char *kind = is_debug_types ? "TU" : "CU";
11676 struct dwo_file *dwo_file;
11677 struct dwo_unit *dwo_unit;
11678 struct virtual_v2_dwo_sections sections;
11679 void **dwo_file_slot;
11680 int i;
11681
11682 gdb_assert (dwp_file->version == 2);
11683
11684 if (dwarf_read_debug)
11685 {
11686 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
11687 kind,
11688 pulongest (unit_index), hex_string (signature),
11689 dwp_file->name);
11690 }
11691
11692 /* Fetch the section offsets of this DWO unit. */
11693
11694 memset (&sections, 0, sizeof (sections));
11695
11696 for (i = 0; i < dwp_htab->nr_columns; ++i)
11697 {
11698 uint32_t offset = read_4_bytes (dbfd,
11699 dwp_htab->section_pool.v2.offsets
11700 + (((unit_index - 1) * dwp_htab->nr_columns
11701 + i)
11702 * sizeof (uint32_t)));
11703 uint32_t size = read_4_bytes (dbfd,
11704 dwp_htab->section_pool.v2.sizes
11705 + (((unit_index - 1) * dwp_htab->nr_columns
11706 + i)
11707 * sizeof (uint32_t)));
11708
11709 switch (dwp_htab->section_pool.v2.section_ids[i])
11710 {
11711 case DW_SECT_INFO:
11712 case DW_SECT_TYPES:
11713 sections.info_or_types_offset = offset;
11714 sections.info_or_types_size = size;
11715 break;
11716 case DW_SECT_ABBREV:
11717 sections.abbrev_offset = offset;
11718 sections.abbrev_size = size;
11719 break;
11720 case DW_SECT_LINE:
11721 sections.line_offset = offset;
11722 sections.line_size = size;
11723 break;
11724 case DW_SECT_LOC:
11725 sections.loc_offset = offset;
11726 sections.loc_size = size;
11727 break;
11728 case DW_SECT_STR_OFFSETS:
11729 sections.str_offsets_offset = offset;
11730 sections.str_offsets_size = size;
11731 break;
11732 case DW_SECT_MACINFO:
11733 sections.macinfo_offset = offset;
11734 sections.macinfo_size = size;
11735 break;
11736 case DW_SECT_MACRO:
11737 sections.macro_offset = offset;
11738 sections.macro_size = size;
11739 break;
11740 }
11741 }
11742
11743 /* It's easier for the rest of the code if we fake a struct dwo_file and
11744 have dwo_unit "live" in that. At least for now.
11745
11746 The DWP file can be made up of a random collection of CUs and TUs.
11747 However, for each CU + set of TUs that came from the same original DWO
11748 file, we can combine them back into a virtual DWO file to save space
11749 (fewer struct dwo_file objects to allocate). Remember that for really
11750 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11751
11752 std::string virtual_dwo_name =
11753 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
11754 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
11755 (long) (sections.line_size ? sections.line_offset : 0),
11756 (long) (sections.loc_size ? sections.loc_offset : 0),
11757 (long) (sections.str_offsets_size
11758 ? sections.str_offsets_offset : 0));
11759 /* Can we use an existing virtual DWO file? */
11760 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11761 virtual_dwo_name.c_str (),
11762 comp_dir);
11763 /* Create one if necessary. */
11764 if (*dwo_file_slot == NULL)
11765 {
11766 if (dwarf_read_debug)
11767 {
11768 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
11769 virtual_dwo_name.c_str ());
11770 }
11771 dwo_file = new struct dwo_file;
11772 dwo_file->dwo_name = objfile->intern (virtual_dwo_name);
11773 dwo_file->comp_dir = comp_dir;
11774 dwo_file->sections.abbrev =
11775 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
11776 sections.abbrev_offset, sections.abbrev_size);
11777 dwo_file->sections.line =
11778 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
11779 sections.line_offset, sections.line_size);
11780 dwo_file->sections.loc =
11781 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
11782 sections.loc_offset, sections.loc_size);
11783 dwo_file->sections.macinfo =
11784 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
11785 sections.macinfo_offset, sections.macinfo_size);
11786 dwo_file->sections.macro =
11787 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
11788 sections.macro_offset, sections.macro_size);
11789 dwo_file->sections.str_offsets =
11790 create_dwp_v2_section (dwarf2_per_objfile,
11791 &dwp_file->sections.str_offsets,
11792 sections.str_offsets_offset,
11793 sections.str_offsets_size);
11794 /* The "str" section is global to the entire DWP file. */
11795 dwo_file->sections.str = dwp_file->sections.str;
11796 /* The info or types section is assigned below to dwo_unit,
11797 there's no need to record it in dwo_file.
11798 Also, we can't simply record type sections in dwo_file because
11799 we record a pointer into the vector in dwo_unit. As we collect more
11800 types we'll grow the vector and eventually have to reallocate space
11801 for it, invalidating all copies of pointers into the previous
11802 contents. */
11803 *dwo_file_slot = dwo_file;
11804 }
11805 else
11806 {
11807 if (dwarf_read_debug)
11808 {
11809 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
11810 virtual_dwo_name.c_str ());
11811 }
11812 dwo_file = (struct dwo_file *) *dwo_file_slot;
11813 }
11814
11815 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11816 dwo_unit->dwo_file = dwo_file;
11817 dwo_unit->signature = signature;
11818 dwo_unit->section =
11819 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
11820 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
11821 is_debug_types
11822 ? &dwp_file->sections.types
11823 : &dwp_file->sections.info,
11824 sections.info_or_types_offset,
11825 sections.info_or_types_size);
11826 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11827
11828 return dwo_unit;
11829 }
11830
11831 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
11832 Returns NULL if the signature isn't found. */
11833
11834 static struct dwo_unit *
11835 lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
11836 struct dwp_file *dwp_file, const char *comp_dir,
11837 ULONGEST signature, int is_debug_types)
11838 {
11839 const struct dwp_hash_table *dwp_htab =
11840 is_debug_types ? dwp_file->tus : dwp_file->cus;
11841 bfd *dbfd = dwp_file->dbfd.get ();
11842 uint32_t mask = dwp_htab->nr_slots - 1;
11843 uint32_t hash = signature & mask;
11844 uint32_t hash2 = ((signature >> 32) & mask) | 1;
11845 unsigned int i;
11846 void **slot;
11847 struct dwo_unit find_dwo_cu;
11848
11849 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
11850 find_dwo_cu.signature = signature;
11851 slot = htab_find_slot (is_debug_types
11852 ? dwp_file->loaded_tus.get ()
11853 : dwp_file->loaded_cus.get (),
11854 &find_dwo_cu, INSERT);
11855
11856 if (*slot != NULL)
11857 return (struct dwo_unit *) *slot;
11858
11859 /* Use a for loop so that we don't loop forever on bad debug info. */
11860 for (i = 0; i < dwp_htab->nr_slots; ++i)
11861 {
11862 ULONGEST signature_in_table;
11863
11864 signature_in_table =
11865 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
11866 if (signature_in_table == signature)
11867 {
11868 uint32_t unit_index =
11869 read_4_bytes (dbfd,
11870 dwp_htab->unit_table + hash * sizeof (uint32_t));
11871
11872 if (dwp_file->version == 1)
11873 {
11874 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
11875 dwp_file, unit_index,
11876 comp_dir, signature,
11877 is_debug_types);
11878 }
11879 else
11880 {
11881 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
11882 dwp_file, unit_index,
11883 comp_dir, signature,
11884 is_debug_types);
11885 }
11886 return (struct dwo_unit *) *slot;
11887 }
11888 if (signature_in_table == 0)
11889 return NULL;
11890 hash = (hash + hash2) & mask;
11891 }
11892
11893 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
11894 " [in module %s]"),
11895 dwp_file->name);
11896 }
11897
11898 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
11899 Open the file specified by FILE_NAME and hand it off to BFD for
11900 preliminary analysis. Return a newly initialized bfd *, which
11901 includes a canonicalized copy of FILE_NAME.
11902 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
11903 SEARCH_CWD is true if the current directory is to be searched.
11904 It will be searched before debug-file-directory.
11905 If successful, the file is added to the bfd include table of the
11906 objfile's bfd (see gdb_bfd_record_inclusion).
11907 If unable to find/open the file, return NULL.
11908 NOTE: This function is derived from symfile_bfd_open. */
11909
11910 static gdb_bfd_ref_ptr
11911 try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
11912 const char *file_name, int is_dwp, int search_cwd)
11913 {
11914 int desc;
11915 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
11916 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
11917 to debug_file_directory. */
11918 const char *search_path;
11919 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
11920
11921 gdb::unique_xmalloc_ptr<char> search_path_holder;
11922 if (search_cwd)
11923 {
11924 if (*debug_file_directory != '\0')
11925 {
11926 search_path_holder.reset (concat (".", dirname_separator_string,
11927 debug_file_directory,
11928 (char *) NULL));
11929 search_path = search_path_holder.get ();
11930 }
11931 else
11932 search_path = ".";
11933 }
11934 else
11935 search_path = debug_file_directory;
11936
11937 openp_flags flags = OPF_RETURN_REALPATH;
11938 if (is_dwp)
11939 flags |= OPF_SEARCH_IN_PATH;
11940
11941 gdb::unique_xmalloc_ptr<char> absolute_name;
11942 desc = openp (search_path, flags, file_name,
11943 O_RDONLY | O_BINARY, &absolute_name);
11944 if (desc < 0)
11945 return NULL;
11946
11947 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
11948 gnutarget, desc));
11949 if (sym_bfd == NULL)
11950 return NULL;
11951 bfd_set_cacheable (sym_bfd.get (), 1);
11952
11953 if (!bfd_check_format (sym_bfd.get (), bfd_object))
11954 return NULL;
11955
11956 /* Success. Record the bfd as having been included by the objfile's bfd.
11957 This is important because things like demangled_names_hash lives in the
11958 objfile's per_bfd space and may have references to things like symbol
11959 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
11960 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
11961
11962 return sym_bfd;
11963 }
11964
11965 /* Try to open DWO file FILE_NAME.
11966 COMP_DIR is the DW_AT_comp_dir attribute.
11967 The result is the bfd handle of the file.
11968 If there is a problem finding or opening the file, return NULL.
11969 Upon success, the canonicalized path of the file is stored in the bfd,
11970 same as symfile_bfd_open. */
11971
11972 static gdb_bfd_ref_ptr
11973 open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
11974 const char *file_name, const char *comp_dir)
11975 {
11976 if (IS_ABSOLUTE_PATH (file_name))
11977 return try_open_dwop_file (dwarf2_per_objfile, file_name,
11978 0 /*is_dwp*/, 0 /*search_cwd*/);
11979
11980 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
11981
11982 if (comp_dir != NULL)
11983 {
11984 gdb::unique_xmalloc_ptr<char> path_to_try
11985 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
11986
11987 /* NOTE: If comp_dir is a relative path, this will also try the
11988 search path, which seems useful. */
11989 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
11990 path_to_try.get (),
11991 0 /*is_dwp*/,
11992 1 /*search_cwd*/));
11993 if (abfd != NULL)
11994 return abfd;
11995 }
11996
11997 /* That didn't work, try debug-file-directory, which, despite its name,
11998 is a list of paths. */
11999
12000 if (*debug_file_directory == '\0')
12001 return NULL;
12002
12003 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12004 0 /*is_dwp*/, 1 /*search_cwd*/);
12005 }
12006
12007 /* This function is mapped across the sections and remembers the offset and
12008 size of each of the DWO debugging sections we are interested in. */
12009
12010 static void
12011 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12012 {
12013 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
12014 const struct dwop_section_names *names = &dwop_section_names;
12015
12016 if (section_is_p (sectp->name, &names->abbrev_dwo))
12017 {
12018 dwo_sections->abbrev.s.section = sectp;
12019 dwo_sections->abbrev.size = bfd_section_size (sectp);
12020 }
12021 else if (section_is_p (sectp->name, &names->info_dwo))
12022 {
12023 dwo_sections->info.s.section = sectp;
12024 dwo_sections->info.size = bfd_section_size (sectp);
12025 }
12026 else if (section_is_p (sectp->name, &names->line_dwo))
12027 {
12028 dwo_sections->line.s.section = sectp;
12029 dwo_sections->line.size = bfd_section_size (sectp);
12030 }
12031 else if (section_is_p (sectp->name, &names->loc_dwo))
12032 {
12033 dwo_sections->loc.s.section = sectp;
12034 dwo_sections->loc.size = bfd_section_size (sectp);
12035 }
12036 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12037 {
12038 dwo_sections->macinfo.s.section = sectp;
12039 dwo_sections->macinfo.size = bfd_section_size (sectp);
12040 }
12041 else if (section_is_p (sectp->name, &names->macro_dwo))
12042 {
12043 dwo_sections->macro.s.section = sectp;
12044 dwo_sections->macro.size = bfd_section_size (sectp);
12045 }
12046 else if (section_is_p (sectp->name, &names->str_dwo))
12047 {
12048 dwo_sections->str.s.section = sectp;
12049 dwo_sections->str.size = bfd_section_size (sectp);
12050 }
12051 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12052 {
12053 dwo_sections->str_offsets.s.section = sectp;
12054 dwo_sections->str_offsets.size = bfd_section_size (sectp);
12055 }
12056 else if (section_is_p (sectp->name, &names->types_dwo))
12057 {
12058 struct dwarf2_section_info type_section;
12059
12060 memset (&type_section, 0, sizeof (type_section));
12061 type_section.s.section = sectp;
12062 type_section.size = bfd_section_size (sectp);
12063 dwo_sections->types.push_back (type_section);
12064 }
12065 }
12066
12067 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12068 by PER_CU. This is for the non-DWP case.
12069 The result is NULL if DWO_NAME can't be found. */
12070
12071 static struct dwo_file *
12072 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
12073 const char *dwo_name, const char *comp_dir)
12074 {
12075 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
12076
12077 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
12078 if (dbfd == NULL)
12079 {
12080 if (dwarf_read_debug)
12081 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12082 return NULL;
12083 }
12084
12085 dwo_file_up dwo_file (new struct dwo_file);
12086 dwo_file->dwo_name = dwo_name;
12087 dwo_file->comp_dir = comp_dir;
12088 dwo_file->dbfd = std::move (dbfd);
12089
12090 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
12091 &dwo_file->sections);
12092
12093 create_cus_hash_table (dwarf2_per_objfile, per_cu->cu, *dwo_file,
12094 dwo_file->sections.info, dwo_file->cus);
12095
12096 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
12097 dwo_file->sections.types, dwo_file->tus);
12098
12099 if (dwarf_read_debug)
12100 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12101
12102 return dwo_file.release ();
12103 }
12104
12105 /* This function is mapped across the sections and remembers the offset and
12106 size of each of the DWP debugging sections common to version 1 and 2 that
12107 we are interested in. */
12108
12109 static void
12110 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12111 void *dwp_file_ptr)
12112 {
12113 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12114 const struct dwop_section_names *names = &dwop_section_names;
12115 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12116
12117 /* Record the ELF section number for later lookup: this is what the
12118 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12119 gdb_assert (elf_section_nr < dwp_file->num_sections);
12120 dwp_file->elf_sections[elf_section_nr] = sectp;
12121
12122 /* Look for specific sections that we need. */
12123 if (section_is_p (sectp->name, &names->str_dwo))
12124 {
12125 dwp_file->sections.str.s.section = sectp;
12126 dwp_file->sections.str.size = bfd_section_size (sectp);
12127 }
12128 else if (section_is_p (sectp->name, &names->cu_index))
12129 {
12130 dwp_file->sections.cu_index.s.section = sectp;
12131 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
12132 }
12133 else if (section_is_p (sectp->name, &names->tu_index))
12134 {
12135 dwp_file->sections.tu_index.s.section = sectp;
12136 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
12137 }
12138 }
12139
12140 /* This function is mapped across the sections and remembers the offset and
12141 size of each of the DWP version 2 debugging sections that we are interested
12142 in. This is split into a separate function because we don't know if we
12143 have version 1 or 2 until we parse the cu_index/tu_index sections. */
12144
12145 static void
12146 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12147 {
12148 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12149 const struct dwop_section_names *names = &dwop_section_names;
12150 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12151
12152 /* Record the ELF section number for later lookup: this is what the
12153 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12154 gdb_assert (elf_section_nr < dwp_file->num_sections);
12155 dwp_file->elf_sections[elf_section_nr] = sectp;
12156
12157 /* Look for specific sections that we need. */
12158 if (section_is_p (sectp->name, &names->abbrev_dwo))
12159 {
12160 dwp_file->sections.abbrev.s.section = sectp;
12161 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12162 }
12163 else if (section_is_p (sectp->name, &names->info_dwo))
12164 {
12165 dwp_file->sections.info.s.section = sectp;
12166 dwp_file->sections.info.size = bfd_section_size (sectp);
12167 }
12168 else if (section_is_p (sectp->name, &names->line_dwo))
12169 {
12170 dwp_file->sections.line.s.section = sectp;
12171 dwp_file->sections.line.size = bfd_section_size (sectp);
12172 }
12173 else if (section_is_p (sectp->name, &names->loc_dwo))
12174 {
12175 dwp_file->sections.loc.s.section = sectp;
12176 dwp_file->sections.loc.size = bfd_section_size (sectp);
12177 }
12178 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12179 {
12180 dwp_file->sections.macinfo.s.section = sectp;
12181 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
12182 }
12183 else if (section_is_p (sectp->name, &names->macro_dwo))
12184 {
12185 dwp_file->sections.macro.s.section = sectp;
12186 dwp_file->sections.macro.size = bfd_section_size (sectp);
12187 }
12188 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12189 {
12190 dwp_file->sections.str_offsets.s.section = sectp;
12191 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12192 }
12193 else if (section_is_p (sectp->name, &names->types_dwo))
12194 {
12195 dwp_file->sections.types.s.section = sectp;
12196 dwp_file->sections.types.size = bfd_section_size (sectp);
12197 }
12198 }
12199
12200 /* Hash function for dwp_file loaded CUs/TUs. */
12201
12202 static hashval_t
12203 hash_dwp_loaded_cutus (const void *item)
12204 {
12205 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
12206
12207 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12208 return dwo_unit->signature;
12209 }
12210
12211 /* Equality function for dwp_file loaded CUs/TUs. */
12212
12213 static int
12214 eq_dwp_loaded_cutus (const void *a, const void *b)
12215 {
12216 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12217 const struct dwo_unit *dub = (const struct dwo_unit *) b;
12218
12219 return dua->signature == dub->signature;
12220 }
12221
12222 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
12223
12224 static htab_up
12225 allocate_dwp_loaded_cutus_table ()
12226 {
12227 return htab_up (htab_create_alloc (3,
12228 hash_dwp_loaded_cutus,
12229 eq_dwp_loaded_cutus,
12230 NULL, xcalloc, xfree));
12231 }
12232
12233 /* Try to open DWP file FILE_NAME.
12234 The result is the bfd handle of the file.
12235 If there is a problem finding or opening the file, return NULL.
12236 Upon success, the canonicalized path of the file is stored in the bfd,
12237 same as symfile_bfd_open. */
12238
12239 static gdb_bfd_ref_ptr
12240 open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12241 const char *file_name)
12242 {
12243 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
12244 1 /*is_dwp*/,
12245 1 /*search_cwd*/));
12246 if (abfd != NULL)
12247 return abfd;
12248
12249 /* Work around upstream bug 15652.
12250 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12251 [Whether that's a "bug" is debatable, but it is getting in our way.]
12252 We have no real idea where the dwp file is, because gdb's realpath-ing
12253 of the executable's path may have discarded the needed info.
12254 [IWBN if the dwp file name was recorded in the executable, akin to
12255 .gnu_debuglink, but that doesn't exist yet.]
12256 Strip the directory from FILE_NAME and search again. */
12257 if (*debug_file_directory != '\0')
12258 {
12259 /* Don't implicitly search the current directory here.
12260 If the user wants to search "." to handle this case,
12261 it must be added to debug-file-directory. */
12262 return try_open_dwop_file (dwarf2_per_objfile,
12263 lbasename (file_name), 1 /*is_dwp*/,
12264 0 /*search_cwd*/);
12265 }
12266
12267 return NULL;
12268 }
12269
12270 /* Initialize the use of the DWP file for the current objfile.
12271 By convention the name of the DWP file is ${objfile}.dwp.
12272 The result is NULL if it can't be found. */
12273
12274 static std::unique_ptr<struct dwp_file>
12275 open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
12276 {
12277 struct objfile *objfile = dwarf2_per_objfile->objfile;
12278
12279 /* Try to find first .dwp for the binary file before any symbolic links
12280 resolving. */
12281
12282 /* If the objfile is a debug file, find the name of the real binary
12283 file and get the name of dwp file from there. */
12284 std::string dwp_name;
12285 if (objfile->separate_debug_objfile_backlink != NULL)
12286 {
12287 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12288 const char *backlink_basename = lbasename (backlink->original_name);
12289
12290 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
12291 }
12292 else
12293 dwp_name = objfile->original_name;
12294
12295 dwp_name += ".dwp";
12296
12297 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
12298 if (dbfd == NULL
12299 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12300 {
12301 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
12302 dwp_name = objfile_name (objfile);
12303 dwp_name += ".dwp";
12304 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
12305 }
12306
12307 if (dbfd == NULL)
12308 {
12309 if (dwarf_read_debug)
12310 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
12311 return std::unique_ptr<dwp_file> ();
12312 }
12313
12314 const char *name = bfd_get_filename (dbfd.get ());
12315 std::unique_ptr<struct dwp_file> dwp_file
12316 (new struct dwp_file (name, std::move (dbfd)));
12317
12318 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
12319 dwp_file->elf_sections =
12320 OBSTACK_CALLOC (&objfile->objfile_obstack,
12321 dwp_file->num_sections, asection *);
12322
12323 bfd_map_over_sections (dwp_file->dbfd.get (),
12324 dwarf2_locate_common_dwp_sections,
12325 dwp_file.get ());
12326
12327 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12328 0);
12329
12330 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12331 1);
12332
12333 /* The DWP file version is stored in the hash table. Oh well. */
12334 if (dwp_file->cus && dwp_file->tus
12335 && dwp_file->cus->version != dwp_file->tus->version)
12336 {
12337 /* Technically speaking, we should try to limp along, but this is
12338 pretty bizarre. We use pulongest here because that's the established
12339 portability solution (e.g, we cannot use %u for uint32_t). */
12340 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12341 " TU version %s [in DWP file %s]"),
12342 pulongest (dwp_file->cus->version),
12343 pulongest (dwp_file->tus->version), dwp_name.c_str ());
12344 }
12345
12346 if (dwp_file->cus)
12347 dwp_file->version = dwp_file->cus->version;
12348 else if (dwp_file->tus)
12349 dwp_file->version = dwp_file->tus->version;
12350 else
12351 dwp_file->version = 2;
12352
12353 if (dwp_file->version == 2)
12354 bfd_map_over_sections (dwp_file->dbfd.get (),
12355 dwarf2_locate_v2_dwp_sections,
12356 dwp_file.get ());
12357
12358 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
12359 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
12360
12361 if (dwarf_read_debug)
12362 {
12363 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
12364 fprintf_unfiltered (gdb_stdlog,
12365 " %s CUs, %s TUs\n",
12366 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12367 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
12368 }
12369
12370 return dwp_file;
12371 }
12372
12373 /* Wrapper around open_and_init_dwp_file, only open it once. */
12374
12375 static struct dwp_file *
12376 get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
12377 {
12378 if (! dwarf2_per_objfile->dwp_checked)
12379 {
12380 dwarf2_per_objfile->dwp_file
12381 = open_and_init_dwp_file (dwarf2_per_objfile);
12382 dwarf2_per_objfile->dwp_checked = 1;
12383 }
12384 return dwarf2_per_objfile->dwp_file.get ();
12385 }
12386
12387 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12388 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12389 or in the DWP file for the objfile, referenced by THIS_UNIT.
12390 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
12391 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12392
12393 This is called, for example, when wanting to read a variable with a
12394 complex location. Therefore we don't want to do file i/o for every call.
12395 Therefore we don't want to look for a DWO file on every call.
12396 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12397 then we check if we've already seen DWO_NAME, and only THEN do we check
12398 for a DWO file.
12399
12400 The result is a pointer to the dwo_unit object or NULL if we didn't find it
12401 (dwo_id mismatch or couldn't find the DWO/DWP file). */
12402
12403 static struct dwo_unit *
12404 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
12405 const char *dwo_name, const char *comp_dir,
12406 ULONGEST signature, int is_debug_types)
12407 {
12408 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
12409 struct objfile *objfile = dwarf2_per_objfile->objfile;
12410 const char *kind = is_debug_types ? "TU" : "CU";
12411 void **dwo_file_slot;
12412 struct dwo_file *dwo_file;
12413 struct dwp_file *dwp_file;
12414
12415 /* First see if there's a DWP file.
12416 If we have a DWP file but didn't find the DWO inside it, don't
12417 look for the original DWO file. It makes gdb behave differently
12418 depending on whether one is debugging in the build tree. */
12419
12420 dwp_file = get_dwp_file (dwarf2_per_objfile);
12421 if (dwp_file != NULL)
12422 {
12423 const struct dwp_hash_table *dwp_htab =
12424 is_debug_types ? dwp_file->tus : dwp_file->cus;
12425
12426 if (dwp_htab != NULL)
12427 {
12428 struct dwo_unit *dwo_cutu =
12429 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
12430 signature, is_debug_types);
12431
12432 if (dwo_cutu != NULL)
12433 {
12434 if (dwarf_read_debug)
12435 {
12436 fprintf_unfiltered (gdb_stdlog,
12437 "Virtual DWO %s %s found: @%s\n",
12438 kind, hex_string (signature),
12439 host_address_to_string (dwo_cutu));
12440 }
12441 return dwo_cutu;
12442 }
12443 }
12444 }
12445 else
12446 {
12447 /* No DWP file, look for the DWO file. */
12448
12449 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12450 dwo_name, comp_dir);
12451 if (*dwo_file_slot == NULL)
12452 {
12453 /* Read in the file and build a table of the CUs/TUs it contains. */
12454 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
12455 }
12456 /* NOTE: This will be NULL if unable to open the file. */
12457 dwo_file = (struct dwo_file *) *dwo_file_slot;
12458
12459 if (dwo_file != NULL)
12460 {
12461 struct dwo_unit *dwo_cutu = NULL;
12462
12463 if (is_debug_types && dwo_file->tus)
12464 {
12465 struct dwo_unit find_dwo_cutu;
12466
12467 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12468 find_dwo_cutu.signature = signature;
12469 dwo_cutu
12470 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12471 &find_dwo_cutu);
12472 }
12473 else if (!is_debug_types && dwo_file->cus)
12474 {
12475 struct dwo_unit find_dwo_cutu;
12476
12477 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12478 find_dwo_cutu.signature = signature;
12479 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
12480 &find_dwo_cutu);
12481 }
12482
12483 if (dwo_cutu != NULL)
12484 {
12485 if (dwarf_read_debug)
12486 {
12487 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
12488 kind, dwo_name, hex_string (signature),
12489 host_address_to_string (dwo_cutu));
12490 }
12491 return dwo_cutu;
12492 }
12493 }
12494 }
12495
12496 /* We didn't find it. This could mean a dwo_id mismatch, or
12497 someone deleted the DWO/DWP file, or the search path isn't set up
12498 correctly to find the file. */
12499
12500 if (dwarf_read_debug)
12501 {
12502 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
12503 kind, dwo_name, hex_string (signature));
12504 }
12505
12506 /* This is a warning and not a complaint because it can be caused by
12507 pilot error (e.g., user accidentally deleting the DWO). */
12508 {
12509 /* Print the name of the DWP file if we looked there, helps the user
12510 better diagnose the problem. */
12511 std::string dwp_text;
12512
12513 if (dwp_file != NULL)
12514 dwp_text = string_printf (" [in DWP file %s]",
12515 lbasename (dwp_file->name));
12516
12517 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
12518 " [in module %s]"),
12519 kind, dwo_name, hex_string (signature),
12520 dwp_text.c_str (),
12521 this_unit->is_debug_types ? "TU" : "CU",
12522 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
12523 }
12524 return NULL;
12525 }
12526
12527 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12528 See lookup_dwo_cutu_unit for details. */
12529
12530 static struct dwo_unit *
12531 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
12532 const char *dwo_name, const char *comp_dir,
12533 ULONGEST signature)
12534 {
12535 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
12536 }
12537
12538 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12539 See lookup_dwo_cutu_unit for details. */
12540
12541 static struct dwo_unit *
12542 lookup_dwo_type_unit (struct signatured_type *this_tu,
12543 const char *dwo_name, const char *comp_dir)
12544 {
12545 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
12546 }
12547
12548 /* Traversal function for queue_and_load_all_dwo_tus. */
12549
12550 static int
12551 queue_and_load_dwo_tu (void **slot, void *info)
12552 {
12553 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
12554 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
12555 ULONGEST signature = dwo_unit->signature;
12556 struct signatured_type *sig_type =
12557 lookup_dwo_signatured_type (per_cu->cu, signature);
12558
12559 if (sig_type != NULL)
12560 {
12561 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
12562
12563 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12564 a real dependency of PER_CU on SIG_TYPE. That is detected later
12565 while processing PER_CU. */
12566 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
12567 load_full_type_unit (sig_cu);
12568 per_cu->imported_symtabs_push (sig_cu);
12569 }
12570
12571 return 1;
12572 }
12573
12574 /* Queue all TUs contained in the DWO of PER_CU to be read in.
12575 The DWO may have the only definition of the type, though it may not be
12576 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12577 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12578
12579 static void
12580 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
12581 {
12582 struct dwo_unit *dwo_unit;
12583 struct dwo_file *dwo_file;
12584
12585 gdb_assert (!per_cu->is_debug_types);
12586 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
12587 gdb_assert (per_cu->cu != NULL);
12588
12589 dwo_unit = per_cu->cu->dwo_unit;
12590 gdb_assert (dwo_unit != NULL);
12591
12592 dwo_file = dwo_unit->dwo_file;
12593 if (dwo_file->tus != NULL)
12594 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu,
12595 per_cu);
12596 }
12597
12598 /* Read in various DIEs. */
12599
12600 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
12601 Inherit only the children of the DW_AT_abstract_origin DIE not being
12602 already referenced by DW_AT_abstract_origin from the children of the
12603 current DIE. */
12604
12605 static void
12606 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
12607 {
12608 struct die_info *child_die;
12609 sect_offset *offsetp;
12610 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
12611 struct die_info *origin_die;
12612 /* Iterator of the ORIGIN_DIE children. */
12613 struct die_info *origin_child_die;
12614 struct attribute *attr;
12615 struct dwarf2_cu *origin_cu;
12616 struct pending **origin_previous_list_in_scope;
12617
12618 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12619 if (!attr)
12620 return;
12621
12622 /* Note that following die references may follow to a die in a
12623 different cu. */
12624
12625 origin_cu = cu;
12626 origin_die = follow_die_ref (die, attr, &origin_cu);
12627
12628 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
12629 symbols in. */
12630 origin_previous_list_in_scope = origin_cu->list_in_scope;
12631 origin_cu->list_in_scope = cu->list_in_scope;
12632
12633 if (die->tag != origin_die->tag
12634 && !(die->tag == DW_TAG_inlined_subroutine
12635 && origin_die->tag == DW_TAG_subprogram))
12636 complaint (_("DIE %s and its abstract origin %s have different tags"),
12637 sect_offset_str (die->sect_off),
12638 sect_offset_str (origin_die->sect_off));
12639
12640 std::vector<sect_offset> offsets;
12641
12642 for (child_die = die->child;
12643 child_die && child_die->tag;
12644 child_die = child_die->sibling)
12645 {
12646 struct die_info *child_origin_die;
12647 struct dwarf2_cu *child_origin_cu;
12648
12649 /* We are trying to process concrete instance entries:
12650 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
12651 it's not relevant to our analysis here. i.e. detecting DIEs that are
12652 present in the abstract instance but not referenced in the concrete
12653 one. */
12654 if (child_die->tag == DW_TAG_call_site
12655 || child_die->tag == DW_TAG_GNU_call_site)
12656 continue;
12657
12658 /* For each CHILD_DIE, find the corresponding child of
12659 ORIGIN_DIE. If there is more than one layer of
12660 DW_AT_abstract_origin, follow them all; there shouldn't be,
12661 but GCC versions at least through 4.4 generate this (GCC PR
12662 40573). */
12663 child_origin_die = child_die;
12664 child_origin_cu = cu;
12665 while (1)
12666 {
12667 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
12668 child_origin_cu);
12669 if (attr == NULL)
12670 break;
12671 child_origin_die = follow_die_ref (child_origin_die, attr,
12672 &child_origin_cu);
12673 }
12674
12675 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
12676 counterpart may exist. */
12677 if (child_origin_die != child_die)
12678 {
12679 if (child_die->tag != child_origin_die->tag
12680 && !(child_die->tag == DW_TAG_inlined_subroutine
12681 && child_origin_die->tag == DW_TAG_subprogram))
12682 complaint (_("Child DIE %s and its abstract origin %s have "
12683 "different tags"),
12684 sect_offset_str (child_die->sect_off),
12685 sect_offset_str (child_origin_die->sect_off));
12686 if (child_origin_die->parent != origin_die)
12687 complaint (_("Child DIE %s and its abstract origin %s have "
12688 "different parents"),
12689 sect_offset_str (child_die->sect_off),
12690 sect_offset_str (child_origin_die->sect_off));
12691 else
12692 offsets.push_back (child_origin_die->sect_off);
12693 }
12694 }
12695 std::sort (offsets.begin (), offsets.end ());
12696 sect_offset *offsets_end = offsets.data () + offsets.size ();
12697 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
12698 if (offsetp[-1] == *offsetp)
12699 complaint (_("Multiple children of DIE %s refer "
12700 "to DIE %s as their abstract origin"),
12701 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
12702
12703 offsetp = offsets.data ();
12704 origin_child_die = origin_die->child;
12705 while (origin_child_die && origin_child_die->tag)
12706 {
12707 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
12708 while (offsetp < offsets_end
12709 && *offsetp < origin_child_die->sect_off)
12710 offsetp++;
12711 if (offsetp >= offsets_end
12712 || *offsetp > origin_child_die->sect_off)
12713 {
12714 /* Found that ORIGIN_CHILD_DIE is really not referenced.
12715 Check whether we're already processing ORIGIN_CHILD_DIE.
12716 This can happen with mutually referenced abstract_origins.
12717 PR 16581. */
12718 if (!origin_child_die->in_process)
12719 process_die (origin_child_die, origin_cu);
12720 }
12721 origin_child_die = origin_child_die->sibling;
12722 }
12723 origin_cu->list_in_scope = origin_previous_list_in_scope;
12724
12725 if (cu != origin_cu)
12726 compute_delayed_physnames (origin_cu);
12727 }
12728
12729 static void
12730 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
12731 {
12732 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
12733 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12734 struct context_stack *newobj;
12735 CORE_ADDR lowpc;
12736 CORE_ADDR highpc;
12737 struct die_info *child_die;
12738 struct attribute *attr, *call_line, *call_file;
12739 const char *name;
12740 CORE_ADDR baseaddr;
12741 struct block *block;
12742 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
12743 std::vector<struct symbol *> template_args;
12744 struct template_symbol *templ_func = NULL;
12745
12746 if (inlined_func)
12747 {
12748 /* If we do not have call site information, we can't show the
12749 caller of this inlined function. That's too confusing, so
12750 only use the scope for local variables. */
12751 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
12752 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
12753 if (call_line == NULL || call_file == NULL)
12754 {
12755 read_lexical_block_scope (die, cu);
12756 return;
12757 }
12758 }
12759
12760 baseaddr = objfile->text_section_offset ();
12761
12762 name = dwarf2_name (die, cu);
12763
12764 /* Ignore functions with missing or empty names. These are actually
12765 illegal according to the DWARF standard. */
12766 if (name == NULL)
12767 {
12768 complaint (_("missing name for subprogram DIE at %s"),
12769 sect_offset_str (die->sect_off));
12770 return;
12771 }
12772
12773 /* Ignore functions with missing or invalid low and high pc attributes. */
12774 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
12775 <= PC_BOUNDS_INVALID)
12776 {
12777 attr = dwarf2_attr (die, DW_AT_external, cu);
12778 if (!attr || !DW_UNSND (attr))
12779 complaint (_("cannot get low and high bounds "
12780 "for subprogram DIE at %s"),
12781 sect_offset_str (die->sect_off));
12782 return;
12783 }
12784
12785 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12786 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
12787
12788 /* If we have any template arguments, then we must allocate a
12789 different sort of symbol. */
12790 for (child_die = die->child; child_die; child_die = child_die->sibling)
12791 {
12792 if (child_die->tag == DW_TAG_template_type_param
12793 || child_die->tag == DW_TAG_template_value_param)
12794 {
12795 templ_func = allocate_template_symbol (objfile);
12796 templ_func->subclass = SYMBOL_TEMPLATE;
12797 break;
12798 }
12799 }
12800
12801 newobj = cu->get_builder ()->push_context (0, lowpc);
12802 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
12803 (struct symbol *) templ_func);
12804
12805 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
12806 set_objfile_main_name (objfile, newobj->name->linkage_name (),
12807 cu->language);
12808
12809 /* If there is a location expression for DW_AT_frame_base, record
12810 it. */
12811 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
12812 if (attr != nullptr)
12813 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
12814
12815 /* If there is a location for the static link, record it. */
12816 newobj->static_link = NULL;
12817 attr = dwarf2_attr (die, DW_AT_static_link, cu);
12818 if (attr != nullptr)
12819 {
12820 newobj->static_link
12821 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
12822 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
12823 cu->per_cu->addr_type ());
12824 }
12825
12826 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
12827
12828 if (die->child != NULL)
12829 {
12830 child_die = die->child;
12831 while (child_die && child_die->tag)
12832 {
12833 if (child_die->tag == DW_TAG_template_type_param
12834 || child_die->tag == DW_TAG_template_value_param)
12835 {
12836 struct symbol *arg = new_symbol (child_die, NULL, cu);
12837
12838 if (arg != NULL)
12839 template_args.push_back (arg);
12840 }
12841 else
12842 process_die (child_die, cu);
12843 child_die = child_die->sibling;
12844 }
12845 }
12846
12847 inherit_abstract_dies (die, cu);
12848
12849 /* If we have a DW_AT_specification, we might need to import using
12850 directives from the context of the specification DIE. See the
12851 comment in determine_prefix. */
12852 if (cu->language == language_cplus
12853 && dwarf2_attr (die, DW_AT_specification, cu))
12854 {
12855 struct dwarf2_cu *spec_cu = cu;
12856 struct die_info *spec_die = die_specification (die, &spec_cu);
12857
12858 while (spec_die)
12859 {
12860 child_die = spec_die->child;
12861 while (child_die && child_die->tag)
12862 {
12863 if (child_die->tag == DW_TAG_imported_module)
12864 process_die (child_die, spec_cu);
12865 child_die = child_die->sibling;
12866 }
12867
12868 /* In some cases, GCC generates specification DIEs that
12869 themselves contain DW_AT_specification attributes. */
12870 spec_die = die_specification (spec_die, &spec_cu);
12871 }
12872 }
12873
12874 struct context_stack cstk = cu->get_builder ()->pop_context ();
12875 /* Make a block for the local symbols within. */
12876 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
12877 cstk.static_link, lowpc, highpc);
12878
12879 /* For C++, set the block's scope. */
12880 if ((cu->language == language_cplus
12881 || cu->language == language_fortran
12882 || cu->language == language_d
12883 || cu->language == language_rust)
12884 && cu->processing_has_namespace_info)
12885 block_set_scope (block, determine_prefix (die, cu),
12886 &objfile->objfile_obstack);
12887
12888 /* If we have address ranges, record them. */
12889 dwarf2_record_block_ranges (die, block, baseaddr, cu);
12890
12891 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
12892
12893 /* Attach template arguments to function. */
12894 if (!template_args.empty ())
12895 {
12896 gdb_assert (templ_func != NULL);
12897
12898 templ_func->n_template_arguments = template_args.size ();
12899 templ_func->template_arguments
12900 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
12901 templ_func->n_template_arguments);
12902 memcpy (templ_func->template_arguments,
12903 template_args.data (),
12904 (templ_func->n_template_arguments * sizeof (struct symbol *)));
12905
12906 /* Make sure that the symtab is set on the new symbols. Even
12907 though they don't appear in this symtab directly, other parts
12908 of gdb assume that symbols do, and this is reasonably
12909 true. */
12910 for (symbol *sym : template_args)
12911 symbol_set_symtab (sym, symbol_symtab (templ_func));
12912 }
12913
12914 /* In C++, we can have functions nested inside functions (e.g., when
12915 a function declares a class that has methods). This means that
12916 when we finish processing a function scope, we may need to go
12917 back to building a containing block's symbol lists. */
12918 *cu->get_builder ()->get_local_symbols () = cstk.locals;
12919 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
12920
12921 /* If we've finished processing a top-level function, subsequent
12922 symbols go in the file symbol list. */
12923 if (cu->get_builder ()->outermost_context_p ())
12924 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
12925 }
12926
12927 /* Process all the DIES contained within a lexical block scope. Start
12928 a new scope, process the dies, and then close the scope. */
12929
12930 static void
12931 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
12932 {
12933 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
12934 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12935 CORE_ADDR lowpc, highpc;
12936 struct die_info *child_die;
12937 CORE_ADDR baseaddr;
12938
12939 baseaddr = objfile->text_section_offset ();
12940
12941 /* Ignore blocks with missing or invalid low and high pc attributes. */
12942 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
12943 as multiple lexical blocks? Handling children in a sane way would
12944 be nasty. Might be easier to properly extend generic blocks to
12945 describe ranges. */
12946 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
12947 {
12948 case PC_BOUNDS_NOT_PRESENT:
12949 /* DW_TAG_lexical_block has no attributes, process its children as if
12950 there was no wrapping by that DW_TAG_lexical_block.
12951 GCC does no longer produces such DWARF since GCC r224161. */
12952 for (child_die = die->child;
12953 child_die != NULL && child_die->tag;
12954 child_die = child_die->sibling)
12955 process_die (child_die, cu);
12956 return;
12957 case PC_BOUNDS_INVALID:
12958 return;
12959 }
12960 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12961 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
12962
12963 cu->get_builder ()->push_context (0, lowpc);
12964 if (die->child != NULL)
12965 {
12966 child_die = die->child;
12967 while (child_die && child_die->tag)
12968 {
12969 process_die (child_die, cu);
12970 child_die = child_die->sibling;
12971 }
12972 }
12973 inherit_abstract_dies (die, cu);
12974 struct context_stack cstk = cu->get_builder ()->pop_context ();
12975
12976 if (*cu->get_builder ()->get_local_symbols () != NULL
12977 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
12978 {
12979 struct block *block
12980 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
12981 cstk.start_addr, highpc);
12982
12983 /* Note that recording ranges after traversing children, as we
12984 do here, means that recording a parent's ranges entails
12985 walking across all its children's ranges as they appear in
12986 the address map, which is quadratic behavior.
12987
12988 It would be nicer to record the parent's ranges before
12989 traversing its children, simply overriding whatever you find
12990 there. But since we don't even decide whether to create a
12991 block until after we've traversed its children, that's hard
12992 to do. */
12993 dwarf2_record_block_ranges (die, block, baseaddr, cu);
12994 }
12995 *cu->get_builder ()->get_local_symbols () = cstk.locals;
12996 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
12997 }
12998
12999 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13000
13001 static void
13002 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13003 {
13004 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13005 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13006 CORE_ADDR pc, baseaddr;
13007 struct attribute *attr;
13008 struct call_site *call_site, call_site_local;
13009 void **slot;
13010 int nparams;
13011 struct die_info *child_die;
13012
13013 baseaddr = objfile->text_section_offset ();
13014
13015 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13016 if (attr == NULL)
13017 {
13018 /* This was a pre-DWARF-5 GNU extension alias
13019 for DW_AT_call_return_pc. */
13020 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13021 }
13022 if (!attr)
13023 {
13024 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13025 "DIE %s [in module %s]"),
13026 sect_offset_str (die->sect_off), objfile_name (objfile));
13027 return;
13028 }
13029 pc = attr->value_as_address () + baseaddr;
13030 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13031
13032 if (cu->call_site_htab == NULL)
13033 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13034 NULL, &objfile->objfile_obstack,
13035 hashtab_obstack_allocate, NULL);
13036 call_site_local.pc = pc;
13037 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13038 if (*slot != NULL)
13039 {
13040 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13041 "DIE %s [in module %s]"),
13042 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13043 objfile_name (objfile));
13044 return;
13045 }
13046
13047 /* Count parameters at the caller. */
13048
13049 nparams = 0;
13050 for (child_die = die->child; child_die && child_die->tag;
13051 child_die = child_die->sibling)
13052 {
13053 if (child_die->tag != DW_TAG_call_site_parameter
13054 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13055 {
13056 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
13057 "DW_TAG_call_site child DIE %s [in module %s]"),
13058 child_die->tag, sect_offset_str (child_die->sect_off),
13059 objfile_name (objfile));
13060 continue;
13061 }
13062
13063 nparams++;
13064 }
13065
13066 call_site
13067 = ((struct call_site *)
13068 obstack_alloc (&objfile->objfile_obstack,
13069 sizeof (*call_site)
13070 + (sizeof (*call_site->parameter) * (nparams - 1))));
13071 *slot = call_site;
13072 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13073 call_site->pc = pc;
13074
13075 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13076 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
13077 {
13078 struct die_info *func_die;
13079
13080 /* Skip also over DW_TAG_inlined_subroutine. */
13081 for (func_die = die->parent;
13082 func_die && func_die->tag != DW_TAG_subprogram
13083 && func_die->tag != DW_TAG_subroutine_type;
13084 func_die = func_die->parent);
13085
13086 /* DW_AT_call_all_calls is a superset
13087 of DW_AT_call_all_tail_calls. */
13088 if (func_die
13089 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13090 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
13091 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
13092 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13093 {
13094 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13095 not complete. But keep CALL_SITE for look ups via call_site_htab,
13096 both the initial caller containing the real return address PC and
13097 the final callee containing the current PC of a chain of tail
13098 calls do not need to have the tail call list complete. But any
13099 function candidate for a virtual tail call frame searched via
13100 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13101 determined unambiguously. */
13102 }
13103 else
13104 {
13105 struct type *func_type = NULL;
13106
13107 if (func_die)
13108 func_type = get_die_type (func_die, cu);
13109 if (func_type != NULL)
13110 {
13111 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
13112
13113 /* Enlist this call site to the function. */
13114 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13115 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13116 }
13117 else
13118 complaint (_("Cannot find function owning DW_TAG_call_site "
13119 "DIE %s [in module %s]"),
13120 sect_offset_str (die->sect_off), objfile_name (objfile));
13121 }
13122 }
13123
13124 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13125 if (attr == NULL)
13126 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13127 if (attr == NULL)
13128 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
13129 if (attr == NULL)
13130 {
13131 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13132 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13133 }
13134 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
13135 if (!attr || (attr->form_is_block () && DW_BLOCK (attr)->size == 0))
13136 /* Keep NULL DWARF_BLOCK. */;
13137 else if (attr->form_is_block ())
13138 {
13139 struct dwarf2_locexpr_baton *dlbaton;
13140
13141 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
13142 dlbaton->data = DW_BLOCK (attr)->data;
13143 dlbaton->size = DW_BLOCK (attr)->size;
13144 dlbaton->per_cu = cu->per_cu;
13145
13146 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13147 }
13148 else if (attr->form_is_ref ())
13149 {
13150 struct dwarf2_cu *target_cu = cu;
13151 struct die_info *target_die;
13152
13153 target_die = follow_die_ref (die, attr, &target_cu);
13154 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
13155 if (die_is_declaration (target_die, target_cu))
13156 {
13157 const char *target_physname;
13158
13159 /* Prefer the mangled name; otherwise compute the demangled one. */
13160 target_physname = dw2_linkage_name (target_die, target_cu);
13161 if (target_physname == NULL)
13162 target_physname = dwarf2_physname (NULL, target_die, target_cu);
13163 if (target_physname == NULL)
13164 complaint (_("DW_AT_call_target target DIE has invalid "
13165 "physname, for referencing DIE %s [in module %s]"),
13166 sect_offset_str (die->sect_off), objfile_name (objfile));
13167 else
13168 SET_FIELD_PHYSNAME (call_site->target, target_physname);
13169 }
13170 else
13171 {
13172 CORE_ADDR lowpc;
13173
13174 /* DW_AT_entry_pc should be preferred. */
13175 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
13176 <= PC_BOUNDS_INVALID)
13177 complaint (_("DW_AT_call_target target DIE has invalid "
13178 "low pc, for referencing DIE %s [in module %s]"),
13179 sect_offset_str (die->sect_off), objfile_name (objfile));
13180 else
13181 {
13182 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13183 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13184 }
13185 }
13186 }
13187 else
13188 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
13189 "block nor reference, for DIE %s [in module %s]"),
13190 sect_offset_str (die->sect_off), objfile_name (objfile));
13191
13192 call_site->per_cu = cu->per_cu;
13193
13194 for (child_die = die->child;
13195 child_die && child_die->tag;
13196 child_die = child_die->sibling)
13197 {
13198 struct call_site_parameter *parameter;
13199 struct attribute *loc, *origin;
13200
13201 if (child_die->tag != DW_TAG_call_site_parameter
13202 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13203 {
13204 /* Already printed the complaint above. */
13205 continue;
13206 }
13207
13208 gdb_assert (call_site->parameter_count < nparams);
13209 parameter = &call_site->parameter[call_site->parameter_count];
13210
13211 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13212 specifies DW_TAG_formal_parameter. Value of the data assumed for the
13213 register is contained in DW_AT_call_value. */
13214
13215 loc = dwarf2_attr (child_die, DW_AT_location, cu);
13216 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13217 if (origin == NULL)
13218 {
13219 /* This was a pre-DWARF-5 GNU extension alias
13220 for DW_AT_call_parameter. */
13221 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13222 }
13223 if (loc == NULL && origin != NULL && origin->form_is_ref ())
13224 {
13225 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
13226
13227 sect_offset sect_off
13228 = (sect_offset) dwarf2_get_ref_die_offset (origin);
13229 if (!cu->header.offset_in_cu_p (sect_off))
13230 {
13231 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13232 binding can be done only inside one CU. Such referenced DIE
13233 therefore cannot be even moved to DW_TAG_partial_unit. */
13234 complaint (_("DW_AT_call_parameter offset is not in CU for "
13235 "DW_TAG_call_site child DIE %s [in module %s]"),
13236 sect_offset_str (child_die->sect_off),
13237 objfile_name (objfile));
13238 continue;
13239 }
13240 parameter->u.param_cu_off
13241 = (cu_offset) (sect_off - cu->header.sect_off);
13242 }
13243 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
13244 {
13245 complaint (_("No DW_FORM_block* DW_AT_location for "
13246 "DW_TAG_call_site child DIE %s [in module %s]"),
13247 sect_offset_str (child_die->sect_off), objfile_name (objfile));
13248 continue;
13249 }
13250 else
13251 {
13252 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13253 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
13254 if (parameter->u.dwarf_reg != -1)
13255 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13256 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
13257 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
13258 &parameter->u.fb_offset))
13259 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13260 else
13261 {
13262 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
13263 "for DW_FORM_block* DW_AT_location is supported for "
13264 "DW_TAG_call_site child DIE %s "
13265 "[in module %s]"),
13266 sect_offset_str (child_die->sect_off),
13267 objfile_name (objfile));
13268 continue;
13269 }
13270 }
13271
13272 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13273 if (attr == NULL)
13274 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
13275 if (attr == NULL || !attr->form_is_block ())
13276 {
13277 complaint (_("No DW_FORM_block* DW_AT_call_value for "
13278 "DW_TAG_call_site child DIE %s [in module %s]"),
13279 sect_offset_str (child_die->sect_off),
13280 objfile_name (objfile));
13281 continue;
13282 }
13283 parameter->value = DW_BLOCK (attr)->data;
13284 parameter->value_size = DW_BLOCK (attr)->size;
13285
13286 /* Parameters are not pre-cleared by memset above. */
13287 parameter->data_value = NULL;
13288 parameter->data_value_size = 0;
13289 call_site->parameter_count++;
13290
13291 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13292 if (attr == NULL)
13293 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
13294 if (attr != nullptr)
13295 {
13296 if (!attr->form_is_block ())
13297 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
13298 "DW_TAG_call_site child DIE %s [in module %s]"),
13299 sect_offset_str (child_die->sect_off),
13300 objfile_name (objfile));
13301 else
13302 {
13303 parameter->data_value = DW_BLOCK (attr)->data;
13304 parameter->data_value_size = DW_BLOCK (attr)->size;
13305 }
13306 }
13307 }
13308 }
13309
13310 /* Helper function for read_variable. If DIE represents a virtual
13311 table, then return the type of the concrete object that is
13312 associated with the virtual table. Otherwise, return NULL. */
13313
13314 static struct type *
13315 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13316 {
13317 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13318 if (attr == NULL)
13319 return NULL;
13320
13321 /* Find the type DIE. */
13322 struct die_info *type_die = NULL;
13323 struct dwarf2_cu *type_cu = cu;
13324
13325 if (attr->form_is_ref ())
13326 type_die = follow_die_ref (die, attr, &type_cu);
13327 if (type_die == NULL)
13328 return NULL;
13329
13330 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13331 return NULL;
13332 return die_containing_type (type_die, type_cu);
13333 }
13334
13335 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13336
13337 static void
13338 read_variable (struct die_info *die, struct dwarf2_cu *cu)
13339 {
13340 struct rust_vtable_symbol *storage = NULL;
13341
13342 if (cu->language == language_rust)
13343 {
13344 struct type *containing_type = rust_containing_type (die, cu);
13345
13346 if (containing_type != NULL)
13347 {
13348 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13349
13350 storage = new (&objfile->objfile_obstack) rust_vtable_symbol ();
13351 initialize_objfile_symbol (storage);
13352 storage->concrete_type = containing_type;
13353 storage->subclass = SYMBOL_RUST_VTABLE;
13354 }
13355 }
13356
13357 struct symbol *res = new_symbol (die, NULL, cu, storage);
13358 struct attribute *abstract_origin
13359 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13360 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13361 if (res == NULL && loc && abstract_origin)
13362 {
13363 /* We have a variable without a name, but with a location and an abstract
13364 origin. This may be a concrete instance of an abstract variable
13365 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13366 later. */
13367 struct dwarf2_cu *origin_cu = cu;
13368 struct die_info *origin_die
13369 = follow_die_ref (die, abstract_origin, &origin_cu);
13370 dwarf2_per_objfile *dpo = cu->per_cu->dwarf2_per_objfile;
13371 dpo->abstract_to_concrete[origin_die->sect_off].push_back (die->sect_off);
13372 }
13373 }
13374
13375 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13376 reading .debug_rnglists.
13377 Callback's type should be:
13378 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13379 Return true if the attributes are present and valid, otherwise,
13380 return false. */
13381
13382 template <typename Callback>
13383 static bool
13384 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13385 Callback &&callback)
13386 {
13387 struct dwarf2_per_objfile *dwarf2_per_objfile
13388 = cu->per_cu->dwarf2_per_objfile;
13389 struct objfile *objfile = dwarf2_per_objfile->objfile;
13390 bfd *obfd = objfile->obfd;
13391 /* Base address selection entry. */
13392 gdb::optional<CORE_ADDR> base;
13393 const gdb_byte *buffer;
13394 CORE_ADDR baseaddr;
13395 bool overflow = false;
13396
13397 base = cu->base_address;
13398
13399 dwarf2_per_objfile->rnglists.read (objfile);
13400 if (offset >= dwarf2_per_objfile->rnglists.size)
13401 {
13402 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13403 offset);
13404 return false;
13405 }
13406 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
13407
13408 baseaddr = objfile->text_section_offset ();
13409
13410 while (1)
13411 {
13412 /* Initialize it due to a false compiler warning. */
13413 CORE_ADDR range_beginning = 0, range_end = 0;
13414 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
13415 + dwarf2_per_objfile->rnglists.size);
13416 unsigned int bytes_read;
13417
13418 if (buffer == buf_end)
13419 {
13420 overflow = true;
13421 break;
13422 }
13423 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13424 switch (rlet)
13425 {
13426 case DW_RLE_end_of_list:
13427 break;
13428 case DW_RLE_base_address:
13429 if (buffer + cu->header.addr_size > buf_end)
13430 {
13431 overflow = true;
13432 break;
13433 }
13434 base = cu->header.read_address (obfd, buffer, &bytes_read);
13435 buffer += bytes_read;
13436 break;
13437 case DW_RLE_start_length:
13438 if (buffer + cu->header.addr_size > buf_end)
13439 {
13440 overflow = true;
13441 break;
13442 }
13443 range_beginning = cu->header.read_address (obfd, buffer,
13444 &bytes_read);
13445 buffer += bytes_read;
13446 range_end = (range_beginning
13447 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13448 buffer += bytes_read;
13449 if (buffer > buf_end)
13450 {
13451 overflow = true;
13452 break;
13453 }
13454 break;
13455 case DW_RLE_offset_pair:
13456 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13457 buffer += bytes_read;
13458 if (buffer > buf_end)
13459 {
13460 overflow = true;
13461 break;
13462 }
13463 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13464 buffer += bytes_read;
13465 if (buffer > buf_end)
13466 {
13467 overflow = true;
13468 break;
13469 }
13470 break;
13471 case DW_RLE_start_end:
13472 if (buffer + 2 * cu->header.addr_size > buf_end)
13473 {
13474 overflow = true;
13475 break;
13476 }
13477 range_beginning = cu->header.read_address (obfd, buffer,
13478 &bytes_read);
13479 buffer += bytes_read;
13480 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
13481 buffer += bytes_read;
13482 break;
13483 default:
13484 complaint (_("Invalid .debug_rnglists data (no base address)"));
13485 return false;
13486 }
13487 if (rlet == DW_RLE_end_of_list || overflow)
13488 break;
13489 if (rlet == DW_RLE_base_address)
13490 continue;
13491
13492 if (!base.has_value ())
13493 {
13494 /* We have no valid base address for the ranges
13495 data. */
13496 complaint (_("Invalid .debug_rnglists data (no base address)"));
13497 return false;
13498 }
13499
13500 if (range_beginning > range_end)
13501 {
13502 /* Inverted range entries are invalid. */
13503 complaint (_("Invalid .debug_rnglists data (inverted range)"));
13504 return false;
13505 }
13506
13507 /* Empty range entries have no effect. */
13508 if (range_beginning == range_end)
13509 continue;
13510
13511 range_beginning += *base;
13512 range_end += *base;
13513
13514 /* A not-uncommon case of bad debug info.
13515 Don't pollute the addrmap with bad data. */
13516 if (range_beginning + baseaddr == 0
13517 && !dwarf2_per_objfile->has_section_at_zero)
13518 {
13519 complaint (_(".debug_rnglists entry has start address of zero"
13520 " [in module %s]"), objfile_name (objfile));
13521 continue;
13522 }
13523
13524 callback (range_beginning, range_end);
13525 }
13526
13527 if (overflow)
13528 {
13529 complaint (_("Offset %d is not terminated "
13530 "for DW_AT_ranges attribute"),
13531 offset);
13532 return false;
13533 }
13534
13535 return true;
13536 }
13537
13538 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13539 Callback's type should be:
13540 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13541 Return 1 if the attributes are present and valid, otherwise, return 0. */
13542
13543 template <typename Callback>
13544 static int
13545 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
13546 Callback &&callback)
13547 {
13548 struct dwarf2_per_objfile *dwarf2_per_objfile
13549 = cu->per_cu->dwarf2_per_objfile;
13550 struct objfile *objfile = dwarf2_per_objfile->objfile;
13551 struct comp_unit_head *cu_header = &cu->header;
13552 bfd *obfd = objfile->obfd;
13553 unsigned int addr_size = cu_header->addr_size;
13554 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13555 /* Base address selection entry. */
13556 gdb::optional<CORE_ADDR> base;
13557 unsigned int dummy;
13558 const gdb_byte *buffer;
13559 CORE_ADDR baseaddr;
13560
13561 if (cu_header->version >= 5)
13562 return dwarf2_rnglists_process (offset, cu, callback);
13563
13564 base = cu->base_address;
13565
13566 dwarf2_per_objfile->ranges.read (objfile);
13567 if (offset >= dwarf2_per_objfile->ranges.size)
13568 {
13569 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13570 offset);
13571 return 0;
13572 }
13573 buffer = dwarf2_per_objfile->ranges.buffer + offset;
13574
13575 baseaddr = objfile->text_section_offset ();
13576
13577 while (1)
13578 {
13579 CORE_ADDR range_beginning, range_end;
13580
13581 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
13582 buffer += addr_size;
13583 range_end = cu->header.read_address (obfd, buffer, &dummy);
13584 buffer += addr_size;
13585 offset += 2 * addr_size;
13586
13587 /* An end of list marker is a pair of zero addresses. */
13588 if (range_beginning == 0 && range_end == 0)
13589 /* Found the end of list entry. */
13590 break;
13591
13592 /* Each base address selection entry is a pair of 2 values.
13593 The first is the largest possible address, the second is
13594 the base address. Check for a base address here. */
13595 if ((range_beginning & mask) == mask)
13596 {
13597 /* If we found the largest possible address, then we already
13598 have the base address in range_end. */
13599 base = range_end;
13600 continue;
13601 }
13602
13603 if (!base.has_value ())
13604 {
13605 /* We have no valid base address for the ranges
13606 data. */
13607 complaint (_("Invalid .debug_ranges data (no base address)"));
13608 return 0;
13609 }
13610
13611 if (range_beginning > range_end)
13612 {
13613 /* Inverted range entries are invalid. */
13614 complaint (_("Invalid .debug_ranges data (inverted range)"));
13615 return 0;
13616 }
13617
13618 /* Empty range entries have no effect. */
13619 if (range_beginning == range_end)
13620 continue;
13621
13622 range_beginning += *base;
13623 range_end += *base;
13624
13625 /* A not-uncommon case of bad debug info.
13626 Don't pollute the addrmap with bad data. */
13627 if (range_beginning + baseaddr == 0
13628 && !dwarf2_per_objfile->has_section_at_zero)
13629 {
13630 complaint (_(".debug_ranges entry has start address of zero"
13631 " [in module %s]"), objfile_name (objfile));
13632 continue;
13633 }
13634
13635 callback (range_beginning, range_end);
13636 }
13637
13638 return 1;
13639 }
13640
13641 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
13642 Return 1 if the attributes are present and valid, otherwise, return 0.
13643 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
13644
13645 static int
13646 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
13647 CORE_ADDR *high_return, struct dwarf2_cu *cu,
13648 dwarf2_psymtab *ranges_pst)
13649 {
13650 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13651 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13652 const CORE_ADDR baseaddr = objfile->text_section_offset ();
13653 int low_set = 0;
13654 CORE_ADDR low = 0;
13655 CORE_ADDR high = 0;
13656 int retval;
13657
13658 retval = dwarf2_ranges_process (offset, cu,
13659 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
13660 {
13661 if (ranges_pst != NULL)
13662 {
13663 CORE_ADDR lowpc;
13664 CORE_ADDR highpc;
13665
13666 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13667 range_beginning + baseaddr)
13668 - baseaddr);
13669 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13670 range_end + baseaddr)
13671 - baseaddr);
13672 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
13673 lowpc, highpc - 1, ranges_pst);
13674 }
13675
13676 /* FIXME: This is recording everything as a low-high
13677 segment of consecutive addresses. We should have a
13678 data structure for discontiguous block ranges
13679 instead. */
13680 if (! low_set)
13681 {
13682 low = range_beginning;
13683 high = range_end;
13684 low_set = 1;
13685 }
13686 else
13687 {
13688 if (range_beginning < low)
13689 low = range_beginning;
13690 if (range_end > high)
13691 high = range_end;
13692 }
13693 });
13694 if (!retval)
13695 return 0;
13696
13697 if (! low_set)
13698 /* If the first entry is an end-of-list marker, the range
13699 describes an empty scope, i.e. no instructions. */
13700 return 0;
13701
13702 if (low_return)
13703 *low_return = low;
13704 if (high_return)
13705 *high_return = high;
13706 return 1;
13707 }
13708
13709 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
13710 definition for the return value. *LOWPC and *HIGHPC are set iff
13711 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
13712
13713 static enum pc_bounds_kind
13714 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
13715 CORE_ADDR *highpc, struct dwarf2_cu *cu,
13716 dwarf2_psymtab *pst)
13717 {
13718 struct dwarf2_per_objfile *dwarf2_per_objfile
13719 = cu->per_cu->dwarf2_per_objfile;
13720 struct attribute *attr;
13721 struct attribute *attr_high;
13722 CORE_ADDR low = 0;
13723 CORE_ADDR high = 0;
13724 enum pc_bounds_kind ret;
13725
13726 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13727 if (attr_high)
13728 {
13729 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13730 if (attr != nullptr)
13731 {
13732 low = attr->value_as_address ();
13733 high = attr_high->value_as_address ();
13734 if (cu->header.version >= 4 && attr_high->form_is_constant ())
13735 high += low;
13736 }
13737 else
13738 /* Found high w/o low attribute. */
13739 return PC_BOUNDS_INVALID;
13740
13741 /* Found consecutive range of addresses. */
13742 ret = PC_BOUNDS_HIGH_LOW;
13743 }
13744 else
13745 {
13746 attr = dwarf2_attr (die, DW_AT_ranges, cu);
13747 if (attr != NULL)
13748 {
13749 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
13750 We take advantage of the fact that DW_AT_ranges does not appear
13751 in DW_TAG_compile_unit of DWO files. */
13752 int need_ranges_base = die->tag != DW_TAG_compile_unit;
13753 unsigned int ranges_offset = (DW_UNSND (attr)
13754 + (need_ranges_base
13755 ? cu->ranges_base
13756 : 0));
13757
13758 /* Value of the DW_AT_ranges attribute is the offset in the
13759 .debug_ranges section. */
13760 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
13761 return PC_BOUNDS_INVALID;
13762 /* Found discontinuous range of addresses. */
13763 ret = PC_BOUNDS_RANGES;
13764 }
13765 else
13766 return PC_BOUNDS_NOT_PRESENT;
13767 }
13768
13769 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
13770 if (high <= low)
13771 return PC_BOUNDS_INVALID;
13772
13773 /* When using the GNU linker, .gnu.linkonce. sections are used to
13774 eliminate duplicate copies of functions and vtables and such.
13775 The linker will arbitrarily choose one and discard the others.
13776 The AT_*_pc values for such functions refer to local labels in
13777 these sections. If the section from that file was discarded, the
13778 labels are not in the output, so the relocs get a value of 0.
13779 If this is a discarded function, mark the pc bounds as invalid,
13780 so that GDB will ignore it. */
13781 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
13782 return PC_BOUNDS_INVALID;
13783
13784 *lowpc = low;
13785 if (highpc)
13786 *highpc = high;
13787 return ret;
13788 }
13789
13790 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
13791 its low and high PC addresses. Do nothing if these addresses could not
13792 be determined. Otherwise, set LOWPC to the low address if it is smaller,
13793 and HIGHPC to the high address if greater than HIGHPC. */
13794
13795 static void
13796 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
13797 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13798 struct dwarf2_cu *cu)
13799 {
13800 CORE_ADDR low, high;
13801 struct die_info *child = die->child;
13802
13803 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
13804 {
13805 *lowpc = std::min (*lowpc, low);
13806 *highpc = std::max (*highpc, high);
13807 }
13808
13809 /* If the language does not allow nested subprograms (either inside
13810 subprograms or lexical blocks), we're done. */
13811 if (cu->language != language_ada)
13812 return;
13813
13814 /* Check all the children of the given DIE. If it contains nested
13815 subprograms, then check their pc bounds. Likewise, we need to
13816 check lexical blocks as well, as they may also contain subprogram
13817 definitions. */
13818 while (child && child->tag)
13819 {
13820 if (child->tag == DW_TAG_subprogram
13821 || child->tag == DW_TAG_lexical_block)
13822 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
13823 child = child->sibling;
13824 }
13825 }
13826
13827 /* Get the low and high pc's represented by the scope DIE, and store
13828 them in *LOWPC and *HIGHPC. If the correct values can't be
13829 determined, set *LOWPC to -1 and *HIGHPC to 0. */
13830
13831 static void
13832 get_scope_pc_bounds (struct die_info *die,
13833 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13834 struct dwarf2_cu *cu)
13835 {
13836 CORE_ADDR best_low = (CORE_ADDR) -1;
13837 CORE_ADDR best_high = (CORE_ADDR) 0;
13838 CORE_ADDR current_low, current_high;
13839
13840 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
13841 >= PC_BOUNDS_RANGES)
13842 {
13843 best_low = current_low;
13844 best_high = current_high;
13845 }
13846 else
13847 {
13848 struct die_info *child = die->child;
13849
13850 while (child && child->tag)
13851 {
13852 switch (child->tag) {
13853 case DW_TAG_subprogram:
13854 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
13855 break;
13856 case DW_TAG_namespace:
13857 case DW_TAG_module:
13858 /* FIXME: carlton/2004-01-16: Should we do this for
13859 DW_TAG_class_type/DW_TAG_structure_type, too? I think
13860 that current GCC's always emit the DIEs corresponding
13861 to definitions of methods of classes as children of a
13862 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
13863 the DIEs giving the declarations, which could be
13864 anywhere). But I don't see any reason why the
13865 standards says that they have to be there. */
13866 get_scope_pc_bounds (child, &current_low, &current_high, cu);
13867
13868 if (current_low != ((CORE_ADDR) -1))
13869 {
13870 best_low = std::min (best_low, current_low);
13871 best_high = std::max (best_high, current_high);
13872 }
13873 break;
13874 default:
13875 /* Ignore. */
13876 break;
13877 }
13878
13879 child = child->sibling;
13880 }
13881 }
13882
13883 *lowpc = best_low;
13884 *highpc = best_high;
13885 }
13886
13887 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
13888 in DIE. */
13889
13890 static void
13891 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
13892 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
13893 {
13894 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13895 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13896 struct attribute *attr;
13897 struct attribute *attr_high;
13898
13899 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13900 if (attr_high)
13901 {
13902 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13903 if (attr != nullptr)
13904 {
13905 CORE_ADDR low = attr->value_as_address ();
13906 CORE_ADDR high = attr_high->value_as_address ();
13907
13908 if (cu->header.version >= 4 && attr_high->form_is_constant ())
13909 high += low;
13910
13911 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
13912 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
13913 cu->get_builder ()->record_block_range (block, low, high - 1);
13914 }
13915 }
13916
13917 attr = dwarf2_attr (die, DW_AT_ranges, cu);
13918 if (attr != nullptr)
13919 {
13920 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
13921 We take advantage of the fact that DW_AT_ranges does not appear
13922 in DW_TAG_compile_unit of DWO files. */
13923 int need_ranges_base = die->tag != DW_TAG_compile_unit;
13924
13925 /* The value of the DW_AT_ranges attribute is the offset of the
13926 address range list in the .debug_ranges section. */
13927 unsigned long offset = (DW_UNSND (attr)
13928 + (need_ranges_base ? cu->ranges_base : 0));
13929
13930 std::vector<blockrange> blockvec;
13931 dwarf2_ranges_process (offset, cu,
13932 [&] (CORE_ADDR start, CORE_ADDR end)
13933 {
13934 start += baseaddr;
13935 end += baseaddr;
13936 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
13937 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
13938 cu->get_builder ()->record_block_range (block, start, end - 1);
13939 blockvec.emplace_back (start, end);
13940 });
13941
13942 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
13943 }
13944 }
13945
13946 /* Check whether the producer field indicates either of GCC < 4.6, or the
13947 Intel C/C++ compiler, and cache the result in CU. */
13948
13949 static void
13950 check_producer (struct dwarf2_cu *cu)
13951 {
13952 int major, minor;
13953
13954 if (cu->producer == NULL)
13955 {
13956 /* For unknown compilers expect their behavior is DWARF version
13957 compliant.
13958
13959 GCC started to support .debug_types sections by -gdwarf-4 since
13960 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
13961 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
13962 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
13963 interpreted incorrectly by GDB now - GCC PR debug/48229. */
13964 }
13965 else if (producer_is_gcc (cu->producer, &major, &minor))
13966 {
13967 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
13968 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
13969 }
13970 else if (producer_is_icc (cu->producer, &major, &minor))
13971 {
13972 cu->producer_is_icc = true;
13973 cu->producer_is_icc_lt_14 = major < 14;
13974 }
13975 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
13976 cu->producer_is_codewarrior = true;
13977 else
13978 {
13979 /* For other non-GCC compilers, expect their behavior is DWARF version
13980 compliant. */
13981 }
13982
13983 cu->checked_producer = true;
13984 }
13985
13986 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
13987 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
13988 during 4.6.0 experimental. */
13989
13990 static bool
13991 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
13992 {
13993 if (!cu->checked_producer)
13994 check_producer (cu);
13995
13996 return cu->producer_is_gxx_lt_4_6;
13997 }
13998
13999
14000 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14001 with incorrect is_stmt attributes. */
14002
14003 static bool
14004 producer_is_codewarrior (struct dwarf2_cu *cu)
14005 {
14006 if (!cu->checked_producer)
14007 check_producer (cu);
14008
14009 return cu->producer_is_codewarrior;
14010 }
14011
14012 /* Return the default accessibility type if it is not overridden by
14013 DW_AT_accessibility. */
14014
14015 static enum dwarf_access_attribute
14016 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14017 {
14018 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14019 {
14020 /* The default DWARF 2 accessibility for members is public, the default
14021 accessibility for inheritance is private. */
14022
14023 if (die->tag != DW_TAG_inheritance)
14024 return DW_ACCESS_public;
14025 else
14026 return DW_ACCESS_private;
14027 }
14028 else
14029 {
14030 /* DWARF 3+ defines the default accessibility a different way. The same
14031 rules apply now for DW_TAG_inheritance as for the members and it only
14032 depends on the container kind. */
14033
14034 if (die->parent->tag == DW_TAG_class_type)
14035 return DW_ACCESS_private;
14036 else
14037 return DW_ACCESS_public;
14038 }
14039 }
14040
14041 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14042 offset. If the attribute was not found return 0, otherwise return
14043 1. If it was found but could not properly be handled, set *OFFSET
14044 to 0. */
14045
14046 static int
14047 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14048 LONGEST *offset)
14049 {
14050 struct attribute *attr;
14051
14052 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14053 if (attr != NULL)
14054 {
14055 *offset = 0;
14056
14057 /* Note that we do not check for a section offset first here.
14058 This is because DW_AT_data_member_location is new in DWARF 4,
14059 so if we see it, we can assume that a constant form is really
14060 a constant and not a section offset. */
14061 if (attr->form_is_constant ())
14062 *offset = dwarf2_get_attr_constant_value (attr, 0);
14063 else if (attr->form_is_section_offset ())
14064 dwarf2_complex_location_expr_complaint ();
14065 else if (attr->form_is_block ())
14066 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14067 else
14068 dwarf2_complex_location_expr_complaint ();
14069
14070 return 1;
14071 }
14072
14073 return 0;
14074 }
14075
14076 /* Add an aggregate field to the field list. */
14077
14078 static void
14079 dwarf2_add_field (struct field_info *fip, struct die_info *die,
14080 struct dwarf2_cu *cu)
14081 {
14082 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14083 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14084 struct nextfield *new_field;
14085 struct attribute *attr;
14086 struct field *fp;
14087 const char *fieldname = "";
14088
14089 if (die->tag == DW_TAG_inheritance)
14090 {
14091 fip->baseclasses.emplace_back ();
14092 new_field = &fip->baseclasses.back ();
14093 }
14094 else
14095 {
14096 fip->fields.emplace_back ();
14097 new_field = &fip->fields.back ();
14098 }
14099
14100 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14101 if (attr != nullptr)
14102 new_field->accessibility = DW_UNSND (attr);
14103 else
14104 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
14105 if (new_field->accessibility != DW_ACCESS_public)
14106 fip->non_public_fields = 1;
14107
14108 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14109 if (attr != nullptr)
14110 new_field->virtuality = DW_UNSND (attr);
14111 else
14112 new_field->virtuality = DW_VIRTUALITY_none;
14113
14114 fp = &new_field->field;
14115
14116 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
14117 {
14118 LONGEST offset;
14119
14120 /* Data member other than a C++ static data member. */
14121
14122 /* Get type of field. */
14123 fp->type = die_type (die, cu);
14124
14125 SET_FIELD_BITPOS (*fp, 0);
14126
14127 /* Get bit size of field (zero if none). */
14128 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
14129 if (attr != nullptr)
14130 {
14131 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
14132 }
14133 else
14134 {
14135 FIELD_BITSIZE (*fp) = 0;
14136 }
14137
14138 /* Get bit offset of field. */
14139 if (handle_data_member_location (die, cu, &offset))
14140 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
14141 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
14142 if (attr != nullptr)
14143 {
14144 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
14145 {
14146 /* For big endian bits, the DW_AT_bit_offset gives the
14147 additional bit offset from the MSB of the containing
14148 anonymous object to the MSB of the field. We don't
14149 have to do anything special since we don't need to
14150 know the size of the anonymous object. */
14151 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
14152 }
14153 else
14154 {
14155 /* For little endian bits, compute the bit offset to the
14156 MSB of the anonymous object, subtract off the number of
14157 bits from the MSB of the field to the MSB of the
14158 object, and then subtract off the number of bits of
14159 the field itself. The result is the bit offset of
14160 the LSB of the field. */
14161 int anonymous_size;
14162 int bit_offset = DW_UNSND (attr);
14163
14164 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14165 if (attr != nullptr)
14166 {
14167 /* The size of the anonymous object containing
14168 the bit field is explicit, so use the
14169 indicated size (in bytes). */
14170 anonymous_size = DW_UNSND (attr);
14171 }
14172 else
14173 {
14174 /* The size of the anonymous object containing
14175 the bit field must be inferred from the type
14176 attribute of the data member containing the
14177 bit field. */
14178 anonymous_size = TYPE_LENGTH (fp->type);
14179 }
14180 SET_FIELD_BITPOS (*fp,
14181 (FIELD_BITPOS (*fp)
14182 + anonymous_size * bits_per_byte
14183 - bit_offset - FIELD_BITSIZE (*fp)));
14184 }
14185 }
14186 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14187 if (attr != NULL)
14188 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
14189 + dwarf2_get_attr_constant_value (attr, 0)));
14190
14191 /* Get name of field. */
14192 fieldname = dwarf2_name (die, cu);
14193 if (fieldname == NULL)
14194 fieldname = "";
14195
14196 /* The name is already allocated along with this objfile, so we don't
14197 need to duplicate it for the type. */
14198 fp->name = fieldname;
14199
14200 /* Change accessibility for artificial fields (e.g. virtual table
14201 pointer or virtual base class pointer) to private. */
14202 if (dwarf2_attr (die, DW_AT_artificial, cu))
14203 {
14204 FIELD_ARTIFICIAL (*fp) = 1;
14205 new_field->accessibility = DW_ACCESS_private;
14206 fip->non_public_fields = 1;
14207 }
14208 }
14209 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
14210 {
14211 /* C++ static member. */
14212
14213 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14214 is a declaration, but all versions of G++ as of this writing
14215 (so through at least 3.2.1) incorrectly generate
14216 DW_TAG_variable tags. */
14217
14218 const char *physname;
14219
14220 /* Get name of field. */
14221 fieldname = dwarf2_name (die, cu);
14222 if (fieldname == NULL)
14223 return;
14224
14225 attr = dwarf2_attr (die, DW_AT_const_value, cu);
14226 if (attr
14227 /* Only create a symbol if this is an external value.
14228 new_symbol checks this and puts the value in the global symbol
14229 table, which we want. If it is not external, new_symbol
14230 will try to put the value in cu->list_in_scope which is wrong. */
14231 && dwarf2_flag_true_p (die, DW_AT_external, cu))
14232 {
14233 /* A static const member, not much different than an enum as far as
14234 we're concerned, except that we can support more types. */
14235 new_symbol (die, NULL, cu);
14236 }
14237
14238 /* Get physical name. */
14239 physname = dwarf2_physname (fieldname, die, cu);
14240
14241 /* The name is already allocated along with this objfile, so we don't
14242 need to duplicate it for the type. */
14243 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
14244 FIELD_TYPE (*fp) = die_type (die, cu);
14245 FIELD_NAME (*fp) = fieldname;
14246 }
14247 else if (die->tag == DW_TAG_inheritance)
14248 {
14249 LONGEST offset;
14250
14251 /* C++ base class field. */
14252 if (handle_data_member_location (die, cu, &offset))
14253 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
14254 FIELD_BITSIZE (*fp) = 0;
14255 FIELD_TYPE (*fp) = die_type (die, cu);
14256 FIELD_NAME (*fp) = TYPE_NAME (fp->type);
14257 }
14258 else if (die->tag == DW_TAG_variant_part)
14259 {
14260 /* process_structure_scope will treat this DIE as a union. */
14261 process_structure_scope (die, cu);
14262
14263 /* The variant part is relative to the start of the enclosing
14264 structure. */
14265 SET_FIELD_BITPOS (*fp, 0);
14266 fp->type = get_die_type (die, cu);
14267 fp->artificial = 1;
14268 fp->name = "<<variant>>";
14269
14270 /* Normally a DW_TAG_variant_part won't have a size, but our
14271 representation requires one, so set it to the maximum of the
14272 child sizes, being sure to account for the offset at which
14273 each child is seen. */
14274 if (TYPE_LENGTH (fp->type) == 0)
14275 {
14276 unsigned max = 0;
14277 for (int i = 0; i < TYPE_NFIELDS (fp->type); ++i)
14278 {
14279 unsigned len = ((TYPE_FIELD_BITPOS (fp->type, i) + 7) / 8
14280 + TYPE_LENGTH (TYPE_FIELD_TYPE (fp->type, i)));
14281 if (len > max)
14282 max = len;
14283 }
14284 TYPE_LENGTH (fp->type) = max;
14285 }
14286 }
14287 else
14288 gdb_assert_not_reached ("missing case in dwarf2_add_field");
14289 }
14290
14291 /* Can the type given by DIE define another type? */
14292
14293 static bool
14294 type_can_define_types (const struct die_info *die)
14295 {
14296 switch (die->tag)
14297 {
14298 case DW_TAG_typedef:
14299 case DW_TAG_class_type:
14300 case DW_TAG_structure_type:
14301 case DW_TAG_union_type:
14302 case DW_TAG_enumeration_type:
14303 return true;
14304
14305 default:
14306 return false;
14307 }
14308 }
14309
14310 /* Add a type definition defined in the scope of the FIP's class. */
14311
14312 static void
14313 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14314 struct dwarf2_cu *cu)
14315 {
14316 struct decl_field fp;
14317 memset (&fp, 0, sizeof (fp));
14318
14319 gdb_assert (type_can_define_types (die));
14320
14321 /* Get name of field. NULL is okay here, meaning an anonymous type. */
14322 fp.name = dwarf2_name (die, cu);
14323 fp.type = read_type_die (die, cu);
14324
14325 /* Save accessibility. */
14326 enum dwarf_access_attribute accessibility;
14327 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14328 if (attr != NULL)
14329 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14330 else
14331 accessibility = dwarf2_default_access_attribute (die, cu);
14332 switch (accessibility)
14333 {
14334 case DW_ACCESS_public:
14335 /* The assumed value if neither private nor protected. */
14336 break;
14337 case DW_ACCESS_private:
14338 fp.is_private = 1;
14339 break;
14340 case DW_ACCESS_protected:
14341 fp.is_protected = 1;
14342 break;
14343 default:
14344 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
14345 }
14346
14347 if (die->tag == DW_TAG_typedef)
14348 fip->typedef_field_list.push_back (fp);
14349 else
14350 fip->nested_types_list.push_back (fp);
14351 }
14352
14353 /* Create the vector of fields, and attach it to the type. */
14354
14355 static void
14356 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
14357 struct dwarf2_cu *cu)
14358 {
14359 int nfields = fip->nfields ();
14360
14361 /* Record the field count, allocate space for the array of fields,
14362 and create blank accessibility bitfields if necessary. */
14363 TYPE_NFIELDS (type) = nfields;
14364 TYPE_FIELDS (type) = (struct field *)
14365 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
14366
14367 if (fip->non_public_fields && cu->language != language_ada)
14368 {
14369 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14370
14371 TYPE_FIELD_PRIVATE_BITS (type) =
14372 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14373 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14374
14375 TYPE_FIELD_PROTECTED_BITS (type) =
14376 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14377 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14378
14379 TYPE_FIELD_IGNORE_BITS (type) =
14380 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14381 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
14382 }
14383
14384 /* If the type has baseclasses, allocate and clear a bit vector for
14385 TYPE_FIELD_VIRTUAL_BITS. */
14386 if (!fip->baseclasses.empty () && cu->language != language_ada)
14387 {
14388 int num_bytes = B_BYTES (fip->baseclasses.size ());
14389 unsigned char *pointer;
14390
14391 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14392 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
14393 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
14394 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
14395 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
14396 }
14397
14398 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
14399 {
14400 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
14401
14402 for (int index = 0; index < nfields; ++index)
14403 {
14404 struct nextfield &field = fip->fields[index];
14405
14406 if (field.variant.is_discriminant)
14407 di->discriminant_index = index;
14408 else if (field.variant.default_branch)
14409 di->default_index = index;
14410 else
14411 di->discriminants[index] = field.variant.discriminant_value;
14412 }
14413 }
14414
14415 /* Copy the saved-up fields into the field vector. */
14416 for (int i = 0; i < nfields; ++i)
14417 {
14418 struct nextfield &field
14419 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
14420 : fip->fields[i - fip->baseclasses.size ()]);
14421
14422 TYPE_FIELD (type, i) = field.field;
14423 switch (field.accessibility)
14424 {
14425 case DW_ACCESS_private:
14426 if (cu->language != language_ada)
14427 SET_TYPE_FIELD_PRIVATE (type, i);
14428 break;
14429
14430 case DW_ACCESS_protected:
14431 if (cu->language != language_ada)
14432 SET_TYPE_FIELD_PROTECTED (type, i);
14433 break;
14434
14435 case DW_ACCESS_public:
14436 break;
14437
14438 default:
14439 /* Unknown accessibility. Complain and treat it as public. */
14440 {
14441 complaint (_("unsupported accessibility %d"),
14442 field.accessibility);
14443 }
14444 break;
14445 }
14446 if (i < fip->baseclasses.size ())
14447 {
14448 switch (field.virtuality)
14449 {
14450 case DW_VIRTUALITY_virtual:
14451 case DW_VIRTUALITY_pure_virtual:
14452 if (cu->language == language_ada)
14453 error (_("unexpected virtuality in component of Ada type"));
14454 SET_TYPE_FIELD_VIRTUAL (type, i);
14455 break;
14456 }
14457 }
14458 }
14459 }
14460
14461 /* Return true if this member function is a constructor, false
14462 otherwise. */
14463
14464 static int
14465 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
14466 {
14467 const char *fieldname;
14468 const char *type_name;
14469 int len;
14470
14471 if (die->parent == NULL)
14472 return 0;
14473
14474 if (die->parent->tag != DW_TAG_structure_type
14475 && die->parent->tag != DW_TAG_union_type
14476 && die->parent->tag != DW_TAG_class_type)
14477 return 0;
14478
14479 fieldname = dwarf2_name (die, cu);
14480 type_name = dwarf2_name (die->parent, cu);
14481 if (fieldname == NULL || type_name == NULL)
14482 return 0;
14483
14484 len = strlen (fieldname);
14485 return (strncmp (fieldname, type_name, len) == 0
14486 && (type_name[len] == '\0' || type_name[len] == '<'));
14487 }
14488
14489 /* Check if the given VALUE is a recognized enum
14490 dwarf_defaulted_attribute constant according to DWARF5 spec,
14491 Table 7.24. */
14492
14493 static bool
14494 is_valid_DW_AT_defaulted (ULONGEST value)
14495 {
14496 switch (value)
14497 {
14498 case DW_DEFAULTED_no:
14499 case DW_DEFAULTED_in_class:
14500 case DW_DEFAULTED_out_of_class:
14501 return true;
14502 }
14503
14504 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
14505 return false;
14506 }
14507
14508 /* Add a member function to the proper fieldlist. */
14509
14510 static void
14511 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
14512 struct type *type, struct dwarf2_cu *cu)
14513 {
14514 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14515 struct attribute *attr;
14516 int i;
14517 struct fnfieldlist *flp = nullptr;
14518 struct fn_field *fnp;
14519 const char *fieldname;
14520 struct type *this_type;
14521 enum dwarf_access_attribute accessibility;
14522
14523 if (cu->language == language_ada)
14524 error (_("unexpected member function in Ada type"));
14525
14526 /* Get name of member function. */
14527 fieldname = dwarf2_name (die, cu);
14528 if (fieldname == NULL)
14529 return;
14530
14531 /* Look up member function name in fieldlist. */
14532 for (i = 0; i < fip->fnfieldlists.size (); i++)
14533 {
14534 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
14535 {
14536 flp = &fip->fnfieldlists[i];
14537 break;
14538 }
14539 }
14540
14541 /* Create a new fnfieldlist if necessary. */
14542 if (flp == nullptr)
14543 {
14544 fip->fnfieldlists.emplace_back ();
14545 flp = &fip->fnfieldlists.back ();
14546 flp->name = fieldname;
14547 i = fip->fnfieldlists.size () - 1;
14548 }
14549
14550 /* Create a new member function field and add it to the vector of
14551 fnfieldlists. */
14552 flp->fnfields.emplace_back ();
14553 fnp = &flp->fnfields.back ();
14554
14555 /* Delay processing of the physname until later. */
14556 if (cu->language == language_cplus)
14557 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
14558 die, cu);
14559 else
14560 {
14561 const char *physname = dwarf2_physname (fieldname, die, cu);
14562 fnp->physname = physname ? physname : "";
14563 }
14564
14565 fnp->type = alloc_type (objfile);
14566 this_type = read_type_die (die, cu);
14567 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
14568 {
14569 int nparams = TYPE_NFIELDS (this_type);
14570
14571 /* TYPE is the domain of this method, and THIS_TYPE is the type
14572 of the method itself (TYPE_CODE_METHOD). */
14573 smash_to_method_type (fnp->type, type,
14574 TYPE_TARGET_TYPE (this_type),
14575 TYPE_FIELDS (this_type),
14576 TYPE_NFIELDS (this_type),
14577 TYPE_VARARGS (this_type));
14578
14579 /* Handle static member functions.
14580 Dwarf2 has no clean way to discern C++ static and non-static
14581 member functions. G++ helps GDB by marking the first
14582 parameter for non-static member functions (which is the this
14583 pointer) as artificial. We obtain this information from
14584 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
14585 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
14586 fnp->voffset = VOFFSET_STATIC;
14587 }
14588 else
14589 complaint (_("member function type missing for '%s'"),
14590 dwarf2_full_name (fieldname, die, cu));
14591
14592 /* Get fcontext from DW_AT_containing_type if present. */
14593 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
14594 fnp->fcontext = die_containing_type (die, cu);
14595
14596 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
14597 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
14598
14599 /* Get accessibility. */
14600 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14601 if (attr != nullptr)
14602 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14603 else
14604 accessibility = dwarf2_default_access_attribute (die, cu);
14605 switch (accessibility)
14606 {
14607 case DW_ACCESS_private:
14608 fnp->is_private = 1;
14609 break;
14610 case DW_ACCESS_protected:
14611 fnp->is_protected = 1;
14612 break;
14613 }
14614
14615 /* Check for artificial methods. */
14616 attr = dwarf2_attr (die, DW_AT_artificial, cu);
14617 if (attr && DW_UNSND (attr) != 0)
14618 fnp->is_artificial = 1;
14619
14620 /* Check for defaulted methods. */
14621 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
14622 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
14623 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
14624
14625 /* Check for deleted methods. */
14626 attr = dwarf2_attr (die, DW_AT_deleted, cu);
14627 if (attr != nullptr && DW_UNSND (attr) != 0)
14628 fnp->is_deleted = 1;
14629
14630 fnp->is_constructor = dwarf2_is_constructor (die, cu);
14631
14632 /* Get index in virtual function table if it is a virtual member
14633 function. For older versions of GCC, this is an offset in the
14634 appropriate virtual table, as specified by DW_AT_containing_type.
14635 For everyone else, it is an expression to be evaluated relative
14636 to the object address. */
14637
14638 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
14639 if (attr != nullptr)
14640 {
14641 if (attr->form_is_block () && DW_BLOCK (attr)->size > 0)
14642 {
14643 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
14644 {
14645 /* Old-style GCC. */
14646 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
14647 }
14648 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
14649 || (DW_BLOCK (attr)->size > 1
14650 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
14651 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
14652 {
14653 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
14654 if ((fnp->voffset % cu->header.addr_size) != 0)
14655 dwarf2_complex_location_expr_complaint ();
14656 else
14657 fnp->voffset /= cu->header.addr_size;
14658 fnp->voffset += 2;
14659 }
14660 else
14661 dwarf2_complex_location_expr_complaint ();
14662
14663 if (!fnp->fcontext)
14664 {
14665 /* If there is no `this' field and no DW_AT_containing_type,
14666 we cannot actually find a base class context for the
14667 vtable! */
14668 if (TYPE_NFIELDS (this_type) == 0
14669 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
14670 {
14671 complaint (_("cannot determine context for virtual member "
14672 "function \"%s\" (offset %s)"),
14673 fieldname, sect_offset_str (die->sect_off));
14674 }
14675 else
14676 {
14677 fnp->fcontext
14678 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
14679 }
14680 }
14681 }
14682 else if (attr->form_is_section_offset ())
14683 {
14684 dwarf2_complex_location_expr_complaint ();
14685 }
14686 else
14687 {
14688 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
14689 fieldname);
14690 }
14691 }
14692 else
14693 {
14694 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14695 if (attr && DW_UNSND (attr))
14696 {
14697 /* GCC does this, as of 2008-08-25; PR debug/37237. */
14698 complaint (_("Member function \"%s\" (offset %s) is virtual "
14699 "but the vtable offset is not specified"),
14700 fieldname, sect_offset_str (die->sect_off));
14701 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14702 TYPE_CPLUS_DYNAMIC (type) = 1;
14703 }
14704 }
14705 }
14706
14707 /* Create the vector of member function fields, and attach it to the type. */
14708
14709 static void
14710 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
14711 struct dwarf2_cu *cu)
14712 {
14713 if (cu->language == language_ada)
14714 error (_("unexpected member functions in Ada type"));
14715
14716 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14717 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
14718 TYPE_ALLOC (type,
14719 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
14720
14721 for (int i = 0; i < fip->fnfieldlists.size (); i++)
14722 {
14723 struct fnfieldlist &nf = fip->fnfieldlists[i];
14724 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
14725
14726 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
14727 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
14728 fn_flp->fn_fields = (struct fn_field *)
14729 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
14730
14731 for (int k = 0; k < nf.fnfields.size (); ++k)
14732 fn_flp->fn_fields[k] = nf.fnfields[k];
14733 }
14734
14735 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
14736 }
14737
14738 /* Returns non-zero if NAME is the name of a vtable member in CU's
14739 language, zero otherwise. */
14740 static int
14741 is_vtable_name (const char *name, struct dwarf2_cu *cu)
14742 {
14743 static const char vptr[] = "_vptr";
14744
14745 /* Look for the C++ form of the vtable. */
14746 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
14747 return 1;
14748
14749 return 0;
14750 }
14751
14752 /* GCC outputs unnamed structures that are really pointers to member
14753 functions, with the ABI-specified layout. If TYPE describes
14754 such a structure, smash it into a member function type.
14755
14756 GCC shouldn't do this; it should just output pointer to member DIEs.
14757 This is GCC PR debug/28767. */
14758
14759 static void
14760 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
14761 {
14762 struct type *pfn_type, *self_type, *new_type;
14763
14764 /* Check for a structure with no name and two children. */
14765 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
14766 return;
14767
14768 /* Check for __pfn and __delta members. */
14769 if (TYPE_FIELD_NAME (type, 0) == NULL
14770 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
14771 || TYPE_FIELD_NAME (type, 1) == NULL
14772 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
14773 return;
14774
14775 /* Find the type of the method. */
14776 pfn_type = TYPE_FIELD_TYPE (type, 0);
14777 if (pfn_type == NULL
14778 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
14779 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
14780 return;
14781
14782 /* Look for the "this" argument. */
14783 pfn_type = TYPE_TARGET_TYPE (pfn_type);
14784 if (TYPE_NFIELDS (pfn_type) == 0
14785 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
14786 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
14787 return;
14788
14789 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
14790 new_type = alloc_type (objfile);
14791 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
14792 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
14793 TYPE_VARARGS (pfn_type));
14794 smash_to_methodptr_type (type, new_type);
14795 }
14796
14797 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
14798 appropriate error checking and issuing complaints if there is a
14799 problem. */
14800
14801 static ULONGEST
14802 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
14803 {
14804 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
14805
14806 if (attr == nullptr)
14807 return 0;
14808
14809 if (!attr->form_is_constant ())
14810 {
14811 complaint (_("DW_AT_alignment must have constant form"
14812 " - DIE at %s [in module %s]"),
14813 sect_offset_str (die->sect_off),
14814 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14815 return 0;
14816 }
14817
14818 ULONGEST align;
14819 if (attr->form == DW_FORM_sdata)
14820 {
14821 LONGEST val = DW_SND (attr);
14822 if (val < 0)
14823 {
14824 complaint (_("DW_AT_alignment value must not be negative"
14825 " - DIE at %s [in module %s]"),
14826 sect_offset_str (die->sect_off),
14827 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14828 return 0;
14829 }
14830 align = val;
14831 }
14832 else
14833 align = DW_UNSND (attr);
14834
14835 if (align == 0)
14836 {
14837 complaint (_("DW_AT_alignment value must not be zero"
14838 " - DIE at %s [in module %s]"),
14839 sect_offset_str (die->sect_off),
14840 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14841 return 0;
14842 }
14843 if ((align & (align - 1)) != 0)
14844 {
14845 complaint (_("DW_AT_alignment value must be a power of 2"
14846 " - DIE at %s [in module %s]"),
14847 sect_offset_str (die->sect_off),
14848 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14849 return 0;
14850 }
14851
14852 return align;
14853 }
14854
14855 /* If the DIE has a DW_AT_alignment attribute, use its value to set
14856 the alignment for TYPE. */
14857
14858 static void
14859 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
14860 struct type *type)
14861 {
14862 if (!set_type_align (type, get_alignment (cu, die)))
14863 complaint (_("DW_AT_alignment value too large"
14864 " - DIE at %s [in module %s]"),
14865 sect_offset_str (die->sect_off),
14866 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14867 }
14868
14869 /* Check if the given VALUE is a valid enum dwarf_calling_convention
14870 constant for a type, according to DWARF5 spec, Table 5.5. */
14871
14872 static bool
14873 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
14874 {
14875 switch (value)
14876 {
14877 case DW_CC_normal:
14878 case DW_CC_pass_by_reference:
14879 case DW_CC_pass_by_value:
14880 return true;
14881
14882 default:
14883 complaint (_("unrecognized DW_AT_calling_convention value "
14884 "(%s) for a type"), pulongest (value));
14885 return false;
14886 }
14887 }
14888
14889 /* Check if the given VALUE is a valid enum dwarf_calling_convention
14890 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
14891 also according to GNU-specific values (see include/dwarf2.h). */
14892
14893 static bool
14894 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
14895 {
14896 switch (value)
14897 {
14898 case DW_CC_normal:
14899 case DW_CC_program:
14900 case DW_CC_nocall:
14901 return true;
14902
14903 case DW_CC_GNU_renesas_sh:
14904 case DW_CC_GNU_borland_fastcall_i386:
14905 case DW_CC_GDB_IBM_OpenCL:
14906 return true;
14907
14908 default:
14909 complaint (_("unrecognized DW_AT_calling_convention value "
14910 "(%s) for a subroutine"), pulongest (value));
14911 return false;
14912 }
14913 }
14914
14915 /* Called when we find the DIE that starts a structure or union scope
14916 (definition) to create a type for the structure or union. Fill in
14917 the type's name and general properties; the members will not be
14918 processed until process_structure_scope. A symbol table entry for
14919 the type will also not be done until process_structure_scope (assuming
14920 the type has a name).
14921
14922 NOTE: we need to call these functions regardless of whether or not the
14923 DIE has a DW_AT_name attribute, since it might be an anonymous
14924 structure or union. This gets the type entered into our set of
14925 user defined types. */
14926
14927 static struct type *
14928 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
14929 {
14930 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14931 struct type *type;
14932 struct attribute *attr;
14933 const char *name;
14934
14935 /* If the definition of this type lives in .debug_types, read that type.
14936 Don't follow DW_AT_specification though, that will take us back up
14937 the chain and we want to go down. */
14938 attr = die->attr (DW_AT_signature);
14939 if (attr != nullptr)
14940 {
14941 type = get_DW_AT_signature_type (die, attr, cu);
14942
14943 /* The type's CU may not be the same as CU.
14944 Ensure TYPE is recorded with CU in die_type_hash. */
14945 return set_die_type (die, type, cu);
14946 }
14947
14948 type = alloc_type (objfile);
14949 INIT_CPLUS_SPECIFIC (type);
14950
14951 name = dwarf2_name (die, cu);
14952 if (name != NULL)
14953 {
14954 if (cu->language == language_cplus
14955 || cu->language == language_d
14956 || cu->language == language_rust)
14957 {
14958 const char *full_name = dwarf2_full_name (name, die, cu);
14959
14960 /* dwarf2_full_name might have already finished building the DIE's
14961 type. If so, there is no need to continue. */
14962 if (get_die_type (die, cu) != NULL)
14963 return get_die_type (die, cu);
14964
14965 TYPE_NAME (type) = full_name;
14966 }
14967 else
14968 {
14969 /* The name is already allocated along with this objfile, so
14970 we don't need to duplicate it for the type. */
14971 TYPE_NAME (type) = name;
14972 }
14973 }
14974
14975 if (die->tag == DW_TAG_structure_type)
14976 {
14977 TYPE_CODE (type) = TYPE_CODE_STRUCT;
14978 }
14979 else if (die->tag == DW_TAG_union_type)
14980 {
14981 TYPE_CODE (type) = TYPE_CODE_UNION;
14982 }
14983 else if (die->tag == DW_TAG_variant_part)
14984 {
14985 TYPE_CODE (type) = TYPE_CODE_UNION;
14986 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
14987 }
14988 else
14989 {
14990 TYPE_CODE (type) = TYPE_CODE_STRUCT;
14991 }
14992
14993 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
14994 TYPE_DECLARED_CLASS (type) = 1;
14995
14996 /* Store the calling convention in the type if it's available in
14997 the die. Otherwise the calling convention remains set to
14998 the default value DW_CC_normal. */
14999 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15000 if (attr != nullptr
15001 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15002 {
15003 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15004 TYPE_CPLUS_CALLING_CONVENTION (type)
15005 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15006 }
15007
15008 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15009 if (attr != nullptr)
15010 {
15011 if (attr->form_is_constant ())
15012 TYPE_LENGTH (type) = DW_UNSND (attr);
15013 else
15014 {
15015 /* For the moment, dynamic type sizes are not supported
15016 by GDB's struct type. The actual size is determined
15017 on-demand when resolving the type of a given object,
15018 so set the type's length to zero for now. Otherwise,
15019 we record an expression as the length, and that expression
15020 could lead to a very large value, which could eventually
15021 lead to us trying to allocate that much memory when creating
15022 a value of that type. */
15023 TYPE_LENGTH (type) = 0;
15024 }
15025 }
15026 else
15027 {
15028 TYPE_LENGTH (type) = 0;
15029 }
15030
15031 maybe_set_alignment (cu, die, type);
15032
15033 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15034 {
15035 /* ICC<14 does not output the required DW_AT_declaration on
15036 incomplete types, but gives them a size of zero. */
15037 TYPE_STUB (type) = 1;
15038 }
15039 else
15040 TYPE_STUB_SUPPORTED (type) = 1;
15041
15042 if (die_is_declaration (die, cu))
15043 TYPE_STUB (type) = 1;
15044 else if (attr == NULL && die->child == NULL
15045 && producer_is_realview (cu->producer))
15046 /* RealView does not output the required DW_AT_declaration
15047 on incomplete types. */
15048 TYPE_STUB (type) = 1;
15049
15050 /* We need to add the type field to the die immediately so we don't
15051 infinitely recurse when dealing with pointers to the structure
15052 type within the structure itself. */
15053 set_die_type (die, type, cu);
15054
15055 /* set_die_type should be already done. */
15056 set_descriptive_type (type, die, cu);
15057
15058 return type;
15059 }
15060
15061 /* A helper for process_structure_scope that handles a single member
15062 DIE. */
15063
15064 static void
15065 handle_struct_member_die (struct die_info *child_die, struct type *type,
15066 struct field_info *fi,
15067 std::vector<struct symbol *> *template_args,
15068 struct dwarf2_cu *cu)
15069 {
15070 if (child_die->tag == DW_TAG_member
15071 || child_die->tag == DW_TAG_variable
15072 || child_die->tag == DW_TAG_variant_part)
15073 {
15074 /* NOTE: carlton/2002-11-05: A C++ static data member
15075 should be a DW_TAG_member that is a declaration, but
15076 all versions of G++ as of this writing (so through at
15077 least 3.2.1) incorrectly generate DW_TAG_variable
15078 tags for them instead. */
15079 dwarf2_add_field (fi, child_die, cu);
15080 }
15081 else if (child_die->tag == DW_TAG_subprogram)
15082 {
15083 /* Rust doesn't have member functions in the C++ sense.
15084 However, it does emit ordinary functions as children
15085 of a struct DIE. */
15086 if (cu->language == language_rust)
15087 read_func_scope (child_die, cu);
15088 else
15089 {
15090 /* C++ member function. */
15091 dwarf2_add_member_fn (fi, child_die, type, cu);
15092 }
15093 }
15094 else if (child_die->tag == DW_TAG_inheritance)
15095 {
15096 /* C++ base class field. */
15097 dwarf2_add_field (fi, child_die, cu);
15098 }
15099 else if (type_can_define_types (child_die))
15100 dwarf2_add_type_defn (fi, child_die, cu);
15101 else if (child_die->tag == DW_TAG_template_type_param
15102 || child_die->tag == DW_TAG_template_value_param)
15103 {
15104 struct symbol *arg = new_symbol (child_die, NULL, cu);
15105
15106 if (arg != NULL)
15107 template_args->push_back (arg);
15108 }
15109 else if (child_die->tag == DW_TAG_variant)
15110 {
15111 /* In a variant we want to get the discriminant and also add a
15112 field for our sole member child. */
15113 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
15114
15115 for (die_info *variant_child = child_die->child;
15116 variant_child != NULL;
15117 variant_child = variant_child->sibling)
15118 {
15119 if (variant_child->tag == DW_TAG_member)
15120 {
15121 handle_struct_member_die (variant_child, type, fi,
15122 template_args, cu);
15123 /* Only handle the one. */
15124 break;
15125 }
15126 }
15127
15128 /* We don't handle this but we might as well report it if we see
15129 it. */
15130 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
15131 complaint (_("DW_AT_discr_list is not supported yet"
15132 " - DIE at %s [in module %s]"),
15133 sect_offset_str (child_die->sect_off),
15134 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15135
15136 /* The first field was just added, so we can stash the
15137 discriminant there. */
15138 gdb_assert (!fi->fields.empty ());
15139 if (discr == NULL)
15140 fi->fields.back ().variant.default_branch = true;
15141 else
15142 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
15143 }
15144 }
15145
15146 /* Finish creating a structure or union type, including filling in
15147 its members and creating a symbol for it. */
15148
15149 static void
15150 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15151 {
15152 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15153 struct die_info *child_die;
15154 struct type *type;
15155
15156 type = get_die_type (die, cu);
15157 if (type == NULL)
15158 type = read_structure_type (die, cu);
15159
15160 /* When reading a DW_TAG_variant_part, we need to notice when we
15161 read the discriminant member, so we can record it later in the
15162 discriminant_info. */
15163 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
15164 sect_offset discr_offset {};
15165 bool has_template_parameters = false;
15166
15167 if (is_variant_part)
15168 {
15169 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15170 if (discr == NULL)
15171 {
15172 /* Maybe it's a univariant form, an extension we support.
15173 In this case arrange not to check the offset. */
15174 is_variant_part = false;
15175 }
15176 else if (discr->form_is_ref ())
15177 {
15178 struct dwarf2_cu *target_cu = cu;
15179 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15180
15181 discr_offset = target_die->sect_off;
15182 }
15183 else
15184 {
15185 complaint (_("DW_AT_discr does not have DIE reference form"
15186 " - DIE at %s [in module %s]"),
15187 sect_offset_str (die->sect_off),
15188 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15189 is_variant_part = false;
15190 }
15191 }
15192
15193 if (die->child != NULL && ! die_is_declaration (die, cu))
15194 {
15195 struct field_info fi;
15196 std::vector<struct symbol *> template_args;
15197
15198 child_die = die->child;
15199
15200 while (child_die && child_die->tag)
15201 {
15202 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
15203
15204 if (is_variant_part && discr_offset == child_die->sect_off)
15205 fi.fields.back ().variant.is_discriminant = true;
15206
15207 child_die = child_die->sibling;
15208 }
15209
15210 /* Attach template arguments to type. */
15211 if (!template_args.empty ())
15212 {
15213 has_template_parameters = true;
15214 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15215 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
15216 TYPE_TEMPLATE_ARGUMENTS (type)
15217 = XOBNEWVEC (&objfile->objfile_obstack,
15218 struct symbol *,
15219 TYPE_N_TEMPLATE_ARGUMENTS (type));
15220 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
15221 template_args.data (),
15222 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15223 * sizeof (struct symbol *)));
15224 }
15225
15226 /* Attach fields and member functions to the type. */
15227 if (fi.nfields () > 0)
15228 dwarf2_attach_fields_to_type (&fi, type, cu);
15229 if (!fi.fnfieldlists.empty ())
15230 {
15231 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
15232
15233 /* Get the type which refers to the base class (possibly this
15234 class itself) which contains the vtable pointer for the current
15235 class from the DW_AT_containing_type attribute. This use of
15236 DW_AT_containing_type is a GNU extension. */
15237
15238 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15239 {
15240 struct type *t = die_containing_type (die, cu);
15241
15242 set_type_vptr_basetype (type, t);
15243 if (type == t)
15244 {
15245 int i;
15246
15247 /* Our own class provides vtbl ptr. */
15248 for (i = TYPE_NFIELDS (t) - 1;
15249 i >= TYPE_N_BASECLASSES (t);
15250 --i)
15251 {
15252 const char *fieldname = TYPE_FIELD_NAME (t, i);
15253
15254 if (is_vtable_name (fieldname, cu))
15255 {
15256 set_type_vptr_fieldno (type, i);
15257 break;
15258 }
15259 }
15260
15261 /* Complain if virtual function table field not found. */
15262 if (i < TYPE_N_BASECLASSES (t))
15263 complaint (_("virtual function table pointer "
15264 "not found when defining class '%s'"),
15265 TYPE_NAME (type) ? TYPE_NAME (type) : "");
15266 }
15267 else
15268 {
15269 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
15270 }
15271 }
15272 else if (cu->producer
15273 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
15274 {
15275 /* The IBM XLC compiler does not provide direct indication
15276 of the containing type, but the vtable pointer is
15277 always named __vfp. */
15278
15279 int i;
15280
15281 for (i = TYPE_NFIELDS (type) - 1;
15282 i >= TYPE_N_BASECLASSES (type);
15283 --i)
15284 {
15285 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15286 {
15287 set_type_vptr_fieldno (type, i);
15288 set_type_vptr_basetype (type, type);
15289 break;
15290 }
15291 }
15292 }
15293 }
15294
15295 /* Copy fi.typedef_field_list linked list elements content into the
15296 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
15297 if (!fi.typedef_field_list.empty ())
15298 {
15299 int count = fi.typedef_field_list.size ();
15300
15301 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15302 TYPE_TYPEDEF_FIELD_ARRAY (type)
15303 = ((struct decl_field *)
15304 TYPE_ALLOC (type,
15305 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15306 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
15307
15308 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15309 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
15310 }
15311
15312 /* Copy fi.nested_types_list linked list elements content into the
15313 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
15314 if (!fi.nested_types_list.empty () && cu->language != language_ada)
15315 {
15316 int count = fi.nested_types_list.size ();
15317
15318 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15319 TYPE_NESTED_TYPES_ARRAY (type)
15320 = ((struct decl_field *)
15321 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15322 TYPE_NESTED_TYPES_COUNT (type) = count;
15323
15324 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15325 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
15326 }
15327 }
15328
15329 quirk_gcc_member_function_pointer (type, objfile);
15330 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
15331 cu->rust_unions.push_back (type);
15332
15333 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15334 snapshots) has been known to create a die giving a declaration
15335 for a class that has, as a child, a die giving a definition for a
15336 nested class. So we have to process our children even if the
15337 current die is a declaration. Normally, of course, a declaration
15338 won't have any children at all. */
15339
15340 child_die = die->child;
15341
15342 while (child_die != NULL && child_die->tag)
15343 {
15344 if (child_die->tag == DW_TAG_member
15345 || child_die->tag == DW_TAG_variable
15346 || child_die->tag == DW_TAG_inheritance
15347 || child_die->tag == DW_TAG_template_value_param
15348 || child_die->tag == DW_TAG_template_type_param)
15349 {
15350 /* Do nothing. */
15351 }
15352 else
15353 process_die (child_die, cu);
15354
15355 child_die = child_die->sibling;
15356 }
15357
15358 /* Do not consider external references. According to the DWARF standard,
15359 these DIEs are identified by the fact that they have no byte_size
15360 attribute, and a declaration attribute. */
15361 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15362 || !die_is_declaration (die, cu))
15363 {
15364 struct symbol *sym = new_symbol (die, type, cu);
15365
15366 if (has_template_parameters)
15367 {
15368 struct symtab *symtab;
15369 if (sym != nullptr)
15370 symtab = symbol_symtab (sym);
15371 else if (cu->line_header != nullptr)
15372 {
15373 /* Any related symtab will do. */
15374 symtab
15375 = cu->line_header->file_names ()[0].symtab;
15376 }
15377 else
15378 {
15379 symtab = nullptr;
15380 complaint (_("could not find suitable "
15381 "symtab for template parameter"
15382 " - DIE at %s [in module %s]"),
15383 sect_offset_str (die->sect_off),
15384 objfile_name (objfile));
15385 }
15386
15387 if (symtab != nullptr)
15388 {
15389 /* Make sure that the symtab is set on the new symbols.
15390 Even though they don't appear in this symtab directly,
15391 other parts of gdb assume that symbols do, and this is
15392 reasonably true. */
15393 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
15394 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
15395 }
15396 }
15397 }
15398 }
15399
15400 /* Assuming DIE is an enumeration type, and TYPE is its associated type,
15401 update TYPE using some information only available in DIE's children. */
15402
15403 static void
15404 update_enumeration_type_from_children (struct die_info *die,
15405 struct type *type,
15406 struct dwarf2_cu *cu)
15407 {
15408 struct die_info *child_die;
15409 int unsigned_enum = 1;
15410 int flag_enum = 1;
15411
15412 auto_obstack obstack;
15413
15414 for (child_die = die->child;
15415 child_die != NULL && child_die->tag;
15416 child_die = child_die->sibling)
15417 {
15418 struct attribute *attr;
15419 LONGEST value;
15420 const gdb_byte *bytes;
15421 struct dwarf2_locexpr_baton *baton;
15422 const char *name;
15423
15424 if (child_die->tag != DW_TAG_enumerator)
15425 continue;
15426
15427 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
15428 if (attr == NULL)
15429 continue;
15430
15431 name = dwarf2_name (child_die, cu);
15432 if (name == NULL)
15433 name = "<anonymous enumerator>";
15434
15435 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
15436 &value, &bytes, &baton);
15437 if (value < 0)
15438 {
15439 unsigned_enum = 0;
15440 flag_enum = 0;
15441 }
15442 else
15443 {
15444 if (count_one_bits_ll (value) >= 2)
15445 flag_enum = 0;
15446 }
15447
15448 /* If we already know that the enum type is neither unsigned, nor
15449 a flag type, no need to look at the rest of the enumerates. */
15450 if (!unsigned_enum && !flag_enum)
15451 break;
15452 }
15453
15454 if (unsigned_enum)
15455 TYPE_UNSIGNED (type) = 1;
15456 if (flag_enum)
15457 TYPE_FLAG_ENUM (type) = 1;
15458 }
15459
15460 /* Given a DW_AT_enumeration_type die, set its type. We do not
15461 complete the type's fields yet, or create any symbols. */
15462
15463 static struct type *
15464 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
15465 {
15466 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15467 struct type *type;
15468 struct attribute *attr;
15469 const char *name;
15470
15471 /* If the definition of this type lives in .debug_types, read that type.
15472 Don't follow DW_AT_specification though, that will take us back up
15473 the chain and we want to go down. */
15474 attr = die->attr (DW_AT_signature);
15475 if (attr != nullptr)
15476 {
15477 type = get_DW_AT_signature_type (die, attr, cu);
15478
15479 /* The type's CU may not be the same as CU.
15480 Ensure TYPE is recorded with CU in die_type_hash. */
15481 return set_die_type (die, type, cu);
15482 }
15483
15484 type = alloc_type (objfile);
15485
15486 TYPE_CODE (type) = TYPE_CODE_ENUM;
15487 name = dwarf2_full_name (NULL, die, cu);
15488 if (name != NULL)
15489 TYPE_NAME (type) = name;
15490
15491 attr = dwarf2_attr (die, DW_AT_type, cu);
15492 if (attr != NULL)
15493 {
15494 struct type *underlying_type = die_type (die, cu);
15495
15496 TYPE_TARGET_TYPE (type) = underlying_type;
15497 }
15498
15499 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15500 if (attr != nullptr)
15501 {
15502 TYPE_LENGTH (type) = DW_UNSND (attr);
15503 }
15504 else
15505 {
15506 TYPE_LENGTH (type) = 0;
15507 }
15508
15509 maybe_set_alignment (cu, die, type);
15510
15511 /* The enumeration DIE can be incomplete. In Ada, any type can be
15512 declared as private in the package spec, and then defined only
15513 inside the package body. Such types are known as Taft Amendment
15514 Types. When another package uses such a type, an incomplete DIE
15515 may be generated by the compiler. */
15516 if (die_is_declaration (die, cu))
15517 TYPE_STUB (type) = 1;
15518
15519 /* Finish the creation of this type by using the enum's children.
15520 We must call this even when the underlying type has been provided
15521 so that we can determine if we're looking at a "flag" enum. */
15522 update_enumeration_type_from_children (die, type, cu);
15523
15524 /* If this type has an underlying type that is not a stub, then we
15525 may use its attributes. We always use the "unsigned" attribute
15526 in this situation, because ordinarily we guess whether the type
15527 is unsigned -- but the guess can be wrong and the underlying type
15528 can tell us the reality. However, we defer to a local size
15529 attribute if one exists, because this lets the compiler override
15530 the underlying type if needed. */
15531 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
15532 {
15533 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
15534 if (TYPE_LENGTH (type) == 0)
15535 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
15536 if (TYPE_RAW_ALIGN (type) == 0
15537 && TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)) != 0)
15538 set_type_align (type, TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)));
15539 }
15540
15541 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
15542
15543 return set_die_type (die, type, cu);
15544 }
15545
15546 /* Given a pointer to a die which begins an enumeration, process all
15547 the dies that define the members of the enumeration, and create the
15548 symbol for the enumeration type.
15549
15550 NOTE: We reverse the order of the element list. */
15551
15552 static void
15553 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
15554 {
15555 struct type *this_type;
15556
15557 this_type = get_die_type (die, cu);
15558 if (this_type == NULL)
15559 this_type = read_enumeration_type (die, cu);
15560
15561 if (die->child != NULL)
15562 {
15563 struct die_info *child_die;
15564 struct symbol *sym;
15565 std::vector<struct field> fields;
15566 const char *name;
15567
15568 child_die = die->child;
15569 while (child_die && child_die->tag)
15570 {
15571 if (child_die->tag != DW_TAG_enumerator)
15572 {
15573 process_die (child_die, cu);
15574 }
15575 else
15576 {
15577 name = dwarf2_name (child_die, cu);
15578 if (name)
15579 {
15580 sym = new_symbol (child_die, this_type, cu);
15581
15582 fields.emplace_back ();
15583 struct field &field = fields.back ();
15584
15585 FIELD_NAME (field) = sym->linkage_name ();
15586 FIELD_TYPE (field) = NULL;
15587 SET_FIELD_ENUMVAL (field, SYMBOL_VALUE (sym));
15588 FIELD_BITSIZE (field) = 0;
15589 }
15590 }
15591
15592 child_die = child_die->sibling;
15593 }
15594
15595 if (!fields.empty ())
15596 {
15597 TYPE_NFIELDS (this_type) = fields.size ();
15598 TYPE_FIELDS (this_type) = (struct field *)
15599 TYPE_ALLOC (this_type, sizeof (struct field) * fields.size ());
15600 memcpy (TYPE_FIELDS (this_type), fields.data (),
15601 sizeof (struct field) * fields.size ());
15602 }
15603 }
15604
15605 /* If we are reading an enum from a .debug_types unit, and the enum
15606 is a declaration, and the enum is not the signatured type in the
15607 unit, then we do not want to add a symbol for it. Adding a
15608 symbol would in some cases obscure the true definition of the
15609 enum, giving users an incomplete type when the definition is
15610 actually available. Note that we do not want to do this for all
15611 enums which are just declarations, because C++0x allows forward
15612 enum declarations. */
15613 if (cu->per_cu->is_debug_types
15614 && die_is_declaration (die, cu))
15615 {
15616 struct signatured_type *sig_type;
15617
15618 sig_type = (struct signatured_type *) cu->per_cu;
15619 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
15620 if (sig_type->type_offset_in_section != die->sect_off)
15621 return;
15622 }
15623
15624 new_symbol (die, this_type, cu);
15625 }
15626
15627 /* Extract all information from a DW_TAG_array_type DIE and put it in
15628 the DIE's type field. For now, this only handles one dimensional
15629 arrays. */
15630
15631 static struct type *
15632 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
15633 {
15634 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15635 struct die_info *child_die;
15636 struct type *type;
15637 struct type *element_type, *range_type, *index_type;
15638 struct attribute *attr;
15639 const char *name;
15640 struct dynamic_prop *byte_stride_prop = NULL;
15641 unsigned int bit_stride = 0;
15642
15643 element_type = die_type (die, cu);
15644
15645 /* The die_type call above may have already set the type for this DIE. */
15646 type = get_die_type (die, cu);
15647 if (type)
15648 return type;
15649
15650 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
15651 if (attr != NULL)
15652 {
15653 int stride_ok;
15654 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
15655
15656 byte_stride_prop
15657 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
15658 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
15659 prop_type);
15660 if (!stride_ok)
15661 {
15662 complaint (_("unable to read array DW_AT_byte_stride "
15663 " - DIE at %s [in module %s]"),
15664 sect_offset_str (die->sect_off),
15665 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15666 /* Ignore this attribute. We will likely not be able to print
15667 arrays of this type correctly, but there is little we can do
15668 to help if we cannot read the attribute's value. */
15669 byte_stride_prop = NULL;
15670 }
15671 }
15672
15673 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
15674 if (attr != NULL)
15675 bit_stride = DW_UNSND (attr);
15676
15677 /* Irix 6.2 native cc creates array types without children for
15678 arrays with unspecified length. */
15679 if (die->child == NULL)
15680 {
15681 index_type = objfile_type (objfile)->builtin_int;
15682 range_type = create_static_range_type (NULL, index_type, 0, -1);
15683 type = create_array_type_with_stride (NULL, element_type, range_type,
15684 byte_stride_prop, bit_stride);
15685 return set_die_type (die, type, cu);
15686 }
15687
15688 std::vector<struct type *> range_types;
15689 child_die = die->child;
15690 while (child_die && child_die->tag)
15691 {
15692 if (child_die->tag == DW_TAG_subrange_type)
15693 {
15694 struct type *child_type = read_type_die (child_die, cu);
15695
15696 if (child_type != NULL)
15697 {
15698 /* The range type was succesfully read. Save it for the
15699 array type creation. */
15700 range_types.push_back (child_type);
15701 }
15702 }
15703 child_die = child_die->sibling;
15704 }
15705
15706 /* Dwarf2 dimensions are output from left to right, create the
15707 necessary array types in backwards order. */
15708
15709 type = element_type;
15710
15711 if (read_array_order (die, cu) == DW_ORD_col_major)
15712 {
15713 int i = 0;
15714
15715 while (i < range_types.size ())
15716 type = create_array_type_with_stride (NULL, type, range_types[i++],
15717 byte_stride_prop, bit_stride);
15718 }
15719 else
15720 {
15721 size_t ndim = range_types.size ();
15722 while (ndim-- > 0)
15723 type = create_array_type_with_stride (NULL, type, range_types[ndim],
15724 byte_stride_prop, bit_stride);
15725 }
15726
15727 /* Understand Dwarf2 support for vector types (like they occur on
15728 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
15729 array type. This is not part of the Dwarf2/3 standard yet, but a
15730 custom vendor extension. The main difference between a regular
15731 array and the vector variant is that vectors are passed by value
15732 to functions. */
15733 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
15734 if (attr != nullptr)
15735 make_vector_type (type);
15736
15737 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
15738 implementation may choose to implement triple vectors using this
15739 attribute. */
15740 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15741 if (attr != nullptr)
15742 {
15743 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
15744 TYPE_LENGTH (type) = DW_UNSND (attr);
15745 else
15746 complaint (_("DW_AT_byte_size for array type smaller "
15747 "than the total size of elements"));
15748 }
15749
15750 name = dwarf2_name (die, cu);
15751 if (name)
15752 TYPE_NAME (type) = name;
15753
15754 maybe_set_alignment (cu, die, type);
15755
15756 /* Install the type in the die. */
15757 set_die_type (die, type, cu);
15758
15759 /* set_die_type should be already done. */
15760 set_descriptive_type (type, die, cu);
15761
15762 return type;
15763 }
15764
15765 static enum dwarf_array_dim_ordering
15766 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
15767 {
15768 struct attribute *attr;
15769
15770 attr = dwarf2_attr (die, DW_AT_ordering, cu);
15771
15772 if (attr != nullptr)
15773 return (enum dwarf_array_dim_ordering) DW_SND (attr);
15774
15775 /* GNU F77 is a special case, as at 08/2004 array type info is the
15776 opposite order to the dwarf2 specification, but data is still
15777 laid out as per normal fortran.
15778
15779 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
15780 version checking. */
15781
15782 if (cu->language == language_fortran
15783 && cu->producer && strstr (cu->producer, "GNU F77"))
15784 {
15785 return DW_ORD_row_major;
15786 }
15787
15788 switch (cu->language_defn->la_array_ordering)
15789 {
15790 case array_column_major:
15791 return DW_ORD_col_major;
15792 case array_row_major:
15793 default:
15794 return DW_ORD_row_major;
15795 };
15796 }
15797
15798 /* Extract all information from a DW_TAG_set_type DIE and put it in
15799 the DIE's type field. */
15800
15801 static struct type *
15802 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
15803 {
15804 struct type *domain_type, *set_type;
15805 struct attribute *attr;
15806
15807 domain_type = die_type (die, cu);
15808
15809 /* The die_type call above may have already set the type for this DIE. */
15810 set_type = get_die_type (die, cu);
15811 if (set_type)
15812 return set_type;
15813
15814 set_type = create_set_type (NULL, domain_type);
15815
15816 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15817 if (attr != nullptr)
15818 TYPE_LENGTH (set_type) = DW_UNSND (attr);
15819
15820 maybe_set_alignment (cu, die, set_type);
15821
15822 return set_die_type (die, set_type, cu);
15823 }
15824
15825 /* A helper for read_common_block that creates a locexpr baton.
15826 SYM is the symbol which we are marking as computed.
15827 COMMON_DIE is the DIE for the common block.
15828 COMMON_LOC is the location expression attribute for the common
15829 block itself.
15830 MEMBER_LOC is the location expression attribute for the particular
15831 member of the common block that we are processing.
15832 CU is the CU from which the above come. */
15833
15834 static void
15835 mark_common_block_symbol_computed (struct symbol *sym,
15836 struct die_info *common_die,
15837 struct attribute *common_loc,
15838 struct attribute *member_loc,
15839 struct dwarf2_cu *cu)
15840 {
15841 struct dwarf2_per_objfile *dwarf2_per_objfile
15842 = cu->per_cu->dwarf2_per_objfile;
15843 struct objfile *objfile = dwarf2_per_objfile->objfile;
15844 struct dwarf2_locexpr_baton *baton;
15845 gdb_byte *ptr;
15846 unsigned int cu_off;
15847 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
15848 LONGEST offset = 0;
15849
15850 gdb_assert (common_loc && member_loc);
15851 gdb_assert (common_loc->form_is_block ());
15852 gdb_assert (member_loc->form_is_block ()
15853 || member_loc->form_is_constant ());
15854
15855 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
15856 baton->per_cu = cu->per_cu;
15857 gdb_assert (baton->per_cu);
15858
15859 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
15860
15861 if (member_loc->form_is_constant ())
15862 {
15863 offset = dwarf2_get_attr_constant_value (member_loc, 0);
15864 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
15865 }
15866 else
15867 baton->size += DW_BLOCK (member_loc)->size;
15868
15869 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
15870 baton->data = ptr;
15871
15872 *ptr++ = DW_OP_call4;
15873 cu_off = common_die->sect_off - cu->per_cu->sect_off;
15874 store_unsigned_integer (ptr, 4, byte_order, cu_off);
15875 ptr += 4;
15876
15877 if (member_loc->form_is_constant ())
15878 {
15879 *ptr++ = DW_OP_addr;
15880 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
15881 ptr += cu->header.addr_size;
15882 }
15883 else
15884 {
15885 /* We have to copy the data here, because DW_OP_call4 will only
15886 use a DW_AT_location attribute. */
15887 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
15888 ptr += DW_BLOCK (member_loc)->size;
15889 }
15890
15891 *ptr++ = DW_OP_plus;
15892 gdb_assert (ptr - baton->data == baton->size);
15893
15894 SYMBOL_LOCATION_BATON (sym) = baton;
15895 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
15896 }
15897
15898 /* Create appropriate locally-scoped variables for all the
15899 DW_TAG_common_block entries. Also create a struct common_block
15900 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
15901 is used to separate the common blocks name namespace from regular
15902 variable names. */
15903
15904 static void
15905 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
15906 {
15907 struct attribute *attr;
15908
15909 attr = dwarf2_attr (die, DW_AT_location, cu);
15910 if (attr != nullptr)
15911 {
15912 /* Support the .debug_loc offsets. */
15913 if (attr->form_is_block ())
15914 {
15915 /* Ok. */
15916 }
15917 else if (attr->form_is_section_offset ())
15918 {
15919 dwarf2_complex_location_expr_complaint ();
15920 attr = NULL;
15921 }
15922 else
15923 {
15924 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
15925 "common block member");
15926 attr = NULL;
15927 }
15928 }
15929
15930 if (die->child != NULL)
15931 {
15932 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15933 struct die_info *child_die;
15934 size_t n_entries = 0, size;
15935 struct common_block *common_block;
15936 struct symbol *sym;
15937
15938 for (child_die = die->child;
15939 child_die && child_die->tag;
15940 child_die = child_die->sibling)
15941 ++n_entries;
15942
15943 size = (sizeof (struct common_block)
15944 + (n_entries - 1) * sizeof (struct symbol *));
15945 common_block
15946 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
15947 size);
15948 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
15949 common_block->n_entries = 0;
15950
15951 for (child_die = die->child;
15952 child_die && child_die->tag;
15953 child_die = child_die->sibling)
15954 {
15955 /* Create the symbol in the DW_TAG_common_block block in the current
15956 symbol scope. */
15957 sym = new_symbol (child_die, NULL, cu);
15958 if (sym != NULL)
15959 {
15960 struct attribute *member_loc;
15961
15962 common_block->contents[common_block->n_entries++] = sym;
15963
15964 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
15965 cu);
15966 if (member_loc)
15967 {
15968 /* GDB has handled this for a long time, but it is
15969 not specified by DWARF. It seems to have been
15970 emitted by gfortran at least as recently as:
15971 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
15972 complaint (_("Variable in common block has "
15973 "DW_AT_data_member_location "
15974 "- DIE at %s [in module %s]"),
15975 sect_offset_str (child_die->sect_off),
15976 objfile_name (objfile));
15977
15978 if (member_loc->form_is_section_offset ())
15979 dwarf2_complex_location_expr_complaint ();
15980 else if (member_loc->form_is_constant ()
15981 || member_loc->form_is_block ())
15982 {
15983 if (attr != nullptr)
15984 mark_common_block_symbol_computed (sym, die, attr,
15985 member_loc, cu);
15986 }
15987 else
15988 dwarf2_complex_location_expr_complaint ();
15989 }
15990 }
15991 }
15992
15993 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
15994 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
15995 }
15996 }
15997
15998 /* Create a type for a C++ namespace. */
15999
16000 static struct type *
16001 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16002 {
16003 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16004 const char *previous_prefix, *name;
16005 int is_anonymous;
16006 struct type *type;
16007
16008 /* For extensions, reuse the type of the original namespace. */
16009 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16010 {
16011 struct die_info *ext_die;
16012 struct dwarf2_cu *ext_cu = cu;
16013
16014 ext_die = dwarf2_extension (die, &ext_cu);
16015 type = read_type_die (ext_die, ext_cu);
16016
16017 /* EXT_CU may not be the same as CU.
16018 Ensure TYPE is recorded with CU in die_type_hash. */
16019 return set_die_type (die, type, cu);
16020 }
16021
16022 name = namespace_name (die, &is_anonymous, cu);
16023
16024 /* Now build the name of the current namespace. */
16025
16026 previous_prefix = determine_prefix (die, cu);
16027 if (previous_prefix[0] != '\0')
16028 name = typename_concat (&objfile->objfile_obstack,
16029 previous_prefix, name, 0, cu);
16030
16031 /* Create the type. */
16032 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
16033
16034 return set_die_type (die, type, cu);
16035 }
16036
16037 /* Read a namespace scope. */
16038
16039 static void
16040 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16041 {
16042 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16043 int is_anonymous;
16044
16045 /* Add a symbol associated to this if we haven't seen the namespace
16046 before. Also, add a using directive if it's an anonymous
16047 namespace. */
16048
16049 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
16050 {
16051 struct type *type;
16052
16053 type = read_type_die (die, cu);
16054 new_symbol (die, type, cu);
16055
16056 namespace_name (die, &is_anonymous, cu);
16057 if (is_anonymous)
16058 {
16059 const char *previous_prefix = determine_prefix (die, cu);
16060
16061 std::vector<const char *> excludes;
16062 add_using_directive (using_directives (cu),
16063 previous_prefix, TYPE_NAME (type), NULL,
16064 NULL, excludes, 0, &objfile->objfile_obstack);
16065 }
16066 }
16067
16068 if (die->child != NULL)
16069 {
16070 struct die_info *child_die = die->child;
16071
16072 while (child_die && child_die->tag)
16073 {
16074 process_die (child_die, cu);
16075 child_die = child_die->sibling;
16076 }
16077 }
16078 }
16079
16080 /* Read a Fortran module as type. This DIE can be only a declaration used for
16081 imported module. Still we need that type as local Fortran "use ... only"
16082 declaration imports depend on the created type in determine_prefix. */
16083
16084 static struct type *
16085 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16086 {
16087 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16088 const char *module_name;
16089 struct type *type;
16090
16091 module_name = dwarf2_name (die, cu);
16092 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
16093
16094 return set_die_type (die, type, cu);
16095 }
16096
16097 /* Read a Fortran module. */
16098
16099 static void
16100 read_module (struct die_info *die, struct dwarf2_cu *cu)
16101 {
16102 struct die_info *child_die = die->child;
16103 struct type *type;
16104
16105 type = read_type_die (die, cu);
16106 new_symbol (die, type, cu);
16107
16108 while (child_die && child_die->tag)
16109 {
16110 process_die (child_die, cu);
16111 child_die = child_die->sibling;
16112 }
16113 }
16114
16115 /* Return the name of the namespace represented by DIE. Set
16116 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16117 namespace. */
16118
16119 static const char *
16120 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
16121 {
16122 struct die_info *current_die;
16123 const char *name = NULL;
16124
16125 /* Loop through the extensions until we find a name. */
16126
16127 for (current_die = die;
16128 current_die != NULL;
16129 current_die = dwarf2_extension (die, &cu))
16130 {
16131 /* We don't use dwarf2_name here so that we can detect the absence
16132 of a name -> anonymous namespace. */
16133 name = dwarf2_string_attr (die, DW_AT_name, cu);
16134
16135 if (name != NULL)
16136 break;
16137 }
16138
16139 /* Is it an anonymous namespace? */
16140
16141 *is_anonymous = (name == NULL);
16142 if (*is_anonymous)
16143 name = CP_ANONYMOUS_NAMESPACE_STR;
16144
16145 return name;
16146 }
16147
16148 /* Extract all information from a DW_TAG_pointer_type DIE and add to
16149 the user defined type vector. */
16150
16151 static struct type *
16152 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
16153 {
16154 struct gdbarch *gdbarch
16155 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
16156 struct comp_unit_head *cu_header = &cu->header;
16157 struct type *type;
16158 struct attribute *attr_byte_size;
16159 struct attribute *attr_address_class;
16160 int byte_size, addr_class;
16161 struct type *target_type;
16162
16163 target_type = die_type (die, cu);
16164
16165 /* The die_type call above may have already set the type for this DIE. */
16166 type = get_die_type (die, cu);
16167 if (type)
16168 return type;
16169
16170 type = lookup_pointer_type (target_type);
16171
16172 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
16173 if (attr_byte_size)
16174 byte_size = DW_UNSND (attr_byte_size);
16175 else
16176 byte_size = cu_header->addr_size;
16177
16178 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
16179 if (attr_address_class)
16180 addr_class = DW_UNSND (attr_address_class);
16181 else
16182 addr_class = DW_ADDR_none;
16183
16184 ULONGEST alignment = get_alignment (cu, die);
16185
16186 /* If the pointer size, alignment, or address class is different
16187 than the default, create a type variant marked as such and set
16188 the length accordingly. */
16189 if (TYPE_LENGTH (type) != byte_size
16190 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16191 && alignment != TYPE_RAW_ALIGN (type))
16192 || addr_class != DW_ADDR_none)
16193 {
16194 if (gdbarch_address_class_type_flags_p (gdbarch))
16195 {
16196 int type_flags;
16197
16198 type_flags = gdbarch_address_class_type_flags
16199 (gdbarch, byte_size, addr_class);
16200 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16201 == 0);
16202 type = make_type_with_address_space (type, type_flags);
16203 }
16204 else if (TYPE_LENGTH (type) != byte_size)
16205 {
16206 complaint (_("invalid pointer size %d"), byte_size);
16207 }
16208 else if (TYPE_RAW_ALIGN (type) != alignment)
16209 {
16210 complaint (_("Invalid DW_AT_alignment"
16211 " - DIE at %s [in module %s]"),
16212 sect_offset_str (die->sect_off),
16213 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16214 }
16215 else
16216 {
16217 /* Should we also complain about unhandled address classes? */
16218 }
16219 }
16220
16221 TYPE_LENGTH (type) = byte_size;
16222 set_type_align (type, alignment);
16223 return set_die_type (die, type, cu);
16224 }
16225
16226 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16227 the user defined type vector. */
16228
16229 static struct type *
16230 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
16231 {
16232 struct type *type;
16233 struct type *to_type;
16234 struct type *domain;
16235
16236 to_type = die_type (die, cu);
16237 domain = die_containing_type (die, cu);
16238
16239 /* The calls above may have already set the type for this DIE. */
16240 type = get_die_type (die, cu);
16241 if (type)
16242 return type;
16243
16244 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
16245 type = lookup_methodptr_type (to_type);
16246 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
16247 {
16248 struct type *new_type
16249 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
16250
16251 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
16252 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
16253 TYPE_VARARGS (to_type));
16254 type = lookup_methodptr_type (new_type);
16255 }
16256 else
16257 type = lookup_memberptr_type (to_type, domain);
16258
16259 return set_die_type (die, type, cu);
16260 }
16261
16262 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
16263 the user defined type vector. */
16264
16265 static struct type *
16266 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16267 enum type_code refcode)
16268 {
16269 struct comp_unit_head *cu_header = &cu->header;
16270 struct type *type, *target_type;
16271 struct attribute *attr;
16272
16273 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16274
16275 target_type = die_type (die, cu);
16276
16277 /* The die_type call above may have already set the type for this DIE. */
16278 type = get_die_type (die, cu);
16279 if (type)
16280 return type;
16281
16282 type = lookup_reference_type (target_type, refcode);
16283 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16284 if (attr != nullptr)
16285 {
16286 TYPE_LENGTH (type) = DW_UNSND (attr);
16287 }
16288 else
16289 {
16290 TYPE_LENGTH (type) = cu_header->addr_size;
16291 }
16292 maybe_set_alignment (cu, die, type);
16293 return set_die_type (die, type, cu);
16294 }
16295
16296 /* Add the given cv-qualifiers to the element type of the array. GCC
16297 outputs DWARF type qualifiers that apply to an array, not the
16298 element type. But GDB relies on the array element type to carry
16299 the cv-qualifiers. This mimics section 6.7.3 of the C99
16300 specification. */
16301
16302 static struct type *
16303 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16304 struct type *base_type, int cnst, int voltl)
16305 {
16306 struct type *el_type, *inner_array;
16307
16308 base_type = copy_type (base_type);
16309 inner_array = base_type;
16310
16311 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
16312 {
16313 TYPE_TARGET_TYPE (inner_array) =
16314 copy_type (TYPE_TARGET_TYPE (inner_array));
16315 inner_array = TYPE_TARGET_TYPE (inner_array);
16316 }
16317
16318 el_type = TYPE_TARGET_TYPE (inner_array);
16319 cnst |= TYPE_CONST (el_type);
16320 voltl |= TYPE_VOLATILE (el_type);
16321 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16322
16323 return set_die_type (die, base_type, cu);
16324 }
16325
16326 static struct type *
16327 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
16328 {
16329 struct type *base_type, *cv_type;
16330
16331 base_type = die_type (die, cu);
16332
16333 /* The die_type call above may have already set the type for this DIE. */
16334 cv_type = get_die_type (die, cu);
16335 if (cv_type)
16336 return cv_type;
16337
16338 /* In case the const qualifier is applied to an array type, the element type
16339 is so qualified, not the array type (section 6.7.3 of C99). */
16340 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
16341 return add_array_cv_type (die, cu, base_type, 1, 0);
16342
16343 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16344 return set_die_type (die, cv_type, cu);
16345 }
16346
16347 static struct type *
16348 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
16349 {
16350 struct type *base_type, *cv_type;
16351
16352 base_type = die_type (die, cu);
16353
16354 /* The die_type call above may have already set the type for this DIE. */
16355 cv_type = get_die_type (die, cu);
16356 if (cv_type)
16357 return cv_type;
16358
16359 /* In case the volatile qualifier is applied to an array type, the
16360 element type is so qualified, not the array type (section 6.7.3
16361 of C99). */
16362 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
16363 return add_array_cv_type (die, cu, base_type, 0, 1);
16364
16365 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
16366 return set_die_type (die, cv_type, cu);
16367 }
16368
16369 /* Handle DW_TAG_restrict_type. */
16370
16371 static struct type *
16372 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
16373 {
16374 struct type *base_type, *cv_type;
16375
16376 base_type = die_type (die, cu);
16377
16378 /* The die_type call above may have already set the type for this DIE. */
16379 cv_type = get_die_type (die, cu);
16380 if (cv_type)
16381 return cv_type;
16382
16383 cv_type = make_restrict_type (base_type);
16384 return set_die_type (die, cv_type, cu);
16385 }
16386
16387 /* Handle DW_TAG_atomic_type. */
16388
16389 static struct type *
16390 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
16391 {
16392 struct type *base_type, *cv_type;
16393
16394 base_type = die_type (die, cu);
16395
16396 /* The die_type call above may have already set the type for this DIE. */
16397 cv_type = get_die_type (die, cu);
16398 if (cv_type)
16399 return cv_type;
16400
16401 cv_type = make_atomic_type (base_type);
16402 return set_die_type (die, cv_type, cu);
16403 }
16404
16405 /* Extract all information from a DW_TAG_string_type DIE and add to
16406 the user defined type vector. It isn't really a user defined type,
16407 but it behaves like one, with other DIE's using an AT_user_def_type
16408 attribute to reference it. */
16409
16410 static struct type *
16411 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
16412 {
16413 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16414 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16415 struct type *type, *range_type, *index_type, *char_type;
16416 struct attribute *attr;
16417 struct dynamic_prop prop;
16418 bool length_is_constant = true;
16419 LONGEST length;
16420
16421 /* There are a couple of places where bit sizes might be made use of
16422 when parsing a DW_TAG_string_type, however, no producer that we know
16423 of make use of these. Handling bit sizes that are a multiple of the
16424 byte size is easy enough, but what about other bit sizes? Lets deal
16425 with that problem when we have to. Warn about these attributes being
16426 unsupported, then parse the type and ignore them like we always
16427 have. */
16428 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
16429 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
16430 {
16431 static bool warning_printed = false;
16432 if (!warning_printed)
16433 {
16434 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
16435 "currently supported on DW_TAG_string_type."));
16436 warning_printed = true;
16437 }
16438 }
16439
16440 attr = dwarf2_attr (die, DW_AT_string_length, cu);
16441 if (attr != nullptr && !attr->form_is_constant ())
16442 {
16443 /* The string length describes the location at which the length of
16444 the string can be found. The size of the length field can be
16445 specified with one of the attributes below. */
16446 struct type *prop_type;
16447 struct attribute *len
16448 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
16449 if (len == nullptr)
16450 len = dwarf2_attr (die, DW_AT_byte_size, cu);
16451 if (len != nullptr && len->form_is_constant ())
16452 {
16453 /* Pass 0 as the default as we know this attribute is constant
16454 and the default value will not be returned. */
16455 LONGEST sz = dwarf2_get_attr_constant_value (len, 0);
16456 prop_type = cu->per_cu->int_type (sz, true);
16457 }
16458 else
16459 {
16460 /* If the size is not specified then we assume it is the size of
16461 an address on this target. */
16462 prop_type = cu->per_cu->addr_sized_int_type (true);
16463 }
16464
16465 /* Convert the attribute into a dynamic property. */
16466 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
16467 length = 1;
16468 else
16469 length_is_constant = false;
16470 }
16471 else if (attr != nullptr)
16472 {
16473 /* This DW_AT_string_length just contains the length with no
16474 indirection. There's no need to create a dynamic property in this
16475 case. Pass 0 for the default value as we know it will not be
16476 returned in this case. */
16477 length = dwarf2_get_attr_constant_value (attr, 0);
16478 }
16479 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
16480 {
16481 /* We don't currently support non-constant byte sizes for strings. */
16482 length = dwarf2_get_attr_constant_value (attr, 1);
16483 }
16484 else
16485 {
16486 /* Use 1 as a fallback length if we have nothing else. */
16487 length = 1;
16488 }
16489
16490 index_type = objfile_type (objfile)->builtin_int;
16491 if (length_is_constant)
16492 range_type = create_static_range_type (NULL, index_type, 1, length);
16493 else
16494 {
16495 struct dynamic_prop low_bound;
16496
16497 low_bound.kind = PROP_CONST;
16498 low_bound.data.const_val = 1;
16499 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
16500 }
16501 char_type = language_string_char_type (cu->language_defn, gdbarch);
16502 type = create_string_type (NULL, char_type, range_type);
16503
16504 return set_die_type (die, type, cu);
16505 }
16506
16507 /* Assuming that DIE corresponds to a function, returns nonzero
16508 if the function is prototyped. */
16509
16510 static int
16511 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
16512 {
16513 struct attribute *attr;
16514
16515 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
16516 if (attr && (DW_UNSND (attr) != 0))
16517 return 1;
16518
16519 /* The DWARF standard implies that the DW_AT_prototyped attribute
16520 is only meaningful for C, but the concept also extends to other
16521 languages that allow unprototyped functions (Eg: Objective C).
16522 For all other languages, assume that functions are always
16523 prototyped. */
16524 if (cu->language != language_c
16525 && cu->language != language_objc
16526 && cu->language != language_opencl)
16527 return 1;
16528
16529 /* RealView does not emit DW_AT_prototyped. We can not distinguish
16530 prototyped and unprototyped functions; default to prototyped,
16531 since that is more common in modern code (and RealView warns
16532 about unprototyped functions). */
16533 if (producer_is_realview (cu->producer))
16534 return 1;
16535
16536 return 0;
16537 }
16538
16539 /* Handle DIES due to C code like:
16540
16541 struct foo
16542 {
16543 int (*funcp)(int a, long l);
16544 int b;
16545 };
16546
16547 ('funcp' generates a DW_TAG_subroutine_type DIE). */
16548
16549 static struct type *
16550 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
16551 {
16552 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16553 struct type *type; /* Type that this function returns. */
16554 struct type *ftype; /* Function that returns above type. */
16555 struct attribute *attr;
16556
16557 type = die_type (die, cu);
16558
16559 /* The die_type call above may have already set the type for this DIE. */
16560 ftype = get_die_type (die, cu);
16561 if (ftype)
16562 return ftype;
16563
16564 ftype = lookup_function_type (type);
16565
16566 if (prototyped_function_p (die, cu))
16567 TYPE_PROTOTYPED (ftype) = 1;
16568
16569 /* Store the calling convention in the type if it's available in
16570 the subroutine die. Otherwise set the calling convention to
16571 the default value DW_CC_normal. */
16572 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
16573 if (attr != nullptr
16574 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
16575 TYPE_CALLING_CONVENTION (ftype)
16576 = (enum dwarf_calling_convention) (DW_UNSND (attr));
16577 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
16578 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
16579 else
16580 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
16581
16582 /* Record whether the function returns normally to its caller or not
16583 if the DWARF producer set that information. */
16584 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
16585 if (attr && (DW_UNSND (attr) != 0))
16586 TYPE_NO_RETURN (ftype) = 1;
16587
16588 /* We need to add the subroutine type to the die immediately so
16589 we don't infinitely recurse when dealing with parameters
16590 declared as the same subroutine type. */
16591 set_die_type (die, ftype, cu);
16592
16593 if (die->child != NULL)
16594 {
16595 struct type *void_type = objfile_type (objfile)->builtin_void;
16596 struct die_info *child_die;
16597 int nparams, iparams;
16598
16599 /* Count the number of parameters.
16600 FIXME: GDB currently ignores vararg functions, but knows about
16601 vararg member functions. */
16602 nparams = 0;
16603 child_die = die->child;
16604 while (child_die && child_die->tag)
16605 {
16606 if (child_die->tag == DW_TAG_formal_parameter)
16607 nparams++;
16608 else if (child_die->tag == DW_TAG_unspecified_parameters)
16609 TYPE_VARARGS (ftype) = 1;
16610 child_die = child_die->sibling;
16611 }
16612
16613 /* Allocate storage for parameters and fill them in. */
16614 TYPE_NFIELDS (ftype) = nparams;
16615 TYPE_FIELDS (ftype) = (struct field *)
16616 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
16617
16618 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
16619 even if we error out during the parameters reading below. */
16620 for (iparams = 0; iparams < nparams; iparams++)
16621 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
16622
16623 iparams = 0;
16624 child_die = die->child;
16625 while (child_die && child_die->tag)
16626 {
16627 if (child_die->tag == DW_TAG_formal_parameter)
16628 {
16629 struct type *arg_type;
16630
16631 /* DWARF version 2 has no clean way to discern C++
16632 static and non-static member functions. G++ helps
16633 GDB by marking the first parameter for non-static
16634 member functions (which is the this pointer) as
16635 artificial. We pass this information to
16636 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
16637
16638 DWARF version 3 added DW_AT_object_pointer, which GCC
16639 4.5 does not yet generate. */
16640 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
16641 if (attr != nullptr)
16642 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
16643 else
16644 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
16645 arg_type = die_type (child_die, cu);
16646
16647 /* RealView does not mark THIS as const, which the testsuite
16648 expects. GCC marks THIS as const in method definitions,
16649 but not in the class specifications (GCC PR 43053). */
16650 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
16651 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
16652 {
16653 int is_this = 0;
16654 struct dwarf2_cu *arg_cu = cu;
16655 const char *name = dwarf2_name (child_die, cu);
16656
16657 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
16658 if (attr != nullptr)
16659 {
16660 /* If the compiler emits this, use it. */
16661 if (follow_die_ref (die, attr, &arg_cu) == child_die)
16662 is_this = 1;
16663 }
16664 else if (name && strcmp (name, "this") == 0)
16665 /* Function definitions will have the argument names. */
16666 is_this = 1;
16667 else if (name == NULL && iparams == 0)
16668 /* Declarations may not have the names, so like
16669 elsewhere in GDB, assume an artificial first
16670 argument is "this". */
16671 is_this = 1;
16672
16673 if (is_this)
16674 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
16675 arg_type, 0);
16676 }
16677
16678 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
16679 iparams++;
16680 }
16681 child_die = child_die->sibling;
16682 }
16683 }
16684
16685 return ftype;
16686 }
16687
16688 static struct type *
16689 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
16690 {
16691 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16692 const char *name = NULL;
16693 struct type *this_type, *target_type;
16694
16695 name = dwarf2_full_name (NULL, die, cu);
16696 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
16697 TYPE_TARGET_STUB (this_type) = 1;
16698 set_die_type (die, this_type, cu);
16699 target_type = die_type (die, cu);
16700 if (target_type != this_type)
16701 TYPE_TARGET_TYPE (this_type) = target_type;
16702 else
16703 {
16704 /* Self-referential typedefs are, it seems, not allowed by the DWARF
16705 spec and cause infinite loops in GDB. */
16706 complaint (_("Self-referential DW_TAG_typedef "
16707 "- DIE at %s [in module %s]"),
16708 sect_offset_str (die->sect_off), objfile_name (objfile));
16709 TYPE_TARGET_TYPE (this_type) = NULL;
16710 }
16711 if (name == NULL)
16712 {
16713 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
16714 anonymous typedefs, which is, strictly speaking, invalid DWARF.
16715 Handle these by just returning the target type, rather than
16716 constructing an anonymous typedef type and trying to handle this
16717 elsewhere. */
16718 set_die_type (die, target_type, cu);
16719 return target_type;
16720 }
16721 return this_type;
16722 }
16723
16724 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
16725 (which may be different from NAME) to the architecture back-end to allow
16726 it to guess the correct format if necessary. */
16727
16728 static struct type *
16729 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
16730 const char *name_hint, enum bfd_endian byte_order)
16731 {
16732 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16733 const struct floatformat **format;
16734 struct type *type;
16735
16736 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
16737 if (format)
16738 type = init_float_type (objfile, bits, name, format, byte_order);
16739 else
16740 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
16741
16742 return type;
16743 }
16744
16745 /* Allocate an integer type of size BITS and name NAME. */
16746
16747 static struct type *
16748 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
16749 int bits, int unsigned_p, const char *name)
16750 {
16751 struct type *type;
16752
16753 /* Versions of Intel's C Compiler generate an integer type called "void"
16754 instead of using DW_TAG_unspecified_type. This has been seen on
16755 at least versions 14, 17, and 18. */
16756 if (bits == 0 && producer_is_icc (cu) && name != nullptr
16757 && strcmp (name, "void") == 0)
16758 type = objfile_type (objfile)->builtin_void;
16759 else
16760 type = init_integer_type (objfile, bits, unsigned_p, name);
16761
16762 return type;
16763 }
16764
16765 /* Initialise and return a floating point type of size BITS suitable for
16766 use as a component of a complex number. The NAME_HINT is passed through
16767 when initialising the floating point type and is the name of the complex
16768 type.
16769
16770 As DWARF doesn't currently provide an explicit name for the components
16771 of a complex number, but it can be helpful to have these components
16772 named, we try to select a suitable name based on the size of the
16773 component. */
16774 static struct type *
16775 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
16776 struct objfile *objfile,
16777 int bits, const char *name_hint,
16778 enum bfd_endian byte_order)
16779 {
16780 gdbarch *gdbarch = get_objfile_arch (objfile);
16781 struct type *tt = nullptr;
16782
16783 /* Try to find a suitable floating point builtin type of size BITS.
16784 We're going to use the name of this type as the name for the complex
16785 target type that we are about to create. */
16786 switch (cu->language)
16787 {
16788 case language_fortran:
16789 switch (bits)
16790 {
16791 case 32:
16792 tt = builtin_f_type (gdbarch)->builtin_real;
16793 break;
16794 case 64:
16795 tt = builtin_f_type (gdbarch)->builtin_real_s8;
16796 break;
16797 case 96: /* The x86-32 ABI specifies 96-bit long double. */
16798 case 128:
16799 tt = builtin_f_type (gdbarch)->builtin_real_s16;
16800 break;
16801 }
16802 break;
16803 default:
16804 switch (bits)
16805 {
16806 case 32:
16807 tt = builtin_type (gdbarch)->builtin_float;
16808 break;
16809 case 64:
16810 tt = builtin_type (gdbarch)->builtin_double;
16811 break;
16812 case 96: /* The x86-32 ABI specifies 96-bit long double. */
16813 case 128:
16814 tt = builtin_type (gdbarch)->builtin_long_double;
16815 break;
16816 }
16817 break;
16818 }
16819
16820 /* If the type we found doesn't match the size we were looking for, then
16821 pretend we didn't find a type at all, the complex target type we
16822 create will then be nameless. */
16823 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
16824 tt = nullptr;
16825
16826 const char *name = (tt == nullptr) ? nullptr : TYPE_NAME (tt);
16827 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
16828 }
16829
16830 /* Find a representation of a given base type and install
16831 it in the TYPE field of the die. */
16832
16833 static struct type *
16834 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
16835 {
16836 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16837 struct type *type;
16838 struct attribute *attr;
16839 int encoding = 0, bits = 0;
16840 const char *name;
16841 gdbarch *arch;
16842
16843 attr = dwarf2_attr (die, DW_AT_encoding, cu);
16844 if (attr != nullptr)
16845 encoding = DW_UNSND (attr);
16846 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16847 if (attr != nullptr)
16848 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
16849 name = dwarf2_name (die, cu);
16850 if (!name)
16851 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
16852
16853 arch = get_objfile_arch (objfile);
16854 enum bfd_endian byte_order = gdbarch_byte_order (arch);
16855
16856 attr = dwarf2_attr (die, DW_AT_endianity, cu);
16857 if (attr)
16858 {
16859 int endianity = DW_UNSND (attr);
16860
16861 switch (endianity)
16862 {
16863 case DW_END_big:
16864 byte_order = BFD_ENDIAN_BIG;
16865 break;
16866 case DW_END_little:
16867 byte_order = BFD_ENDIAN_LITTLE;
16868 break;
16869 default:
16870 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
16871 break;
16872 }
16873 }
16874
16875 switch (encoding)
16876 {
16877 case DW_ATE_address:
16878 /* Turn DW_ATE_address into a void * pointer. */
16879 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
16880 type = init_pointer_type (objfile, bits, name, type);
16881 break;
16882 case DW_ATE_boolean:
16883 type = init_boolean_type (objfile, bits, 1, name);
16884 break;
16885 case DW_ATE_complex_float:
16886 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
16887 byte_order);
16888 type = init_complex_type (objfile, name, type);
16889 break;
16890 case DW_ATE_decimal_float:
16891 type = init_decfloat_type (objfile, bits, name);
16892 break;
16893 case DW_ATE_float:
16894 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
16895 break;
16896 case DW_ATE_signed:
16897 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
16898 break;
16899 case DW_ATE_unsigned:
16900 if (cu->language == language_fortran
16901 && name
16902 && startswith (name, "character("))
16903 type = init_character_type (objfile, bits, 1, name);
16904 else
16905 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
16906 break;
16907 case DW_ATE_signed_char:
16908 if (cu->language == language_ada || cu->language == language_m2
16909 || cu->language == language_pascal
16910 || cu->language == language_fortran)
16911 type = init_character_type (objfile, bits, 0, name);
16912 else
16913 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
16914 break;
16915 case DW_ATE_unsigned_char:
16916 if (cu->language == language_ada || cu->language == language_m2
16917 || cu->language == language_pascal
16918 || cu->language == language_fortran
16919 || cu->language == language_rust)
16920 type = init_character_type (objfile, bits, 1, name);
16921 else
16922 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
16923 break;
16924 case DW_ATE_UTF:
16925 {
16926 if (bits == 16)
16927 type = builtin_type (arch)->builtin_char16;
16928 else if (bits == 32)
16929 type = builtin_type (arch)->builtin_char32;
16930 else
16931 {
16932 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
16933 bits);
16934 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
16935 }
16936 return set_die_type (die, type, cu);
16937 }
16938 break;
16939
16940 default:
16941 complaint (_("unsupported DW_AT_encoding: '%s'"),
16942 dwarf_type_encoding_name (encoding));
16943 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
16944 break;
16945 }
16946
16947 if (name && strcmp (name, "char") == 0)
16948 TYPE_NOSIGN (type) = 1;
16949
16950 maybe_set_alignment (cu, die, type);
16951
16952 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
16953
16954 return set_die_type (die, type, cu);
16955 }
16956
16957 /* Parse dwarf attribute if it's a block, reference or constant and put the
16958 resulting value of the attribute into struct bound_prop.
16959 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
16960
16961 static int
16962 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
16963 struct dwarf2_cu *cu, struct dynamic_prop *prop,
16964 struct type *default_type)
16965 {
16966 struct dwarf2_property_baton *baton;
16967 struct obstack *obstack
16968 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
16969
16970 gdb_assert (default_type != NULL);
16971
16972 if (attr == NULL || prop == NULL)
16973 return 0;
16974
16975 if (attr->form_is_block ())
16976 {
16977 baton = XOBNEW (obstack, struct dwarf2_property_baton);
16978 baton->property_type = default_type;
16979 baton->locexpr.per_cu = cu->per_cu;
16980 baton->locexpr.size = DW_BLOCK (attr)->size;
16981 baton->locexpr.data = DW_BLOCK (attr)->data;
16982 switch (attr->name)
16983 {
16984 case DW_AT_string_length:
16985 baton->locexpr.is_reference = true;
16986 break;
16987 default:
16988 baton->locexpr.is_reference = false;
16989 break;
16990 }
16991 prop->data.baton = baton;
16992 prop->kind = PROP_LOCEXPR;
16993 gdb_assert (prop->data.baton != NULL);
16994 }
16995 else if (attr->form_is_ref ())
16996 {
16997 struct dwarf2_cu *target_cu = cu;
16998 struct die_info *target_die;
16999 struct attribute *target_attr;
17000
17001 target_die = follow_die_ref (die, attr, &target_cu);
17002 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17003 if (target_attr == NULL)
17004 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17005 target_cu);
17006 if (target_attr == NULL)
17007 return 0;
17008
17009 switch (target_attr->name)
17010 {
17011 case DW_AT_location:
17012 if (target_attr->form_is_section_offset ())
17013 {
17014 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17015 baton->property_type = die_type (target_die, target_cu);
17016 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17017 prop->data.baton = baton;
17018 prop->kind = PROP_LOCLIST;
17019 gdb_assert (prop->data.baton != NULL);
17020 }
17021 else if (target_attr->form_is_block ())
17022 {
17023 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17024 baton->property_type = die_type (target_die, target_cu);
17025 baton->locexpr.per_cu = cu->per_cu;
17026 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17027 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17028 baton->locexpr.is_reference = true;
17029 prop->data.baton = baton;
17030 prop->kind = PROP_LOCEXPR;
17031 gdb_assert (prop->data.baton != NULL);
17032 }
17033 else
17034 {
17035 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17036 "dynamic property");
17037 return 0;
17038 }
17039 break;
17040 case DW_AT_data_member_location:
17041 {
17042 LONGEST offset;
17043
17044 if (!handle_data_member_location (target_die, target_cu,
17045 &offset))
17046 return 0;
17047
17048 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17049 baton->property_type = read_type_die (target_die->parent,
17050 target_cu);
17051 baton->offset_info.offset = offset;
17052 baton->offset_info.type = die_type (target_die, target_cu);
17053 prop->data.baton = baton;
17054 prop->kind = PROP_ADDR_OFFSET;
17055 break;
17056 }
17057 }
17058 }
17059 else if (attr->form_is_constant ())
17060 {
17061 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
17062 prop->kind = PROP_CONST;
17063 }
17064 else
17065 {
17066 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17067 dwarf2_name (die, cu));
17068 return 0;
17069 }
17070
17071 return 1;
17072 }
17073
17074 /* See read.h. */
17075
17076 struct type *
17077 dwarf2_per_cu_data::int_type (int size_in_bytes, bool unsigned_p) const
17078 {
17079 struct objfile *objfile = dwarf2_per_objfile->objfile;
17080 struct type *int_type;
17081
17082 /* Helper macro to examine the various builtin types. */
17083 #define TRY_TYPE(F) \
17084 int_type = (unsigned_p \
17085 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17086 : objfile_type (objfile)->builtin_ ## F); \
17087 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
17088 return int_type
17089
17090 TRY_TYPE (char);
17091 TRY_TYPE (short);
17092 TRY_TYPE (int);
17093 TRY_TYPE (long);
17094 TRY_TYPE (long_long);
17095
17096 #undef TRY_TYPE
17097
17098 gdb_assert_not_reached ("unable to find suitable integer type");
17099 }
17100
17101 /* See read.h. */
17102
17103 struct type *
17104 dwarf2_per_cu_data::addr_sized_int_type (bool unsigned_p) const
17105 {
17106 int addr_size = this->addr_size ();
17107 return int_type (addr_size, unsigned_p);
17108 }
17109
17110 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17111 present (which is valid) then compute the default type based on the
17112 compilation units address size. */
17113
17114 static struct type *
17115 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17116 {
17117 struct type *index_type = die_type (die, cu);
17118
17119 /* Dwarf-2 specifications explicitly allows to create subrange types
17120 without specifying a base type.
17121 In that case, the base type must be set to the type of
17122 the lower bound, upper bound or count, in that order, if any of these
17123 three attributes references an object that has a type.
17124 If no base type is found, the Dwarf-2 specifications say that
17125 a signed integer type of size equal to the size of an address should
17126 be used.
17127 For the following C code: `extern char gdb_int [];'
17128 GCC produces an empty range DIE.
17129 FIXME: muller/2010-05-28: Possible references to object for low bound,
17130 high bound or count are not yet handled by this code. */
17131 if (TYPE_CODE (index_type) == TYPE_CODE_VOID)
17132 index_type = cu->per_cu->addr_sized_int_type (false);
17133
17134 return index_type;
17135 }
17136
17137 /* Read the given DW_AT_subrange DIE. */
17138
17139 static struct type *
17140 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17141 {
17142 struct type *base_type, *orig_base_type;
17143 struct type *range_type;
17144 struct attribute *attr;
17145 struct dynamic_prop low, high;
17146 int low_default_is_valid;
17147 int high_bound_is_count = 0;
17148 const char *name;
17149 ULONGEST negative_mask;
17150
17151 orig_base_type = read_subrange_index_type (die, cu);
17152
17153 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17154 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17155 creating the range type, but we use the result of check_typedef
17156 when examining properties of the type. */
17157 base_type = check_typedef (orig_base_type);
17158
17159 /* The die_type call above may have already set the type for this DIE. */
17160 range_type = get_die_type (die, cu);
17161 if (range_type)
17162 return range_type;
17163
17164 low.kind = PROP_CONST;
17165 high.kind = PROP_CONST;
17166 high.data.const_val = 0;
17167
17168 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17169 omitting DW_AT_lower_bound. */
17170 switch (cu->language)
17171 {
17172 case language_c:
17173 case language_cplus:
17174 low.data.const_val = 0;
17175 low_default_is_valid = 1;
17176 break;
17177 case language_fortran:
17178 low.data.const_val = 1;
17179 low_default_is_valid = 1;
17180 break;
17181 case language_d:
17182 case language_objc:
17183 case language_rust:
17184 low.data.const_val = 0;
17185 low_default_is_valid = (cu->header.version >= 4);
17186 break;
17187 case language_ada:
17188 case language_m2:
17189 case language_pascal:
17190 low.data.const_val = 1;
17191 low_default_is_valid = (cu->header.version >= 4);
17192 break;
17193 default:
17194 low.data.const_val = 0;
17195 low_default_is_valid = 0;
17196 break;
17197 }
17198
17199 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
17200 if (attr != nullptr)
17201 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
17202 else if (!low_default_is_valid)
17203 complaint (_("Missing DW_AT_lower_bound "
17204 "- DIE at %s [in module %s]"),
17205 sect_offset_str (die->sect_off),
17206 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17207
17208 struct attribute *attr_ub, *attr_count;
17209 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
17210 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17211 {
17212 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
17213 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17214 {
17215 /* If bounds are constant do the final calculation here. */
17216 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17217 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17218 else
17219 high_bound_is_count = 1;
17220 }
17221 else
17222 {
17223 if (attr_ub != NULL)
17224 complaint (_("Unresolved DW_AT_upper_bound "
17225 "- DIE at %s [in module %s]"),
17226 sect_offset_str (die->sect_off),
17227 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17228 if (attr_count != NULL)
17229 complaint (_("Unresolved DW_AT_count "
17230 "- DIE at %s [in module %s]"),
17231 sect_offset_str (die->sect_off),
17232 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17233 }
17234 }
17235
17236 LONGEST bias = 0;
17237 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
17238 if (bias_attr != nullptr && bias_attr->form_is_constant ())
17239 bias = dwarf2_get_attr_constant_value (bias_attr, 0);
17240
17241 /* Normally, the DWARF producers are expected to use a signed
17242 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17243 But this is unfortunately not always the case, as witnessed
17244 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17245 is used instead. To work around that ambiguity, we treat
17246 the bounds as signed, and thus sign-extend their values, when
17247 the base type is signed. */
17248 negative_mask =
17249 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
17250 if (low.kind == PROP_CONST
17251 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17252 low.data.const_val |= negative_mask;
17253 if (high.kind == PROP_CONST
17254 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17255 high.data.const_val |= negative_mask;
17256
17257 /* Check for bit and byte strides. */
17258 struct dynamic_prop byte_stride_prop;
17259 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
17260 if (attr_byte_stride != nullptr)
17261 {
17262 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
17263 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
17264 prop_type);
17265 }
17266
17267 struct dynamic_prop bit_stride_prop;
17268 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
17269 if (attr_bit_stride != nullptr)
17270 {
17271 /* It only makes sense to have either a bit or byte stride. */
17272 if (attr_byte_stride != nullptr)
17273 {
17274 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
17275 "- DIE at %s [in module %s]"),
17276 sect_offset_str (die->sect_off),
17277 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17278 attr_bit_stride = nullptr;
17279 }
17280 else
17281 {
17282 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
17283 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
17284 prop_type);
17285 }
17286 }
17287
17288 if (attr_byte_stride != nullptr
17289 || attr_bit_stride != nullptr)
17290 {
17291 bool byte_stride_p = (attr_byte_stride != nullptr);
17292 struct dynamic_prop *stride
17293 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
17294
17295 range_type
17296 = create_range_type_with_stride (NULL, orig_base_type, &low,
17297 &high, bias, stride, byte_stride_p);
17298 }
17299 else
17300 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
17301
17302 if (high_bound_is_count)
17303 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17304
17305 /* Ada expects an empty array on no boundary attributes. */
17306 if (attr == NULL && cu->language != language_ada)
17307 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
17308
17309 name = dwarf2_name (die, cu);
17310 if (name)
17311 TYPE_NAME (range_type) = name;
17312
17313 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17314 if (attr != nullptr)
17315 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17316
17317 maybe_set_alignment (cu, die, range_type);
17318
17319 set_die_type (die, range_type, cu);
17320
17321 /* set_die_type should be already done. */
17322 set_descriptive_type (range_type, die, cu);
17323
17324 return range_type;
17325 }
17326
17327 static struct type *
17328 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17329 {
17330 struct type *type;
17331
17332 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
17333 NULL);
17334 TYPE_NAME (type) = dwarf2_name (die, cu);
17335
17336 /* In Ada, an unspecified type is typically used when the description
17337 of the type is deferred to a different unit. When encountering
17338 such a type, we treat it as a stub, and try to resolve it later on,
17339 when needed. */
17340 if (cu->language == language_ada)
17341 TYPE_STUB (type) = 1;
17342
17343 return set_die_type (die, type, cu);
17344 }
17345
17346 /* Read a single die and all its descendents. Set the die's sibling
17347 field to NULL; set other fields in the die correctly, and set all
17348 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17349 location of the info_ptr after reading all of those dies. PARENT
17350 is the parent of the die in question. */
17351
17352 static struct die_info *
17353 read_die_and_children (const struct die_reader_specs *reader,
17354 const gdb_byte *info_ptr,
17355 const gdb_byte **new_info_ptr,
17356 struct die_info *parent)
17357 {
17358 struct die_info *die;
17359 const gdb_byte *cur_ptr;
17360
17361 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
17362 if (die == NULL)
17363 {
17364 *new_info_ptr = cur_ptr;
17365 return NULL;
17366 }
17367 store_in_ref_table (die, reader->cu);
17368
17369 if (die->has_children)
17370 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
17371 else
17372 {
17373 die->child = NULL;
17374 *new_info_ptr = cur_ptr;
17375 }
17376
17377 die->sibling = NULL;
17378 die->parent = parent;
17379 return die;
17380 }
17381
17382 /* Read a die, all of its descendents, and all of its siblings; set
17383 all of the fields of all of the dies correctly. Arguments are as
17384 in read_die_and_children. */
17385
17386 static struct die_info *
17387 read_die_and_siblings_1 (const struct die_reader_specs *reader,
17388 const gdb_byte *info_ptr,
17389 const gdb_byte **new_info_ptr,
17390 struct die_info *parent)
17391 {
17392 struct die_info *first_die, *last_sibling;
17393 const gdb_byte *cur_ptr;
17394
17395 cur_ptr = info_ptr;
17396 first_die = last_sibling = NULL;
17397
17398 while (1)
17399 {
17400 struct die_info *die
17401 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
17402
17403 if (die == NULL)
17404 {
17405 *new_info_ptr = cur_ptr;
17406 return first_die;
17407 }
17408
17409 if (!first_die)
17410 first_die = die;
17411 else
17412 last_sibling->sibling = die;
17413
17414 last_sibling = die;
17415 }
17416 }
17417
17418 /* Read a die, all of its descendents, and all of its siblings; set
17419 all of the fields of all of the dies correctly. Arguments are as
17420 in read_die_and_children.
17421 This the main entry point for reading a DIE and all its children. */
17422
17423 static struct die_info *
17424 read_die_and_siblings (const struct die_reader_specs *reader,
17425 const gdb_byte *info_ptr,
17426 const gdb_byte **new_info_ptr,
17427 struct die_info *parent)
17428 {
17429 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17430 new_info_ptr, parent);
17431
17432 if (dwarf_die_debug)
17433 {
17434 fprintf_unfiltered (gdb_stdlog,
17435 "Read die from %s@0x%x of %s:\n",
17436 reader->die_section->get_name (),
17437 (unsigned) (info_ptr - reader->die_section->buffer),
17438 bfd_get_filename (reader->abfd));
17439 dump_die (die, dwarf_die_debug);
17440 }
17441
17442 return die;
17443 }
17444
17445 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17446 attributes.
17447 The caller is responsible for filling in the extra attributes
17448 and updating (*DIEP)->num_attrs.
17449 Set DIEP to point to a newly allocated die with its information,
17450 except for its child, sibling, and parent fields. */
17451
17452 static const gdb_byte *
17453 read_full_die_1 (const struct die_reader_specs *reader,
17454 struct die_info **diep, const gdb_byte *info_ptr,
17455 int num_extra_attrs)
17456 {
17457 unsigned int abbrev_number, bytes_read, i;
17458 struct abbrev_info *abbrev;
17459 struct die_info *die;
17460 struct dwarf2_cu *cu = reader->cu;
17461 bfd *abfd = reader->abfd;
17462
17463 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
17464 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17465 info_ptr += bytes_read;
17466 if (!abbrev_number)
17467 {
17468 *diep = NULL;
17469 return info_ptr;
17470 }
17471
17472 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
17473 if (!abbrev)
17474 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
17475 abbrev_number,
17476 bfd_get_filename (abfd));
17477
17478 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
17479 die->sect_off = sect_off;
17480 die->tag = abbrev->tag;
17481 die->abbrev = abbrev_number;
17482 die->has_children = abbrev->has_children;
17483
17484 /* Make the result usable.
17485 The caller needs to update num_attrs after adding the extra
17486 attributes. */
17487 die->num_attrs = abbrev->num_attrs;
17488
17489 std::vector<int> indexes_that_need_reprocess;
17490 for (i = 0; i < abbrev->num_attrs; ++i)
17491 {
17492 bool need_reprocess;
17493 info_ptr =
17494 read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
17495 info_ptr, &need_reprocess);
17496 if (need_reprocess)
17497 indexes_that_need_reprocess.push_back (i);
17498 }
17499
17500 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
17501 if (attr != nullptr)
17502 cu->str_offsets_base = DW_UNSND (attr);
17503
17504 auto maybe_addr_base = die->addr_base ();
17505 if (maybe_addr_base.has_value ())
17506 cu->addr_base = *maybe_addr_base;
17507 for (int index : indexes_that_need_reprocess)
17508 read_attribute_reprocess (reader, &die->attrs[index]);
17509 *diep = die;
17510 return info_ptr;
17511 }
17512
17513 /* Read a die and all its attributes.
17514 Set DIEP to point to a newly allocated die with its information,
17515 except for its child, sibling, and parent fields. */
17516
17517 static const gdb_byte *
17518 read_full_die (const struct die_reader_specs *reader,
17519 struct die_info **diep, const gdb_byte *info_ptr)
17520 {
17521 const gdb_byte *result;
17522
17523 result = read_full_die_1 (reader, diep, info_ptr, 0);
17524
17525 if (dwarf_die_debug)
17526 {
17527 fprintf_unfiltered (gdb_stdlog,
17528 "Read die from %s@0x%x of %s:\n",
17529 reader->die_section->get_name (),
17530 (unsigned) (info_ptr - reader->die_section->buffer),
17531 bfd_get_filename (reader->abfd));
17532 dump_die (*diep, dwarf_die_debug);
17533 }
17534
17535 return result;
17536 }
17537 \f
17538
17539 /* Returns nonzero if TAG represents a type that we might generate a partial
17540 symbol for. */
17541
17542 static int
17543 is_type_tag_for_partial (int tag)
17544 {
17545 switch (tag)
17546 {
17547 #if 0
17548 /* Some types that would be reasonable to generate partial symbols for,
17549 that we don't at present. */
17550 case DW_TAG_array_type:
17551 case DW_TAG_file_type:
17552 case DW_TAG_ptr_to_member_type:
17553 case DW_TAG_set_type:
17554 case DW_TAG_string_type:
17555 case DW_TAG_subroutine_type:
17556 #endif
17557 case DW_TAG_base_type:
17558 case DW_TAG_class_type:
17559 case DW_TAG_interface_type:
17560 case DW_TAG_enumeration_type:
17561 case DW_TAG_structure_type:
17562 case DW_TAG_subrange_type:
17563 case DW_TAG_typedef:
17564 case DW_TAG_union_type:
17565 return 1;
17566 default:
17567 return 0;
17568 }
17569 }
17570
17571 /* Load all DIEs that are interesting for partial symbols into memory. */
17572
17573 static struct partial_die_info *
17574 load_partial_dies (const struct die_reader_specs *reader,
17575 const gdb_byte *info_ptr, int building_psymtab)
17576 {
17577 struct dwarf2_cu *cu = reader->cu;
17578 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17579 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
17580 unsigned int bytes_read;
17581 unsigned int load_all = 0;
17582 int nesting_level = 1;
17583
17584 parent_die = NULL;
17585 last_die = NULL;
17586
17587 gdb_assert (cu->per_cu != NULL);
17588 if (cu->per_cu->load_all_dies)
17589 load_all = 1;
17590
17591 cu->partial_dies
17592 = htab_create_alloc_ex (cu->header.length / 12,
17593 partial_die_hash,
17594 partial_die_eq,
17595 NULL,
17596 &cu->comp_unit_obstack,
17597 hashtab_obstack_allocate,
17598 dummy_obstack_deallocate);
17599
17600 while (1)
17601 {
17602 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
17603
17604 /* A NULL abbrev means the end of a series of children. */
17605 if (abbrev == NULL)
17606 {
17607 if (--nesting_level == 0)
17608 return first_die;
17609
17610 info_ptr += bytes_read;
17611 last_die = parent_die;
17612 parent_die = parent_die->die_parent;
17613 continue;
17614 }
17615
17616 /* Check for template arguments. We never save these; if
17617 they're seen, we just mark the parent, and go on our way. */
17618 if (parent_die != NULL
17619 && cu->language == language_cplus
17620 && (abbrev->tag == DW_TAG_template_type_param
17621 || abbrev->tag == DW_TAG_template_value_param))
17622 {
17623 parent_die->has_template_arguments = 1;
17624
17625 if (!load_all)
17626 {
17627 /* We don't need a partial DIE for the template argument. */
17628 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
17629 continue;
17630 }
17631 }
17632
17633 /* We only recurse into c++ subprograms looking for template arguments.
17634 Skip their other children. */
17635 if (!load_all
17636 && cu->language == language_cplus
17637 && parent_die != NULL
17638 && parent_die->tag == DW_TAG_subprogram)
17639 {
17640 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
17641 continue;
17642 }
17643
17644 /* Check whether this DIE is interesting enough to save. Normally
17645 we would not be interested in members here, but there may be
17646 later variables referencing them via DW_AT_specification (for
17647 static members). */
17648 if (!load_all
17649 && !is_type_tag_for_partial (abbrev->tag)
17650 && abbrev->tag != DW_TAG_constant
17651 && abbrev->tag != DW_TAG_enumerator
17652 && abbrev->tag != DW_TAG_subprogram
17653 && abbrev->tag != DW_TAG_inlined_subroutine
17654 && abbrev->tag != DW_TAG_lexical_block
17655 && abbrev->tag != DW_TAG_variable
17656 && abbrev->tag != DW_TAG_namespace
17657 && abbrev->tag != DW_TAG_module
17658 && abbrev->tag != DW_TAG_member
17659 && abbrev->tag != DW_TAG_imported_unit
17660 && abbrev->tag != DW_TAG_imported_declaration)
17661 {
17662 /* Otherwise we skip to the next sibling, if any. */
17663 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
17664 continue;
17665 }
17666
17667 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
17668 abbrev);
17669
17670 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
17671
17672 /* This two-pass algorithm for processing partial symbols has a
17673 high cost in cache pressure. Thus, handle some simple cases
17674 here which cover the majority of C partial symbols. DIEs
17675 which neither have specification tags in them, nor could have
17676 specification tags elsewhere pointing at them, can simply be
17677 processed and discarded.
17678
17679 This segment is also optional; scan_partial_symbols and
17680 add_partial_symbol will handle these DIEs if we chain
17681 them in normally. When compilers which do not emit large
17682 quantities of duplicate debug information are more common,
17683 this code can probably be removed. */
17684
17685 /* Any complete simple types at the top level (pretty much all
17686 of them, for a language without namespaces), can be processed
17687 directly. */
17688 if (parent_die == NULL
17689 && pdi.has_specification == 0
17690 && pdi.is_declaration == 0
17691 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
17692 || pdi.tag == DW_TAG_base_type
17693 || pdi.tag == DW_TAG_subrange_type))
17694 {
17695 if (building_psymtab && pdi.name != NULL)
17696 add_psymbol_to_list (pdi.name, false,
17697 VAR_DOMAIN, LOC_TYPEDEF, -1,
17698 psymbol_placement::STATIC,
17699 0, cu->language, objfile);
17700 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
17701 continue;
17702 }
17703
17704 /* The exception for DW_TAG_typedef with has_children above is
17705 a workaround of GCC PR debug/47510. In the case of this complaint
17706 type_name_or_error will error on such types later.
17707
17708 GDB skipped children of DW_TAG_typedef by the shortcut above and then
17709 it could not find the child DIEs referenced later, this is checked
17710 above. In correct DWARF DW_TAG_typedef should have no children. */
17711
17712 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
17713 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
17714 "- DIE at %s [in module %s]"),
17715 sect_offset_str (pdi.sect_off), objfile_name (objfile));
17716
17717 /* If we're at the second level, and we're an enumerator, and
17718 our parent has no specification (meaning possibly lives in a
17719 namespace elsewhere), then we can add the partial symbol now
17720 instead of queueing it. */
17721 if (pdi.tag == DW_TAG_enumerator
17722 && parent_die != NULL
17723 && parent_die->die_parent == NULL
17724 && parent_die->tag == DW_TAG_enumeration_type
17725 && parent_die->has_specification == 0)
17726 {
17727 if (pdi.name == NULL)
17728 complaint (_("malformed enumerator DIE ignored"));
17729 else if (building_psymtab)
17730 add_psymbol_to_list (pdi.name, false,
17731 VAR_DOMAIN, LOC_CONST, -1,
17732 cu->language == language_cplus
17733 ? psymbol_placement::GLOBAL
17734 : psymbol_placement::STATIC,
17735 0, cu->language, objfile);
17736
17737 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
17738 continue;
17739 }
17740
17741 struct partial_die_info *part_die
17742 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
17743
17744 /* We'll save this DIE so link it in. */
17745 part_die->die_parent = parent_die;
17746 part_die->die_sibling = NULL;
17747 part_die->die_child = NULL;
17748
17749 if (last_die && last_die == parent_die)
17750 last_die->die_child = part_die;
17751 else if (last_die)
17752 last_die->die_sibling = part_die;
17753
17754 last_die = part_die;
17755
17756 if (first_die == NULL)
17757 first_die = part_die;
17758
17759 /* Maybe add the DIE to the hash table. Not all DIEs that we
17760 find interesting need to be in the hash table, because we
17761 also have the parent/sibling/child chains; only those that we
17762 might refer to by offset later during partial symbol reading.
17763
17764 For now this means things that might have be the target of a
17765 DW_AT_specification, DW_AT_abstract_origin, or
17766 DW_AT_extension. DW_AT_extension will refer only to
17767 namespaces; DW_AT_abstract_origin refers to functions (and
17768 many things under the function DIE, but we do not recurse
17769 into function DIEs during partial symbol reading) and
17770 possibly variables as well; DW_AT_specification refers to
17771 declarations. Declarations ought to have the DW_AT_declaration
17772 flag. It happens that GCC forgets to put it in sometimes, but
17773 only for functions, not for types.
17774
17775 Adding more things than necessary to the hash table is harmless
17776 except for the performance cost. Adding too few will result in
17777 wasted time in find_partial_die, when we reread the compilation
17778 unit with load_all_dies set. */
17779
17780 if (load_all
17781 || abbrev->tag == DW_TAG_constant
17782 || abbrev->tag == DW_TAG_subprogram
17783 || abbrev->tag == DW_TAG_variable
17784 || abbrev->tag == DW_TAG_namespace
17785 || part_die->is_declaration)
17786 {
17787 void **slot;
17788
17789 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
17790 to_underlying (part_die->sect_off),
17791 INSERT);
17792 *slot = part_die;
17793 }
17794
17795 /* For some DIEs we want to follow their children (if any). For C
17796 we have no reason to follow the children of structures; for other
17797 languages we have to, so that we can get at method physnames
17798 to infer fully qualified class names, for DW_AT_specification,
17799 and for C++ template arguments. For C++, we also look one level
17800 inside functions to find template arguments (if the name of the
17801 function does not already contain the template arguments).
17802
17803 For Ada and Fortran, we need to scan the children of subprograms
17804 and lexical blocks as well because these languages allow the
17805 definition of nested entities that could be interesting for the
17806 debugger, such as nested subprograms for instance. */
17807 if (last_die->has_children
17808 && (load_all
17809 || last_die->tag == DW_TAG_namespace
17810 || last_die->tag == DW_TAG_module
17811 || last_die->tag == DW_TAG_enumeration_type
17812 || (cu->language == language_cplus
17813 && last_die->tag == DW_TAG_subprogram
17814 && (last_die->name == NULL
17815 || strchr (last_die->name, '<') == NULL))
17816 || (cu->language != language_c
17817 && (last_die->tag == DW_TAG_class_type
17818 || last_die->tag == DW_TAG_interface_type
17819 || last_die->tag == DW_TAG_structure_type
17820 || last_die->tag == DW_TAG_union_type))
17821 || ((cu->language == language_ada
17822 || cu->language == language_fortran)
17823 && (last_die->tag == DW_TAG_subprogram
17824 || last_die->tag == DW_TAG_lexical_block))))
17825 {
17826 nesting_level++;
17827 parent_die = last_die;
17828 continue;
17829 }
17830
17831 /* Otherwise we skip to the next sibling, if any. */
17832 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
17833
17834 /* Back to the top, do it again. */
17835 }
17836 }
17837
17838 partial_die_info::partial_die_info (sect_offset sect_off_,
17839 struct abbrev_info *abbrev)
17840 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
17841 {
17842 }
17843
17844 /* Read a minimal amount of information into the minimal die structure.
17845 INFO_PTR should point just after the initial uleb128 of a DIE. */
17846
17847 const gdb_byte *
17848 partial_die_info::read (const struct die_reader_specs *reader,
17849 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
17850 {
17851 struct dwarf2_cu *cu = reader->cu;
17852 struct dwarf2_per_objfile *dwarf2_per_objfile
17853 = cu->per_cu->dwarf2_per_objfile;
17854 unsigned int i;
17855 int has_low_pc_attr = 0;
17856 int has_high_pc_attr = 0;
17857 int high_pc_relative = 0;
17858
17859 std::vector<struct attribute> attr_vec (abbrev.num_attrs);
17860 for (i = 0; i < abbrev.num_attrs; ++i)
17861 {
17862 bool need_reprocess;
17863 info_ptr = read_attribute (reader, &attr_vec[i], &abbrev.attrs[i],
17864 info_ptr, &need_reprocess);
17865 /* String and address offsets that need to do the reprocessing have
17866 already been read at this point, so there is no need to wait until
17867 the loop terminates to do the reprocessing. */
17868 if (need_reprocess)
17869 read_attribute_reprocess (reader, &attr_vec[i]);
17870 attribute &attr = attr_vec[i];
17871 /* Store the data if it is of an attribute we want to keep in a
17872 partial symbol table. */
17873 switch (attr.name)
17874 {
17875 case DW_AT_name:
17876 switch (tag)
17877 {
17878 case DW_TAG_compile_unit:
17879 case DW_TAG_partial_unit:
17880 case DW_TAG_type_unit:
17881 /* Compilation units have a DW_AT_name that is a filename, not
17882 a source language identifier. */
17883 case DW_TAG_enumeration_type:
17884 case DW_TAG_enumerator:
17885 /* These tags always have simple identifiers already; no need
17886 to canonicalize them. */
17887 name = DW_STRING (&attr);
17888 break;
17889 default:
17890 {
17891 struct objfile *objfile = dwarf2_per_objfile->objfile;
17892
17893 name
17894 = dwarf2_canonicalize_name (DW_STRING (&attr), cu, objfile);
17895 }
17896 break;
17897 }
17898 break;
17899 case DW_AT_linkage_name:
17900 case DW_AT_MIPS_linkage_name:
17901 /* Note that both forms of linkage name might appear. We
17902 assume they will be the same, and we only store the last
17903 one we see. */
17904 linkage_name = DW_STRING (&attr);
17905 break;
17906 case DW_AT_low_pc:
17907 has_low_pc_attr = 1;
17908 lowpc = attr.value_as_address ();
17909 break;
17910 case DW_AT_high_pc:
17911 has_high_pc_attr = 1;
17912 highpc = attr.value_as_address ();
17913 if (cu->header.version >= 4 && attr.form_is_constant ())
17914 high_pc_relative = 1;
17915 break;
17916 case DW_AT_location:
17917 /* Support the .debug_loc offsets. */
17918 if (attr.form_is_block ())
17919 {
17920 d.locdesc = DW_BLOCK (&attr);
17921 }
17922 else if (attr.form_is_section_offset ())
17923 {
17924 dwarf2_complex_location_expr_complaint ();
17925 }
17926 else
17927 {
17928 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17929 "partial symbol information");
17930 }
17931 break;
17932 case DW_AT_external:
17933 is_external = DW_UNSND (&attr);
17934 break;
17935 case DW_AT_declaration:
17936 is_declaration = DW_UNSND (&attr);
17937 break;
17938 case DW_AT_type:
17939 has_type = 1;
17940 break;
17941 case DW_AT_abstract_origin:
17942 case DW_AT_specification:
17943 case DW_AT_extension:
17944 has_specification = 1;
17945 spec_offset = dwarf2_get_ref_die_offset (&attr);
17946 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
17947 || cu->per_cu->is_dwz);
17948 break;
17949 case DW_AT_sibling:
17950 /* Ignore absolute siblings, they might point outside of
17951 the current compile unit. */
17952 if (attr.form == DW_FORM_ref_addr)
17953 complaint (_("ignoring absolute DW_AT_sibling"));
17954 else
17955 {
17956 const gdb_byte *buffer = reader->buffer;
17957 sect_offset off = dwarf2_get_ref_die_offset (&attr);
17958 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
17959
17960 if (sibling_ptr < info_ptr)
17961 complaint (_("DW_AT_sibling points backwards"));
17962 else if (sibling_ptr > reader->buffer_end)
17963 reader->die_section->overflow_complaint ();
17964 else
17965 sibling = sibling_ptr;
17966 }
17967 break;
17968 case DW_AT_byte_size:
17969 has_byte_size = 1;
17970 break;
17971 case DW_AT_const_value:
17972 has_const_value = 1;
17973 break;
17974 case DW_AT_calling_convention:
17975 /* DWARF doesn't provide a way to identify a program's source-level
17976 entry point. DW_AT_calling_convention attributes are only meant
17977 to describe functions' calling conventions.
17978
17979 However, because it's a necessary piece of information in
17980 Fortran, and before DWARF 4 DW_CC_program was the only
17981 piece of debugging information whose definition refers to
17982 a 'main program' at all, several compilers marked Fortran
17983 main programs with DW_CC_program --- even when those
17984 functions use the standard calling conventions.
17985
17986 Although DWARF now specifies a way to provide this
17987 information, we support this practice for backward
17988 compatibility. */
17989 if (DW_UNSND (&attr) == DW_CC_program
17990 && cu->language == language_fortran)
17991 main_subprogram = 1;
17992 break;
17993 case DW_AT_inline:
17994 if (DW_UNSND (&attr) == DW_INL_inlined
17995 || DW_UNSND (&attr) == DW_INL_declared_inlined)
17996 may_be_inlined = 1;
17997 break;
17998
17999 case DW_AT_import:
18000 if (tag == DW_TAG_imported_unit)
18001 {
18002 d.sect_off = dwarf2_get_ref_die_offset (&attr);
18003 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18004 || cu->per_cu->is_dwz);
18005 }
18006 break;
18007
18008 case DW_AT_main_subprogram:
18009 main_subprogram = DW_UNSND (&attr);
18010 break;
18011
18012 case DW_AT_ranges:
18013 {
18014 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18015 but that requires a full DIE, so instead we just
18016 reimplement it. */
18017 int need_ranges_base = tag != DW_TAG_compile_unit;
18018 unsigned int ranges_offset = (DW_UNSND (&attr)
18019 + (need_ranges_base
18020 ? cu->ranges_base
18021 : 0));
18022
18023 /* Value of the DW_AT_ranges attribute is the offset in the
18024 .debug_ranges section. */
18025 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18026 nullptr))
18027 has_pc_info = 1;
18028 }
18029 break;
18030
18031 default:
18032 break;
18033 }
18034 }
18035
18036 /* For Ada, if both the name and the linkage name appear, we prefer
18037 the latter. This lets "catch exception" work better, regardless
18038 of the order in which the name and linkage name were emitted.
18039 Really, though, this is just a workaround for the fact that gdb
18040 doesn't store both the name and the linkage name. */
18041 if (cu->language == language_ada && linkage_name != nullptr)
18042 name = linkage_name;
18043
18044 if (high_pc_relative)
18045 highpc += lowpc;
18046
18047 if (has_low_pc_attr && has_high_pc_attr)
18048 {
18049 /* When using the GNU linker, .gnu.linkonce. sections are used to
18050 eliminate duplicate copies of functions and vtables and such.
18051 The linker will arbitrarily choose one and discard the others.
18052 The AT_*_pc values for such functions refer to local labels in
18053 these sections. If the section from that file was discarded, the
18054 labels are not in the output, so the relocs get a value of 0.
18055 If this is a discarded function, mark the pc bounds as invalid,
18056 so that GDB will ignore it. */
18057 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
18058 {
18059 struct objfile *objfile = dwarf2_per_objfile->objfile;
18060 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18061
18062 complaint (_("DW_AT_low_pc %s is zero "
18063 "for DIE at %s [in module %s]"),
18064 paddress (gdbarch, lowpc),
18065 sect_offset_str (sect_off),
18066 objfile_name (objfile));
18067 }
18068 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
18069 else if (lowpc >= highpc)
18070 {
18071 struct objfile *objfile = dwarf2_per_objfile->objfile;
18072 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18073
18074 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
18075 "for DIE at %s [in module %s]"),
18076 paddress (gdbarch, lowpc),
18077 paddress (gdbarch, highpc),
18078 sect_offset_str (sect_off),
18079 objfile_name (objfile));
18080 }
18081 else
18082 has_pc_info = 1;
18083 }
18084
18085 return info_ptr;
18086 }
18087
18088 /* Find a cached partial DIE at OFFSET in CU. */
18089
18090 struct partial_die_info *
18091 dwarf2_cu::find_partial_die (sect_offset sect_off)
18092 {
18093 struct partial_die_info *lookup_die = NULL;
18094 struct partial_die_info part_die (sect_off);
18095
18096 lookup_die = ((struct partial_die_info *)
18097 htab_find_with_hash (partial_dies, &part_die,
18098 to_underlying (sect_off)));
18099
18100 return lookup_die;
18101 }
18102
18103 /* Find a partial DIE at OFFSET, which may or may not be in CU,
18104 except in the case of .debug_types DIEs which do not reference
18105 outside their CU (they do however referencing other types via
18106 DW_FORM_ref_sig8). */
18107
18108 static const struct cu_partial_die_info
18109 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
18110 {
18111 struct dwarf2_per_objfile *dwarf2_per_objfile
18112 = cu->per_cu->dwarf2_per_objfile;
18113 struct objfile *objfile = dwarf2_per_objfile->objfile;
18114 struct dwarf2_per_cu_data *per_cu = NULL;
18115 struct partial_die_info *pd = NULL;
18116
18117 if (offset_in_dwz == cu->per_cu->is_dwz
18118 && cu->header.offset_in_cu_p (sect_off))
18119 {
18120 pd = cu->find_partial_die (sect_off);
18121 if (pd != NULL)
18122 return { cu, pd };
18123 /* We missed recording what we needed.
18124 Load all dies and try again. */
18125 per_cu = cu->per_cu;
18126 }
18127 else
18128 {
18129 /* TUs don't reference other CUs/TUs (except via type signatures). */
18130 if (cu->per_cu->is_debug_types)
18131 {
18132 error (_("Dwarf Error: Type Unit at offset %s contains"
18133 " external reference to offset %s [in module %s].\n"),
18134 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
18135 bfd_get_filename (objfile->obfd));
18136 }
18137 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
18138 dwarf2_per_objfile);
18139
18140 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18141 load_partial_comp_unit (per_cu);
18142
18143 per_cu->cu->last_used = 0;
18144 pd = per_cu->cu->find_partial_die (sect_off);
18145 }
18146
18147 /* If we didn't find it, and not all dies have been loaded,
18148 load them all and try again. */
18149
18150 if (pd == NULL && per_cu->load_all_dies == 0)
18151 {
18152 per_cu->load_all_dies = 1;
18153
18154 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18155 THIS_CU->cu may already be in use. So we can't just free it and
18156 replace its DIEs with the ones we read in. Instead, we leave those
18157 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18158 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18159 set. */
18160 load_partial_comp_unit (per_cu);
18161
18162 pd = per_cu->cu->find_partial_die (sect_off);
18163 }
18164
18165 if (pd == NULL)
18166 internal_error (__FILE__, __LINE__,
18167 _("could not find partial DIE %s "
18168 "in cache [from module %s]\n"),
18169 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
18170 return { per_cu->cu, pd };
18171 }
18172
18173 /* See if we can figure out if the class lives in a namespace. We do
18174 this by looking for a member function; its demangled name will
18175 contain namespace info, if there is any. */
18176
18177 static void
18178 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18179 struct dwarf2_cu *cu)
18180 {
18181 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18182 what template types look like, because the demangler
18183 frequently doesn't give the same name as the debug info. We
18184 could fix this by only using the demangled name to get the
18185 prefix (but see comment in read_structure_type). */
18186
18187 struct partial_die_info *real_pdi;
18188 struct partial_die_info *child_pdi;
18189
18190 /* If this DIE (this DIE's specification, if any) has a parent, then
18191 we should not do this. We'll prepend the parent's fully qualified
18192 name when we create the partial symbol. */
18193
18194 real_pdi = struct_pdi;
18195 while (real_pdi->has_specification)
18196 {
18197 auto res = find_partial_die (real_pdi->spec_offset,
18198 real_pdi->spec_is_dwz, cu);
18199 real_pdi = res.pdi;
18200 cu = res.cu;
18201 }
18202
18203 if (real_pdi->die_parent != NULL)
18204 return;
18205
18206 for (child_pdi = struct_pdi->die_child;
18207 child_pdi != NULL;
18208 child_pdi = child_pdi->die_sibling)
18209 {
18210 if (child_pdi->tag == DW_TAG_subprogram
18211 && child_pdi->linkage_name != NULL)
18212 {
18213 gdb::unique_xmalloc_ptr<char> actual_class_name
18214 (language_class_name_from_physname (cu->language_defn,
18215 child_pdi->linkage_name));
18216 if (actual_class_name != NULL)
18217 {
18218 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18219 struct_pdi->name = objfile->intern (actual_class_name.get ());
18220 }
18221 break;
18222 }
18223 }
18224 }
18225
18226 void
18227 partial_die_info::fixup (struct dwarf2_cu *cu)
18228 {
18229 /* Once we've fixed up a die, there's no point in doing so again.
18230 This also avoids a memory leak if we were to call
18231 guess_partial_die_structure_name multiple times. */
18232 if (fixup_called)
18233 return;
18234
18235 /* If we found a reference attribute and the DIE has no name, try
18236 to find a name in the referred to DIE. */
18237
18238 if (name == NULL && has_specification)
18239 {
18240 struct partial_die_info *spec_die;
18241
18242 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18243 spec_die = res.pdi;
18244 cu = res.cu;
18245
18246 spec_die->fixup (cu);
18247
18248 if (spec_die->name)
18249 {
18250 name = spec_die->name;
18251
18252 /* Copy DW_AT_external attribute if it is set. */
18253 if (spec_die->is_external)
18254 is_external = spec_die->is_external;
18255 }
18256 }
18257
18258 /* Set default names for some unnamed DIEs. */
18259
18260 if (name == NULL && tag == DW_TAG_namespace)
18261 name = CP_ANONYMOUS_NAMESPACE_STR;
18262
18263 /* If there is no parent die to provide a namespace, and there are
18264 children, see if we can determine the namespace from their linkage
18265 name. */
18266 if (cu->language == language_cplus
18267 && !cu->per_cu->dwarf2_per_objfile->types.empty ()
18268 && die_parent == NULL
18269 && has_children
18270 && (tag == DW_TAG_class_type
18271 || tag == DW_TAG_structure_type
18272 || tag == DW_TAG_union_type))
18273 guess_partial_die_structure_name (this, cu);
18274
18275 /* GCC might emit a nameless struct or union that has a linkage
18276 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
18277 if (name == NULL
18278 && (tag == DW_TAG_class_type
18279 || tag == DW_TAG_interface_type
18280 || tag == DW_TAG_structure_type
18281 || tag == DW_TAG_union_type)
18282 && linkage_name != NULL)
18283 {
18284 gdb::unique_xmalloc_ptr<char> demangled
18285 (gdb_demangle (linkage_name, DMGL_TYPES));
18286 if (demangled != nullptr)
18287 {
18288 const char *base;
18289
18290 /* Strip any leading namespaces/classes, keep only the base name.
18291 DW_AT_name for named DIEs does not contain the prefixes. */
18292 base = strrchr (demangled.get (), ':');
18293 if (base && base > demangled.get () && base[-1] == ':')
18294 base++;
18295 else
18296 base = demangled.get ();
18297
18298 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18299 name = objfile->intern (base);
18300 }
18301 }
18302
18303 fixup_called = 1;
18304 }
18305
18306 /* Process the attributes that had to be skipped in the first round. These
18307 attributes are the ones that need str_offsets_base or addr_base attributes.
18308 They could not have been processed in the first round, because at the time
18309 the values of str_offsets_base or addr_base may not have been known. */
18310 void read_attribute_reprocess (const struct die_reader_specs *reader,
18311 struct attribute *attr)
18312 {
18313 struct dwarf2_cu *cu = reader->cu;
18314 switch (attr->form)
18315 {
18316 case DW_FORM_addrx:
18317 case DW_FORM_GNU_addr_index:
18318 DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
18319 break;
18320 case DW_FORM_strx:
18321 case DW_FORM_strx1:
18322 case DW_FORM_strx2:
18323 case DW_FORM_strx3:
18324 case DW_FORM_strx4:
18325 case DW_FORM_GNU_str_index:
18326 {
18327 unsigned int str_index = DW_UNSND (attr);
18328 if (reader->dwo_file != NULL)
18329 {
18330 DW_STRING (attr) = read_dwo_str_index (reader, str_index);
18331 DW_STRING_IS_CANONICAL (attr) = 0;
18332 }
18333 else
18334 {
18335 DW_STRING (attr) = read_stub_str_index (cu, str_index);
18336 DW_STRING_IS_CANONICAL (attr) = 0;
18337 }
18338 break;
18339 }
18340 default:
18341 gdb_assert_not_reached (_("Unexpected DWARF form."));
18342 }
18343 }
18344
18345 /* Read an attribute value described by an attribute form. */
18346
18347 static const gdb_byte *
18348 read_attribute_value (const struct die_reader_specs *reader,
18349 struct attribute *attr, unsigned form,
18350 LONGEST implicit_const, const gdb_byte *info_ptr,
18351 bool *need_reprocess)
18352 {
18353 struct dwarf2_cu *cu = reader->cu;
18354 struct dwarf2_per_objfile *dwarf2_per_objfile
18355 = cu->per_cu->dwarf2_per_objfile;
18356 struct objfile *objfile = dwarf2_per_objfile->objfile;
18357 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18358 bfd *abfd = reader->abfd;
18359 struct comp_unit_head *cu_header = &cu->header;
18360 unsigned int bytes_read;
18361 struct dwarf_block *blk;
18362 *need_reprocess = false;
18363
18364 attr->form = (enum dwarf_form) form;
18365 switch (form)
18366 {
18367 case DW_FORM_ref_addr:
18368 if (cu->header.version == 2)
18369 DW_UNSND (attr) = cu->header.read_address (abfd, info_ptr,
18370 &bytes_read);
18371 else
18372 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr,
18373 &bytes_read);
18374 info_ptr += bytes_read;
18375 break;
18376 case DW_FORM_GNU_ref_alt:
18377 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
18378 info_ptr += bytes_read;
18379 break;
18380 case DW_FORM_addr:
18381 DW_ADDR (attr) = cu->header.read_address (abfd, info_ptr, &bytes_read);
18382 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
18383 info_ptr += bytes_read;
18384 break;
18385 case DW_FORM_block2:
18386 blk = dwarf_alloc_block (cu);
18387 blk->size = read_2_bytes (abfd, info_ptr);
18388 info_ptr += 2;
18389 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18390 info_ptr += blk->size;
18391 DW_BLOCK (attr) = blk;
18392 break;
18393 case DW_FORM_block4:
18394 blk = dwarf_alloc_block (cu);
18395 blk->size = read_4_bytes (abfd, info_ptr);
18396 info_ptr += 4;
18397 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18398 info_ptr += blk->size;
18399 DW_BLOCK (attr) = blk;
18400 break;
18401 case DW_FORM_data2:
18402 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
18403 info_ptr += 2;
18404 break;
18405 case DW_FORM_data4:
18406 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
18407 info_ptr += 4;
18408 break;
18409 case DW_FORM_data8:
18410 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
18411 info_ptr += 8;
18412 break;
18413 case DW_FORM_data16:
18414 blk = dwarf_alloc_block (cu);
18415 blk->size = 16;
18416 blk->data = read_n_bytes (abfd, info_ptr, 16);
18417 info_ptr += 16;
18418 DW_BLOCK (attr) = blk;
18419 break;
18420 case DW_FORM_sec_offset:
18421 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
18422 info_ptr += bytes_read;
18423 break;
18424 case DW_FORM_string:
18425 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
18426 DW_STRING_IS_CANONICAL (attr) = 0;
18427 info_ptr += bytes_read;
18428 break;
18429 case DW_FORM_strp:
18430 if (!cu->per_cu->is_dwz)
18431 {
18432 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
18433 abfd, info_ptr, cu_header,
18434 &bytes_read);
18435 DW_STRING_IS_CANONICAL (attr) = 0;
18436 info_ptr += bytes_read;
18437 break;
18438 }
18439 /* FALLTHROUGH */
18440 case DW_FORM_line_strp:
18441 if (!cu->per_cu->is_dwz)
18442 {
18443 DW_STRING (attr)
18444 = dwarf2_per_objfile->read_line_string (info_ptr, cu_header,
18445 &bytes_read);
18446 DW_STRING_IS_CANONICAL (attr) = 0;
18447 info_ptr += bytes_read;
18448 break;
18449 }
18450 /* FALLTHROUGH */
18451 case DW_FORM_GNU_strp_alt:
18452 {
18453 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
18454 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
18455 &bytes_read);
18456
18457 DW_STRING (attr) = dwz->read_string (objfile, str_offset);
18458 DW_STRING_IS_CANONICAL (attr) = 0;
18459 info_ptr += bytes_read;
18460 }
18461 break;
18462 case DW_FORM_exprloc:
18463 case DW_FORM_block:
18464 blk = dwarf_alloc_block (cu);
18465 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18466 info_ptr += bytes_read;
18467 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18468 info_ptr += blk->size;
18469 DW_BLOCK (attr) = blk;
18470 break;
18471 case DW_FORM_block1:
18472 blk = dwarf_alloc_block (cu);
18473 blk->size = read_1_byte (abfd, info_ptr);
18474 info_ptr += 1;
18475 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18476 info_ptr += blk->size;
18477 DW_BLOCK (attr) = blk;
18478 break;
18479 case DW_FORM_data1:
18480 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18481 info_ptr += 1;
18482 break;
18483 case DW_FORM_flag:
18484 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18485 info_ptr += 1;
18486 break;
18487 case DW_FORM_flag_present:
18488 DW_UNSND (attr) = 1;
18489 break;
18490 case DW_FORM_sdata:
18491 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
18492 info_ptr += bytes_read;
18493 break;
18494 case DW_FORM_udata:
18495 case DW_FORM_rnglistx:
18496 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18497 info_ptr += bytes_read;
18498 break;
18499 case DW_FORM_ref1:
18500 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18501 + read_1_byte (abfd, info_ptr));
18502 info_ptr += 1;
18503 break;
18504 case DW_FORM_ref2:
18505 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18506 + read_2_bytes (abfd, info_ptr));
18507 info_ptr += 2;
18508 break;
18509 case DW_FORM_ref4:
18510 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18511 + read_4_bytes (abfd, info_ptr));
18512 info_ptr += 4;
18513 break;
18514 case DW_FORM_ref8:
18515 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18516 + read_8_bytes (abfd, info_ptr));
18517 info_ptr += 8;
18518 break;
18519 case DW_FORM_ref_sig8:
18520 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
18521 info_ptr += 8;
18522 break;
18523 case DW_FORM_ref_udata:
18524 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18525 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
18526 info_ptr += bytes_read;
18527 break;
18528 case DW_FORM_indirect:
18529 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18530 info_ptr += bytes_read;
18531 if (form == DW_FORM_implicit_const)
18532 {
18533 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
18534 info_ptr += bytes_read;
18535 }
18536 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
18537 info_ptr, need_reprocess);
18538 break;
18539 case DW_FORM_implicit_const:
18540 DW_SND (attr) = implicit_const;
18541 break;
18542 case DW_FORM_addrx:
18543 case DW_FORM_GNU_addr_index:
18544 *need_reprocess = true;
18545 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18546 info_ptr += bytes_read;
18547 break;
18548 case DW_FORM_strx:
18549 case DW_FORM_strx1:
18550 case DW_FORM_strx2:
18551 case DW_FORM_strx3:
18552 case DW_FORM_strx4:
18553 case DW_FORM_GNU_str_index:
18554 {
18555 ULONGEST str_index;
18556 if (form == DW_FORM_strx1)
18557 {
18558 str_index = read_1_byte (abfd, info_ptr);
18559 info_ptr += 1;
18560 }
18561 else if (form == DW_FORM_strx2)
18562 {
18563 str_index = read_2_bytes (abfd, info_ptr);
18564 info_ptr += 2;
18565 }
18566 else if (form == DW_FORM_strx3)
18567 {
18568 str_index = read_3_bytes (abfd, info_ptr);
18569 info_ptr += 3;
18570 }
18571 else if (form == DW_FORM_strx4)
18572 {
18573 str_index = read_4_bytes (abfd, info_ptr);
18574 info_ptr += 4;
18575 }
18576 else
18577 {
18578 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18579 info_ptr += bytes_read;
18580 }
18581 *need_reprocess = true;
18582 DW_UNSND (attr) = str_index;
18583 }
18584 break;
18585 default:
18586 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
18587 dwarf_form_name (form),
18588 bfd_get_filename (abfd));
18589 }
18590
18591 /* Super hack. */
18592 if (cu->per_cu->is_dwz && attr->form_is_ref ())
18593 attr->form = DW_FORM_GNU_ref_alt;
18594
18595 /* We have seen instances where the compiler tried to emit a byte
18596 size attribute of -1 which ended up being encoded as an unsigned
18597 0xffffffff. Although 0xffffffff is technically a valid size value,
18598 an object of this size seems pretty unlikely so we can relatively
18599 safely treat these cases as if the size attribute was invalid and
18600 treat them as zero by default. */
18601 if (attr->name == DW_AT_byte_size
18602 && form == DW_FORM_data4
18603 && DW_UNSND (attr) >= 0xffffffff)
18604 {
18605 complaint
18606 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
18607 hex_string (DW_UNSND (attr)));
18608 DW_UNSND (attr) = 0;
18609 }
18610
18611 return info_ptr;
18612 }
18613
18614 /* Read an attribute described by an abbreviated attribute. */
18615
18616 static const gdb_byte *
18617 read_attribute (const struct die_reader_specs *reader,
18618 struct attribute *attr, struct attr_abbrev *abbrev,
18619 const gdb_byte *info_ptr, bool *need_reprocess)
18620 {
18621 attr->name = abbrev->name;
18622 return read_attribute_value (reader, attr, abbrev->form,
18623 abbrev->implicit_const, info_ptr,
18624 need_reprocess);
18625 }
18626
18627 /* Return pointer to string at .debug_str offset STR_OFFSET. */
18628
18629 static const char *
18630 read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
18631 LONGEST str_offset)
18632 {
18633 return dwarf2_per_objfile->str.read_string (dwarf2_per_objfile->objfile,
18634 str_offset, "DW_FORM_strp");
18635 }
18636
18637 /* Return pointer to string at .debug_str offset as read from BUF.
18638 BUF is assumed to be in a compilation unit described by CU_HEADER.
18639 Return *BYTES_READ_PTR count of bytes read from BUF. */
18640
18641 static const char *
18642 read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
18643 const gdb_byte *buf,
18644 const struct comp_unit_head *cu_header,
18645 unsigned int *bytes_read_ptr)
18646 {
18647 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
18648
18649 return read_indirect_string_at_offset (dwarf2_per_objfile, str_offset);
18650 }
18651
18652 /* See read.h. */
18653
18654 const char *
18655 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
18656 const struct comp_unit_head *cu_header,
18657 unsigned int *bytes_read_ptr)
18658 {
18659 bfd *abfd = objfile->obfd;
18660 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
18661
18662 return line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
18663 }
18664
18665 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
18666 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
18667 ADDR_SIZE is the size of addresses from the CU header. */
18668
18669 static CORE_ADDR
18670 read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
18671 unsigned int addr_index, gdb::optional<ULONGEST> addr_base,
18672 int addr_size)
18673 {
18674 struct objfile *objfile = dwarf2_per_objfile->objfile;
18675 bfd *abfd = objfile->obfd;
18676 const gdb_byte *info_ptr;
18677 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
18678
18679 dwarf2_per_objfile->addr.read (objfile);
18680 if (dwarf2_per_objfile->addr.buffer == NULL)
18681 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
18682 objfile_name (objfile));
18683 if (addr_base_or_zero + addr_index * addr_size
18684 >= dwarf2_per_objfile->addr.size)
18685 error (_("DW_FORM_addr_index pointing outside of "
18686 ".debug_addr section [in module %s]"),
18687 objfile_name (objfile));
18688 info_ptr = (dwarf2_per_objfile->addr.buffer
18689 + addr_base_or_zero + addr_index * addr_size);
18690 if (addr_size == 4)
18691 return bfd_get_32 (abfd, info_ptr);
18692 else
18693 return bfd_get_64 (abfd, info_ptr);
18694 }
18695
18696 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
18697
18698 static CORE_ADDR
18699 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
18700 {
18701 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
18702 cu->addr_base, cu->header.addr_size);
18703 }
18704
18705 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
18706
18707 static CORE_ADDR
18708 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
18709 unsigned int *bytes_read)
18710 {
18711 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
18712 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
18713
18714 return read_addr_index (cu, addr_index);
18715 }
18716
18717 /* See read.h. */
18718
18719 CORE_ADDR
18720 dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu, unsigned int addr_index)
18721 {
18722 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
18723 struct dwarf2_cu *cu = per_cu->cu;
18724 gdb::optional<ULONGEST> addr_base;
18725 int addr_size;
18726
18727 /* We need addr_base and addr_size.
18728 If we don't have PER_CU->cu, we have to get it.
18729 Nasty, but the alternative is storing the needed info in PER_CU,
18730 which at this point doesn't seem justified: it's not clear how frequently
18731 it would get used and it would increase the size of every PER_CU.
18732 Entry points like dwarf2_per_cu_addr_size do a similar thing
18733 so we're not in uncharted territory here.
18734 Alas we need to be a bit more complicated as addr_base is contained
18735 in the DIE.
18736
18737 We don't need to read the entire CU(/TU).
18738 We just need the header and top level die.
18739
18740 IWBN to use the aging mechanism to let us lazily later discard the CU.
18741 For now we skip this optimization. */
18742
18743 if (cu != NULL)
18744 {
18745 addr_base = cu->addr_base;
18746 addr_size = cu->header.addr_size;
18747 }
18748 else
18749 {
18750 cutu_reader reader (per_cu, NULL, 0, false);
18751 addr_base = reader.cu->addr_base;
18752 addr_size = reader.cu->header.addr_size;
18753 }
18754
18755 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
18756 addr_size);
18757 }
18758
18759 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
18760 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
18761 DWO file. */
18762
18763 static const char *
18764 read_str_index (struct dwarf2_cu *cu,
18765 struct dwarf2_section_info *str_section,
18766 struct dwarf2_section_info *str_offsets_section,
18767 ULONGEST str_offsets_base, ULONGEST str_index)
18768 {
18769 struct dwarf2_per_objfile *dwarf2_per_objfile
18770 = cu->per_cu->dwarf2_per_objfile;
18771 struct objfile *objfile = dwarf2_per_objfile->objfile;
18772 const char *objf_name = objfile_name (objfile);
18773 bfd *abfd = objfile->obfd;
18774 const gdb_byte *info_ptr;
18775 ULONGEST str_offset;
18776 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
18777
18778 str_section->read (objfile);
18779 str_offsets_section->read (objfile);
18780 if (str_section->buffer == NULL)
18781 error (_("%s used without %s section"
18782 " in CU at offset %s [in module %s]"),
18783 form_name, str_section->get_name (),
18784 sect_offset_str (cu->header.sect_off), objf_name);
18785 if (str_offsets_section->buffer == NULL)
18786 error (_("%s used without %s section"
18787 " in CU at offset %s [in module %s]"),
18788 form_name, str_section->get_name (),
18789 sect_offset_str (cu->header.sect_off), objf_name);
18790 info_ptr = (str_offsets_section->buffer
18791 + str_offsets_base
18792 + str_index * cu->header.offset_size);
18793 if (cu->header.offset_size == 4)
18794 str_offset = bfd_get_32 (abfd, info_ptr);
18795 else
18796 str_offset = bfd_get_64 (abfd, info_ptr);
18797 if (str_offset >= str_section->size)
18798 error (_("Offset from %s pointing outside of"
18799 " .debug_str.dwo section in CU at offset %s [in module %s]"),
18800 form_name, sect_offset_str (cu->header.sect_off), objf_name);
18801 return (const char *) (str_section->buffer + str_offset);
18802 }
18803
18804 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
18805
18806 static const char *
18807 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
18808 {
18809 ULONGEST str_offsets_base = reader->cu->header.version >= 5
18810 ? reader->cu->header.addr_size : 0;
18811 return read_str_index (reader->cu,
18812 &reader->dwo_file->sections.str,
18813 &reader->dwo_file->sections.str_offsets,
18814 str_offsets_base, str_index);
18815 }
18816
18817 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
18818
18819 static const char *
18820 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
18821 {
18822 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18823 const char *objf_name = objfile_name (objfile);
18824 static const char form_name[] = "DW_FORM_GNU_str_index";
18825 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
18826
18827 if (!cu->str_offsets_base.has_value ())
18828 error (_("%s used in Fission stub without %s"
18829 " in CU at offset 0x%lx [in module %s]"),
18830 form_name, str_offsets_attr_name,
18831 (long) cu->header.offset_size, objf_name);
18832
18833 return read_str_index (cu,
18834 &cu->per_cu->dwarf2_per_objfile->str,
18835 &cu->per_cu->dwarf2_per_objfile->str_offsets,
18836 *cu->str_offsets_base, str_index);
18837 }
18838
18839 /* Return the length of an LEB128 number in BUF. */
18840
18841 static int
18842 leb128_size (const gdb_byte *buf)
18843 {
18844 const gdb_byte *begin = buf;
18845 gdb_byte byte;
18846
18847 while (1)
18848 {
18849 byte = *buf++;
18850 if ((byte & 128) == 0)
18851 return buf - begin;
18852 }
18853 }
18854
18855 static void
18856 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
18857 {
18858 switch (lang)
18859 {
18860 case DW_LANG_C89:
18861 case DW_LANG_C99:
18862 case DW_LANG_C11:
18863 case DW_LANG_C:
18864 case DW_LANG_UPC:
18865 cu->language = language_c;
18866 break;
18867 case DW_LANG_Java:
18868 case DW_LANG_C_plus_plus:
18869 case DW_LANG_C_plus_plus_11:
18870 case DW_LANG_C_plus_plus_14:
18871 cu->language = language_cplus;
18872 break;
18873 case DW_LANG_D:
18874 cu->language = language_d;
18875 break;
18876 case DW_LANG_Fortran77:
18877 case DW_LANG_Fortran90:
18878 case DW_LANG_Fortran95:
18879 case DW_LANG_Fortran03:
18880 case DW_LANG_Fortran08:
18881 cu->language = language_fortran;
18882 break;
18883 case DW_LANG_Go:
18884 cu->language = language_go;
18885 break;
18886 case DW_LANG_Mips_Assembler:
18887 cu->language = language_asm;
18888 break;
18889 case DW_LANG_Ada83:
18890 case DW_LANG_Ada95:
18891 cu->language = language_ada;
18892 break;
18893 case DW_LANG_Modula2:
18894 cu->language = language_m2;
18895 break;
18896 case DW_LANG_Pascal83:
18897 cu->language = language_pascal;
18898 break;
18899 case DW_LANG_ObjC:
18900 cu->language = language_objc;
18901 break;
18902 case DW_LANG_Rust:
18903 case DW_LANG_Rust_old:
18904 cu->language = language_rust;
18905 break;
18906 case DW_LANG_Cobol74:
18907 case DW_LANG_Cobol85:
18908 default:
18909 cu->language = language_minimal;
18910 break;
18911 }
18912 cu->language_defn = language_def (cu->language);
18913 }
18914
18915 /* Return the named attribute or NULL if not there. */
18916
18917 static struct attribute *
18918 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
18919 {
18920 for (;;)
18921 {
18922 unsigned int i;
18923 struct attribute *spec = NULL;
18924
18925 for (i = 0; i < die->num_attrs; ++i)
18926 {
18927 if (die->attrs[i].name == name)
18928 return &die->attrs[i];
18929 if (die->attrs[i].name == DW_AT_specification
18930 || die->attrs[i].name == DW_AT_abstract_origin)
18931 spec = &die->attrs[i];
18932 }
18933
18934 if (!spec)
18935 break;
18936
18937 die = follow_die_ref (die, spec, &cu);
18938 }
18939
18940 return NULL;
18941 }
18942
18943 /* Return the string associated with a string-typed attribute, or NULL if it
18944 is either not found or is of an incorrect type. */
18945
18946 static const char *
18947 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
18948 {
18949 struct attribute *attr;
18950 const char *str = NULL;
18951
18952 attr = dwarf2_attr (die, name, cu);
18953
18954 if (attr != NULL)
18955 {
18956 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
18957 || attr->form == DW_FORM_string
18958 || attr->form == DW_FORM_strx
18959 || attr->form == DW_FORM_strx1
18960 || attr->form == DW_FORM_strx2
18961 || attr->form == DW_FORM_strx3
18962 || attr->form == DW_FORM_strx4
18963 || attr->form == DW_FORM_GNU_str_index
18964 || attr->form == DW_FORM_GNU_strp_alt)
18965 str = DW_STRING (attr);
18966 else
18967 complaint (_("string type expected for attribute %s for "
18968 "DIE at %s in module %s"),
18969 dwarf_attr_name (name), sect_offset_str (die->sect_off),
18970 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
18971 }
18972
18973 return str;
18974 }
18975
18976 /* Return the dwo name or NULL if not present. If present, it is in either
18977 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
18978 static const char *
18979 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
18980 {
18981 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
18982 if (dwo_name == nullptr)
18983 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
18984 return dwo_name;
18985 }
18986
18987 /* Return non-zero iff the attribute NAME is defined for the given DIE,
18988 and holds a non-zero value. This function should only be used for
18989 DW_FORM_flag or DW_FORM_flag_present attributes. */
18990
18991 static int
18992 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
18993 {
18994 struct attribute *attr = dwarf2_attr (die, name, cu);
18995
18996 return (attr && DW_UNSND (attr));
18997 }
18998
18999 static int
19000 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
19001 {
19002 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19003 which value is non-zero. However, we have to be careful with
19004 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19005 (via dwarf2_flag_true_p) follows this attribute. So we may
19006 end up accidently finding a declaration attribute that belongs
19007 to a different DIE referenced by the specification attribute,
19008 even though the given DIE does not have a declaration attribute. */
19009 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19010 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
19011 }
19012
19013 /* Return the die giving the specification for DIE, if there is
19014 one. *SPEC_CU is the CU containing DIE on input, and the CU
19015 containing the return value on output. If there is no
19016 specification, but there is an abstract origin, that is
19017 returned. */
19018
19019 static struct die_info *
19020 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
19021 {
19022 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19023 *spec_cu);
19024
19025 if (spec_attr == NULL)
19026 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19027
19028 if (spec_attr == NULL)
19029 return NULL;
19030 else
19031 return follow_die_ref (die, spec_attr, spec_cu);
19032 }
19033
19034 /* Stub for free_line_header to match void * callback types. */
19035
19036 static void
19037 free_line_header_voidp (void *arg)
19038 {
19039 struct line_header *lh = (struct line_header *) arg;
19040
19041 delete lh;
19042 }
19043
19044 /* A convenience function to find the proper .debug_line section for a CU. */
19045
19046 static struct dwarf2_section_info *
19047 get_debug_line_section (struct dwarf2_cu *cu)
19048 {
19049 struct dwarf2_section_info *section;
19050 struct dwarf2_per_objfile *dwarf2_per_objfile
19051 = cu->per_cu->dwarf2_per_objfile;
19052
19053 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19054 DWO file. */
19055 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19056 section = &cu->dwo_unit->dwo_file->sections.line;
19057 else if (cu->per_cu->is_dwz)
19058 {
19059 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
19060
19061 section = &dwz->line;
19062 }
19063 else
19064 section = &dwarf2_per_objfile->line;
19065
19066 return section;
19067 }
19068
19069 /* Read the statement program header starting at OFFSET in
19070 .debug_line, or .debug_line.dwo. Return a pointer
19071 to a struct line_header, allocated using xmalloc.
19072 Returns NULL if there is a problem reading the header, e.g., if it
19073 has a version we don't understand.
19074
19075 NOTE: the strings in the include directory and file name tables of
19076 the returned object point into the dwarf line section buffer,
19077 and must not be freed. */
19078
19079 static line_header_up
19080 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
19081 {
19082 struct dwarf2_section_info *section;
19083 struct dwarf2_per_objfile *dwarf2_per_objfile
19084 = cu->per_cu->dwarf2_per_objfile;
19085
19086 section = get_debug_line_section (cu);
19087 section->read (dwarf2_per_objfile->objfile);
19088 if (section->buffer == NULL)
19089 {
19090 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19091 complaint (_("missing .debug_line.dwo section"));
19092 else
19093 complaint (_("missing .debug_line section"));
19094 return 0;
19095 }
19096
19097 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
19098 dwarf2_per_objfile, section,
19099 &cu->header);
19100 }
19101
19102 /* Subroutine of dwarf_decode_lines to simplify it.
19103 Return the file name of the psymtab for the given file_entry.
19104 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
19105 If space for the result is malloc'd, *NAME_HOLDER will be set.
19106 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
19107
19108 static const char *
19109 psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
19110 const dwarf2_psymtab *pst,
19111 const char *comp_dir,
19112 gdb::unique_xmalloc_ptr<char> *name_holder)
19113 {
19114 const char *include_name = fe.name;
19115 const char *include_name_to_compare = include_name;
19116 const char *pst_filename;
19117 int file_is_pst;
19118
19119 const char *dir_name = fe.include_dir (lh);
19120
19121 gdb::unique_xmalloc_ptr<char> hold_compare;
19122 if (!IS_ABSOLUTE_PATH (include_name)
19123 && (dir_name != NULL || comp_dir != NULL))
19124 {
19125 /* Avoid creating a duplicate psymtab for PST.
19126 We do this by comparing INCLUDE_NAME and PST_FILENAME.
19127 Before we do the comparison, however, we need to account
19128 for DIR_NAME and COMP_DIR.
19129 First prepend dir_name (if non-NULL). If we still don't
19130 have an absolute path prepend comp_dir (if non-NULL).
19131 However, the directory we record in the include-file's
19132 psymtab does not contain COMP_DIR (to match the
19133 corresponding symtab(s)).
19134
19135 Example:
19136
19137 bash$ cd /tmp
19138 bash$ gcc -g ./hello.c
19139 include_name = "hello.c"
19140 dir_name = "."
19141 DW_AT_comp_dir = comp_dir = "/tmp"
19142 DW_AT_name = "./hello.c"
19143
19144 */
19145
19146 if (dir_name != NULL)
19147 {
19148 name_holder->reset (concat (dir_name, SLASH_STRING,
19149 include_name, (char *) NULL));
19150 include_name = name_holder->get ();
19151 include_name_to_compare = include_name;
19152 }
19153 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
19154 {
19155 hold_compare.reset (concat (comp_dir, SLASH_STRING,
19156 include_name, (char *) NULL));
19157 include_name_to_compare = hold_compare.get ();
19158 }
19159 }
19160
19161 pst_filename = pst->filename;
19162 gdb::unique_xmalloc_ptr<char> copied_name;
19163 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
19164 {
19165 copied_name.reset (concat (pst->dirname, SLASH_STRING,
19166 pst_filename, (char *) NULL));
19167 pst_filename = copied_name.get ();
19168 }
19169
19170 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
19171
19172 if (file_is_pst)
19173 return NULL;
19174 return include_name;
19175 }
19176
19177 /* State machine to track the state of the line number program. */
19178
19179 class lnp_state_machine
19180 {
19181 public:
19182 /* Initialize a machine state for the start of a line number
19183 program. */
19184 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
19185 bool record_lines_p);
19186
19187 file_entry *current_file ()
19188 {
19189 /* lh->file_names is 0-based, but the file name numbers in the
19190 statement program are 1-based. */
19191 return m_line_header->file_name_at (m_file);
19192 }
19193
19194 /* Record the line in the state machine. END_SEQUENCE is true if
19195 we're processing the end of a sequence. */
19196 void record_line (bool end_sequence);
19197
19198 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
19199 nop-out rest of the lines in this sequence. */
19200 void check_line_address (struct dwarf2_cu *cu,
19201 const gdb_byte *line_ptr,
19202 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
19203
19204 void handle_set_discriminator (unsigned int discriminator)
19205 {
19206 m_discriminator = discriminator;
19207 m_line_has_non_zero_discriminator |= discriminator != 0;
19208 }
19209
19210 /* Handle DW_LNE_set_address. */
19211 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
19212 {
19213 m_op_index = 0;
19214 address += baseaddr;
19215 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
19216 }
19217
19218 /* Handle DW_LNS_advance_pc. */
19219 void handle_advance_pc (CORE_ADDR adjust);
19220
19221 /* Handle a special opcode. */
19222 void handle_special_opcode (unsigned char op_code);
19223
19224 /* Handle DW_LNS_advance_line. */
19225 void handle_advance_line (int line_delta)
19226 {
19227 advance_line (line_delta);
19228 }
19229
19230 /* Handle DW_LNS_set_file. */
19231 void handle_set_file (file_name_index file);
19232
19233 /* Handle DW_LNS_negate_stmt. */
19234 void handle_negate_stmt ()
19235 {
19236 m_is_stmt = !m_is_stmt;
19237 }
19238
19239 /* Handle DW_LNS_const_add_pc. */
19240 void handle_const_add_pc ();
19241
19242 /* Handle DW_LNS_fixed_advance_pc. */
19243 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
19244 {
19245 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19246 m_op_index = 0;
19247 }
19248
19249 /* Handle DW_LNS_copy. */
19250 void handle_copy ()
19251 {
19252 record_line (false);
19253 m_discriminator = 0;
19254 }
19255
19256 /* Handle DW_LNE_end_sequence. */
19257 void handle_end_sequence ()
19258 {
19259 m_currently_recording_lines = true;
19260 }
19261
19262 private:
19263 /* Advance the line by LINE_DELTA. */
19264 void advance_line (int line_delta)
19265 {
19266 m_line += line_delta;
19267
19268 if (line_delta != 0)
19269 m_line_has_non_zero_discriminator = m_discriminator != 0;
19270 }
19271
19272 struct dwarf2_cu *m_cu;
19273
19274 gdbarch *m_gdbarch;
19275
19276 /* True if we're recording lines.
19277 Otherwise we're building partial symtabs and are just interested in
19278 finding include files mentioned by the line number program. */
19279 bool m_record_lines_p;
19280
19281 /* The line number header. */
19282 line_header *m_line_header;
19283
19284 /* These are part of the standard DWARF line number state machine,
19285 and initialized according to the DWARF spec. */
19286
19287 unsigned char m_op_index = 0;
19288 /* The line table index of the current file. */
19289 file_name_index m_file = 1;
19290 unsigned int m_line = 1;
19291
19292 /* These are initialized in the constructor. */
19293
19294 CORE_ADDR m_address;
19295 bool m_is_stmt;
19296 unsigned int m_discriminator;
19297
19298 /* Additional bits of state we need to track. */
19299
19300 /* The last file that we called dwarf2_start_subfile for.
19301 This is only used for TLLs. */
19302 unsigned int m_last_file = 0;
19303 /* The last file a line number was recorded for. */
19304 struct subfile *m_last_subfile = NULL;
19305
19306 /* When true, record the lines we decode. */
19307 bool m_currently_recording_lines = false;
19308
19309 /* The last line number that was recorded, used to coalesce
19310 consecutive entries for the same line. This can happen, for
19311 example, when discriminators are present. PR 17276. */
19312 unsigned int m_last_line = 0;
19313 bool m_line_has_non_zero_discriminator = false;
19314 };
19315
19316 void
19317 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
19318 {
19319 CORE_ADDR addr_adj = (((m_op_index + adjust)
19320 / m_line_header->maximum_ops_per_instruction)
19321 * m_line_header->minimum_instruction_length);
19322 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19323 m_op_index = ((m_op_index + adjust)
19324 % m_line_header->maximum_ops_per_instruction);
19325 }
19326
19327 void
19328 lnp_state_machine::handle_special_opcode (unsigned char op_code)
19329 {
19330 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
19331 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
19332 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
19333 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
19334 / m_line_header->maximum_ops_per_instruction)
19335 * m_line_header->minimum_instruction_length);
19336 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19337 m_op_index = ((m_op_index + adj_opcode_d)
19338 % m_line_header->maximum_ops_per_instruction);
19339
19340 int line_delta = m_line_header->line_base + adj_opcode_r;
19341 advance_line (line_delta);
19342 record_line (false);
19343 m_discriminator = 0;
19344 }
19345
19346 void
19347 lnp_state_machine::handle_set_file (file_name_index file)
19348 {
19349 m_file = file;
19350
19351 const file_entry *fe = current_file ();
19352 if (fe == NULL)
19353 dwarf2_debug_line_missing_file_complaint ();
19354 else if (m_record_lines_p)
19355 {
19356 const char *dir = fe->include_dir (m_line_header);
19357
19358 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
19359 m_line_has_non_zero_discriminator = m_discriminator != 0;
19360 dwarf2_start_subfile (m_cu, fe->name, dir);
19361 }
19362 }
19363
19364 void
19365 lnp_state_machine::handle_const_add_pc ()
19366 {
19367 CORE_ADDR adjust
19368 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
19369
19370 CORE_ADDR addr_adj
19371 = (((m_op_index + adjust)
19372 / m_line_header->maximum_ops_per_instruction)
19373 * m_line_header->minimum_instruction_length);
19374
19375 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19376 m_op_index = ((m_op_index + adjust)
19377 % m_line_header->maximum_ops_per_instruction);
19378 }
19379
19380 /* Return non-zero if we should add LINE to the line number table.
19381 LINE is the line to add, LAST_LINE is the last line that was added,
19382 LAST_SUBFILE is the subfile for LAST_LINE.
19383 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
19384 had a non-zero discriminator.
19385
19386 We have to be careful in the presence of discriminators.
19387 E.g., for this line:
19388
19389 for (i = 0; i < 100000; i++);
19390
19391 clang can emit four line number entries for that one line,
19392 each with a different discriminator.
19393 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
19394
19395 However, we want gdb to coalesce all four entries into one.
19396 Otherwise the user could stepi into the middle of the line and
19397 gdb would get confused about whether the pc really was in the
19398 middle of the line.
19399
19400 Things are further complicated by the fact that two consecutive
19401 line number entries for the same line is a heuristic used by gcc
19402 to denote the end of the prologue. So we can't just discard duplicate
19403 entries, we have to be selective about it. The heuristic we use is
19404 that we only collapse consecutive entries for the same line if at least
19405 one of those entries has a non-zero discriminator. PR 17276.
19406
19407 Note: Addresses in the line number state machine can never go backwards
19408 within one sequence, thus this coalescing is ok. */
19409
19410 static int
19411 dwarf_record_line_p (struct dwarf2_cu *cu,
19412 unsigned int line, unsigned int last_line,
19413 int line_has_non_zero_discriminator,
19414 struct subfile *last_subfile)
19415 {
19416 if (cu->get_builder ()->get_current_subfile () != last_subfile)
19417 return 1;
19418 if (line != last_line)
19419 return 1;
19420 /* Same line for the same file that we've seen already.
19421 As a last check, for pr 17276, only record the line if the line
19422 has never had a non-zero discriminator. */
19423 if (!line_has_non_zero_discriminator)
19424 return 1;
19425 return 0;
19426 }
19427
19428 /* Use the CU's builder to record line number LINE beginning at
19429 address ADDRESS in the line table of subfile SUBFILE. */
19430
19431 static void
19432 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
19433 unsigned int line, CORE_ADDR address, bool is_stmt,
19434 struct dwarf2_cu *cu)
19435 {
19436 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
19437
19438 if (dwarf_line_debug)
19439 {
19440 fprintf_unfiltered (gdb_stdlog,
19441 "Recording line %u, file %s, address %s\n",
19442 line, lbasename (subfile->name),
19443 paddress (gdbarch, address));
19444 }
19445
19446 if (cu != nullptr)
19447 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
19448 }
19449
19450 /* Subroutine of dwarf_decode_lines_1 to simplify it.
19451 Mark the end of a set of line number records.
19452 The arguments are the same as for dwarf_record_line_1.
19453 If SUBFILE is NULL the request is ignored. */
19454
19455 static void
19456 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
19457 CORE_ADDR address, struct dwarf2_cu *cu)
19458 {
19459 if (subfile == NULL)
19460 return;
19461
19462 if (dwarf_line_debug)
19463 {
19464 fprintf_unfiltered (gdb_stdlog,
19465 "Finishing current line, file %s, address %s\n",
19466 lbasename (subfile->name),
19467 paddress (gdbarch, address));
19468 }
19469
19470 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
19471 }
19472
19473 void
19474 lnp_state_machine::record_line (bool end_sequence)
19475 {
19476 if (dwarf_line_debug)
19477 {
19478 fprintf_unfiltered (gdb_stdlog,
19479 "Processing actual line %u: file %u,"
19480 " address %s, is_stmt %u, discrim %u%s\n",
19481 m_line, m_file,
19482 paddress (m_gdbarch, m_address),
19483 m_is_stmt, m_discriminator,
19484 (end_sequence ? "\t(end sequence)" : ""));
19485 }
19486
19487 file_entry *fe = current_file ();
19488
19489 if (fe == NULL)
19490 dwarf2_debug_line_missing_file_complaint ();
19491 /* For now we ignore lines not starting on an instruction boundary.
19492 But not when processing end_sequence for compatibility with the
19493 previous version of the code. */
19494 else if (m_op_index == 0 || end_sequence)
19495 {
19496 fe->included_p = 1;
19497 if (m_record_lines_p)
19498 {
19499 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
19500 || end_sequence)
19501 {
19502 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
19503 m_currently_recording_lines ? m_cu : nullptr);
19504 }
19505
19506 if (!end_sequence)
19507 {
19508 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
19509
19510 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
19511 m_line_has_non_zero_discriminator,
19512 m_last_subfile))
19513 {
19514 buildsym_compunit *builder = m_cu->get_builder ();
19515 dwarf_record_line_1 (m_gdbarch,
19516 builder->get_current_subfile (),
19517 m_line, m_address, is_stmt,
19518 m_currently_recording_lines ? m_cu : nullptr);
19519 }
19520 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
19521 m_last_line = m_line;
19522 }
19523 }
19524 }
19525 }
19526
19527 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
19528 line_header *lh, bool record_lines_p)
19529 {
19530 m_cu = cu;
19531 m_gdbarch = arch;
19532 m_record_lines_p = record_lines_p;
19533 m_line_header = lh;
19534
19535 m_currently_recording_lines = true;
19536
19537 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
19538 was a line entry for it so that the backend has a chance to adjust it
19539 and also record it in case it needs it. This is currently used by MIPS
19540 code, cf. `mips_adjust_dwarf2_line'. */
19541 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
19542 m_is_stmt = lh->default_is_stmt;
19543 m_discriminator = 0;
19544 }
19545
19546 void
19547 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
19548 const gdb_byte *line_ptr,
19549 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
19550 {
19551 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
19552 the pc range of the CU. However, we restrict the test to only ADDRESS
19553 values of zero to preserve GDB's previous behaviour which is to handle
19554 the specific case of a function being GC'd by the linker. */
19555
19556 if (address == 0 && address < unrelocated_lowpc)
19557 {
19558 /* This line table is for a function which has been
19559 GCd by the linker. Ignore it. PR gdb/12528 */
19560
19561 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19562 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
19563
19564 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
19565 line_offset, objfile_name (objfile));
19566 m_currently_recording_lines = false;
19567 /* Note: m_currently_recording_lines is left as false until we see
19568 DW_LNE_end_sequence. */
19569 }
19570 }
19571
19572 /* Subroutine of dwarf_decode_lines to simplify it.
19573 Process the line number information in LH.
19574 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
19575 program in order to set included_p for every referenced header. */
19576
19577 static void
19578 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
19579 const int decode_for_pst_p, CORE_ADDR lowpc)
19580 {
19581 const gdb_byte *line_ptr, *extended_end;
19582 const gdb_byte *line_end;
19583 unsigned int bytes_read, extended_len;
19584 unsigned char op_code, extended_op;
19585 CORE_ADDR baseaddr;
19586 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19587 bfd *abfd = objfile->obfd;
19588 struct gdbarch *gdbarch = get_objfile_arch (objfile);
19589 /* True if we're recording line info (as opposed to building partial
19590 symtabs and just interested in finding include files mentioned by
19591 the line number program). */
19592 bool record_lines_p = !decode_for_pst_p;
19593
19594 baseaddr = objfile->text_section_offset ();
19595
19596 line_ptr = lh->statement_program_start;
19597 line_end = lh->statement_program_end;
19598
19599 /* Read the statement sequences until there's nothing left. */
19600 while (line_ptr < line_end)
19601 {
19602 /* The DWARF line number program state machine. Reset the state
19603 machine at the start of each sequence. */
19604 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
19605 bool end_sequence = false;
19606
19607 if (record_lines_p)
19608 {
19609 /* Start a subfile for the current file of the state
19610 machine. */
19611 const file_entry *fe = state_machine.current_file ();
19612
19613 if (fe != NULL)
19614 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
19615 }
19616
19617 /* Decode the table. */
19618 while (line_ptr < line_end && !end_sequence)
19619 {
19620 op_code = read_1_byte (abfd, line_ptr);
19621 line_ptr += 1;
19622
19623 if (op_code >= lh->opcode_base)
19624 {
19625 /* Special opcode. */
19626 state_machine.handle_special_opcode (op_code);
19627 }
19628 else switch (op_code)
19629 {
19630 case DW_LNS_extended_op:
19631 extended_len = read_unsigned_leb128 (abfd, line_ptr,
19632 &bytes_read);
19633 line_ptr += bytes_read;
19634 extended_end = line_ptr + extended_len;
19635 extended_op = read_1_byte (abfd, line_ptr);
19636 line_ptr += 1;
19637 switch (extended_op)
19638 {
19639 case DW_LNE_end_sequence:
19640 state_machine.handle_end_sequence ();
19641 end_sequence = true;
19642 break;
19643 case DW_LNE_set_address:
19644 {
19645 CORE_ADDR address
19646 = cu->header.read_address (abfd, line_ptr, &bytes_read);
19647 line_ptr += bytes_read;
19648
19649 state_machine.check_line_address (cu, line_ptr,
19650 lowpc - baseaddr, address);
19651 state_machine.handle_set_address (baseaddr, address);
19652 }
19653 break;
19654 case DW_LNE_define_file:
19655 {
19656 const char *cur_file;
19657 unsigned int mod_time, length;
19658 dir_index dindex;
19659
19660 cur_file = read_direct_string (abfd, line_ptr,
19661 &bytes_read);
19662 line_ptr += bytes_read;
19663 dindex = (dir_index)
19664 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19665 line_ptr += bytes_read;
19666 mod_time =
19667 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19668 line_ptr += bytes_read;
19669 length =
19670 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19671 line_ptr += bytes_read;
19672 lh->add_file_name (cur_file, dindex, mod_time, length);
19673 }
19674 break;
19675 case DW_LNE_set_discriminator:
19676 {
19677 /* The discriminator is not interesting to the
19678 debugger; just ignore it. We still need to
19679 check its value though:
19680 if there are consecutive entries for the same
19681 (non-prologue) line we want to coalesce them.
19682 PR 17276. */
19683 unsigned int discr
19684 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19685 line_ptr += bytes_read;
19686
19687 state_machine.handle_set_discriminator (discr);
19688 }
19689 break;
19690 default:
19691 complaint (_("mangled .debug_line section"));
19692 return;
19693 }
19694 /* Make sure that we parsed the extended op correctly. If e.g.
19695 we expected a different address size than the producer used,
19696 we may have read the wrong number of bytes. */
19697 if (line_ptr != extended_end)
19698 {
19699 complaint (_("mangled .debug_line section"));
19700 return;
19701 }
19702 break;
19703 case DW_LNS_copy:
19704 state_machine.handle_copy ();
19705 break;
19706 case DW_LNS_advance_pc:
19707 {
19708 CORE_ADDR adjust
19709 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19710 line_ptr += bytes_read;
19711
19712 state_machine.handle_advance_pc (adjust);
19713 }
19714 break;
19715 case DW_LNS_advance_line:
19716 {
19717 int line_delta
19718 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
19719 line_ptr += bytes_read;
19720
19721 state_machine.handle_advance_line (line_delta);
19722 }
19723 break;
19724 case DW_LNS_set_file:
19725 {
19726 file_name_index file
19727 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
19728 &bytes_read);
19729 line_ptr += bytes_read;
19730
19731 state_machine.handle_set_file (file);
19732 }
19733 break;
19734 case DW_LNS_set_column:
19735 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19736 line_ptr += bytes_read;
19737 break;
19738 case DW_LNS_negate_stmt:
19739 state_machine.handle_negate_stmt ();
19740 break;
19741 case DW_LNS_set_basic_block:
19742 break;
19743 /* Add to the address register of the state machine the
19744 address increment value corresponding to special opcode
19745 255. I.e., this value is scaled by the minimum
19746 instruction length since special opcode 255 would have
19747 scaled the increment. */
19748 case DW_LNS_const_add_pc:
19749 state_machine.handle_const_add_pc ();
19750 break;
19751 case DW_LNS_fixed_advance_pc:
19752 {
19753 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
19754 line_ptr += 2;
19755
19756 state_machine.handle_fixed_advance_pc (addr_adj);
19757 }
19758 break;
19759 default:
19760 {
19761 /* Unknown standard opcode, ignore it. */
19762 int i;
19763
19764 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
19765 {
19766 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19767 line_ptr += bytes_read;
19768 }
19769 }
19770 }
19771 }
19772
19773 if (!end_sequence)
19774 dwarf2_debug_line_missing_end_sequence_complaint ();
19775
19776 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
19777 in which case we still finish recording the last line). */
19778 state_machine.record_line (true);
19779 }
19780 }
19781
19782 /* Decode the Line Number Program (LNP) for the given line_header
19783 structure and CU. The actual information extracted and the type
19784 of structures created from the LNP depends on the value of PST.
19785
19786 1. If PST is NULL, then this procedure uses the data from the program
19787 to create all necessary symbol tables, and their linetables.
19788
19789 2. If PST is not NULL, this procedure reads the program to determine
19790 the list of files included by the unit represented by PST, and
19791 builds all the associated partial symbol tables.
19792
19793 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
19794 It is used for relative paths in the line table.
19795 NOTE: When processing partial symtabs (pst != NULL),
19796 comp_dir == pst->dirname.
19797
19798 NOTE: It is important that psymtabs have the same file name (via strcmp)
19799 as the corresponding symtab. Since COMP_DIR is not used in the name of the
19800 symtab we don't use it in the name of the psymtabs we create.
19801 E.g. expand_line_sal requires this when finding psymtabs to expand.
19802 A good testcase for this is mb-inline.exp.
19803
19804 LOWPC is the lowest address in CU (or 0 if not known).
19805
19806 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
19807 for its PC<->lines mapping information. Otherwise only the filename
19808 table is read in. */
19809
19810 static void
19811 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
19812 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
19813 CORE_ADDR lowpc, int decode_mapping)
19814 {
19815 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19816 const int decode_for_pst_p = (pst != NULL);
19817
19818 if (decode_mapping)
19819 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
19820
19821 if (decode_for_pst_p)
19822 {
19823 /* Now that we're done scanning the Line Header Program, we can
19824 create the psymtab of each included file. */
19825 for (auto &file_entry : lh->file_names ())
19826 if (file_entry.included_p == 1)
19827 {
19828 gdb::unique_xmalloc_ptr<char> name_holder;
19829 const char *include_name =
19830 psymtab_include_file_name (lh, file_entry, pst,
19831 comp_dir, &name_holder);
19832 if (include_name != NULL)
19833 dwarf2_create_include_psymtab (include_name, pst, objfile);
19834 }
19835 }
19836 else
19837 {
19838 /* Make sure a symtab is created for every file, even files
19839 which contain only variables (i.e. no code with associated
19840 line numbers). */
19841 buildsym_compunit *builder = cu->get_builder ();
19842 struct compunit_symtab *cust = builder->get_compunit_symtab ();
19843
19844 for (auto &fe : lh->file_names ())
19845 {
19846 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
19847 if (builder->get_current_subfile ()->symtab == NULL)
19848 {
19849 builder->get_current_subfile ()->symtab
19850 = allocate_symtab (cust,
19851 builder->get_current_subfile ()->name);
19852 }
19853 fe.symtab = builder->get_current_subfile ()->symtab;
19854 }
19855 }
19856 }
19857
19858 /* Start a subfile for DWARF. FILENAME is the name of the file and
19859 DIRNAME the name of the source directory which contains FILENAME
19860 or NULL if not known.
19861 This routine tries to keep line numbers from identical absolute and
19862 relative file names in a common subfile.
19863
19864 Using the `list' example from the GDB testsuite, which resides in
19865 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
19866 of /srcdir/list0.c yields the following debugging information for list0.c:
19867
19868 DW_AT_name: /srcdir/list0.c
19869 DW_AT_comp_dir: /compdir
19870 files.files[0].name: list0.h
19871 files.files[0].dir: /srcdir
19872 files.files[1].name: list0.c
19873 files.files[1].dir: /srcdir
19874
19875 The line number information for list0.c has to end up in a single
19876 subfile, so that `break /srcdir/list0.c:1' works as expected.
19877 start_subfile will ensure that this happens provided that we pass the
19878 concatenation of files.files[1].dir and files.files[1].name as the
19879 subfile's name. */
19880
19881 static void
19882 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
19883 const char *dirname)
19884 {
19885 gdb::unique_xmalloc_ptr<char> copy;
19886
19887 /* In order not to lose the line information directory,
19888 we concatenate it to the filename when it makes sense.
19889 Note that the Dwarf3 standard says (speaking of filenames in line
19890 information): ``The directory index is ignored for file names
19891 that represent full path names''. Thus ignoring dirname in the
19892 `else' branch below isn't an issue. */
19893
19894 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
19895 {
19896 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
19897 filename = copy.get ();
19898 }
19899
19900 cu->get_builder ()->start_subfile (filename);
19901 }
19902
19903 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
19904 buildsym_compunit constructor. */
19905
19906 struct compunit_symtab *
19907 dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
19908 CORE_ADDR low_pc)
19909 {
19910 gdb_assert (m_builder == nullptr);
19911
19912 m_builder.reset (new struct buildsym_compunit
19913 (per_cu->dwarf2_per_objfile->objfile,
19914 name, comp_dir, language, low_pc));
19915
19916 list_in_scope = get_builder ()->get_file_symbols ();
19917
19918 get_builder ()->record_debugformat ("DWARF 2");
19919 get_builder ()->record_producer (producer);
19920
19921 processing_has_namespace_info = false;
19922
19923 return get_builder ()->get_compunit_symtab ();
19924 }
19925
19926 static void
19927 var_decode_location (struct attribute *attr, struct symbol *sym,
19928 struct dwarf2_cu *cu)
19929 {
19930 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19931 struct comp_unit_head *cu_header = &cu->header;
19932
19933 /* NOTE drow/2003-01-30: There used to be a comment and some special
19934 code here to turn a symbol with DW_AT_external and a
19935 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
19936 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
19937 with some versions of binutils) where shared libraries could have
19938 relocations against symbols in their debug information - the
19939 minimal symbol would have the right address, but the debug info
19940 would not. It's no longer necessary, because we will explicitly
19941 apply relocations when we read in the debug information now. */
19942
19943 /* A DW_AT_location attribute with no contents indicates that a
19944 variable has been optimized away. */
19945 if (attr->form_is_block () && DW_BLOCK (attr)->size == 0)
19946 {
19947 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
19948 return;
19949 }
19950
19951 /* Handle one degenerate form of location expression specially, to
19952 preserve GDB's previous behavior when section offsets are
19953 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
19954 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
19955
19956 if (attr->form_is_block ()
19957 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
19958 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
19959 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
19960 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
19961 && (DW_BLOCK (attr)->size
19962 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
19963 {
19964 unsigned int dummy;
19965
19966 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
19967 SET_SYMBOL_VALUE_ADDRESS
19968 (sym, cu->header.read_address (objfile->obfd,
19969 DW_BLOCK (attr)->data + 1,
19970 &dummy));
19971 else
19972 SET_SYMBOL_VALUE_ADDRESS
19973 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
19974 &dummy));
19975 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
19976 fixup_symbol_section (sym, objfile);
19977 SET_SYMBOL_VALUE_ADDRESS
19978 (sym,
19979 SYMBOL_VALUE_ADDRESS (sym)
19980 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
19981 return;
19982 }
19983
19984 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
19985 expression evaluator, and use LOC_COMPUTED only when necessary
19986 (i.e. when the value of a register or memory location is
19987 referenced, or a thread-local block, etc.). Then again, it might
19988 not be worthwhile. I'm assuming that it isn't unless performance
19989 or memory numbers show me otherwise. */
19990
19991 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
19992
19993 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
19994 cu->has_loclist = true;
19995 }
19996
19997 /* Given a pointer to a DWARF information entry, figure out if we need
19998 to make a symbol table entry for it, and if so, create a new entry
19999 and return a pointer to it.
20000 If TYPE is NULL, determine symbol type from the die, otherwise
20001 used the passed type.
20002 If SPACE is not NULL, use it to hold the new symbol. If it is
20003 NULL, allocate a new symbol on the objfile's obstack. */
20004
20005 static struct symbol *
20006 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
20007 struct symbol *space)
20008 {
20009 struct dwarf2_per_objfile *dwarf2_per_objfile
20010 = cu->per_cu->dwarf2_per_objfile;
20011 struct objfile *objfile = dwarf2_per_objfile->objfile;
20012 struct gdbarch *gdbarch = get_objfile_arch (objfile);
20013 struct symbol *sym = NULL;
20014 const char *name;
20015 struct attribute *attr = NULL;
20016 struct attribute *attr2 = NULL;
20017 CORE_ADDR baseaddr;
20018 struct pending **list_to_add = NULL;
20019
20020 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
20021
20022 baseaddr = objfile->text_section_offset ();
20023
20024 name = dwarf2_name (die, cu);
20025 if (name)
20026 {
20027 const char *linkagename;
20028 int suppress_add = 0;
20029
20030 if (space)
20031 sym = space;
20032 else
20033 sym = allocate_symbol (objfile);
20034 OBJSTAT (objfile, n_syms++);
20035
20036 /* Cache this symbol's name and the name's demangled form (if any). */
20037 sym->set_language (cu->language, &objfile->objfile_obstack);
20038 linkagename = dwarf2_physname (name, die, cu);
20039 sym->compute_and_set_names (linkagename, false, objfile->per_bfd);
20040
20041 /* Fortran does not have mangling standard and the mangling does differ
20042 between gfortran, iFort etc. */
20043 if (cu->language == language_fortran
20044 && symbol_get_demangled_name (sym) == NULL)
20045 symbol_set_demangled_name (sym,
20046 dwarf2_full_name (name, die, cu),
20047 NULL);
20048
20049 /* Default assumptions.
20050 Use the passed type or decode it from the die. */
20051 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20052 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
20053 if (type != NULL)
20054 SYMBOL_TYPE (sym) = type;
20055 else
20056 SYMBOL_TYPE (sym) = die_type (die, cu);
20057 attr = dwarf2_attr (die,
20058 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
20059 cu);
20060 if (attr != nullptr)
20061 {
20062 SYMBOL_LINE (sym) = DW_UNSND (attr);
20063 }
20064
20065 attr = dwarf2_attr (die,
20066 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
20067 cu);
20068 if (attr != nullptr)
20069 {
20070 file_name_index file_index = (file_name_index) DW_UNSND (attr);
20071 struct file_entry *fe;
20072
20073 if (cu->line_header != NULL)
20074 fe = cu->line_header->file_name_at (file_index);
20075 else
20076 fe = NULL;
20077
20078 if (fe == NULL)
20079 complaint (_("file index out of range"));
20080 else
20081 symbol_set_symtab (sym, fe->symtab);
20082 }
20083
20084 switch (die->tag)
20085 {
20086 case DW_TAG_label:
20087 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
20088 if (attr != nullptr)
20089 {
20090 CORE_ADDR addr;
20091
20092 addr = attr->value_as_address ();
20093 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
20094 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
20095 }
20096 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
20097 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
20098 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
20099 add_symbol_to_list (sym, cu->list_in_scope);
20100 break;
20101 case DW_TAG_subprogram:
20102 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20103 finish_block. */
20104 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20105 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20106 if ((attr2 && (DW_UNSND (attr2) != 0))
20107 || cu->language == language_ada
20108 || cu->language == language_fortran)
20109 {
20110 /* Subprograms marked external are stored as a global symbol.
20111 Ada and Fortran subprograms, whether marked external or
20112 not, are always stored as a global symbol, because we want
20113 to be able to access them globally. For instance, we want
20114 to be able to break on a nested subprogram without having
20115 to specify the context. */
20116 list_to_add = cu->get_builder ()->get_global_symbols ();
20117 }
20118 else
20119 {
20120 list_to_add = cu->list_in_scope;
20121 }
20122 break;
20123 case DW_TAG_inlined_subroutine:
20124 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20125 finish_block. */
20126 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20127 SYMBOL_INLINED (sym) = 1;
20128 list_to_add = cu->list_in_scope;
20129 break;
20130 case DW_TAG_template_value_param:
20131 suppress_add = 1;
20132 /* Fall through. */
20133 case DW_TAG_constant:
20134 case DW_TAG_variable:
20135 case DW_TAG_member:
20136 /* Compilation with minimal debug info may result in
20137 variables with missing type entries. Change the
20138 misleading `void' type to something sensible. */
20139 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
20140 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
20141
20142 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20143 /* In the case of DW_TAG_member, we should only be called for
20144 static const members. */
20145 if (die->tag == DW_TAG_member)
20146 {
20147 /* dwarf2_add_field uses die_is_declaration,
20148 so we do the same. */
20149 gdb_assert (die_is_declaration (die, cu));
20150 gdb_assert (attr);
20151 }
20152 if (attr != nullptr)
20153 {
20154 dwarf2_const_value (attr, sym, cu);
20155 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20156 if (!suppress_add)
20157 {
20158 if (attr2 && (DW_UNSND (attr2) != 0))
20159 list_to_add = cu->get_builder ()->get_global_symbols ();
20160 else
20161 list_to_add = cu->list_in_scope;
20162 }
20163 break;
20164 }
20165 attr = dwarf2_attr (die, DW_AT_location, cu);
20166 if (attr != nullptr)
20167 {
20168 var_decode_location (attr, sym, cu);
20169 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20170
20171 /* Fortran explicitly imports any global symbols to the local
20172 scope by DW_TAG_common_block. */
20173 if (cu->language == language_fortran && die->parent
20174 && die->parent->tag == DW_TAG_common_block)
20175 attr2 = NULL;
20176
20177 if (SYMBOL_CLASS (sym) == LOC_STATIC
20178 && SYMBOL_VALUE_ADDRESS (sym) == 0
20179 && !dwarf2_per_objfile->has_section_at_zero)
20180 {
20181 /* When a static variable is eliminated by the linker,
20182 the corresponding debug information is not stripped
20183 out, but the variable address is set to null;
20184 do not add such variables into symbol table. */
20185 }
20186 else if (attr2 && (DW_UNSND (attr2) != 0))
20187 {
20188 if (SYMBOL_CLASS (sym) == LOC_STATIC
20189 && (objfile->flags & OBJF_MAINLINE) == 0
20190 && dwarf2_per_objfile->can_copy)
20191 {
20192 /* A global static variable might be subject to
20193 copy relocation. We first check for a local
20194 minsym, though, because maybe the symbol was
20195 marked hidden, in which case this would not
20196 apply. */
20197 bound_minimal_symbol found
20198 = (lookup_minimal_symbol_linkage
20199 (sym->linkage_name (), objfile));
20200 if (found.minsym != nullptr)
20201 sym->maybe_copied = 1;
20202 }
20203
20204 /* A variable with DW_AT_external is never static,
20205 but it may be block-scoped. */
20206 list_to_add
20207 = ((cu->list_in_scope
20208 == cu->get_builder ()->get_file_symbols ())
20209 ? cu->get_builder ()->get_global_symbols ()
20210 : cu->list_in_scope);
20211 }
20212 else
20213 list_to_add = cu->list_in_scope;
20214 }
20215 else
20216 {
20217 /* We do not know the address of this symbol.
20218 If it is an external symbol and we have type information
20219 for it, enter the symbol as a LOC_UNRESOLVED symbol.
20220 The address of the variable will then be determined from
20221 the minimal symbol table whenever the variable is
20222 referenced. */
20223 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20224
20225 /* Fortran explicitly imports any global symbols to the local
20226 scope by DW_TAG_common_block. */
20227 if (cu->language == language_fortran && die->parent
20228 && die->parent->tag == DW_TAG_common_block)
20229 {
20230 /* SYMBOL_CLASS doesn't matter here because
20231 read_common_block is going to reset it. */
20232 if (!suppress_add)
20233 list_to_add = cu->list_in_scope;
20234 }
20235 else if (attr2 && (DW_UNSND (attr2) != 0)
20236 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
20237 {
20238 /* A variable with DW_AT_external is never static, but it
20239 may be block-scoped. */
20240 list_to_add
20241 = ((cu->list_in_scope
20242 == cu->get_builder ()->get_file_symbols ())
20243 ? cu->get_builder ()->get_global_symbols ()
20244 : cu->list_in_scope);
20245
20246 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
20247 }
20248 else if (!die_is_declaration (die, cu))
20249 {
20250 /* Use the default LOC_OPTIMIZED_OUT class. */
20251 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
20252 if (!suppress_add)
20253 list_to_add = cu->list_in_scope;
20254 }
20255 }
20256 break;
20257 case DW_TAG_formal_parameter:
20258 {
20259 /* If we are inside a function, mark this as an argument. If
20260 not, we might be looking at an argument to an inlined function
20261 when we do not have enough information to show inlined frames;
20262 pretend it's a local variable in that case so that the user can
20263 still see it. */
20264 struct context_stack *curr
20265 = cu->get_builder ()->get_current_context_stack ();
20266 if (curr != nullptr && curr->name != nullptr)
20267 SYMBOL_IS_ARGUMENT (sym) = 1;
20268 attr = dwarf2_attr (die, DW_AT_location, cu);
20269 if (attr != nullptr)
20270 {
20271 var_decode_location (attr, sym, cu);
20272 }
20273 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20274 if (attr != nullptr)
20275 {
20276 dwarf2_const_value (attr, sym, cu);
20277 }
20278
20279 list_to_add = cu->list_in_scope;
20280 }
20281 break;
20282 case DW_TAG_unspecified_parameters:
20283 /* From varargs functions; gdb doesn't seem to have any
20284 interest in this information, so just ignore it for now.
20285 (FIXME?) */
20286 break;
20287 case DW_TAG_template_type_param:
20288 suppress_add = 1;
20289 /* Fall through. */
20290 case DW_TAG_class_type:
20291 case DW_TAG_interface_type:
20292 case DW_TAG_structure_type:
20293 case DW_TAG_union_type:
20294 case DW_TAG_set_type:
20295 case DW_TAG_enumeration_type:
20296 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20297 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
20298
20299 {
20300 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
20301 really ever be static objects: otherwise, if you try
20302 to, say, break of a class's method and you're in a file
20303 which doesn't mention that class, it won't work unless
20304 the check for all static symbols in lookup_symbol_aux
20305 saves you. See the OtherFileClass tests in
20306 gdb.c++/namespace.exp. */
20307
20308 if (!suppress_add)
20309 {
20310 buildsym_compunit *builder = cu->get_builder ();
20311 list_to_add
20312 = (cu->list_in_scope == builder->get_file_symbols ()
20313 && cu->language == language_cplus
20314 ? builder->get_global_symbols ()
20315 : cu->list_in_scope);
20316
20317 /* The semantics of C++ state that "struct foo {
20318 ... }" also defines a typedef for "foo". */
20319 if (cu->language == language_cplus
20320 || cu->language == language_ada
20321 || cu->language == language_d
20322 || cu->language == language_rust)
20323 {
20324 /* The symbol's name is already allocated along
20325 with this objfile, so we don't need to
20326 duplicate it for the type. */
20327 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
20328 TYPE_NAME (SYMBOL_TYPE (sym)) = sym->search_name ();
20329 }
20330 }
20331 }
20332 break;
20333 case DW_TAG_typedef:
20334 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20335 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20336 list_to_add = cu->list_in_scope;
20337 break;
20338 case DW_TAG_base_type:
20339 case DW_TAG_subrange_type:
20340 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20341 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20342 list_to_add = cu->list_in_scope;
20343 break;
20344 case DW_TAG_enumerator:
20345 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20346 if (attr != nullptr)
20347 {
20348 dwarf2_const_value (attr, sym, cu);
20349 }
20350 {
20351 /* NOTE: carlton/2003-11-10: See comment above in the
20352 DW_TAG_class_type, etc. block. */
20353
20354 list_to_add
20355 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
20356 && cu->language == language_cplus
20357 ? cu->get_builder ()->get_global_symbols ()
20358 : cu->list_in_scope);
20359 }
20360 break;
20361 case DW_TAG_imported_declaration:
20362 case DW_TAG_namespace:
20363 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20364 list_to_add = cu->get_builder ()->get_global_symbols ();
20365 break;
20366 case DW_TAG_module:
20367 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20368 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
20369 list_to_add = cu->get_builder ()->get_global_symbols ();
20370 break;
20371 case DW_TAG_common_block:
20372 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
20373 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
20374 add_symbol_to_list (sym, cu->list_in_scope);
20375 break;
20376 default:
20377 /* Not a tag we recognize. Hopefully we aren't processing
20378 trash data, but since we must specifically ignore things
20379 we don't recognize, there is nothing else we should do at
20380 this point. */
20381 complaint (_("unsupported tag: '%s'"),
20382 dwarf_tag_name (die->tag));
20383 break;
20384 }
20385
20386 if (suppress_add)
20387 {
20388 sym->hash_next = objfile->template_symbols;
20389 objfile->template_symbols = sym;
20390 list_to_add = NULL;
20391 }
20392
20393 if (list_to_add != NULL)
20394 add_symbol_to_list (sym, list_to_add);
20395
20396 /* For the benefit of old versions of GCC, check for anonymous
20397 namespaces based on the demangled name. */
20398 if (!cu->processing_has_namespace_info
20399 && cu->language == language_cplus)
20400 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
20401 }
20402 return (sym);
20403 }
20404
20405 /* Given an attr with a DW_FORM_dataN value in host byte order,
20406 zero-extend it as appropriate for the symbol's type. The DWARF
20407 standard (v4) is not entirely clear about the meaning of using
20408 DW_FORM_dataN for a constant with a signed type, where the type is
20409 wider than the data. The conclusion of a discussion on the DWARF
20410 list was that this is unspecified. We choose to always zero-extend
20411 because that is the interpretation long in use by GCC. */
20412
20413 static gdb_byte *
20414 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
20415 struct dwarf2_cu *cu, LONGEST *value, int bits)
20416 {
20417 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20418 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
20419 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
20420 LONGEST l = DW_UNSND (attr);
20421
20422 if (bits < sizeof (*value) * 8)
20423 {
20424 l &= ((LONGEST) 1 << bits) - 1;
20425 *value = l;
20426 }
20427 else if (bits == sizeof (*value) * 8)
20428 *value = l;
20429 else
20430 {
20431 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
20432 store_unsigned_integer (bytes, bits / 8, byte_order, l);
20433 return bytes;
20434 }
20435
20436 return NULL;
20437 }
20438
20439 /* Read a constant value from an attribute. Either set *VALUE, or if
20440 the value does not fit in *VALUE, set *BYTES - either already
20441 allocated on the objfile obstack, or newly allocated on OBSTACK,
20442 or, set *BATON, if we translated the constant to a location
20443 expression. */
20444
20445 static void
20446 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
20447 const char *name, struct obstack *obstack,
20448 struct dwarf2_cu *cu,
20449 LONGEST *value, const gdb_byte **bytes,
20450 struct dwarf2_locexpr_baton **baton)
20451 {
20452 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20453 struct comp_unit_head *cu_header = &cu->header;
20454 struct dwarf_block *blk;
20455 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
20456 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
20457
20458 *value = 0;
20459 *bytes = NULL;
20460 *baton = NULL;
20461
20462 switch (attr->form)
20463 {
20464 case DW_FORM_addr:
20465 case DW_FORM_addrx:
20466 case DW_FORM_GNU_addr_index:
20467 {
20468 gdb_byte *data;
20469
20470 if (TYPE_LENGTH (type) != cu_header->addr_size)
20471 dwarf2_const_value_length_mismatch_complaint (name,
20472 cu_header->addr_size,
20473 TYPE_LENGTH (type));
20474 /* Symbols of this form are reasonably rare, so we just
20475 piggyback on the existing location code rather than writing
20476 a new implementation of symbol_computed_ops. */
20477 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
20478 (*baton)->per_cu = cu->per_cu;
20479 gdb_assert ((*baton)->per_cu);
20480
20481 (*baton)->size = 2 + cu_header->addr_size;
20482 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
20483 (*baton)->data = data;
20484
20485 data[0] = DW_OP_addr;
20486 store_unsigned_integer (&data[1], cu_header->addr_size,
20487 byte_order, DW_ADDR (attr));
20488 data[cu_header->addr_size + 1] = DW_OP_stack_value;
20489 }
20490 break;
20491 case DW_FORM_string:
20492 case DW_FORM_strp:
20493 case DW_FORM_strx:
20494 case DW_FORM_GNU_str_index:
20495 case DW_FORM_GNU_strp_alt:
20496 /* DW_STRING is already allocated on the objfile obstack, point
20497 directly to it. */
20498 *bytes = (const gdb_byte *) DW_STRING (attr);
20499 break;
20500 case DW_FORM_block1:
20501 case DW_FORM_block2:
20502 case DW_FORM_block4:
20503 case DW_FORM_block:
20504 case DW_FORM_exprloc:
20505 case DW_FORM_data16:
20506 blk = DW_BLOCK (attr);
20507 if (TYPE_LENGTH (type) != blk->size)
20508 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
20509 TYPE_LENGTH (type));
20510 *bytes = blk->data;
20511 break;
20512
20513 /* The DW_AT_const_value attributes are supposed to carry the
20514 symbol's value "represented as it would be on the target
20515 architecture." By the time we get here, it's already been
20516 converted to host endianness, so we just need to sign- or
20517 zero-extend it as appropriate. */
20518 case DW_FORM_data1:
20519 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
20520 break;
20521 case DW_FORM_data2:
20522 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
20523 break;
20524 case DW_FORM_data4:
20525 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
20526 break;
20527 case DW_FORM_data8:
20528 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
20529 break;
20530
20531 case DW_FORM_sdata:
20532 case DW_FORM_implicit_const:
20533 *value = DW_SND (attr);
20534 break;
20535
20536 case DW_FORM_udata:
20537 *value = DW_UNSND (attr);
20538 break;
20539
20540 default:
20541 complaint (_("unsupported const value attribute form: '%s'"),
20542 dwarf_form_name (attr->form));
20543 *value = 0;
20544 break;
20545 }
20546 }
20547
20548
20549 /* Copy constant value from an attribute to a symbol. */
20550
20551 static void
20552 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
20553 struct dwarf2_cu *cu)
20554 {
20555 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20556 LONGEST value;
20557 const gdb_byte *bytes;
20558 struct dwarf2_locexpr_baton *baton;
20559
20560 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
20561 sym->print_name (),
20562 &objfile->objfile_obstack, cu,
20563 &value, &bytes, &baton);
20564
20565 if (baton != NULL)
20566 {
20567 SYMBOL_LOCATION_BATON (sym) = baton;
20568 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
20569 }
20570 else if (bytes != NULL)
20571 {
20572 SYMBOL_VALUE_BYTES (sym) = bytes;
20573 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
20574 }
20575 else
20576 {
20577 SYMBOL_VALUE (sym) = value;
20578 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
20579 }
20580 }
20581
20582 /* Return the type of the die in question using its DW_AT_type attribute. */
20583
20584 static struct type *
20585 die_type (struct die_info *die, struct dwarf2_cu *cu)
20586 {
20587 struct attribute *type_attr;
20588
20589 type_attr = dwarf2_attr (die, DW_AT_type, cu);
20590 if (!type_attr)
20591 {
20592 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20593 /* A missing DW_AT_type represents a void type. */
20594 return objfile_type (objfile)->builtin_void;
20595 }
20596
20597 return lookup_die_type (die, type_attr, cu);
20598 }
20599
20600 /* True iff CU's producer generates GNAT Ada auxiliary information
20601 that allows to find parallel types through that information instead
20602 of having to do expensive parallel lookups by type name. */
20603
20604 static int
20605 need_gnat_info (struct dwarf2_cu *cu)
20606 {
20607 /* Assume that the Ada compiler was GNAT, which always produces
20608 the auxiliary information. */
20609 return (cu->language == language_ada);
20610 }
20611
20612 /* Return the auxiliary type of the die in question using its
20613 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
20614 attribute is not present. */
20615
20616 static struct type *
20617 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
20618 {
20619 struct attribute *type_attr;
20620
20621 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
20622 if (!type_attr)
20623 return NULL;
20624
20625 return lookup_die_type (die, type_attr, cu);
20626 }
20627
20628 /* If DIE has a descriptive_type attribute, then set the TYPE's
20629 descriptive type accordingly. */
20630
20631 static void
20632 set_descriptive_type (struct type *type, struct die_info *die,
20633 struct dwarf2_cu *cu)
20634 {
20635 struct type *descriptive_type = die_descriptive_type (die, cu);
20636
20637 if (descriptive_type)
20638 {
20639 ALLOCATE_GNAT_AUX_TYPE (type);
20640 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
20641 }
20642 }
20643
20644 /* Return the containing type of the die in question using its
20645 DW_AT_containing_type attribute. */
20646
20647 static struct type *
20648 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
20649 {
20650 struct attribute *type_attr;
20651 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20652
20653 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
20654 if (!type_attr)
20655 error (_("Dwarf Error: Problem turning containing type into gdb type "
20656 "[in module %s]"), objfile_name (objfile));
20657
20658 return lookup_die_type (die, type_attr, cu);
20659 }
20660
20661 /* Return an error marker type to use for the ill formed type in DIE/CU. */
20662
20663 static struct type *
20664 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
20665 {
20666 struct dwarf2_per_objfile *dwarf2_per_objfile
20667 = cu->per_cu->dwarf2_per_objfile;
20668 struct objfile *objfile = dwarf2_per_objfile->objfile;
20669 char *saved;
20670
20671 std::string message
20672 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
20673 objfile_name (objfile),
20674 sect_offset_str (cu->header.sect_off),
20675 sect_offset_str (die->sect_off));
20676 saved = obstack_strdup (&objfile->objfile_obstack, message);
20677
20678 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
20679 }
20680
20681 /* Look up the type of DIE in CU using its type attribute ATTR.
20682 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
20683 DW_AT_containing_type.
20684 If there is no type substitute an error marker. */
20685
20686 static struct type *
20687 lookup_die_type (struct die_info *die, const struct attribute *attr,
20688 struct dwarf2_cu *cu)
20689 {
20690 struct dwarf2_per_objfile *dwarf2_per_objfile
20691 = cu->per_cu->dwarf2_per_objfile;
20692 struct objfile *objfile = dwarf2_per_objfile->objfile;
20693 struct type *this_type;
20694
20695 gdb_assert (attr->name == DW_AT_type
20696 || attr->name == DW_AT_GNAT_descriptive_type
20697 || attr->name == DW_AT_containing_type);
20698
20699 /* First see if we have it cached. */
20700
20701 if (attr->form == DW_FORM_GNU_ref_alt)
20702 {
20703 struct dwarf2_per_cu_data *per_cu;
20704 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
20705
20706 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
20707 dwarf2_per_objfile);
20708 this_type = get_die_type_at_offset (sect_off, per_cu);
20709 }
20710 else if (attr->form_is_ref ())
20711 {
20712 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
20713
20714 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
20715 }
20716 else if (attr->form == DW_FORM_ref_sig8)
20717 {
20718 ULONGEST signature = DW_SIGNATURE (attr);
20719
20720 return get_signatured_type (die, signature, cu);
20721 }
20722 else
20723 {
20724 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
20725 " at %s [in module %s]"),
20726 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
20727 objfile_name (objfile));
20728 return build_error_marker_type (cu, die);
20729 }
20730
20731 /* If not cached we need to read it in. */
20732
20733 if (this_type == NULL)
20734 {
20735 struct die_info *type_die = NULL;
20736 struct dwarf2_cu *type_cu = cu;
20737
20738 if (attr->form_is_ref ())
20739 type_die = follow_die_ref (die, attr, &type_cu);
20740 if (type_die == NULL)
20741 return build_error_marker_type (cu, die);
20742 /* If we find the type now, it's probably because the type came
20743 from an inter-CU reference and the type's CU got expanded before
20744 ours. */
20745 this_type = read_type_die (type_die, type_cu);
20746 }
20747
20748 /* If we still don't have a type use an error marker. */
20749
20750 if (this_type == NULL)
20751 return build_error_marker_type (cu, die);
20752
20753 return this_type;
20754 }
20755
20756 /* Return the type in DIE, CU.
20757 Returns NULL for invalid types.
20758
20759 This first does a lookup in die_type_hash,
20760 and only reads the die in if necessary.
20761
20762 NOTE: This can be called when reading in partial or full symbols. */
20763
20764 static struct type *
20765 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
20766 {
20767 struct type *this_type;
20768
20769 this_type = get_die_type (die, cu);
20770 if (this_type)
20771 return this_type;
20772
20773 return read_type_die_1 (die, cu);
20774 }
20775
20776 /* Read the type in DIE, CU.
20777 Returns NULL for invalid types. */
20778
20779 static struct type *
20780 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
20781 {
20782 struct type *this_type = NULL;
20783
20784 switch (die->tag)
20785 {
20786 case DW_TAG_class_type:
20787 case DW_TAG_interface_type:
20788 case DW_TAG_structure_type:
20789 case DW_TAG_union_type:
20790 this_type = read_structure_type (die, cu);
20791 break;
20792 case DW_TAG_enumeration_type:
20793 this_type = read_enumeration_type (die, cu);
20794 break;
20795 case DW_TAG_subprogram:
20796 case DW_TAG_subroutine_type:
20797 case DW_TAG_inlined_subroutine:
20798 this_type = read_subroutine_type (die, cu);
20799 break;
20800 case DW_TAG_array_type:
20801 this_type = read_array_type (die, cu);
20802 break;
20803 case DW_TAG_set_type:
20804 this_type = read_set_type (die, cu);
20805 break;
20806 case DW_TAG_pointer_type:
20807 this_type = read_tag_pointer_type (die, cu);
20808 break;
20809 case DW_TAG_ptr_to_member_type:
20810 this_type = read_tag_ptr_to_member_type (die, cu);
20811 break;
20812 case DW_TAG_reference_type:
20813 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
20814 break;
20815 case DW_TAG_rvalue_reference_type:
20816 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
20817 break;
20818 case DW_TAG_const_type:
20819 this_type = read_tag_const_type (die, cu);
20820 break;
20821 case DW_TAG_volatile_type:
20822 this_type = read_tag_volatile_type (die, cu);
20823 break;
20824 case DW_TAG_restrict_type:
20825 this_type = read_tag_restrict_type (die, cu);
20826 break;
20827 case DW_TAG_string_type:
20828 this_type = read_tag_string_type (die, cu);
20829 break;
20830 case DW_TAG_typedef:
20831 this_type = read_typedef (die, cu);
20832 break;
20833 case DW_TAG_subrange_type:
20834 this_type = read_subrange_type (die, cu);
20835 break;
20836 case DW_TAG_base_type:
20837 this_type = read_base_type (die, cu);
20838 break;
20839 case DW_TAG_unspecified_type:
20840 this_type = read_unspecified_type (die, cu);
20841 break;
20842 case DW_TAG_namespace:
20843 this_type = read_namespace_type (die, cu);
20844 break;
20845 case DW_TAG_module:
20846 this_type = read_module_type (die, cu);
20847 break;
20848 case DW_TAG_atomic_type:
20849 this_type = read_tag_atomic_type (die, cu);
20850 break;
20851 default:
20852 complaint (_("unexpected tag in read_type_die: '%s'"),
20853 dwarf_tag_name (die->tag));
20854 break;
20855 }
20856
20857 return this_type;
20858 }
20859
20860 /* See if we can figure out if the class lives in a namespace. We do
20861 this by looking for a member function; its demangled name will
20862 contain namespace info, if there is any.
20863 Return the computed name or NULL.
20864 Space for the result is allocated on the objfile's obstack.
20865 This is the full-die version of guess_partial_die_structure_name.
20866 In this case we know DIE has no useful parent. */
20867
20868 static const char *
20869 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
20870 {
20871 struct die_info *spec_die;
20872 struct dwarf2_cu *spec_cu;
20873 struct die_info *child;
20874 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20875
20876 spec_cu = cu;
20877 spec_die = die_specification (die, &spec_cu);
20878 if (spec_die != NULL)
20879 {
20880 die = spec_die;
20881 cu = spec_cu;
20882 }
20883
20884 for (child = die->child;
20885 child != NULL;
20886 child = child->sibling)
20887 {
20888 if (child->tag == DW_TAG_subprogram)
20889 {
20890 const char *linkage_name = dw2_linkage_name (child, cu);
20891
20892 if (linkage_name != NULL)
20893 {
20894 gdb::unique_xmalloc_ptr<char> actual_name
20895 (language_class_name_from_physname (cu->language_defn,
20896 linkage_name));
20897 const char *name = NULL;
20898
20899 if (actual_name != NULL)
20900 {
20901 const char *die_name = dwarf2_name (die, cu);
20902
20903 if (die_name != NULL
20904 && strcmp (die_name, actual_name.get ()) != 0)
20905 {
20906 /* Strip off the class name from the full name.
20907 We want the prefix. */
20908 int die_name_len = strlen (die_name);
20909 int actual_name_len = strlen (actual_name.get ());
20910 const char *ptr = actual_name.get ();
20911
20912 /* Test for '::' as a sanity check. */
20913 if (actual_name_len > die_name_len + 2
20914 && ptr[actual_name_len - die_name_len - 1] == ':')
20915 name = obstack_strndup (
20916 &objfile->per_bfd->storage_obstack,
20917 ptr, actual_name_len - die_name_len - 2);
20918 }
20919 }
20920 return name;
20921 }
20922 }
20923 }
20924
20925 return NULL;
20926 }
20927
20928 /* GCC might emit a nameless typedef that has a linkage name. Determine the
20929 prefix part in such case. See
20930 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
20931
20932 static const char *
20933 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
20934 {
20935 struct attribute *attr;
20936 const char *base;
20937
20938 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
20939 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
20940 return NULL;
20941
20942 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
20943 return NULL;
20944
20945 attr = dw2_linkage_name_attr (die, cu);
20946 if (attr == NULL || DW_STRING (attr) == NULL)
20947 return NULL;
20948
20949 /* dwarf2_name had to be already called. */
20950 gdb_assert (DW_STRING_IS_CANONICAL (attr));
20951
20952 /* Strip the base name, keep any leading namespaces/classes. */
20953 base = strrchr (DW_STRING (attr), ':');
20954 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
20955 return "";
20956
20957 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20958 return obstack_strndup (&objfile->per_bfd->storage_obstack,
20959 DW_STRING (attr),
20960 &base[-1] - DW_STRING (attr));
20961 }
20962
20963 /* Return the name of the namespace/class that DIE is defined within,
20964 or "" if we can't tell. The caller should not xfree the result.
20965
20966 For example, if we're within the method foo() in the following
20967 code:
20968
20969 namespace N {
20970 class C {
20971 void foo () {
20972 }
20973 };
20974 }
20975
20976 then determine_prefix on foo's die will return "N::C". */
20977
20978 static const char *
20979 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
20980 {
20981 struct dwarf2_per_objfile *dwarf2_per_objfile
20982 = cu->per_cu->dwarf2_per_objfile;
20983 struct die_info *parent, *spec_die;
20984 struct dwarf2_cu *spec_cu;
20985 struct type *parent_type;
20986 const char *retval;
20987
20988 if (cu->language != language_cplus
20989 && cu->language != language_fortran && cu->language != language_d
20990 && cu->language != language_rust)
20991 return "";
20992
20993 retval = anonymous_struct_prefix (die, cu);
20994 if (retval)
20995 return retval;
20996
20997 /* We have to be careful in the presence of DW_AT_specification.
20998 For example, with GCC 3.4, given the code
20999
21000 namespace N {
21001 void foo() {
21002 // Definition of N::foo.
21003 }
21004 }
21005
21006 then we'll have a tree of DIEs like this:
21007
21008 1: DW_TAG_compile_unit
21009 2: DW_TAG_namespace // N
21010 3: DW_TAG_subprogram // declaration of N::foo
21011 4: DW_TAG_subprogram // definition of N::foo
21012 DW_AT_specification // refers to die #3
21013
21014 Thus, when processing die #4, we have to pretend that we're in
21015 the context of its DW_AT_specification, namely the contex of die
21016 #3. */
21017 spec_cu = cu;
21018 spec_die = die_specification (die, &spec_cu);
21019 if (spec_die == NULL)
21020 parent = die->parent;
21021 else
21022 {
21023 parent = spec_die->parent;
21024 cu = spec_cu;
21025 }
21026
21027 if (parent == NULL)
21028 return "";
21029 else if (parent->building_fullname)
21030 {
21031 const char *name;
21032 const char *parent_name;
21033
21034 /* It has been seen on RealView 2.2 built binaries,
21035 DW_TAG_template_type_param types actually _defined_ as
21036 children of the parent class:
21037
21038 enum E {};
21039 template class <class Enum> Class{};
21040 Class<enum E> class_e;
21041
21042 1: DW_TAG_class_type (Class)
21043 2: DW_TAG_enumeration_type (E)
21044 3: DW_TAG_enumerator (enum1:0)
21045 3: DW_TAG_enumerator (enum2:1)
21046 ...
21047 2: DW_TAG_template_type_param
21048 DW_AT_type DW_FORM_ref_udata (E)
21049
21050 Besides being broken debug info, it can put GDB into an
21051 infinite loop. Consider:
21052
21053 When we're building the full name for Class<E>, we'll start
21054 at Class, and go look over its template type parameters,
21055 finding E. We'll then try to build the full name of E, and
21056 reach here. We're now trying to build the full name of E,
21057 and look over the parent DIE for containing scope. In the
21058 broken case, if we followed the parent DIE of E, we'd again
21059 find Class, and once again go look at its template type
21060 arguments, etc., etc. Simply don't consider such parent die
21061 as source-level parent of this die (it can't be, the language
21062 doesn't allow it), and break the loop here. */
21063 name = dwarf2_name (die, cu);
21064 parent_name = dwarf2_name (parent, cu);
21065 complaint (_("template param type '%s' defined within parent '%s'"),
21066 name ? name : "<unknown>",
21067 parent_name ? parent_name : "<unknown>");
21068 return "";
21069 }
21070 else
21071 switch (parent->tag)
21072 {
21073 case DW_TAG_namespace:
21074 parent_type = read_type_die (parent, cu);
21075 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
21076 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
21077 Work around this problem here. */
21078 if (cu->language == language_cplus
21079 && strcmp (TYPE_NAME (parent_type), "::") == 0)
21080 return "";
21081 /* We give a name to even anonymous namespaces. */
21082 return TYPE_NAME (parent_type);
21083 case DW_TAG_class_type:
21084 case DW_TAG_interface_type:
21085 case DW_TAG_structure_type:
21086 case DW_TAG_union_type:
21087 case DW_TAG_module:
21088 parent_type = read_type_die (parent, cu);
21089 if (TYPE_NAME (parent_type) != NULL)
21090 return TYPE_NAME (parent_type);
21091 else
21092 /* An anonymous structure is only allowed non-static data
21093 members; no typedefs, no member functions, et cetera.
21094 So it does not need a prefix. */
21095 return "";
21096 case DW_TAG_compile_unit:
21097 case DW_TAG_partial_unit:
21098 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
21099 if (cu->language == language_cplus
21100 && !dwarf2_per_objfile->types.empty ()
21101 && die->child != NULL
21102 && (die->tag == DW_TAG_class_type
21103 || die->tag == DW_TAG_structure_type
21104 || die->tag == DW_TAG_union_type))
21105 {
21106 const char *name = guess_full_die_structure_name (die, cu);
21107 if (name != NULL)
21108 return name;
21109 }
21110 return "";
21111 case DW_TAG_subprogram:
21112 /* Nested subroutines in Fortran get a prefix with the name
21113 of the parent's subroutine. */
21114 if (cu->language == language_fortran)
21115 {
21116 if ((die->tag == DW_TAG_subprogram)
21117 && (dwarf2_name (parent, cu) != NULL))
21118 return dwarf2_name (parent, cu);
21119 }
21120 return determine_prefix (parent, cu);
21121 case DW_TAG_enumeration_type:
21122 parent_type = read_type_die (parent, cu);
21123 if (TYPE_DECLARED_CLASS (parent_type))
21124 {
21125 if (TYPE_NAME (parent_type) != NULL)
21126 return TYPE_NAME (parent_type);
21127 return "";
21128 }
21129 /* Fall through. */
21130 default:
21131 return determine_prefix (parent, cu);
21132 }
21133 }
21134
21135 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
21136 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
21137 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
21138 an obconcat, otherwise allocate storage for the result. The CU argument is
21139 used to determine the language and hence, the appropriate separator. */
21140
21141 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
21142
21143 static char *
21144 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
21145 int physname, struct dwarf2_cu *cu)
21146 {
21147 const char *lead = "";
21148 const char *sep;
21149
21150 if (suffix == NULL || suffix[0] == '\0'
21151 || prefix == NULL || prefix[0] == '\0')
21152 sep = "";
21153 else if (cu->language == language_d)
21154 {
21155 /* For D, the 'main' function could be defined in any module, but it
21156 should never be prefixed. */
21157 if (strcmp (suffix, "D main") == 0)
21158 {
21159 prefix = "";
21160 sep = "";
21161 }
21162 else
21163 sep = ".";
21164 }
21165 else if (cu->language == language_fortran && physname)
21166 {
21167 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
21168 DW_AT_MIPS_linkage_name is preferred and used instead. */
21169
21170 lead = "__";
21171 sep = "_MOD_";
21172 }
21173 else
21174 sep = "::";
21175
21176 if (prefix == NULL)
21177 prefix = "";
21178 if (suffix == NULL)
21179 suffix = "";
21180
21181 if (obs == NULL)
21182 {
21183 char *retval
21184 = ((char *)
21185 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
21186
21187 strcpy (retval, lead);
21188 strcat (retval, prefix);
21189 strcat (retval, sep);
21190 strcat (retval, suffix);
21191 return retval;
21192 }
21193 else
21194 {
21195 /* We have an obstack. */
21196 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
21197 }
21198 }
21199
21200 /* Get name of a die, return NULL if not found. */
21201
21202 static const char *
21203 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
21204 struct objfile *objfile)
21205 {
21206 if (name && cu->language == language_cplus)
21207 {
21208 std::string canon_name = cp_canonicalize_string (name);
21209
21210 if (!canon_name.empty ())
21211 {
21212 if (canon_name != name)
21213 name = objfile->intern (canon_name);
21214 }
21215 }
21216
21217 return name;
21218 }
21219
21220 /* Get name of a die, return NULL if not found.
21221 Anonymous namespaces are converted to their magic string. */
21222
21223 static const char *
21224 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
21225 {
21226 struct attribute *attr;
21227 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21228
21229 attr = dwarf2_attr (die, DW_AT_name, cu);
21230 if ((!attr || !DW_STRING (attr))
21231 && die->tag != DW_TAG_namespace
21232 && die->tag != DW_TAG_class_type
21233 && die->tag != DW_TAG_interface_type
21234 && die->tag != DW_TAG_structure_type
21235 && die->tag != DW_TAG_union_type)
21236 return NULL;
21237
21238 switch (die->tag)
21239 {
21240 case DW_TAG_compile_unit:
21241 case DW_TAG_partial_unit:
21242 /* Compilation units have a DW_AT_name that is a filename, not
21243 a source language identifier. */
21244 case DW_TAG_enumeration_type:
21245 case DW_TAG_enumerator:
21246 /* These tags always have simple identifiers already; no need
21247 to canonicalize them. */
21248 return DW_STRING (attr);
21249
21250 case DW_TAG_namespace:
21251 if (attr != NULL && DW_STRING (attr) != NULL)
21252 return DW_STRING (attr);
21253 return CP_ANONYMOUS_NAMESPACE_STR;
21254
21255 case DW_TAG_class_type:
21256 case DW_TAG_interface_type:
21257 case DW_TAG_structure_type:
21258 case DW_TAG_union_type:
21259 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
21260 structures or unions. These were of the form "._%d" in GCC 4.1,
21261 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
21262 and GCC 4.4. We work around this problem by ignoring these. */
21263 if (attr && DW_STRING (attr)
21264 && (startswith (DW_STRING (attr), "._")
21265 || startswith (DW_STRING (attr), "<anonymous")))
21266 return NULL;
21267
21268 /* GCC might emit a nameless typedef that has a linkage name. See
21269 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21270 if (!attr || DW_STRING (attr) == NULL)
21271 {
21272 attr = dw2_linkage_name_attr (die, cu);
21273 if (attr == NULL || DW_STRING (attr) == NULL)
21274 return NULL;
21275
21276 /* Avoid demangling DW_STRING (attr) the second time on a second
21277 call for the same DIE. */
21278 if (!DW_STRING_IS_CANONICAL (attr))
21279 {
21280 gdb::unique_xmalloc_ptr<char> demangled
21281 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
21282 if (demangled == nullptr)
21283 return nullptr;
21284
21285 DW_STRING (attr) = objfile->intern (demangled.get ());
21286 DW_STRING_IS_CANONICAL (attr) = 1;
21287 }
21288
21289 /* Strip any leading namespaces/classes, keep only the base name.
21290 DW_AT_name for named DIEs does not contain the prefixes. */
21291 const char *base = strrchr (DW_STRING (attr), ':');
21292 if (base && base > DW_STRING (attr) && base[-1] == ':')
21293 return &base[1];
21294 else
21295 return DW_STRING (attr);
21296 }
21297 break;
21298
21299 default:
21300 break;
21301 }
21302
21303 if (!DW_STRING_IS_CANONICAL (attr))
21304 {
21305 DW_STRING (attr) = dwarf2_canonicalize_name (DW_STRING (attr), cu,
21306 objfile);
21307 DW_STRING_IS_CANONICAL (attr) = 1;
21308 }
21309 return DW_STRING (attr);
21310 }
21311
21312 /* Return the die that this die in an extension of, or NULL if there
21313 is none. *EXT_CU is the CU containing DIE on input, and the CU
21314 containing the return value on output. */
21315
21316 static struct die_info *
21317 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
21318 {
21319 struct attribute *attr;
21320
21321 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
21322 if (attr == NULL)
21323 return NULL;
21324
21325 return follow_die_ref (die, attr, ext_cu);
21326 }
21327
21328 static void
21329 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
21330 {
21331 unsigned int i;
21332
21333 print_spaces (indent, f);
21334 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
21335 dwarf_tag_name (die->tag), die->abbrev,
21336 sect_offset_str (die->sect_off));
21337
21338 if (die->parent != NULL)
21339 {
21340 print_spaces (indent, f);
21341 fprintf_unfiltered (f, " parent at offset: %s\n",
21342 sect_offset_str (die->parent->sect_off));
21343 }
21344
21345 print_spaces (indent, f);
21346 fprintf_unfiltered (f, " has children: %s\n",
21347 dwarf_bool_name (die->child != NULL));
21348
21349 print_spaces (indent, f);
21350 fprintf_unfiltered (f, " attributes:\n");
21351
21352 for (i = 0; i < die->num_attrs; ++i)
21353 {
21354 print_spaces (indent, f);
21355 fprintf_unfiltered (f, " %s (%s) ",
21356 dwarf_attr_name (die->attrs[i].name),
21357 dwarf_form_name (die->attrs[i].form));
21358
21359 switch (die->attrs[i].form)
21360 {
21361 case DW_FORM_addr:
21362 case DW_FORM_addrx:
21363 case DW_FORM_GNU_addr_index:
21364 fprintf_unfiltered (f, "address: ");
21365 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
21366 break;
21367 case DW_FORM_block2:
21368 case DW_FORM_block4:
21369 case DW_FORM_block:
21370 case DW_FORM_block1:
21371 fprintf_unfiltered (f, "block: size %s",
21372 pulongest (DW_BLOCK (&die->attrs[i])->size));
21373 break;
21374 case DW_FORM_exprloc:
21375 fprintf_unfiltered (f, "expression: size %s",
21376 pulongest (DW_BLOCK (&die->attrs[i])->size));
21377 break;
21378 case DW_FORM_data16:
21379 fprintf_unfiltered (f, "constant of 16 bytes");
21380 break;
21381 case DW_FORM_ref_addr:
21382 fprintf_unfiltered (f, "ref address: ");
21383 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21384 break;
21385 case DW_FORM_GNU_ref_alt:
21386 fprintf_unfiltered (f, "alt ref address: ");
21387 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21388 break;
21389 case DW_FORM_ref1:
21390 case DW_FORM_ref2:
21391 case DW_FORM_ref4:
21392 case DW_FORM_ref8:
21393 case DW_FORM_ref_udata:
21394 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
21395 (long) (DW_UNSND (&die->attrs[i])));
21396 break;
21397 case DW_FORM_data1:
21398 case DW_FORM_data2:
21399 case DW_FORM_data4:
21400 case DW_FORM_data8:
21401 case DW_FORM_udata:
21402 case DW_FORM_sdata:
21403 fprintf_unfiltered (f, "constant: %s",
21404 pulongest (DW_UNSND (&die->attrs[i])));
21405 break;
21406 case DW_FORM_sec_offset:
21407 fprintf_unfiltered (f, "section offset: %s",
21408 pulongest (DW_UNSND (&die->attrs[i])));
21409 break;
21410 case DW_FORM_ref_sig8:
21411 fprintf_unfiltered (f, "signature: %s",
21412 hex_string (DW_SIGNATURE (&die->attrs[i])));
21413 break;
21414 case DW_FORM_string:
21415 case DW_FORM_strp:
21416 case DW_FORM_line_strp:
21417 case DW_FORM_strx:
21418 case DW_FORM_GNU_str_index:
21419 case DW_FORM_GNU_strp_alt:
21420 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
21421 DW_STRING (&die->attrs[i])
21422 ? DW_STRING (&die->attrs[i]) : "",
21423 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
21424 break;
21425 case DW_FORM_flag:
21426 if (DW_UNSND (&die->attrs[i]))
21427 fprintf_unfiltered (f, "flag: TRUE");
21428 else
21429 fprintf_unfiltered (f, "flag: FALSE");
21430 break;
21431 case DW_FORM_flag_present:
21432 fprintf_unfiltered (f, "flag: TRUE");
21433 break;
21434 case DW_FORM_indirect:
21435 /* The reader will have reduced the indirect form to
21436 the "base form" so this form should not occur. */
21437 fprintf_unfiltered (f,
21438 "unexpected attribute form: DW_FORM_indirect");
21439 break;
21440 case DW_FORM_implicit_const:
21441 fprintf_unfiltered (f, "constant: %s",
21442 plongest (DW_SND (&die->attrs[i])));
21443 break;
21444 default:
21445 fprintf_unfiltered (f, "unsupported attribute form: %d.",
21446 die->attrs[i].form);
21447 break;
21448 }
21449 fprintf_unfiltered (f, "\n");
21450 }
21451 }
21452
21453 static void
21454 dump_die_for_error (struct die_info *die)
21455 {
21456 dump_die_shallow (gdb_stderr, 0, die);
21457 }
21458
21459 static void
21460 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
21461 {
21462 int indent = level * 4;
21463
21464 gdb_assert (die != NULL);
21465
21466 if (level >= max_level)
21467 return;
21468
21469 dump_die_shallow (f, indent, die);
21470
21471 if (die->child != NULL)
21472 {
21473 print_spaces (indent, f);
21474 fprintf_unfiltered (f, " Children:");
21475 if (level + 1 < max_level)
21476 {
21477 fprintf_unfiltered (f, "\n");
21478 dump_die_1 (f, level + 1, max_level, die->child);
21479 }
21480 else
21481 {
21482 fprintf_unfiltered (f,
21483 " [not printed, max nesting level reached]\n");
21484 }
21485 }
21486
21487 if (die->sibling != NULL && level > 0)
21488 {
21489 dump_die_1 (f, level, max_level, die->sibling);
21490 }
21491 }
21492
21493 /* This is called from the pdie macro in gdbinit.in.
21494 It's not static so gcc will keep a copy callable from gdb. */
21495
21496 void
21497 dump_die (struct die_info *die, int max_level)
21498 {
21499 dump_die_1 (gdb_stdlog, 0, max_level, die);
21500 }
21501
21502 static void
21503 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
21504 {
21505 void **slot;
21506
21507 slot = htab_find_slot_with_hash (cu->die_hash, die,
21508 to_underlying (die->sect_off),
21509 INSERT);
21510
21511 *slot = die;
21512 }
21513
21514 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
21515 required kind. */
21516
21517 static sect_offset
21518 dwarf2_get_ref_die_offset (const struct attribute *attr)
21519 {
21520 if (attr->form_is_ref ())
21521 return (sect_offset) DW_UNSND (attr);
21522
21523 complaint (_("unsupported die ref attribute form: '%s'"),
21524 dwarf_form_name (attr->form));
21525 return {};
21526 }
21527
21528 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
21529 * the value held by the attribute is not constant. */
21530
21531 static LONGEST
21532 dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
21533 {
21534 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
21535 return DW_SND (attr);
21536 else if (attr->form == DW_FORM_udata
21537 || attr->form == DW_FORM_data1
21538 || attr->form == DW_FORM_data2
21539 || attr->form == DW_FORM_data4
21540 || attr->form == DW_FORM_data8)
21541 return DW_UNSND (attr);
21542 else
21543 {
21544 /* For DW_FORM_data16 see attribute::form_is_constant. */
21545 complaint (_("Attribute value is not a constant (%s)"),
21546 dwarf_form_name (attr->form));
21547 return default_value;
21548 }
21549 }
21550
21551 /* Follow reference or signature attribute ATTR of SRC_DIE.
21552 On entry *REF_CU is the CU of SRC_DIE.
21553 On exit *REF_CU is the CU of the result. */
21554
21555 static struct die_info *
21556 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
21557 struct dwarf2_cu **ref_cu)
21558 {
21559 struct die_info *die;
21560
21561 if (attr->form_is_ref ())
21562 die = follow_die_ref (src_die, attr, ref_cu);
21563 else if (attr->form == DW_FORM_ref_sig8)
21564 die = follow_die_sig (src_die, attr, ref_cu);
21565 else
21566 {
21567 dump_die_for_error (src_die);
21568 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
21569 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
21570 }
21571
21572 return die;
21573 }
21574
21575 /* Follow reference OFFSET.
21576 On entry *REF_CU is the CU of the source die referencing OFFSET.
21577 On exit *REF_CU is the CU of the result.
21578 Returns NULL if OFFSET is invalid. */
21579
21580 static struct die_info *
21581 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
21582 struct dwarf2_cu **ref_cu)
21583 {
21584 struct die_info temp_die;
21585 struct dwarf2_cu *target_cu, *cu = *ref_cu;
21586 struct dwarf2_per_objfile *dwarf2_per_objfile
21587 = cu->per_cu->dwarf2_per_objfile;
21588
21589 gdb_assert (cu->per_cu != NULL);
21590
21591 target_cu = cu;
21592
21593 if (cu->per_cu->is_debug_types)
21594 {
21595 /* .debug_types CUs cannot reference anything outside their CU.
21596 If they need to, they have to reference a signatured type via
21597 DW_FORM_ref_sig8. */
21598 if (!cu->header.offset_in_cu_p (sect_off))
21599 return NULL;
21600 }
21601 else if (offset_in_dwz != cu->per_cu->is_dwz
21602 || !cu->header.offset_in_cu_p (sect_off))
21603 {
21604 struct dwarf2_per_cu_data *per_cu;
21605
21606 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
21607 dwarf2_per_objfile);
21608
21609 /* If necessary, add it to the queue and load its DIEs. */
21610 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
21611 load_full_comp_unit (per_cu, false, cu->language);
21612
21613 target_cu = per_cu->cu;
21614 }
21615 else if (cu->dies == NULL)
21616 {
21617 /* We're loading full DIEs during partial symbol reading. */
21618 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
21619 load_full_comp_unit (cu->per_cu, false, language_minimal);
21620 }
21621
21622 *ref_cu = target_cu;
21623 temp_die.sect_off = sect_off;
21624
21625 if (target_cu != cu)
21626 target_cu->ancestor = cu;
21627
21628 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
21629 &temp_die,
21630 to_underlying (sect_off));
21631 }
21632
21633 /* Follow reference attribute ATTR of SRC_DIE.
21634 On entry *REF_CU is the CU of SRC_DIE.
21635 On exit *REF_CU is the CU of the result. */
21636
21637 static struct die_info *
21638 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
21639 struct dwarf2_cu **ref_cu)
21640 {
21641 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
21642 struct dwarf2_cu *cu = *ref_cu;
21643 struct die_info *die;
21644
21645 die = follow_die_offset (sect_off,
21646 (attr->form == DW_FORM_GNU_ref_alt
21647 || cu->per_cu->is_dwz),
21648 ref_cu);
21649 if (!die)
21650 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
21651 "at %s [in module %s]"),
21652 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
21653 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
21654
21655 return die;
21656 }
21657
21658 /* See read.h. */
21659
21660 struct dwarf2_locexpr_baton
21661 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
21662 dwarf2_per_cu_data *per_cu,
21663 CORE_ADDR (*get_frame_pc) (void *baton),
21664 void *baton, bool resolve_abstract_p)
21665 {
21666 struct dwarf2_cu *cu;
21667 struct die_info *die;
21668 struct attribute *attr;
21669 struct dwarf2_locexpr_baton retval;
21670 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
21671 struct objfile *objfile = dwarf2_per_objfile->objfile;
21672
21673 if (per_cu->cu == NULL)
21674 load_cu (per_cu, false);
21675 cu = per_cu->cu;
21676 if (cu == NULL)
21677 {
21678 /* We shouldn't get here for a dummy CU, but don't crash on the user.
21679 Instead just throw an error, not much else we can do. */
21680 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
21681 sect_offset_str (sect_off), objfile_name (objfile));
21682 }
21683
21684 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
21685 if (!die)
21686 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
21687 sect_offset_str (sect_off), objfile_name (objfile));
21688
21689 attr = dwarf2_attr (die, DW_AT_location, cu);
21690 if (!attr && resolve_abstract_p
21691 && (dwarf2_per_objfile->abstract_to_concrete.find (die->sect_off)
21692 != dwarf2_per_objfile->abstract_to_concrete.end ()))
21693 {
21694 CORE_ADDR pc = (*get_frame_pc) (baton);
21695 CORE_ADDR baseaddr = objfile->text_section_offset ();
21696 struct gdbarch *gdbarch = get_objfile_arch (objfile);
21697
21698 for (const auto &cand_off
21699 : dwarf2_per_objfile->abstract_to_concrete[die->sect_off])
21700 {
21701 struct dwarf2_cu *cand_cu = cu;
21702 struct die_info *cand
21703 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
21704 if (!cand
21705 || !cand->parent
21706 || cand->parent->tag != DW_TAG_subprogram)
21707 continue;
21708
21709 CORE_ADDR pc_low, pc_high;
21710 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
21711 if (pc_low == ((CORE_ADDR) -1))
21712 continue;
21713 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
21714 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
21715 if (!(pc_low <= pc && pc < pc_high))
21716 continue;
21717
21718 die = cand;
21719 attr = dwarf2_attr (die, DW_AT_location, cu);
21720 break;
21721 }
21722 }
21723
21724 if (!attr)
21725 {
21726 /* DWARF: "If there is no such attribute, then there is no effect.".
21727 DATA is ignored if SIZE is 0. */
21728
21729 retval.data = NULL;
21730 retval.size = 0;
21731 }
21732 else if (attr->form_is_section_offset ())
21733 {
21734 struct dwarf2_loclist_baton loclist_baton;
21735 CORE_ADDR pc = (*get_frame_pc) (baton);
21736 size_t size;
21737
21738 fill_in_loclist_baton (cu, &loclist_baton, attr);
21739
21740 retval.data = dwarf2_find_location_expression (&loclist_baton,
21741 &size, pc);
21742 retval.size = size;
21743 }
21744 else
21745 {
21746 if (!attr->form_is_block ())
21747 error (_("Dwarf Error: DIE at %s referenced in module %s "
21748 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
21749 sect_offset_str (sect_off), objfile_name (objfile));
21750
21751 retval.data = DW_BLOCK (attr)->data;
21752 retval.size = DW_BLOCK (attr)->size;
21753 }
21754 retval.per_cu = cu->per_cu;
21755
21756 age_cached_comp_units (dwarf2_per_objfile);
21757
21758 return retval;
21759 }
21760
21761 /* See read.h. */
21762
21763 struct dwarf2_locexpr_baton
21764 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
21765 dwarf2_per_cu_data *per_cu,
21766 CORE_ADDR (*get_frame_pc) (void *baton),
21767 void *baton)
21768 {
21769 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
21770
21771 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
21772 }
21773
21774 /* Write a constant of a given type as target-ordered bytes into
21775 OBSTACK. */
21776
21777 static const gdb_byte *
21778 write_constant_as_bytes (struct obstack *obstack,
21779 enum bfd_endian byte_order,
21780 struct type *type,
21781 ULONGEST value,
21782 LONGEST *len)
21783 {
21784 gdb_byte *result;
21785
21786 *len = TYPE_LENGTH (type);
21787 result = (gdb_byte *) obstack_alloc (obstack, *len);
21788 store_unsigned_integer (result, *len, byte_order, value);
21789
21790 return result;
21791 }
21792
21793 /* See read.h. */
21794
21795 const gdb_byte *
21796 dwarf2_fetch_constant_bytes (sect_offset sect_off,
21797 dwarf2_per_cu_data *per_cu,
21798 obstack *obstack,
21799 LONGEST *len)
21800 {
21801 struct dwarf2_cu *cu;
21802 struct die_info *die;
21803 struct attribute *attr;
21804 const gdb_byte *result = NULL;
21805 struct type *type;
21806 LONGEST value;
21807 enum bfd_endian byte_order;
21808 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
21809
21810 if (per_cu->cu == NULL)
21811 load_cu (per_cu, false);
21812 cu = per_cu->cu;
21813 if (cu == NULL)
21814 {
21815 /* We shouldn't get here for a dummy CU, but don't crash on the user.
21816 Instead just throw an error, not much else we can do. */
21817 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
21818 sect_offset_str (sect_off), objfile_name (objfile));
21819 }
21820
21821 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
21822 if (!die)
21823 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
21824 sect_offset_str (sect_off), objfile_name (objfile));
21825
21826 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21827 if (attr == NULL)
21828 return NULL;
21829
21830 byte_order = (bfd_big_endian (objfile->obfd)
21831 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21832
21833 switch (attr->form)
21834 {
21835 case DW_FORM_addr:
21836 case DW_FORM_addrx:
21837 case DW_FORM_GNU_addr_index:
21838 {
21839 gdb_byte *tem;
21840
21841 *len = cu->header.addr_size;
21842 tem = (gdb_byte *) obstack_alloc (obstack, *len);
21843 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
21844 result = tem;
21845 }
21846 break;
21847 case DW_FORM_string:
21848 case DW_FORM_strp:
21849 case DW_FORM_strx:
21850 case DW_FORM_GNU_str_index:
21851 case DW_FORM_GNU_strp_alt:
21852 /* DW_STRING is already allocated on the objfile obstack, point
21853 directly to it. */
21854 result = (const gdb_byte *) DW_STRING (attr);
21855 *len = strlen (DW_STRING (attr));
21856 break;
21857 case DW_FORM_block1:
21858 case DW_FORM_block2:
21859 case DW_FORM_block4:
21860 case DW_FORM_block:
21861 case DW_FORM_exprloc:
21862 case DW_FORM_data16:
21863 result = DW_BLOCK (attr)->data;
21864 *len = DW_BLOCK (attr)->size;
21865 break;
21866
21867 /* The DW_AT_const_value attributes are supposed to carry the
21868 symbol's value "represented as it would be on the target
21869 architecture." By the time we get here, it's already been
21870 converted to host endianness, so we just need to sign- or
21871 zero-extend it as appropriate. */
21872 case DW_FORM_data1:
21873 type = die_type (die, cu);
21874 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
21875 if (result == NULL)
21876 result = write_constant_as_bytes (obstack, byte_order,
21877 type, value, len);
21878 break;
21879 case DW_FORM_data2:
21880 type = die_type (die, cu);
21881 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
21882 if (result == NULL)
21883 result = write_constant_as_bytes (obstack, byte_order,
21884 type, value, len);
21885 break;
21886 case DW_FORM_data4:
21887 type = die_type (die, cu);
21888 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
21889 if (result == NULL)
21890 result = write_constant_as_bytes (obstack, byte_order,
21891 type, value, len);
21892 break;
21893 case DW_FORM_data8:
21894 type = die_type (die, cu);
21895 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
21896 if (result == NULL)
21897 result = write_constant_as_bytes (obstack, byte_order,
21898 type, value, len);
21899 break;
21900
21901 case DW_FORM_sdata:
21902 case DW_FORM_implicit_const:
21903 type = die_type (die, cu);
21904 result = write_constant_as_bytes (obstack, byte_order,
21905 type, DW_SND (attr), len);
21906 break;
21907
21908 case DW_FORM_udata:
21909 type = die_type (die, cu);
21910 result = write_constant_as_bytes (obstack, byte_order,
21911 type, DW_UNSND (attr), len);
21912 break;
21913
21914 default:
21915 complaint (_("unsupported const value attribute form: '%s'"),
21916 dwarf_form_name (attr->form));
21917 break;
21918 }
21919
21920 return result;
21921 }
21922
21923 /* See read.h. */
21924
21925 struct type *
21926 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
21927 dwarf2_per_cu_data *per_cu)
21928 {
21929 struct dwarf2_cu *cu;
21930 struct die_info *die;
21931
21932 if (per_cu->cu == NULL)
21933 load_cu (per_cu, false);
21934 cu = per_cu->cu;
21935 if (!cu)
21936 return NULL;
21937
21938 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
21939 if (!die)
21940 return NULL;
21941
21942 return die_type (die, cu);
21943 }
21944
21945 /* See read.h. */
21946
21947 struct type *
21948 dwarf2_get_die_type (cu_offset die_offset,
21949 struct dwarf2_per_cu_data *per_cu)
21950 {
21951 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
21952 return get_die_type_at_offset (die_offset_sect, per_cu);
21953 }
21954
21955 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
21956 On entry *REF_CU is the CU of SRC_DIE.
21957 On exit *REF_CU is the CU of the result.
21958 Returns NULL if the referenced DIE isn't found. */
21959
21960 static struct die_info *
21961 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
21962 struct dwarf2_cu **ref_cu)
21963 {
21964 struct die_info temp_die;
21965 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
21966 struct die_info *die;
21967
21968 /* While it might be nice to assert sig_type->type == NULL here,
21969 we can get here for DW_AT_imported_declaration where we need
21970 the DIE not the type. */
21971
21972 /* If necessary, add it to the queue and load its DIEs. */
21973
21974 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
21975 read_signatured_type (sig_type);
21976
21977 sig_cu = sig_type->per_cu.cu;
21978 gdb_assert (sig_cu != NULL);
21979 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
21980 temp_die.sect_off = sig_type->type_offset_in_section;
21981 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
21982 to_underlying (temp_die.sect_off));
21983 if (die)
21984 {
21985 struct dwarf2_per_objfile *dwarf2_per_objfile
21986 = (*ref_cu)->per_cu->dwarf2_per_objfile;
21987
21988 /* For .gdb_index version 7 keep track of included TUs.
21989 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
21990 if (dwarf2_per_objfile->index_table != NULL
21991 && dwarf2_per_objfile->index_table->version <= 7)
21992 {
21993 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
21994 }
21995
21996 *ref_cu = sig_cu;
21997 if (sig_cu != cu)
21998 sig_cu->ancestor = cu;
21999
22000 return die;
22001 }
22002
22003 return NULL;
22004 }
22005
22006 /* Follow signatured type referenced by ATTR in SRC_DIE.
22007 On entry *REF_CU is the CU of SRC_DIE.
22008 On exit *REF_CU is the CU of the result.
22009 The result is the DIE of the type.
22010 If the referenced type cannot be found an error is thrown. */
22011
22012 static struct die_info *
22013 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
22014 struct dwarf2_cu **ref_cu)
22015 {
22016 ULONGEST signature = DW_SIGNATURE (attr);
22017 struct signatured_type *sig_type;
22018 struct die_info *die;
22019
22020 gdb_assert (attr->form == DW_FORM_ref_sig8);
22021
22022 sig_type = lookup_signatured_type (*ref_cu, signature);
22023 /* sig_type will be NULL if the signatured type is missing from
22024 the debug info. */
22025 if (sig_type == NULL)
22026 {
22027 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22028 " from DIE at %s [in module %s]"),
22029 hex_string (signature), sect_offset_str (src_die->sect_off),
22030 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
22031 }
22032
22033 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
22034 if (die == NULL)
22035 {
22036 dump_die_for_error (src_die);
22037 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22038 " from DIE at %s [in module %s]"),
22039 hex_string (signature), sect_offset_str (src_die->sect_off),
22040 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
22041 }
22042
22043 return die;
22044 }
22045
22046 /* Get the type specified by SIGNATURE referenced in DIE/CU,
22047 reading in and processing the type unit if necessary. */
22048
22049 static struct type *
22050 get_signatured_type (struct die_info *die, ULONGEST signature,
22051 struct dwarf2_cu *cu)
22052 {
22053 struct dwarf2_per_objfile *dwarf2_per_objfile
22054 = cu->per_cu->dwarf2_per_objfile;
22055 struct signatured_type *sig_type;
22056 struct dwarf2_cu *type_cu;
22057 struct die_info *type_die;
22058 struct type *type;
22059
22060 sig_type = lookup_signatured_type (cu, signature);
22061 /* sig_type will be NULL if the signatured type is missing from
22062 the debug info. */
22063 if (sig_type == NULL)
22064 {
22065 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22066 " from DIE at %s [in module %s]"),
22067 hex_string (signature), sect_offset_str (die->sect_off),
22068 objfile_name (dwarf2_per_objfile->objfile));
22069 return build_error_marker_type (cu, die);
22070 }
22071
22072 /* If we already know the type we're done. */
22073 if (sig_type->type != NULL)
22074 return sig_type->type;
22075
22076 type_cu = cu;
22077 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
22078 if (type_die != NULL)
22079 {
22080 /* N.B. We need to call get_die_type to ensure only one type for this DIE
22081 is created. This is important, for example, because for c++ classes
22082 we need TYPE_NAME set which is only done by new_symbol. Blech. */
22083 type = read_type_die (type_die, type_cu);
22084 if (type == NULL)
22085 {
22086 complaint (_("Dwarf Error: Cannot build signatured type %s"
22087 " referenced from DIE at %s [in module %s]"),
22088 hex_string (signature), sect_offset_str (die->sect_off),
22089 objfile_name (dwarf2_per_objfile->objfile));
22090 type = build_error_marker_type (cu, die);
22091 }
22092 }
22093 else
22094 {
22095 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22096 " from DIE at %s [in module %s]"),
22097 hex_string (signature), sect_offset_str (die->sect_off),
22098 objfile_name (dwarf2_per_objfile->objfile));
22099 type = build_error_marker_type (cu, die);
22100 }
22101 sig_type->type = type;
22102
22103 return type;
22104 }
22105
22106 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
22107 reading in and processing the type unit if necessary. */
22108
22109 static struct type *
22110 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
22111 struct dwarf2_cu *cu) /* ARI: editCase function */
22112 {
22113 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
22114 if (attr->form_is_ref ())
22115 {
22116 struct dwarf2_cu *type_cu = cu;
22117 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
22118
22119 return read_type_die (type_die, type_cu);
22120 }
22121 else if (attr->form == DW_FORM_ref_sig8)
22122 {
22123 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
22124 }
22125 else
22126 {
22127 struct dwarf2_per_objfile *dwarf2_per_objfile
22128 = cu->per_cu->dwarf2_per_objfile;
22129
22130 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
22131 " at %s [in module %s]"),
22132 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
22133 objfile_name (dwarf2_per_objfile->objfile));
22134 return build_error_marker_type (cu, die);
22135 }
22136 }
22137
22138 /* Load the DIEs associated with type unit PER_CU into memory. */
22139
22140 static void
22141 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
22142 {
22143 struct signatured_type *sig_type;
22144
22145 /* Caller is responsible for ensuring type_unit_groups don't get here. */
22146 gdb_assert (! per_cu->type_unit_group_p ());
22147
22148 /* We have the per_cu, but we need the signatured_type.
22149 Fortunately this is an easy translation. */
22150 gdb_assert (per_cu->is_debug_types);
22151 sig_type = (struct signatured_type *) per_cu;
22152
22153 gdb_assert (per_cu->cu == NULL);
22154
22155 read_signatured_type (sig_type);
22156
22157 gdb_assert (per_cu->cu != NULL);
22158 }
22159
22160 /* Read in a signatured type and build its CU and DIEs.
22161 If the type is a stub for the real type in a DWO file,
22162 read in the real type from the DWO file as well. */
22163
22164 static void
22165 read_signatured_type (struct signatured_type *sig_type)
22166 {
22167 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
22168
22169 gdb_assert (per_cu->is_debug_types);
22170 gdb_assert (per_cu->cu == NULL);
22171
22172 cutu_reader reader (per_cu, NULL, 0, false);
22173
22174 if (!reader.dummy_p)
22175 {
22176 struct dwarf2_cu *cu = reader.cu;
22177 const gdb_byte *info_ptr = reader.info_ptr;
22178
22179 gdb_assert (cu->die_hash == NULL);
22180 cu->die_hash =
22181 htab_create_alloc_ex (cu->header.length / 12,
22182 die_hash,
22183 die_eq,
22184 NULL,
22185 &cu->comp_unit_obstack,
22186 hashtab_obstack_allocate,
22187 dummy_obstack_deallocate);
22188
22189 if (reader.comp_unit_die->has_children)
22190 reader.comp_unit_die->child
22191 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
22192 reader.comp_unit_die);
22193 cu->dies = reader.comp_unit_die;
22194 /* comp_unit_die is not stored in die_hash, no need. */
22195
22196 /* We try not to read any attributes in this function, because
22197 not all CUs needed for references have been loaded yet, and
22198 symbol table processing isn't initialized. But we have to
22199 set the CU language, or we won't be able to build types
22200 correctly. Similarly, if we do not read the producer, we can
22201 not apply producer-specific interpretation. */
22202 prepare_one_comp_unit (cu, cu->dies, language_minimal);
22203
22204 reader.keep ();
22205 }
22206
22207 sig_type->per_cu.tu_read = 1;
22208 }
22209
22210 /* Decode simple location descriptions.
22211 Given a pointer to a dwarf block that defines a location, compute
22212 the location and return the value.
22213
22214 NOTE drow/2003-11-18: This function is called in two situations
22215 now: for the address of static or global variables (partial symbols
22216 only) and for offsets into structures which are expected to be
22217 (more or less) constant. The partial symbol case should go away,
22218 and only the constant case should remain. That will let this
22219 function complain more accurately. A few special modes are allowed
22220 without complaint for global variables (for instance, global
22221 register values and thread-local values).
22222
22223 A location description containing no operations indicates that the
22224 object is optimized out. The return value is 0 for that case.
22225 FIXME drow/2003-11-16: No callers check for this case any more; soon all
22226 callers will only want a very basic result and this can become a
22227 complaint.
22228
22229 Note that stack[0] is unused except as a default error return. */
22230
22231 static CORE_ADDR
22232 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
22233 {
22234 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22235 size_t i;
22236 size_t size = blk->size;
22237 const gdb_byte *data = blk->data;
22238 CORE_ADDR stack[64];
22239 int stacki;
22240 unsigned int bytes_read, unsnd;
22241 gdb_byte op;
22242
22243 i = 0;
22244 stacki = 0;
22245 stack[stacki] = 0;
22246 stack[++stacki] = 0;
22247
22248 while (i < size)
22249 {
22250 op = data[i++];
22251 switch (op)
22252 {
22253 case DW_OP_lit0:
22254 case DW_OP_lit1:
22255 case DW_OP_lit2:
22256 case DW_OP_lit3:
22257 case DW_OP_lit4:
22258 case DW_OP_lit5:
22259 case DW_OP_lit6:
22260 case DW_OP_lit7:
22261 case DW_OP_lit8:
22262 case DW_OP_lit9:
22263 case DW_OP_lit10:
22264 case DW_OP_lit11:
22265 case DW_OP_lit12:
22266 case DW_OP_lit13:
22267 case DW_OP_lit14:
22268 case DW_OP_lit15:
22269 case DW_OP_lit16:
22270 case DW_OP_lit17:
22271 case DW_OP_lit18:
22272 case DW_OP_lit19:
22273 case DW_OP_lit20:
22274 case DW_OP_lit21:
22275 case DW_OP_lit22:
22276 case DW_OP_lit23:
22277 case DW_OP_lit24:
22278 case DW_OP_lit25:
22279 case DW_OP_lit26:
22280 case DW_OP_lit27:
22281 case DW_OP_lit28:
22282 case DW_OP_lit29:
22283 case DW_OP_lit30:
22284 case DW_OP_lit31:
22285 stack[++stacki] = op - DW_OP_lit0;
22286 break;
22287
22288 case DW_OP_reg0:
22289 case DW_OP_reg1:
22290 case DW_OP_reg2:
22291 case DW_OP_reg3:
22292 case DW_OP_reg4:
22293 case DW_OP_reg5:
22294 case DW_OP_reg6:
22295 case DW_OP_reg7:
22296 case DW_OP_reg8:
22297 case DW_OP_reg9:
22298 case DW_OP_reg10:
22299 case DW_OP_reg11:
22300 case DW_OP_reg12:
22301 case DW_OP_reg13:
22302 case DW_OP_reg14:
22303 case DW_OP_reg15:
22304 case DW_OP_reg16:
22305 case DW_OP_reg17:
22306 case DW_OP_reg18:
22307 case DW_OP_reg19:
22308 case DW_OP_reg20:
22309 case DW_OP_reg21:
22310 case DW_OP_reg22:
22311 case DW_OP_reg23:
22312 case DW_OP_reg24:
22313 case DW_OP_reg25:
22314 case DW_OP_reg26:
22315 case DW_OP_reg27:
22316 case DW_OP_reg28:
22317 case DW_OP_reg29:
22318 case DW_OP_reg30:
22319 case DW_OP_reg31:
22320 stack[++stacki] = op - DW_OP_reg0;
22321 if (i < size)
22322 dwarf2_complex_location_expr_complaint ();
22323 break;
22324
22325 case DW_OP_regx:
22326 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
22327 i += bytes_read;
22328 stack[++stacki] = unsnd;
22329 if (i < size)
22330 dwarf2_complex_location_expr_complaint ();
22331 break;
22332
22333 case DW_OP_addr:
22334 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
22335 &bytes_read);
22336 i += bytes_read;
22337 break;
22338
22339 case DW_OP_const1u:
22340 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
22341 i += 1;
22342 break;
22343
22344 case DW_OP_const1s:
22345 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
22346 i += 1;
22347 break;
22348
22349 case DW_OP_const2u:
22350 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
22351 i += 2;
22352 break;
22353
22354 case DW_OP_const2s:
22355 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
22356 i += 2;
22357 break;
22358
22359 case DW_OP_const4u:
22360 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
22361 i += 4;
22362 break;
22363
22364 case DW_OP_const4s:
22365 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
22366 i += 4;
22367 break;
22368
22369 case DW_OP_const8u:
22370 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
22371 i += 8;
22372 break;
22373
22374 case DW_OP_constu:
22375 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
22376 &bytes_read);
22377 i += bytes_read;
22378 break;
22379
22380 case DW_OP_consts:
22381 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
22382 i += bytes_read;
22383 break;
22384
22385 case DW_OP_dup:
22386 stack[stacki + 1] = stack[stacki];
22387 stacki++;
22388 break;
22389
22390 case DW_OP_plus:
22391 stack[stacki - 1] += stack[stacki];
22392 stacki--;
22393 break;
22394
22395 case DW_OP_plus_uconst:
22396 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
22397 &bytes_read);
22398 i += bytes_read;
22399 break;
22400
22401 case DW_OP_minus:
22402 stack[stacki - 1] -= stack[stacki];
22403 stacki--;
22404 break;
22405
22406 case DW_OP_deref:
22407 /* If we're not the last op, then we definitely can't encode
22408 this using GDB's address_class enum. This is valid for partial
22409 global symbols, although the variable's address will be bogus
22410 in the psymtab. */
22411 if (i < size)
22412 dwarf2_complex_location_expr_complaint ();
22413 break;
22414
22415 case DW_OP_GNU_push_tls_address:
22416 case DW_OP_form_tls_address:
22417 /* The top of the stack has the offset from the beginning
22418 of the thread control block at which the variable is located. */
22419 /* Nothing should follow this operator, so the top of stack would
22420 be returned. */
22421 /* This is valid for partial global symbols, but the variable's
22422 address will be bogus in the psymtab. Make it always at least
22423 non-zero to not look as a variable garbage collected by linker
22424 which have DW_OP_addr 0. */
22425 if (i < size)
22426 dwarf2_complex_location_expr_complaint ();
22427 stack[stacki]++;
22428 break;
22429
22430 case DW_OP_GNU_uninit:
22431 break;
22432
22433 case DW_OP_addrx:
22434 case DW_OP_GNU_addr_index:
22435 case DW_OP_GNU_const_index:
22436 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
22437 &bytes_read);
22438 i += bytes_read;
22439 break;
22440
22441 default:
22442 {
22443 const char *name = get_DW_OP_name (op);
22444
22445 if (name)
22446 complaint (_("unsupported stack op: '%s'"),
22447 name);
22448 else
22449 complaint (_("unsupported stack op: '%02x'"),
22450 op);
22451 }
22452
22453 return (stack[stacki]);
22454 }
22455
22456 /* Enforce maximum stack depth of SIZE-1 to avoid writing
22457 outside of the allocated space. Also enforce minimum>0. */
22458 if (stacki >= ARRAY_SIZE (stack) - 1)
22459 {
22460 complaint (_("location description stack overflow"));
22461 return 0;
22462 }
22463
22464 if (stacki <= 0)
22465 {
22466 complaint (_("location description stack underflow"));
22467 return 0;
22468 }
22469 }
22470 return (stack[stacki]);
22471 }
22472
22473 /* memory allocation interface */
22474
22475 static struct dwarf_block *
22476 dwarf_alloc_block (struct dwarf2_cu *cu)
22477 {
22478 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
22479 }
22480
22481 static struct die_info *
22482 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
22483 {
22484 struct die_info *die;
22485 size_t size = sizeof (struct die_info);
22486
22487 if (num_attrs > 1)
22488 size += (num_attrs - 1) * sizeof (struct attribute);
22489
22490 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
22491 memset (die, 0, sizeof (struct die_info));
22492 return (die);
22493 }
22494
22495 \f
22496
22497 /* Macro support. */
22498
22499 /* An overload of dwarf_decode_macros that finds the correct section
22500 and ensures it is read in before calling the other overload. */
22501
22502 static void
22503 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
22504 int section_is_gnu)
22505 {
22506 struct dwarf2_per_objfile *dwarf2_per_objfile
22507 = cu->per_cu->dwarf2_per_objfile;
22508 struct objfile *objfile = dwarf2_per_objfile->objfile;
22509 const struct line_header *lh = cu->line_header;
22510 unsigned int offset_size = cu->header.offset_size;
22511 struct dwarf2_section_info *section;
22512 const char *section_name;
22513
22514 if (cu->dwo_unit != nullptr)
22515 {
22516 if (section_is_gnu)
22517 {
22518 section = &cu->dwo_unit->dwo_file->sections.macro;
22519 section_name = ".debug_macro.dwo";
22520 }
22521 else
22522 {
22523 section = &cu->dwo_unit->dwo_file->sections.macinfo;
22524 section_name = ".debug_macinfo.dwo";
22525 }
22526 }
22527 else
22528 {
22529 if (section_is_gnu)
22530 {
22531 section = &dwarf2_per_objfile->macro;
22532 section_name = ".debug_macro";
22533 }
22534 else
22535 {
22536 section = &dwarf2_per_objfile->macinfo;
22537 section_name = ".debug_macinfo";
22538 }
22539 }
22540
22541 section->read (objfile);
22542 if (section->buffer == nullptr)
22543 {
22544 complaint (_("missing %s section"), section_name);
22545 return;
22546 }
22547
22548 buildsym_compunit *builder = cu->get_builder ();
22549
22550 dwarf_decode_macros (dwarf2_per_objfile, builder, section, lh,
22551 offset_size, offset, section_is_gnu);
22552 }
22553
22554 /* Return the .debug_loc section to use for CU.
22555 For DWO files use .debug_loc.dwo. */
22556
22557 static struct dwarf2_section_info *
22558 cu_debug_loc_section (struct dwarf2_cu *cu)
22559 {
22560 struct dwarf2_per_objfile *dwarf2_per_objfile
22561 = cu->per_cu->dwarf2_per_objfile;
22562
22563 if (cu->dwo_unit)
22564 {
22565 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
22566
22567 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
22568 }
22569 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
22570 : &dwarf2_per_objfile->loc);
22571 }
22572
22573 /* A helper function that fills in a dwarf2_loclist_baton. */
22574
22575 static void
22576 fill_in_loclist_baton (struct dwarf2_cu *cu,
22577 struct dwarf2_loclist_baton *baton,
22578 const struct attribute *attr)
22579 {
22580 struct dwarf2_per_objfile *dwarf2_per_objfile
22581 = cu->per_cu->dwarf2_per_objfile;
22582 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
22583
22584 section->read (dwarf2_per_objfile->objfile);
22585
22586 baton->per_cu = cu->per_cu;
22587 gdb_assert (baton->per_cu);
22588 /* We don't know how long the location list is, but make sure we
22589 don't run off the edge of the section. */
22590 baton->size = section->size - DW_UNSND (attr);
22591 baton->data = section->buffer + DW_UNSND (attr);
22592 if (cu->base_address.has_value ())
22593 baton->base_address = *cu->base_address;
22594 else
22595 baton->base_address = 0;
22596 baton->from_dwo = cu->dwo_unit != NULL;
22597 }
22598
22599 static void
22600 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
22601 struct dwarf2_cu *cu, int is_block)
22602 {
22603 struct dwarf2_per_objfile *dwarf2_per_objfile
22604 = cu->per_cu->dwarf2_per_objfile;
22605 struct objfile *objfile = dwarf2_per_objfile->objfile;
22606 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
22607
22608 if (attr->form_is_section_offset ()
22609 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
22610 the section. If so, fall through to the complaint in the
22611 other branch. */
22612 && DW_UNSND (attr) < section->get_size (objfile))
22613 {
22614 struct dwarf2_loclist_baton *baton;
22615
22616 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
22617
22618 fill_in_loclist_baton (cu, baton, attr);
22619
22620 if (!cu->base_address.has_value ())
22621 complaint (_("Location list used without "
22622 "specifying the CU base address."));
22623
22624 SYMBOL_ACLASS_INDEX (sym) = (is_block
22625 ? dwarf2_loclist_block_index
22626 : dwarf2_loclist_index);
22627 SYMBOL_LOCATION_BATON (sym) = baton;
22628 }
22629 else
22630 {
22631 struct dwarf2_locexpr_baton *baton;
22632
22633 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
22634 baton->per_cu = cu->per_cu;
22635 gdb_assert (baton->per_cu);
22636
22637 if (attr->form_is_block ())
22638 {
22639 /* Note that we're just copying the block's data pointer
22640 here, not the actual data. We're still pointing into the
22641 info_buffer for SYM's objfile; right now we never release
22642 that buffer, but when we do clean up properly this may
22643 need to change. */
22644 baton->size = DW_BLOCK (attr)->size;
22645 baton->data = DW_BLOCK (attr)->data;
22646 }
22647 else
22648 {
22649 dwarf2_invalid_attrib_class_complaint ("location description",
22650 sym->natural_name ());
22651 baton->size = 0;
22652 }
22653
22654 SYMBOL_ACLASS_INDEX (sym) = (is_block
22655 ? dwarf2_locexpr_block_index
22656 : dwarf2_locexpr_index);
22657 SYMBOL_LOCATION_BATON (sym) = baton;
22658 }
22659 }
22660
22661 /* See read.h. */
22662
22663 struct objfile *
22664 dwarf2_per_cu_data::objfile () const
22665 {
22666 struct objfile *objfile = dwarf2_per_objfile->objfile;
22667
22668 /* Return the master objfile, so that we can report and look up the
22669 correct file containing this variable. */
22670 if (objfile->separate_debug_objfile_backlink)
22671 objfile = objfile->separate_debug_objfile_backlink;
22672
22673 return objfile;
22674 }
22675
22676 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
22677 (CU_HEADERP is unused in such case) or prepare a temporary copy at
22678 CU_HEADERP first. */
22679
22680 static const struct comp_unit_head *
22681 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
22682 const struct dwarf2_per_cu_data *per_cu)
22683 {
22684 const gdb_byte *info_ptr;
22685
22686 if (per_cu->cu)
22687 return &per_cu->cu->header;
22688
22689 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
22690
22691 memset (cu_headerp, 0, sizeof (*cu_headerp));
22692 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
22693 rcuh_kind::COMPILE);
22694
22695 return cu_headerp;
22696 }
22697
22698 /* See read.h. */
22699
22700 int
22701 dwarf2_per_cu_data::addr_size () const
22702 {
22703 struct comp_unit_head cu_header_local;
22704 const struct comp_unit_head *cu_headerp;
22705
22706 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
22707
22708 return cu_headerp->addr_size;
22709 }
22710
22711 /* See read.h. */
22712
22713 int
22714 dwarf2_per_cu_data::offset_size () const
22715 {
22716 struct comp_unit_head cu_header_local;
22717 const struct comp_unit_head *cu_headerp;
22718
22719 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
22720
22721 return cu_headerp->offset_size;
22722 }
22723
22724 /* See read.h. */
22725
22726 int
22727 dwarf2_per_cu_data::ref_addr_size () const
22728 {
22729 struct comp_unit_head cu_header_local;
22730 const struct comp_unit_head *cu_headerp;
22731
22732 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
22733
22734 if (cu_headerp->version == 2)
22735 return cu_headerp->addr_size;
22736 else
22737 return cu_headerp->offset_size;
22738 }
22739
22740 /* See read.h. */
22741
22742 CORE_ADDR
22743 dwarf2_per_cu_data::text_offset () const
22744 {
22745 struct objfile *objfile = dwarf2_per_objfile->objfile;
22746
22747 return objfile->text_section_offset ();
22748 }
22749
22750 /* See read.h. */
22751
22752 struct type *
22753 dwarf2_per_cu_data::addr_type () const
22754 {
22755 struct objfile *objfile = dwarf2_per_objfile->objfile;
22756 struct type *void_type = objfile_type (objfile)->builtin_void;
22757 struct type *addr_type = lookup_pointer_type (void_type);
22758 int addr_size = this->addr_size ();
22759
22760 if (TYPE_LENGTH (addr_type) == addr_size)
22761 return addr_type;
22762
22763 addr_type = addr_sized_int_type (TYPE_UNSIGNED (addr_type));
22764 return addr_type;
22765 }
22766
22767 /* A helper function for dwarf2_find_containing_comp_unit that returns
22768 the index of the result, and that searches a vector. It will
22769 return a result even if the offset in question does not actually
22770 occur in any CU. This is separate so that it can be unit
22771 tested. */
22772
22773 static int
22774 dwarf2_find_containing_comp_unit
22775 (sect_offset sect_off,
22776 unsigned int offset_in_dwz,
22777 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
22778 {
22779 int low, high;
22780
22781 low = 0;
22782 high = all_comp_units.size () - 1;
22783 while (high > low)
22784 {
22785 struct dwarf2_per_cu_data *mid_cu;
22786 int mid = low + (high - low) / 2;
22787
22788 mid_cu = all_comp_units[mid];
22789 if (mid_cu->is_dwz > offset_in_dwz
22790 || (mid_cu->is_dwz == offset_in_dwz
22791 && mid_cu->sect_off + mid_cu->length > sect_off))
22792 high = mid;
22793 else
22794 low = mid + 1;
22795 }
22796 gdb_assert (low == high);
22797 return low;
22798 }
22799
22800 /* Locate the .debug_info compilation unit from CU's objfile which contains
22801 the DIE at OFFSET. Raises an error on failure. */
22802
22803 static struct dwarf2_per_cu_data *
22804 dwarf2_find_containing_comp_unit (sect_offset sect_off,
22805 unsigned int offset_in_dwz,
22806 struct dwarf2_per_objfile *dwarf2_per_objfile)
22807 {
22808 int low
22809 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
22810 dwarf2_per_objfile->all_comp_units);
22811 struct dwarf2_per_cu_data *this_cu
22812 = dwarf2_per_objfile->all_comp_units[low];
22813
22814 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
22815 {
22816 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
22817 error (_("Dwarf Error: could not find partial DIE containing "
22818 "offset %s [in module %s]"),
22819 sect_offset_str (sect_off),
22820 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
22821
22822 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
22823 <= sect_off);
22824 return dwarf2_per_objfile->all_comp_units[low-1];
22825 }
22826 else
22827 {
22828 if (low == dwarf2_per_objfile->all_comp_units.size () - 1
22829 && sect_off >= this_cu->sect_off + this_cu->length)
22830 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
22831 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
22832 return this_cu;
22833 }
22834 }
22835
22836 #if GDB_SELF_TEST
22837
22838 namespace selftests {
22839 namespace find_containing_comp_unit {
22840
22841 static void
22842 run_test ()
22843 {
22844 struct dwarf2_per_cu_data one {};
22845 struct dwarf2_per_cu_data two {};
22846 struct dwarf2_per_cu_data three {};
22847 struct dwarf2_per_cu_data four {};
22848
22849 one.length = 5;
22850 two.sect_off = sect_offset (one.length);
22851 two.length = 7;
22852
22853 three.length = 5;
22854 three.is_dwz = 1;
22855 four.sect_off = sect_offset (three.length);
22856 four.length = 7;
22857 four.is_dwz = 1;
22858
22859 std::vector<dwarf2_per_cu_data *> units;
22860 units.push_back (&one);
22861 units.push_back (&two);
22862 units.push_back (&three);
22863 units.push_back (&four);
22864
22865 int result;
22866
22867 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
22868 SELF_CHECK (units[result] == &one);
22869 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
22870 SELF_CHECK (units[result] == &one);
22871 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
22872 SELF_CHECK (units[result] == &two);
22873
22874 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
22875 SELF_CHECK (units[result] == &three);
22876 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
22877 SELF_CHECK (units[result] == &three);
22878 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
22879 SELF_CHECK (units[result] == &four);
22880 }
22881
22882 }
22883 }
22884
22885 #endif /* GDB_SELF_TEST */
22886
22887 /* Initialize dwarf2_cu CU, owned by PER_CU. */
22888
22889 dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
22890 : per_cu (per_cu_),
22891 mark (false),
22892 has_loclist (false),
22893 checked_producer (false),
22894 producer_is_gxx_lt_4_6 (false),
22895 producer_is_gcc_lt_4_3 (false),
22896 producer_is_icc (false),
22897 producer_is_icc_lt_14 (false),
22898 producer_is_codewarrior (false),
22899 processing_has_namespace_info (false)
22900 {
22901 per_cu->cu = this;
22902 }
22903
22904 /* Destroy a dwarf2_cu. */
22905
22906 dwarf2_cu::~dwarf2_cu ()
22907 {
22908 per_cu->cu = NULL;
22909 }
22910
22911 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
22912
22913 static void
22914 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
22915 enum language pretend_language)
22916 {
22917 struct attribute *attr;
22918
22919 /* Set the language we're debugging. */
22920 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
22921 if (attr != nullptr)
22922 set_cu_language (DW_UNSND (attr), cu);
22923 else
22924 {
22925 cu->language = pretend_language;
22926 cu->language_defn = language_def (cu->language);
22927 }
22928
22929 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
22930 }
22931
22932 /* Increase the age counter on each cached compilation unit, and free
22933 any that are too old. */
22934
22935 static void
22936 age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
22937 {
22938 struct dwarf2_per_cu_data *per_cu, **last_chain;
22939
22940 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
22941 per_cu = dwarf2_per_objfile->read_in_chain;
22942 while (per_cu != NULL)
22943 {
22944 per_cu->cu->last_used ++;
22945 if (per_cu->cu->last_used <= dwarf_max_cache_age)
22946 dwarf2_mark (per_cu->cu);
22947 per_cu = per_cu->cu->read_in_chain;
22948 }
22949
22950 per_cu = dwarf2_per_objfile->read_in_chain;
22951 last_chain = &dwarf2_per_objfile->read_in_chain;
22952 while (per_cu != NULL)
22953 {
22954 struct dwarf2_per_cu_data *next_cu;
22955
22956 next_cu = per_cu->cu->read_in_chain;
22957
22958 if (!per_cu->cu->mark)
22959 {
22960 delete per_cu->cu;
22961 *last_chain = next_cu;
22962 }
22963 else
22964 last_chain = &per_cu->cu->read_in_chain;
22965
22966 per_cu = next_cu;
22967 }
22968 }
22969
22970 /* Remove a single compilation unit from the cache. */
22971
22972 static void
22973 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
22974 {
22975 struct dwarf2_per_cu_data *per_cu, **last_chain;
22976 struct dwarf2_per_objfile *dwarf2_per_objfile
22977 = target_per_cu->dwarf2_per_objfile;
22978
22979 per_cu = dwarf2_per_objfile->read_in_chain;
22980 last_chain = &dwarf2_per_objfile->read_in_chain;
22981 while (per_cu != NULL)
22982 {
22983 struct dwarf2_per_cu_data *next_cu;
22984
22985 next_cu = per_cu->cu->read_in_chain;
22986
22987 if (per_cu == target_per_cu)
22988 {
22989 delete per_cu->cu;
22990 per_cu->cu = NULL;
22991 *last_chain = next_cu;
22992 break;
22993 }
22994 else
22995 last_chain = &per_cu->cu->read_in_chain;
22996
22997 per_cu = next_cu;
22998 }
22999 }
23000
23001 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
23002 We store these in a hash table separate from the DIEs, and preserve them
23003 when the DIEs are flushed out of cache.
23004
23005 The CU "per_cu" pointer is needed because offset alone is not enough to
23006 uniquely identify the type. A file may have multiple .debug_types sections,
23007 or the type may come from a DWO file. Furthermore, while it's more logical
23008 to use per_cu->section+offset, with Fission the section with the data is in
23009 the DWO file but we don't know that section at the point we need it.
23010 We have to use something in dwarf2_per_cu_data (or the pointer to it)
23011 because we can enter the lookup routine, get_die_type_at_offset, from
23012 outside this file, and thus won't necessarily have PER_CU->cu.
23013 Fortunately, PER_CU is stable for the life of the objfile. */
23014
23015 struct dwarf2_per_cu_offset_and_type
23016 {
23017 const struct dwarf2_per_cu_data *per_cu;
23018 sect_offset sect_off;
23019 struct type *type;
23020 };
23021
23022 /* Hash function for a dwarf2_per_cu_offset_and_type. */
23023
23024 static hashval_t
23025 per_cu_offset_and_type_hash (const void *item)
23026 {
23027 const struct dwarf2_per_cu_offset_and_type *ofs
23028 = (const struct dwarf2_per_cu_offset_and_type *) item;
23029
23030 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
23031 }
23032
23033 /* Equality function for a dwarf2_per_cu_offset_and_type. */
23034
23035 static int
23036 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
23037 {
23038 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
23039 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
23040 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
23041 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
23042
23043 return (ofs_lhs->per_cu == ofs_rhs->per_cu
23044 && ofs_lhs->sect_off == ofs_rhs->sect_off);
23045 }
23046
23047 /* Set the type associated with DIE to TYPE. Save it in CU's hash
23048 table if necessary. For convenience, return TYPE.
23049
23050 The DIEs reading must have careful ordering to:
23051 * Not cause infinite loops trying to read in DIEs as a prerequisite for
23052 reading current DIE.
23053 * Not trying to dereference contents of still incompletely read in types
23054 while reading in other DIEs.
23055 * Enable referencing still incompletely read in types just by a pointer to
23056 the type without accessing its fields.
23057
23058 Therefore caller should follow these rules:
23059 * Try to fetch any prerequisite types we may need to build this DIE type
23060 before building the type and calling set_die_type.
23061 * After building type call set_die_type for current DIE as soon as
23062 possible before fetching more types to complete the current type.
23063 * Make the type as complete as possible before fetching more types. */
23064
23065 static struct type *
23066 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
23067 {
23068 struct dwarf2_per_objfile *dwarf2_per_objfile
23069 = cu->per_cu->dwarf2_per_objfile;
23070 struct dwarf2_per_cu_offset_and_type **slot, ofs;
23071 struct objfile *objfile = dwarf2_per_objfile->objfile;
23072 struct attribute *attr;
23073 struct dynamic_prop prop;
23074
23075 /* For Ada types, make sure that the gnat-specific data is always
23076 initialized (if not already set). There are a few types where
23077 we should not be doing so, because the type-specific area is
23078 already used to hold some other piece of info (eg: TYPE_CODE_FLT
23079 where the type-specific area is used to store the floatformat).
23080 But this is not a problem, because the gnat-specific information
23081 is actually not needed for these types. */
23082 if (need_gnat_info (cu)
23083 && TYPE_CODE (type) != TYPE_CODE_FUNC
23084 && TYPE_CODE (type) != TYPE_CODE_FLT
23085 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
23086 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
23087 && TYPE_CODE (type) != TYPE_CODE_METHOD
23088 && !HAVE_GNAT_AUX_INFO (type))
23089 INIT_GNAT_SPECIFIC (type);
23090
23091 /* Read DW_AT_allocated and set in type. */
23092 attr = dwarf2_attr (die, DW_AT_allocated, cu);
23093 if (attr != NULL && attr->form_is_block ())
23094 {
23095 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
23096 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
23097 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
23098 }
23099 else if (attr != NULL)
23100 {
23101 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
23102 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23103 sect_offset_str (die->sect_off));
23104 }
23105
23106 /* Read DW_AT_associated and set in type. */
23107 attr = dwarf2_attr (die, DW_AT_associated, cu);
23108 if (attr != NULL && attr->form_is_block ())
23109 {
23110 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
23111 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
23112 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
23113 }
23114 else if (attr != NULL)
23115 {
23116 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
23117 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23118 sect_offset_str (die->sect_off));
23119 }
23120
23121 /* Read DW_AT_data_location and set in type. */
23122 attr = dwarf2_attr (die, DW_AT_data_location, cu);
23123 if (attr_to_dynamic_prop (attr, die, cu, &prop,
23124 cu->per_cu->addr_type ()))
23125 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
23126
23127 if (dwarf2_per_objfile->die_type_hash == NULL)
23128 dwarf2_per_objfile->die_type_hash
23129 = htab_up (htab_create_alloc (127,
23130 per_cu_offset_and_type_hash,
23131 per_cu_offset_and_type_eq,
23132 NULL, xcalloc, xfree));
23133
23134 ofs.per_cu = cu->per_cu;
23135 ofs.sect_off = die->sect_off;
23136 ofs.type = type;
23137 slot = (struct dwarf2_per_cu_offset_and_type **)
23138 htab_find_slot (dwarf2_per_objfile->die_type_hash.get (), &ofs, INSERT);
23139 if (*slot)
23140 complaint (_("A problem internal to GDB: DIE %s has type already set"),
23141 sect_offset_str (die->sect_off));
23142 *slot = XOBNEW (&objfile->objfile_obstack,
23143 struct dwarf2_per_cu_offset_and_type);
23144 **slot = ofs;
23145 return type;
23146 }
23147
23148 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
23149 or return NULL if the die does not have a saved type. */
23150
23151 static struct type *
23152 get_die_type_at_offset (sect_offset sect_off,
23153 struct dwarf2_per_cu_data *per_cu)
23154 {
23155 struct dwarf2_per_cu_offset_and_type *slot, ofs;
23156 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
23157
23158 if (dwarf2_per_objfile->die_type_hash == NULL)
23159 return NULL;
23160
23161 ofs.per_cu = per_cu;
23162 ofs.sect_off = sect_off;
23163 slot = ((struct dwarf2_per_cu_offset_and_type *)
23164 htab_find (dwarf2_per_objfile->die_type_hash.get (), &ofs));
23165 if (slot)
23166 return slot->type;
23167 else
23168 return NULL;
23169 }
23170
23171 /* Look up the type for DIE in CU in die_type_hash,
23172 or return NULL if DIE does not have a saved type. */
23173
23174 static struct type *
23175 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
23176 {
23177 return get_die_type_at_offset (die->sect_off, cu->per_cu);
23178 }
23179
23180 /* Add a dependence relationship from CU to REF_PER_CU. */
23181
23182 static void
23183 dwarf2_add_dependence (struct dwarf2_cu *cu,
23184 struct dwarf2_per_cu_data *ref_per_cu)
23185 {
23186 void **slot;
23187
23188 if (cu->dependencies == NULL)
23189 cu->dependencies
23190 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
23191 NULL, &cu->comp_unit_obstack,
23192 hashtab_obstack_allocate,
23193 dummy_obstack_deallocate);
23194
23195 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
23196 if (*slot == NULL)
23197 *slot = ref_per_cu;
23198 }
23199
23200 /* Subroutine of dwarf2_mark to pass to htab_traverse.
23201 Set the mark field in every compilation unit in the
23202 cache that we must keep because we are keeping CU. */
23203
23204 static int
23205 dwarf2_mark_helper (void **slot, void *data)
23206 {
23207 struct dwarf2_per_cu_data *per_cu;
23208
23209 per_cu = (struct dwarf2_per_cu_data *) *slot;
23210
23211 /* cu->dependencies references may not yet have been ever read if QUIT aborts
23212 reading of the chain. As such dependencies remain valid it is not much
23213 useful to track and undo them during QUIT cleanups. */
23214 if (per_cu->cu == NULL)
23215 return 1;
23216
23217 if (per_cu->cu->mark)
23218 return 1;
23219 per_cu->cu->mark = true;
23220
23221 if (per_cu->cu->dependencies != NULL)
23222 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
23223
23224 return 1;
23225 }
23226
23227 /* Set the mark field in CU and in every other compilation unit in the
23228 cache that we must keep because we are keeping CU. */
23229
23230 static void
23231 dwarf2_mark (struct dwarf2_cu *cu)
23232 {
23233 if (cu->mark)
23234 return;
23235 cu->mark = true;
23236 if (cu->dependencies != NULL)
23237 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
23238 }
23239
23240 static void
23241 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
23242 {
23243 while (per_cu)
23244 {
23245 per_cu->cu->mark = false;
23246 per_cu = per_cu->cu->read_in_chain;
23247 }
23248 }
23249
23250 /* Trivial hash function for partial_die_info: the hash value of a DIE
23251 is its offset in .debug_info for this objfile. */
23252
23253 static hashval_t
23254 partial_die_hash (const void *item)
23255 {
23256 const struct partial_die_info *part_die
23257 = (const struct partial_die_info *) item;
23258
23259 return to_underlying (part_die->sect_off);
23260 }
23261
23262 /* Trivial comparison function for partial_die_info structures: two DIEs
23263 are equal if they have the same offset. */
23264
23265 static int
23266 partial_die_eq (const void *item_lhs, const void *item_rhs)
23267 {
23268 const struct partial_die_info *part_die_lhs
23269 = (const struct partial_die_info *) item_lhs;
23270 const struct partial_die_info *part_die_rhs
23271 = (const struct partial_die_info *) item_rhs;
23272
23273 return part_die_lhs->sect_off == part_die_rhs->sect_off;
23274 }
23275
23276 struct cmd_list_element *set_dwarf_cmdlist;
23277 struct cmd_list_element *show_dwarf_cmdlist;
23278
23279 static void
23280 set_dwarf_cmd (const char *args, int from_tty)
23281 {
23282 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
23283 gdb_stdout);
23284 }
23285
23286 static void
23287 show_dwarf_cmd (const char *args, int from_tty)
23288 {
23289 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
23290 }
23291
23292 static void
23293 show_check_physname (struct ui_file *file, int from_tty,
23294 struct cmd_list_element *c, const char *value)
23295 {
23296 fprintf_filtered (file,
23297 _("Whether to check \"physname\" is %s.\n"),
23298 value);
23299 }
23300
23301 void _initialize_dwarf2_read ();
23302 void
23303 _initialize_dwarf2_read ()
23304 {
23305 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
23306 Set DWARF specific variables.\n\
23307 Configure DWARF variables such as the cache size."),
23308 &set_dwarf_cmdlist, "maintenance set dwarf ",
23309 0/*allow-unknown*/, &maintenance_set_cmdlist);
23310
23311 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
23312 Show DWARF specific variables.\n\
23313 Show DWARF variables such as the cache size."),
23314 &show_dwarf_cmdlist, "maintenance show dwarf ",
23315 0/*allow-unknown*/, &maintenance_show_cmdlist);
23316
23317 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
23318 &dwarf_max_cache_age, _("\
23319 Set the upper bound on the age of cached DWARF compilation units."), _("\
23320 Show the upper bound on the age of cached DWARF compilation units."), _("\
23321 A higher limit means that cached compilation units will be stored\n\
23322 in memory longer, and more total memory will be used. Zero disables\n\
23323 caching, which can slow down startup."),
23324 NULL,
23325 show_dwarf_max_cache_age,
23326 &set_dwarf_cmdlist,
23327 &show_dwarf_cmdlist);
23328
23329 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
23330 Set debugging of the DWARF reader."), _("\
23331 Show debugging of the DWARF reader."), _("\
23332 When enabled (non-zero), debugging messages are printed during DWARF\n\
23333 reading and symtab expansion. A value of 1 (one) provides basic\n\
23334 information. A value greater than 1 provides more verbose information."),
23335 NULL,
23336 NULL,
23337 &setdebuglist, &showdebuglist);
23338
23339 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
23340 Set debugging of the DWARF DIE reader."), _("\
23341 Show debugging of the DWARF DIE reader."), _("\
23342 When enabled (non-zero), DIEs are dumped after they are read in.\n\
23343 The value is the maximum depth to print."),
23344 NULL,
23345 NULL,
23346 &setdebuglist, &showdebuglist);
23347
23348 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
23349 Set debugging of the dwarf line reader."), _("\
23350 Show debugging of the dwarf line reader."), _("\
23351 When enabled (non-zero), line number entries are dumped as they are read in.\n\
23352 A value of 1 (one) provides basic information.\n\
23353 A value greater than 1 provides more verbose information."),
23354 NULL,
23355 NULL,
23356 &setdebuglist, &showdebuglist);
23357
23358 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
23359 Set cross-checking of \"physname\" code against demangler."), _("\
23360 Show cross-checking of \"physname\" code against demangler."), _("\
23361 When enabled, GDB's internal \"physname\" code is checked against\n\
23362 the demangler."),
23363 NULL, show_check_physname,
23364 &setdebuglist, &showdebuglist);
23365
23366 add_setshow_boolean_cmd ("use-deprecated-index-sections",
23367 no_class, &use_deprecated_index_sections, _("\
23368 Set whether to use deprecated gdb_index sections."), _("\
23369 Show whether to use deprecated gdb_index sections."), _("\
23370 When enabled, deprecated .gdb_index sections are used anyway.\n\
23371 Normally they are ignored either because of a missing feature or\n\
23372 performance issue.\n\
23373 Warning: This option must be enabled before gdb reads the file."),
23374 NULL,
23375 NULL,
23376 &setlist, &showlist);
23377
23378 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
23379 &dwarf2_locexpr_funcs);
23380 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
23381 &dwarf2_loclist_funcs);
23382
23383 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
23384 &dwarf2_block_frame_base_locexpr_funcs);
23385 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
23386 &dwarf2_block_frame_base_loclist_funcs);
23387
23388 #if GDB_SELF_TEST
23389 selftests::register_test ("dw2_expand_symtabs_matching",
23390 selftests::dw2_expand_symtabs_matching::run_test);
23391 selftests::register_test ("dwarf2_find_containing_comp_unit",
23392 selftests::find_containing_comp_unit::run_test);
23393 #endif
23394 }
This page took 0.495923 seconds and 5 git commands to generate.